|
| 1 | +import syntaxtree.*; |
| 2 | +import visitor.*; |
| 3 | + |
| 4 | +public class FirstVisitor extends GJDepthFirst<String, Void>{ |
| 5 | + |
| 6 | + static public SymbolTable symbolTable = new SymbolTable(); |
| 7 | + // to know at what class and method I am in |
| 8 | + private String curClass; |
| 9 | + private String curMethod; |
| 10 | + |
| 11 | + |
| 12 | + /** |
| 13 | + * f0 -> "class" |
| 14 | + * f1 -> Identifier() |
| 15 | + * f2 -> "{" |
| 16 | + * f3 -> "public" |
| 17 | + * f4 -> "static" |
| 18 | + * f5 -> "void" |
| 19 | + * f6 -> "main" |
| 20 | + * f7 -> "(" |
| 21 | + * f8 -> "String" |
| 22 | + * f9 -> "[" |
| 23 | + * f10 -> "]" |
| 24 | + * f11 -> Identifier() |
| 25 | + * f12 -> ")" |
| 26 | + * f13 -> "{" |
| 27 | + * f14 -> ( VarDeclaration() )* |
| 28 | + * f15 -> ( Statement() )* |
| 29 | + * f16 -> "}" |
| 30 | + * f17 -> "}" |
| 31 | + */ |
| 32 | + @Override |
| 33 | + public String visit(MainClass n, Void argu) throws Exception { |
| 34 | + |
| 35 | + String className = n.f1.accept(this, null); |
| 36 | + |
| 37 | + curClass = className; |
| 38 | + curMethod = "main"; |
| 39 | + |
| 40 | + // insert class at the symbol table |
| 41 | + symbolTable.classDeclaration(className,null); |
| 42 | + |
| 43 | + // insert method main at the symbol table |
| 44 | + symbolTable.MethodDeclaration(className,curMethod,"void"); |
| 45 | + |
| 46 | + super.visit(n, argu); |
| 47 | + |
| 48 | + curClass = null; |
| 49 | + curMethod = null; |
| 50 | + |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * f0 -> "class" |
| 57 | + * f1 -> Identifier() |
| 58 | + * f2 -> "{" |
| 59 | + * f3 -> ( VarDeclaration() )* |
| 60 | + * f4 -> ( MethodDeclaration() )* |
| 61 | + * f5 -> "}" |
| 62 | + */ |
| 63 | + @Override |
| 64 | + public String visit(ClassDeclaration n, Void argu) throws Exception { |
| 65 | + |
| 66 | + String className = n.f1.accept(this, null); |
| 67 | + |
| 68 | + curClass = className; |
| 69 | + |
| 70 | + // insert class at the symbol table |
| 71 | + symbolTable.classDeclaration(className, null); |
| 72 | + |
| 73 | + super.visit(n, argu); |
| 74 | + |
| 75 | + curClass = null; |
| 76 | + |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + /** |
| 82 | + * f0 -> "class" |
| 83 | + * f1 -> Identifier() |
| 84 | + * f2 -> "extends" |
| 85 | + * f3 -> Identifier() |
| 86 | + * f4 -> "{" |
| 87 | + * f5 -> ( VarDeclaration() )* |
| 88 | + * f6 -> ( MethodDeclaration() )* |
| 89 | + * f7 -> "}" |
| 90 | + */ |
| 91 | + @Override |
| 92 | + public String visit(ClassExtendsDeclaration n, Void argu) throws Exception { |
| 93 | + String className = n.f1.accept(this, null); |
| 94 | + String parentName = n.f3.accept(this, null); |
| 95 | + |
| 96 | + curClass = className; |
| 97 | + |
| 98 | + // insert class at the symbol table |
| 99 | + symbolTable.classDeclaration(className, parentName); |
| 100 | + |
| 101 | + super.visit(n, argu); |
| 102 | + |
| 103 | + curClass = null; |
| 104 | + |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + /** |
| 110 | + * f0 -> "public" |
| 111 | + * f1 -> Type() |
| 112 | + * f2 -> Identifier() |
| 113 | + * f3 -> "(" |
| 114 | + * f4 -> ( FormalParameterList() )? |
| 115 | + * f5 -> ")" |
| 116 | + * f6 -> "{" |
| 117 | + * f7 -> ( VarDeclaration() )* |
| 118 | + * f8 -> ( Statement() )* |
| 119 | + * f9 -> "return" |
| 120 | + * f10 -> Expression() |
| 121 | + * f11 -> ";" |
| 122 | + * f12 -> "}" |
| 123 | + */ |
| 124 | + @Override |
| 125 | + public String visit(MethodDeclaration n, Void argu) throws Exception { |
| 126 | + |
| 127 | + String methodType = n.f1.accept(this, null); |
| 128 | + String methodName = n.f2.accept(this, null); |
| 129 | + |
| 130 | + curMethod = methodName; |
| 131 | + |
| 132 | + // insert method at the symbol table |
| 133 | + symbolTable.MethodDeclaration(curClass,methodName,methodType); |
| 134 | + |
| 135 | + super.visit(n, argu); |
| 136 | + |
| 137 | + curMethod = null; |
| 138 | + |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + /** |
| 144 | + * f0 -> Type() |
| 145 | + * f1 -> Identifier() |
| 146 | + */ |
| 147 | + @Override |
| 148 | + public String visit(FormalParameter n, Void argu) throws Exception{ |
| 149 | + String type = n.f0.accept(this, null); |
| 150 | + String id = n.f1.accept(this, null); |
| 151 | + |
| 152 | + // insert method argument at the symbol table |
| 153 | + symbolTable.MethoInsertVarsPars(curClass,curMethod,id,type); |
| 154 | + |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + /** |
| 160 | + * f0 -> Type() |
| 161 | + * f1 -> Identifier() |
| 162 | + * f2 -> ";" |
| 163 | + */ |
| 164 | + @Override |
| 165 | + public String visit(VarDeclaration n, Void argu) throws Exception { |
| 166 | + String type = n.f0.accept(this, argu); |
| 167 | + String id = n.f1.accept(this, argu); |
| 168 | + |
| 169 | + if(curMethod != null){ |
| 170 | + // method variable |
| 171 | + // insert it at the symbol table |
| 172 | + symbolTable.MethoInsertVars(curClass,curMethod,id,type); |
| 173 | + } |
| 174 | + else{ |
| 175 | + // class variable |
| 176 | + // insert it at the symbol table |
| 177 | + symbolTable.classInsertVars(curClass, id, type); |
| 178 | + } |
| 179 | + |
| 180 | + n.f2.accept(this, argu); |
| 181 | + |
| 182 | + return null; |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + |
| 187 | + @Override |
| 188 | + public String visit(ArrayType n, Void argu) { |
| 189 | + return "int[]"; |
| 190 | + } |
| 191 | + |
| 192 | + |
| 193 | + @Override |
| 194 | + public String visit(BooleanType n, Void argu) { |
| 195 | + return "boolean"; |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + @Override |
| 200 | + public String visit(IntegerType n, Void argu) { |
| 201 | + return "int"; |
| 202 | + } |
| 203 | + |
| 204 | + |
| 205 | + @Override |
| 206 | + public String visit(Identifier n, Void argu) { |
| 207 | + return n.f0.toString(); |
| 208 | + } |
| 209 | +} |
0 commit comments