add copying token value and add Boehm GC ready for runtime implimentation

This commit is contained in:
2025-05-29 00:40:11 +01:00
parent b82e351daf
commit 626445a906
7 changed files with 67 additions and 10 deletions

View File

@@ -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)

View File

@@ -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,

View File

@@ -1,6 +1,7 @@
#include "token.h"
#include <stdio.h>
#include <stdlib.h>
#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;
}

View File

@@ -1,6 +1,7 @@
#include "lexer/lexer.h"
#include "lexer/token.h"
#include "parser/parser.h"
#include "memory.h"
#include <stdbool.h>
#include <stdio.h>
@@ -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;
}

39
src/memory.c Normal file
View File

@@ -0,0 +1,39 @@
#include "memory.h"
#include <gc.h>
#include <string.h>
#include <stdlib.h> // 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;
}

19
src/memory.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef ARGON_MEMORY_H
#define ARGON_MEMORY_H
#include <stddef.h> // 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

View File

@@ -1,5 +1,4 @@
#include "string.h"
#include "../../string/string.h"
#include "../../lexer/token.h"
#include <cjson/cJSON.h>
@@ -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),
};
}