diff --git a/Makefile b/Makefile index a843c75..bf50497 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ LEXER_C = src/lexer/lex.yy.c LEXER_H = src/lexer/lex.yy.h CFILES = $(shell find src -name '*.c') -CFLAGS = -lm -lcjson -Wall -Wextra -Wno-unused-function -s +CFLAGS = -lm -lcjson -lgc -Wall -Wextra -Wno-unused-function -s BINARY = bin/argon all: $(BINARY) diff --git a/src/lexer/lex.l b/src/lexer/lex.l index b09b9b6..b5e7b42 100644 --- a/src/lexer/lex.l +++ b/src/lexer/lex.l @@ -16,7 +16,7 @@ int yywrap(void *) { %% -(\"(\\[a-z\"'`]|[^\\"])*\") { +\"((\\([\"\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^\\\"\n])*\" { GET_STATE Token * token = create_token( TOKEN_STRING, @@ -28,7 +28,7 @@ int yywrap(void *) { ADD_TO_COLUMN } -('((\\([a-z'\"`]))|[^'])*') { +\'((\\([\'\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^\\\'\n])*\' { GET_STATE append(TOKENS, create_token( TOKEN_STRING, diff --git a/src/lexer/token.c b/src/lexer/token.c index 8e7f174..cb0b4c3 100644 --- a/src/lexer/token.c +++ b/src/lexer/token.c @@ -1,6 +1,7 @@ #include "token.h" #include #include +#include "../string/string.h" Token *create_token(TokenType type, int line, int column, char *value) { Token * token = malloc(sizeof(Token)); @@ -8,6 +9,6 @@ Token *create_token(TokenType type, int line, int column, char *value) { token->type = type; token->line=line; token->column=column; - token->value=value; + token->value=cloneString(value); return token; } \ No newline at end of file diff --git a/src/main.c b/src/main.c index f9ac72f..9b1ac9e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #include "lexer/lexer.h" #include "lexer/token.h" #include "parser/parser.h" +#include "memory.h" #include #include @@ -35,6 +36,7 @@ char* read_file_as_text(const char* filename) { } int main() { + ar_memory_init(); const char * path = "test.ar"; char *content = read_file_as_text(path); @@ -52,12 +54,10 @@ int main() { LinkedList * parsed = create_list(sizeof(TaggedValue)); parser(parsed, tokens, false); - free_list(tokens); - free(content); Node *current = parsed->head; while (current) { - printf("%s\n", (char*)current->data); + printf("%s\n", (char*)((TaggedValue*)current->data)->data); current = current->next; } diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..9e61430 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,39 @@ +#include "memory.h" +#include +#include +#include // for malloc/free (temp arena fallback) + +static char* temp_arena = NULL; +static size_t temp_arena_capacity = 0; +static size_t temp_arena_offset = 0; + +#define TEMP_ARENA_INITIAL_CAPACITY 4096 + +void ar_memory_init() { + GC_INIT(); +} + +void ar_memory_shutdown() { + // No-op for Boehm, but could clean up temp arena here + if (temp_arena) { + free(temp_arena); + temp_arena = NULL; + temp_arena_capacity = 0; + temp_arena_offset = 0; + } +} + +void* ar_alloc(size_t size) { + return GC_MALLOC(size); +} + +void* ar_alloc_atomic(size_t size) { + return GC_MALLOC_ATOMIC(size); +} + +char* ar_strdup(const char* str) { + size_t len = strlen(str) + 1; + char* copy = (char*)GC_MALLOC(len); + memcpy(copy, str, len); + return copy; +} \ No newline at end of file diff --git a/src/memory.h b/src/memory.h new file mode 100644 index 0000000..38319ca --- /dev/null +++ b/src/memory.h @@ -0,0 +1,19 @@ +#ifndef ARGON_MEMORY_H +#define ARGON_MEMORY_H + +#include // for size_t + +// GC-managed allocations +void* ar_alloc(size_t size); +void* ar_alloc_atomic(size_t size); +char* ar_strdup(const char* str); + +// Optional: temporary/arena allocations (e.g., for parsing) +void* ar_temp_alloc(size_t size); +void ar_temp_free_all(); + +// Memory init/shutdown +void ar_memory_init(); +void ar_memory_shutdown(); + +#endif // ARGON_MEMORY_H \ No newline at end of file diff --git a/src/parser/string/string.c b/src/parser/string/string.c index ae79389..2eadcec 100644 --- a/src/parser/string/string.c +++ b/src/parser/string/string.c @@ -1,5 +1,4 @@ #include "string.h" -#include "../../string/string.h" #include "../../lexer/token.h" #include @@ -27,7 +26,6 @@ char *swap_quotes(char *input) { } char *unquote(char *str) { - return str; if (*str == '\0') return NULL; @@ -76,6 +74,6 @@ char *unquote(char *str) { TaggedValue parse_string(Token token) { return (TaggedValue){ AST_STRING, - cloneString(token.value), + unquote(token.value), }; } \ No newline at end of file