list data module
authorcassowarii <cassowary@cassowary.me>
Tue, 30 Jun 2026 23:25:22 +0000 (16:25 -0700)
committercassowarii <cassowary@cassowary.me>
Tue, 30 Jun 2026 23:25:22 +0000 (16:25 -0700)
12 files changed:
src/compile/emit.c
src/compile/ir.c
src/compile/ir.h
src/data/data.c [new file with mode: 0644]
src/data/data.h
src/data/list.c [new file with mode: 0644]
src/data/list.h [new file with mode: 0644]
src/data/value.h
src/main.c
src/vm/exec.c
src/vm/operations.c [moved from src/data/operations.c with 98% similarity]
src/vm/operations.h [moved from src/data/operations.h with 100% similarity]

index 8c460da..fed66a9 100644 (file)
@@ -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 */
index cdd9f88..c0ea39f 100644 (file)
@@ -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 = &param;
     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:
index 5b129e4..a696c5d 100644 (file)
@@ -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 (file)
index 0000000..296fb36
--- /dev/null
@@ -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();
+}
index f3e3031..9130b59 100644 (file)
@@ -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 (file)
index 0000000..1ea58db
--- /dev/null
@@ -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 (file)
index 0000000..cd7325e
--- /dev/null
@@ -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);
index 5ab763f..c016de0 100644 (file)
@@ -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"
index d653061..40701b8 100644 (file)
@@ -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] <filename>\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] <filename>\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");
+  }
 }
index df5ca69..16919cc 100644 (file)
@@ -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);
similarity index 98%
rename from src/data/operations.c
rename to src/vm/operations.c
index 4ca5827..d7fbcbc 100644 (file)
@@ -1,4 +1,4 @@
-#include "data/operations.h"
+#include "vm/operations.h"
 
 #include "data/integer.h"
 
similarity index 100%
rename from src/data/operations.h
rename to src/vm/operations.h