@@ -8,6 +8,29 @@ public class ASTTypeChecker extends DepthFirstAdapter {
8
8
private HashMap <String , String > symbolTable = new HashMap <String , String >();
9
9
private String result ;
10
10
11
+ /**
12
+ * Check if break is only used in a while context
13
+ */
14
+ public void caseABreakExpr (ABreakExpr node ) {
15
+ Node parent = node .parent ();
16
+ String parentName ;
17
+
18
+ do {
19
+ parent = parent .parent ();
20
+ parentName = parent .getClass ().getSimpleName ().replaceAll (" " ,"" );
21
+
22
+ if (parentName .equals ("AWhileExpr" )) break ;
23
+
24
+ } while (!parentName .equals ("AStartExpr" ));
25
+
26
+ if (parentName .equals ("AStartExpr" )) {
27
+ System .out .println ("# Error: User 'break' only in a 'while' context!" );
28
+ System .exit (1 );
29
+ } else {
30
+ printValidOperation ("break" );
31
+ }
32
+ }
33
+
11
34
/**
12
35
* Look up all the declarations and put it into the HashMap
13
36
*/
@@ -159,14 +182,24 @@ public void caseAMinusExpr(AMinusExpr node) {
159
182
}
160
183
@ Override
161
184
public void caseAUnaryMinusExpr (AUnaryMinusExpr node ) {
162
- String operation = "unaryminus " ;
185
+ String operation = "unary - " ;
163
186
node .getExpr ().apply (this );
164
187
165
- System .out .println ("+ " +node .getExpr ().getClass ().getSimpleName ());
188
+ if (!result .equals ("integer" ) && !node .getExpr ().getClass ().getSimpleName ().equals ("AIdentifierExpr" ) && !node .getExpr ().getClass ().getSimpleName ().equals ("ANumberExpr" )) {
189
+ System .out .println ("# Error: Syntax of '" +operation +"' is '" +operation +"' 'integer' = 'integer'." );
190
+ System .exit (1 );
191
+ } else
192
+ printValidOperation (operation );
193
+ }
194
+ @ Override
195
+ public void caseAUnaryPlusExpr (AUnaryPlusExpr node ) {
196
+ String operation = "unary +" ;
197
+ node .getExpr ().apply (this );
166
198
167
- if (!result .equals ("integer" ) || !node .getExpr ().getClass ().getSimpleName ().equals ("AIdentifierExpr" ))
168
- printErrorArithmeticOperation (operation );
169
- else
199
+ if (!result .equals ("integer" ) && !node .getExpr ().getClass ().getSimpleName ().equals ("AIdentifierExpr" ) && !node .getExpr ().getClass ().getSimpleName ().equals ("ANumberExpr" )) {
200
+ System .out .println ("# Error: Syntax of '" +operation +"' is '" +operation +"' 'integer' = 'integer'." );
201
+ System .exit (1 );
202
+ } else
170
203
printValidOperation (operation );
171
204
}
172
205
@ Override
0 commit comments