diff --git a/docs/quick-start/exec.md b/docs/quick-start/exec.md index fffb2271..c6bdb3bf 100644 --- a/docs/quick-start/exec.md +++ b/docs/quick-start/exec.md @@ -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. + `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. + `JSON_MODE`, this mode is used for JSON parsing. It is similar to `EVAL_MODE`, but uses a lexing rule designed for JSON. diff --git a/src/compiler.h b/src/compiler.h index 5a2c77ad..df21438e 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -816,7 +816,7 @@ __SUBSCR_END: EXPR_TUPLE(); if(!try_compile_assignment()){ 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); }else{ ctx()->emit(OP_POP_TOP, BC_NOARG, BC_KEEPLINE); diff --git a/src/error.h b/src/error.h index bd7182cd..2485c1c6 100644 --- a/src/error.h +++ b/src/error.h @@ -19,6 +19,7 @@ enum CompileMode { EXEC_MODE, EVAL_MODE, REPL_MODE, + CELL_MODE, JSON_MODE, }; diff --git a/src/pocketpy.h b/src/pocketpy.h index 67108cba..3600ee97 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -1354,6 +1354,11 @@ extern "C" { 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, "", pkpy::CELL_MODE); + } + PK_LEGACY_EXPORT char* pkpy_vm_get_global(pkpy::VM* vm, const char* name){ pkpy::PyObject* val = vm->_main->attr().try_get(name);