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
22 | NEGATIVE (* unary minus *)
31 | BANG (* logical not *)
41 | Cast of Type.vtype * exp
42 | ConstExp of Word32.word
44 | OpExp of oper * exp list
45 | Marked of (* Kane *) exp Mark.marked
46 | FuncCall of ident * (exp list)
47 | Member of exp * ident
48 | DerefMember of exp * ident
50 | ArrIndex of exp * exp
52 | NewArr of Type.vtype * exp
54 | Conditional of exp * exp * exp
57 | AsnOp of oper * exp * exp
58 | Effect of exp (* Just side effect the expression *)
63 | If of exp * stm list * stm list option
64 | For of stm option * exp * stm option * stm list
65 | While of exp * stm list
66 | MarkedStm of stm Mark.marked
69 Extern of Type.vtype * (Type.variable list)
70 | Function of Type.vtype * (Type.variable list) * (Type.variable list) * stm list
71 | MarkedFunction of function Mark.marked
73 type program = Type.typedef Symbol.table * function Symbol.table
75 (* print as source, with redundant parentheses *)
78 val pp_exp : exp -> string
79 val pp_stm : stm -> string
80 val pp_program : program -> string
84 structure Ast :> AST =
86 type ident = Symbol.symbol
94 | NEGATIVE (* unary minus *)
113 | Cast of Type.vtype * exp
114 | ConstExp of Word32.word
115 | StringExp of string
116 | OpExp of oper * exp list
117 | Marked of exp Mark.marked
118 | FuncCall of ident * (exp list)
119 | Member of exp * ident
120 | DerefMember of exp * ident
122 | ArrIndex of exp * exp
124 | NewArr of Type.vtype * exp
126 | Conditional of exp * exp * exp
129 | AsnOp of oper * exp * exp
130 | Effect of exp (* Just side effect the expression *)
135 | If of exp * stm list * stm list option
136 | For of stm option * exp * stm option * stm list
137 | While of exp * stm list
138 | MarkedStm of stm Mark.marked
141 Extern of Type.vtype * (Type.variable list)
142 | Function of Type.vtype * (Type.variable list) * (Type.variable list) * stm list
143 | MarkedFunction of function Mark.marked
145 type program = Type.typedef Symbol.table * function Symbol.table
147 (* print programs and expressions in source form
148 * using redundant parentheses to clarify precedence
152 fun pp_ident id = Symbol.name id
154 fun pp_oper PLUS = "+"
155 | pp_oper MINUS = "-"
156 | pp_oper TIMES = "*"
157 | pp_oper DIVIDEDBY = "/"
158 | pp_oper MODULO = "%"
159 | pp_oper NEGATIVE = "-"
162 | pp_oper LOGAND = "&&"
163 | pp_oper LOGOR = "||"
164 | pp_oper BITAND = "&"
165 | pp_oper BITXOR = "^"
166 | pp_oper BITOR = "|"
167 | pp_oper BITNOT = "~"
176 fun pp_exp (Var(id)) = pp_ident id
177 | pp_exp (Cast(ty, exp)) = "["^(Type.Print.pp_type ty)^"]"^(pp_exp exp)
178 | pp_exp (ConstExp(c)) = Word32Signed.toString c
179 | pp_exp (StringExp(s)) = "\"" ^ s ^ "\""
180 | pp_exp (OpExp(oper, [e])) =
181 pp_oper oper ^ "(" ^ pp_exp e ^ ")"
182 | pp_exp (OpExp(oper, [e1,e2])) =
183 "(" ^ pp_exp e1 ^ " " ^ pp_oper oper
184 ^ " " ^ pp_exp e2 ^ ")"
185 | pp_exp (OpExp(oper, _)) =
187 | pp_exp (FuncCall(id, l)) = pp_ident id ^ "(" ^ pp_expl l ^ ")"
188 | pp_exp (Marked(marked_exp)) =
189 pp_exp (Mark.data marked_exp)
190 | pp_exp (Member(e, i)) = pp_exp e ^ "." ^ pp_ident i
191 | pp_exp (DerefMember(e, i)) = pp_exp e ^ "->" ^ pp_ident i
192 | pp_exp (Dereference(e)) = "*(" ^ pp_exp e ^ ")"
193 | pp_exp (ArrIndex(e1, e2)) = pp_exp e1 ^ "[" ^pp_exp e2 ^ "]"
194 | pp_exp (New t) = "new(" ^ Type.Print.pp_type t ^ ")"
195 | pp_exp (NewArr (t, s)) = "new(" ^ Type.Print.pp_type t ^ "[" ^ pp_exp s ^ "])"
196 | pp_exp Null = "NULL"
197 | pp_exp (Conditional (q, e1, e2)) = "("^(pp_exp q)^"?"^(pp_exp e1)^":"^(pp_exp e2)^")"
200 | pp_expl (e::a::l) = (pp_exp e) ^ ", " ^ (pp_expl (a::l))
201 | pp_expl (e::l) = (pp_exp e) ^ (pp_expl l)
203 and pp_stm (Assign (e1,e2)) =
204 pp_exp e1 ^ " = " ^ pp_exp e2 ^ ";\n"
205 | pp_stm (AsnOp (oop, e1, e2)) =
206 pp_exp e1 ^ " " ^ pp_oper oop ^ "= " ^ pp_exp e2 ^ ";\n"
207 | pp_stm (Effect (e)) =
209 | pp_stm (Return e) =
210 "return " ^ pp_exp e ^ ";\n"
212 | pp_stm Break = "break;\n"
213 | pp_stm Continue = "continue;\n"
214 | pp_stm (If (e, s, NONE)) = "if ("^pp_exp e^")\n"^pp_block s
215 | pp_stm (If (e, s, SOME s2)) = "if ("^pp_exp e^")\n"^pp_block s^"else\n"^pp_block s2
216 | pp_stm (While (e, s)) = "while ("^pp_exp e^")\n"^pp_block s
217 | 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 "") ^ ")\n" ^ pp_block s
218 | pp_stm (MarkedStm m) = pp_stm (Mark.data m)
220 and pp_block (nil) = ";"
221 | pp_block (a::nil) = pp_stm a
223 val contents = map pp_stm l
225 "{\n" ^ String.concat contents ^ "}\n"
229 | pp_stms (s::ss) = pp_stm s ^ "\n" ^ pp_stms ss
231 and pp_params nil = ""
232 | pp_params ((i, t)::a::l) = (pp_ident i) ^ " : " ^ (Type.Print.pp_type t) ^ ", " ^ (pp_params (a::l))
233 | pp_params ((i, t)::l) = (pp_ident i) ^ " : " ^ (Type.Print.pp_type t) ^ (pp_params l)
236 | pp_vars ((i, t)::l) = "var " ^ (pp_ident i) ^ " : " ^ (Type.Print.pp_type t) ^ ";\n" ^ (pp_vars l)
238 and pp_function (n, Extern(t, pl)) = "extern " ^ (Type.Print.pp_type t) ^ " " ^ (pp_ident n) ^ "(" ^ (pp_params pl) ^ ");\n"
239 | pp_function (n, Function(t, pl, vl, stms)) = (Type.Print.pp_type t) ^ " " ^ (pp_ident n) ^ "(" ^ (pp_params pl) ^ ")\n{\n" ^ (pp_vars vl) ^ (String.concat (map pp_stm stms)) ^ "\n}\n"
240 | pp_function (n, MarkedFunction d) = pp_function (n, Mark.data d)
242 and pp_program (types, funs) = String.concat ((map Type.Print.pp_typedef (Symbol.elemsi types)) @ (map pp_function (Symbol.elemsi funs)))