Skip to content

Commit dfe944c

Browse files
committed
py: Expose compile.c:list_get as mp_parse_node_extract_list.
1 parent 8dfbd2d commit dfe944c

3 files changed

Lines changed: 36 additions & 36 deletions

File tree

py/compile.c

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -400,25 +400,6 @@ STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kin
400400
}
401401
}
402402

403-
STATIC int list_get(mp_parse_node_t *pn, pn_kind_t pn_kind, mp_parse_node_t **nodes) {
404-
if (MP_PARSE_NODE_IS_NULL(*pn)) {
405-
*nodes = NULL;
406-
return 0;
407-
} else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
408-
*nodes = pn;
409-
return 1;
410-
} else {
411-
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
412-
if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
413-
*nodes = pn;
414-
return 1;
415-
} else {
416-
*nodes = pns->nodes;
417-
return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
418-
}
419-
}
420-
}
421-
422403
STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
423404
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
424405
for (int i = 0; i < num_nodes; i++) {
@@ -1213,7 +1194,7 @@ STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_
12131194
STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
12141195
// get the list of decorators
12151196
mp_parse_node_t *nodes;
1216-
int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1197+
int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
12171198

12181199
// inherit emit options for this function/class definition
12191200
uint emit_options = comp->scope_cur->emit_options;
@@ -1226,7 +1207,7 @@ STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
12261207

12271208
// nodes[0] contains the decorator function, which is a dotted name
12281209
mp_parse_node_t *name_nodes;
1229-
int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1210+
int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
12301211

12311212
// check for built-in decorators
12321213
if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
@@ -1529,7 +1510,7 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
15291510

15301511
// get the list of . and/or ...'s
15311512
mp_parse_node_t *nodes;
1532-
int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1513+
int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
15331514

15341515
// count the total number of .'s
15351516
for (int i = 0; i < n; i++) {
@@ -1563,7 +1544,7 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
15631544

15641545
// build the "fromlist" tuple
15651546
mp_parse_node_t *pn_nodes;
1566-
int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
1547+
int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
15671548
#if MICROPY_EMIT_CPYTHON
15681549
{
15691550
vstr_t *vstr = vstr_new();
@@ -1635,7 +1616,7 @@ STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qs
16351616
STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
16361617
if (comp->pass == MP_PASS_SCOPE) {
16371618
mp_parse_node_t *nodes;
1638-
int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1619+
int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
16391620
for (int i = 0; i < n; i++) {
16401621
compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
16411622
}
@@ -1665,7 +1646,7 @@ STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns)
16651646
return;
16661647
}
16671648
mp_parse_node_t *nodes;
1668-
int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1649+
int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
16691650
for (int i = 0; i < n; i++) {
16701651
compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
16711652
}
@@ -1717,7 +1698,7 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
17171698

17181699
// compile elif blocks (if any)
17191700
mp_parse_node_t *pn_elif;
1720-
int n_elif = list_get(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1701+
int n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
17211702
for (int i = 0; i < n_elif; i++) {
17221703
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
17231704
mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
@@ -1900,7 +1881,7 @@ STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
19001881
&& MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
19011882
mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
19021883
mp_parse_node_t *args;
1903-
int n_args = list_get(&pn_range_args, PN_arglist, &args);
1884+
int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
19041885
mp_parse_node_t pn_range_start;
19051886
mp_parse_node_t pn_range_end;
19061887
mp_parse_node_t pn_range_step;
@@ -2094,7 +2075,7 @@ STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
20942075
} else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
20952076
// try-except and possibly else and/or finally
20962077
mp_parse_node_t *pn_excepts;
2097-
int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
2078+
int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
20982079
if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
20992080
// no finally
21002081
compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
@@ -2105,7 +2086,7 @@ STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
21052086
} else {
21062087
// just try-except
21072088
mp_parse_node_t *pn_excepts;
2108-
int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
2089+
int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
21092090
compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
21102091
}
21112092
} else {
@@ -2148,7 +2129,7 @@ STATIC void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *n
21482129
STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
21492130
// get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
21502131
mp_parse_node_t *nodes;
2151-
int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2132+
int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
21522133
assert(n > 0);
21532134

21542135
// compile in a nested fashion
@@ -2509,7 +2490,7 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar
25092490

25102491
// get the list of arguments
25112492
mp_parse_node_t *args;
2512-
int n_args = list_get(&pn_arglist, PN_arglist, &args);
2493+
int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
25132494

25142495
// compile the arguments
25152496
// Rather than calling compile_node on the list, we go through the list of args
@@ -2771,7 +2752,7 @@ STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
27712752

27722753
// get tail elements (2nd, 3rd, ...)
27732754
mp_parse_node_t *nodes;
2774-
int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2755+
int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
27752756

27762757
// first element sets whether it's a dict or set
27772758
bool is_dict;
@@ -3439,7 +3420,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
34393420
// parameters are in pns->nodes[1]
34403421
if (comp->pass == MP_PASS_CODE_SIZE) {
34413422
mp_parse_node_t *pn_params;
3442-
int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
3423+
int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
34433424
scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
34443425
if (comp->compile_error != MP_OBJ_NULL) {
34453426
goto inline_asm_error;
@@ -3450,7 +3431,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
34503431

34513432
mp_parse_node_t pn_body = pns->nodes[3]; // body
34523433
mp_parse_node_t *nodes;
3453-
int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3434+
int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
34543435

34553436
/*
34563437
if (comp->pass == MP_PASS_EMIT) {
@@ -3482,7 +3463,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
34823463
qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
34833464
pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
34843465
mp_parse_node_t *pn_arg;
3485-
int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3466+
int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
34863467

34873468
// emit instructions
34883469
if (op == MP_QSTR_label) {

py/parse.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,25 @@ void mp_parse_node_free(mp_parse_node_t pn) {
199199
}
200200
}
201201

202+
int mp_parse_node_extract_list(mp_parse_node_t *pn, mp_uint_t pn_kind, mp_parse_node_t **nodes) {
203+
if (MP_PARSE_NODE_IS_NULL(*pn)) {
204+
*nodes = NULL;
205+
return 0;
206+
} else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
207+
*nodes = pn;
208+
return 1;
209+
} else {
210+
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
211+
if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
212+
*nodes = pn;
213+
return 1;
214+
} else {
215+
*nodes = pns->nodes;
216+
return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
217+
}
218+
}
219+
}
220+
202221
#if MICROPY_DEBUG_PRINTERS
203222
void mp_parse_node_print(mp_parse_node_t pn, mp_uint_t indent) {
204223
if (MP_PARSE_NODE_IS_STRUCT(pn)) {

py/parse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ typedef struct _mp_parse_node_struct_t {
7777

7878
mp_parse_node_t mp_parse_node_new_leaf(mp_int_t kind, mp_int_t arg);
7979
void mp_parse_node_free(mp_parse_node_t pn);
80-
80+
int mp_parse_node_extract_list(mp_parse_node_t *pn, mp_uint_t pn_kind, mp_parse_node_t **nodes);
8181
void mp_parse_node_print(mp_parse_node_t pn, mp_uint_t indent);
8282

8383
typedef enum {

0 commit comments

Comments
 (0)