This commit is contained in:
blueloveTH 2023-05-25 17:11:50 +08:00
parent 5e3293d5a5
commit 8c8ececc83
4 changed files with 8 additions and 1 deletions

View File

@ -25,6 +25,7 @@ The `mode` parameter controls how the source code is compiled. There are 4 possi
+ `EXEC_MODE`, this is the default mode. Just do normal execution. + `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. + `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. + `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.
+ `JSON_MODE`, this mode is used for JSON parsing. It is similar to `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.

View File

@ -816,7 +816,7 @@ __SUBSCR_END:
EXPR_TUPLE(); EXPR_TUPLE();
if(!try_compile_assignment()){ if(!try_compile_assignment()){
ctx()->emit_expr(); ctx()->emit_expr();
if(mode()==REPL_MODE && name_scope()==NAME_GLOBAL){ if((mode()==CELL_MODE || mode()==REPL_MODE) && name_scope()==NAME_GLOBAL){
ctx()->emit(OP_PRINT_EXPR, BC_NOARG, BC_KEEPLINE); ctx()->emit(OP_PRINT_EXPR, BC_NOARG, BC_KEEPLINE);
}else{ }else{
ctx()->emit(OP_POP_TOP, BC_NOARG, BC_KEEPLINE); ctx()->emit(OP_POP_TOP, BC_NOARG, BC_KEEPLINE);

View File

@ -19,6 +19,7 @@ enum CompileMode {
EXEC_MODE, EXEC_MODE,
EVAL_MODE, EVAL_MODE,
REPL_MODE, REPL_MODE,
CELL_MODE,
JSON_MODE, JSON_MODE,
}; };

View File

@ -1354,6 +1354,11 @@ extern "C" {
vm->exec(source, "main.py", pkpy::EXEC_MODE); vm->exec(source, "main.py", pkpy::EXEC_MODE);
} }
PK_LEGACY_EXPORT
void pkpy_vm_exec_cell(pkpy::VM* vm, const char* source){
vm->exec(source, "<cell>", pkpy::CELL_MODE);
}
PK_LEGACY_EXPORT PK_LEGACY_EXPORT
char* pkpy_vm_get_global(pkpy::VM* vm, const char* name){ char* pkpy_vm_get_global(pkpy::VM* vm, const char* name){
pkpy::PyObject* val = vm->_main->attr().try_get(name); pkpy::PyObject* val = vm->_main->attr().try_get(name);