From df4a225ccf79dd9f5fa3faef4fd68ae87471f0ca Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 13 Sep 2024 11:11:18 -0400 Subject: better --- masm/lex.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'masm/lex.c') diff --git a/masm/lex.c b/masm/lex.c index 788523b..37adf8e 100644 --- a/masm/lex.c +++ b/masm/lex.c @@ -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); +} -- cgit v1.2.3-freya