From 1fb031744f5584d1bd0c88d28fbe7e261832c7a8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 3 Jan 2014 14:22:03 +0000 Subject: [PATCH] Change mp_compile so that it returns a function object for the module. --- py/builtinimport.c | 2 +- py/compile.c | 20 ++++++++++++++++--- py/compile.h | 2 +- py/emitbc.c | 1 - py/emitcpy.c | 1 - py/emitpass1.c | 1 - stm/audio.c | 1 - stm/lcd.c | 1 - stm/main.c | 48 ++++++++++++++++++---------------------------- stm/pybwlan.c | 1 - stm/timer.c | 1 - unix-cpy/main.c | 6 +++--- unix/main.c | 48 ++++++++++++++++++++-------------------------- 13 files changed, 62 insertions(+), 71 deletions(-) diff --git a/py/builtinimport.c b/py/builtinimport.c index f1479ab123..47dbf21216 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -11,8 +11,8 @@ #include "lexer.h" #include "lexerunix.h" #include "parse.h" -#include "compile.h" #include "obj.h" +#include "compile.h" #include "runtime0.h" #include "runtime.h" #include "map.h" diff --git a/py/compile.c b/py/compile.c index b00ab7ef64..68ac20804d 100644 --- a/py/compile.c +++ b/py/compile.c @@ -10,9 +10,11 @@ #include "lexer.h" #include "parse.h" #include "scope.h" -#include "compile.h" #include "runtime0.h" #include "emit.h" +#include "obj.h" +#include "compile.h" +#include "runtime.h" // TODO need to mangle __attr names @@ -3016,7 +3018,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) { } } -bool mp_compile(mp_parse_node_t pn, bool is_repl) { +mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) { compiler_t *comp = m_new(compiler_t, 1); comp->qstr___class__ = qstr_from_str_static("__class__"); @@ -3146,7 +3148,19 @@ bool mp_compile(mp_parse_node_t pn, bool is_repl) { } } + bool had_error = comp->had_error; m_del_obj(compiler_t, comp); - return !comp->had_error; + if (had_error) { + // TODO return a proper error message + return mp_const_none; + } else { +#if MICROPY_EMIT_CPYTHON + // can't create code, so just return true + return mp_const_true; +#else + // return function that executes the outer module + return rt_make_function_from_id(1); +#endif + } } diff --git a/py/compile.h b/py/compile.h index e283442bb0..770c2524da 100644 --- a/py/compile.h +++ b/py/compile.h @@ -1 +1 @@ -bool mp_compile(mp_parse_node_t pn, bool is_repl); +mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl); diff --git a/py/emitbc.c b/py/emitbc.c index 790fe3e4e5..dc1988582c 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -9,7 +9,6 @@ #include "mpconfig.h" #include "lexer.h" #include "parse.h" -#include "compile.h" #include "scope.h" #include "runtime0.h" #include "emit.h" diff --git a/py/emitcpy.c b/py/emitcpy.c index b107c0bf11..652617cc88 100644 --- a/py/emitcpy.c +++ b/py/emitcpy.c @@ -9,7 +9,6 @@ #include "mpconfig.h" #include "lexer.h" #include "parse.h" -#include "compile.h" #include "scope.h" #include "runtime0.h" #include "emit.h" diff --git a/py/emitpass1.c b/py/emitpass1.c index 4ed0549727..1c11241e0d 100644 --- a/py/emitpass1.c +++ b/py/emitpass1.c @@ -9,7 +9,6 @@ #include "mpconfig.h" #include "lexer.h" #include "parse.h" -#include "compile.h" #include "scope.h" #include "runtime0.h" #include "emit.h" diff --git a/stm/audio.c b/stm/audio.c index 38e0012b41..34adefbcd6 100644 --- a/stm/audio.c +++ b/stm/audio.c @@ -8,7 +8,6 @@ #include "misc.h" #include "mpconfig.h" #include "parse.h" -#include "compile.h" #include "obj.h" #include "runtime.h" diff --git a/stm/lcd.c b/stm/lcd.c index 9f5a157d05..70d1a26423 100644 --- a/stm/lcd.c +++ b/stm/lcd.c @@ -5,7 +5,6 @@ #include "misc.h" #include "mpconfig.h" #include "parse.h" -#include "compile.h" #include "obj.h" #include "runtime.h" diff --git a/stm/main.c b/stm/main.c index f16ce252d2..e8db2be2ca 100644 --- a/stm/main.c +++ b/stm/main.c @@ -20,8 +20,8 @@ #include "lexer.h" #include "lexerstm.h" #include "parse.h" -#include "compile.h" #include "obj.h" +#include "compile.h" #include "runtime0.h" #include "runtime.h" #include "repl.h" @@ -489,25 +489,22 @@ void do_repl(void) { mp_lexer_free(lex); if (pn != MP_PARSE_NODE_NULL) { - bool comp_ok = mp_compile(pn, true); - if (comp_ok) { - mp_obj_t module_fun = rt_make_function_from_id(1); - if (module_fun != mp_const_none) { - nlr_buf_t nlr; - uint32_t start = sys_tick_counter; - if (nlr_push(&nlr) == 0) { - rt_call_function_0(module_fun); - nlr_pop(); - // optional timing - if (0) { - uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly - printf("(took %lu ms)\n", ticks); - } - } else { - // uncaught exception - mp_obj_print((mp_obj_t)nlr.ret_val); - printf("\n"); + mp_obj_t module_fun = mp_compile(pn, true); + if (module_fun != mp_const_none) { + nlr_buf_t nlr; + uint32_t start = sys_tick_counter; + if (nlr_push(&nlr) == 0) { + rt_call_function_0(module_fun); + nlr_pop(); + // optional timing + if (0) { + uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly + printf("(took %lu ms)\n", ticks); } + } else { + // uncaught exception + mp_obj_print((mp_obj_t)nlr.ret_val); + printf("\n"); } } } @@ -532,12 +529,7 @@ bool do_file(const char *filename) { return false; } - bool comp_ok = mp_compile(pn, false); - if (!comp_ok) { - return false; - } - - mp_obj_t module_fun = rt_make_function_from_id(1); + mp_obj_t module_fun = mp_compile(pn, false); if (module_fun == mp_const_none) { return false; } @@ -1133,17 +1125,15 @@ soft_reset: printf("pars;al=%u\n", m_get_total_bytes_allocated()); sys_tick_delay_ms(1000); //parse_node_show(pn, 0); - bool comp_ok = mp_compile(pn, false); + mp_obj_t module_fun = mp_compile(pn, false); printf("comp;al=%u\n", m_get_total_bytes_allocated()); sys_tick_delay_ms(1000); - if (!comp_ok) { + if (module_fun == mp_const_none) { printf("compile error\n"); } else { // execute it! - mp_obj_t module_fun = rt_make_function_from_id(1); - // flash once led_state(PYB_LED_G1, 1); sys_tick_delay_ms(100); diff --git a/stm/pybwlan.c b/stm/pybwlan.c index 6341d0676b..6988f1c848 100644 --- a/stm/pybwlan.c +++ b/stm/pybwlan.c @@ -18,7 +18,6 @@ #include "misc.h" #include "lexer.h" #include "parse.h" -#include "compile.h" #include "obj.h" #include "map.h" #include "runtime.h" diff --git a/stm/timer.c b/stm/timer.c index 28148b0c1b..2605d4b4bc 100644 --- a/stm/timer.c +++ b/stm/timer.c @@ -9,7 +9,6 @@ #include "misc.h" #include "mpconfig.h" #include "parse.h" -#include "compile.h" #include "obj.h" #include "runtime.h" diff --git a/unix-cpy/main.c b/unix-cpy/main.c index 2b59df4f37..eba97f527b 100644 --- a/unix-cpy/main.c +++ b/unix-cpy/main.c @@ -8,8 +8,8 @@ #include "lexer.h" #include "lexerunix.h" #include "parse.h" -#include "compile.h" #include "obj.h" +#include "compile.h" #include "runtime0.h" #include "runtime.h" @@ -37,10 +37,10 @@ void do_file(const char *file) { //printf("----------------\n"); //parse_node_show(pn, 0); //printf("----------------\n"); - bool comp_ok = mp_compile(pn, false); + mp_obj_t module_fun = mp_compile(pn, false); //printf("----------------\n"); - if (!comp_ok) { + if (module_fun == mp_const_none) { printf("compile error\n"); } } diff --git a/unix/main.c b/unix/main.c index c23a8e54c4..1aa3331202 100644 --- a/unix/main.c +++ b/unix/main.c @@ -9,8 +9,8 @@ #include "lexer.h" #include "lexerunix.h" #include "parse.h" -#include "compile.h" #include "obj.h" +#include "compile.h" #include "runtime0.h" #include "runtime.h" #include "repl.h" @@ -85,19 +85,16 @@ static void do_repl(void) { if (pn != MP_PARSE_NODE_NULL) { //mp_parse_node_show(pn, 0); - bool comp_ok = mp_compile(pn, true); - if (comp_ok) { - mp_obj_t module_fun = rt_make_function_from_id(1); - if (module_fun != mp_const_none) { - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - rt_call_function_0(module_fun); - nlr_pop(); - } else { - // uncaught exception - mp_obj_print((mp_obj_t)nlr.ret_val); - printf("\n"); - } + mp_obj_t module_fun = mp_compile(pn, true); + if (module_fun != mp_const_none) { + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + rt_call_function_0(module_fun); + nlr_pop(); + } else { + // uncaught exception + mp_obj_print((mp_obj_t)nlr.ret_val); + printf("\n"); } } } @@ -142,7 +139,7 @@ void do_file(const char *file) { //printf("----------------\n"); //parse_node_show(pn, 0); //printf("----------------\n"); - bool comp_ok = mp_compile(pn, false); + mp_obj_t module_fun = mp_compile(pn, false); //printf("----------------\n"); #if MICROPY_EMIT_CPYTHON @@ -150,19 +147,16 @@ void do_file(const char *file) { printf("compile error\n"); } #else - if (1 && comp_ok) { + if (1 && module_fun != mp_const_none) { // execute it - mp_obj_t module_fun = rt_make_function_from_id(1); - if (module_fun != mp_const_none) { - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - rt_call_function_0(module_fun); - nlr_pop(); - } else { - // uncaught exception - mp_obj_print((mp_obj_t)nlr.ret_val); - printf("\n"); - } + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + rt_call_function_0(module_fun); + nlr_pop(); + } else { + // uncaught exception + mp_obj_print((mp_obj_t)nlr.ret_val); + printf("\n"); } } #endif