Skip to content

Commit 54eb4e7

Browse files
committed
lexer: Convert type (u)int to mp_(u)int_t.
1 parent 40f3c02 commit 54eb4e7

File tree

5 files changed

+37
-36
lines changed

5 files changed

+37
-36
lines changed

py/lexer.c

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,25 @@ struct _mp_lexer_t {
5050

5151
unichar chr0, chr1, chr2; // current cached characters from source
5252

53-
uint line; // source line
54-
uint column; // source column
53+
mp_uint_t line; // source line
54+
mp_uint_t column; // source column
5555

56-
int emit_dent; // non-zero when there are INDENT/DEDENT tokens to emit
57-
int nested_bracket_level; // >0 when there are nested brackets over multiple lines
56+
mp_int_t emit_dent; // non-zero when there are INDENT/DEDENT tokens to emit
57+
mp_int_t nested_bracket_level; // >0 when there are nested brackets over multiple lines
5858

59-
uint alloc_indent_level;
60-
uint num_indent_level;
59+
mp_uint_t alloc_indent_level;
60+
mp_uint_t num_indent_level;
6161
uint16_t *indent_level;
6262

6363
vstr_t vstr;
6464
mp_token_t tok_cur;
6565
};
6666

67-
uint mp_optimise_value;
67+
mp_uint_t mp_optimise_value;
6868

