Skip to content

Commit d18fa9b

Browse files
committed
feat: Implement mark-and-sweep garbage collector with VM integration.
1 parent d8abbe3 commit d18fa9b

4 files changed

Lines changed: 33 additions & 10 deletions

File tree

include/gc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "vm.h"
77

88
// Initialize GC state
9-
void initGC(VM* vm);
9+
void gc_init(VM* vm);
1010

1111
// Free all objects (called at VM shutdown)
1212
void freeObjects(VM* vm);

src/runtime/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// Access the global VM instance
1717
extern VM vm;
1818

19-
void initGC(VM* vm) {
19+
void gc_init(VM* vm) {
2020
vm->grayCount = 0;
2121
vm->grayCapacity = 0;
2222
vm->grayStack = NULL;

src/runtime/vm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static void resetStack(VM *pvm) {
2828
void initVM(VM *pvm) {
2929
resetStack(pvm);
3030
pvm->objects = NULL;
31-
initGC(pvm); // CRITICAL: Initialize GC state
31+
gc_init(pvm); // CRITICAL: Initialize GC state
3232
initTable(&pvm->globals);
3333
initTable(&pvm->strings);
3434
pvm->source = NULL;

tools/llvm_gen_test.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3-
#include "../include/lexer.h"
3+
#include <string.h>
4+
#include "../include/common.h"
5+
#include "../include/scanner.h"
46
#include "../include/parser.h"
5-
#include "../include/ir.h"
6-
#include "../include/ir_opt.h"
7+
#include "../include/compiler.h"
78
#include "../include/backend_llvm.h"
89
#include "../include/vm.h"
910

1011
VM vm;
1112

1213
int main() {
13-
initVM();
14+
initVM(&vm);
1415

1516
const char* source =
1617
"let x = 10;\n"
@@ -27,11 +28,30 @@ int main() {
2728

2829
printf("Testing Source:\n%s\n", source);
2930

31+
// 1. Tokenize
32+
Scanner scanner;
33+
initScanner(&scanner, source);
34+
Token tokens[4096];
35+
int tokenCount = 0;
36+
37+
for (;;) {
38+
Token token = scanToken(&scanner);
39+
tokens[tokenCount++] = token;
40+
if (token.type == TOKEN_EOF) break;
41+
if (token.type == TOKEN_ERROR) {
42+
fprintf(stderr, "Scan Error: %.*s\n", token.length, token.start);
43+
return 1;
44+
}
45+
}
46+
47+
// 2. Parse
3048
Parser parser;
31-
initParser(&parser, source);
49+
initParser(&parser, tokens, tokenCount);
3250
StmtList* program = parse(&parser);
3351

3452
if (program) {
53+
printf("Parsed successfully.\n");
54+
3555
IRModule* ir = generateSSA_IR(program);
3656

3757
for (int i = 0; i < ir->funcCount; i++) {
@@ -41,12 +61,15 @@ int main() {
4161
}
4262

4363
printf("Generated LLVM IR:\n");
64+
// emitLLVM is defined in backend_llvm.cpp and exposed via extern "C"
4465
emitLLVM(ir);
4566

4667
freeIRModule(ir);
47-
// Free program, etc.
68+
freeStmtList(program);
69+
} else {
70+
printf("Parse failed.\n");
4871
}
4972

50-
freeVM();
73+
freeVM(&vm);
5174
return 0;
5275
}

0 commit comments

Comments
 (0)