2026-05-21 01:01:10 -04:00
|
|
|
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()
|
2026-07-09 12:54:44 -04:00
|
|
|
ctx._state["_program"] = {
|
|
|
|
|
"symbols": prog.symbols,
|
|
|
|
|
"instructions": prog.instructions,
|
|
|
|
|
}
|
|
|
|
|
i = 0
|
|
|
|
|
while i < len(prog.instructions):
|
|
|
|
|
instr = prog.instructions[i]
|
2026-05-21 01:01:10 -04:00
|
|
|
op_name = instr.op
|
|
|
|
|
if op_name not in OP_TABLE:
|
2026-07-09 12:54:44 -04:00
|
|
|
raise XICRuntimeError(f"Unknown operation at instruction {i}: {op_name}")
|
2026-05-21 01:01:10 -04:00
|
|
|
op_handler = OP_TABLE[op_name]
|
|
|
|
|
try:
|
|
|
|
|
op_handler(ctx, *instr.args)
|
|
|
|
|
except Exception as e:
|
2026-07-09 12:54:44 -04:00
|
|
|
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
|
2026-05-21 01:01:10 -04:00
|
|
|
return ctx
|