import { toBytecode, run } from "#reef" // Example 1: Simple arithmetic const arithmetic = toBytecode([ ["PUSH", 5], ["PUSH", 10], ["ADD"], ["HALT"] ]) console.log("5 + 10 =", (await run(arithmetic)).value) // Example 2: Loop with labels const loop = toBytecode([ ["PUSH", 0], ["STORE", "counter"], [".loop:"], ["LOAD", "counter"], ["PUSH", 5], ["LT"], ["JUMP_IF_FALSE", ".end"], ["LOAD", "counter"], ["PUSH", 1], ["ADD"], ["STORE", "counter"], ["JUMP", ".loop"], [".end:"], ["LOAD", "counter"], ["HALT"] ]) console.log("Counter result:", (await run(loop)).value) // Example 3: Function with defaults const functionExample = toBytecode([ ["MAKE_FUNCTION", ["x", "y=10"], ".add_body"], ["STORE", "add"], ["JUMP", ".after"], [".add_body:"], ["LOAD", "x"], ["LOAD", "y"], ["ADD"], ["RETURN"], [".after:"], ["LOAD", "add"], ["PUSH", 5], ["PUSH", 1], ["PUSH", 0], ["CALL"], ["HALT"] ]) console.log("add(5) with default y=10:", (await run(functionExample)).value) // Example 4: Variadic function const variadic = toBytecode([ ["MAKE_FUNCTION", ["...args"], ".sum_body"], ["STORE", "sum"], ["JUMP", ".after"], [".sum_body:"], ["PUSH", 0], ["STORE", "total"], ["LOAD", "args"], ["ARRAY_LEN"], ["STORE", "len"], ["PUSH", 0], ["STORE", "i"], [".loop:"], ["LOAD", "i"], ["LOAD", "len"], ["LT"], ["JUMP_IF_FALSE", ".done"], ["LOAD", "total"], ["LOAD", "args"], ["LOAD", "i"], ["ARRAY_GET"], ["ADD"], ["STORE", "total"], ["LOAD", "i"], ["PUSH", 1], ["ADD"], ["STORE", "i"], ["JUMP", ".loop"], [".done:"], ["LOAD", "total"], ["RETURN"], [".after:"], ["LOAD", "sum"], ["PUSH", 10], ["PUSH", 20], ["PUSH", 30], ["PUSH", 3], ["PUSH", 0], ["CALL"], ["HALT"] ]) console.log("sum(10, 20, 30):", (await run(variadic)).value)