OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
Kevin Millikin (Google)
2017/03/02 10:30:30
2017
| |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 library kernerl.interpreter; | |
5 | |
6 import 'dart:collection'; | |
7 import '../ast.dart'; | |
8 import 'interpreter_visitor.dart'; | |
Kevin Millikin (Google)
2017/03/02 10:30:29
This is already exported by ast.dart.
| |
9 | |
10 class NotImplemented { | |
11 String message; | |
12 | |
13 NotImplemented(this.message); | |
14 | |
15 String toString() => message; | |
16 } | |
17 | |
18 class Interpreter { | |
19 Program program; | |
20 | |
21 Interpreter(this.program); | |
22 | |
23 void evalProgram() { | |
24 assert(program.libraries.isEmpty); | |
25 Procedure mainMethod = program.mainMethod; | |
26 Statement statementBlock = mainMethod.function.body; | |
27 // Evaluate only statement with one expression, ExpressionStatement, which | |
28 // is StaticInvocation of the method print. | |
29 if (statementBlock is Block) { | |
30 Statement statement = statementBlock.statements[0]; | |
Kevin Millikin (Google)
2017/03/02 10:30:29
This can be statementBlock.first.
| |
31 if (statement is ExpressionStatement) { | |
32 statement.expression.accept1(new ExpressionEval1(), | |
33 new ExpressionState(new HashMap<String, Object>())); | |
34 } | |
35 } else { | |
36 throw new NotImplemented('Evaluation for statement type ' | |
37 '${statementBlock.runtimeType} is not implemented'); | |
38 } | |
39 } | |
40 } | |
41 | |
42 class InvalidExpressionError { | |
43 InvalidExpression expression; | |
44 | |
45 InvalidExpressionError(this.expression); | |
46 | |
47 String toString() => 'Invalid expression at ' | |
48 '${expression.location.toString()}'; | |
49 } | |
50 | |
51 class ExpressionState extends State { | |
52 Map<String, Object> environment; | |
53 | |
54 ExpressionState(this.environment); | |
55 } | |
56 | |
57 class ExpressionEval1 extends ExpressionVisitor1<Object> { | |
58 @override | |
59 Object defaultExpression1(Expression node, State state) { | |
60 throw new NotImplemented('Evaluation for expressions of type ' | |
61 '${node.runtimeType} is not implemented.'); | |
62 } | |
63 | |
64 Object visitInvalidExpression1(InvalidExpression node, State state) => | |
65 throw new InvalidExpressionError(node); | |
66 | |
67 Object visitStaticInvocation1(StaticInvocation node, State state) { | |
68 if ('print' == node.name.toString()) { | |
69 // Special evaluation of print. | |
70 var res = node.arguments.positional[0].accept1(this, state); | |
71 print(res); | |
72 } else { | |
73 throw new NotImplemented('Support for statement type ' | |
74 '${node.runtimeType} is not implemented'); | |
75 } | |
76 } | |
77 | |
78 // Evaluation of BasicLiterals. | |
79 Object visitStringLiteral1(StringLiteral node, State state) => node.value; | |
80 | |
81 Object visitIntLiteral1(IntLiteral node, State state) => node.value; | |
82 | |
83 Object visitDoubleLiteral1(DoubleLiteral node, State state) => node.value; | |
84 | |
85 Object visitBoolLiteral1(BoolLiteral node, State state) => node.value; | |
86 | |
87 Object visitNullLiteral1(NullLiteral node, State state) => node.value; | |
88 } | |
OLD | NEW |