move to meson for build system

This commit is contained in:
Lobo 2026-01-19 10:25:56 -03:00
parent fdd1ee61b5
commit 9616fb616e
28 changed files with 123 additions and 24 deletions

View file

@ -1,7 +1,13 @@
root = true root = true
[*] [*]
end_of_line = lf end_of_line = lf
insert_final_newline = true insert_final_newline = true
[*.{c,h,grr}] [*.{c,h,grr}]
indent_style = space indent_style = space
indent_size = 2 indent_size = 2
[meson.build]
indent_style = space
indent_size = 2

5
.gitignore vendored
View file

@ -1,5 +1,4 @@
*.o /.cache
.cache /build
/growl
/.envrc /.envrc
/compile_commands.json /compile_commands.json

View file

@ -1,11 +0,0 @@
CC := cc
CFLAGS := -Og -g -std=c99 -Wpedantic -Wall
OBJS = chunk.o gc.o main.o object.o parser.o print.o vm.o vendor/mpc.o \
vendor/yar.o
growl: $(OBJS)
$(CC) -o growl $(OBJS)
.PHONY: clean
clean:
rm -f growl $(OBJS)

25
meson.build Normal file
View file

@ -0,0 +1,25 @@
project(
'growl',
'c',
meson_version : '>= 1.3.0',
version : '0.1',
default_options : ['buildtype=debugoptimized', 'c_std=c99', 'warning_level=3'],
)
sources = [
'src/chunk.c',
'src/compile.c',
'src/gc.c',
'src/object.c',
'src/parser.c',
'src/print.c',
'src/vm.c',
'src/vendor/mpc.c',
'src/vendor/yar.c',
]
exe = executable(
'growl',
'src/main.c', sources,
install : true,
)

View file

View file

View file

View file

@ -3,5 +3,6 @@
pkgs.mkShell { pkgs.mkShell {
buildInputs = with pkgs; [ buildInputs = with pkgs; [
clang-tools bear gdb tinycc clang-tools bear gdb tinycc
meson ninja
]; ];
} }

6
src/compile.c Normal file
View file

@ -0,0 +1,6 @@
#include "compile.h"
I compile_program(mpc_ast_t *ast) {
}

4
src/compile.h Normal file
View file

@ -0,0 +1,4 @@
#include "common.h"
#include "vendor/mpc.h"
I compile_program(mpc_ast_t *);

View file

