Change mp_compile so that it returns a function object for the module.

This commit is contained in:
Damien George 2014-01-03 14:22:03 +00:00
parent 14f945c2ca
commit 1fb031744f
13 changed files with 62 additions and 71 deletions

View File

@ -11,8 +11,8 @@
#include "lexer.h" #include "lexer.h"
#include "lexerunix.h" #include "lexerunix.h"
#include "parse.h" #include "parse.h"
#include "compile.h"
#include "obj.h" #include "obj.h"
#include "compile.h"
#include "runtime0.h" #include "runtime0.h"
#include "runtime.h" #include "runtime.h"
#include "map.h" #include "map.h"

View File

@ -10,9 +10,11 @@
#include "lexer.h" #include "lexer.h"
#include "parse.h" #include "parse.h"
#include "scope.h" #include "scope.h"
#include "compile.h"
#include "runtime0.h" #include "runtime0.h"
#include "emit.h" #include "emit.h"
#include "obj.h"
#include "compile.h"
#include "runtime.h"
// TODO need to mangle __attr names // 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); compiler_t *comp = m_new(compiler_t, 1);
comp->qstr___class__ = qstr_from_str_static("__class__"); 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); 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
}
} }

View File

@ -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);

View File

@ -9,7 +9,6 @@
#include "mpconfig.h" #include "mpconfig.h"
#include "lexer.h" #include "lexer.h"
#include "parse.h" #include "parse.h"
#include "compile.h"
#include "scope.h" #include "scope.h"
#include "runtime0.h" #include "runtime0.h"
#include "emit.h" #include "emit.h"

View File

@ -9,7 +9,6 @@
#include "mpconfig.h" #include "mpconfig.h"
#include "lexer.h" #include "lexer.h"
#include "parse.h" #include "parse.h"
#include "compile.h"
#include "scope.h" #include "scope.h"
#include "runtime0.h" #include "runtime0.h"
#include "emit.h" #include "emit.h"

View File

@ -9,7 +9,6 @@
#include "mpconfig.h" #include "mpconfig.h"
#include "lexer.h" #include "lexer.h"
#include "parse.h" #include "parse.h"
#include "compile.h"
#include "scope.h" #include "scope.h"
#include "runtime0.h" #include "runtime0.h"
#include "emit.h" #include "emit.h"

View File

@ -8,7 +8,6 @@
#include "misc.h" #include "misc.h"
#include "mpconfig.h" #include "mpconfig.h"
#include "parse.h" #include "parse.h"
#include "compile.h"
#include "obj.h" #include "obj.h"
#include "runtime.h" #include "runtime.h"

View File

@ -5,7 +5,6 @@
#include "misc.h" #include "misc.h"
#include "mpconfig.h" #include "mpconfig.h"
#include "parse.h" #include "parse.h"
#include "compile.h"
#include "obj.h" #include "obj.h"
#include "runtime.h" #include "runtime.h"

View File

