From 240cec35599f5aef40fb8adb4c19d40d66864669 Mon Sep 17 00:00:00 2001 From: szdytom Date: Tue, 11 Jun 2024 19:07:01 +0800 Subject: [PATCH] change enum back --- docs/cheatsheet.md | 2 +- docs/features/precompile.md | 6 +++--- docs/quick-start/exec.md | 16 ++++++++-------- include/pocketpy/objects/sourcedata.hpp | 10 ++++++++++ 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/docs/cheatsheet.md b/docs/cheatsheet.md index 5a141b23..d9896949 100644 --- a/docs/cheatsheet.md +++ b/docs/cheatsheet.md @@ -41,7 +41,7 @@ std::cout << py_cast(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 diff --git a/docs/features/precompile.md b/docs/features/precompile.md index 4e69a48c..93c56ecc 100644 --- a/docs/features/precompile.md +++ b/docs/features/precompile.md @@ -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!')", "", PK_EXEC_MODE); +CodeObject_ code = vm->compile("print('Hello, world!')", "", 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!')", "", PK_EXEC_MODE); +Str source = vm->precompile("print('Hello, world!')", "", EXEC_MODE); -CodeObject code = vm->compile(source, "", PK_EXEC_MODE); +CodeObject code = vm->compile(source, "", EXEC_MODE); vm->_exec(code); // Hello, world! ``` diff --git a/docs/quick-start/exec.md b/docs/quick-start/exec.md index 2ded9192..cbc88b20 100644 --- a/docs/quick-start/exec.md +++ b/docs/quick-start/exec.md @@ -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 diff --git a/include/pocketpy/objects/sourcedata.hpp b/include/pocketpy/objects/sourcedata.hpp index 141ef54f..27ec6978 100644 --- a/include/pocketpy/objects/sourcedata.hpp +++ b/include/pocketpy/objects/sourcedata.hpp @@ -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;