6969
// TODO replace with a call to a standard function
70-
bool str_strn_equal(const char *str, const char *strn, int len) {
71-
uint i = 0;
70+
bool str_strn_equal(const char *str, const char *strn, mp_uint_t len) {
71+
mp_uint_t i = 0;
7272

7373
while (i < len && *str == *strn) {
7474
++i;
@@ -81,7 +81,7 @@ bool str_strn_equal(const char *str, const char *strn, int len) {
8181

8282
#ifdef MICROPY_DEBUG_PRINTERS
8383
void mp_token_show(const mp_token_t *tok) {
84-
printf("(%d:%d) kind:%d str:%p len:%d", tok->src_line, tok->src_column, tok->kind, tok->str, tok->len);
84+
printf("(" UINT_FMT ":" UINT_FMT ") kind:%u str:%p len:" UINT_FMT, tok->src_line, tok->src_column, tok->kind, tok->str, tok->len);
8585
if (tok->str != NULL && tok->len > 0) {
8686
const byte *i = (const byte *)tok->str;
8787
const byte *j = (const byte *)i + tok->len;
@@ -175,7 +175,7 @@ STATIC void next_char(mp_lexer_t *lex) {
175175
return;
176176
}
177177

178-
int advance = 1;
178+
mp_uint_t advance = 1;
179179

180180
if (lex->chr0 == '\n') {
181181
// LF is a new line
@@ -210,7 +210,7 @@ STATIC void next_char(mp_lexer_t *lex) {
210210
}
211211
}
212212

213-
void indent_push(mp_lexer_t *lex, uint indent) {
213+
void indent_push(mp_lexer_t *lex, mp_uint_t indent) {
214214
if (lex->num_indent_level >= lex->alloc_indent_level) {
215215
// TODO use m_renew_maybe and somehow indicate an error if it fails... probably by using MP_TOKEN_MEMORY_ERROR
216216
lex->indent_level = m_renew(uint16_t, lex->indent_level, lex->alloc_indent_level, lex->alloc_indent_level + MICROPY_ALLOC_LEXEL_INDENT_INC);
@@ -219,7 +219,7 @@ void indent_push(mp_lexer_t *lex, uint indent) {
219219
lex->indent_level[lex->num_indent_level++] = indent;
220220
}
221221

222-
uint indent_top(mp_lexer_t *lex) {
222+
mp_uint_t indent_top(mp_lexer_t *lex) {
223223
return lex->indent_level[lex->num_indent_level - 1];
224224
}
225225

@@ -308,9 +308,9 @@ STATIC const char *tok_kw[] = {
308308
"__debug__",
309309
};
310310

311-
STATIC int hex_digit(unichar c) {
311+
STATIC mp_uint_t hex_digit(unichar c) {
312312
// c is assumed to be hex digit
313-
int n = c - '0';
313+
mp_uint_t n = c - '0';
314314
if (n > 9) {
315315
n &= ~('a' - 'A');
316316
n -= ('A' - ('9' + 1));
@@ -320,8 +320,9 @@ STATIC int hex_digit(unichar c) {
320320

321321
// This is called with CUR_CHAR() before first hex digit, and should return with
322322
// it pointing to last hex digit
323-
STATIC bool get_hex(mp_lexer_t *lex, int num_digits, uint *result) {
324-
uint num = 0;
323+
// num_digits must be greater than zero
324+
STATIC bool get_hex(mp_lexer_t *lex, mp_uint_t num_digits, mp_uint_t *result) {
325+
mp_uint_t num = 0;
325326
while (num_digits-- != 0) {
326327
next_char(lex);
327328
unichar c = CUR_CHAR(lex);
@@ -394,7 +395,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
394395
} else if (had_physical_newline && lex->nested_bracket_level == 0) {
395396
tok->kind = MP_TOKEN_NEWLINE;
396397

397-
uint num_spaces = lex->column - 1;
398+
mp_uint_t num_spaces = lex->column - 1;
398399
lex->emit_dent = 0;
399400
if (num_spaces == indent_top(lex)) {
400401
} else if (num_spaces > indent_top(lex)) {
@@ -463,7 +464,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
463464
next_char(lex);
464465

465466
// work out if it's a single or triple quoted literal
466-
int num_quotes;
467+
mp_uint_t num_quotes;
467468
if (is_char_and(lex, quote_char, quote_char)) {
468469
// triple quotes
469470
next_char(lex);
@@ -475,7 +476,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
475476
}
476477

477478
// parse the literal
478-
int n_closing = 0;
479+
mp_uint_t n_closing = 0;
479480
while (!is_end(lex) && (num_quotes > 1 || !is_char(lex, '\n')) && n_closing < num_quotes) {
480481
if (is_char(lex, quote_char)) {
481482
n_closing += 1;
@@ -512,7 +513,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
512513
// Otherwise fall through.
513514
case 'x':
514515
{
515-
uint num = 0;
516+
mp_uint_t num = 0;
516517
if (!get_hex(lex, (c == 'x' ? 2 : c == 'u' ? 4 : 8), &num)) {
517518
// TODO error message
518519
assert(0);
@@ -531,8 +532,8 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
531532
default:
532533
if (c >= '0' && c <= '7') {
533534
// Octal sequence, 1-3 chars
534-
int digits = 3;
535-
int num = c - '0';
535+
mp_uint_t digits = 3;
536+
mp_uint_t num = c - '0';
536537
while (is_following_odigit(lex) && --digits != 0) {
537538
next_char(lex);
538539
num = num * 8 + (CUR_CHAR(lex) - '0');
@@ -627,7 +628,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
627628
// search for encoded delimiter or operator
628629

629630
const char *t = tok_enc;
630-
uint tok_enc_index = 0;
631+
mp_uint_t tok_enc_index = 0;
631632
for (; *t != 0 && !is_char(lex, *t); t += 1) {
632633
if (*t == 'e' || *t == 'c') {
633634
t += 1;
@@ -649,7 +650,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
649650

650651
// get the maximum characters for a valid token
651652
t += 1;
652-
uint t_index = tok_enc_index;
653+
mp_uint_t t_index = tok_enc_index;
653654
for (;;) {
654655
for (; *t == 'e'; t += 1) {
655656
t += 1;
@@ -712,8 +713,8 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
712713
// the parser gives a syntax error on, eg, x.__debug__. Otherwise, we
713714
// need to check for this special token in many places in the compiler.
714715
// TODO improve speed of these string comparisons
715-
//for (int i = 0; tok_kw[i] != NULL; i++) {
716-
for (int i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
716+
//for (mp_int_t i = 0; tok_kw[i] != NULL; i++) {
717+
for (mp_int_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
717718
if (str_strn_equal(tok_kw[i], tok->str, tok->len)) {
718719
if (i == MP_ARRAY_SIZE(tok_kw) - 1) {
719720
// tok_kw[MP_ARRAY_SIZE(tok_kw) - 1] == "__debug__"

py/lexer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ typedef enum _mp_token_kind_t {
131131
} mp_token_kind_t;
132132

133133
typedef struct _mp_token_t {
134-
uint src_line; // source line
135-
uint src_column; // source column
134+
mp_uint_t src_line; // source line
135+
mp_uint_t src_column; // source column
136136

137137
mp_token_kind_t kind; // kind of token
138138
const char *str; // string of token (valid only while this token is current token)
139-
uint len; // (byte) length of string of token
139+
mp_uint_t len; // (byte) length of string of token
140140
} mp_token_t;
141141

142142
// the next-char function must return the next character in the stream
@@ -151,7 +151,7 @@ typedef struct _mp_lexer_t mp_lexer_t;
151151
void mp_token_show(const mp_token_t *tok);
152152

153153
mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_char_t stream_next_char, mp_lexer_stream_close_t stream_close);
154-
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, uint len, uint free_len);
154+
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len);
155155

156156
void mp_lexer_free(mp_lexer_t *lex);
157157
qstr mp_lexer_source_name(mp_lexer_t *lex);
@@ -177,4 +177,4 @@ typedef enum {
177177
mp_import_stat_t mp_import_stat(const char *path);
178178
mp_lexer_t *mp_lexer_new_from_file(const char *filename);
179179

180-
extern uint mp_optimise_value;
180+
extern mp_uint_t mp_optimise_value;

py/lexerstr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "lexer.h"
3131

3232
typedef struct _mp_lexer_str_buf_t {
33-
uint free_len; // if > 0, src_beg will be freed when done by: m_free(src_beg, free_len)
33+
mp_uint_t free_len; // if > 0, src_beg will be freed when done by: m_free(src_beg, free_len)
3434
const char *src_beg; // beginning of source
3535
const char *src_cur; // current location in source
3636
const char *src_end; // end (exclusive) of source
@@ -51,7 +51,7 @@ STATIC void str_buf_free(mp_lexer_str_buf_t *sb) {
5151
m_del_obj(mp_lexer_str_buf_t, sb);
5252
}
5353

54-
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, uint len, uint free_len) {
54+
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len) {
5555
mp_lexer_str_buf_t *sb = m_new_obj(mp_lexer_str_buf_t);
5656
sb->free_len = free_len;
5757
sb->src_beg = str;

py/parsehelper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#define STR_INVALID_SYNTAX "invalid syntax"
4444

4545
void mp_parse_show_exception(mp_lexer_t *lex, mp_parse_error_kind_t parse_error_kind) {
46-
printf(" File \"%s\", line %d, column %d\n", qstr_str(mp_lexer_source_name(lex)), mp_lexer_cur(lex)->src_line, mp_lexer_cur(lex)->src_column);
46+
printf(" File \"%s\", line " UINT_FMT ", column " UINT_FMT "\n", qstr_str(mp_lexer_source_name(lex)), mp_lexer_cur(lex)->src_line, mp_lexer_cur(lex)->src_column);
4747
switch (parse_error_kind) {
4848
case MP_PARSE_ERROR_MEMORY:
4949
printf("MemoryError: %s\n", STR_MEMORY);

teensy/lexermemzip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ mp_lexer_t *mp_lexer_new_from_file(const char *filename)
1616
return NULL;
1717
}
1818

19-
return mp_lexer_new_from_str_len(qstr_from_str(filename), (const char *)data, (uint)len, 0);
19+
return mp_lexer_new_from_str_len(qstr_from_str(filename), (const char *)data, (mp_uint_t)len, 0);
2020
}
2121

0 commit comments

Comments
 (0)