Standard ML- Part III Compiler Baojian Hua
Recap SML core language declarations functions Module system signature structure functor Today, we glue these together by study an example—the min-ML language
min-ML Syntax prog -> decs decs -> dec; decs | dec -> val id = exp | val _ = printInt (exp) exp -> id | num | exp + exp | true | false | if (exp) then exp else exp
Representation One structure for one left-hand side non-terminal Inside, relevant algebraic datatypes and auxiliary functions
min-ML Declaration signature Dec = sig datatype t = Bind of {var: string, exp: Exp.t} | Print of Exp.t val layout: t -> Layout.t val size: t -> int end
Sample Program val x = 5; val _ = printInt (x); val y = 6; val z = if (false) then 7 else x + y; val _ = printInt (z);
Sample Program val prog = Prog.T [Stm.Bind {var = "x", exp = Exp.Int 5}, Stm.Print (Exp.Id "x"), Stm.Bind {var = "y", exp = Exp.Int 6}, Stm.Bind {var = "z", exp = Exp.If {cond = Exp.False, truee = Exp.Int 7, falsee = Exp.Add (Exp.Id "x", Exp.Id "y")}}, Stm.Print (Exp.Id "z")]
Interpreter val x = 5; val _ = printInt (x); val y = 6; val z = if (false) then 7 else x + y; val _ = printInt (z); signature INTERP = sig exception Unbound of string val interp: Prog.t -> unit end
Storage Model As the interpreter goes along, it must remember what variables are assigned what values just like a machine memory we make use of the dictionary DS efficiency issues are important! also must handle the case of undeclared variables
Glue Together Datatype definitions for language (syntax) Exp, Dec, Prog, etc. Evaluation (semantics) machine model, etc May be more interesting to add more components, such as type checker, etc.
Lab 1: mini-JVM Design and implement a stack-based virtual machine, just like the Java virtual machine decode the binary (bytecode) syntax tree construction memory model execution engine