Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cool.compiler;
- import cool.structures.*;
- public class SecondPassVisitor implements ASTVisitor<Void> {
- Scope currentScope = null;
- @Override
- public Void visit(Program prog) {
- currentScope = new DefaultScope(null);
- currentScope.add(TypeSymbol.INT);
- currentScope.add(TypeSymbol.STRING);
- currentScope.add(TypeSymbol.BOOL);
- currentScope.add(TypeSymbol.IO);
- for (var stmt: prog.stmts) {
- //System.out.println(stmt.token.getText());
- stmt.accept(this);
- }
- return null;
- }
- @Override
- public Void visit(ClassDef classDef) {
- String err;
- if (classDef.inher_type != null) {
- var name = classDef.type.token.getText();
- var parent = SymbolTable.tree.get(name);
- if(parent.equals("SELF_TYPE") || parent.equals("Int") ||
- parent.equals("String") || parent.equals("IO") ||
- parent.equals("Bool")) {
- err = "Class " + name + " has illegal parent " + parent;
- SymbolTable.error(classDef.inher_type.token, err);
- return null;
- }
- if(!SymbolTable.tree.containsKey(parent)) {
- err = "Class " + name + " has undefined parent " + parent;
- SymbolTable.error(classDef.inher_type.token, err);
- return null;
- }
- var parentClass = parent;
- while(!parentClass.equals("")) {
- parentClass = SymbolTable.tree.get(parentClass);
- if (parentClass.equals(name)) {
- err = "Inheritance cycle for class " + name;
- SymbolTable.error(classDef.type.token, err);
- return null;
- }
- }
- }
- var id = classDef.type;
- var type = classDef.inher_type;
- var classSymbol = new ClassSymbol(currentScope, id.token.getText());
- currentScope = classSymbol;
- classDef.setScope(currentScope);
- // if (! currentScope.getParent().add(classSymbol)) {
- // System.out.println("DIN CLASA\n");
- // return null;
- // }
- //
- // id.setSymbol(functionSymbol);
- // id.setScope(currentScope);
- //
- // // Căutăm tipul funcției.
- // var typeSymbol = (TypeSymbol)currentScope.lookup(type.getToken().getText());
- //
- // // Semnalăm eroare dacă nu există.
- // if (typeSymbol == null) {
- // ASTVisitor.error(type.getToken(),
- // id.getToken().getText() + " has undefined return type " +
- // type.getToken().getText());
- // return null;
- // }
- //
- // // Reținem informația de tip în cadrul simbolului aferent funcției.
- // functionSymbol.setType(typeSymbol);
- //SymbolTable.addClassInValidTree(name, parent);
- for (var feature: classDef.features) {
- feature.accept(this);
- }
- currentScope = currentScope.getParent();
- return null;
- }
- @Override
- public Void visit(Type type) {
- return null;
- }
- @Override
- public Void visit(Id id) {
- return null;
- }
- @Override
- public Void visit(Int intt) {
- return null;
- }
- @Override
- public Void visit(BoolFalse bool) {
- return null;
- }
- @Override
- public Void visit(BoolTrue bool) {
- return null;
- }
- @Override
- public Void visit(StringDef str) {
- return null;
- }
- @Override
- public Void visit(Relational rel) {
- return null;
- }
- @Override
- public Void visit(EqualExpr eq) {
- return null;
- }
- @Override
- public Void visit(Assign assign) {
- return null;
- }
- @Override
- public Void visit(Plus plus) {
- return null;
- }
- @Override
- public Void visit(Minus minus) {
- return null;
- }
- @Override
- public Void visit(Mult mult) {
- return null;
- }
- @Override
- public Void visit(Div div) {
- return null;
- }
- @Override
- public Void visit(NegExpr neg) {
- return null;
- }
- @Override
- public Void visit(NewType type) {
- return null;
- }
- @Override
- public Void visit(IsVoidExpr e) {
- return null;
- }
- @Override
- public Void visit(NotExpr expr) {
- return null;
- }
- @Override
- public Void visit(ParenExpr paren) {
- return null;
- }
- @Override
- public Void visit(AttributeDef attr) {
- attr.formal.accept(this);
- return null;
- }
- @Override
- public Void visit(MethodDef method) {
- return null;
- }
- @Override
- public Void visit(ExprBlock block) {
- return null;
- }
- @Override
- public Void visit(CaseBranch cb) {
- return null;
- }
- @Override
- public Void visit(CaseBlock cb) {
- return null;
- }
- @Override
- public Void visit(LetBlock lb) {
- return null;
- }
- @Override
- public Void visit(Dispatch d) {
- return null;
- }
- @Override
- public Void visit(Call call) {
- return null;
- }
- @Override
- public Void visit(IfBlock iff) {
- return null;
- }
- @Override
- public Void visit(WhileBlock wb) {
- return null;
- }
- @Override
- public Void visit(Formal formal) {
- var id = formal.name;
- var type = formal.type;
- String error;
- String className = (currentScope instanceof ClassSymbol) ? ((ClassSymbol)currentScope).getName() : "";
- if (id.token.getText().equals("self")) {
- error = "Class " + className + " has attribute with illegal name self";
- SymbolTable.error(id.token, error);
- return null;
- }
- var symbol = new IdSymbol(id.token.getText());
- // Verificăm dacă parametrul deja există în scope-ul curent.
- if (!currentScope.add(symbol)) {
- error = "Class " + className + " redefines attribute " + id.token.getText();
- SymbolTable.error(id.token, error);
- return null;
- }
- if (SymbolTable.globals.lookup(formal.type.token.getText()) == null) {
- error = "Class " + className + " has attribute " + id.token.getText() + " with undefined type " + type.token.getText();
- SymbolTable.error(id.token, error);
- return null;
- }
- // id.setSymbol(symbol);
- // id.setScope(currentScope);
- //
- // // Căutăm tipul variabilei.
- // var typeSymbol = (TypeSymbol)currentScope.lookup(type.getToken().getText());
- //
- // // Semnalăm eroare dacă nu există.
- // if (typeSymbol == null) {
- // String err = "EROARE ----- ROARE\n";
- // SymbolTable.error(type, err);
- // return null;
- // }
- //
- // // Reținem informația de tip în cadrul simbolului aferent
- // // variabilei
- // symbol.setType(typeSymbol);
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement