From: cassowarii Date: Tue, 30 Jun 2026 23:25:22 +0000 (-0700) Subject: list data module X-Git-Url: https://git.cassowary.me/gitweb.cgi?a=commitdiff_plain;h=b25bd2c154dff77ce766b751e44fe1fe4311abe3;p=sarabande.git list data module --- diff --git a/src/compile/emit.c b/src/compile/emit.c index 8c460da..fed66a9 100644 --- a/src/compile/emit.c +++ b/src/compile/emit.c @@ -196,8 +196,8 @@ void compile_expr(sbVmCompiler *cm, sbIrExpr *expr) { E1 = expr->call.param; while (E1) { count ++; - compile_expr(cm, E1->param.this); - E1 = E1->param.next; + compile_expr(cm, E1->list.this); + E1 = E1->list.next; } EMIT(BC_LD_IMM); EARG(count); /* calling convention: store argument count on stack */ diff --git a/src/compile/ir.c b/src/compile/ir.c index cdd9f88..c0ea39f 100644 --- a/src/compile/ir.c +++ b/src/compile/ir.c @@ -203,6 +203,9 @@ static flag int_constant_fold(sbAstOp op, hInteger left, hInteger right, hIntege case AST_OP_MOD: *result = left % right; break; + case AST_OP_UNMINUS: + *result = -left; + break; default: return FALSE; } @@ -211,10 +214,10 @@ static flag int_constant_fold(sbAstOp op, hInteger left, hInteger right, hIntege static sbIrExpr *expr_op(hIrChunk ck, sbAstOp op, sbIrExpr *left, sbIrExpr *right) { if (left->type == IR_E_VALUE && left->value.type == IT_INTEGER - && right->type == IR_E_VALUE && right->value.type == IT_INTEGER) { + && (!right || (right->type == IR_E_VALUE && right->value.type == IT_INTEGER))) { /* Try constant folding */ hInteger result; - flag folded = int_constant_fold(op, left->value.integer, right->value.integer, &result); + flag folded = int_constant_fold(op, left->value.integer, right ? right->value.integer : 0, &result); if (folded) { return new_expr(ck, &(sbIrExpr) { .type = IR_E_VALUE, @@ -242,7 +245,7 @@ static sbIrExpr *expr_call(hIrChunk ck, sbIrExpr *to_call, sbIrExpr *param) { static sbIrExpr *expr_param(hIrChunk ck, sbIrExpr *value) { return new_expr(ck, &(sbIrExpr) { .type = IR_E_PARAM, - .param.this = value, + .list.this = value, }); } @@ -734,7 +737,7 @@ static sbIrExpr *compile_ast_expr(hIrChunk ck, sbAst node) { sbIrExpr **place_here = ¶m; while (param_ast != NO_NODE) { *place_here = expr_param(ck, compile_ast_expr(ck, param_ast->seq.left)); - place_here = &(*place_here)->param.next; + place_here = &(*place_here)->list.next; param_ast = param_ast->seq.right; } return expr_call(ck, called, param); @@ -799,9 +802,9 @@ static void print_expr(sbIrExpr *e) { debug(" with params "); sbIrExpr *param = e->call.param; while (param) { - print_expr(param->param.this); + print_expr(param->list.this); debug(","); - param = param->param.next; + param = param->list.next; } break; default: diff --git a/src/compile/ir.h b/src/compile/ir.h index 5b129e4..a696c5d 100644 --- a/src/compile/ir.h +++ b/src/compile/ir.h @@ -88,7 +88,7 @@ typedef struct sbIrExpr { struct { struct sbIrExpr *this; struct sbIrExpr *next; - } param; + } list; }; } sbIrExpr; diff --git a/src/data/data.c b/src/data/data.c new file mode 100644 index 0000000..296fb36 --- /dev/null +++ b/src/data/data.c @@ -0,0 +1,20 @@ +#include "data/data.h" + +#include "data/string.h" +#include "data/hashtable.h" +#include "data/symbol.h" +#include "data/list.h" + +void data_sys_init() { + sbString_sys_init(); + sbHash_sys_init(); + sbSymbol_sys_init(); + sbList_sys_init(); +} + +void data_sys_deinit() { + sbList_sys_deinit(); + sbSymbol_sys_deinit(); + sbHash_sys_deinit(); + sbString_sys_deinit(); +} diff --git a/src/data/data.h b/src/data/data.h index f3e3031..9130b59 100644 --- a/src/data/data.h +++ b/src/data/data.h @@ -3,4 +3,8 @@ #include "data/value.h" +void data_sys_init(); + +void data_sys_deinit(); + #endif diff --git a/src/data/list.c b/src/data/list.c new file mode 100644 index 0000000..1ea58db --- /dev/null +++ b/src/data/list.c @@ -0,0 +1,56 @@ +#include "data/list.h" + +#include "gc/gcinfo.h" + +#define LIST_PER_BLOCK 256 + +/* TODO etc etc */ +sbPool g_list_pool; + +typedef struct sbList { + GCINFO gc; + u64 handle; + sbBuffer items; +} sbList; + +sbList *get_list_by_handle(hList handle); + +void sbList_sys_init() { + sbPool_initialize(&g_list_pool, sizeof(sbList), LIST_PER_BLOCK); +} + +void sbList_sys_deinit() { + //sbPool_deinitialize(&g_list_pool); +} + +hList sbList_new(usize capacity) { + usize index; + sbList *l = sbPool_alloc(&g_list_pool, &index); + sbBuffer_initialize(&l->items, capacity * sizeof(hV)); + return index; +} + +void sbList_append(hList list, hV *item) { + sbList *l = get_list_by_handle(list); + sbV_retain(item); + sbBuffer_append(&l->items, item, sizeof(hV)); +} + +hV *sbList_get_value(hList list, usize *length) { + sbList *l = get_list_by_handle(list); + if (length) *length = l->items.size / sizeof(hV); + return (hV*)l->items.data; +} + +hV sbList_index(hList list, usize index) { + sbList *l = get_list_by_handle(list); + hV *item = &((hV*)l->items.data)[index]; + sbV_retain(item); + return *item; +} + +/* --- */ + +sbList *get_list_by_handle(hList handle) { + return sbPool_get_entry(&g_list_pool, handle); +} diff --git a/src/data/list.h b/src/data/list.h new file mode 100644 index 0000000..cd7325e --- /dev/null +++ b/src/data/list.h @@ -0,0 +1,11 @@ +#include "common.h" + +void sbList_sys_init(); + +void sbList_sys_deinit(); + +hList sbList_new(usize capacity); + +void sbList_append(hList list, hV *item); + +hV *sbList_get_value(hList list, usize *length); diff --git a/src/data/value.h b/src/data/value.h index 5ab763f..c016de0 100644 --- a/src/data/value.h +++ b/src/data/value.h @@ -13,6 +13,7 @@ typedef u64 hHash; typedef u64 hString; typedef u64 hSymbol; typedef i64 hInteger; +typedef u64 hList; enum intrinsic_type { IT_NOTHING, // sentinel for "no value here" diff --git a/src/main.c b/src/main.c index d653061..40701b8 100644 --- a/src/main.c +++ b/src/main.c @@ -9,80 +9,76 @@ #include "vm/exec.h" int main(int argc, char **argv) { - if (argc >= 2) { - sbString_sys_init(); - sbHash_sys_init(); - sbSymbol_sys_init(); - - sbParser pr; - sbParser_initialize(&pr); - - const char *filename; - flag debugmode = FALSE; - if (argc == 3 && sbstrncmp(argv[1], "-D", 2) == 0) { - filename = argv[2]; - debugmode = TRUE; - } else if (argc == 2) { - filename = argv[1]; - } else { - printf("usage: %s [-D] \n", argv[0]); - printf("usage: -D = bytecode debugger\n"); - return 0; - } - - sbAst parse_result = sbParser_parse_file(&pr, filename); - - if (parse_result == NULL) { - fprintf(stderr, "fatal error: Could not open script '%s'\n", filename); - return -2; - } else if (parse_result->type == AST_ERROR) { - fprintf(stderr, "fatal error: Could not run '%s' due to syntax errors.\n", filename); - return -1; - } - - /* syntax is valid, now try to compile */ - - sbIrProgram ir = {0}; - - sbIrProgram_initialize(&ir, 32768); - - sbIrProgram_compile_ast(&ir, parse_result); - - /* free AST, don't need it anymore */ - sbParser_deinitialize(&pr); - - if (ir.error_count > 0) { - fprintf(stderr, "Could not run '%s' due to errors.\n", filename); - return -3; - } else { - // print out program - sbIrProgram_print(&ir); - } - - sbVmProgram pm; - sbVmProgram_initialize(&pm, 65536); - - sbEmit_compile_program(&pm, &ir); - - sbIrProgram_deinitialize(&ir); - - sbVm vm = {0}; - sbVm_initialize(&vm, 1048576, 1048576, debugmode); - - sbVm_execute(&vm, &pm); - - printf("Stack result: "); - for (u8 *p = vm.stack; p < vm.sp; p++) { - printf("%02X ", *p); - } - printf("\n"); - - sbVmProgram_deinitialize(&pm); - sbVm_deinitialize(&vm); - sbSymbol_sys_deinit(); - sbHash_sys_deinit(); - sbString_sys_deinit(); + if (argc >= 2) { + const char *filename; + flag debugmode = FALSE; + if (argc == 3 && sbstrncmp(argv[1], "-D", 2) == 0) { + filename = argv[2]; + debugmode = TRUE; + } else if (argc == 2) { + filename = argv[1]; } else { - fprintf(stderr, "please provide a file as input\n"); + printf("usage: %s [-D] \n", argv[0]); + printf("usage: -D = bytecode debugger\n"); + return 0; } + + data_sys_init(); + + sbParser pr; + sbParser_initialize(&pr); + + sbAst parse_result = sbParser_parse_file(&pr, filename); + + if (parse_result == NULL) { + fprintf(stderr, "fatal error: Could not open script '%s'\n", filename); + return -2; + } else if (parse_result->type == AST_ERROR) { + fprintf(stderr, "fatal error: Could not run '%s' due to syntax errors.\n", filename); + return -1; + } + + /* syntax is valid, now try to compile */ + + sbIrProgram ir = {0}; + + sbIrProgram_initialize(&ir, 32768); + + sbIrProgram_compile_ast(&ir, parse_result); + + /* free AST, don't need it anymore */ + sbParser_deinitialize(&pr); + + if (ir.error_count > 0) { + fprintf(stderr, "Could not run '%s' due to errors.\n", filename); + return -3; + } else { + // print out program + sbIrProgram_print(&ir); + } + + sbVmProgram pm; + sbVmProgram_initialize(&pm, 65536); + + sbEmit_compile_program(&pm, &ir); + + sbIrProgram_deinitialize(&ir); + + sbVm vm = {0}; + sbVm_initialize(&vm, 1048576, 1048576, debugmode); + + sbVm_execute(&vm, &pm); + + printf("Stack result: "); + for (u8 *p = vm.stack; p < vm.sp; p++) { + printf("%02X ", *p); + } + printf("\n"); + + sbVmProgram_deinitialize(&pm); + sbVm_deinitialize(&vm); + data_sys_deinit(); + } else { + fprintf(stderr, "please provide a file as input\n"); + } } diff --git a/src/vm/exec.c b/src/vm/exec.c index df5ca69..16919cc 100644 --- a/src/vm/exec.c +++ b/src/vm/exec.c @@ -1,6 +1,6 @@ #include "vm/exec.h" -#include "data/operations.h" +#include "vm/operations.h" void call_block(hVm vm, usize block_id); void execute_instruction(hVm vm); diff --git a/src/data/operations.c b/src/vm/operations.c similarity index 98% rename from src/data/operations.c rename to src/vm/operations.c index 4ca5827..d7fbcbc 100644 --- a/src/data/operations.c +++ b/src/vm/operations.c @@ -1,4 +1,4 @@ -#include "data/operations.h" +#include "vm/operations.h" #include "data/integer.h" diff --git a/src/data/operations.h b/src/vm/operations.h similarity index 100% rename from src/data/operations.h rename to src/vm/operations.h