Presentation is loading. Please wait.

Presentation is loading. Please wait.

Standard ML- Part III Compiler Baojian Hua

Similar presentations


Presentation on theme: "Standard ML- Part III Compiler Baojian Hua"— Presentation transcript:

1 Standard ML- Part III Compiler Baojian Hua bjhua@ustc.edu.cn

2 Recap SML core language declarations functions Module system signature structure functor Today, we glue these together by study an example—the min-ML language

3 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

4 Representation One structure for one left-hand side non-terminal Inside, relevant algebraic datatypes and auxiliary functions

5 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

6 Sample Program val x = 5; val _ = printInt (x); val y = 6; val z = if (false) then 7 else x + y; val _ = printInt (z);

7 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")]

8 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

9 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

10 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.

11 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


Download ppt "Standard ML- Part III Compiler Baojian Hua"

Similar presentations


Ads by Google