3 * Author: Kaustuv Chaudhuri <kaustuv+@cs.cmu.edu>
4 * Modified: Frank Pfenning <fp@cs.cmu.edu>
5 * Modified: Joshua Wise <jwise@andrew.cmu.edu>
6 * Modified: Chris Lu <czl@andrew.cmu.edu>
10 structure AU = AstUtils
11 structure AUP = AstUtils.Program
13 (* for simplicity, we only mark expressions, not statements *)
15 (* mark e with region (left, right) in source file *)
16 fun mark (e, (left, right)) = A.Marked (Mark.mark' (e, ParseState.ext (left, right)))
17 fun markstm (e, (left, right)) = A.MarkedStm (Mark.mark' (e, ParseState.ext (left, right)))
18 fun markfunction (e, (left, right)) = A.MarkedFunction (Mark.mark' (e, ParseState.ext (left, right)))
19 fun marktypedef (e, (left, right)) = A.MarkedTypedef (Mark.mark' (e, ParseState.ext (left, right)))
21 (* create lval from expression; here just an id *)
22 (* generates error if not an identifier *)
23 fun make_lval (A.Var(id)) ext = id
24 | make_lval (A.Marked(marked_exp)) ext =
25 make_lval (Mark.data marked_exp) (Mark.ext marked_exp)
26 | make_lval _ ext = ( ErrorMsg.error ext "not a variable" ;
30 %header (functor L4LrValsFn (structure Token : TOKEN))
35 | INTNUM of Word32.word
36 | IDENT of Symbol.symbol
38 | PLUS | MINUS | STAR | SLASH | PERCENT | LSH | RSH | LOGOR | LOGAND | BITAND | BITXOR | BITOR | BITNOT | BANG
39 | ASSIGN | PLUSEQ | MINUSEQ | STAREQ | SLASHEQ | PERCENTEQ | LSHEQ | RSHEQ | BITANDEQ | BITXOREQ | BITOREQ
40 | EQ | NEQ | LT | LE | GT | GE
41 | IF | ELSE | WHILE | FOR | CONTINUE | BREAK
44 | UNARY | ASNOP (* dummy *)
45 | EXTERN | VAR | INT | COLON | COMMA | STRUCT | NULL | LBRACKET | RBRACKET | ARROW | DOT | NEW
49 | programx of A.program
55 | explist of A.exp list
59 | simpoption of A.stm option
60 | elseoption of A.stm list option
61 | idents of A.ident list
64 | extdecl of A.ident * A.function
65 | paramlist of A.variable list
67 | typedecl of A.ident * A.typedef
68 | memberlist of (A.ident * A.vtype) list
69 | member of (A.ident * A.vtype)
70 | function of A.ident * A.function
71 | vardecl of A.variable list
72 | vardecls of A.variable list
74 %verbose (* print summary of errors *)
75 %pos int (* positions *)
91 %left STAR SLASH PERCENT
93 %left LPAREN LBRACKET ARROW DOT
97 program : programx (programx)
99 programx : decls (decls)
100 | programx function (AUP.append_function programx function)
103 | IDENT (A.Typedef IDENT)
104 | vtype STAR (A.Pointer vtype)
105 | vtype LBRACKET RBRACKET
108 decls : (Symbol.empty, Symbol.empty)
109 | typedecl decls (AUP.append_typedef decls typedecl)
110 | extdecl decls (AUP.append_function decls extdecl)
112 extdecl : EXTERN vtype IDENT LPAREN RPAREN SEMI
113 (IDENT, markfunction (A.Extern (vtype, []), (EXTERNleft, SEMIright)))
114 | EXTERN vtype IDENT LPAREN paramlist RPAREN SEMI
115 (IDENT, markfunction (A.Extern (vtype, paramlist), (EXTERNleft, SEMIright)))
117 paramlist : param COMMA paramlist (param :: paramlist)
120 param : IDENT COLON vtype (IDENT, vtype)
122 typedecl : STRUCT IDENT LBRACE RBRACE SEMI
123 (IDENT, marktypedef (A.Struct ([]), (STRUCTleft, SEMIright)))
124 | STRUCT IDENT LBRACE memberlist RBRACE SEMI
125 (IDENT, marktypedef (A.Struct (memberlist), (STRUCTleft, SEMIright)))
127 memberlist : member memberlist (member :: memberlist)
130 member : IDENT COLON vtype SEMI (IDENT, vtype)
132 function : vtype IDENT LPAREN paramlist RPAREN LBRACE vardecls stms RBRACE
133 (IDENT, markfunction (A.Function (vtype, paramlist, vardecls, stms), (vtypeleft, RBRACEright)))
134 | vtype IDENT LPAREN RPAREN LBRACE vardecls stms RBRACE
135 (IDENT, markfunction (A.Function (vtype, [], vardecls, stms), (vtypeleft, RBRACEright)))
138 | vardecl vardecls (vardecl @ vardecls)
140 vardecl : VAR idents COLON vtype SEMI
141 (map (fn x => (x, vtype)) idents)
143 idents : IDENT ([IDENT])
144 | IDENT COMMA idents (IDENT :: idents)
147 | stm stms (stm :: stms)
149 stm : simp SEMI (simp)
153 simp : exp ASSIGN exp %prec ASNOP
154 (A.Assign(exp1, exp2))
155 | exp asnop exp %prec ASNOP
156 (A.AsnOp(asnop, exp1, exp2))
157 | exp (markstm (A.Effect (exp), (expleft, expright)))
159 control : IF LPAREN exp RPAREN block elseoption
160 (markstm ((A.If (exp, block, elseoption)), (IFleft, elseoptionright)))
161 | WHILE LPAREN exp RPAREN block
162 (markstm ((A.While (exp, block)), (WHILEleft, blockright)))
163 | FOR LPAREN simpoption SEMI exp SEMI simpoption RPAREN block
164 (markstm ((A.For (simpoption1, exp, simpoption2, block)), (FORleft, blockright)))
165 | CONTINUE SEMI (markstm ((A.Continue), (CONTINUEleft, SEMIright)))
166 | BREAK SEMI (markstm ((A.Break), (BREAKleft, SEMIright)))
167 | RETURN exp SEMI (markstm ((A.Return exp), (RETURNleft, SEMIright)))
169 elseoption : ELSE block (SOME block)
176 | LBRACE stms RBRACE (stms)
178 exp : LPAREN exp RPAREN (exp)
179 | INTNUM (mark (A.ConstExp(INTNUM),(INTNUMleft,INTNUMright)))
180 | IDENT (mark (A.Var(IDENT), (IDENTleft,IDENTright)))
181 | exp DOT IDENT (mark (A.Member(exp, IDENT), (expleft, IDENTright)))
182 | exp ARROW IDENT (mark (A.DerefMember(exp, IDENT), (expleft, IDENTright)))
183 | STAR exp %prec UNARY (mark (A.Dereference(exp), (STARleft, expright)))
184 | exp LBRACKET exp RBRACKET
185 (mark (A.ArrIndex(exp1, exp2), (exp1left, exp2right)))
186 | exp PLUS exp (mark (A.OpExp (A.PLUS, [exp1,exp2]), (exp1left,exp2right)))
187 | exp MINUS exp (mark (A.OpExp (A.MINUS, [exp1,exp2]), (exp1left,exp2right)))
188 | exp STAR exp (mark (A.OpExp (A.TIMES, [exp1,exp2]), (exp1left,exp2right)))
189 | exp SLASH exp (mark (A.OpExp (A.DIVIDEDBY, [exp1,exp2]), (exp1left,exp2right)))
190 | exp PERCENT exp (mark (A.OpExp (A.MODULO, [exp1,exp2]), (exp1left,exp2right)))
191 | exp LSH exp (mark (A.OpExp (A.LSH, [exp1,exp2]), (exp1left,exp2right)))
192 | exp RSH exp (mark (A.OpExp (A.RSH, [exp1,exp2]), (exp1left,exp2right)))
193 | exp LOGOR exp (mark (A.OpExp (A.LOGOR, [exp1,exp2]), (exp1left,exp2right)))
194 | exp LOGAND exp (mark (A.OpExp (A.LOGAND, [exp1,exp2]), (exp1left,exp2right)))
195 | exp BITOR exp (mark (A.OpExp (A.BITOR, [exp1,exp2]), (exp1left,exp2right)))
196 | exp BITAND exp (mark (A.OpExp (A.BITAND, [exp1,exp2]), (exp1left,exp2right)))
197 | exp BITXOR exp (mark (A.OpExp (A.BITXOR, [exp1,exp2]), (exp1left,exp2right)))
198 | exp EQ exp (mark (A.OpExp (A.EQ, [exp1,exp2]), (exp1left,exp2right)))
199 | exp NEQ exp (mark (A.OpExp (A.NEQ, [exp1,exp2]), (exp1left,exp2right)))
200 | exp LT exp (mark (A.OpExp (A.LT, [exp1,exp2]), (exp1left,exp2right)))
201 | exp LE exp (mark (A.OpExp (A.LE, [exp1,exp2]), (exp1left,exp2right)))
202 | exp GT exp (mark (A.OpExp (A.GT, [exp1,exp2]), (exp1left,exp2right)))
203 | exp GE exp (mark (A.OpExp (A.GE, [exp1,exp2]), (exp1left,exp2right)))
204 | NULL (mark (A.Null, (NULLleft, NULLright)))
205 | IDENT LPAREN RPAREN (mark (A.FuncCall(IDENT, []), (IDENTleft, RPARENright)))
206 | IDENT LPAREN explist RPAREN
207 (mark (A.FuncCall(IDENT, explist), (IDENTleft, RPARENright)))
208 | NEW LPAREN vtype RPAREN
209 (mark (A.New (vtype), (NEWleft, RPARENright)))
210 | NEW LPAREN vtype LBRACKET exp RBRACKET RPAREN
211 (mark (A.NewArr (vtype, exp), (NEWleft, RPARENright)))
212 | MINUS exp %prec UNARY (mark (A.OpExp (A.NEGATIVE, [exp]), (MINUSleft,expright)))
213 | BITNOT exp %prec UNARY (mark (A.OpExp (A.BITNOT, [exp]), (BITNOTleft,expright)))
214 | BANG exp %prec UNARY (mark (A.OpExp (A.BANG, [exp]), (BANGleft,expright)))
216 explist : exp ([exp])
217 | exp COMMA explist (exp :: explist)
219 asnop : PLUSEQ (A.PLUS)
222 | SLASHEQ (A.DIVIDEDBY)
223 | PERCENTEQ (A.MODULO)
227 | BITANDEQ (A.BITAND)
228 | BITXOREQ (A.BITXOR)