parser: Convert (u)int to mp_(u)int_t.

This commit is contained in:
Damien George 2014-07-03 14:13:33 +01:00
parent 54eb4e723e
commit 381618269a
6 changed files with 55 additions and 54 deletions

View File

@ -109,20 +109,20 @@ STATIC const rule_t *rules[] = {
}; };
typedef struct _rule_stack_t { typedef struct _rule_stack_t {
unsigned int src_line : 24; mp_uint_t src_line : 24;
unsigned int rule_id : 8; mp_uint_t rule_id : 8;
int32_t arg_i; // what should be the size and signedness? mp_uint_t arg_i : 32; // what should the bit-size be?
} rule_stack_t; } rule_stack_t;
typedef struct _parser_t { typedef struct _parser_t {
bool had_memory_error; bool had_memory_error;
uint rule_stack_alloc; mp_uint_t rule_stack_alloc;
uint rule_stack_top; mp_uint_t rule_stack_top;
rule_stack_t *rule_stack; rule_stack_t *rule_stack;
uint result_stack_alloc; mp_uint_t result_stack_alloc;
uint result_stack_top; mp_uint_t result_stack_top;
mp_parse_node_t *result_stack; mp_parse_node_t *result_stack;
mp_lexer_t *lexer; mp_lexer_t *lexer;
@ -132,7 +132,7 @@ STATIC inline void memory_error(parser_t *parser) {
parser->had_memory_error = true; parser->had_memory_error = true;
} }
STATIC void push_rule(parser_t *parser, int src_line, const rule_t *rule, int arg_i) { STATIC void push_rule(parser_t *parser, mp_uint_t src_line, const rule_t *rule, mp_uint_t arg_i) {
if (parser->had_memory_error) { if (parser->had_memory_error) {
return; return;
} }
@ -151,14 +151,14 @@ STATIC void push_rule(parser_t *parser, int src_line, const rule_t *rule, int ar
rs->arg_i = arg_i; rs->arg_i = arg_i;
} }
STATIC void push_rule_from_arg(parser_t *parser, uint arg) { STATIC void push_rule_from_arg(parser_t *parser, mp_uint_t arg) {
assert((arg & RULE_ARG_KIND_MASK) == RULE_ARG_RULE || (arg & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE); assert((arg & RULE_ARG_KIND_MASK) == RULE_ARG_RULE || (arg & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE);
uint rule_id = arg & RULE_ARG_ARG_MASK; mp_uint_t rule_id = arg & RULE_ARG_ARG_MASK;
assert(rule_id < RULE_maximum_number_of); assert(rule_id < RULE_maximum_number_of);
push_rule(parser, mp_lexer_cur(parser->lexer)->src_line, rules[rule_id], 0); push_rule(parser, mp_lexer_cur(parser->lexer)->src_line, rules[rule_id], 0);
} }
STATIC void pop_rule(parser_t *parser, const rule_t **rule, uint *arg_i, uint *src_line) { STATIC void pop_rule(parser_t *parser, const rule_t **rule, mp_uint_t *arg_i, mp_uint_t *src_line) {
assert(!parser->had_memory_error); assert(!parser->had_memory_error);
parser->rule_stack_top -= 1; parser->rule_stack_top -= 1;
*rule = rules[parser->rule_stack[parser->rule_stack_top].rule_id]; *rule = rules[parser->rule_stack[parser->rule_stack_top].rule_id];
@ -176,8 +176,8 @@ mp_parse_node_t mp_parse_node_new_leaf(mp_int_t kind, mp_int_t arg) {
void mp_parse_node_free(mp_parse_node_t pn) { void mp_parse_node_free(mp_parse_node_t pn) {
if (MP_PARSE_NODE_IS_STRUCT(pn)) { if (MP_PARSE_NODE_IS_STRUCT(pn)) {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn; mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); mp_uint_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
uint rule_id = MP_PARSE_NODE_STRUCT_KIND(pns); mp_uint_t rule_id = MP_PARSE_NODE_STRUCT_KIND(pns);
if (rule_id == RULE_string) { if (rule_id == RULE_string) {
return; return;
} }
@ -185,7 +185,7 @@ void mp_parse_node_free(mp_parse_node_t pn) {
if (adjust) { if (adjust) {
n--; n--;
} }
for (uint i = 0; i < n; i++) { for (mp_uint_t i = 0; i < n; i++) {
mp_parse_node_free(pns->nodes[i]); mp_parse_node_free(pns->nodes[i]);
} }
if (adjust) { if (adjust) {
@ -196,13 +196,13 @@ void mp_parse_node_free(mp_parse_node_t pn) {
} }
#if MICROPY_DEBUG_PRINTERS #if MICROPY_DEBUG_PRINTERS
void mp_parse_node_print(mp_parse_node_t pn, int indent) { void mp_parse_node_print(mp_parse_node_t pn, mp_uint_t indent) {
if (MP_PARSE_NODE_IS_STRUCT(pn)) { if (MP_PARSE_NODE_IS_STRUCT(pn)) {
printf("[% 4d] ", (int)((mp_parse_node_struct_t*)pn)->source_line); printf("[% 4d] ", (int)((mp_parse_node_struct_t*)pn)->source_line);
} else { } else {
printf(" "); printf(" ");
} }
for (int i = 0; i < indent; i++) { for (mp_uint_t i = 0; i < indent; i++) {
printf(" "); printf(" ");
} }
if (MP_PARSE_NODE_IS_NULL(pn)) { if (MP_PARSE_NODE_IS_NULL(pn)) {
@ -227,13 +227,13 @@ void mp_parse_node_print(mp_parse_node_t pn, int indent) {
if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) { if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) {
printf("literal str(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]); printf("literal str(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
} else { } else {
uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); mp_uint_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
#ifdef USE_RULE_NAME #ifdef USE_RULE_NAME
printf("%s(%d) (n=%d)\n", rules[MP_PARSE_NODE_STRUCT_KIND(pns)]->rule_name, MP_PARSE_NODE_STRUCT_KIND(pns), n); 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);
#else #else
printf("rule(%u) (n=%d)\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns), n); printf("rule(" UINT_FMT ") (n=" UINT_FMT ")\n", (mp_uint_t)MP_PARSE_NODE_STRUCT_KIND(pns), n);
#endif #endif
for (uint i = 0; i < n; i++) { for (mp_uint_t i = 0; i < n; i++) {
mp_parse_node_print(pns->nodes[i], indent + 2); mp_parse_node_print(pns->nodes[i], indent + 2);
} }
} }
@ -244,7 +244,7 @@ void mp_parse_node_print(mp_parse_node_t pn, int indent) {
/* /*
STATIC void result_stack_show(parser_t *parser) { STATIC void result_stack_show(parser_t *parser) {
printf("result stack, most recent first\n"); printf("result stack, most recent first\n");
for (int i = parser->result_stack_top - 1; i >= 0; i--) { for (mp_int_t i = parser->result_stack_top - 1; i >= 0; i--) {
mp_parse_node_print(parser->result_stack[i], 0); mp_parse_node_print(parser->result_stack[i], 0);
} }
} }
@ -258,7 +258,7 @@ STATIC mp_parse_node_t pop_result(parser_t *parser) {
return parser->result_stack[--parser->result_stack_top]; return parser->result_stack[--parser->result_stack_top];
} }
STATIC mp_parse_node_t peek_result(parser_t *parser, int pos) { STATIC mp_parse_node_t peek_result(parser_t *parser, mp_uint_t pos) {
if (parser->had_memory_error) { if (parser->had_memory_error) {
return MP_PARSE_NODE_NULL; return MP_PARSE_NODE_NULL;
} }
@ -282,7 +282,7 @@ STATIC void push_result_node(parser_t *parser, mp_parse_node_t pn) {
parser->result_stack[parser->result_stack_top++] = pn; parser->result_stack[parser->result_stack_top++] = pn;
} }
STATIC void push_result_string(parser_t *parser, int src_line, const char *str, uint len) { STATIC void push_result_string(parser_t *parser, mp_uint_t src_line, const char *str, mp_uint_t len) {
mp_parse_node_struct_t *pn = m_new_obj_var_maybe(mp_parse_node_struct_t, mp_parse_node_t, 2); mp_parse_node_struct_t *pn = m_new_obj_var_maybe(mp_parse_node_struct_t, mp_parse_node_t, 2);
if (pn == NULL) { if (pn == NULL) {
memory_error(parser); memory_error(parser);
@ -306,13 +306,13 @@ STATIC void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
bool dec = false; bool dec = false;
bool small_int = true; bool small_int = true;
mp_int_t int_val = 0; mp_int_t int_val = 0;
int len = tok->len; mp_uint_t len = tok->len;
const char *str = tok->str; const char *str = tok->str;
int base = 0; mp_uint_t base = 0;
int i = mp_parse_num_base(str, len, &base); mp_uint_t i = mp_parse_num_base(str, len, &base);
bool overflow = false; bool overflow = false;
for (; i < len; i++) { for (; i < len; i++) {
int dig; mp_uint_t dig;
if (unichar_isdigit(str[i]) && str[i] - '0' < base) { if (unichar_isdigit(str[i]) && str[i] - '0' < base) {
dig = str[i] - '0'; dig = str[i] - '0';
} else if (base == 16 && 'a' <= str[i] && str[i] <= 'f') { } else if (base == 16 && 'a' <= str[i] && str[i] <= 'f') {
@ -369,7 +369,7 @@ STATIC void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
push_result_node(parser, pn); push_result_node(parser, pn);
} }
STATIC void push_result_rule(parser_t *parser, int src_line, const rule_t *rule, int num_args) { STATIC void push_result_rule(parser_t *parser, mp_uint_t src_line, const rule_t *rule, mp_uint_t num_args) {
mp_parse_node_struct_t *pn = m_new_obj_var_maybe(mp_parse_node_struct_t, mp_parse_node_t, num_args); mp_parse_node_struct_t *pn = m_new_obj_var_maybe(mp_parse_node_struct_t, mp_parse_node_t, num_args);
if (pn == NULL) { if (pn == NULL) {
memory_error(parser); memory_error(parser);
@ -377,7 +377,7 @@ STATIC void push_result_rule(parser_t *parser, int src_line, const rule_t *rule,
} }
pn->source_line = src_line; pn->source_line = src_line;
pn->kind_num_nodes = (rule->rule_id & 0xff) | (num_args << 8); pn->kind_num_nodes = (rule->rule_id & 0xff) | (num_args << 8);
for (int i = num_args; i > 0; i--) { for (mp_uint_t i = num_args; i > 0; i--) {
pn->nodes[i - 1] = pop_result(parser); pn->nodes[i - 1] = pop_result(parser);
} }
push_result_node(parser, (mp_parse_node_t)pn); 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
} }
// work out the top-level rule to use, and push it on the stack // work out the top-level rule to use, and push it on the stack
int top_level_rule; mp_uint_t top_level_rule;
switch (input_kind) { switch (input_kind) {
case MP_PARSE_SINGLE_INPUT: top_level_rule = RULE_single_input; break; case MP_PARSE_SINGLE_INPUT: top_level_rule = RULE_single_input; break;
case MP_PARSE_EVAL_INPUT: top_level_rule = RULE_eval_input; break; 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
// parse! // parse!
uint n, i; // state for the current rule mp_uint_t n, i; // state for the current rule
uint rule_src_line; // source line for the first token matched by the current rule mp_uint_t rule_src_line; // source line for the first token matched by the current rule
bool backtrack = false; bool backtrack = false;
const rule_t *rule = NULL; const rule_t *rule = NULL;
mp_token_kind_t tok_kind; 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
// count number of arguments for the parse_node // count number of arguments for the parse_node
i = 0; i = 0;
emit_rule = false; emit_rule = false;
for (int x = 0; x < n; ++x) { for (mp_uint_t x = 0; x < n; ++x) {
if ((rule->arg[x] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) { if ((rule->arg[x] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
tok_kind = rule->arg[x] & RULE_ARG_ARG_MASK; tok_kind = rule->arg[x] & RULE_ARG_ARG_MASK;
if (tok_kind >= MP_TOKEN_NAME) { 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
i += 1; i += 1;
} }
int num_not_nil = 0; mp_uint_t num_not_nil = 0;
for (int x = 0; x < i; ++x) { for (mp_uint_t x = 0; x < i; ++x) {
if (peek_result(&parser, x) != MP_PARSE_NODE_NULL) { if (peek_result(&parser, x) != MP_PARSE_NODE_NULL) {
num_not_nil += 1; num_not_nil += 1;
} }
@ -605,7 +605,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
} else if (num_not_nil == 1) { } else if (num_not_nil == 1) {
// single result, leave it on stack // single result, leave it on stack
mp_parse_node_t pn = MP_PARSE_NODE_NULL; mp_parse_node_t pn = MP_PARSE_NODE_NULL;
for (int x = 0; x < i; ++x) { for (mp_uint_t x = 0; x < i; ++x) {
mp_parse_node_t pn2 = pop_result(&parser); mp_parse_node_t pn2 = pop_result(&parser);
if (pn2 != MP_PARSE_NODE_NULL) { if (pn2 != MP_PARSE_NODE_NULL) {
pn = pn2; 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
} }
} else { } else {
for (;;) { for (;;) {
uint arg = rule->arg[i & 1 & n]; mp_uint_t arg = rule->arg[i & 1 & n];
switch (arg & RULE_ARG_KIND_MASK) { switch (arg & RULE_ARG_KIND_MASK) {
case RULE_ARG_TOK: case RULE_ARG_TOK:
if (mp_lexer_is_kind(lex, arg & RULE_ARG_ARG_MASK)) { if (mp_lexer_is_kind(lex, arg & RULE_ARG_ARG_MASK)) {

View File

@ -77,7 +77,7 @@ typedef struct _mp_parse_node_struct_t {
mp_parse_node_t mp_parse_node_new_leaf(mp_int_t kind, mp_int_t arg); mp_parse_node_t mp_parse_node_new_leaf(mp_int_t kind, mp_int_t arg);
void mp_parse_node_free(mp_parse_node_t pn); void mp_parse_node_free(mp_parse_node_t pn);
void mp_parse_node_print(mp_parse_node_t pn, int indent); void mp_parse_node_print(mp_parse_node_t pn, mp_uint_t indent);
typedef enum { typedef enum {
MP_PARSE_SINGLE_INPUT, MP_PARSE_SINGLE_INPUT,

View File

@ -41,7 +41,7 @@
#include <math.h> #include <math.h>
#endif #endif
mp_obj_t mp_parse_num_integer(const char *restrict str_, uint len, int base) { mp_obj_t mp_parse_num_integer(const char *restrict str_, mp_uint_t len, mp_uint_t base) {
const byte *restrict str = (const byte *)str_; const byte *restrict str = (const byte *)str_;
const byte *restrict top = str + len; const byte *restrict top = str + len;
bool neg = false; bool neg = false;
@ -74,7 +74,7 @@ mp_obj_t mp_parse_num_integer(const char *restrict str_, uint len, int base) {
const byte *restrict str_val_start = str; const byte *restrict str_val_start = str;
for (; str < top; str++) { for (; str < top; str++) {
// get next digit as a value // get next digit as a value
int dig = *str; mp_uint_t dig = *str;
if (unichar_isdigit(dig) && dig - '0' < base) { if (unichar_isdigit(dig) && dig - '0' < base) {
// 0-9 digit // 0-9 digit
dig = dig - '0'; dig = dig - '0';
@ -141,11 +141,13 @@ value_error:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid syntax for integer with base %d: '%s'", base, str)); nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid syntax for integer with base %d: '%s'", base, str));
} }
#define PARSE_DEC_IN_INTG (1) typedef enum {
#define PARSE_DEC_IN_FRAC (2) PARSE_DEC_IN_INTG,
#define PARSE_DEC_IN_EXP (3) PARSE_DEC_IN_FRAC,
PARSE_DEC_IN_EXP,
} parse_dec_in_t;
mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex) { mp_obj_t mp_parse_num_decimal(const char *str, mp_uint_t len, bool allow_imag, bool force_complex) {
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
const char *top = str + len; const char *top = str + len;
mp_float_t dec_val = 0; 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
} }
} else { } else {
// string should be a decimal number // string should be a decimal number
int in = PARSE_DEC_IN_INTG; parse_dec_in_t in = PARSE_DEC_IN_INTG;
bool exp_neg = false; bool exp_neg = false;
int exp_val = 0; mp_int_t exp_val = 0;
int exp_extra = 0; mp_int_t exp_extra = 0;
for (; str < top; str++) { for (; str < top; str++) {
int dig = *str; mp_uint_t dig = *str;
if ('0' <= dig && dig <= '9') { if ('0' <= dig && dig <= '9') {
dig -= '0'; dig -= '0';
if (in == PARSE_DEC_IN_EXP) { if (in == PARSE_DEC_IN_EXP) {

View File

@ -24,5 +24,5 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base); mp_obj_t mp_parse_num_integer(const char *restrict str, mp_uint_t len, mp_uint_t base);
mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex); mp_obj_t mp_parse_num_decimal(const char *str, mp_uint_t len, bool allow_imag, bool force_complex);

View File

@ -30,9 +30,9 @@
// find real radix base, and strip preceding '0x', '0o' and '0b' // find real radix base, and strip preceding '0x', '0o' and '0b'
// puts base in *base, and returns number of bytes to skip the prefix // puts base in *base, and returns number of bytes to skip the prefix
int mp_parse_num_base(const char *str, uint len, int *base) { mp_uint_t mp_parse_num_base(const char *str, mp_uint_t len, mp_uint_t *base) {
const byte *p = (const byte*)str; const byte *p = (const byte*)str;
int c = *(p++); unichar c = *(p++);
if ((*base == 0 || *base == 16) && c == '0') { if ((*base == 0 || *base == 16) && c == '0') {
c = *(p++); c = *(p++);
if ((c | 32) == 'x') { if ((c | 32) == 'x') {
@ -65,4 +65,3 @@ int mp_parse_num_base(const char *str, uint len, int *base) {
} }
return p - (const byte*)str; return p - (const byte*)str;
} }

View File

@ -24,4 +24,4 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
int mp_parse_num_base(const char *str, uint len, int *base); mp_uint_t mp_parse_num_base(const char *str, mp_uint_t len, mp_uint_t *base);