commit f57583bece1d460cec1a66c374c9651321a28113
parent 089ddd6eb5b5f433627e13926131bba586f0bc39
Author: keyle <keyle@capsule.org>
Date: Thu, 25 Jun 2026 18:51:23 +1000
reformat
Diffstat:
6 files changed, 354 insertions(+), 373 deletions(-)
diff --git a/.clang-format b/.clang-format
@@ -1,17 +0,0 @@
-UseTab: Never
-IndentWidth: 4
-TabWidth: 4
-ColumnLimit: 0
-SpacesBeforeTrailingComments: 2
-PointerAlignment: Left
-AlignArrayOfStructures: Right
-AllowShortIfStatementsOnASingleLine: true
-AllowShortCaseLabelsOnASingleLine: false
-AllowShortEnumsOnASingleLine: true
-AllowShortFunctionsOnASingleLine: false
-SortIncludes: false
-BreakBeforeBraces: Stroustrup
-PenaltyBreakBeforeFirstCallParameter: 1
-#AlignConsecutiveAssignments: Consecutive
-InsertNewlineAtEOF: true
-KeepEmptyLinesAtEOF: true
diff --git a/src/lexer.c b/src/lexer.c
@@ -6,17 +6,17 @@
#include "array.h"
#include "str.h"
-static void add_token(Lexer* lex, Token t);
-static void add_to_string(Token* tok, char c);
-static char peek(Lexer* lex);
-static char advance(Lexer* lex);
-static void run_until_char(Lexer* lex, char c);
-static void lex_number(Lexer* lex, Token* tok);
-static void lex_ident(Lexer* lex, Token* tok);
-static Token new_token(Lexer* lex);
+static void add_token(Lexer *lex, Token t);
+static void add_to_string(Token *tok, char c);
+static char peek(Lexer *lex);
+static char advance(Lexer *lex);
+static void run_until_char(Lexer *lex, char c);
+static void lex_number(Lexer *lex, Token *tok);
+static void lex_ident(Lexer *lex, Token *tok);
+static Token new_token(Lexer *lex);
-void lexer_lex(Lexer* lex)
-{
+void
+lexer_lex(Lexer *lex) {
Token t;
char c = '\0';
@@ -45,247 +45,247 @@ void lexer_lex(Lexer* lex)
}
switch (c) {
- case '\"':
- t.type = LIT_STRING;
- advance(lex);
- c = lex->code[lex->state.pos];
-
- while (lex->code[lex->state.pos] != '\"' || lex->state.pos > lex->code_len) {
- add_to_string(&t, c);
- lex->state.col++;
- c = lex->code[++lex->state.pos];
- }
- advance(lex);
- add_token(lex, t);
- continue;
- case '=':
- if (peek(lex) == '=') {
- t.type = EQ_EQ;
- add_token(lex, t);
+ case '\"':
+ t.type = LIT_STRING;
advance(lex);
+ c = lex->code[lex->state.pos];
+
+ while (lex->code[lex->state.pos] != '\"' || lex->state.pos > lex->code_len) {
+ add_to_string(&t, c);
+ lex->state.col++;
+ c = lex->code[++lex->state.pos];
+ }
advance(lex);
- continue;
- }
- if (peek(lex) == '>') {
- t.type = EQ_GT;
add_token(lex, t);
- advance(lex);
- advance(lex);
continue;
- }
- t.type = EQ;
- add_token(lex, t);
- advance(lex);
- continue;
- case '!':
- if (peek(lex) == '=') {
- t.type = BANG_EQ;
+ case '=':
+ if (peek(lex) == '=') {
+ t.type = EQ_EQ;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ if (peek(lex) == '>') {
+ t.type = EQ_GT;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ t.type = EQ;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- t.type = BANG;
- add_token(lex, t);
- advance(lex);
- continue;
- case '*':
- if (peek(lex) == '=') {
- t.type = STAR_EQ;
+ case '!':
+ if (peek(lex) == '=') {
+ t.type = BANG_EQ;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ t.type = BANG;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- t.type = STAR;
- add_token(lex, t);
- advance(lex);
- continue;
- case '/':
- if (peek(lex) == '=') {
- t.type = SLASH_EQ;
+ case '*':
+ if (peek(lex) == '=') {
+ t.type = STAR_EQ;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ t.type = STAR;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- t.type = SLASH;
- add_token(lex, t);
- advance(lex);
- continue;
- case '%':
- if (peek(lex) == '=') {
- t.type = PERCENT_EQ;
+ case '/':
+ if (peek(lex) == '=') {
+ t.type = SLASH_EQ;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ t.type = SLASH;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- t.type = PERCENT;
- add_token(lex, t);
- advance(lex);
- continue;
- case '&':
- if (peek(lex) == '=') {
- t.type = AMPERSAND_EQ;
+ case '%':
+ if (peek(lex) == '=') {
+ t.type = PERCENT_EQ;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ t.type = PERCENT;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- if (peek(lex) == '&') {
- t.type = AMPERSAND_AMPERSAND;
+ case '&':
+ if (peek(lex) == '=') {
+ t.type = AMPERSAND_EQ;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ if (peek(lex) == '&') {
+ t.type = AMPERSAND_AMPERSAND;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ t.type = AMPERSAND;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- t.type = AMPERSAND;
- add_token(lex, t);
- advance(lex);
- continue;
- case '|':
- if (peek(lex) == '=') {
- t.type = PIPE_EQ;
- add_token(lex, t);
- advance(lex);
- advance(lex);
- continue;
- }
- if (peek(lex) == '|') {
- t.type = PIPE_PIPE;
+ case '|':
+ if (peek(lex) == '=') {
+ t.type = PIPE_EQ;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ if (peek(lex) == '|') {
+ t.type = PIPE_PIPE;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ t.type = PIPE;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- t.type = PIPE;
- add_token(lex, t);
- advance(lex);
- continue;
- case '+':
- if (peek(lex) == '+') {
- t.type = PLUS_PLUS;
+ case '+':
+ if (peek(lex) == '+') {
+ t.type = PLUS_PLUS;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ if (peek(lex) == '=') {
+ t.type = PLUS_EQ;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ t.type = PLUS;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- if (peek(lex) == '=') {
- t.type = PLUS_EQ;
+ case '-':
+ if (peek(lex) == '>') {
+ t.type = MINUS_GT;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ if (peek(lex) == '-') {
+ t.type = MINUS_MINUS;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ if (peek(lex) == '=') {
+ t.type = MINUS_EQ;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ t.type = EQ;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- t.type = PLUS;
- add_token(lex, t);
- advance(lex);
- continue;
- case '-':
- if (peek(lex) == '>') {
- t.type = MINUS_GT;
+ case '<':
+ if (peek(lex) == '=') {
+ t.type = LT_EQ;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ if (peek(lex) == '<') {
+ t.type = LT_LT;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ if (peek(lex) == '-') {
+ t.type = LT_MINUS;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ t.type = LT;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- if (peek(lex) == '-') {
- t.type = MINUS_MINUS;
+ case '>':
+ if (peek(lex) == '=') {
+ t.type = GT_EQ;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ if (peek(lex) == '>') {
+ t.type = GT_GT;
+ add_token(lex, t);
+ advance(lex);
+ advance(lex);
+ continue;
+ }
+ t.type = GT;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- if (peek(lex) == '=') {
- t.type = MINUS_EQ;
+ case ';':
+ t.type = SEMICOL;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- t.type = EQ;
- add_token(lex, t);
- advance(lex);
- continue;
- case '<':
- if (peek(lex) == '=') {
- t.type = LT_EQ;
+ case '(':
+ t.type = LPAREN;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- if (peek(lex) == '<') {
- t.type = LT_LT;
+ case ')':
+ t.type = RPAREN;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- if (peek(lex) == '-') {
- t.type = LT_MINUS;
+ case '{':
+ t.type = LBRACE;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- t.type = LT;
- add_token(lex, t);
- advance(lex);
- continue;
- case '>':
- if (peek(lex) == '=') {
- t.type = GT_EQ;
+ case '}':
+ t.type = RBRACE;
add_token(lex, t);
advance(lex);
- advance(lex);
continue;
- }
- if (peek(lex) == '>') {
- t.type = GT_GT;
- add_token(lex, t);
- advance(lex);
+ case '\n':
+ case '\r':
+ case '\t':
+ case ' ':
advance(lex);
continue;
- }
- t.type = GT;
- add_token(lex, t);
- advance(lex);
- continue;
- case ';':
- t.type = SEMICOL;
- add_token(lex, t);
- advance(lex);
- continue;
- case '(':
- t.type = LPAREN;
- add_token(lex, t);
- advance(lex);
- continue;
- case ')':
- t.type = RPAREN;
- add_token(lex, t);
- advance(lex);
- continue;
- case '{':
- t.type = LBRACE;
- add_token(lex, t);
- advance(lex);
- continue;
- case '}':
- t.type = RBRACE;
- add_token(lex, t);
- advance(lex);
- continue;
- case '\n':
- case '\r':
- case '\t':
- case ' ':
- advance(lex);
- continue;
- break;
+ break;
}
printf("unhandled: %s: %zu:%zu %c\n", lex->filename, lex->state.line, lex->state.col, c);
@@ -293,19 +293,19 @@ void lexer_lex(Lexer* lex)
}
}
-static void add_token(Lexer* lex, Token t)
-{
+static void
+add_token(Lexer *lex, Token t) {
ARRAY_PUSH(lex->tokens, lex->len, lex->cap, t);
}
-static void add_to_string(Token* tok, char c)
-{
- Str* str = &tok->lexeme;
+static void
+add_to_string(Token *tok, char c) {
+ Str *str = &tok->lexeme;
str_append(str, c);
}
-static char peek(Lexer* lex)
-{
+static char
+peek(Lexer *lex) {
size_t next = lex->state.pos + 1;
if (next >= lex->code_len) {
@@ -315,45 +315,44 @@ static char peek(Lexer* lex)
return lex->code[next];
}
-static char advance(Lexer* lex)
-{
+static char
+advance(Lexer *lex) {
const char c = peek(lex);
if (c == '\r') advance(lex);
if (c == '\n') {
lex->state.line++;
lex->state.col = 0;
- }
- else {
+ } else {
lex->state.col++;
}
lex->state.pos++;
return c;
}
-static void run_until_char(Lexer* lex, char c)
-{
+static void
+run_until_char(Lexer *lex, char c) {
do {
advance(lex);
} while (peek(lex) != c);
advance(lex);
}
-static void err(Lexer* lex, const char* message)
-{
+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)
-{
+static void
+lex_number(Lexer *lex, Token *tok) {
char c = lex->code[lex->state.pos];
- Str* str = &tok->lexeme;
+ Str *str = &tok->lexeme;
tok->type = LIT_INT;
str_append(str, c);
while (lex->state.pos < lex->code_len) {
c = peek(lex);
if (c == '_' && tok->type == LIT_INT) {
- advance(lex); // allow _ in large integers
+ advance(lex); // allow _ in large integers
continue;
}
if (c != '.' && !isdigit((unsigned char)c)) break;
@@ -368,10 +367,10 @@ static void lex_number(Lexer* lex, Token* tok)
add_token(lex, *tok);
}
-static void lex_ident(Lexer* lex, Token* tok)
-{
+static void
+lex_ident(Lexer *lex, Token *tok) {
char c = lex->code[lex->state.pos];
- Str* str = &tok->lexeme;
+ Str *str = &tok->lexeme;
tok->type = IDENT;
while (lex->state.pos < lex->code_len) {
str_append(str, c);
@@ -383,8 +382,8 @@ static void lex_ident(Lexer* lex, Token* tok)
add_token(lex, *tok);
}
-static Token new_token(Lexer* lex)
-{
+static Token
+new_token(Lexer *lex) {
return (Token){
.filename = lex->filename,
.path = lex->path,
diff --git a/src/lexer.h b/src/lexer.h
@@ -16,16 +16,16 @@ typedef struct Lexer_State {
} Lexer_State;
typedef struct Lexer {
- const char* code;
+ const char *code;
size_t code_len;
- const char* path;
- const char* filename;
+ const char *path;
+ const char *filename;
Lexer_State state;
- Token* tokens;
+ Token *tokens;
size_t len;
size_t cap;
} Lexer;
/////////////////////////////////////////////////
-void lexer_lex(Lexer* lexer);
+void lexer_lex(Lexer *lexer);
diff --git a/src/lexer_tools.c b/src/lexer_tools.c
@@ -1,7 +1,7 @@
#include "lexer.h"
-void print_tokens(Lexer* lex)
-{
+void
+print_tokens(Lexer *lex) {
Token t;
Token_Type typ;
size_t i;
@@ -15,131 +15,130 @@ void print_tokens(Lexer* lex)
printf("%s: %zu:%zu ", t.filename, t.line, t.col);
switch (typ) {
- case LIT_STRING:
- printf("STRING LITERAL: %s\n", lex->tokens[i].lexeme.value);
- break;
- case LIT_DECIMAL:
- printf("DECIMAL LITERAL: %s\n", lex->tokens[i].lexeme.value);
- break;
- case LIT_INT:
- printf("DECIMAL LITERAL: %s\n", lex->tokens[i].lexeme.value);
- break;
- case LBRACE:
- printf("LBRACE {\n");
- break;
- case RBRACE:
- printf("RBRACE }\n");
- break;
- case LPAREN:
- printf("LPAREN (\n");
- break;
- case RPAREN:
- printf("RPAREN )\n");
- break;
- case BANG:
- printf("BANG !\n");
- break;
- case BANG_EQ:
- printf("BANG_EQ !=\n");
- break;
- case EQ:
- printf("EQ =\n");
- break;
- case EQ_EQ:
- printf("EQ_EQ ==\n");
- break;
- case EQ_GT:
- printf("EQ_GT =>\n");
- break;
- case SEMICOL:
- printf("SEMICOL ;\n");
- break;
- case LT_EQ:
- printf("LT_EQ <=\n");
- break;
- case LT_LT:
- printf("LT_LT <<\n");
- break;
- case LT_MINUS:
- printf("LT_MINUS <-\n");
- break;
- case LT:
- printf("LT <\n");
- break;
- case GT:
- printf("GT >\n");
- break;
- case GT_EQ:
- printf("GT_EQ >=\n");
- break;
- case GT_GT:
- printf("GT_GT >>\n");
- break;
+ case LIT_STRING:
+ printf("STRING LITERAL: %s\n", lex->tokens[i].lexeme.value);
+ break;
+ case LIT_DECIMAL:
+ printf("DECIMAL LITERAL: %s\n", lex->tokens[i].lexeme.value);
+ break;
+ case LIT_INT:
+ printf("DECIMAL LITERAL: %s\n", lex->tokens[i].lexeme.value);
+ break;
+ case LBRACE:
+ printf("LBRACE {\n");
+ break;
+ case RBRACE:
+ printf("RBRACE }\n");
+ break;
+ case LPAREN:
+ printf("LPAREN (\n");
+ break;
+ case RPAREN:
+ printf("RPAREN )\n");
+ break;
+ case BANG:
+ printf("BANG !\n");
+ break;
+ case BANG_EQ:
+ printf("BANG_EQ !=\n");
+ break;
+ case EQ:
+ printf("EQ =\n");
+ break;
+ case EQ_EQ:
+ printf("EQ_EQ ==\n");
+ break;
+ case EQ_GT:
+ printf("EQ_GT =>\n");
+ break;
+ case SEMICOL:
+ printf("SEMICOL ;\n");
+ break;
+ case LT_EQ:
+ printf("LT_EQ <=\n");
+ break;
+ case LT_LT:
+ printf("LT_LT <<\n");
+ break;
+ case LT_MINUS:
+ printf("LT_MINUS <-\n");
+ break;
+ case LT:
+ printf("LT <\n");
+ break;
+ case GT:
+ printf("GT >\n");
+ break;
+ case GT_EQ:
+ printf("GT_EQ >=\n");
+ break;
+ case GT_GT:
+ printf("GT_GT >>\n");
+ break;
- case PLUS:
- printf("PLUS +\n");
- break;
- case PLUS_PLUS:
- printf("PLUS_PLUS ++\n");
- break;
- case PLUS_EQ:
- printf("PLUS_EQ +=\n");
- break;
- case MINUS:
- printf("MINUS -\n");
- break;
- case MINUS_EQ:
- printf("MINUS_EQ -=\n");
- break;
- case MINUS_MINUS:
- printf("MINUS_MINUS --\n");
- break;
- case MINUS_GT:
- printf("MINUS_GT ->\n");
- break;
- case STAR:
- printf("STAR *\n");
- break;
- case STAR_EQ:
- printf("STAR_EQ *=\n");
- break;
- case PERCENT:
- printf("PERCENT %%\n");
- break;
- case PERCENT_EQ:
- printf("PERCENT_EQ %%=\n");
- break;
- case AMPERSAND:
- printf("AMPERSAND &\n");
- break;
- case AMPERSAND_AMPERSAND:
- printf("AMPERSAND_AMPERSAND &&\n");
- break;
- case AMPERSAND_EQ:
- printf("AMPERSAND_EQ &=\n");
- break;
- case PIPE:
- printf("PIPE |\n");
- break;
- case PIPE_PIPE:
- printf("PIPE_PIPE ||\n");
- break;
- case PIPE_EQ:
- printf("PIPE_EQ |=\n");
- break;
- case SLASH:
- printf("SLASH /\n");
- break;
- case SLASH_EQ:
- printf("SLASH_EQ /=\n");
- break;
- case IDENT:
- printf("IDENT: %s\n", lex->tokens[i].lexeme.value);
- break;
- default:
- printf("print_tokens: unhandled token %i\n", typ);
- break;
+ case PLUS:
+ printf("PLUS +\n");
+ break;
+ case PLUS_PLUS:
+ printf("PLUS_PLUS ++\n");
+ break;
+ case PLUS_EQ:
+ printf("PLUS_EQ +=\n");
+ break;
+ case MINUS:
+ printf("MINUS -\n");
+ break;
+ case MINUS_EQ:
+ printf("MINUS_EQ -=\n");
+ break;
+ case MINUS_MINUS:
+ printf("MINUS_MINUS --\n");
+ break;
+ case MINUS_GT:
+ printf("MINUS_GT ->\n");
+ break;
+ case STAR:
+ printf("STAR *\n");
+ break;
+ case STAR_EQ:
+ printf("STAR_EQ *=\n");
+ break;
+ case PERCENT:
+ printf("PERCENT %%\n");
+ break;
+ case PERCENT_EQ:
+ printf("PERCENT_EQ %%=\n");
+ break;
+ case AMPERSAND:
+ printf("AMPERSAND &\n");
+ break;
+ case AMPERSAND_AMPERSAND:
+ printf("AMPERSAND_AMPERSAND &&\n");
+ break;
+ case AMPERSAND_EQ:
+ printf("AMPERSAND_EQ &=\n");
+ break;
+ case PIPE:
+ printf("PIPE |\n");
+ break;
+ case PIPE_PIPE:
+ printf("PIPE_PIPE ||\n");
+ break;
+ case PIPE_EQ:
+ printf("PIPE_EQ |=\n");
+ break;
+ case SLASH:
+ printf("SLASH /\n");
+ break;
+ case SLASH_EQ:
+ printf("SLASH_EQ /=\n");
+ break;
+ case IDENT:
+ printf("IDENT: %s\n", lex->tokens[i].lexeme.value);
+ break;
+ default:
+ printf("print_tokens: unhandled token %i\n", typ);
+ break;
}
}
}
-
diff --git a/src/lexer_tools.h b/src/lexer_tools.h
@@ -2,4 +2,4 @@
#include "lexer.h"
-void print_tokens(Lexer*);
+void print_tokens(Lexer *);
diff --git a/src/main.c b/src/main.c
@@ -5,10 +5,10 @@
#include "lexer_tools.h"
#include "utils.h"
-int main(int argc, char** args)
-{
- char* filename;
- char* contents;
+int
+main(int argc, char **args) {
+ char *filename;
+ char *contents;
Lexer lexer = {0};
if (argc < 1) return 1;