add printing to samples; fix a couple bugs that got overturned
authorcassowarii <2374677+cassowarii@users.noreply.github.com>
Thu, 9 Jul 2026 20:55:18 +0000 (13:55 -0700)
committercassowarii <2374677+cassowarii@users.noreply.github.com>
Thu, 9 Jul 2026 20:55:18 +0000 (13:55 -0700)
sample/project_euler/pe01.sa
sample/project_euler/pe02.sa
sample/project_euler/pe04.sa
src/compile/emit.c
src/vm/exec.c

index a202def..ed0cbc8 100644 (file)
@@ -16,6 +16,6 @@ def iota N {
   L
 }
 
-sum iota(1000).filter => num {
+println sum iota(1000).filter => num {
   [3, 5].any? => factor { num %% factor }
 }
index 33615a7..0b5cc3a 100644 (file)
@@ -16,4 +16,4 @@ repeat {
   accumulator = accumulator + f if f %% 2
 } while f < 4_000_000
 
-accumulator
+println accumulator
index 9b8ef33..52b0b74 100644 (file)
@@ -15,4 +15,4 @@ while a < 1000 {
   a = a + 1
 }
 
-max
+println max
index b28b891..9b40754 100644 (file)
@@ -148,7 +148,7 @@ void compile_stmt(sbVmCompiler *cm, sbIrStmt *stmt) {
         EMIT(BC_JMP);
       } else {
         compile_expr(cm, stmt->jump.condition);
-        debug("%3zu ", sbVmCompiler_get_position(cm));
+        if (cm->debugmode) debug("%3zu ", sbVmCompiler_get_position(cm));
         if (stmt->jump.inverted) {
           EMIT(BC_JF);
         } else {
@@ -170,7 +170,7 @@ void compile_stmt(sbVmCompiler *cm, sbIrStmt *stmt) {
     case IR_S_LABEL:
       stmt->label->found_yet = TRUE;
       stmt->label->block_position = sbVmCompiler_get_position(cm);
-      debug("\n");
+      if (cm->debugmode) debug("\n");
       break;
     case IR_S_ASSIGN:
       compile_expr(cm, stmt->assign.expr);
@@ -196,7 +196,7 @@ void compile_stmt(sbVmCompiler *cm, sbIrStmt *stmt) {
       EMIT(BC_RET);
       break;
     default:
-      debug("haven't implemented this yet!\n");
+      PANIC("haven't implemented this yet!\n");
   }
 }
 
@@ -414,7 +414,7 @@ void compile_expr(sbVmCompiler *cm, sbIrExpr *expr) {
       }
       break;
     default:
-      debug("(compile an expr)\n");
+      PANIC("haven't implemented this expr type!");
   }
 }
 
index 6d49308..7ec60ff 100644 (file)
@@ -220,20 +220,29 @@ void swap_stack_top(hVm vm) {
   hV *first_x = &((hV*)vm->xsp)[-1];
   hV *second_x = &((hV*)vm->xsp)[-2];
 
-  hV xtmp = *first_x;
+  /* if we're swapping things that are allocated on the x-stack,
+   * we have to swap their pointers as well so we don't accidentally
+   * overwrite what they're pointing to. but if they're pointing
+   * somewhere else, we don't care */
+
+  /* x2 x1    &v2 &v1 (might be &x2 &x1)*/
+
+  hV xtmp = *second_x;
   if (*first_v == first_x) {
-    *first_x = *second_x;
-    *first_v = second_x;
+                          /* x2a x1a   &v2  &x1a */
+    *second_x = *first_x; /* x1b x1a   &v2  &x1a */
+    *first_v = second_x;  /* x1b x1a   &v2  &x1b */
   }
 
   if (*second_v == second_x) {
-    *second_x = xtmp;
-    *second_v = first_x;
+                          /* x2a x1a   &x2a &v1  */
+    *first_x = xtmp;      /* x2a x2b   &x2a &v1  */
+    *second_v = first_x;  /* x2a x2b   &x2b &v1  */
   }
 
-  hV *vtmp = *first_v;
-  *first_v = *second_v;
-  *second_v = vtmp;
+  hV *vtmp = *first_v;    /* x2  x1    &v2  &v1  */
+  *first_v = *second_v;   /* x2  x1    &v2  &v2  */
+  *second_v = vtmp;       /* x2  x1    &v1  &v2  */
 }
 
 hV *peek_stack(hVm vm, isize offset) {