Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. package cool.compiler;
  2. import cool.structures.*;
  3.  
  4. public class SecondPassVisitor implements ASTVisitor<Void> {
  5. Scope currentScope = null;
  6.  
  7. @Override
  8. public Void visit(Program prog) {
  9. currentScope = new DefaultScope(null);
  10. currentScope.add(TypeSymbol.INT);
  11. currentScope.add(TypeSymbol.STRING);
  12. currentScope.add(TypeSymbol.BOOL);
  13. currentScope.add(TypeSymbol.IO);
  14.  
  15. for (var stmt: prog.stmts) {
  16. //System.out.println(stmt.token.getText());
  17. stmt.accept(this);
  18. }
  19. return null;
  20. }
  21.  
  22. @Override
  23. public Void visit(ClassDef classDef) {
  24. String err;
  25. if (classDef.inher_type != null) {
  26. var name = classDef.type.token.getText();
  27. var parent = SymbolTable.tree.get(name);
  28.  
  29. if(parent.equals("SELF_TYPE") || parent.equals("Int") ||
  30. parent.equals("String") || parent.equals("IO") ||
  31. parent.equals("Bool")) {
  32. err = "Class " + name + " has illegal parent " + parent;
  33. SymbolTable.error(classDef.inher_type.token, err);
  34. return null;
  35. }
  36. if(!SymbolTable.tree.containsKey(parent)) {
  37. err = "Class " + name + " has undefined parent " + parent;
  38. SymbolTable.error(classDef.inher_type.token, err);
  39. return null;
  40. }
  41. var parentClass = parent;
  42. while(!parentClass.equals("")) {
  43. parentClass = SymbolTable.tree.get(parentClass);
  44. if (parentClass.equals(name)) {
  45. err = "Inheritance cycle for class " + name;
  46. SymbolTable.error(classDef.type.token, err);
  47. return null;
  48. }
  49. }
  50. }
  51.  
  52. var id = classDef.type;
  53. var type = classDef.inher_type;
  54.  
  55. var classSymbol = new ClassSymbol(currentScope, id.token.getText());
  56. currentScope = classSymbol;
  57. classDef.setScope(currentScope);
  58.  
  59. // if (! currentScope.getParent().add(classSymbol)) {
  60. // System.out.println("DIN CLASA\n");
  61. // return null;
  62. // }
  63. //
  64. // id.setSymbol(functionSymbol);
  65. // id.setScope(currentScope);
  66. //
  67. // // Căutăm tipul funcției.
  68. // var typeSymbol = (TypeSymbol)currentScope.lookup(type.getToken().getText());
  69. //
  70. // // Semnalăm eroare dacă nu există.
  71. // if (typeSymbol == null) {
  72. // ASTVisitor.error(type.getToken(),
  73. // id.getToken().getText() + " has undefined return type " +
  74. // type.getToken().getText());
  75. // return null;
  76. // }
  77. //
  78. // // Reținem informația de tip în cadrul simbolului aferent funcției.
  79. // functionSymbol.setType(typeSymbol);
  80.  
  81. //SymbolTable.addClassInValidTree(name, parent);
  82.  
  83. for (var feature: classDef.features) {
  84. feature.accept(this);
  85. }
  86.  
  87. currentScope = currentScope.getParent();
  88. return null;
  89. }
  90.  
  91.  
  92. @Override
  93. public Void visit(Type type) {
  94. return null;
  95. }
  96.  
  97. @Override
  98. public Void visit(Id id) {
  99. return null;
  100. }
  101.  
  102. @Override
  103. public Void visit(Int intt) {
  104. return null;
  105. }
  106.  
  107. @Override
  108. public Void visit(BoolFalse bool) {
  109. return null;
  110. }
  111.  
  112. @Override
  113. public Void visit(BoolTrue bool) {
  114. return null;
  115. }
  116.  
  117. @Override
  118. public Void visit(StringDef str) {
  119. return null;
  120. }
  121.  
  122. @Override
  123. public Void visit(Relational rel) {
  124. return null;
  125. }
  126.  
  127. @Override
  128. public Void visit(EqualExpr eq) {
  129. return null;
  130. }
  131.  
  132. @Override
  133. public Void visit(Assign assign) {
  134. return null;
  135. }
  136.  
  137. @Override
  138. public Void visit(Plus plus) {
  139. return null;
  140. }
  141.  
  142. @Override
  143. public Void visit(Minus minus) {
  144. return null;
  145. }
  146.  
  147. @Override
  148. public Void visit(Mult mult) {
  149. return null;
  150. }
  151.  
  152. @Override
  153. public Void visit(Div div) {
  154. return null;
  155. }
  156.  
  157. @Override
  158. public Void visit(NegExpr neg) {
  159. return null;
  160. }
  161.  
  162. @Override
  163. public Void visit(NewType type) {
  164. return null;
  165. }
  166.  
  167. @Override
  168. public Void visit(IsVoidExpr e) {
  169. return null;
  170. }
  171.  
  172. @Override
  173. public Void visit(NotExpr expr) {
  174. return null;
  175. }
  176.  
  177. @Override
  178. public Void visit(ParenExpr paren) {
  179. return null;
  180. }
  181.  
  182. @Override
  183. public Void visit(AttributeDef attr) {
  184. attr.formal.accept(this);
  185. return null;
  186. }
  187.  
  188. @Override
  189. public Void visit(MethodDef method) {
  190. return null;
  191. }
  192.  
  193. @Override
  194. public Void visit(ExprBlock block) {
  195. return null;
  196. }
  197.  
  198.  
  199. @Override
  200. public Void visit(CaseBranch cb) {
  201. return null;
  202. }
  203.  
  204. @Override
  205. public Void visit(CaseBlock cb) {
  206. return null;
  207. }
  208.  
  209. @Override
  210. public Void visit(LetBlock lb) {
  211. return null;
  212. }
  213.  
  214. @Override
  215. public Void visit(Dispatch d) {
  216. return null;
  217. }
  218.  
  219. @Override
  220. public Void visit(Call call) {
  221. return null;
  222. }
  223.  
  224. @Override
  225. public Void visit(IfBlock iff) {
  226. return null;
  227. }
  228.  
  229. @Override
  230. public Void visit(WhileBlock wb) {
  231. return null;
  232. }
  233.  
  234.  
  235. @Override
  236. public Void visit(Formal formal) {
  237. var id = formal.name;
  238. var type = formal.type;
  239. String error;
  240. String className = (currentScope instanceof ClassSymbol) ? ((ClassSymbol)currentScope).getName() : "";
  241.  
  242. if (id.token.getText().equals("self")) {
  243. error = "Class " + className + " has attribute with illegal name self";
  244. SymbolTable.error(id.token, error);
  245. return null;
  246. }
  247. var symbol = new IdSymbol(id.token.getText());
  248.  
  249. // Verificăm dacă parametrul deja există în scope-ul curent.
  250. if (!currentScope.add(symbol)) {
  251. error = "Class " + className + " redefines attribute " + id.token.getText();
  252. SymbolTable.error(id.token, error);
  253. return null;
  254. }
  255.  
  256. if (SymbolTable.globals.lookup(formal.type.token.getText()) == null) {
  257. error = "Class " + className + " has attribute " + id.token.getText() + " with undefined type " + type.token.getText();
  258. SymbolTable.error(id.token, error);
  259. return null;
  260. }
  261.  
  262. // id.setSymbol(symbol);
  263. // id.setScope(currentScope);
  264. //
  265. // // Căutăm tipul variabilei.
  266. // var typeSymbol = (TypeSymbol)currentScope.lookup(type.getToken().getText());
  267. //
  268. // // Semnalăm eroare dacă nu există.
  269. // if (typeSymbol == null) {
  270. // String err = "EROARE ----- ROARE\n";
  271. // SymbolTable.error(type, err);
  272. // return null;
  273. // }
  274. //
  275. // // Reținem informația de tip în cadrul simbolului aferent
  276. // // variabilei
  277. // symbol.setType(typeSymbol);
  278.  
  279. return null;
  280. }
  281.  
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement