FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

DynamicJava: Introduced an ExpressionStatement node (renamed the mark… · SatGarcia/drjava@6b388fb · GitHub

Repository navigation

Commit 6b388fb

Browse files
dlsmith
committed
DynamicJava: Introduced an ExpressionStatement node (renamed the marker interface with this name previously to StatementExpression); reworked the parser so that blocks never include expressions, but instead wrap them in ExpressionStatements. Modified interpreter to take advantage of this redesign.
git-svn-id: file:///tmp/test-svn/trunk@4386 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent 15e62c4 commit 6b388fb

19 files changed

Lines changed: 412 additions & 274 deletions

‎dynamicjava/src/edu/rice/cs/dynamicjava/interpreter/Interpreter.java‎

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@
1212
import koala.dynamicjava.parser.wrapper.ParseError;
1313
import edu.rice.cs.dynamicjava.Options;
1414

15-
// temporary imports to support hacked assignment handling and void returns
16-
import koala.dynamicjava.tree.SimpleAssignExpression;
17-
import koala.dynamicjava.tree.AmbiguousName;
18-
import koala.dynamicjava.tree.VariableDeclaration;
19-
import koala.dynamicjava.SourceInfo;
20-
import koala.dynamicjava.interpreter.NodeProperties;
21-
import edu.rice.cs.dynamicjava.symbol.TypeSystem;
22-
2315
import static edu.rice.cs.plt.debug.DebugUtil.debug;
2416

2517
/**
@@ -80,27 +72,7 @@ private TypeContext typeCheck(Iterable<Node> tree) throws InterpreterException {
8072
try {
8173
TypeContext newContext = _typeContext;
8274
for (Node n : tree) {
83-
84-
// A hack to allow declarations without types: (TODO: fix this)
85-
if (n instanceof SimpleAssignExpression) {
86-
SimpleAssignExpression assign = (SimpleAssignExpression) n;
87-
if (assign.getLeftExpression() instanceof AmbiguousName) {
88-
AmbiguousName ambigName = (AmbiguousName) assign.getLeftExpression();
89-
if (ambigName.getIdentifiers().size() == 1) {
90-
String name = ambigName.getRepresentation();
91-
if (!newContext.variableExists(name, _opt.typeSystem())) {
92-
SourceInfo si = n.getSourceInfo();
93-
n = new VariableDeclaration(false, null, name, assign.getRightExpression(),
94-
si.getFilename(), si.getStartLine(), si.getStartColumn(),
95-
si.getEndLine(), si.getEndColumn());
96-
assign.setProperty("assignmentAsDeclaration", n);
97-
}
98-
}
99-
}
100-
}
101-
102-
if (n instanceof Expression) { n.acceptVisitor(new ExpressionChecker(newContext, _opt)); }
103-
else { newContext = n.acceptVisitor(new StatementChecker(newContext, _opt)); }
75+
newContext = n.acceptVisitor(new StatementChecker(newContext, _opt));
10476
}
10577
return newContext;
10678
}
@@ -112,22 +84,9 @@ private Pair<RuntimeBindings, Option<Object>> evaluate(Iterable<Node> tree) thro
11284
RuntimeBindings newBindings = _bindings;
11385
Option<Object> val = Option.none();
11486
for (Node n : tree) {
115-
// TODO: eliminate hacks that support inferred assignment and void returns
116-
if (n.hasProperty("assignmentAsDeclaration")) {
117-
n = (Node) n.getProperty("assignmentAsDeclaration");
118-
}
119-
if (n instanceof Expression) {
120-
Object evalResult = new ExpressionEvaluator(newBindings, _opt).value(n);
121-
if (evalResult == null && NodeProperties.getType(n).equals(TypeSystem.VOID)) {
122-
val = Option.none();
123-
}
124-
else { val = Option.some(evalResult); }
125-
}
126-
else {
127-
StatementEvaluator.Result r = n.acceptVisitor(new StatementEvaluator(newBindings, _opt));
128-
newBindings = r.bindings();
129-
val = r.value();
130-
}
87+
StatementEvaluator.Result r = n.acceptVisitor(new StatementEvaluator(newBindings, _opt));
88+
newBindings = r.bindings();
89+
val = r.value();
13190
}
13291
return Pair.make(newBindings, val);
13392
}

‎dynamicjava/src/edu/rice/cs/dynamicjava/interpreter/StatementChecker.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import edu.rice.cs.plt.tuple.Pair;
8282
import edu.rice.cs.plt.lambda.Lambda;
8383

84+
import koala.dynamicjava.SourceInfo;
8485
import koala.dynamicjava.tree.*;
8586
import koala.dynamicjava.tree.tiger.*;
8687
import koala.dynamicjava.tree.visitor.*;
@@ -719,4 +720,27 @@ else if (ts.isAssignable(TypeSystem.EXCEPTION, thrown)) {
719720
return context;
720721
}
721722

723+
@Override public TypeContext visit(ExpressionStatement node) {
724+
if (node.getExpression() instanceof SimpleAssignExpression) {
725+
SimpleAssignExpression assign = (SimpleAssignExpression) node.getExpression();
726+
if (assign.getLeftExpression() instanceof AmbiguousName) {
727+
AmbiguousName ambigName = (AmbiguousName) assign.getLeftExpression();
728+
if (ambigName.getIdentifiers().size() == 1) {
729+
String name = ambigName.getRepresentation();
730+
if (!context.variableExists(name, opt.typeSystem())) {
731+
SourceInfo si = node.getSourceInfo();
732+
Node decl = new VariableDeclaration(false, null, name, assign.getRightExpression(),
733+
si.getFilename(), si.getStartLine(), si.getStartColumn(),
734+
si.getEndLine(), si.getEndColumn());
735+
setStatementTranslation(node, decl);
736+
return decl.acceptVisitor(this);
737+
}
738+
}
739+
}
740+
}
741+
// all other cases that don't match the nested ifs:
742+
checkType(node.getExpression());
743+
return context;
744+
}
745+
722746
}

‎dynamicjava/src/edu/rice/cs/dynamicjava/interpreter/StatementEvaluator.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import edu.rice.cs.dynamicjava.Options;
1717
import edu.rice.cs.dynamicjava.symbol.LocalVariable;
18+
import edu.rice.cs.dynamicjava.symbol.TypeSystem;
1819

1920
import static koala.dynamicjava.interpreter.NodeProperties.*;
2021

@@ -93,6 +94,19 @@ private Result evaluateSequence(Iterable<? extends Node> nodes) {
9394
* * * * * * * * * */
9495

9596
@Override public Result visit(EmptyStatement node) { return new Result(_bindings); }
97+
98+
@Override public Result visit(ExpressionStatement node) {
99+
if (hasStatementTranslation(node)) {
100+
return getStatementTranslation(node).acceptVisitor(this);
101+
}
102+
else {
103+
Object val = new ExpressionEvaluator(_bindings, _opt).value(node.getExpression());
104+
if (node.getHasSemicolon() || getType(node.getExpression()).equals(TypeSystem.VOID)) {
105+
return new Result(_bindings);
106+
}
107+
else { return new Result(val, _bindings); }
108+
}
109+
}
96110

97111
@Override public Result visit(WhileStatement node) {
98112
ExpressionEvaluator eval = new ExpressionEvaluator(_bindings, _opt);

‎dynamicjava/src/koala/dynamicjava/interpreter/NodeProperties.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,24 @@ public static boolean hasTranslation(Node n) {
196196
return n.hasProperty(TRANSLATION);
197197
}
198198

199+
/**
200+
* A Node representing the translated equivalent of the tagged statement (or declaration)
201+
*/
202+
public final static String STATEMENT_TRANSLATION = "statementTranslation";
203+
204+
public static Node getStatementTranslation(Node n) {
205+
return (Node) n.getProperty(STATEMENT_TRANSLATION);
206+
}
207+
208+
public static Node setStatementTranslation(Node n, Node s) {
209+
n.setProperty(STATEMENT_TRANSLATION, s);
210+
return s;
211+
}
212+
213+
public static boolean hasStatementTranslation(Node n) {
214+
return n.hasProperty(STATEMENT_TRANSLATION);
215+
}
216+
199217

200218
/** An Object value of a constant expression */
201219
public final static String VALUE = "value";

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL