最终由mp_execute_bytecode执行bytecode
// execute the byte code with the correct globals context
code_state->old_globals = mp_globals_get();
mp_globals_set(self->globals);
mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
mp_globals_set(code_state->old_globals);
它由fun_bc_call()调用,位于objfun.c
如果一切正常,中途没有ctrl+c , ctrl+d, 则执行:
// got a line with non-zero length, see if it needs continuing
while (mp_repl_continue_with_input(vstr_null_terminated_str(&line)))
每次读取一行。
line 作为一个变长的string ,存在如下结构体中:
/** variable string *********************************************/
typedef struct _vstr_t {
size_t alloc;
size_t len;
char *buf;
bool had_error : 1;
bool fixed_buf : 1;
} vstr_t;
fun_bc_call 做成了一个type.
const mp_obj_type_t mp_type_fun_bc = {
{ &mp_type_type },
.name = MP_QSTR_function,
#if MICROPY_CPYTHON_COMPAT
.print = fun_bc_print,
#endif
.call = fun_bc_call,
.unary_op = mp_generic_unary_op,
#if MICROPY_PY_FUNCTION_ATTRS
.attr = fun_bc_attr,
#endif
};
mp_parse 初始化分析器,并为其自身分配堆栈。
由execute_from_lexer 完成编译
编译分成了两步:
mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
然后
mp_obj_t module_fun = mp_compile(&parse_tree, source_name, emit_opt, is_repl);
最后执行:
mp_call_function_0(module_fun);