diff options
author | Freya Murphy <freya@freyacat.org> | 2024-09-13 11:11:18 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-09-13 11:11:18 -0400 |
commit | df4a225ccf79dd9f5fa3faef4fd68ae87471f0ca (patch) | |
tree | 44629775a57e229024f56d087ab5d1aebed978d3 /masm/lex.c | |
parent | dont upload those :p (diff) | |
download | mips-df4a225ccf79dd9f5fa3faef4fd68ae87471f0ca.tar.gz mips-df4a225ccf79dd9f5fa3faef4fd68ae87471f0ca.tar.bz2 mips-df4a225ccf79dd9f5fa3faef4fd68ae87471f0ca.zip |
better
Diffstat (limited to 'masm/lex.c')
-rw-r--r-- | masm/lex.c | 32 |
1 files changed, 31 insertions, 1 deletions
@@ -149,6 +149,14 @@ static int lex_number(struct lexer *lexer, int64_t *n) { int64_t number = 0; int base = 10; + int neg = 0; + + // check if negative + if (lex_peek(lexer) == '-') { + lex_next(lexer); + neg = 1; + } + // skip all leading zeros, they dont do anything. // this also allows us to directly check for 0b, 0o, and 0x @@ -200,6 +208,9 @@ static int lex_number(struct lexer *lexer, int64_t *n) number += n; } + if (neg) + number = -number; + *n = number; return M_SUCCESS; } @@ -226,7 +237,8 @@ again: // use label to avoid whitespace recursion case ';': case '#': skip_comment(lexer); - goto again; + token->type = TOK_NL; + break; case ' ': case '\t': // skip white space @@ -267,6 +279,7 @@ again: // use label to avoid whitespace recursion lex_next(lexer); res = lex_string(lexer, token->text); break; + case '-': case '0': case '1': case '2': @@ -341,3 +354,20 @@ char *token_str(enum token_type type) } return "unknown"; } + +void lexer_save(struct lexer *lexer, struct lexer_state *state) +{ + state->x = lexer->x; + state->y = lexer->y; + state->peek = lexer->peek; + state->offset = ftell(lexer->file); +} + +/* load a different state into a lexer */ +void lexer_load(struct lexer *lexer, const struct lexer_state *state) +{ + lexer->x = state->x; + lexer->y = state->y; + lexer->peek = state->peek; + fseek(lexer->file, state->offset, SEEK_SET); +} |