ox

The Ox programming language, compiler and tools (WIP)
Log | Files | Refs | README | LICENSE

commit bda5a5b3262aa69d3a1d569cc7364b3a0ff92b5a
parent edce80e2ec802a03a0430ccc70d13f412eb4221a
Author: citbl <citbl@citbl.org>
Date:   Sun, 16 Nov 2025 16:51:36 +1000

handle negative integers, kek

Diffstat:
M.zed/debug.json | 2+-
Msrc/lexer.c | 12+++++++++++-
2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/.zed/debug.json b/.zed/debug.json @@ -11,7 +11,7 @@ "cwd": "$ZED_WORKTREE_ROOT" }, "program": "$ZED_WORKTREE_ROOT/oxc", - "args": ["$ZED_WORKTREE_ROOT/tests/ex-call-args-same-names.ox"], + "args": ["$ZED_WORKTREE_ROOT/tests/ex-print-minus-values.ox"], "request": "launch", "adapter": "CodeLLDB" } diff --git a/src/lexer.c b/src/lexer.c @@ -113,6 +113,11 @@ static Token make_number(Lexer* lex, size_t pos, size_t line, size_t col) { bool is_float = false; + + if (peek(lex) == '-') { + nudge(lex); // pass through negative values + } + while (isdigit(peek(lex))) nudge(lex); if (peek(lex) == '.' && isdigit(peek2(lex))) { @@ -121,7 +126,9 @@ make_number(Lexer* lex, size_t pos, size_t line, size_t col) while (isdigit(peek(lex))) nudge(lex); } - return (Token) { .type = is_float ? TOKEN_FLOAT_LITERAL : TOKEN_INT_LITERAL, .start = pos, .end = lex->pos, .line = line, .col = col }; + return (Token) { + .type = is_float ? TOKEN_FLOAT_LITERAL : TOKEN_INT_LITERAL, .start = pos, .end = lex->pos, .line = line, .col = col + }; } static Token @@ -146,7 +153,10 @@ next_token(Lexer* lex) if (c == 0) return (Token) { .type = TOKEN_EOF, .start = start, .end = lex->pos, .col = col, .line = line }; if (isalpha(c) || c == '_') return make_ident(lex, start, line, col); + if (isdigit(c)) return make_number(lex, start, line, col); + if (c == '-' && isdigit(peek2(lex))) { return make_number(lex, start, line, col); } + if (c == '"') return make_string(lex, start, line, col); TokenType type = TOKEN_UNKNOWN;