change enum back

This commit is contained in:
方而静 2024-06-11 19:07:01 +08:00
parent a9376bd80f
commit 240cec3559
Signed by: szTom
GPG Key ID: 072D999D60C6473C
4 changed files with 22 additions and 12 deletions

View File

@ -41,7 +41,7 @@ std::cout << py_cast<int>(vm, obj); // 123
Compile a source string into a code object
```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

View File

@ -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`.
```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!
```
@ -27,9 +27,9 @@ It does some basic preprocessing and outputs the result as a human-readable stri
```cpp
// 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!
```

View File

@ -8,7 +8,7 @@ order: 93
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
+ `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
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.
+ `PK_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.
+ `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.
+ `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.
+ `EXEC_MODE`, this is the default mode. Just do normal execution.
+ `EVAL_MODE`, this mode is used for evaluating a single expression. The `source` should be a single expression. It cannot contain any statements.
+ `REPL_MODE`, this mode is used for REPL. It is similar to `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.
+ `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
@ -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.
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)`
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
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);
}catch(TopLevelException e){
// use e.summary() to get a summary of the exception

View File

@ -6,6 +6,16 @@
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 {
pkpy_SourceData *self;