py/parse: Move function to check for const parse node to parse.[ch].
This commit is contained in:
parent
3f8bb80eb2
commit
b0cbfb0492
26
py/compile.c
26
py/compile.c
|
@ -243,23 +243,13 @@ STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns)
|
|||
c_tuple(comp, MP_PARSE_NODE_NULL, pns);
|
||||
}
|
||||
|
||||
STATIC bool node_is_const_false(mp_parse_node_t pn) {
|
||||
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
|
||||
|| (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
|
||||
}
|
||||
|
||||
STATIC bool node_is_const_true(mp_parse_node_t pn) {
|
||||
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
|
||||
|| (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
|
||||
}
|
||||
|
||||
STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
|
||||
if (node_is_const_false(pn)) {
|
||||
if (mp_parse_node_is_const_false(pn)) {
|
||||
if (jump_if == false) {
|
||||
EMIT_ARG(jump, label);
|
||||
}
|
||||
return;
|
||||
} else if (node_is_const_true(pn)) {
|
||||
} else if (mp_parse_node_is_const_true(pn)) {
|
||||
if (jump_if == true) {
|
||||
EMIT_ARG(jump, label);
|
||||
}
|
||||
|
@ -1218,14 +1208,14 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
|||
uint l_end = comp_next_label(comp);
|
||||
|
||||
// optimisation: don't emit anything when "if False"
|
||||
if (!node_is_const_false(pns->nodes[0])) {
|
||||
if (!mp_parse_node_is_const_false(pns->nodes[0])) {
|
||||
uint l_fail = comp_next_label(comp);
|
||||
c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
|
||||
|
||||
compile_node(comp, pns->nodes[1]); // if block
|
||||
|
||||
// optimisation: skip everything else when "if True"
|
||||
if (node_is_const_true(pns->nodes[0])) {
|
||||
if (mp_parse_node_is_const_true(pns->nodes[0])) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
|
@ -1250,14 +1240,14 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
|||
mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
|
||||
|
||||
// optimisation: don't emit anything when "if False"
|
||||
if (!node_is_const_false(pns_elif->nodes[0])) {
|
||||
if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) {
|
||||
uint l_fail = comp_next_label(comp);
|
||||
c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
|
||||
|
||||
compile_node(comp, pns_elif->nodes[1]); // elif block
|
||||
|
||||
// optimisation: skip everything else when "elif True"
|
||||
if (node_is_const_true(pns_elif->nodes[0])) {
|
||||
if (mp_parse_node_is_const_true(pns_elif->nodes[0])) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
|
@ -1294,9 +1284,9 @@ done:
|
|||
STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
START_BREAK_CONTINUE_BLOCK
|
||||
|
||||
if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
|
||||
if (!mp_parse_node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
|
||||
uint top_label = comp_next_label(comp);
|
||||
if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
|
||||
if (!mp_parse_node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
|
||||
EMIT_ARG(jump, continue_label);
|
||||
}
|
||||
EMIT_ARG(label_assign, top_label);
|
||||
|
|
10
py/parse.c
10
py/parse.c
|
@ -234,6 +234,16 @@ mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
|
|||
return (mp_parse_node_t)(kind | (arg << 4));
|
||||
}
|
||||
|
||||
bool mp_parse_node_is_const_false(mp_parse_node_t pn) {
|
||||
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
|
||||
|| (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
|
||||
}
|
||||
|
||||
bool mp_parse_node_is_const_true(mp_parse_node_t pn) {
|
||||
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
|
||||
|| (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
|
||||
}
|
||||
|
||||
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) {
|
||||
if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
|
||||
*o = MP_OBJ_NEW_SMALL_INT(MP_PARSE_NODE_LEAF_SMALL_INT(pn));
|
||||
|
|
|
@ -77,6 +77,8 @@ typedef struct _mp_parse_node_struct_t {
|
|||
#define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8)
|
||||
|
||||
mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg);
|
||||
bool mp_parse_node_is_const_false(mp_parse_node_t pn);
|
||||
bool mp_parse_node_is_const_true(mp_parse_node_t pn);
|
||||
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o);
|
||||
int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes);
|
||||
void mp_parse_node_print(mp_parse_node_t pn, size_t indent);
|
||||
|
|
Loading…
Reference in New Issue