2 * Abstract Syntax Trees
3 * Author: Alex Vaynberg
4 * Modified: Frank Pfenning <fp@cs.cmu.edu>
6 * Uses pretty printing library
7 * structure PP -- see util/pp.sml
12 type ident = Symbol.symbol
20 | NEGATIVE (* unary minus *)
24 | ConstExp of Word32.word
25 | OpExp of oper * exp list
26 | Marked of exp Mark.marked
31 type program = stm list
33 (* print as source, with redundant parentheses *)
36 val pp_exp : exp -> string
37 val pp_stm : stm -> string
38 val pp_program : program -> string
43 structure Ast :> AST =
45 type ident = Symbol.symbol
53 | NEGATIVE (* unary minus *)
57 | ConstExp of Word32.word
58 | OpExp of oper * exp list
59 | Marked of exp Mark.marked
64 type program = stm list
66 (* print programs and expressions in source form
67 * using redundant parentheses to clarify precedence
71 fun pp_ident id = Symbol.name id
73 fun pp_oper PLUS = "+"
76 | pp_oper DIVIDEDBY = "/"
77 | pp_oper MODULO = "%"
78 | pp_oper NEGATIVE = "-"
80 fun pp_exp (Var(id)) = pp_ident id
81 | pp_exp (ConstExp(c)) = Word32Signed.toString c
82 | pp_exp (OpExp(oper, [e])) =
83 pp_oper oper ^ "(" ^ pp_exp e ^ ")"
84 | pp_exp (OpExp(oper, [e1,e2])) =
85 "(" ^ pp_exp e1 ^ " " ^ pp_oper oper
86 ^ " " ^ pp_exp e2 ^ ")"
87 | pp_exp (Marked(marked_exp)) =
88 pp_exp (Mark.data marked_exp)
90 fun pp_stm (Assign (id,e)) =
91 pp_ident id ^ " = " ^ pp_exp e ^ ";"
93 "return " ^ pp_exp e ^ ";"
96 | pp_stms (s::ss) = pp_stm s ^ "\n" ^ pp_stms ss
98 fun pp_program ss = "{\n" ^ pp_stms ss ^ "}"