Skip to content

Commit 3816182

Browse files
committed
parser: Convert (u)int to mp_(u)int_t.
1 parent 54eb4e7 commit 3816182

6 files changed

Lines changed: 55 additions & 54 deletions

File tree

py/parse.c

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,20 @@ STATIC const rule_t *rules[] = {
109109
};
110110

111111
typedef struct _rule_stack_t {
112-
unsigned int src_line : 24;
113-
unsigned int rule_id : 8;
114-
int32_t arg_i; // what should be the size and signedness?
112+
mp_uint_t src_line : 24;
113+
mp_uint_t rule_id : 8;
114+
mp_uint_t arg_i : 32; // what should the bit-size be?
115115
} rule_stack_t;
116116

117117
typedef struct _parser_t {
118118
bool had_memory_error;
119119

120-
uint rule_stack_alloc;
121-
uint rule_stack_top;
120+
mp_uint_t rule_stack_alloc;
121+
mp_uint_t rule_stack_top;
122122
rule_stack_t *rule_stack;
123123

124-
uint result_stack_alloc;
125-
uint result_stack_top;
124+
mp_uint_t result_stack_alloc;
125+
mp_uint_t result_stack_top;
126126
mp_parse_node_t *result_stack;
127127

128128
mp_lexer_t *lexer;
@@ -132,7 +132,7 @@ STATIC inline void memory_error(parser_t *parser) {
132132
parser->had_memory_error = true;
133133
}
134134

135-
STATIC void push_rule(parser_t *parser, int src_line, const rule_t *rule, int arg_i) {
135+
STATIC void push_rule(parser_t *parser, mp_uint_t src_line, const rule_t *rule, mp_uint_t arg_i) {
136136
if (parser->had_memory_error) {
137137
return;
138138
}
@@ -151,14 +151,14 @@ STATIC void push_rule(parser_t *parser, int src_line, const rule_t *rule, int ar
151151
rs->arg_i = arg_i;
152152
}
153153

154-
STATIC void push_rule_from_arg(parser_t *parser, uint arg) {
154+
STATIC void push_rule_from_arg(parser_t *parser, mp_uint_t arg) {
155155
assert((arg & RULE_ARG_KIND_MASK) == RULE_ARG_RULE || (arg & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE);
156-
uint rule_id = arg & RULE_ARG_ARG_MASK;
156+
mp_uint_t rule_id = arg & RULE_ARG_ARG_MASK;
157157
assert(rule_id < RULE_maximum_number_of);
158158
push_rule(parser, mp_lexer_cur(parser->lexer)->src_line, rules[rule_id], 0);
159159
}
160160

161-
STATIC void pop_rule(parser_t *parser, const rule_t **rule, uint *arg_i, uint *src_line) {
161+
STATIC void pop_rule(parser_t *parser, const rule_t **rule, mp_uint_t *arg_i, mp_uint_t *src_line) {
162162
assert(!parser->had_memory_error);
163163
parser->rule_stack_top -= 1;
164164
*rule = rules[parser->rule_stack[parser->rule_stack_top].rule_id];
@@ -176,16 +176,16 @@ mp_parse_node_t mp_parse_node_new_leaf(mp_int_t kind, mp_int_t arg) {
176176
void mp_parse_node_free(mp_parse_node_t pn) {
177177
if (MP_PARSE_NODE_IS_STRUCT(pn)) {
178178
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
179-
uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
180-
uint rule_id = MP_PARSE_NODE_STRUCT_KIND(pns);
179+
mp_uint_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
180+
mp_uint_t rule_id = MP_PARSE_NODE_STRUCT_KIND(pns);
181181
if (rule_id == RULE_string) {
182182
return;
183183
}
184184
bool adjust = ADD_BLANK_NODE(rule_id);
185185
if (adjust) {
186186
n--;
187187
}
188-
for (uint i = 0; i < n; i++) {
188+
for (mp_uint_t i = 0; i < n; i++) {
189189
mp_parse_node_free(pns->nodes[i]);
190190
}
191191
if (adjust) {
@@ -196,13 +196,13 @@ void mp_parse_node_free(mp_parse_node_t pn) {
196196
}
197197

198198
#if MICROPY_DEBUG_PRINTERS
199-
void mp_parse_node_print(mp_parse_node_t pn, int indent) {
199+
void mp_parse_node_print(mp_parse_node_t pn, mp_uint_t indent) {
200200
if (MP_PARSE_NODE_IS_STRUCT(pn)) {
201201
printf("[% 4d] ", (int)((mp_parse_node_struct_t*)pn)->source_line);
202202
} else {
203203
printf(" ");
204204
}
205-
for (int i = 0; i < indent; i++) {
205+
for (mp_uint_t i = 0; i < indent; i++) {
206206
printf(" ");
207207
}
208208
if (MP_PARSE_NODE_IS_NULL(pn)) {
@@ -227,13 +227,13 @@ void mp_parse_node_print(mp_parse_node_t pn, int indent) {
227227
if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) {
228228
printf("literal str(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
229229
} else {
230-
uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
230+
mp_uint_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
231231
#ifdef USE_RULE_NAME
232-
printf("%s(%d) (n=%d)\n", rules[MP_PARSE_NODE_STRUCT_KIND(pns)]->rule_name, MP_PARSE_NODE_STRUCT_KIND(pns), n);
232+
printf("%s(" UINT_FMT ") (n=" UINT_FMT ")\n", rules[MP_PARSE_NODE_STRUCT_KIND(pns)]->rule_name, (mp_uint_t)MP_PARSE_NODE_STRUCT_KIND(pns), n);
233233
#else
234-
printf("rule(%u) (n=%d)\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns), n);
234+
printf("rule(" UINT_FMT ") (n=" UINT_FMT ")\n", (mp_uint_t)MP_PARSE_NODE_STRUCT_KIND(pns), n);
235235
#endif
236-
for (uint i = 0; i < n; i++) {
236+
for (mp_uint_t i = 0; i < n; i++) {
237237
mp_parse_node_print(pns->nodes[i], indent + 2);
238238
}
239239
}
@@ -244,7 +244,7 @@ void mp_parse_node_print(mp_parse_node_t pn, int indent) {
244244
/*
245245
STATIC void result_stack_show(parser_t *parser) {
246246
printf("result stack, most recent first\n");
247-
for (int i = parser->result_stack_top - 1; i >= 0; i--) {
247+
for (mp_int_t i = parser->result_stack_top - 1; i >= 0; i--) {
248248
mp_parse_node_print(parser->result_stack[i], 0);
249249
}
250250
}
@@ -258,7 +258,7 @@ STATIC mp_parse_node_t pop_result(parser_t *parser) {
258258
return parser->result_stack[--parser->result_stack_top];
259259
}
260260

261-
STATIC mp_parse_node_t peek_result(parser_t *parser, int pos) {
261+
STATIC mp_parse_node_t peek_result(parser_t *parser, mp_uint_t pos) {
262262
if (parser->had_memory_error) {
263263
return MP_PARSE_NODE_NULL;
264264
}
@@ -282,7 +282,7 @@ STATIC void push_result_node(parser_t *parser, mp_parse_node_t pn) {
282282
parser->result_stack[parser->result_stack_top++] = pn;
283283
}
284284

285-
STATIC void push_result_string(parser_t *parser, int src_line, const char *str, uint len) {
285+
STATIC void push_result_string(parser_t *parser, mp_uint_t src_line, const char *str, mp_uint_t len) {
286286
mp_parse_node_struct_t *pn = m_new_obj_var_maybe(mp_parse_node_struct_t, mp_parse_node_t, 2);
287287
if (pn == NULL) {
288288
memory_error(parser);
@@ -306,13 +306,13 @@ STATIC void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
306306
bool dec = false;
307307
bool small_int = true;
308308
mp_int_t int_val = 0;
309-
int len = tok->len;
309+
mp_uint_t len = tok->len;
310310
const char *str = tok->str;
311-
int base = 0;
312-
int i = mp_parse_num_base(str, len, &base);
311+
mp_uint_t base = 0;
312+
mp_uint_t i = mp_parse_num_base(str, len, &base);
313313
bool overflow = false;
314314
for (; i < len; i++) {
315-
int dig;
315+
mp_uint_t dig;
316316
if (unichar_isdigit(str[i]) && str[i] - '0' < base) {
317317
dig = str[i] - '0';
318318
} else if (base == 16 && 'a' <= str[i] && str[i] <= 'f') {
@@ -369,15 +369,15 @@ STATIC void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
369369
push_result_node(parser, pn);
370370
}
371371

372-
STATIC void push_result_rule(parser_t *parser, int src_line, const rule_t *rule, int num_args) {
372+
STATIC void push_result_rule(parser_t *parser, mp_uint_t src_line, const rule_t *rule, mp_uint_t num_args) {
373373
mp_parse_node_struct_t *pn = m_new_obj_var_maybe(mp_parse_node_struct_t, mp_parse_node_t, num_args);
374374
if (pn == NULL) {
375375
memory_error(parser);
376376
return;
377377
}
378378
pn->source_line = src_line;
379379
pn->kind_num_nodes = (rule->rule_id & 0xff) | (num_args << 8);
380-
for (int i = num_args; i > 0; i--) {
380+
for (mp_uint_t i = num_args; i > 0; i--) {
381381
pn->nodes[i - 1] = pop_result(parser);
382382
}
383383
push_result_node(parser, (mp_parse_node_t)pn);
@@ -407,7 +407,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
407407
}
408408

409409
// work out the top-level rule to use, and push it on the stack
410-
int top_level_rule;
410+
mp_uint_t top_level_rule;
411411
switch (input_kind) {
412412
case MP_PARSE_SINGLE_INPUT: top_level_rule = RULE_single_input; break;
413413
case MP_PARSE_EVAL_INPUT: top_level_rule = RULE_eval_input; break;
@@ -417,8 +417,8 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
417417

418418
// parse!
419419

420-
uint n, i; // state for the current rule
421-
uint rule_src_line; // source line for the first token matched by the current rule
420+
mp_uint_t n, i; // state for the current rule
421+
mp_uint_t rule_src_line; // source line for the first token matched by the current rule
422422
bool backtrack = false;
423423
const rule_t *rule = NULL;
424424
mp_token_kind_t tok_kind;
@@ -541,7 +541,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
541541
// count number of arguments for the parse_node
542542
i = 0;
543543
emit_rule = false;
544-
for (int x = 0; x < n; ++x) {
544+
for (mp_uint_t x = 0; x < n; ++x) {
545545
if ((rule->arg[x] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
546546
tok_kind = rule->arg[x] & RULE_ARG_ARG_MASK;
547547
if (tok_kind >= MP_TOKEN_NAME) {
@@ -589,8 +589,8 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
589589
i += 1;
590590
}
591591

592-
int num_not_nil = 0;
593-
for (int x = 0; x < i; ++x) {
592+
mp_uint_t num_not_nil = 0;
593+
for (mp_uint_t x = 0; x < i; ++x) {
594594
if (peek_result(&parser, x) != MP_PARSE_NODE_NULL) {
595595
num_not_nil += 1;
596596
}
@@ -605,7 +605,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
605605
} else if (num_not_nil == 1) {
606606
// single result, leave it on stack
607607
mp_parse_node_t pn = MP_PARSE_NODE_NULL;
608-
for (int x = 0; x < i; ++x) {
608+
for (mp_uint_t x = 0; x < i; ++x) {
609609
mp_parse_node_t pn2 = pop_result(&parser);
610610
if (pn2 != MP_PARSE_NODE_NULL) {
611611
pn = pn2;
@@ -653,7 +653,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
653653
}
654654
} else {
655655
for (;;) {
656-
uint arg = rule->arg[i & 1 & n];
656+
mp_uint_t arg = rule->arg[i & 1 & n];
657657
switch (arg & RULE_ARG_KIND_MASK) {
658658
case RULE_ARG_TOK:
659659
if (mp_lexer_is_kind(lex, arg & RULE_ARG_ARG_MASK)) {

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
mp_parse_node_t mp_parse_node_new_leaf(mp_int_t kind, mp_int_t arg);
7878
void mp_parse_node_free(mp_parse_node_t pn);
7979

80-
void mp_parse_node_print(mp_parse_node_t pn, int indent);
80+
void mp_parse_node_print(mp_parse_node_t pn, mp_uint_t indent);
8181

8282
typedef enum {
8383
MP_PARSE_SINGLE_INPUT,

py/parsenum.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <math.h>
4242
#endif
4343

44-
mp_obj_t mp_parse_num_integer(const char *restrict str_, uint len, int base) {
44+
mp_obj_t mp_parse_num_integer(const char *restrict str_, mp_uint_t len, mp_uint_t base) {
4545
const byte *restrict str = (const byte *)str_;
4646
const byte *restrict top = str + len;
4747
bool neg = false;
@@ -74,7 +74,7 @@ mp_obj_t mp_parse_num_integer(const char *restrict str_, uint len, int base) {
7474
const byte *restrict str_val_start = str;
7575
for (; str < top; str++) {
7676
// get next digit as a value
77-
int dig = *str;
77+
mp_uint_t dig = *str;
7878
if (unichar_isdigit(dig) && dig - '0' < base) {
7979
// 0-9 digit
8080
dig = dig - '0';
@@ -141,11 +141,13 @@ mp_obj_t mp_parse_num_integer(const char *restrict str_, uint len, int base) {
141141
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid syntax for integer with base %d: '%s'", base, str));
142142
}
143143

144-
#define PARSE_DEC_IN_INTG (1)
145-
#define PARSE_DEC_IN_FRAC (2)
146-
#define PARSE_DEC_IN_EXP (3)
144+
typedef enum {
145+
PARSE_DEC_IN_INTG,
146+
PARSE_DEC_IN_FRAC,
147+
PARSE_DEC_IN_EXP,
148+
} parse_dec_in_t;
147149

148-
mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex) {
150+
mp_obj_t mp_parse_num_decimal(const char *str, mp_uint_t len, bool allow_imag, bool force_complex) {
149151
#if MICROPY_PY_BUILTINS_FLOAT
150152
const char *top = str + len;
151153
mp_float_t dec_val = 0;
@@ -187,12 +189,12 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool f
187189
}
188190
} else {
189191
// string should be a decimal number
190-
int in = PARSE_DEC_IN_INTG;
192+
parse_dec_in_t in = PARSE_DEC_IN_INTG;
191193
bool exp_neg = false;
192-
int exp_val = 0;
193-
int exp_extra = 0;
194+
mp_int_t exp_val = 0;
195+
mp_int_t exp_extra = 0;
194196
for (; str < top; str++) {
195-
int dig = *str;
197+
mp_uint_t dig = *str;
196198
if ('0' <= dig && dig <= '9') {
197199
dig -= '0';
198200
if (in == PARSE_DEC_IN_EXP) {

py/parsenum.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base);
28-
mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex);
27+
mp_obj_t mp_parse_num_integer(const char *restrict str, mp_uint_t len, mp_uint_t base);
28+
mp_obj_t mp_parse_num_decimal(const char *str, mp_uint_t len, bool allow_imag, bool force_complex);

py/parsenumbase.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131
// find real radix base, and strip preceding '0x', '0o' and '0b'
3232
// puts base in *base, and returns number of bytes to skip the prefix
33-
int mp_parse_num_base(const char *str, uint len, int *base) {
33+
mp_uint_t mp_parse_num_base(const char *str, mp_uint_t len, mp_uint_t *base) {
3434
const byte *p = (const byte*)str;
35-
int c = *(p++);
35+
unichar c = *(p++);
3636
if ((*base == 0 || *base == 16) && c == '0') {
3737
c = *(p++);
3838
if ((c | 32) == 'x') {
@@ -65,4 +65,3 @@ int mp_parse_num_base(const char *str, uint len, int *base) {
6565
}
6666
return p - (const byte*)str;
6767
}
68-

py/parsenumbase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
int mp_parse_num_base(const char *str, uint len, int *base);
27+
mp_uint_t mp_parse_num_base(const char *str, mp_uint_t len, mp_uint_t *base);

0 commit comments

Comments
 (0)