commit 618272bc406d90e052965e24a5727d7a6fee7559
parent 0873c441b396300f529ae201f8c89f0e1cd079f0
Author: citbl <citbl@citbl.org>
Date: Tue, 12 May 2026 21:29:09 +1000
fixes
Diffstat:
4 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/src/lexer.c b/src/lexer.c
@@ -12,7 +12,7 @@ static void add_token(Lexer* lex, Token t)
if (lex->len >= lex->cap) {
new_cap = lex->cap == 0 ? 256 : lex->cap * 2;
check(new_cap <= lex->cap || new_cap > SIZE_MAX / sizeof(Token), "token capacity overflow\n");
- p = realloc(lex->tokens, lex->cap * sizeof(Token));
+ p = realloc(lex->tokens, new_cap * sizeof(Token));
check(!p, "token reallocation failed\n");
lex->tokens = p;
lex->cap = new_cap;
@@ -84,6 +84,12 @@ static void run_until_char(Lexer* lex, char c)
advance(lex);
}
+static void err(Lexer* lex, const char* message)
+{
+ fprintf(stderr, "%s %zu:%zu %s", lex->filename, lex->state.line, lex->state.col, message);
+ exit(1);
+}
+
static void lex_number(Lexer* lex, Token* tok)
{
char c;
@@ -92,7 +98,10 @@ static void lex_number(Lexer* lex, Token* tok)
while (1) {
advance(lex);
c = peek(lex);
- if (c == '.') tok->type = LIT_DECIMAL;
+ if (c == '.' && tok->type == LIT_DECIMAL) {
+ err(lex, "parsing number failed with more than one decimal point '.'\n");
+ }
+ if (c == '.' && tok->type == LIT_INT) tok->type = LIT_DECIMAL;
str_append(str, c);
if (c != '.' && !isdigit(c)) break;
}
@@ -107,9 +116,11 @@ Lexer* lexer_lex(Lexer* lex)
lex->tokens = calloc(250, sizeof(Token));
lex->state.pos = 0;
+ lex->state.line = 1;
+ lex->state.col = 1;
// longest valid token first
- while (lex->state.pos <= lex->code_len) {
+ while (lex->state.pos < lex->code_len) {
c = lex->code[lex->state.pos];
if (c == '/' && peek(lex) == '/') {
@@ -117,7 +128,7 @@ Lexer* lexer_lex(Lexer* lex)
continue;
}
- if (isdigit(c)) {
+ if (isdigit((unsigned char)c)) {
t.type = LIT_INT;
lex_number(lex, &t);
}
@@ -135,8 +146,6 @@ Lexer* lexer_lex(Lexer* lex)
advance(lex);
add_token(lex, t);
continue;
- case EOF:
- return lex;
case '\n':
case '\r':
advance(lex);
diff --git a/src/main.c b/src/main.c
@@ -19,6 +19,7 @@ int main(int argc, char** args)
if (contents == NULL) return 1;
lexer.code = contents;
lexer.code_len = strlen(contents);
+ lexer.filename = filename;
lexer = *lexer_lex(&lexer);
diff --git a/src/str.c b/src/str.c
@@ -7,7 +7,7 @@ void str_append(Str* str, const char c)
size_t new_cap;
char* s;
- if (str->len + 1 >= str->cap) {
+ if (str->len + 1 >= str->cap) { // + 1 for the \0
new_cap = str->cap == 0 ? 32 : str->cap * 2;
check(new_cap <= str->cap || new_cap > SIZE_MAX, "could not reallocate string\n");
s = realloc(str->value, new_cap * sizeof(char));
diff --git a/test.sic b/test.sic
@@ -1,5 +1,5 @@
// this is a comment
str name = "Johnny Mnemonic";
-int age = 45;
+int age = 45.4.2;