add copying token value and add Boehm GC ready for runtime implimentation
This commit is contained in:
2
Makefile
2
Makefile
@@ -3,7 +3,7 @@ LEXER_C = src/lexer/lex.yy.c
|
|||||||
LEXER_H = src/lexer/lex.yy.h
|
LEXER_H = src/lexer/lex.yy.h
|
||||||
|
|
||||||
CFILES = $(shell find src -name '*.c')
|
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
|
BINARY = bin/argon
|
||||||
|
|
||||||
all: $(BINARY)
|
all: $(BINARY)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ int yywrap(void *) {
|
|||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
(\"(\\[a-z\"'`]|[^\\"])*\") {
|
\"((\\([\"\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^\\\"\n])*\" {
|
||||||
GET_STATE
|
GET_STATE
|
||||||
Token * token = create_token(
|
Token * token = create_token(
|
||||||
TOKEN_STRING,
|
TOKEN_STRING,
|
||||||
@@ -28,7 +28,7 @@ int yywrap(void *) {
|
|||||||
ADD_TO_COLUMN
|
ADD_TO_COLUMN
|
||||||
}
|
}
|
||||||
|
|
||||||
('((\\([a-z'\"`]))|[^'])*') {
|
\'((\\([\'\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^\\\'\n])*\' {
|
||||||
GET_STATE
|
GET_STATE
|
||||||
append(TOKENS, create_token(
|
append(TOKENS, create_token(
|
||||||
TOKEN_STRING,
|
TOKEN_STRING,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "token.h"
|
#include "token.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include "../string/string.h"
|
||||||
|
|
||||||
Token *create_token(TokenType type, int line, int column, char *value) {
|
Token *create_token(TokenType type, int line, int column, char *value) {
|
||||||
Token * token = malloc(sizeof(Token));
|
Token * token = malloc(sizeof(Token));
|
||||||
@@ -8,6 +9,6 @@ Token *create_token(TokenType type, int line, int column, char *value) {
|
|||||||
token->type = type;
|
token->type = type;
|
||||||
token->line=line;
|
token->line=line;
|
||||||
token->column=column;
|
token->column=column;
|
||||||
token->value=value;
|
token->value=cloneString(value);
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "lexer/lexer.h"
|
#include "lexer/lexer.h"
|
||||||
#include "lexer/token.h"
|
#include "lexer/token.h"
|
||||||
#include "parser/parser.h"
|
#include "parser/parser.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -35,6 +36,7 @@ char* read_file_as_text(const char* filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
ar_memory_init();
|
||||||
const char * path = "test.ar";
|
const char * path = "test.ar";
|
||||||
|
|
||||||
char *content = read_file_as_text(path);
|
char *content = read_file_as_text(path);
|
||||||
@@ -52,12 +54,10 @@ int main() {
|
|||||||
LinkedList * parsed = create_list(sizeof(TaggedValue));
|
LinkedList * parsed = create_list(sizeof(TaggedValue));
|
||||||
|
|
||||||
parser(parsed, tokens, false);
|
parser(parsed, tokens, false);
|
||||||
free_list(tokens);
|
|
||||||
free(content);
|
|
||||||
|
|
||||||
Node *current = parsed->head;
|
Node *current = parsed->head;
|
||||||
while (current) {
|
while (current) {
|
||||||
printf("%s\n", (char*)current->data);
|
printf("%s\n", (char*)((TaggedValue*)current->data)->data);
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
39
src/memory.c
Normal file
39
src/memory.c
Normal 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
19
src/memory.h
Normal 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
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "../../string/string.h"
|
|
||||||
#include "../../lexer/token.h"
|
#include "../../lexer/token.h"
|
||||||
|
|
||||||
#include <cjson/cJSON.h>
|
#include <cjson/cJSON.h>
|
||||||
@@ -27,7 +26,6 @@ char *swap_quotes(char *input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char *unquote(char *str) {
|
char *unquote(char *str) {
|
||||||
return str;
|
|
||||||
if (*str == '\0')
|
if (*str == '\0')
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@@ -76,6 +74,6 @@ char *unquote(char *str) {
|
|||||||
TaggedValue parse_string(Token token) {
|
TaggedValue parse_string(Token token) {
|
||||||
return (TaggedValue){
|
return (TaggedValue){
|
||||||
AST_STRING,
|
AST_STRING,
|
||||||
cloneString(token.value),
|
unquote(token.value),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user