Files
2125_GCE/xic_vm.py
T
2026-07-09 12:54:44 -04:00

33 lines
986 B
Python
Executable File

from xic_loader import XICProgram
from xic_ops import XICContext, OP_TABLE
class XICRuntimeError(Exception):
pass
def run_xic_program(prog: XICProgram) -> XICContext:
ctx = XICContext()
ctx._state["_program"] = {
"symbols": prog.symbols,
"instructions": prog.instructions,
}
i = 0
while i < len(prog.instructions):
instr = prog.instructions[i]
op_name = instr.op
if op_name not in OP_TABLE:
raise XICRuntimeError(f"Unknown operation at instruction {i}: {op_name}")
op_handler = OP_TABLE[op_name]
try:
op_handler(ctx, *instr.args)
except Exception as e:
raise XICRuntimeError(f"Operation {op_name} failed at instruction {i}: {e}")
if "chain_target" in ctx._state:
target = ctx._state.pop("chain_target")
if target in prog.symbols:
i = prog.symbols[target]
continue
i += 1
return ctx