]> Joshua Wise's Git repositories - snipe.git/blame - parse/ast.sml
Add string pasting support to the gramamr.
[snipe.git] / parse / ast.sml
CommitLineData
6ade8b0a 1(* L3 Compiler
12aa4087
JW
2 * Abstract Syntax Trees
3 * Author: Alex Vaynberg
4 * Modified: Frank Pfenning <fp@cs.cmu.edu>
0a24e44d
JW
5 * Modified: Joshua Wise <jwise@andrew.cmu.edu>
6 * Modified: Chris Lu <czl@andrew.cmu.edu>
12aa4087
JW
7 *
8 * Uses pretty printing library
9 * structure PP -- see util/pp.sml
10 *)
11
12signature AST =
13sig
14 type ident = Symbol.symbol
6ade8b0a 15
12aa4087
JW
16 datatype oper =
17 PLUS
18 | MINUS
19 | TIMES
20 | DIVIDEDBY
21 | MODULO
22 | NEGATIVE (* unary minus *)
0a24e44d
JW
23 | LSH
24 | RSH
25 | LOGOR
26 | LOGAND
27 | BITAND
28 | BITXOR
29 | BITOR
30 | BITNOT
31 | BANG (* logical not *)
32 | NEQ
33 | EQ
34 | LT
35 | LE
36 | GE
37 | GT
12aa4087
JW
38
39 datatype exp =
40 Var of ident
e63d3705 41 | Cast of Type.vtype * exp
12aa4087 42 | ConstExp of Word32.word
2ab9671f 43 | StringExp of string
12aa4087 44 | OpExp of oper * exp list
0a24e44d 45 | Marked of (* Kane *) exp Mark.marked
6ade8b0a 46 | FuncCall of ident * (exp list)
1144856b
JW
47 | Member of exp * ident
48 | DerefMember of exp * ident
49 | Dereference of exp
50 | ArrIndex of exp * exp
5c79bb68
JW
51 | New of Type.vtype
52 | NewArr of Type.vtype * exp
1144856b 53 | Null
5c79bb68 54 | Conditional of exp * exp * exp
12aa4087 55 and stm =
1144856b
JW
56 Assign of exp * exp
57 | AsnOp of oper * exp * exp
58 | Effect of exp (* Just side effect the expression *)
12aa4087 59 | Return of exp
0a24e44d
JW
60 | Nop
61 | Break
62 | Continue
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
12aa4087 67
6ade8b0a 68 datatype function =
5c79bb68
JW
69 Extern of Type.vtype * (Type.variable list)
70 | Function of Type.vtype * (Type.variable list) * (Type.variable list) * stm list
1144856b 71 | MarkedFunction of function Mark.marked
6ade8b0a 72
5c79bb68 73 type program = Type.typedef Symbol.table * function Symbol.table
12aa4087
JW
74
75 (* print as source, with redundant parentheses *)
76 structure Print :
77 sig
78 val pp_exp : exp -> string
79 val pp_stm : stm -> string
80 val pp_program : program -> string
81 end
12aa4087
JW
82end
83
84structure Ast :> AST =
85struct
86 type ident = Symbol.symbol
87
88 datatype oper =
89 PLUS
90 | MINUS
91 | TIMES
92 | DIVIDEDBY
93 | MODULO
94 | NEGATIVE (* unary minus *)
0a24e44d
JW
95 | LSH
96 | RSH
97 | LOGOR
98 | LOGAND
99 | BITAND
100 | BITXOR
101 | BITOR
102 | BITNOT
103 | BANG
104 | NEQ
105 | EQ
106 | LT
107 | LE
108 | GE
109 | GT
12aa4087
JW
110
111 datatype exp =
112 Var of ident
e63d3705 113 | Cast of Type.vtype * exp
12aa4087 114 | ConstExp of Word32.word
2ab9671f 115 | StringExp of string
12aa4087
JW
116 | OpExp of oper * exp list
117 | Marked of exp Mark.marked
6ade8b0a 118 | FuncCall of ident * (exp list)
1144856b
JW
119 | Member of exp * ident
120 | DerefMember of exp * ident
121 | Dereference of exp
122 | ArrIndex of exp * exp
5c79bb68
JW
123 | New of Type.vtype
124 | NewArr of Type.vtype * exp
1144856b 125 | Null
5c79bb68 126 | Conditional of exp * exp * exp
12aa4087 127 and stm =
1144856b
JW
128 Assign of exp * exp
129 | AsnOp of oper * exp * exp
130 | Effect of exp (* Just side effect the expression *)
0a24e44d
JW
131 | Return of exp
132 | Nop
133 | Break
134 | Continue
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
12aa4087 139
6ade8b0a 140 datatype function =
5c79bb68
JW
141 Extern of Type.vtype * (Type.variable list)
142 | Function of Type.vtype * (Type.variable list) * (Type.variable list) * stm list
1144856b 143 | MarkedFunction of function Mark.marked
6ade8b0a 144
5c79bb68 145 type program = Type.typedef Symbol.table * function Symbol.table
12aa4087
JW
146
147 (* print programs and expressions in source form
148 * using redundant parentheses to clarify precedence
149 *)
150 structure Print =
151 struct
152 fun pp_ident id = Symbol.name id
153
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 = "-"
0a24e44d
JW
160 | pp_oper LSH = "<<"
161 | pp_oper RSH = ">>"
162 | pp_oper LOGAND = "&&"
163 | pp_oper LOGOR = "||"
164 | pp_oper BITAND = "&"
165 | pp_oper BITXOR = "^"
166 | pp_oper BITOR = "|"
167 | pp_oper BITNOT = "~"
168 | pp_oper BANG = "!"
169 | pp_oper NEQ = "!="
170 | pp_oper EQ = "=="
171 | pp_oper LT = "<"
172 | pp_oper LE = "<="
173 | pp_oper GT = ">"
174 | pp_oper GE = ">="
12aa4087
JW
175
176 fun pp_exp (Var(id)) = pp_ident id
e63d3705 177 | pp_exp (Cast(ty, exp)) = "["^(Type.Print.pp_type ty)^"]"^(pp_exp exp)
12aa4087 178 | pp_exp (ConstExp(c)) = Word32Signed.toString c
2ab9671f 179 | pp_exp (StringExp(s)) = "\"" ^ s ^ "\""
12aa4087
JW
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 ^ ")"
0a24e44d
JW
185 | pp_exp (OpExp(oper, _)) =
186 pp_oper oper
6ade8b0a 187 | pp_exp (FuncCall(id, l)) = pp_ident id ^ "(" ^ pp_expl l ^ ")"
12aa4087
JW
188 | pp_exp (Marked(marked_exp)) =
189 pp_exp (Mark.data marked_exp)
1144856b
JW
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 ^ "]"
5c79bb68
JW
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 ^ "])"
1144856b 196 | pp_exp Null = "NULL"
5c79bb68 197 | pp_exp (Conditional (q, e1, e2)) = "("^(pp_exp q)^"?"^(pp_exp e1)^":"^(pp_exp e2)^")"
6ade8b0a
JW
198
199 and pp_expl nil = ""
200 | pp_expl (e::a::l) = (pp_exp e) ^ ", " ^ (pp_expl (a::l))
201 | pp_expl (e::l) = (pp_exp e) ^ (pp_expl l)
12aa4087 202
1144856b
JW
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)) =
208 pp_exp e ^ ";\n"
12aa4087 209 | pp_stm (Return e) =
1144856b
JW
210 "return " ^ pp_exp e ^ ";\n"
211 | pp_stm Nop = ";\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
0a24e44d
JW
218 | pp_stm (MarkedStm m) = pp_stm (Mark.data m)
219
220 and pp_block (nil) = ";"
221 | pp_block (a::nil) = pp_stm a
222 | pp_block (l) = let
223 val contents = map pp_stm l
224 in
6ade8b0a 225 "{\n" ^ String.concat contents ^ "}\n"
0a24e44d 226 end
12aa4087 227
6ade8b0a 228 and pp_stms nil = ""
12aa4087 229 | pp_stms (s::ss) = pp_stm s ^ "\n" ^ pp_stms ss
5c79bb68 230
6ade8b0a 231 and pp_params nil = ""
5c79bb68
JW
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)
6ade8b0a
JW
234
235 and pp_vars nil = ""
5c79bb68 236 | pp_vars ((i, t)::l) = "var " ^ (pp_ident i) ^ " : " ^ (Type.Print.pp_type t) ^ ";\n" ^ (pp_vars l)
6ade8b0a 237
5c79bb68
JW
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"
1144856b 240 | pp_function (n, MarkedFunction d) = pp_function (n, Mark.data d)
5c79bb68
JW
241
242 and pp_program (types, funs) = String.concat ((map Type.Print.pp_typedef (Symbol.elemsi types)) @ (map pp_function (Symbol.elemsi funs)))
12aa4087
JW
243 end
244end
This page took 0.043239 seconds and 4 git commands to generate.