31 lines
813 B
Python
31 lines
813 B
Python
|
|
from xic_loader import XICProgram
|
||
|
|
from xic_ops import XICContext, OP_TABLE
|
||
|
|
|
||
|
|
|
||
|
|
class XICRuntimeError(Exception):
|
||
|
|
pass
|
||
|
|
|
||
|
|
|
||
|
|
def run_xic_program(prog: XICProgram) -> XICContext:
|
||
|
|
"""Execute an XIC program.
|
||
|
|
|
||
|
|
Creates a context, iterates through instructions, dispatches
|
||
|
|
each operation to the handler in OP_TABLE, and returns the final context.
|
||
|
|
"""
|
||
|
|
ctx = XICContext()
|
||
|
|
|
||
|
|
for i, instr in enumerate(prog.instructions):
|
||
|
|
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}")
|
||
|
|
|
||
|
|
return ctx
|