| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 15e62c4 commit 6b388fb
19 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,14 +12,6 @@ | |||
| 12 | 12 | import koala.dynamicjava.parser.wrapper.ParseError; | |
| 13 | 13 | import edu.rice.cs.dynamicjava.Options; | |
| 14 | 14 | ||
| 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 | - | ||
| 23 | 15 | import static edu.rice.cs.plt.debug.DebugUtil.debug; | |
| 24 | 16 | ||
| 25 | 17 | /** | |
@@ -80,27 +72,7 @@ private TypeContext typeCheck(Iterable<Node> tree) throws InterpreterException { | |||
| 80 | 72 | try { | |
| 81 | 73 | TypeContext newContext = _typeContext; | |
| 82 | 74 | 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)); | ||
| 104 | 76 | } | |
| 105 | 77 | return newContext; | |
| 106 | 78 | } | |
@@ -112,22 +84,9 @@ private Pair<RuntimeBindings, Option<Object>> evaluate(Iterable<Node> tree) thro | |||
| 112 | 84 | RuntimeBindings newBindings = _bindings; | |
| 113 | 85 | Option<Object> val = Option.none(); | |
| 114 | 86 | 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(); | ||
| 131 | 90 | } | |
| 132 | 91 | return Pair.make(newBindings, val); | |
| 133 | 92 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -81,6 +81,7 @@ | |||
| 81 | 81 | import edu.rice.cs.plt.tuple.Pair; | |
| 82 | 82 | import edu.rice.cs.plt.lambda.Lambda; | |
| 83 | 83 | ||
| 84 | + import koala.dynamicjava.SourceInfo; | ||
| 84 | 85 | import koala.dynamicjava.tree.*; | |
| 85 | 86 | import koala.dynamicjava.tree.tiger.*; | |
| 86 | 87 | import koala.dynamicjava.tree.visitor.*; | |
@@ -719,4 +720,27 @@ else if (ts.isAssignable(TypeSystem.EXCEPTION, thrown)) { | |||
| 719 | 720 | return context; | |
| 720 | 721 | } | |
| 721 | 722 | ||
| 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 | + | ||
| 722 | 746 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,7 @@ | |||
| 15 | 15 | ||
| 16 | 16 | import edu.rice.cs.dynamicjava.Options; | |
| 17 | 17 | import edu.rice.cs.dynamicjava.symbol.LocalVariable; | |
| 18 | + import edu.rice.cs.dynamicjava.symbol.TypeSystem; | ||
| 18 | 19 | ||
| 19 | 20 | import static koala.dynamicjava.interpreter.NodeProperties.*; | |
| 20 | 21 | ||
@@ -93,6 +94,19 @@ private Result evaluateSequence(Iterable<? extends Node> nodes) { | |||
| 93 | 94 | * * * * * * * * * */ | |
| 94 | 95 | ||
| 95 | 96 | @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 | + } | ||
| 96 | 110 | ||
| 97 | 111 | @Override public Result visit(WhileStatement node) { | |
| 98 | 112 | ExpressionEvaluator eval = new ExpressionEvaluator(_bindings, _opt); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -196,6 +196,24 @@ public static boolean hasTranslation(Node n) { | |||
| 196 | 196 | return n.hasProperty(TRANSLATION); | |
| 197 | 197 | } | |
| 198 | 198 | ||
| 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 | + | ||
| 199 | 217 | ||
| 200 | 218 | /** An Object value of a constant expression */ | |
| 201 | 219 | public final static String VALUE = "value"; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments