sic

The sic programming language, compiler and tools (WIP)
Log | Files | Refs

commit d24e1b588e62bef0a64aafbf34f720a0bf6e3867
parent d8295a48cf792eafc3d1fb12c80b2946bfa66427
Author: citbl <citbl@citbl.org>
Date:   Wed, 13 May 2026 22:25:26 +1000

refac and ident lexing

Diffstat:
Msrc/array.h | 1+
Asrc/check.c | 10++++++++++
Asrc/check.h | 5+++++
Msrc/common.h | 68++------------------------------------------------------------------
Msrc/lexer.c | 23++++++++++++++++++++---
Msrc/str.c | 1-
Asrc/token.h | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/utils.c | 7-------
8 files changed, 101 insertions(+), 77 deletions(-)

diff --git a/src/array.h b/src/array.h @@ -1,5 +1,6 @@ #pragma once +#include <stdlib.h> #include <stdio.h> #define ARRAY_PUSH(dest, len, cap, item) \ diff --git a/src/check.c b/src/check.c @@ -0,0 +1,10 @@ +#include "check.h" +#include <stdio.h> +#include <stdlib.h> + +void check(bool condition, const char* message) +{ + if (!condition) return; + fprintf(stderr, "%s", message); + exit(1); +} diff --git a/src/check.h b/src/check.h @@ -0,0 +1,5 @@ +#pragma once + +#include <stdbool.h> + +void check(bool condition, const char* message); diff --git a/src/common.h b/src/common.h @@ -5,74 +5,10 @@ #include <stdio.h> #include <stdlib.h> -#include "str.h" - -typedef enum Token_Type { - NOTYETSET = 9, - IDENT, - KEYWORD, - SYMBOL, - PLUS, - MIN, - STAR, - SLASH, - BACKSLASH, - PERCENT, - LBRACE, - RBRACE, - LPAREN, - RPAREN, - EQ_EQ, - BANG_EQ, - EQ_GT, - LT_EQ, - LT_DASH, - DASH_GT, - EQ, - BANG, - LIT_STRING, - LIT_DECIMAL, - LIT_INT, - LIT_BOOL, - LIT_VOID, -} Token_Type; - -typedef enum Keyword { - IF = 139, - ELSE, - WHILE, - OPT, - LAZY, - MATCH, - DEF, - FOR, - EACH, - SOME, - NONE, - OK, - ERR, - IN, - IS, - CAST -} Keyword; +#include "token.h" ///////////////////////////////////////////////// -typedef struct String { - char* value; - size_t cap; - size_t len; -} String; - -typedef struct Token { - Token_Type type; - Str lexeme; - const char* path; - const char* filename; - size_t line; - size_t col; -} Token; - typedef struct Lexer_State { size_t pos; size_t line; @@ -92,4 +28,4 @@ typedef struct Lexer { ///////////////////////////////////////////////// -void check(bool condition, const char* message); +// void check(bool condition, const char* message); diff --git a/src/lexer.c b/src/lexer.c @@ -1,9 +1,10 @@ -#include <stdio.h> + #include <string.h> #include <ctype.h> #include "lexer.h" #include "array.h" +#include "str.h" static void add_token(Lexer* lex, Token t) { @@ -104,6 +105,21 @@ static void lex_number(Lexer* lex, Token* tok) add_token(lex, *tok); } +static void lex_ident(Lexer* lex, Token* tok) +{ + char c = lex->code[lex->state.pos]; + Str* str = &tok->lexeme; + str_append(str, c); + tok->type = IDENT; + while (lex->state.pos < lex->code_len) { + advance(lex); + c = peek(lex); + if (!isalnum((unsigned char)c)) break; + str_append(str, c); + } + add_token(lex, *tok); +} + static Token new_token(Lexer* lex) { return (Token){ @@ -135,9 +151,10 @@ Lexer* lexer_lex(Lexer* lex) run_until_char(lex, '\n'); continue; } - + if (isalpha((unsigned char)c)) { + lex_ident(lex, &t); + } if (isdigit((unsigned char)c)) { - t.type = LIT_INT; lex_number(lex, &t); } diff --git a/src/str.c b/src/str.c @@ -1,4 +1,3 @@ -#include <stdlib.h> #include "str.h" #include "array.h" diff --git a/src/token.h b/src/token.h @@ -0,0 +1,63 @@ +#pragma once +#include "str.h" + +typedef enum Token_Type { + NOTYETSET = 9, + IDENT, + KEYWORD, + SYMBOL, + PLUS, + MIN, + STAR, + SLASH, + BACKSLASH, + PERCENT, + LBRACE, + RBRACE, + LPAREN, + RPAREN, + EQ_EQ, + BANG_EQ, + EQ_GT, + LT_EQ, + LT_DASH, + DASH_GT, + EQ, + BANG, + LIT_STRING, + LIT_DECIMAL, + LIT_INT, + LIT_BOOL, + LIT_VOID, +} Token_Type; + +typedef enum Keyword { + IF = 139, + ELSE, + WHILE, + OPT, + LAZY, + MATCH, + DEF, + FOR, + EACH, + SOME, + NONE, + OK, + ERR, + IN, + IS, + CAST +} Keyword; + +///////////////////////////////////////////////// + +typedef struct Token { + Token_Type type; + Str lexeme; + const char* path; + const char* filename; + size_t line; + size_t col; +} Token; + diff --git a/src/utils.c b/src/utils.c @@ -56,10 +56,3 @@ void separate_file_from_path(const char* fullpath, char** out_path, char** out_f *out_filename = strdup(filename); free(path); } - -void check(bool condition, const char* message) -{ - if (!condition) return; - fprintf(stderr, "%s", message); - exit(1); -}