Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(345)

Unified Diff: pkg/kernel/lib/interpreter/interpreter.dart

Issue 2722283003: Initial implementation of interpreter for Kernel (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/kernel/lib/interpreter/interpreter.dart
diff --git a/pkg/kernel/lib/interpreter/interpreter.dart b/pkg/kernel/lib/interpreter/interpreter.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6e8b775c531d9583d3acb7239bbabc02e6d7c4f0
--- /dev/null
+++ b/pkg/kernel/lib/interpreter/interpreter.dart
@@ -0,0 +1,88 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
Kevin Millikin (Google) 2017/03/02 10:30:30 2017
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+library kernerl.interpreter;
+
+import 'dart:collection';
+import '../ast.dart';
+import 'interpreter_visitor.dart';
Kevin Millikin (Google) 2017/03/02 10:30:29 This is already exported by ast.dart.
+
+class NotImplemented {
+ String message;
+
+ NotImplemented(this.message);
+
+ String toString() => message;
+}
+
+class Interpreter {
+ Program program;
+
+ Interpreter(this.program);
+
+ void evalProgram() {
+ assert(program.libraries.isEmpty);
+ Procedure mainMethod = program.mainMethod;
+ Statement statementBlock = mainMethod.function.body;
+ // Evaluate only statement with one expression, ExpressionStatement, which
+ // is StaticInvocation of the method print.
+ if (statementBlock is Block) {
+ Statement statement = statementBlock.statements[0];
Kevin Millikin (Google) 2017/03/02 10:30:29 This can be statementBlock.first.
+ if (statement is ExpressionStatement) {
+ statement.expression.accept1(new ExpressionEval1(),
+ new ExpressionState(new HashMap<String, Object>()));
+ }
+ } else {
+ throw new NotImplemented('Evaluation for statement type '
+ '${statementBlock.runtimeType} is not implemented');
+ }
+ }
+}
+
+class InvalidExpressionError {
+ InvalidExpression expression;
+
+ InvalidExpressionError(this.expression);
+
+ String toString() => 'Invalid expression at '
+ '${expression.location.toString()}';
+}
+
+class ExpressionState extends State {
+ Map<String, Object> environment;
+
+ ExpressionState(this.environment);
+}
+
+class ExpressionEval1 extends ExpressionVisitor1<Object> {
+ @override
+ Object defaultExpression1(Expression node, State state) {
+ throw new NotImplemented('Evaluation for expressions of type '
+ '${node.runtimeType} is not implemented.');
+ }
+
+ Object visitInvalidExpression1(InvalidExpression node, State state) =>
+ throw new InvalidExpressionError(node);
+
+ Object visitStaticInvocation1(StaticInvocation node, State state) {
+ if ('print' == node.name.toString()) {
+ // Special evaluation of print.
+ var res = node.arguments.positional[0].accept1(this, state);
+ print(res);
+ } else {
+ throw new NotImplemented('Support for statement type '
+ '${node.runtimeType} is not implemented');
+ }
+ }
+
+ // Evaluation of BasicLiterals.
+ Object visitStringLiteral1(StringLiteral node, State state) => node.value;
+
+ Object visitIntLiteral1(IntLiteral node, State state) => node.value;
+
+ Object visitDoubleLiteral1(DoubleLiteral node, State state) => node.value;
+
+ Object visitBoolLiteral1(BoolLiteral node, State state) => node.value;
+
+ Object visitNullLiteral1(NullLiteral node, State state) => node.value;
+}

Powered by Google App Engine
This is Rietveld 408576698