commit b21f1ed60ef477c87581b0c38a3a0551109f2059
parent be550ebbc7fcacdf82759b2249f2485166ec15ff
Author: citbl <citbl@citbl.org>
Date: Mon, 11 May 2026 20:46:17 +1000
one more reformat
Diffstat:
7 files changed, 46 insertions(+), 33 deletions(-)
diff --git a/.clang-format b/.clang-format
@@ -3,7 +3,7 @@ IndentWidth: 4
TabWidth: 4
ColumnLimit: 0
SpacesBeforeTrailingComments: 2
-PointerAlignment: Right
+PointerAlignment: Left
AlignArrayOfStructures: Right
AllowShortIfStatementsOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: false
diff --git a/src/common.h b/src/common.h
@@ -57,7 +57,7 @@ typedef enum Keyword {
/////////////////////////////////////////////////
typedef struct String {
- char *value;
+ char* value;
size_t cap;
size_t len;
} String;
@@ -65,8 +65,8 @@ typedef struct String {
typedef struct Token {
Token_Type type;
String lexeme;
- const char *path;
- const char *filename;
+ const char* path;
+ const char* filename;
size_t line;
size_t col;
} Token;
@@ -78,16 +78,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 check(bool condition, const char *message);
+void check(bool condition, const char* message);
diff --git a/src/lexer.c b/src/lexer.c
@@ -3,7 +3,7 @@
#include <string.h>
#include <ctype.h>
-static void add_token(Lexer *lex, Token t)
+static void add_token(Lexer* lex, Token t)
{
if (lex->len >= lex->cap) {
lex->cap = lex->cap == 0 ? 256 : lex->cap * 2;
@@ -14,7 +14,7 @@ static void add_token(Lexer *lex, Token t)
lex->tokens[lex->len++] = t;
}
-void print_tokens(Lexer *lex)
+void print_tokens(Lexer* lex)
{
Token t;
Token_Type typ;
@@ -37,9 +37,9 @@ void print_tokens(Lexer *lex)
}
}
-static void add_to_string(Token *tok, char c)
+static void add_to_string(Token* tok, char c)
{
- String *str = &tok->lexeme;
+ String* str = &tok->lexeme;
if (str->len >= str->cap) {
str->cap = str->cap == 0 ? 256 : str->cap * 2;
@@ -51,7 +51,7 @@ static void add_to_string(Token *tok, char c)
str->value[str->len] = '\0';
}
-static char peek(Lexer *lex)
+static char peek(Lexer* lex)
{
size_t next = lex->state.pos + 1;
@@ -62,7 +62,7 @@ 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) return c;
@@ -77,7 +77,7 @@ static char advance(Lexer *lex)
return c;
}
-static void run_until_char(Lexer *lex, char c)
+static void run_until_char(Lexer* lex, char c)
{
do {
advance(lex);
@@ -85,17 +85,29 @@ static void run_until_char(Lexer *lex, char c)
advance(lex);
}
-static void lex_number(Lexer *lex)
+static void lex_number(Lexer* lex, Token* tok)
{
char c;
+ String* str = &tok->lexeme;
+ tok->type = LIT_INT;
while (1) {
advance(lex);
c = peek(lex);
+ if (c == '.') tok->type = LIT_DECIMAL;
+
+ 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;
if (c != '.' && !isdigit(c)) break;
}
+ str->value[str->len] = '\0';
}
-Lexer *lexer_lex(Lexer *lex)
+Lexer* lexer_lex(Lexer* lex)
{
char c = '\0';
size_t len = strlen(lex->code);
@@ -115,7 +127,8 @@ Lexer *lexer_lex(Lexer *lex)
}
if (isdigit(c)) {
- lex_number(lex);
+ t.type = LIT_INT;
+ lex_number(lex, &t);
}
switch (c) {
diff --git a/src/lexer.h b/src/lexer.h
@@ -2,5 +2,5 @@
#include "common.h"
-void print_tokens(Lexer *l);
-Lexer *lexer_lex(Lexer *lexer);
+void print_tokens(Lexer* l);
+Lexer* lexer_lex(Lexer* lexer);
diff --git a/src/main.c b/src/main.c
@@ -4,10 +4,10 @@
#include "lexer.h"
#include "utils.h"
-int main(int argc, char **args)
+int main(int argc, char** args)
{
- char *filename;
- char *contents;
+ char* filename;
+ char* contents;
Lexer lexer = {0};
if (argc < 1) return 1;
diff --git a/src/utils.c b/src/utils.c
@@ -4,11 +4,11 @@
#include <stdlib.h>
#include <string.h>
-char *read_file(const char *filename)
+char* read_file(const char* filename)
{
long fsize;
- char *source;
- FILE *fp = fopen(filename, "r");
+ char* source;
+ FILE* fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "file not found\n");
return NULL;
@@ -26,10 +26,10 @@ char *read_file(const char *filename)
return source;
}
-void separate_file_from_path(const char *fullpath, char **out_path, char **out_filename)
+void separate_file_from_path(const char* fullpath, char** out_path, char** out_filename)
{
- char *path = strdup(fullpath);
- char *filename = strrchr(path, '/');
+ char* path = strdup(fullpath);
+ char* filename = strrchr(path, '/');
if (filename == NULL) {
printf("No path found\n");
@@ -43,7 +43,7 @@ void separate_file_from_path(const char *fullpath, char **out_path, char **out_f
free(path);
}
-void check(bool condition, const char *message)
+void check(bool condition, const char* message)
{
if (!condition) return;
fprintf(stderr, "%s", message);
diff --git a/src/utils.h b/src/utils.h
@@ -2,6 +2,6 @@
#include <stdbool.h>
-char *read_file(const char *filename);
+char* read_file(const char* filename);
-void separate_file_from_path(const char *fullpath, char **out_path, char **out_filename);
+void separate_file_from_path(const char* fullpath, char** out_path, char** out_filename);