forked from sennpy/CSC431
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeCheck.g
More file actions
executable file
·403 lines (370 loc) · 11.9 KB
/
Copy pathTypeCheck.g
File metadata and controls
executable file
·403 lines (370 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
tree grammar TypeCheck;
options
{
tokenVocab=Mini;
ASTLabelType=CommonTree;
}
/*
Tree Parser -- typechecks given program
*/
@header
{
import java.util.HashMap;
}
@members
{
private HashMap<String, Type> structs = new HashMap<String, Type>();
private HashMap<String, Type> sTable = new HashMap<String, Type>();
}
translate throws SyntaxException
: ^(PROGRAM t=types d=declarations[sTable] f=functions)
{
if ($f.mainFound == false)
{
throw new SyntaxException("Main function was not found");
}
}
;
types
: ^(TYPES (t=type_decl)*)
|
;
type_decl
: ^(ast=STRUCT id=ID {structs.put($id.text, new RecType());} n=nested_decl)
{
structs.put($id.text, new StructType($n.hash, null));
}
;
nested_decl
returns [HashMap<String, Type> hash = new HashMap<String, Type>();]
: (f=field_decl { $hash.put($f.name, $f.fieldType); })+
;
field_decl
returns [String name, Type fieldType]
: ^(DECL ^(TYPE t=type) id=ID)
{
$name = $id.text;
$fieldType = $t.typeName;
}
;
type
returns [Type typeName = null;]
: INT { $typeName = new IntType(); }
| BOOL { $typeName = new BoolType(); }
| ^(STRUCT id=ID) { $typeName = structs.get($id.text); }
;
declarations[HashMap<String, Type> hash]
: ^(DECLS (decl_list[hash])*)
;
decl_list[HashMap<String, Type> hash]
: ^(DECLLIST ^(TYPE t=type)
(id=ID
{
hash.put($id.text, $t.typeName);
}
)+
)
;
functions
returns [boolean mainFound = false] throws SyntaxException
: ^(FUNCS (f = function {if ($f.mainFound) $mainFound = true;})*)
;
function
returns [boolean mainFound = false] throws SyntaxException
scope { HashMap<String, Type> localHash;
Type ret; }
@init{ $function::localHash = new HashMap<String, Type>(sTable); }
: ^(ast=FUN id=ID p=parameters r=return_type { sTable.put($id.text, new FunType($p.params, $r.typeName));
$function::localHash.put($id.text, new FunType($p.params, $r.typeName));
$function::ret = $r.typeName;}
d=declarations[$function::localHash]
s=statement_list)
{
if ($id.text.equals("main"))
mainFound = true;
if ($s.retCheck == false && !$function::ret.equals(new VoidType()))
throw new SyntaxException("Function " + $id.text + " is missing a return for one or more cases");
}
;
parameters
returns [ArrayList<Type> params = new ArrayList<Type>()]
: ^(PARAMS (p=param_decl {$params.add($p.t2); })*)
;
param_decl
returns [Type t2]
: ^(DECL ^(TYPE t=type) id=ID)
{
$function::localHash.put($id.text, $t.typeName);
$t2 = $t.typeName;
}
;
return_type
returns [Type typeName = null]
: ^(RETTYPE rtype) { $typeName = $rtype.typeName; }
;
rtype
returns [Type typeName = null]
: t=type { $typeName = $t.typeName; }
| VOID { $typeName = new VoidType(); }
;
statement
returns [boolean retCheck = false] throws SyntaxException
: (s=block {$retCheck = $s.retCheck; }
| s=assignment
| s=print
| s=invocation_stmt
| s=delete
| s=read
| s=return_stmt {$retCheck = true;}
| s=conditional {$retCheck = $s.retCheck;}
| s=loop {$retCheck = $s.retCheck;}
)
;
block
returns [boolean retCheck = false] throws SyntaxException
: ^(BLOCK s=statement_list) {$retCheck = $s.retCheck;}
;
statement_list
returns [boolean retCheck = false] throws SyntaxException
: ^(STMTS (s=statement {$retCheck = $retCheck || $s.retCheck;})*)
;
assignment throws SyntaxException
: ^(ast=ASSIGN e=expression l=lvalue)
{
if (!($e.typeName.getClass().equals(NullType.class) || $e.typeName.equals($l.typeName)))
{
throw new SyntaxException("Assignment type mismatch at " + $ast.line + ": have "
+ $e.typeName.getClass().toString() + ", need " + $l.typeName.getClass().toString());
}
}
;
print throws SyntaxException
: ^(ast=PRINT e=expression (ENDL)?)
{
if (!$e.typeName.equals(new IntType()))
{
throw new SyntaxException("Printing non-integer at " + $ast.line);
}
}
;
read throws SyntaxException
: ^(ast=READ l=lvalue)
{
if (!$l.typeName.equals(new IntType()))
{
throw new SyntaxException("Read into non-integer at " + $ast.line);
}
}
;
conditional
returns [boolean retCheck = false] throws SyntaxException
: ^(ast=IF g=expression t=block (e=block {$retCheck = $t.retCheck && $e.retCheck;})?)
{
if (!$g.typeName.equals(new BoolType()))
{
throw new SyntaxException("Non-boolean condition at " + $ast.line);
}
}
;
loop
returns [boolean retCheck = false] throws SyntaxException
: ^(ast=WHILE e=expression b=block expression)
{
$retCheck = $b.retCheck;
if (!$e.typeName.equals(new BoolType()))
{
throw new SyntaxException("Non-boolean condition at " + $ast.line);
}
}
;
delete throws SyntaxException
: ^(ast=DELETE e=expression)
{
if (!$e.typeName.getClass().equals(StructType.class))
{
throw new SyntaxException("Deletion of non-struct at " + $ast.line);
}
}
;
return_stmt throws SyntaxException
@init { boolean retTypeCheck = false; }
: ^(ast=RETURN { if ($function::ret.getClass().equals(VoidType.class))
retTypeCheck = true;
}
(e=expression
{
retTypeCheck = $function::ret.equals($e.typeName);
}
)?)
{
if (!retTypeCheck)
throw new SyntaxException("Invalid return type at " + $ast.line);
}
;
invocation_stmt throws SyntaxException
@init { int index = 0;
Type function;
ArrayList<Type> params;
}
: ^(ast=INVOKE id=ID {function = sTable.get($id.text);
if (function == null || !function.getClass().equals(FunType.class))
throw new SyntaxException("Invocation of non-function at "
+ $ast.line + ": " + $id.text);
params = ((FunType)function).getParams();
}
^(ARGS (e=expression { if (!params.get(index++).equals($e.typeName))
throw new SyntaxException("Non-matching argument for "
+ $id.text + " at " + $ast.line + ": " + $e.text);})*))
{
if (index < params.size())
{
throw new SyntaxException("Too few arguments for " + $id.text + " at " + $ast.line);
}
}
;
catch[IndexOutOfBoundsException ex] { throw new SyntaxException("Too many arguments for function " + $id.text + " at " + $ast.line); }
lvalue
returns [Type typeName = null] throws SyntaxException
: id=ID
{
$typeName = $function::localHash.get($id.text);
if ($typeName == null)
{
throw new SyntaxException("Access of undeclared variable: " + $id.text);
}
}
| ^(ast=DOT l=lvalue id=ID)
{
if (!$l.typeName.getClass().equals(StructType.class))
{
throw new SyntaxException("Access of field of non struct at "
+ $ast.line + ": " + $id.text);
}
$typeName = ((StructType)($l.typeName)).getFieldType($id.text);
if ($typeName == null)
{
throw new SyntaxException("Non-existing field" + $id.text + " at " + $ast.line);
}
}
;
expression
returns [Type typeName = null] throws SyntaxException
: ^((ast=PLUS | ast=MINUS | ast=TIMES | ast=DIVIDE)
lft=expression rht=expression)
{
if (!($lft.typeName.getClass().equals(IntType.class) && $rht.typeName.getClass().equals(IntType.class)))
{
throw new SyntaxException("Need int at " + $ast.line);
}
$typeName = $lft.typeName;
}
| ^((ast=LT | ast=GT | ast=LE | ast=GE) lft=expression rht=expression)
{
if (!($lft.typeName.getClass().equals(IntType.class) && $rht.typeName.getClass().equals(IntType.class)))
{
throw new SyntaxException("Need int at " + $ast.line);
}
$typeName = new BoolType();
}
| ^((ast=EQ | ast=NE) lft=expression rht=expression)
{
if (!($lft.typeName.equals($rht.typeName)))
{
throw new SyntaxException("Need same types at " + $ast.line);
}
$typeName = new BoolType();
}
| ^((ast=AND | ast=OR) lft=expression rht=expression)
{
if(!($lft.typeName.equals(new BoolType()) && $rht.typeName.equals(new BoolType())))
{
throw new SyntaxException("Need boolean at " + $ast.line);
}
$typeName = new BoolType();
}
| ^(ast=NOT e=expression)
{
if (!$e.typeName.getClass().equals(BoolType.class))
{
throw new SyntaxException("Need boolean at " + $ast.line);
}
$typeName = $e.typeName;
}
| ^(ast=NEG e=expression)
{
if (!$e.typeName.getClass().equals(IntType.class))
{
throw new SyntaxException("Need int at " + $ast.line);
}
$typeName = $e.typeName;
}
| ^(ast=DOT e=expression id=ID)
{
if (!$e.typeName.getClass().equals(StructType.class))
{
throw new SyntaxException("Access of field of non struct at "
+ $ast.line + ": " + $id.text);
}
$typeName = ((StructType)($e.typeName)).getFieldType($id.text);
if ($typeName == null)
{
throw new SyntaxException("Non-existing field " +$id.text+ " at " + $ast.line);
}
}
| ie=invocation_exp { $typeName = $ie.typeName; }
| id=ID
{
$typeName = $function::localHash.get($id.text);
if ($typeName == null)
{
throw new SyntaxException("Undeclared variable: " + $id.text);
}
}
| i=INTEGER
{
$typeName = new IntType();
}
| ast=TRUE
{
$typeName = new BoolType();
}
| ast=FALSE
{
$typeName = new BoolType();
}
| ^(ast=NEW id=ID)
{
$typeName = structs.get($id.text);
if ($typeName == null || !$typeName.getClass().equals(StructType.class))
{
throw new SyntaxException("Unknown type: " + $id.text + " at " + $ast.line);
}
}
| ast=NULL
{
$typeName = new NullType();
}
;
invocation_exp
returns [Type typeName = null] throws SyntaxException
@init { int index = 0;
Type function;
ArrayList<Type> params;
}
: ^(ast=INVOKE id=ID {function = sTable.get($id.text);
if (function == null || !function.getClass().equals(FunType.class))
throw new SyntaxException("Invocation of non-function: " + $id.text);
params = ((FunType)function).getParams();
}
^(ARGS (e=expression { if (!params.get(index++).equals($e.typeName))
throw new SyntaxException("Non-matching argument for "
+ $id.text + " at " + $ast.line + ": " + $e.text);})*))
{
if (index < params.size())
{
throw new SyntaxException("Too few arguments for " + $id.text + " at " + $ast.line);
}
$typeName = ((FunType)function).getReturnType();
}
;
catch[IndexOutOfBoundsException ex] { throw new SyntaxException("Too many arguments for function " + $id.text + " at " + $ast.line); }