OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 library kernel.interpreter; | 4 library kernel.interpreter; |
5 | 5 |
6 import '../ast.dart'; | 6 import '../ast.dart'; |
7 import '../ast.dart' as ast show Class; | 7 import '../ast.dart' as ast show Class; |
8 | 8 |
9 import '../log.dart'; | 9 import '../log.dart'; |
10 export '../log.dart'; | 10 export '../log.dart'; |
11 | 11 |
12 class NotImplemented { | 12 class NotImplemented { |
13 String message; | 13 String message; |
14 | 14 |
15 NotImplemented(this.message); | 15 NotImplemented(this.message); |
16 | 16 |
17 String toString() => message; | 17 String toString() => message; |
18 } | 18 } |
19 | 19 |
20 class Interpreter { | 20 class Interpreter { |
21 Program program; | |
22 StatementExecuter visitor = new StatementExecuter(); | |
23 | |
24 // The execution of the program starts with empty main environment. | 21 // The execution of the program starts with empty main environment. |
25 static MainEnvironment mainEnvironment = | 22 static MainEnvironment mainEnvironment = |
26 new MainEnvironment(<Member, Location>{}); | 23 new MainEnvironment(<Member, Location>{}); |
27 | 24 |
| 25 final Program program; |
| 26 final StatementExecuter visitor = new StatementExecuter(); |
| 27 |
28 Interpreter(this.program); | 28 Interpreter(this.program); |
29 | 29 |
30 void run() { | 30 void run() { |
31 assert(program.libraries.isEmpty); | 31 assert(program.libraries.isEmpty); |
32 Procedure mainMethod = program.mainMethod; | 32 Procedure mainMethod = program.mainMethod; |
33 | 33 |
34 if (mainMethod == null) return; | 34 if (mainMethod == null) return; |
35 | 35 |
36 Statement statementBlock = mainMethod.function.body; | 36 Statement statementBlock = mainMethod.function.body; |
37 ExecConfiguration configuration = new ExecConfiguration( | 37 ExecConfiguration configuration = new ExecConfiguration( |
(...skipping 1532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1570 /// Represents the handler that either executes a matching catch clause or | 1570 /// Represents the handler that either executes a matching catch clause or |
1571 /// applies the next handler to the given exception. | 1571 /// applies the next handler to the given exception. |
1572 class CatchHandler extends ExceptionHandler { | 1572 class CatchHandler extends ExceptionHandler { |
1573 final List<Catch> catches; | 1573 final List<Catch> catches; |
1574 final Environment environment; | 1574 final Environment environment; |
1575 final State state; | 1575 final State state; |
1576 | 1576 |
1577 CatchHandler(this.catches, this.environment, this.state); | 1577 CatchHandler(this.catches, this.environment, this.state); |
1578 | 1578 |
1579 Configuration call(Value exception, StackTrace stackTrace) { | 1579 Configuration call(Value exception, StackTrace stackTrace) { |
| 1580 if (catches.isEmpty) { |
| 1581 return new ThrowConfiguration( |
| 1582 state.exceptionComponents.handler, exception, stackTrace); |
| 1583 } |
| 1584 |
1580 // TODO(zhivkag): Check if there is a matching catch clause instead. | 1585 // TODO(zhivkag): Check if there is a matching catch clause instead. |
1581 return new ThrowConfiguration( | 1586 var currentCatch = catches.first; |
1582 state.exceptionComponents.handler, exception, stackTrace); | 1587 if (currentCatch.guard is DynamicType) { |
| 1588 // Exception is caught, execute the body. |
| 1589 |
| 1590 var env = environment; |
| 1591 if (currentCatch.exception != null) { |
| 1592 env = env.extend(currentCatch.exception, exception); |
| 1593 } |
| 1594 if (currentCatch.stackTrace != null) { |
| 1595 env = env.extend( |
| 1596 currentCatch.stackTrace, new StringValue(stackTrace.toString())); |
| 1597 } |
| 1598 var ecs = new ExceptionComponents(state.exceptionComponents.handler, |
| 1599 state.exceptionComponents.stackTrace, stackTrace, exception); |
| 1600 var newState = new State.initial() |
| 1601 .withContinuation(state.continuation) |
| 1602 .withReturnContinuation(state.returnContinuation) |
| 1603 .withException(ecs); |
| 1604 return new ExecConfiguration(currentCatch.body, env, newState); |
| 1605 } |
| 1606 |
| 1607 var catchHandler = |
| 1608 new CatchHandler(catches.skip(1).toList(), environment, state); |
| 1609 return new ThrowConfiguration(catchHandler, exception, stackTrace); |
1583 } | 1610 } |
1584 } | 1611 } |
1585 | 1612 |
1586 /// Represents the handler that executes the corresponding finalizer before | 1613 /// Represents the handler that executes the corresponding finalizer before |
1587 /// applying the next handler to the given exception. | 1614 /// applying the next handler to the given exception. |
1588 /// | 1615 /// |
1589 /// Applying the next handler to the given exception is supported with adding | 1616 /// Applying the next handler to the given exception is supported with adding |
1590 /// [RethrowSK] as next statement continuation. | 1617 /// [RethrowSK] as next statement continuation. |
1591 class FinallyHandler extends ExceptionHandler { | 1618 class FinallyHandler extends ExceptionHandler { |
1592 final Statement finallyStatement; | 1619 final Statement finallyStatement; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1654 /// Execution of a statement completes in one of the following ways: | 1681 /// Execution of a statement completes in one of the following ways: |
1655 /// - It completes normally, in which case the execution proceeds to applying | 1682 /// - It completes normally, in which case the execution proceeds to applying |
1656 /// the next continuation. | 1683 /// the next continuation. |
1657 /// - It breaks with a label, in which case the corresponding continuation is | 1684 /// - It breaks with a label, in which case the corresponding continuation is |
1658 /// returned and applied. | 1685 /// returned and applied. |
1659 /// - It returns with or without value, in which case the return continuation is | 1686 /// - It returns with or without value, in which case the return continuation is |
1660 /// returned and applied accordingly. | 1687 /// returned and applied accordingly. |
1661 /// - It throws, in which case the handler is returned and applied accordingly. | 1688 /// - It throws, in which case the handler is returned and applied accordingly. |
1662 class StatementExecuter | 1689 class StatementExecuter |
1663 extends StatementVisitor1<Configuration, ExecConfiguration> { | 1690 extends StatementVisitor1<Configuration, ExecConfiguration> { |
1664 Evaluator evaluator = new Evaluator(); | 1691 final Evaluator evaluator = new Evaluator(); |
1665 | 1692 |
1666 void trampolinedExecution(Configuration configuration) { | 1693 void trampolinedExecution(Configuration configuration) { |
1667 while (configuration != null) { | 1694 while (configuration != null) { |
1668 configuration = configuration.step(this); | 1695 configuration = configuration.step(this); |
1669 } | 1696 } |
1670 } | 1697 } |
1671 | 1698 |
1672 Configuration exec(Statement statement, ExecConfiguration conf) => | 1699 Configuration exec(Statement statement, ExecConfiguration conf) => |
1673 statement.accept1(this, conf); | 1700 statement.accept1(this, conf); |
1674 Configuration eval(Expression expression, EvalConfiguration config) => | 1701 Configuration eval(Expression expression, EvalConfiguration config) => |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2119 /// Initializes all non initialized fields from the provided class to | 2146 /// Initializes all non initialized fields from the provided class to |
2120 /// `Value.nullInstance` in the provided value. | 2147 /// `Value.nullInstance` in the provided value. |
2121 void _initializeNullFields(Class class_, Value value) { | 2148 void _initializeNullFields(Class class_, Value value) { |
2122 int startIndex = class_.superclass?.instanceSize ?? 0; | 2149 int startIndex = class_.superclass?.instanceSize ?? 0; |
2123 for (int i = startIndex; i < class_.instanceSize; i++) { | 2150 for (int i = startIndex; i < class_.instanceSize; i++) { |
2124 if (value.fields[i].value == null) { | 2151 if (value.fields[i].value == null) { |
2125 value.fields[i].value = Value.nullInstance; | 2152 value.fields[i].value = Value.nullInstance; |
2126 } | 2153 } |
2127 } | 2154 } |
2128 } | 2155 } |
OLD | NEW |