diff options
author | Damien George <damien.p.george@gmail.com> | 2014-05-07 15:42:03 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-05-07 15:42:03 +0100 |
commit | ca25c15d560f3f5337819bac65832cccc4752495 (patch) | |
tree | abeefa5b3b7401f33860ccab0e6591eb1ba4e36d /py | |
parent | 7c6c843965fa3a573773771229abd6c1ab37eaaa (diff) | |
download | micropython-ca25c15d560f3f5337819bac65832cccc4752495.tar.gz micropython-ca25c15d560f3f5337819bac65832cccc4752495.zip |
py, compiler: Start adding support for compile-time constants.
Just a start, no working code yet. As per issue #573.
Diffstat (limited to 'py')
-rw-r--r-- | py/compile.c | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/py/compile.c b/py/compile.c index fdae6fbcf4..9a660e2ef5 100644 --- a/py/compile.c +++ b/py/compile.c @@ -2045,7 +2045,36 @@ void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store } } else if (kind == PN_expr_stmt_assign) { - if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr) + if (0) { + // dummy +#if 0 + // code to compile constants: id = const(...) + } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) + && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power) + && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) + && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const + && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren) + && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2]) + ) { + if (comp->pass == PASS_1) { + qstr const_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); + + if (!MP_PARSE_NODE_IS_SMALL_INT(((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0])) { + compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer"); + } + machine_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0]); + + printf("assign const: %s = %ld\n", qstr_str(const_id), value); + mp_map_elem_t *elem = mp_map_lookup(&comp->module_consts, MP_OBJ_NEW_QSTR(const_id), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); + if (elem->value != MP_OBJ_NULL) { + compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined"); + } + elem->value = MP_OBJ_NEW_SMALL_INT(value); + } + goto no_optimisation; + +#endif + } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr) && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) { |