lama_byterun/src/SM.ml

60 lines
2.3 KiB
OCaml
Raw Normal View History

2018-02-20 01:28:29 +03:00
open GT
2018-02-25 19:14:25 +03:00
open Language
2018-02-20 01:28:29 +03:00
(* The type for the stack machine instructions *)
@type insn =
(* binary operator *) | BINOP of string
2018-03-04 18:14:32 +03:00
(* put a constant on the stack *) | CONST of int
2018-02-20 01:28:29 +03:00
(* 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
2018-03-27 03:13:00 +03:00
(* 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
2018-02-20 01:28:29 +03:00
(* The type for the stack machine program *)
type prg = insn list
2018-03-27 22:53:31 +03:00
2018-03-27 03:13:00 +03:00
(* The type for the stack machine configuration: control stack, stack and configuration from statement
2018-02-20 01:28:29 +03:00
interpreter
*)
2018-03-27 03:13:00 +03:00
type config = (prg * State.t) list * int list * Stmt.config
2018-02-20 01:28:29 +03:00
(* Stack machine interpreter
val eval : env -> config -> prg -> config
2018-02-20 01:28:29 +03:00
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 <label_name>)
*)
2018-03-28 17:13:59 +03:00
let eval env ((cstack, stack, ((st, i, o) as c)) as conf) = failwith "Not implemented"
2018-02-20 01:28:29 +03:00
2018-02-25 15:02:30 +03:00
(* Top-level evaluation
2018-03-06 18:46:57 +03:00
val run : prg -> int list -> int list
2018-02-25 15:02:30 +03:00
2018-03-07 14:54:02 +03:00
Takes a program, an input stream, and returns an output stream this program calculates
2018-02-25 15:02:30 +03:00
*)
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
2018-03-27 03:13:00 +03:00
let (_, _, (_, _, o)) = eval (object method labeled l = M.find l m end) ([], [], (State.empty, i, [])) p in o
2018-02-25 15:02:30 +03:00
2018-02-20 01:28:29 +03:00
(* Stack machine compiler
2018-03-27 03:13:00 +03:00
val compile : Language.t -> prg
2018-02-20 01:28:29 +03:00
Takes a program in the source language and returns an equivalent program for the
stack machine
*)
2018-03-28 17:13:59 +03:00
let compile (defs, p) = failwith "Not implemented"