2 * Abstract Syntax Trees
3 * Author: Alex Vaynberg
4 * Modified: Frank Pfenning <fp@cs.cmu.edu>
5 * Modified: Joshua Wise <jwise@andrew.cmu.edu>
6 * Modified: Chris Lu <czl@andrew.cmu.edu>
8 * Uses pretty printing library
9 * structure PP -- see util/pp.sml
14 type ident = Symbol.symbol
17 type variable = ident * vtype
25 | NEGATIVE (* unary minus *)
34 | BANG (* logical not *)
44 | ConstExp of Word32.word
45 | OpExp of oper * exp list
46 | Marked of (* Kane *) exp Mark.marked
47 | FuncCall of ident * (exp list)
54 | If of exp * stm list * stm list option
55 | For of stm option * exp * stm option * stm list
56 | While of exp * stm list
57 | MarkedStm of stm Mark.marked
60 Extern of vtype * ident * (variable list)
61 | Function of vtype * ident * (variable list) * (variable list) * stm list
63 type program = function list
65 (* print as source, with redundant parentheses *)
68 val pp_exp : exp -> string
69 val pp_stm : stm -> string
70 val pp_program : program -> string
75 structure Ast :> AST =
77 type ident = Symbol.symbol
80 type variable = ident * vtype
88 | NEGATIVE (* unary minus *)
107 | ConstExp of Word32.word
108 | OpExp of oper * exp list
109 | Marked of exp Mark.marked
110 | FuncCall of ident * (exp list)
112 Assign of ident * exp
117 | If of exp * stm list * stm list option
118 | For of stm option * exp * stm option * stm list
119 | While of exp * stm list
120 | MarkedStm of stm Mark.marked
123 Extern of vtype * ident * (variable list)
124 | Function of vtype * ident * (variable list) * (variable list) * stm list
126 type program = function list
128 (* print programs and expressions in source form
129 * using redundant parentheses to clarify precedence
133 fun pp_ident id = Symbol.name id
135 fun pp_oper PLUS = "+"
136 | pp_oper MINUS = "-"
137 | pp_oper TIMES = "*"
138 | pp_oper DIVIDEDBY = "/"
139 | pp_oper MODULO = "%"
140 | pp_oper NEGATIVE = "-"
143 | pp_oper LOGAND = "&&"
144 | pp_oper LOGOR = "||"
145 | pp_oper BITAND = "&"
146 | pp_oper BITXOR = "^"
147 | pp_oper BITOR = "|"
148 | pp_oper BITNOT = "~"
157 fun pp_exp (Var(id)) = pp_ident id
158 | pp_exp (ConstExp(c)) = Word32Signed.toString c
159 | pp_exp (OpExp(oper, [e])) =
160 pp_oper oper ^ "(" ^ pp_exp e ^ ")"
161 | pp_exp (OpExp(oper, [e1,e2])) =
162 "(" ^ pp_exp e1 ^ " " ^ pp_oper oper
163 ^ " " ^ pp_exp e2 ^ ")"
164 | pp_exp (OpExp(oper, _)) =
166 | pp_exp (FuncCall(id, l)) = pp_ident id ^ "(" ^ pp_expl l ^ ")"
167 | pp_exp (Marked(marked_exp)) =
168 pp_exp (Mark.data marked_exp)
171 | pp_expl (e::a::l) = (pp_exp e) ^ ", " ^ (pp_expl (a::l))
172 | pp_expl (e::l) = (pp_exp e) ^ (pp_expl l)
174 fun pp_stm (Assign (id,e)) =
175 pp_ident id ^ " = " ^ pp_exp e ^ ";"
176 | pp_stm (Return e) =
177 "return " ^ pp_exp e ^ ";"
179 | pp_stm Break = "break;"
180 | pp_stm Continue = "continue;"
181 | pp_stm (If (e, s, NONE)) = "if ("^pp_exp e^")"^pp_block s
182 | pp_stm (If (e, s, SOME s2)) = "if ("^pp_exp e^")"^pp_block s^" else "^pp_block s2
183 | pp_stm (While (e, s)) = "while ("^pp_exp e^") "^pp_block s
184 | pp_stm (For (so1, e, so2, s)) = "for ("^ (if (isSome so1) then pp_stm (valOf so1) else "") ^ pp_exp e ^ (if(isSome so2) then pp_stm (valOf so2) else "") ^ ")" ^ pp_block s
185 | pp_stm (MarkedStm m) = pp_stm (Mark.data m)
187 and pp_block (nil) = ";"
188 | pp_block (a::nil) = pp_stm a
190 val contents = map pp_stm l
192 "{\n" ^ String.concat contents ^ "}\n"
196 | pp_stms (s::ss) = pp_stm s ^ "\n" ^ pp_stms ss
198 and pp_type Int = "int"
200 and pp_params nil = ""
201 | pp_params ((i, t)::a::l) = (pp_ident i) ^ " : " ^ (pp_type t) ^ ", " ^ (pp_params (a::l))
202 | pp_params ((i, t)::l) = (pp_ident i) ^ " : " ^ (pp_type t) ^ (pp_params l)
205 | pp_vars ((i, t)::l) = "var " ^ (pp_ident i) ^ " : " ^ (pp_type t) ^ ";\n" ^ (pp_vars l)
207 and pp_function (Extern(t, n, pl)) = "extern " ^ (pp_type t) ^ " " ^ (pp_ident n) ^ "(" ^ (pp_params pl) ^ ");\n"
208 | pp_function (Function(t, n, pl, vl, stms)) = (pp_type t) ^ " " ^ (pp_ident n) ^ "(" ^ (pp_params pl) ^ ")\n{\n" ^ (pp_vars vl) ^ (String.concat (map pp_stm stms)) ^ "\n}\n"
210 and pp_program (p) = String.concat (map pp_function p)