Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. package br.ufpe.cin.if688.minijava.visitor;
  2.  
  3. import br.ufpe.cin.if688.minijava.ast.*;
  4. import br.ufpe.cin.if688.minijava.exceptions.PrintException;
  5. import br.ufpe.cin.if688.minijava.symboltable.Class;
  6. import br.ufpe.cin.if688.minijava.symboltable.Method;
  7. import br.ufpe.cin.if688.minijava.symboltable.SymbolTable;
  8.  
  9. public class BuildSymbolTableVisitor implements IVisitor<Void> {
  10.  
  11.     SymbolTable symbolTable;
  12.  
  13.     public BuildSymbolTableVisitor() {
  14.         symbolTable = new SymbolTable();
  15.     }
  16.  
  17.     public SymbolTable getSymbolTable() {
  18.         return symbolTable;
  19.     }
  20.  
  21.     private Class currClass;
  22.     private Method currMethod;
  23.     private boolean fromMethod = false;
  24.  
  25.     // MainClass m;
  26.     // ClassDeclList cl;
  27.     public Void visit(Program n) {
  28.         n.m.accept(this);
  29.         for (int i = 0; i < n.cl.size(); i++) {
  30.             n.cl.elementAt(i).accept(this);
  31.         }
  32.         return null;
  33.     }
  34.  
  35.     // Identifier i1,i2;
  36.     // Statement s;
  37.     public Void visit(MainClass n) {
  38.         this.symbolTable.addClass(n.i1.toString(), null);
  39.         this.currClass = this.symbolTable.getClass(n.i1.toString());
  40.         this.currClass.addMethod("main", null);
  41.         Type t = new IntegerType();
  42.         this.currClass.getMethod("main").addParam(n.i2.toString(), t);
  43.         n.i1.accept(this);
  44.         n.i2.accept(this);
  45.         n.s.accept(this);
  46.         return null;
  47.     }
  48.  
  49.     // Identifier i;
  50.     // VarDeclList vl;
  51.     // MethodDeclList ml;
  52.     public Void visit(ClassDeclSimple n) {
  53.         if(this.symbolTable.addClass(n.i.toString(), null)){
  54.             this.currClass = this.symbolTable.getClass(n.i.toString());
  55.             n.i.accept(this);
  56.             for (int i = 0; i < n.vl.size(); i++) {
  57.                 n.vl.elementAt(i).accept(this);
  58.             }
  59.             for (int i = 0; i < n.ml.size(); i++) {
  60.                 n.ml.elementAt(i).accept(this);
  61.             }
  62.         }else{
  63.             PrintException.duplicateClass(n.i.s);
  64.         }
  65.         return null;
  66.     }
  67.  
  68.     // Identifier i;
  69.     // Identifier j;
  70.     // VarDeclList vl;
  71.     // MethodDeclList ml;
  72.     public Void visit(ClassDeclExtends n) {
  73.         if(this.symbolTable.addClass(n.i.toString(), n.j.toString())){
  74.             this.currClass = this.symbolTable.getClass(n.i.toString());
  75.             n.i.accept(this);
  76.             n.j.accept(this);
  77.             for (int i = 0; i < n.vl.size(); i++) {
  78.                 n.vl.elementAt(i).accept(this);
  79.             }
  80.             for (int i = 0; i < n.ml.size(); i++) {
  81.                 n.ml.elementAt(i).accept(this);
  82.             }
  83.         }else{
  84.             PrintException.duplicateClass(n.i.s);
  85.         }
  86.         return null;
  87.     }
  88.  
  89.     // Type t;
  90.     // Identifier i;
  91.     public Void visit(VarDecl n) {
  92.         n.t.accept(this);
  93.         n.i.accept(this);
  94.         if(this.fromMethod){
  95.             if(!this.currMethod.addVar(n.i.toString(), n.t)){
  96.                 PrintException.duplicateVariable(n.i.s);
  97.                 System.out.println("Variable " + n.i.toString() + " is already defined in method " + this.currMethod.getId()+".");
  98.                 System.exit(0);
  99.             }
  100.         }else if(!this.currClass.addVar(n.i.toString(), n.t)){
  101.             PrintException.duplicateVariable(n.i.s);
  102.             System.out.println("Variable " + n.i.toString() + " is already defined in class " + this.currClass.getId()+".");
  103.             System.exit(0);
  104.         }
  105.         return null;
  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.         if(this.currClass == null){
  116.             PrintException.methodDeclarationOutsideOfClass(n.i.s);
  117.         }
  118.         this.fromMethod = true;
  119.         if(this.currClass.addMethod(n.i.toString(), n.t)){
  120.             this.currMethod = this.currClass.getMethod(n.i.toString());
  121.             n.t.accept(this);
  122.             n.i.accept(this);
  123.             for (int i = 0; i < n.fl.size(); i++) {
  124.                 n.fl.elementAt(i).accept(this);
  125.             }
  126.             for (int i = 0; i < n.vl.size(); i++) {
  127.                 n.vl.elementAt(i).accept(this);
  128.             }
  129.             for (int i = 0; i < n.sl.size(); i++) {
  130.                 n.sl.elementAt(i).accept(this);
  131.             }
  132.             n.e.accept(this);
  133.         }else{
  134.             PrintException.duplicateMethod(n.i.s);
  135.             System.out.println("Method " + n.i.toString() + " is already defined in class " + this.currClass.getId() + ".");
  136.             System.exit(0);
  137.         }
  138.         this.fromMethod = false;
  139.         return null;
  140.     }
  141.  
  142.     // Type t;
  143.     // Identifier i;
  144.     public Void visit(Formal n) {
  145.         n.t.accept(this);
  146.         n.i.accept(this);
  147.         if(!this.currMethod.addParam(n.i.toString(),n.t)) {
  148.             PrintException.duplicateParameter(n.i.s);
  149.             System.out.println("Variable " + n.i.toString() + " is already defined in method " + this.currMethod.getId()+".");
  150.             System.exit(0);
  151.         }
  152.         return null;
  153.     }
  154.  
  155.     public Void visit(IntArrayType n) {
  156.         return null;
  157.     }
  158.  
  159.     public Void visit(BooleanType n) {
  160.         return null;
  161.     }
  162.  
  163.     public Void visit(IntegerType n) {
  164.         return null;
  165.     }
  166.  
  167.     // String s;
  168.     public Void visit(IdentifierType n) {
  169.         return null;
  170.     }
  171.  
  172.     // StatementList sl;
  173.     public Void visit(Block n) {
  174.         for (int i = 0; i < n.sl.size(); i++) {
  175.             n.sl.elementAt(i).accept(this);
  176.         }
  177.         return null;
  178.     }
  179.  
  180.     // Exp e;
  181.     // Statement s1,s2;
  182.     public Void visit(If n) {
  183.         n.e.accept(this);
  184.         n.s1.accept(this);
  185.         n.s2.accept(this);
  186.         return null;
  187.     }
  188.  
  189.     // Exp e;
  190.     // Statement s;
  191.     public Void visit(While n) {
  192.         n.e.accept(this);
  193.         n.s.accept(this);
  194.         return null;
  195.     }
  196.  
  197.     // Exp e;
  198.     public Void visit(Print n) {
  199.         n.e.accept(this);
  200.         return null;
  201.     }
  202.  
  203.     // Identifier i;
  204.     // Exp e;
  205.     public Void visit(Assign n) {
  206.         n.i.accept(this);
  207.         n.e.accept(this);
  208.         return null;
  209.     }
  210.  
  211.     // Identifier i;
  212.     // Exp e1,e2;
  213.     public Void visit(ArrayAssign n) {
  214.         n.i.accept(this);
  215.         n.e1.accept(this);
  216.         n.e2.accept(this);
  217.         return null;
  218.     }
  219.  
  220.     // Exp e1,e2;
  221.     public Void visit(And n) {
  222.         n.e1.accept(this);
  223.         n.e2.accept(this);
  224.         return null;
  225.     }
  226.  
  227.     // Exp e1,e2;
  228.     public Void visit(LessThan n) {
  229.         n.e1.accept(this);
  230.         n.e2.accept(this);
  231.         return null;
  232.     }
  233.  
  234.     // Exp e1,e2;
  235.     public Void visit(Plus n) {
  236.         n.e1.accept(this);
  237.         n.e2.accept(this);
  238.         return null;
  239.     }
  240.  
  241.     // Exp e1,e2;
  242.     public Void visit(Minus n) {
  243.         n.e1.accept(this);
  244.         n.e2.accept(this);
  245.         return null;
  246.     }
  247.  
  248.     // Exp e1,e2;
  249.     public Void visit(Times n) {
  250.         n.e1.accept(this);
  251.         n.e2.accept(this);
  252.         return null;
  253.     }
  254.  
  255.     // Exp e1,e2;
  256.     public Void visit(ArrayLookup n) {
  257.         n.e1.accept(this);
  258.         n.e2.accept(this);
  259.         return null;
  260.     }
  261.  
  262.     // Exp e;
  263.     public Void visit(ArrayLength n) {
  264.         n.e.accept(this);
  265.         return null;
  266.     }
  267.  
  268.     // Exp e;
  269.     // Identifier i;
  270.     // ExpList el;
  271.     public Void visit(Call n) {
  272.         n.e.accept(this);
  273.         n.i.accept(this);
  274.         for (int i = 0; i < n.el.size(); i++) {
  275.             n.el.elementAt(i).accept(this);
  276.         }
  277.         return null;
  278.     }
  279.  
  280.     // int i;
  281.     public Void visit(IntegerLiteral n) {
  282.         return null;
  283.     }
  284.  
  285.     public Void visit(True n) {
  286.         return null;
  287.     }
  288.  
  289.     public Void visit(False n) {
  290.         return null;
  291.     }
  292.  
  293.     // String s;
  294.     public Void visit(IdentifierExp n) {
  295.         return null;
  296.     }
  297.  
  298.     public Void visit(This n) {
  299.         return null;
  300.     }
  301.  
  302.     // Exp e;
  303.     public Void visit(NewArray n) {
  304.         n.e.accept(this);
  305.         return null;
  306.     }
  307.  
  308.     // Identifier i;
  309.     public Void visit(NewObject n) {
  310.         return null;
  311.     }
  312.  
  313.     // Exp e;
  314.     public Void visit(Not n) {
  315.         n.e.accept(this);
  316.         return null;
  317.     }
  318.  
  319.     // String s;
  320.     public Void visit(Identifier n) {
  321.         return null;
  322.     }
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement