mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-09 13:10:17 +00:00
change enum back
This commit is contained in:
parent
a9376bd80f
commit
240cec3559
@ -41,7 +41,7 @@ std::cout << py_cast<int>(vm, obj); // 123
|
|||||||
Compile a source string into a code object
|
Compile a source string into a code object
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
CodeObject_ co = vm->compile("print('Hello!')", "main.py", PK_EXEC_MODE);
|
CodeObject_ co = vm->compile("print('Hello!')", "main.py", EXEC_MODE);
|
||||||
```
|
```
|
||||||
|
|
||||||
Execute a compiled code object
|
Execute a compiled code object
|
||||||
|
|||||||
@ -11,7 +11,7 @@ You can use `vm->compile` to compile your source code into a `CodeObject_` objec
|
|||||||
This object can be executed later by `vm->_exec`.
|
This object can be executed later by `vm->_exec`.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
CodeObject_ code = vm->compile("print('Hello, world!')", "<string>", PK_EXEC_MODE);
|
CodeObject_ code = vm->compile("print('Hello, world!')", "<string>", EXEC_MODE);
|
||||||
vm->_exec(code); // Hello, world!
|
vm->_exec(code); // Hello, world!
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -27,9 +27,9 @@ It does some basic preprocessing and outputs the result as a human-readable stri
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
// precompile the source code into a string
|
// precompile the source code into a string
|
||||||
Str source = vm->precompile("print('Hello, world!')", "<string>", PK_EXEC_MODE);
|
Str source = vm->precompile("print('Hello, world!')", "<string>", EXEC_MODE);
|
||||||
|
|
||||||
CodeObject code = vm->compile(source, "<string>", PK_EXEC_MODE);
|
CodeObject code = vm->compile(source, "<string>", EXEC_MODE);
|
||||||
vm->_exec(code); // Hello, world!
|
vm->_exec(code); // Hello, world!
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -8,7 +8,7 @@ order: 93
|
|||||||
|
|
||||||
Once you have a `VM` instance, you can execute python code by calling `exec` method.
|
Once you have a `VM` instance, you can execute python code by calling `exec` method.
|
||||||
|
|
||||||
#### `PyVar exec(Str source, Str filename, pkpy_CompileMode mode, PyVar _module=nullptr)`
|
#### `PyVar exec(Str source, Str filename, CompileMode mode, PyVar _module=nullptr)`
|
||||||
|
|
||||||
+ `source`, the python source code to be executed
|
+ `source`, the python source code to be executed
|
||||||
+ `filename`, the filename of the source code. This is used for error reporting
|
+ `filename`, the filename of the source code. This is used for error reporting
|
||||||
@ -26,11 +26,11 @@ There are also overloaded versions of `exec` and `eval`, which is useful for sim
|
|||||||
### Compile mode
|
### Compile mode
|
||||||
|
|
||||||
The `mode` parameter controls how the source code is compiled. There are 5 possible values:
|
The `mode` parameter controls how the source code is compiled. There are 5 possible values:
|
||||||
+ `PK_EXEC_MODE`, this is the default mode. Just do normal execution.
|
+ `EXEC_MODE`, this is the default mode. Just do normal execution.
|
||||||
+ `PK_EVAL_MODE`, this mode is used for evaluating a single expression. The `source` should be a single expression. It cannot contain any statements.
|
+ `EVAL_MODE`, this mode is used for evaluating a single expression. The `source` should be a single expression. It cannot contain any statements.
|
||||||
+ `PK_REPL_MODE`, this mode is used for REPL. It is similar to `PK_EXEC_MODE`, but generates `PRINT_EXPR` opcode when necessary.
|
+ `REPL_MODE`, this mode is used for REPL. It is similar to `EXEC_MODE`, but generates `PRINT_EXPR` opcode when necessary.
|
||||||
+ `PK_CELL_MODE`, this mode is designed for Jupyter like execution. It is similar to `PK_EXEC_MODE`, but generates `PRINT_EXPR` opcode when necessary.
|
+ `CELL_MODE`, this mode is designed for Jupyter like execution. It is similar to `EXEC_MODE`, but generates `PRINT_EXPR` opcode when necessary.
|
||||||
+ `PK_JSON_MODE`, this mode is used for JSON parsing. It is similar to `PK_EVAL_MODE`, but uses a lexing rule designed for JSON.
|
+ `JSON_MODE`, this mode is used for JSON parsing. It is similar to `EVAL_MODE`, but uses a lexing rule designed for JSON.
|
||||||
|
|
||||||
|
|
||||||
### Fine-grained execution
|
### Fine-grained execution
|
||||||
@ -38,7 +38,7 @@ The `mode` parameter controls how the source code is compiled. There are 5 possi
|
|||||||
In some cases, you may want to execute python code in a more fine-grained way.
|
In some cases, you may want to execute python code in a more fine-grained way.
|
||||||
These two methods are provided for this purpose:
|
These two methods are provided for this purpose:
|
||||||
|
|
||||||
+ `CodeObject_ compile(Str source, Str filename, pkpy_CompileMode mode, bool unknown_global_scope)`
|
+ `CodeObject_ compile(Str source, Str filename, CompileMode mode, bool unknown_global_scope)`
|
||||||
+ `PyVar _exec(CodeObject_ co, PyVar _module)`
|
+ `PyVar _exec(CodeObject_ co, PyVar _module)`
|
||||||
|
|
||||||
1. `compile` compiles the source code into a `CodeObject_` instance. Leave `unknown_global_scope` to `false` if you don't know what it means.
|
1. `compile` compiles the source code into a `CodeObject_` instance. Leave `unknown_global_scope` to `false` if you don't know what it means.
|
||||||
@ -50,7 +50,7 @@ These two methods are provided for this purpose:
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
try{
|
try{
|
||||||
CodeObject_ code = vm->compile("a[0]", "main.py", PK_EXEC_MODE, false);
|
CodeObject_ code = vm->compile("a[0]", "main.py", EXEC_MODE, false);
|
||||||
vm->_exec(code, vm->_main);
|
vm->_exec(code, vm->_main);
|
||||||
}catch(TopLevelException e){
|
}catch(TopLevelException e){
|
||||||
// use e.summary() to get a summary of the exception
|
// use e.summary() to get a summary of the exception
|
||||||
|
|||||||
@ -6,6 +6,16 @@
|
|||||||
|
|
||||||
namespace pkpy {
|
namespace pkpy {
|
||||||
|
|
||||||
|
using CompileMode = pkpy_CompileMode;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
EXEC_MODE = PK_EXEC_MODE,
|
||||||
|
EVAL_MODE = PK_EVAL_MODE,
|
||||||
|
REPL_MODE = PK_REPL_MODE,
|
||||||
|
JSON_MODE = PK_JSON_MODE,
|
||||||
|
CELL_MODE = PK_CELL_MODE,
|
||||||
|
};
|
||||||
|
|
||||||
struct SourceData {
|
struct SourceData {
|
||||||
pkpy_SourceData *self;
|
pkpy_SourceData *self;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user