Skip to content

Commit b0cbfb0

Browse files
committed
py/parse: Move function to check for const parse node to parse.[ch].
1 parent 3f8bb80 commit b0cbfb0

3 files changed

Lines changed: 20 additions & 18 deletions

File tree

py/compile.c

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -243,23 +243,13 @@ STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns)
243243
c_tuple(comp, MP_PARSE_NODE_NULL, pns);
244244
}
245245

246-
STATIC bool node_is_const_false(mp_parse_node_t pn) {
247-
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
248-
|| (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
249-
}
250-
251-
STATIC bool node_is_const_true(mp_parse_node_t pn) {
252-
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
253-
|| (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
254-
}
255-
256246
STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
257-
if (node_is_const_false(pn)) {
247+
if (mp_parse_node_is_const_false(pn)) {
258248
if (jump_if == false) {
259249
EMIT_ARG(jump, label);
260250
}
261251
return;
262-
} else if (node_is_const_true(pn)) {
252+
} else if (mp_parse_node_is_const_true(pn)) {
263253
if (jump_if == true) {
264254
EMIT_ARG(jump, label);
265255
}
@@ -1218,14 +1208,14 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
12181208
uint l_end = comp_next_label(comp);
12191209

12201210
// optimisation: don't emit anything when "if False"
1221-
if (!node_is_const_false(pns->nodes[0])) {
1211+
if (!mp_parse_node_is_const_false(pns->nodes[0])) {
12221212
uint l_fail = comp_next_label(comp);
12231213
c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
12241214

12251215
compile_node(comp, pns->nodes[1]); // if block
12261216

12271217
// optimisation: skip everything else when "if True"
1228-
if (node_is_const_true(pns->nodes[0])) {
1218+
if (mp_parse_node_is_const_true(pns->nodes[0])) {
12291219
goto done;
12301220
}
12311221

@@ -1250,14 +1240,14 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
12501240
mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
12511241

12521242
// optimisation: don't emit anything when "if False"
1253-
if (!node_is_const_false(pns_elif->nodes[0])) {
1243+
if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) {
12541244
uint l_fail = comp_next_label(comp);
12551245
c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
12561246

12571247
compile_node(comp, pns_elif->nodes[1]); // elif block
12581248

12591249
// optimisation: skip everything else when "elif True"
1260-
if (node_is_const_true(pns_elif->nodes[0])) {
1250+
if (mp_parse_node_is_const_true(pns_elif->nodes[0])) {
12611251
goto done;
12621252
}
12631253

@@ -1294,9 +1284,9 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
12941284
STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
12951285
START_BREAK_CONTINUE_BLOCK
12961286

1297-
if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1287+
if (!mp_parse_node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
12981288
uint top_label = comp_next_label(comp);
1299-
if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1289+
if (!mp_parse_node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
13001290
EMIT_ARG(jump, continue_label);
13011291
}
13021292
EMIT_ARG(label_assign, top_label);

py/parse.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@ mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
234234
return (mp_parse_node_t)(kind | (arg << 4));
235235
}
236236

237+
bool mp_parse_node_is_const_false(mp_parse_node_t pn) {
238+
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
239+
|| (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
240+
}
241+
242+
bool mp_parse_node_is_const_true(mp_parse_node_t pn) {
243+
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
244+
|| (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
245+
}
246+
237247
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) {
238248
if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
239249
*o = MP_OBJ_NEW_SMALL_INT(MP_PARSE_NODE_LEAF_SMALL_INT(pn));

py/parse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ typedef struct _mp_parse_node_struct_t {
7777
#define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8)
7878

7979
mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg);
80+
bool mp_parse_node_is_const_false(mp_parse_node_t pn);
81+
bool mp_parse_node_is_const_true(mp_parse_node_t pn);
8082
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o);
8183
int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes);
8284
void mp_parse_node_print(mp_parse_node_t pn, size_t indent);

0 commit comments

Comments
 (0)