Parametrize over the additional agrument type for StatementVisitor1 and ExpressionVisitor1

BUG=
R=dmitryas@google.com

Review-Url: https://codereview.chromium.org/2835453002 .
This commit is contained in:
Zhivka Gucevska
2017-04-20 18:03:13 +02:00
parent 324dd07904
commit b00a85231e
2 changed files with 115 additions and 104 deletions
+44 -35
View File
@@ -75,48 +75,50 @@ class Environment {
}
/// Evaluate expressions.
class Evaluator extends ExpressionVisitor1<Configuration> {
class Evaluator extends ExpressionVisitor1<Configuration, ExpressionState> {
Configuration eval(Expression expr, ExpressionState state) =>
expr.accept1(this, state);
Configuration defaultExpression(Expression node, state) {
Configuration defaultExpression(Expression node, ExpressionState state) {
throw new NotImplemented('Evaluation for expressions of type '
'${node.runtimeType} is not implemented.');
}
Configuration visitInvalidExpression1(InvalidExpression node, state) {
Configuration visitInvalidExpression1(
InvalidExpression node, ExpressionState state) {
throw 'Invalid expression at ${node.location.toString()}';
}
Configuration visitVariableGet(VariableGet node, state) {
Configuration visitVariableGet(VariableGet node, ExpressionState state) {
Value value = state.environment.lookup(node.variable);
return new ContinuationConfiguration(state.continuation, value);
}
Configuration visitVariableSet(VariableSet node, state) {
Configuration visitVariableSet(VariableSet node, ExpressionState state) {
var cont = new VariableSetContinuation(state, node.variable);
return new ExpressionConfiguration(
node.value, state.withContinuation(cont));
}
Configuration visitPropertyGet(PropertyGet node, state) {
Configuration visitPropertyGet(PropertyGet node, ExpressionState state) {
var cont = new PropertyGetContinuation(node.name, state);
return new ExpressionConfiguration(
node.receiver, state.withContinuation(cont));
}
Configuration visitPropertySet(PropertySet node, state) {
Configuration visitPropertySet(PropertySet node, ExpressionState state) {
var cont = new PropertySetContinuation(node.value, node.name, state);
return new ExpressionConfiguration(
node.receiver, state.withContinuation(cont));
}
Configuration visitStaticGet(StaticGet node, state) =>
Configuration visitStaticGet(StaticGet node, ExpressionState state) =>
defaultExpression(node, state);
Configuration visitStaticSet(StaticSet node, state) =>
Configuration visitStaticSet(StaticSet node, ExpressionState state) =>
defaultExpression(node, state);
Configuration visitStaticInvocation(StaticInvocation node, state) {
Configuration visitStaticInvocation(
StaticInvocation node, ExpressionState state) {
if ('print' == node.name.toString()) {
return new ExpressionConfiguration(node.arguments.positional.first,
state.withContinuation(new PrintContinuation(state)));
@@ -135,7 +137,8 @@ class Evaluator extends ExpressionVisitor1<Configuration> {
}
}
Configuration visitMethodInvocation(MethodInvocation node, state) {
Configuration visitMethodInvocation(
MethodInvocation node, ExpressionState state) {
// Currently supports only method invocation with <2 arguments and is used
// to evaluate implemented operators for int, double and String values.
var cont =
@@ -145,7 +148,8 @@ class Evaluator extends ExpressionVisitor1<Configuration> {
node.receiver, state.withContinuation(cont));
}
Configuration visitConstructorInvocation(ConstructorInvocation node, state) {
Configuration visitConstructorInvocation(
ConstructorInvocation node, ExpressionState state) {
Class class_ = new Class(node.target.enclosingClass.reference);
// Currently we don't support initializers.
@@ -158,12 +162,13 @@ class Evaluator extends ExpressionVisitor1<Configuration> {
state.continuation, new ObjectValue(class_, fields));
}
Configuration visitNot(Not node, state) {
Configuration visitNot(Not node, ExpressionState state) {
return new ExpressionConfiguration(
node.operand, state.withContinuation(new NotContinuation(state)));
}
Configuration visitLogicalExpression(LogicalExpression node, state) {
Configuration visitLogicalExpression(
LogicalExpression node, ExpressionState state) {
if ('||' == node.operator) {
var cont = new OrContinuation(node.right, state);
return new ExpressionConfiguration(
@@ -176,45 +181,47 @@ class Evaluator extends ExpressionVisitor1<Configuration> {
}
}
Configuration visitConditionalExpression(ConditionalExpression node, state) {
Configuration visitConditionalExpression(
ConditionalExpression node, ExpressionState state) {
var cont = new ConditionalContinuation(node.then, node.otherwise, state);
return new ExpressionConfiguration(
node.condition, state.withContinuation(cont));
}
Configuration visitStringConcatenation(StringConcatenation node, state) {
Configuration visitStringConcatenation(
StringConcatenation node, ExpressionState state) {
var cont = new StringConcatenationContinuation(node.expressions, state);
return new ExpressionConfiguration(
node.expressions.first, state.withContinuation(cont));
}
// Evaluation of BasicLiterals.
Configuration visitStringLiteral(StringLiteral node, state) {
Configuration visitStringLiteral(StringLiteral node, ExpressionState state) {
return new ContinuationConfiguration(
state.continuation, new StringValue(node.value));
}
Configuration visitIntLiteral(IntLiteral node, state) {
Configuration visitIntLiteral(IntLiteral node, ExpressionState state) {
return new ContinuationConfiguration(
state.continuation, new IntValue(node.value));
}
Configuration visitDoubleLiteral(DoubleLiteral node, state) {
Configuration visitDoubleLiteral(DoubleLiteral node, ExpressionState state) {
return new ContinuationConfiguration(
state.continuation, new DoubleValue(node.value));
}
Configuration visitBoolLiteral(BoolLiteral node, state) {
Configuration visitBoolLiteral(BoolLiteral node, ExpressionState state) {
Value value = node.value ? Value.trueInstance : Value.falseInstance;
return new ContinuationConfiguration(state.continuation, value);
}
Configuration visitNullLiteral(NullLiteral node, state) {
Configuration visitNullLiteral(NullLiteral node, ExpressionState state) {
return new ContinuationConfiguration(
state.continuation, Value.nullInstance);
}
Configuration visitLet(Let node, state) {
Configuration visitLet(Let node, ExpressionState state) {
var letCont = new LetContinuation(node.variable, node.body, state);
return new ExpressionConfiguration(
node.variable.initializer, state.withContinuation(letCont));
@@ -628,7 +635,7 @@ class VariableInitializerContinuation extends ExpressionContinuation {
/// returned and applied
/// - it returns with or without value, TBD
/// - it throws, TBD
class StatementExecuter extends StatementVisitor1<Configuration> {
class StatementExecuter extends StatementVisitor1<Configuration, State> {
Evaluator evaluator = new Evaluator();
void trampolinedExecution(Configuration configuration) {
@@ -642,21 +649,22 @@ class StatementExecuter extends StatementVisitor1<Configuration> {
Configuration eval(Expression expression, ExpressionState state) =>
evaluator.eval(expression, state);
Configuration defaultStatement(Statement node, state) {
Configuration defaultStatement(Statement node, State state) {
throw notImplemented(
m: "Execution is not implemented for statement:\n$node ");
}
Configuration visitInvalidStatement(InvalidStatement node, state) {
Configuration visitInvalidStatement(InvalidStatement node, State state) {
throw "Invalid statement at ${node.location}";
}
Configuration visitExpressionStatement(ExpressionStatement node, state) {
Configuration visitExpressionStatement(
ExpressionStatement node, State state) {
return new ExpressionConfiguration(
node.expression, new ExpressionState.fromStatementState(state));
}
Configuration visitBlock(Block node, state) {
Configuration visitBlock(Block node, State state) {
if (node.statements.isEmpty) {
return state.statementConfiguration;
}
@@ -670,26 +678,26 @@ class StatementExecuter extends StatementVisitor1<Configuration> {
return configuration;
}
Configuration visitEmptyStatement(EmptyStatement node, state) {
Configuration visitEmptyStatement(EmptyStatement node, State state) {
return state.statementConfiguration;
}
Configuration visitIfStatement(IfStatement node, state) {
Configuration visitIfStatement(IfStatement node, State state) {
var expState = new ExpressionState.fromStatementState(state);
var cont = new IfConditionContinuation(node.then, node.otherwise, state);
return new ExpressionConfiguration(
node.condition, expState.withContinuation(cont));
}
Configuration visitLabeledStatement(LabeledStatement node, state) {
Configuration visitLabeledStatement(LabeledStatement node, State state) {
return new StatementConfiguration(node.body, state.withBreak(node));
}
Configuration visitBreakStatement(BreakStatement node, state) {
Configuration visitBreakStatement(BreakStatement node, State state) {
return state.lookupLabel(node.target).configuration;
}
Configuration visitWhileStatement(WhileStatement node, state) {
Configuration visitWhileStatement(WhileStatement node, State state) {
var expState = new ExpressionState.fromStatementState(state);
var cont = new WhileConditionContinuation(node, state);
@@ -697,7 +705,7 @@ class StatementExecuter extends StatementVisitor1<Configuration> {
node.condition, expState.withContinuation(cont));
}
Configuration visitDoStatement(DoStatement node, state) {
Configuration visitDoStatement(DoStatement node, State state) {
WhileStatement whileStatement =
new WhileStatement(node.condition, node.body);
StatementConfiguration configuration =
@@ -707,7 +715,7 @@ class StatementExecuter extends StatementVisitor1<Configuration> {
node.body, state.withConfiguration(configuration));
}
Configuration visitReturnStatement(ReturnStatement node, state) {
Configuration visitReturnStatement(ReturnStatement node, State state) {
assert(state.returnContinuation != null);
// The new ExpressionState contains the next expression continuation.
var expState = new ExpressionState.fromStatementState(state)
@@ -716,7 +724,8 @@ class StatementExecuter extends StatementVisitor1<Configuration> {
node.expression ?? new NullLiteral(), expState);
}
Configuration visitVariableDeclaration(VariableDeclaration node, state) {
Configuration visitVariableDeclaration(
VariableDeclaration node, State state) {
if (node.initializer != null) {
var expState = new ExpressionState.fromStatementState(state);
var cont = new VariableInitializerContinuation(
+71 -69
View File
@@ -328,111 +328,113 @@ class Transformer extends TreeVisitor<TreeNode> {
}
}
abstract class ExpressionVisitor1<R> {
R defaultExpression(Expression node, arg) => null;
R defaultBasicLiteral(BasicLiteral node, arg) => defaultExpression(node, arg);
R visitInvalidExpression(InvalidExpression node, arg) =>
abstract class ExpressionVisitor1<R, T> {
R defaultExpression(Expression node, T arg) => null;
R defaultBasicLiteral(BasicLiteral node, T arg) =>
defaultExpression(node, arg);
R visitVariableGet(VariableGet node, arg) => defaultExpression(node, arg);
R visitVariableSet(VariableSet node, arg) => defaultExpression(node, arg);
R visitPropertyGet(PropertyGet node, arg) => defaultExpression(node, arg);
R visitPropertySet(PropertySet node, arg) => defaultExpression(node, arg);
R visitDirectPropertyGet(DirectPropertyGet node, arg) =>
R visitInvalidExpression(InvalidExpression node, T arg) =>
defaultExpression(node, arg);
R visitDirectPropertySet(DirectPropertySet node, arg) =>
R visitVariableGet(VariableGet node, T arg) => defaultExpression(node, arg);
R visitVariableSet(VariableSet node, T arg) => defaultExpression(node, arg);
R visitPropertyGet(PropertyGet node, T arg) => defaultExpression(node, arg);
R visitPropertySet(PropertySet node, T arg) => defaultExpression(node, arg);
R visitDirectPropertyGet(DirectPropertyGet node, T arg) =>
defaultExpression(node, arg);
R visitSuperPropertyGet(SuperPropertyGet node, arg) =>
R visitDirectPropertySet(DirectPropertySet node, T arg) =>
defaultExpression(node, arg);
R visitSuperPropertySet(SuperPropertySet node, arg) =>
R visitSuperPropertyGet(SuperPropertyGet node, T arg) =>
defaultExpression(node, arg);
R visitStaticGet(StaticGet node, arg) => defaultExpression(node, arg);
R visitStaticSet(StaticSet node, arg) => defaultExpression(node, arg);
R visitMethodInvocation(MethodInvocation node, arg) =>
R visitSuperPropertySet(SuperPropertySet node, T arg) =>
defaultExpression(node, arg);
R visitDirectMethodInvocation(DirectMethodInvocation node, arg) =>
R visitStaticGet(StaticGet node, T arg) => defaultExpression(node, arg);
R visitStaticSet(StaticSet node, T arg) => defaultExpression(node, arg);
R visitMethodInvocation(MethodInvocation node, T arg) =>
defaultExpression(node, arg);
R visitSuperMethodInvocation(SuperMethodInvocation node, arg) =>
R visitDirectMethodInvocation(DirectMethodInvocation node, T arg) =>
defaultExpression(node, arg);
R visitStaticInvocation(StaticInvocation node, arg) =>
R visitSuperMethodInvocation(SuperMethodInvocation node, T arg) =>
defaultExpression(node, arg);
R visitConstructorInvocation(ConstructorInvocation node, arg) =>
R visitStaticInvocation(StaticInvocation node, T arg) =>
defaultExpression(node, arg);
R visitNot(Not node, arg) => defaultExpression(node, arg);
R visitLogicalExpression(LogicalExpression node, arg) =>
R visitConstructorInvocation(ConstructorInvocation node, T arg) =>
defaultExpression(node, arg);
R visitConditionalExpression(ConditionalExpression node, arg) =>
R visitNot(Not node, T arg) => defaultExpression(node, arg);
R visitLogicalExpression(LogicalExpression node, T arg) =>
defaultExpression(node, arg);
R visitStringConcatenation(StringConcatenation node, arg) =>
R visitConditionalExpression(ConditionalExpression node, T arg) =>
defaultExpression(node, arg);
R visitIsExpression(IsExpression node, arg) => defaultExpression(node, arg);
R visitAsExpression(AsExpression node, arg) => defaultExpression(node, arg);
R visitSymbolLiteral(SymbolLiteral node, arg) => defaultExpression(node, arg);
R visitTypeLiteral(TypeLiteral node, arg) => defaultExpression(node, arg);
R visitThisExpression(ThisExpression node, arg) =>
R visitStringConcatenation(StringConcatenation node, T arg) =>
defaultExpression(node, arg);
R visitRethrow(Rethrow node, arg) => defaultExpression(node, arg);
R visitThrow(Throw node, arg) => defaultExpression(node, arg);
R visitListLiteral(ListLiteral node, arg) => defaultExpression(node, arg);
R visitMapLiteral(MapLiteral node, arg) => defaultExpression(node, arg);
R visitAwaitExpression(AwaitExpression node, arg) =>
R visitIsExpression(IsExpression node, T arg) => defaultExpression(node, arg);
R visitAsExpression(AsExpression node, T arg) => defaultExpression(node, arg);
R visitSymbolLiteral(SymbolLiteral node, T arg) =>
defaultExpression(node, arg);
R visitFunctionExpression(FunctionExpression node, arg) =>
R visitTypeLiteral(TypeLiteral node, T arg) => defaultExpression(node, arg);
R visitThisExpression(ThisExpression node, T arg) =>
defaultExpression(node, arg);
R visitStringLiteral(StringLiteral node, arg) =>
R visitRethrow(Rethrow node, T arg) => defaultExpression(node, arg);
R visitThrow(Throw node, T arg) => defaultExpression(node, arg);
R visitListLiteral(ListLiteral node, T arg) => defaultExpression(node, arg);
R visitMapLiteral(MapLiteral node, T arg) => defaultExpression(node, arg);
R visitAwaitExpression(AwaitExpression node, T arg) =>
defaultExpression(node, arg);
R visitFunctionExpression(FunctionExpression node, T arg) =>
defaultExpression(node, arg);
R visitStringLiteral(StringLiteral node, T arg) =>
defaultBasicLiteral(node, arg);
R visitIntLiteral(IntLiteral node, arg) => defaultBasicLiteral(node, arg);
R visitDoubleLiteral(DoubleLiteral node, arg) =>
R visitIntLiteral(IntLiteral node, T arg) => defaultBasicLiteral(node, arg);
R visitDoubleLiteral(DoubleLiteral node, T arg) =>
defaultBasicLiteral(node, arg);
R visitBoolLiteral(BoolLiteral node, arg) => defaultBasicLiteral(node, arg);
R visitNullLiteral(NullLiteral node, arg) => defaultBasicLiteral(node, arg);
R visitLet(Let node, arg) => defaultExpression(node, arg);
R visitLoadLibrary(LoadLibrary node, arg) => defaultExpression(node, arg);
R visitCheckLibraryIsLoaded(CheckLibraryIsLoaded node, arg) =>
R visitBoolLiteral(BoolLiteral node, T arg) => defaultBasicLiteral(node, arg);
R visitNullLiteral(NullLiteral node, T arg) => defaultBasicLiteral(node, arg);
R visitLet(Let node, T arg) => defaultExpression(node, arg);
R visitLoadLibrary(LoadLibrary node, T arg) => defaultExpression(node, arg);
R visitCheckLibraryIsLoaded(CheckLibraryIsLoaded node, T arg) =>
defaultExpression(node, arg);
R visitVectorCreation(VectorCreation node, arg) =>
R visitVectorCreation(VectorCreation node, T arg) =>
defaultExpression(node, arg);
R visitVectorGet(VectorGet node, arg) => defaultExpression(node, arg);
R visitVectorSet(VectorSet node, arg) => defaultExpression(node, arg);
R visitVectorCopy(VectorCopy node, arg) => defaultExpression(node, arg);
R visitClosureCreation(ClosureCreation node, arg) =>
R visitVectorGet(VectorGet node, T arg) => defaultExpression(node, arg);
R visitVectorSet(VectorSet node, T arg) => defaultExpression(node, arg);
R visitVectorCopy(VectorCopy node, T arg) => defaultExpression(node, arg);
R visitClosureCreation(ClosureCreation node, T arg) =>
defaultExpression(node, arg);
}
abstract class StatementVisitor1<R> {
R defaultStatement(Statement node, arg) => null;
abstract class StatementVisitor1<R, T> {
R defaultStatement(Statement node, T arg) => null;
R visitInvalidStatement(InvalidStatement node, arg) =>
R visitInvalidStatement(InvalidStatement node, T arg) =>
defaultStatement(node, arg);
R visitExpressionStatement(ExpressionStatement node, arg) =>
R visitExpressionStatement(ExpressionStatement node, T arg) =>
defaultStatement(node, arg);
R visitBlock(Block node, arg) => defaultStatement(node, arg);
R visitEmptyStatement(EmptyStatement node, arg) =>
R visitBlock(Block node, T arg) => defaultStatement(node, arg);
R visitEmptyStatement(EmptyStatement node, T arg) =>
defaultStatement(node, arg);
R visitAssertStatement(AssertStatement node, arg) =>
R visitAssertStatement(AssertStatement node, T arg) =>
defaultStatement(node, arg);
R visitLabeledStatement(LabeledStatement node, arg) =>
R visitLabeledStatement(LabeledStatement node, T arg) =>
defaultStatement(node, arg);
R visitBreakStatement(BreakStatement node, arg) =>
R visitBreakStatement(BreakStatement node, T arg) =>
defaultStatement(node, arg);
R visitWhileStatement(WhileStatement node, arg) =>
R visitWhileStatement(WhileStatement node, T arg) =>
defaultStatement(node, arg);
R visitDoStatement(DoStatement node, arg) => defaultStatement(node, arg);
R visitForStatement(ForStatement node, arg) => defaultStatement(node, arg);
R visitForInStatement(ForInStatement node, arg) =>
R visitDoStatement(DoStatement node, T arg) => defaultStatement(node, arg);
R visitForStatement(ForStatement node, T arg) => defaultStatement(node, arg);
R visitForInStatement(ForInStatement node, T arg) =>
defaultStatement(node, arg);
R visitSwitchStatement(SwitchStatement node, arg) =>
R visitSwitchStatement(SwitchStatement node, T arg) =>
defaultStatement(node, arg);
R visitContinueSwitchStatement(ContinueSwitchStatement node, arg) =>
R visitContinueSwitchStatement(ContinueSwitchStatement node, T arg) =>
defaultStatement(node, arg);
R visitIfStatement(IfStatement node, arg) => defaultStatement(node, arg);
R visitReturnStatement(ReturnStatement node, arg) =>
R visitIfStatement(IfStatement node, T arg) => defaultStatement(node, arg);
R visitReturnStatement(ReturnStatement node, T arg) =>
defaultStatement(node, arg);
R visitTryCatch(TryCatch node, arg) => defaultStatement(node, arg);
R visitTryFinally(TryFinally node, arg) => defaultStatement(node, arg);
R visitYieldStatement(YieldStatement node, arg) =>
R visitTryCatch(TryCatch node, T arg) => defaultStatement(node, arg);
R visitTryFinally(TryFinally node, T arg) => defaultStatement(node, arg);
R visitYieldStatement(YieldStatement node, T arg) =>
defaultStatement(node, arg);
R visitVariableDeclaration(VariableDeclaration node, arg) =>
R visitVariableDeclaration(VariableDeclaration node, T arg) =>
defaultStatement(node, arg);
R visitFunctionDeclaration(FunctionDeclaration node, arg) =>
R visitFunctionDeclaration(FunctionDeclaration node, T arg) =>
defaultStatement(node, arg);
}