Skip to content

Commit 4bca6d3

Browse files
authored
first commit
First project commit
1 parent b72ad0f commit 4bca6d3

19 files changed

Lines changed: 1436 additions & 0 deletions

File tree

pom.xml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>siadem.com</groupId>
8+
<artifactId>InterpreterLTY</artifactId>
9+
<version>1.5-SNAPSHOT</version>
10+
11+
<properties>
12+
<junit.version>4.12</junit.version>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>${junit.version}</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
24+
<build>
25+
<plugins>
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-compiler-plugin</artifactId>
29+
<version>3.2</version>
30+
</plugin>
31+
<plugin>
32+
<groupId>org.jfrog.buildinfo</groupId>
33+
<artifactId>artifactory-maven-plugin</artifactId>
34+
<version>2.6.1</version>
35+
<inherited>false</inherited>
36+
<executions>
37+
<execution>
38+
<id>build-info</id>
39+
<goals>
40+
<goal>publish</goal>
41+
</goals>
42+
<configuration>
43+
<deployProperties>
44+
<gradle>awesome</gradle>
45+
<review.team>qa</review.team>
46+
</deployProperties>
47+
<publisher>
48+
<contextUrl>http://192.168.1.120:8081/artifactory</contextUrl>
49+
<username>lugty</username>
50+
<password>centinela77</password>
51+
<repoKey>libs-release-local</repoKey>
52+
<snapshotRepoKey>libs-snapshot-local</snapshotRepoKey>
53+
</publisher>
54+
</configuration>
55+
</execution>
56+
</executions>
57+
</plugin>
58+
59+
</plugins>
60+
</build>
61+
62+
</project>
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package InterpreterLTY.interpreter;
2+
3+
import InterpreterLTY.interpreter.parser.AST;
4+
5+
import java.lang.reflect.InvocationTargetException;
6+
import java.lang.reflect.Method;
7+
8+
/**
9+
* Created by lugty on 4/20/17.
10+
*/
11+
public class NodeVisitor {
12+
public Object visit(AST node){
13+
try {
14+
String name = "visit"+node.getClass().getSimpleName();
15+
Class ctl = this.getClass();
16+
Method method = this.getClass().getMethod(name, AST.class);
17+
Object rst = method.invoke(this, node);
18+
19+
return rst;
20+
} catch (NoSuchMethodException e) {
21+
e.printStackTrace();
22+
} catch (IllegalAccessException e) {
23+
e.printStackTrace();
24+
} catch (InvocationTargetException e) {
25+
e.printStackTrace();
26+
}
27+
return null;
28+
}
29+
30+
public void genericVisit(AST node) throws Exception{
31+
throw new Exception("No visit" + node.getClass().getName() + "method");
32+
}
33+
}

0 commit comments

Comments
 (0)