commit 407194a44947ada45cf0baaa0c458077156a2eb2
parent 05c4135547a761744cd1553980fed2b9f853cade
Author: citbl <citbl@citbl.org>
Date: Sun, 10 May 2026 20:28:19 +1000
parsing number wip
Diffstat:
4 files changed, 15 insertions(+), 14 deletions(-)
diff --git a/src/common.h b/src/common.h
@@ -64,12 +64,7 @@ typedef struct String {
typedef struct Token {
Token_Type type;
- union Value {
- String as_string;
- char as_char;
- size_t as_int;
- bool as_bool;
- } value;
+ String lexeme;
const char *path;
const char *filename;
size_t line;
@@ -97,4 +92,4 @@ typedef struct Lexer {
/////////////////////////////////////////////////
-void die(bool condition, const char *message);
+void check(bool condition, const char *message);
diff --git a/src/lexer.c b/src/lexer.c
@@ -8,6 +8,7 @@ static void add_token(Lexer *lex, Token t)
if (lex->len >= lex->cap) {
lex->cap = lex->cap == 0 ? 256 : lex->cap * 2;
lex->tokens = realloc(lex->tokens, lex->cap * sizeof(Token));
+ check(lex->tokens == NULL, "could not allocate memory for the tokens\n");
}
lex->tokens[lex->len++] = t;
@@ -27,7 +28,7 @@ void print_tokens(Lexer *lex)
switch (typ) {
case LIT_STRING:
- printf("STRING LITERAL: %s\n", lex->tokens[i].value.as_string.value);
+ printf("STRING LITERAL: %s\n", lex->tokens[i].lexeme.value);
break;
default:
printf("print_tokens: unhandled token %i", typ);
@@ -38,11 +39,12 @@ void print_tokens(Lexer *lex)
static void add_to_string(Token *tok, char c)
{
- String *str = &tok->value.as_string;
+ String *str = &tok->lexeme;
if (str->len >= str->cap) {
str->cap = str->cap == 0 ? 256 : str->cap * 2;
str->value = realloc(str->value, str->cap * sizeof(char));
+ check(str->value == NULL, "could not allocate memory for string in lexer\n");
}
str->value[str->len++] = c;
@@ -83,12 +85,16 @@ static void run_until_char(Lexer *lex, char c)
advance(lex);
}
+static void lex_number(Lexer *lex)
+{
+}
+
Lexer *lexer_lex(Lexer *lex)
{
char c = '\0';
size_t len = strlen(lex->code);
Token t = {
- .filename = lex->filename, .path = lex->path, .col = -1, .line = -1, .type = NOTYETSET, .value = {0}};
+ .filename = lex->filename, .path = lex->path, .col = -1, .line = -1, .type = NOTYETSET, .lexeme = {0}};
lex->tokens = calloc(250, sizeof(Token));
lex->state.pos = 0;
@@ -104,6 +110,7 @@ Lexer *lexer_lex(Lexer *lex)
}
if (isdigit(c)) {
+ lex_number(lex);
}
switch (c) {
diff --git a/src/utils.c b/src/utils.c
@@ -43,7 +43,7 @@ void separate_file_from_path(const char *fullpath, char **out_path, char **out_f
free(path);
}
-void die(bool condition, const char *message)
+void check(bool condition, const char *message)
{
if (!condition) return;
fprintf(stderr, "%s", message);
diff --git a/test.sic b/test.sic
@@ -1,6 +1,5 @@
// this is a comment
-void main() {
- str name = "Johnny Mnemonic";
-}
+str name = "Johnny Mnemonic";
+int age = 45;