@ -20,8 +20,8 @@
#include "lexer.h" #include "lexer.h"
#include "lexerstm.h" #include "lexerstm.h"
#include "parse.h" #include "parse.h"
#include "compile.h"
#include "obj.h" #include "obj.h"
#include "compile.h"
#include "runtime0.h" #include "runtime0.h"
#include "runtime.h" #include "runtime.h"
#include "repl.h" #include "repl.h"
@ -489,25 +489,22 @@ void do_repl(void) {
mp_lexer_free(lex); mp_lexer_free(lex);
if (pn != MP_PARSE_NODE_NULL) { if (pn != MP_PARSE_NODE_NULL) {
bool comp_ok = mp_compile(pn, true); mp_obj_t module_fun = mp_compile(pn, true);
if (comp_ok) { if (module_fun != mp_const_none) {
mp_obj_t module_fun = rt_make_function_from_id(1); nlr_buf_t nlr;
if (module_fun != mp_const_none) { uint32_t start = sys_tick_counter;
nlr_buf_t nlr; if (nlr_push(&nlr) == 0) {
uint32_t start = sys_tick_counter; rt_call_function_0(module_fun);
if (nlr_push(&nlr) == 0) { nlr_pop();
rt_call_function_0(module_fun); // optional timing
nlr_pop(); if (0) {
// optional timing uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly
if (0) { printf("(took %lu ms)\n", ticks);
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");
} }
} 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; return false;
} }
bool comp_ok = mp_compile(pn, false); mp_obj_t module_fun = mp_compile(pn, false);
if (!comp_ok) {
return false;
}
mp_obj_t module_fun = rt_make_function_from_id(1);
if (module_fun == mp_const_none) { if (module_fun == mp_const_none) {
return false; return false;
} }
@ -1133,17 +1125,15 @@ soft_reset:
printf("pars;al=%u\n", m_get_total_bytes_allocated()); printf("pars;al=%u\n", m_get_total_bytes_allocated());
sys_tick_delay_ms(1000); sys_tick_delay_ms(1000);
//parse_node_show(pn, 0); //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()); printf("comp;al=%u\n", m_get_total_bytes_allocated());
sys_tick_delay_ms(1000); sys_tick_delay_ms(1000);
if (!comp_ok) { if (module_fun == mp_const_none) {
printf("compile error\n"); printf("compile error\n");
} else { } else {
// execute it! // execute it!
mp_obj_t module_fun = rt_make_function_from_id(1);
// flash once // flash once
led_state(PYB_LED_G1, 1); led_state(PYB_LED_G1, 1);
sys_tick_delay_ms(100); sys_tick_delay_ms(100);

View File

@ -18,7 +18,6 @@
#include "misc.h" #include "misc.h"
#include "lexer.h" #include "lexer.h"
#include "parse.h" #include "parse.h"
#include "compile.h"
#include "obj.h" #include "obj.h"
#include "map.h" #include "map.h"
#include "runtime.h" #include "runtime.h"

View File

@ -9,7 +9,6 @@
#include "misc.h" #include "misc.h"
#include "mpconfig.h" #include "mpconfig.h"
#include "parse.h" #include "parse.h"
#include "compile.h"
#include "obj.h" #include "obj.h"
#include "runtime.h" #include "runtime.h"

View File

@ -8,8 +8,8 @@
#include "lexer.h" #include "lexer.h"
#include "lexerunix.h" #include "lexerunix.h"
#include "parse.h" #include "parse.h"
#include "compile.h"
#include "obj.h" #include "obj.h"
#include "compile.h"
#include "runtime0.h" #include "runtime0.h"
#include "runtime.h" #include "runtime.h"
@ -37,10 +37,10 @@ void do_file(const char *file) {
//printf("----------------\n"); //printf("----------------\n");
//parse_node_show(pn, 0); //parse_node_show(pn, 0);
//printf("----------------\n"); //printf("----------------\n");
bool comp_ok = mp_compile(pn, false); mp_obj_t module_fun = mp_compile(pn, false);
//printf("----------------\n"); //printf("----------------\n");
if (!comp_ok) { if (module_fun == mp_const_none) {
printf("compile error\n"); printf("compile error\n");
} }
} }

View File

@ -9,8 +9,8 @@
#include "lexer.h" #include "lexer.h"
#include "lexerunix.h" #include "lexerunix.h"
#include "parse.h" #include "parse.h"
#include "compile.h"
#include "obj.h" #include "obj.h"
#include "compile.h"
#include "runtime0.h" #include "runtime0.h"
#include "runtime.h" #include "runtime.h"
#include "repl.h" #include "repl.h"
@ -85,19 +85,16 @@ static void do_repl(void) {
if (pn != MP_PARSE_NODE_NULL) { if (pn != MP_PARSE_NODE_NULL) {
//mp_parse_node_show(pn, 0); //mp_parse_node_show(pn, 0);
bool comp_ok = mp_compile(pn, true); mp_obj_t module_fun = mp_compile(pn, true);
if (comp_ok) { if (module_fun != mp_const_none) {
mp_obj_t module_fun = rt_make_function_from_id(1); nlr_buf_t nlr;
if (module_fun != mp_const_none) { if (nlr_push(&nlr) == 0) {
nlr_buf_t nlr; rt_call_function_0(module_fun);
if (nlr_push(&nlr) == 0) { nlr_pop();
rt_call_function_0(module_fun); } else {
nlr_pop(); // uncaught exception
} else { mp_obj_print((mp_obj_t)nlr.ret_val);
// uncaught exception printf("\n");
mp_obj_print((mp_obj_t)nlr.ret_val);
printf("\n");
}
} }
} }
} }
@ -142,7 +139,7 @@ void do_file(const char *file) {
//printf("----------------\n"); //printf("----------------\n");
//parse_node_show(pn, 0); //parse_node_show(pn, 0);
//printf("----------------\n"); //printf("----------------\n");
bool comp_ok = mp_compile(pn, false); mp_obj_t module_fun = mp_compile(pn, false);
//printf("----------------\n"); //printf("----------------\n");
#if MICROPY_EMIT_CPYTHON #if MICROPY_EMIT_CPYTHON
@ -150,19 +147,16 @@ void do_file(const char *file) {
printf("compile error\n"); printf("compile error\n");
} }
#else #else
if (1 && comp_ok) { if (1 && module_fun != mp_const_none) {
// execute it // execute it
mp_obj_t module_fun = rt_make_function_from_id(1); nlr_buf_t nlr;
if (module_fun != mp_const_none) { if (nlr_push(&nlr) == 0) {
nlr_buf_t nlr; rt_call_function_0(module_fun);
if (nlr_push(&nlr) == 0) { nlr_pop();
rt_call_function_0(module_fun); } else {
nlr_pop(); // uncaught exception
} else { mp_obj_print((mp_obj_t)nlr.ret_val);
// uncaught exception printf("\n");
mp_obj_print((mp_obj_t)nlr.ret_val);
printf("\n");
}
} }
} }
#endif #endif