| 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'; |
| 10 export '../log.dart'; |
| 11 |
| 9 class NotImplemented { | 12 class NotImplemented { |
| 10 String message; | 13 String message; |
| 11 | 14 |
| 12 NotImplemented(this.message); | 15 NotImplemented(this.message); |
| 13 | 16 |
| 14 String toString() => message; | 17 String toString() => message; |
| 15 } | 18 } |
| 16 | 19 |
| 17 class Interpreter { | 20 class Interpreter { |
| 18 Program program; | 21 Program program; |
| 19 StatementExecuter visitor = new StatementExecuter(); | 22 StatementExecuter visitor = new StatementExecuter(); |
| 20 | 23 |
| 21 Interpreter(this.program); | 24 Interpreter(this.program); |
| 22 | 25 |
| 23 void run() { | 26 void run() { |
| 24 assert(program.libraries.isEmpty); | 27 assert(program.libraries.isEmpty); |
| 25 Procedure mainMethod = program.mainMethod; | 28 Procedure mainMethod = program.mainMethod; |
| 29 |
| 30 if (mainMethod == null) return; |
| 31 |
| 26 Statement statementBlock = mainMethod.function.body; | 32 Statement statementBlock = mainMethod.function.body; |
| 27 StatementConfiguration configuration = | 33 StatementConfiguration configuration = |
| 28 new StatementConfiguration(statementBlock, new State.initial()); | 34 new StatementConfiguration(statementBlock, new State.initial()); |
| 29 visitor.trampolinedExecution(configuration); | 35 visitor.trampolinedExecution(configuration); |
| 30 } | 36 } |
| 31 } | 37 } |
| 32 | 38 |
| 33 class Binding { | 39 class Binding { |
| 34 final VariableDeclaration variable; | 40 final VariableDeclaration variable; |
| 35 Value value; | 41 Value value; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 StaticSet node, ExpressionConfiguration config) => | 130 StaticSet node, ExpressionConfiguration config) => |
| 125 defaultExpression(node, config); | 131 defaultExpression(node, config); |
| 126 | 132 |
| 127 Configuration visitStaticInvocation( | 133 Configuration visitStaticInvocation( |
| 128 StaticInvocation node, ExpressionConfiguration config) { | 134 StaticInvocation node, ExpressionConfiguration config) { |
| 129 if ('print' == node.name.toString()) { | 135 if ('print' == node.name.toString()) { |
| 130 var cont = new PrintContinuation(config.continuation); | 136 var cont = new PrintContinuation(config.continuation); |
| 131 return new ExpressionConfiguration( | 137 return new ExpressionConfiguration( |
| 132 node.arguments.positional.first, config.environment, cont); | 138 node.arguments.positional.first, config.environment, cont); |
| 133 } else { | 139 } else { |
| 140 log.info('static-invocation-${node.target.name.toString()}\n'); |
| 134 var cont = new ActualArgumentsContinuation(node.arguments, | 141 var cont = new ActualArgumentsContinuation(node.arguments, |
| 135 node.target.function, config.environment, config.continuation); | 142 node.target.function, config.environment, config.continuation); |
| 136 return cont.createCurrentConfiguration(); | 143 return cont.createCurrentConfiguration(); |
| 137 } | 144 } |
| 138 } | 145 } |
| 139 | 146 |
| 140 Configuration visitMethodInvocation( | 147 Configuration visitMethodInvocation( |
| 141 MethodInvocation node, ExpressionConfiguration config) { | 148 MethodInvocation node, ExpressionConfiguration config) { |
| 142 // Currently supports only method invocation with <2 arguments and is used | 149 // Currently supports only method invocation with <2 arguments and is used |
| 143 // to evaluate implemented operators for int, double and String values. | 150 // to evaluate implemented operators for int, double and String values. |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 return configuration; | 363 return configuration; |
| 357 } | 364 } |
| 358 } | 365 } |
| 359 | 366 |
| 360 class PrintContinuation extends ExpressionContinuation { | 367 class PrintContinuation extends ExpressionContinuation { |
| 361 final ExpressionContinuation continuation; | 368 final ExpressionContinuation continuation; |
| 362 | 369 |
| 363 PrintContinuation(this.continuation); | 370 PrintContinuation(this.continuation); |
| 364 | 371 |
| 365 Configuration call(Value v) { | 372 Configuration call(Value v) { |
| 373 log.info('print(${v.value.runtimeType}: ${v.value})\n'); |
| 366 print(v.value); | 374 print(v.value); |
| 367 return new ContinuationConfiguration(continuation, Value.nullInstance); | 375 return new ContinuationConfiguration(continuation, Value.nullInstance); |
| 368 } | 376 } |
| 369 } | 377 } |
| 370 | 378 |
| 371 class PropertyGetContinuation extends ExpressionContinuation { | 379 class PropertyGetContinuation extends ExpressionContinuation { |
| 372 final Name name; | 380 final Name name; |
| 373 final ExpressionContinuation continuation; | 381 final ExpressionContinuation continuation; |
| 374 | 382 |
| 375 PropertyGetContinuation(this.name, this.continuation); | 383 PropertyGetContinuation(this.name, this.continuation); |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 725 /// Represents the continuation for the condition expression in [IfStatement]. | 733 /// Represents the continuation for the condition expression in [IfStatement]. |
| 726 class IfConditionContinuation extends ExpressionContinuation { | 734 class IfConditionContinuation extends ExpressionContinuation { |
| 727 final Statement then; | 735 final Statement then; |
| 728 final Statement otherwise; | 736 final Statement otherwise; |
| 729 final State state; | 737 final State state; |
| 730 | 738 |
| 731 IfConditionContinuation(this.then, this.otherwise, this.state); | 739 IfConditionContinuation(this.then, this.otherwise, this.state); |
| 732 | 740 |
| 733 StatementConfiguration call(Value v) { | 741 StatementConfiguration call(Value v) { |
| 734 if (identical(v, Value.trueInstance)) { | 742 if (identical(v, Value.trueInstance)) { |
| 743 log.info("if-then\n"); |
| 735 return new StatementConfiguration(then, state); | 744 return new StatementConfiguration(then, state); |
| 736 } else if (otherwise != null) { | 745 } else if (otherwise != null) { |
| 746 log.info("if-otherwise\n"); |
| 737 return new StatementConfiguration(otherwise, state); | 747 return new StatementConfiguration(otherwise, state); |
| 738 } | 748 } |
| 739 return state.statementConfiguration; | 749 return state.statementConfiguration; |
| 740 } | 750 } |
| 741 } | 751 } |
| 742 | 752 |
| 743 /// Represents the continuation for the initializer expression in | 753 /// Represents the continuation for the initializer expression in |
| 744 /// [VariableDeclaration]. | 754 /// [VariableDeclaration]. |
| 745 class VariableInitializerContinuation extends ExpressionContinuation { | 755 class VariableInitializerContinuation extends ExpressionContinuation { |
| 746 final VariableDeclaration variable; | 756 final VariableDeclaration variable; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 839 new WhileStatement(node.condition, node.body); | 849 new WhileStatement(node.condition, node.body); |
| 840 StatementConfiguration configuration = | 850 StatementConfiguration configuration = |
| 841 new StatementConfiguration(whileStatement, state); | 851 new StatementConfiguration(whileStatement, state); |
| 842 | 852 |
| 843 return new StatementConfiguration( | 853 return new StatementConfiguration( |
| 844 node.body, state.withConfiguration(configuration)); | 854 node.body, state.withConfiguration(configuration)); |
| 845 } | 855 } |
| 846 | 856 |
| 847 Configuration visitReturnStatement(ReturnStatement node, State state) { | 857 Configuration visitReturnStatement(ReturnStatement node, State state) { |
| 848 assert(state.returnContinuation != null); | 858 assert(state.returnContinuation != null); |
| 859 log.info('return\n'); |
| 849 if (node.expression == null) { | 860 if (node.expression == null) { |
| 850 return new ContinuationConfiguration( | 861 return new ContinuationConfiguration( |
| 851 state.returnContinuation, Value.nullInstance); | 862 state.returnContinuation, Value.nullInstance); |
| 852 } | 863 } |
| 853 | 864 |
| 854 return new ExpressionConfiguration( | 865 return new ExpressionConfiguration( |
| 855 node.expression, state.environment, state.returnContinuation); | 866 node.expression, state.environment, state.returnContinuation); |
| 856 } | 867 } |
| 857 | 868 |
| 858 Configuration visitVariableDeclaration( | 869 Configuration visitVariableDeclaration( |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 977 | 988 |
| 978 const Value(); | 989 const Value(); |
| 979 | 990 |
| 980 BoolValue toBoolean() { | 991 BoolValue toBoolean() { |
| 981 return identical(this, Value.trueInstance) | 992 return identical(this, Value.trueInstance) |
| 982 ? Value.trueInstance | 993 ? Value.trueInstance |
| 983 : Value.falseInstance; | 994 : Value.falseInstance; |
| 984 } | 995 } |
| 985 | 996 |
| 986 BoolValue equals(Value other) => | 997 BoolValue equals(Value other) => |
| 987 value == other.value ? Value.trueInstance : Value.falseInstance; | 998 value == other?.value ? Value.trueInstance : Value.falseInstance; |
| 988 | 999 |
| 989 Value invokeMethod(Name name, [Value arg]) { | 1000 Value invokeMethod(Name name, [Value arg]) { |
| 1001 if (name.toString() == "==") return equals(arg); |
| 990 throw notImplemented(obj: name); | 1002 throw notImplemented(obj: name); |
| 991 } | 1003 } |
| 992 } | 1004 } |
| 993 | 1005 |
| 994 class ObjectValue extends Value { | 1006 class ObjectValue extends Value { |
| 995 Class class_; | 1007 Class class_; |
| 996 List<Value> fields; | 1008 List<Value> fields; |
| 997 Object get value => this; | 1009 Object get value => this; |
| 998 | 1010 |
| 999 ObjectValue(this.class_, this.fields); | 1011 ObjectValue(this.class_, this.fields); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1091 | 1103 |
| 1092 class NullValue extends LiteralValue { | 1104 class NullValue extends LiteralValue { |
| 1093 Object get value => null; | 1105 Object get value => null; |
| 1094 | 1106 |
| 1095 const NullValue(); | 1107 const NullValue(); |
| 1096 } | 1108 } |
| 1097 | 1109 |
| 1098 notImplemented({String m, Object obj}) { | 1110 notImplemented({String m, Object obj}) { |
| 1099 throw new NotImplemented(m ?? 'Evaluation for $obj is not implemented'); | 1111 throw new NotImplemented(m ?? 'Evaluation for $obj is not implemented'); |
| 1100 } | 1112 } |
| OLD | NEW |