open GT open Language (* The type for the stack machine instructions *) @type insn = (* binary operator *) | BINOP of string (* put a constant on the stack *) | CONST of int (* read to stack *) | READ (* write from stack *) | WRITE (* load a variable to the stack *) | LD of string (* store a variable from the stack *) | ST of string (* a label *) | LABEL of string (* unconditional jump *) | JMP of string (* conditional jump *) | CJMP of string * string (* begins procedure definition *) | BEGIN of string list * string list (* end procedure definition *) | END (* calls a procedure *) | CALL of string with show (* The type for the stack machine program *) type prg = insn list (* The type for the stack machine configuration: control stack, stack and configuration from statement interpreter *) type config = (prg * State.t) list * int list * Stmt.config (* Stack machine interpreter val eval : env -> config -> prg -> config Takes an environment, a configuration and a program, and returns a configuration as a result. The environment is used to locate a label to jump to (via method env#labeled ) *) let eval env ((cstack, stack, ((st, i, o) as c)) as conf) = failwith "Not implemented" (* Top-level evaluation val run : prg -> int list -> int list Takes a program, an input stream, and returns an output stream this program calculates *) let run p i = let module M = Map.Make (String) in let rec make_map m = function | [] -> m | (LABEL l) :: tl -> make_map (M.add l tl m) tl | _ :: tl -> make_map m tl in let m = make_map M.empty p in let (_, _, (_, _, o)) = eval (object method labeled l = M.find l m end) ([], [], (State.empty, i, [])) p in o (* Stack machine compiler val compile : Language.t -> prg Takes a program in the source language and returns an equivalent program for the stack machine *) let compile (defs, p) = failwith "Not implemented"