diff options
author | Damien George <damien@micropython.org> | 2023-06-24 17:02:58 +1000 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2024-03-22 13:05:25 +1100 |
commit | 2b8e88c563e4bab53e3183d15f04795d645eea28 (patch) | |
tree | 5c3cc7fc56a799c6434c83f9e7429ccc7ef84302 /py/compile.c | |
parent | acbdbcd95e6d953b08f0642a844c1808323e0b7c (diff) |
py/compile: Add option to allow compiling top-level await.
Enabled by MICROPY_COMPILE_ALLOW_TOP_LEVEL_AWAIT. When enabled, this means
that scope such as module-level functions and REPL statements can yield.
The outer C code must then handle this yielded generator.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py/compile.c')
-rw-r--r-- | py/compile.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/py/compile.c b/py/compile.c index 7a359e662..62757de3c 100644 --- a/py/compile.c +++ b/py/compile.c @@ -196,6 +196,10 @@ typedef struct _compiler_t { mp_emit_common_t emit_common; } compiler_t; +#if MICROPY_COMP_ALLOW_TOP_LEVEL_AWAIT +bool mp_compile_allow_top_level_await = false; +#endif + /******************************************************************************/ // mp_emit_common_t helper functions // These are defined here so they can be inlined, to reduce code size. @@ -2759,8 +2763,13 @@ static void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { #if MICROPY_PY_ASYNC_AWAIT static void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) { if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) { - compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("'await' outside function")); - return; + #if MICROPY_COMP_ALLOW_TOP_LEVEL_AWAIT + if (!mp_compile_allow_top_level_await) + #endif + { + compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("'await' outside function")); + return; + } } compile_atom_expr_normal(comp, pns); compile_yield_from(comp); |