|
| 1 | +//Mark Klara |
| 2 | +//mak241@pitt.edu |
| 3 | +//CS 1622 - Project 3 |
| 4 | +//IRVisitor.java |
| 5 | + |
| 6 | +package visitor; |
| 7 | + |
| 8 | +import syntaxtree.*; |
| 9 | +import symboltable.*; |
| 10 | +import IR.*; |
| 11 | + |
| 12 | +public class IRVisitor implements Visitor |
| 13 | +{ |
| 14 | + private Scope currentScope; |
| 15 | + private int blockNumber; |
| 16 | + |
| 17 | + public IRVisitor(Scope symbolTable) |
| 18 | + { |
| 19 | + currentScope = symbolTable; |
| 20 | + blockNumber = 0; |
| 21 | + } |
| 22 | + |
| 23 | + //Helper function to create unique numbers (as strings) for the blocks |
| 24 | + public String nextBlockNumber() |
| 25 | + { |
| 26 | + blockNumber++; |
| 27 | + return ("" + blockNumber); |
| 28 | + } |
| 29 | + |
| 30 | + // MainClass m; |
| 31 | + // ClassDeclList cl; |
| 32 | + public void visit(Program n) |
| 33 | + { |
| 34 | + n.m.accept(this); |
| 35 | + |
| 36 | + for ( int i = 0; i < n.cl.size(); i++ ) |
| 37 | + { |
| 38 | + n.cl.elementAt(i).accept(this); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Identifier i1,i2; |
| 43 | + // Statement s; |
| 44 | + public void visit(MainClass n) |
| 45 | + { |
| 46 | + currentScope = currentScope.enterScope(n.i1.toString()); //Enter class |
| 47 | + currentScope = currentScope.enterScope("main"); |
| 48 | + |
| 49 | + n.i1.accept(this); |
| 50 | + n.i2.accept(this); |
| 51 | + n.s.accept(this); |
| 52 | + |
| 53 | + currentScope = currentScope.exitScope(); //Exit "main" method |
| 54 | + currentScope = currentScope.exitScope(); //Exit class |
| 55 | + } |
| 56 | + |
| 57 | + // Identifier i; |
| 58 | + // VarDeclList vl; |
| 59 | + // MethodDeclList ml; |
| 60 | + public void visit(ClassDeclSimple n) |
| 61 | + { |
| 62 | + currentScope = currentScope.enterScope(n.i.toString()); //Enter class |
| 63 | + |
| 64 | + n.i.accept(this); |
| 65 | + for ( int i = 0; i < n.vl.size(); i++ ) |
| 66 | + { |
| 67 | + n.vl.elementAt(i).accept(this); |
| 68 | + } |
| 69 | + for ( int i = 0; i < n.ml.size(); i++ ) |
| 70 | + { |
| 71 | + n.ml.elementAt(i).accept(this); |
| 72 | + } |
| 73 | + |
| 74 | + currentScope = currentScope.exitScope(); //Exit class |
| 75 | + } |
| 76 | + |
| 77 | + // Identifier i; |
| 78 | + // Identifier j; |
| 79 | + // VarDeclList vl; |
| 80 | + // MethodDeclList ml; |
| 81 | + public void visit(ClassDeclExtends n) |
| 82 | + { |
| 83 | + currentScope = currentScope.enterScope(n.i.toString()); //Enter class |
| 84 | + |
| 85 | + n.i.accept(this); |
| 86 | + n.j.accept(this); |
| 87 | + |
| 88 | + for ( int i = 0; i < n.vl.size(); i++ ) |
| 89 | + { |
| 90 | + n.vl.elementAt(i).accept(this); |
| 91 | + } |
| 92 | + for ( int i = 0; i < n.ml.size(); i++ ) |
| 93 | + { |
| 94 | + n.ml.elementAt(i).accept(this); |
| 95 | + } |
| 96 | + |
| 97 | + currentScope = currentScope.exitScope(); //Exit class |
| 98 | + } |
| 99 | + |
| 100 | + // Type t; |
| 101 | + // Identifier i; |
| 102 | + public void visit(VarDecl n) |
| 103 | + { |
| 104 | + n.t.accept(this); |
| 105 | + n.i.accept(this); |
| 106 | + } |
| 107 | + |
| 108 | + // Type t; |
| 109 | + // Identifier i; |
| 110 | + // FormalList fl; |
| 111 | + // VarDeclList vl; |
| 112 | + // StatementList sl; |
| 113 | + // Exp e; |
| 114 | + public void visit(MethodDecl n) |
| 115 | + { |
| 116 | + currentScope = currentScope.enterScope(n.i.toString()); //Enter method |
| 117 | + |
| 118 | + n.t.accept(this); |
| 119 | + n.i.accept(this); |
| 120 | + |
| 121 | + for ( int i = 0; i < n.fl.size(); i++ ) |
| 122 | + { |
| 123 | + n.fl.elementAt(i).accept(this); |
| 124 | + } |
| 125 | + for ( int i = 0; i < n.vl.size(); i++ ) |
| 126 | + { |
| 127 | + n.vl.elementAt(i).accept(this); |
| 128 | + } |
| 129 | + for ( int i = 0; i < n.sl.size(); i++ ) |
| 130 | + { |
| 131 | + n.sl.elementAt(i).accept(this); |
| 132 | + } |
| 133 | + n.e.accept(this); |
| 134 | + |
| 135 | + currentScope = currentScope.exitScope(); //Exit method |
| 136 | + } |
| 137 | + |
| 138 | + // Type t; |
| 139 | + // Identifier i; |
| 140 | + public void visit(Formal n) |
| 141 | + { |
| 142 | + n.t.accept(this); |
| 143 | + n.i.accept(this); |
| 144 | + } |
| 145 | + |
| 146 | + public void visit(IntArrayType n) |
| 147 | + {} |
| 148 | + |
| 149 | + public void visit(BooleanType n) |
| 150 | + {} |
| 151 | + |
| 152 | + public void visit(IntegerType n) |
| 153 | + {} |
| 154 | + |
| 155 | + // String s; |
| 156 | + public void visit(IdentifierType n) |
| 157 | + {} |
| 158 | + |
| 159 | + // StatementList sl; |
| 160 | + public void visit(Block n) |
| 161 | + { |
| 162 | + String blockNum = nextBlockNumber(); |
| 163 | + currentScope = currentScope.enterScope(blockNum); //Enter block |
| 164 | + |
| 165 | + for ( int i = 0; i < n.sl.size(); i++ ) |
| 166 | + { |
| 167 | + n.sl.elementAt(i).accept(this); |
| 168 | + } |
| 169 | + |
| 170 | + currentScope = currentScope.exitScope(); //Exit block |
| 171 | + } |
| 172 | + |
| 173 | + // Exp e; |
| 174 | + // Statement s1,s2; |
| 175 | + public void visit(If n) |
| 176 | + { |
| 177 | + n.e.accept(this); |
| 178 | + n.s1.accept(this); |
| 179 | + n.s2.accept(this); |
| 180 | + } |
| 181 | + |
| 182 | + // Exp e; |
| 183 | + // Statement s; |
| 184 | + public void visit(While n) |
| 185 | + { |
| 186 | + n.e.accept(this); |
| 187 | + n.s.accept(this); |
| 188 | + } |
| 189 | + |
| 190 | + // Exp e; |
| 191 | + public void visit(Print n) |
| 192 | + { |
| 193 | + n.e.accept(this); |
| 194 | + } |
| 195 | + |
| 196 | + // Identifier i; |
| 197 | + // Exp e; |
| 198 | + public void visit(Assign n) |
| 199 | + { |
| 200 | + n.i.accept(this); |
| 201 | + n.e.accept(this); |
| 202 | + } |
| 203 | + |
| 204 | + // Identifier i; |
| 205 | + // Exp e1,e2; |
| 206 | + public void visit(ArrayAssign n) |
| 207 | + { |
| 208 | + n.i.accept(this); |
| 209 | + n.e1.accept(this); |
| 210 | + n.e2.accept(this); |
| 211 | + } |
| 212 | + |
| 213 | + // Exp e1,e2; |
| 214 | + public void visit(And n) |
| 215 | + { |
| 216 | + n.e1.accept(this); |
| 217 | + n.e2.accept(this); |
| 218 | + } |
| 219 | + |
| 220 | + // Exp e1,e2; |
| 221 | + public void visit(LessThan n) |
| 222 | + { |
| 223 | + n.e1.accept(this); |
| 224 | + n.e2.accept(this); |
| 225 | + } |
| 226 | + |
| 227 | + // Exp e1,e2; |
| 228 | + public void visit(Plus n) |
| 229 | + { |
| 230 | + n.e1.accept(this); |
| 231 | + n.e2.accept(this); |
| 232 | + } |
| 233 | + |
| 234 | + // Exp e1,e2; |
| 235 | + public void visit(Minus n) |
| 236 | + { |
| 237 | + n.e1.accept(this); |
| 238 | + n.e2.accept(this); |
| 239 | + } |
| 240 | + |
| 241 | + // Exp e1,e2; |
| 242 | + public void visit(Times n) |
| 243 | + { |
| 244 | + n.e1.accept(this); |
| 245 | + n.e2.accept(this); |
| 246 | + } |
| 247 | + |
| 248 | + // Exp e1,e2; |
| 249 | + public void visit(ArrayLookup n) |
| 250 | + { |
| 251 | + n.e1.accept(this); |
| 252 | + n.e2.accept(this); |
| 253 | + } |
| 254 | + |
| 255 | + // Exp e; |
| 256 | + public void visit(ArrayLength n) |
| 257 | + { |
| 258 | + n.e.accept(this); |
| 259 | + } |
| 260 | + |
| 261 | + // Exp e; |
| 262 | + // Identifier i; |
| 263 | + // ExpList el; |
| 264 | + public void visit(Call n) |
| 265 | + { |
| 266 | + n.e.accept(this); |
| 267 | + n.i.accept(this); |
| 268 | + |
| 269 | + for ( int i = 0; i < n.el.size(); i++ ) |
| 270 | + { |
| 271 | + n.el.elementAt(i).accept(this); |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + // int i; |
| 276 | + public void visit(IntegerLiteral n) |
| 277 | + {} |
| 278 | + |
| 279 | + public void visit(True n) |
| 280 | + {} |
| 281 | + |
| 282 | + public void visit(False n) |
| 283 | + {} |
| 284 | + |
| 285 | + // String s; |
| 286 | + public void visit(IdentifierExp n) |
| 287 | + {} |
| 288 | + |
| 289 | + public void visit(This n) |
| 290 | + {} |
| 291 | + |
| 292 | + // Exp e; |
| 293 | + public void visit(NewArray n) |
| 294 | + { |
| 295 | + n.e.accept(this); |
| 296 | + } |
| 297 | + |
| 298 | + // Identifier i; |
| 299 | + public void visit(NewObject n) |
| 300 | + {} |
| 301 | + |
| 302 | + // Exp e; |
| 303 | + public void visit(Not n) |
| 304 | + { |
| 305 | + n.e.accept(this); |
| 306 | + } |
| 307 | + |
| 308 | + // String s; |
| 309 | + public void visit(Identifier n) |
| 310 | + {} |
| 311 | +} |
0 commit comments