VM spec in SX + 72 tests passing on both JS and OCaml

spec/vm.sx — bytecode VM written in SX (the spec):
  - Stack-based interpreter for bytecode from compiler.sx
  - 24 opcodes: constants, variables (local/upvalue/global), control flow,
    function calls (with TCO), closures with upvalue capture, collections,
    string concat, define
  - Upvalue cells for shared mutable closure variables
  - Call dispatch: vm-closure (fast path), native-fn, CEK fallback
  - Platform interface: 7 primitives (vm-stack-*, call-primitive, cek-call,
    get-primitive, env-parent)

spec/tests/test-vm.sx — 72 tests exercising compile→bytecode→VM pipeline:
  constants, arithmetic, comparison, control flow (if/when/cond/case/and/or),
  let bindings, lambda, closures, upvalue mutation, TCO (10K iterations),
  collections, strings, define, letrec, quasiquote, threading, integration
  (fibonacci, recursive map/filter/reduce, compose)

spec/compiler.sx — fix :else keyword detection in case/cond compilation
  (was comparing Keyword object to evaluated string, now checks type)

Platform primitives added (JS + OCaml):
  make-vm-stack, vm-stack-get, vm-stack-set!, vm-stack-length, vm-stack-copy!,
  primitive?, get-primitive, call-primitive, set-nth! (JS)

Test runners updated to load bytecode.sx + compiler.sx + vm.sx for --full.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-24 01:20:00 +00:00
parent 5270d2e956
commit 23c8b97cb1
8 changed files with 1169 additions and 7 deletions

View File

@@ -14,7 +14,7 @@
// =========================================================================
var NIL = Object.freeze({ _nil: true, toString: function() { return "nil"; } });
var SX_VERSION = "2026-03-23T23:55:49Z";
var SX_VERSION = "2026-03-24T01:05:46Z";
function isNil(x) { return x === NIL || x === null || x === undefined; }
function isSxTruthy(x) { return x !== false && !isNil(x); }
@@ -521,6 +521,39 @@
PRIMITIVES["scope-emit!"] = scopeEmit;
PRIMITIVES["scope-peek"] = scopePeek;
// ---- VM stack primitives ----
// The VM spec (vm.sx) requires these array-like operations.
// In JS, a plain Array serves as the stack.
PRIMITIVES["make-vm-stack"] = function(size) {
var a = new Array(size);
for (var i = 0; i < size; i++) a[i] = NIL;
return a;
};
PRIMITIVES["vm-stack-get"] = function(stack, idx) { return stack[idx]; };
PRIMITIVES["vm-stack-set!"] = function(stack, idx, value) { stack[idx] = value; return NIL; };
PRIMITIVES["vm-stack-length"] = function(stack) { return stack.length; };
PRIMITIVES["vm-stack-copy!"] = function(src, dst, count) {
for (var i = 0; i < count; i++) dst[i] = src[i];
return NIL;
};
PRIMITIVES["get-primitive"] = function(name) {
if (name in PRIMITIVES) return PRIMITIVES[name];
throw new Error("VM undefined: " + name);
};
PRIMITIVES["primitive?"] = function(name) {
return name in PRIMITIVES;
};
PRIMITIVES["set-nth!"] = function(lst, idx, val) {
lst[idx] = val;
return NIL;
};
PRIMITIVES["env-parent"] = function(env) {
if (env && Object.getPrototypeOf(env) !== Object.prototype &&
Object.getPrototypeOf(env) !== null)
return Object.getPrototypeOf(env);
return NIL;
};
function isPrimitive(name) { return name in PRIMITIVES; }
function getPrimitive(name) { return PRIMITIVES[name]; }