lariciamota

Untitled

Apr 23rd, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. package br.ufpe.cin.if688.visitor;
  2.  
  3. import br.ufpe.cin.if688.ast.AssignStm;
  4. import br.ufpe.cin.if688.ast.CompoundStm;
  5. import br.ufpe.cin.if688.ast.EseqExp;
  6. import br.ufpe.cin.if688.ast.Exp;
  7. import br.ufpe.cin.if688.ast.ExpList;
  8. import br.ufpe.cin.if688.ast.IdExp;
  9. import br.ufpe.cin.if688.ast.LastExpList;
  10. import br.ufpe.cin.if688.ast.NumExp;
  11. import br.ufpe.cin.if688.ast.OpExp;
  12. import br.ufpe.cin.if688.ast.PairExpList;
  13. import br.ufpe.cin.if688.ast.PrintStm;
  14. import br.ufpe.cin.if688.ast.Stm;
  15. import br.ufpe.cin.if688.symboltable.IntAndTable;
  16. import br.ufpe.cin.if688.symboltable.Table;
  17.  
  18. public class IntAndTableVisitor implements IVisitor<IntAndTable> {
  19.     private Table t;
  20.  
  21.     public IntAndTableVisitor(Table t) {
  22.         this.t = t;
  23.     }
  24.  
  25.     @Override
  26.     public IntAndTable visit(Stm s) {
  27.         return s.accept(this);
  28.     }
  29.  
  30.     @Override
  31.     public IntAndTable visit(AssignStm s) {
  32.         this.t.id = s.getId();
  33.         this.t.value = s.getExp().accept(this).result;
  34.         IntAndTable it = new IntAndTable(this.t.value, this.t);
  35.         return it;
  36.     }
  37.  
  38.     @Override
  39.     public IntAndTable visit(CompoundStm s) {
  40.         //?
  41.         return null;
  42.     }
  43.  
  44.     @Override
  45.     public IntAndTable visit(PrintStm s) {
  46.         return s.getExps().accept(this);
  47.     }
  48.  
  49.     @Override
  50.     public IntAndTable visit(Exp e) {
  51.         return e.accept(this);
  52.     }
  53.  
  54.     @Override
  55.     public IntAndTable visit(EseqExp e) {
  56.         Table tstm = e.getStm().accept(new Interpreter(this.t));
  57.         IntAndTable itexp = e.getExp().accept(this);
  58.         IntAndTable it = new IntAndTable(itexp.result, tstm);
  59.         return it;
  60.     }
  61.  
  62.     @Override
  63.     public IntAndTable visit(IdExp e) {
  64.         IntAndTable it = new IntAndTable(0, t);
  65.         if (this.t.id == e.getId()){
  66.             it.result = this.t.value;
  67.         } else {
  68.             it = new IntAndTableVisitor(t.tail).visit(e);
  69.         }
  70.         return it;
  71.     }
  72.  
  73.     @Override
  74.     public IntAndTable visit(NumExp e) {
  75.         IntAndTable it = new IntAndTable(e.getNum(), t);
  76.         return it;
  77.     }
  78.  
  79.     @Override
  80.     public IntAndTable visit(OpExp e) {
  81.         int left = e.getLeft().accept(this).result;
  82.         int right = e.getRight().accept(this).result;
  83.         int op = e.getOper();
  84.         int result = 0;
  85.         switch(op) {
  86.         case 1:
  87.             result = left + right;
  88.             break;
  89.         case 2:
  90.             result = left - right;
  91.             break;
  92.         case 3:
  93.             result = left * right;
  94.             break;
  95.         case 4:
  96.             result = left / right;
  97.             break;
  98.         default: System.out.println("Op invalida");
  99.         }
  100.         IntAndTable it = new IntAndTable(result, this.t);
  101.         return it;
  102.     }
  103.  
  104.     @Override
  105.     public IntAndTable visit(ExpList el) {
  106.         return el.accept(this);
  107.     }
  108.  
  109.     @Override
  110.     public IntAndTable visit(PairExpList el) {
  111.         el.getHead().accept(this);
  112.         el.getTail().accept(this);
  113.         return null;
  114.     }
  115.  
  116.     @Override
  117.     public IntAndTable visit(LastExpList el) {
  118.         return el.getHead().accept(this);
  119.     }
  120.  
  121.  
  122. }
Add Comment
Please, Sign In to add comment