Skip to content

Commit d4dba88

Browse files
committed
py/compile: Add mp_compile_to_raw_code() to return raw code object.
This can then be passed to mp_raw_code_save_file to save a .mpy file.
1 parent f5c554d commit d4dba88

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

py/compile.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,7 +3090,10 @@ STATIC void scope_compute_things(scope_t *scope) {
30903090
}
30913091
}
30923092

3093-
mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
3093+
#if !MICROPY_PERSISTENT_CODE_SAVE
3094+
STATIC
3095+
#endif
3096+
mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
30943097
// put compiler state on the stack, it's relatively small
30953098
compiler_t comp_state = {0};
30963099
compiler_t *comp = &comp_state;
@@ -3263,7 +3266,12 @@ mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt
32633266
if (comp->compile_error != MP_OBJ_NULL) {
32643267
nlr_raise(comp->compile_error);
32653268
} else {
3266-
// return function that executes the outer module
3267-
return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
3269+
return outer_raw_code;
32683270
}
32693271
}
3272+
3273+
mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
3274+
mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, emit_opt, is_repl);
3275+
// return function that executes the outer module
3276+
return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
3277+
}

py/compile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ enum {
4343
// the compiler will clear the parse tree before it returns
4444
mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl);
4545

46+
#if MICROPY_PERSISTENT_CODE_SAVE
47+
// this has the same semantics as mp_compile
48+
mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl);
49+
#endif
50+
4651
// this is implemented in runtime.c
4752
mp_obj_t mp_parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t parse_input_kind, mp_obj_dict_t *globals, mp_obj_dict_t *locals);
4853

0 commit comments

Comments
 (0)