Skip to content

Commit 1fb0317

Browse files
committed
Change mp_compile so that it returns a function object for the module.
1 parent 14f945c commit 1fb0317

13 files changed

Lines changed: 62 additions & 71 deletions

File tree

py/builtinimport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include "lexer.h"
1212
#include "lexerunix.h"
1313
#include "parse.h"
14-
#include "compile.h"
1514
#include "obj.h"
15+
#include "compile.h"
1616
#include "runtime0.h"
1717
#include "runtime.h"
1818
#include "map.h"

py/compile.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include "lexer.h"
1111
#include "parse.h"
1212
#include "scope.h"
13-
#include "compile.h"
1413
#include "runtime0.h"
1514
#include "emit.h"
15+
#include "obj.h"
16+
#include "compile.h"
17+
#include "runtime.h"
1618

1719
// TODO need to mangle __attr names
1820

@@ -3016,7 +3018,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
30163018
}
30173019
}
30183020

3019-
bool mp_compile(mp_parse_node_t pn, bool is_repl) {
3021+
mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) {
30203022
compiler_t *comp = m_new(compiler_t, 1);
30213023

30223024
comp->qstr___class__ = qstr_from_str_static("__class__");
@@ -3146,7 +3148,19 @@ bool mp_compile(mp_parse_node_t pn, bool is_repl) {
31463148
}
31473149
}
31483150

3151+
bool had_error = comp->had_error;
31493152
m_del_obj(compiler_t, comp);
31503153

3151-
return !comp->had_error;
3154+
if (had_error) {
3155+
// TODO return a proper error message
3156+
return mp_const_none;
3157+
} else {
3158+
#if MICROPY_EMIT_CPYTHON
3159+
// can't create code, so just return true
3160+
return mp_const_true;
3161+
#else
3162+
// return function that executes the outer module
3163+
return rt_make_function_from_id(1);
3164+
#endif
3165+
}
31523166
}

py/compile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bool mp_compile(mp_parse_node_t pn, bool is_repl);
1+
mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl);

py/emitbc.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "mpconfig.h"
1010
#include "lexer.h"
1111
#include "parse.h"
12-
#include "compile.h"
1312
#include "scope.h"
1413
#include "runtime0.h"
1514
#include "emit.h"

py/emitcpy.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "mpconfig.h"
1010
#include "lexer.h"
1111
#include "parse.h"
12-
#include "compile.h"
1312
#include "scope.h"
1413
#include "runtime0.h"
1514
#include "emit.h"

py/emitpass1.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "mpconfig.h"
1010
#include "lexer.h"
1111
#include "parse.h"
12-
#include "compile.h"
1312
#include "scope.h"
1413
#include "runtime0.h"
1514
#include "emit.h"

stm/audio.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "misc.h"
99
#include "mpconfig.h"
1010
#include "parse.h"
11-
#include "compile.h"
1211
#include "obj.h"
1312
#include "runtime.h"
1413

stm/lcd.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "misc.h"
66
#include "mpconfig.h"
77
#include "parse.h"
8-
#include "compile.h"
98
#include "obj.h"
109
#include "runtime.h"
1110

stm/main.c

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include "lexer.h"
2121
#include "lexerstm.h"
2222
#include "parse.h"
23-
#include "compile.h"
2423
#include "obj.h"
24+
#include "compile.h"
2525
#include "runtime0.h"
2626
#include "runtime.h"
2727
#include "repl.h"
@@ -489,25 +489,22 @@ void do_repl(void) {
489489
mp_lexer_free(lex);
490490

491491
if (pn != MP_PARSE_NODE_NULL) {
492-
bool comp_ok = mp_compile(pn, true);
493-
if (comp_ok) {
494-
mp_obj_t module_fun = rt_make_function_from_id(1);
495-
if (module_fun != mp_const_none) {
496-
nlr_buf_t nlr;
497-
uint32_t start = sys_tick_counter;
498-
if (nlr_push(&nlr) == 0) {
499-
rt_call_function_0(module_fun);
500-
nlr_pop();
501-
// optional timing
502-
if (0) {
503-
uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly
504-
printf("(took %lu ms)\n", ticks);
505-
}
506-
} else {
507-
// uncaught exception
508-
mp_obj_print((mp_obj_t)nlr.ret_val);
509-
printf("\n");
492+
mp_obj_t module_fun = mp_compile(pn, true);
493+
if (module_fun != mp_const_none) {
494+
nlr_buf_t nlr;
495+
uint32_t start = sys_tick_counter;
496+
if (nlr_push(&nlr) == 0) {
497+
rt_call_function_0(module_fun);
498+
nlr_pop();
499+
// optional timing
500+
if (0) {
501+
uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly
502+
printf("(took %lu ms)\n", ticks);
510503
}
504+
} else {
505+
// uncaught exception
506+
mp_obj_print((mp_obj_t)nlr.ret_val);
507+
printf("\n");
511508
}
512509
}
513510
}
@@ -532,12 +529,7 @@ bool do_file(const char *filename) {
532529
return false;
533530
}
534531

535-
bool comp_ok = mp_compile(pn, false);
536-
if (!comp_ok) {
537-
return false;
538-
}
539-
540-
mp_obj_t module_fun = rt_make_function_from_id(1);
532+
mp_obj_t module_fun = mp_compile(pn, false);
541533
if (module_fun == mp_const_none) {
542534
return false;
543535
}
@@ -1133,17 +1125,15 @@ int main(void) {
11331125
printf("pars;al=%u\n", m_get_total_bytes_allocated());
11341126
sys_tick_delay_ms(1000);
11351127
//parse_node_show(pn, 0);
1136-
bool comp_ok = mp_compile(pn, false);
1128+
mp_obj_t module_fun = mp_compile(pn, false);
11371129
printf("comp;al=%u\n", m_get_total_bytes_allocated());
11381130
sys_tick_delay_ms(1000);
11391131

1140-
if (!comp_ok) {
1132+
if (module_fun == mp_const_none) {
11411133
printf("compile error\n");
11421134
} else {
11431135
// execute it!
11441136

1145-
mp_obj_t module_fun = rt_make_function_from_id(1);
1146-
11471137
// flash once
11481138
led_state(PYB_LED_G1, 1);
11491139
sys_tick_delay_ms(100);

stm/pybwlan.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "misc.h"
1919
#include "lexer.h"
2020
#include "parse.h"
21-
#include "compile.h"
2221
#include "obj.h"
2322
#include "map.h"
2423
#include "runtime.h"

0 commit comments

Comments
 (0)