|
| 1 | +package InterpreterLTY.interpreter; |
| 2 | + |
| 3 | +import InterpreterLTY.interpreter.parser.*; |
| 4 | +import InterpreterLTY.interpreter.util.TokenTypes; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +/** |
| 11 | + * Created by lugty on 4/19/17. |
| 12 | + */ |
| 13 | +public class Interpreter extends NodeVisitor { |
| 14 | + private HashMap<String, Double> GLOBAL_SCOPE = new HashMap<String, Double>(); |
| 15 | + private AST tree; |
| 16 | + |
| 17 | + public Interpreter(AST tree)throws Exception{ |
| 18 | + this.tree = tree; |
| 19 | + } |
| 20 | + |
| 21 | + public Interpreter(AST tree, HashMap<String, Double> varTable)throws Exception{ |
| 22 | + this.tree = tree; |
| 23 | + this.GLOBAL_SCOPE = varTable; |
| 24 | + } |
| 25 | + |
| 26 | + public Double visitBinOp(AST nodeAST){ |
| 27 | + BinOp node = (BinOp)nodeAST; |
| 28 | + |
| 29 | + if (node.getOp().getType() == TokenTypes.PLUS){ |
| 30 | + Double dd = (Double)this.visit(node.getLeft()) + (Double)this.visit(node.getRight()); |
| 31 | + return dd; |
| 32 | + } else if (node.getOp().getType() == TokenTypes.MINUS) |
| 33 | + return (Double)this.visit(node.getLeft()) - (Double)this.visit(node.getRight()); |
| 34 | + else if (node.getOp().getType() == TokenTypes.MUL) |
| 35 | + return (Double)this.visit(node.getLeft()) * (Double)this.visit(node.getRight()); |
| 36 | + else if (node.getOp().getType() == TokenTypes.DIV) |
| 37 | + return (Double)this.visit(node.getLeft()) / (Double)this.visit(node.getRight()); |
| 38 | + return 0.0; |
| 39 | + } |
| 40 | + |
| 41 | + public Double visitNum(AST nodeAST){ |
| 42 | + Num node = (Num)nodeAST; |
| 43 | + Object valueObj = node.getValue(); |
| 44 | + if(valueObj instanceof Integer){ |
| 45 | + Integer vInt = (Integer)valueObj; |
| 46 | + return vInt.doubleValue(); |
| 47 | + }else if(valueObj instanceof Double){ |
| 48 | + Double vDouble = (Double) valueObj; |
| 49 | + return vDouble; |
| 50 | + } |
| 51 | + |
| 52 | + return (Double) node.getValue(); |
| 53 | + } |
| 54 | + |
| 55 | + public Double visitUnaryOp(AST node){ |
| 56 | + UnaryOp unaryOp = (UnaryOp) node; |
| 57 | + TokenTypes typeOp = unaryOp.getOp().getType(); |
| 58 | + if(typeOp == TokenTypes.PLUS){ |
| 59 | + return +(Double)this.visit(unaryOp.getExpr()); |
| 60 | + }else if(typeOp == TokenTypes.MINUS){ |
| 61 | + return -(Double)this.visit(unaryOp.getExpr()); |
| 62 | + } |
| 63 | + return 0.0; |
| 64 | + } |
| 65 | + |
| 66 | + public List<Object> visitCompound(AST nodesAST){ |
| 67 | + Compound nodes = (Compound) nodesAST; |
| 68 | + List<Object> responses = new ArrayList(); |
| 69 | + |
| 70 | + for(AST node: nodes.getChildren()){ |
| 71 | + Object res = this.visit(node); |
| 72 | + responses.add(res); |
| 73 | + } |
| 74 | + return responses; |
| 75 | + } |
| 76 | + |
| 77 | + public void visitAssign(AST node){ |
| 78 | + Assign nodeVar = (Assign) node; |
| 79 | + |
| 80 | + String varName = nodeVar.getLeft().getValue(); |
| 81 | + Object value = this.visit(nodeVar.getRight()); |
| 82 | + this.GLOBAL_SCOPE.put(varName, (Double)value); |
| 83 | + } |
| 84 | + |
| 85 | + public Object visitVar(AST node) throws Exception{ |
| 86 | + Var varNode = (Var) node; |
| 87 | + String varName = varNode.getValue(); |
| 88 | + Object nodeVarFound = this.GLOBAL_SCOPE.get(varName); |
| 89 | + if(nodeVarFound == null) |
| 90 | + throw new Exception(); |
| 91 | + else |
| 92 | + return nodeVarFound; |
| 93 | + } |
| 94 | + |
| 95 | + public Object visitIfStatement(AST node) throws Exception{ |
| 96 | + IfStatement ifNode = (IfStatement) node; |
| 97 | + Boolean status = (Boolean) this.visit(ifNode.getCondition()); |
| 98 | + if(status){ |
| 99 | + Object rest = this.visit(ifNode.getTrueStatement()); |
| 100 | + return rest; |
| 101 | + }else{ |
| 102 | + Object rest = this.visit(ifNode.getFalseStatement()); |
| 103 | + return rest; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public Boolean visitConditionalOp(AST node) throws Exception{ |
| 108 | + ConditionalOp condNode = (ConditionalOp) node; |
| 109 | + Double valLeft = (Double) this.visit(condNode.getLeft()); |
| 110 | + Double valRight = (Double) this.visit(condNode.getRight()); |
| 111 | + |
| 112 | + if(condNode.getOp().getType() ==TokenTypes.EQUAL){ |
| 113 | + if(valLeft != null && valRight != null){ |
| 114 | + if(valLeft.doubleValue() == valRight.doubleValue()) return true; else return false; |
| 115 | + }else{ |
| 116 | + if(valLeft == valRight) return true; else return false; |
| 117 | + } |
| 118 | + }else if(condNode.getOp().getType() ==TokenTypes.NOTEQUAL){ |
| 119 | + if(valLeft != valRight) return true; else return false; |
| 120 | + }else if(condNode.getOp().getType() ==TokenTypes.GREATER){ |
| 121 | + if(valLeft > valRight) return true; else return false; |
| 122 | + }else if(condNode.getOp().getType() ==TokenTypes.GREATEREQ){ |
| 123 | + if(valLeft >= valRight) return true; else return false; |
| 124 | + }else if(condNode.getOp().getType() ==TokenTypes.LESS){ |
| 125 | + if(valLeft < valRight) return true; else return false; |
| 126 | + }else if(condNode.getOp().getType() ==TokenTypes.LESSEQ){ |
| 127 | + if(valLeft <= valRight) return true; else return false; |
| 128 | + } |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + public void visitNoOp(AST node){} |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + public Object interprete() throws Exception{ |
| 137 | + if(this.tree == null) |
| 138 | + return ""; |
| 139 | + return this.visit(tree); |
| 140 | + |
| 141 | + } |
| 142 | +} |
0 commit comments