@ -3,7 +3,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "chunk.h"
#include "gc.h" #include "gc.h"
#include "object.h"
#include "vendor/yar.h" #include "vendor/yar.h"
#define ALIGN(n) (((n) + 7) & ~7) #define ALIGN(n) (((n) + 7) & ~7)
@ -18,14 +20,14 @@ V gc_reset(Gc *gc, I mark) { gc->roots.count = mark; }
static O copy(Gc *gc, Hd *hdr) { static O copy(Gc *gc, Hd *hdr) {
assert(infrom(gc, hdr)); assert(infrom(gc, hdr));
assert(hdr->type != TYPE_FWD); assert(hdr->type != OBJ_FWD);
Z sz = ALIGN(hdr->size); Z sz = ALIGN(hdr->size);
Hd *new = (Hd *)gc->to.free; Hd *new = (Hd *)gc->to.free;
gc->to.free += sz; gc->to.free += sz;
memcpy(new, hdr, sz); memcpy(new, hdr, sz);
hdr->type = TYPE_FWD; hdr->type = OBJ_FWD;
O *obj = (O *)(hdr + 1); O *obj = (O *)(hdr + 1);
*obj = BOX(new); *obj = BOX(new);
return *obj; return *obj;
@ -40,7 +42,7 @@ static O forward(Gc *gc, O obj) {
return obj; return obj;
Hd *hdr = UNBOX(obj); Hd *hdr = UNBOX(obj);
if (hdr->type == TYPE_FWD) { if (hdr->type == OBJ_FWD) {
O *o = (O *)(hdr + 1); O *o = (O *)(hdr + 1);
return *o; return *o;
} else { } else {
@ -76,7 +78,13 @@ V gc_collect(Gc *gc) {
Hd *hdr = (Hd *)scan; Hd *hdr = (Hd *)scan;
switch (hdr->type) { switch (hdr->type) {
// TODO: the rest of the owl // TODO: the rest of the owl
case TYPE_FWD: case OBJ_QUOT: {
Bc *chunk = (Bc *)(hdr + 1);
for (Z i = 0; i < chunk->constants.count; i++)
chunk->constants.items[i] = forward(gc, chunk->constants.items[i]);
break;
}
case OBJ_FWD:
fprintf(stderr, "fatal GC error: forwarding pointer in to-space\n"); fprintf(stderr, "fatal GC error: forwarding pointer in to-space\n");
abort(); abort();
default: default:

View file

View file

10
src/object.c Normal file
View file

@ -0,0 +1,10 @@
#include "object.h"
I type(O o) {
if (o == NIL)
return TYPE_NIL;
if (IMM(o))
return TYPE_NUM;
Hd *h = UNBOX(o);
return h->type;
}

View file

@ -11,7 +11,14 @@
#define ORD(x) ((O)(x) >> 1) #define ORD(x) ((O)(x) >> 1)
enum { enum {
TYPE_FWD, OBJ_FWD = 2,
OBJ_QUOT,
};
enum {
TYPE_NIL = 0,
TYPE_NUM = 1,
TYPE_FWD = OBJ_FWD,
}; };
typedef uintptr_t O; typedef uintptr_t O;
@ -21,4 +28,6 @@ typedef struct Hd {
U32 size, type; U32 size, type;
} Hd; } Hd;
I type(O);
#endif #endif

View file

@ -4,7 +4,6 @@
#include "common.h" #include "common.h"
#include "vendor/mpc.h" #include "vendor/mpc.h"
V parser_init(V); V parser_init(V);
V parser_deinit(V); V parser_deinit(V);

20
src/print.c Normal file
View file

@ -0,0 +1,20 @@
#include <inttypes.h>
#include <stdio.h>
#include "object.h"
#include "print.h"
V print(O o) {
if (o == NIL) {
printf("nil");
} else if (IMM(o)) {
printf("%" PRIdPTR, ORD(o));
} else {
printf("<obj type=%ld ptr=%p>", type(o), (void *)o);
}
}
V println(O o) {
print(o);
putchar('\n');
}

10
src/print.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef PRINT_H
#define PRINT_H
#include "common.h"
#include "object.h"
V print(O);
V println(O);
#endif

View file

View file

View file

View file

View file

@ -1,5 +1,7 @@
#include "vm.h" #include "vm.h"
#include "gc.h" #include "gc.h"
#include "print.h"
#include <stdio.h>
static I decode_sleb128(U8 **ptr) { static I decode_sleb128(U8 **ptr) {
I result = 0; I result = 0;
@ -45,16 +47,23 @@ V vm_run(Vm *vm, Bc *chunk, I offset) {
U8 opcode; U8 opcode;
switch (opcode = *vm->ip++) { switch (opcode = *vm->ip++) {
case OP_NOP: case OP_NOP:
break; continue;
case OP_RETURN:
return;
case OP_CONST: { case OP_CONST: {
I idx = decode_sleb128(&vm->ip); I idx = decode_sleb128(&vm->ip);
vm_push(vm, chunk->constants.items[idx]); vm_push(vm, chunk->constants.items[idx]);
break; break;
} }
case OP_RETURN:
goto done;
} }
} }
done:
gc_reset(&vm->gc, mark); gc_reset(&vm->gc, mark);
// print stack :3
for (O *i = vm->stack; i < vm->sp; i++) {
print(*i);
putchar(' ');
}
putchar('\n');
} }

View file

@ -9,8 +9,12 @@
enum { enum {
OP_NOP = 0, OP_NOP = 0,
OP_CONST, // Push constant to stack
OP_JUMP, // Relative jump
OP_JUMP_IF_NIL, // Relative jump if top-of-stack is nil
OP_DOWORD,
OP_CALL,
OP_RETURN, OP_RETURN,
OP_CONST,
}; };
#define STACK_SIZE 256 #define STACK_SIZE 256