| 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'; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 Interpreter(this.program); | 24 Interpreter(this.program); |
| 25 | 25 |
| 26 void run() { | 26 void run() { |
| 27 assert(program.libraries.isEmpty); | 27 assert(program.libraries.isEmpty); |
| 28 Procedure mainMethod = program.mainMethod; | 28 Procedure mainMethod = program.mainMethod; |
| 29 | 29 |
| 30 if (mainMethod == null) return; | 30 if (mainMethod == null) return; |
| 31 | 31 |
| 32 Statement statementBlock = mainMethod.function.body; | 32 Statement statementBlock = mainMethod.function.body; |
| 33 StatementConfiguration configuration = | 33 ExecConfiguration configuration = |
| 34 new StatementConfiguration(statementBlock, new State.initial()); | 34 new ExecConfiguration(statementBlock, new State.initial()); |
| 35 visitor.trampolinedExecution(configuration); | 35 visitor.trampolinedExecution(configuration); |
| 36 } | 36 } |
| 37 } | 37 } |
| 38 | 38 |
| 39 class Location { | 39 class Location { |
| 40 Value value; | 40 Value value; |
| 41 | 41 |
| 42 Location(this.value); | 42 Location(this.value); |
| 43 } | 43 } |
| 44 | 44 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 } | 91 } |
| 92 | 92 |
| 93 class InstanceEnvironment extends Environment { | 93 class InstanceEnvironment extends Environment { |
| 94 final ObjectValue _thisInstance; | 94 final ObjectValue _thisInstance; |
| 95 Value get thisInstance => _thisInstance; | 95 Value get thisInstance => _thisInstance; |
| 96 | 96 |
| 97 InstanceEnvironment(this._thisInstance, Environment env) : super(env); | 97 InstanceEnvironment(this._thisInstance, Environment env) : super(env); |
| 98 } | 98 } |
| 99 | 99 |
| 100 /// Evaluate expressions. | 100 /// Evaluate expressions. |
| 101 class Evaluator | 101 class Evaluator extends ExpressionVisitor1<Configuration, EvalConfiguration> { |
| 102 extends ExpressionVisitor1<Configuration, ExpressionConfiguration> { | 102 Configuration eval(Expression expr, EvalConfiguration config) => |
| 103 Configuration eval(Expression expr, ExpressionConfiguration config) => | |
| 104 expr.accept1(this, config); | 103 expr.accept1(this, config); |
| 105 | 104 |
| 106 Configuration evalList(List<InterpreterExpression> list, Environment env, | 105 Configuration evalList(List<InterpreterExpression> list, Environment env, |
| 107 ApplicationContinuation cont) { | 106 ApplicationContinuation cont) { |
| 108 if (list.isNotEmpty) { | 107 if (list.isNotEmpty) { |
| 109 return new ExpressionConfiguration(list.first.expression, env, | 108 return new EvalConfiguration(list.first.expression, env, |
| 110 new ExpressionListContinuation(list.first, list.skip(1), env, cont)); | 109 new ExpressionListContinuation(list.first, list.skip(1), env, cont)); |
| 111 } | 110 } |
| 112 return new ExpressionListContinuationConfiguration( | 111 return new ApplicationConfiguration(cont, <InterpreterValue>[]); |
| 113 cont, <InterpreterValue>[]); | |
| 114 } | 112 } |
| 115 | 113 |
| 116 Configuration defaultExpression( | 114 Configuration defaultExpression(Expression node, EvalConfiguration config) { |
| 117 Expression node, ExpressionConfiguration config) { | |
| 118 throw new NotImplemented('Evaluation for expressions of type ' | 115 throw new NotImplemented('Evaluation for expressions of type ' |
| 119 '${node.runtimeType} is not implemented.'); | 116 '${node.runtimeType} is not implemented.'); |
| 120 } | 117 } |
| 121 | 118 |
| 122 Configuration visitInvalidExpression1( | 119 Configuration visitInvalidExpression1( |
| 123 InvalidExpression node, ExpressionConfiguration config) { | 120 InvalidExpression node, EvalConfiguration config) { |
| 124 throw 'Invalid expression at ${node.location.toString()}'; | 121 throw 'Invalid expression at ${node.location.toString()}'; |
| 125 } | 122 } |
| 126 | 123 |
| 127 Configuration visitVariableGet( | 124 Configuration visitVariableGet(VariableGet node, EvalConfiguration config) { |
| 128 VariableGet node, ExpressionConfiguration config) { | |
| 129 Value value = config.environment.lookup(node.variable); | 125 Value value = config.environment.lookup(node.variable); |
| 130 return new ContinuationConfiguration(config.continuation, value); | 126 return new ValuePassingConfiguration(config.continuation, value); |
| 131 } | 127 } |
| 132 | 128 |
| 133 Configuration visitVariableSet( | 129 Configuration visitVariableSet(VariableSet node, EvalConfiguration config) { |
| 134 VariableSet node, ExpressionConfiguration config) { | |
| 135 var cont = new VariableSetContinuation( | 130 var cont = new VariableSetContinuation( |
| 136 node.variable, config.environment, config.continuation); | 131 node.variable, config.environment, config.continuation); |
| 137 return new ExpressionConfiguration(node.value, config.environment, cont); | 132 return new EvalConfiguration(node.value, config.environment, cont); |
| 138 } | 133 } |
| 139 | 134 |
| 140 Configuration visitPropertyGet( | 135 Configuration visitPropertyGet(PropertyGet node, EvalConfiguration config) { |
| 141 PropertyGet node, ExpressionConfiguration config) { | |
| 142 var cont = new PropertyGetContinuation(node.name, config.continuation); | 136 var cont = new PropertyGetContinuation(node.name, config.continuation); |
| 143 return new ExpressionConfiguration(node.receiver, config.environment, cont); | 137 return new EvalConfiguration(node.receiver, config.environment, cont); |
| 144 } | 138 } |
| 145 | 139 |
| 146 Configuration visitPropertySet( | 140 Configuration visitPropertySet(PropertySet node, EvalConfiguration config) { |
| 147 PropertySet node, ExpressionConfiguration config) { | |
| 148 var cont = new PropertySetContinuation( | 141 var cont = new PropertySetContinuation( |
| 149 node.value, node.name, config.environment, config.continuation); | 142 node.value, node.name, config.environment, config.continuation); |
| 150 return new ExpressionConfiguration(node.receiver, config.environment, cont); | 143 return new EvalConfiguration(node.receiver, config.environment, cont); |
| 151 } | 144 } |
| 152 | 145 |
| 153 Configuration visitStaticGet( | 146 Configuration visitStaticGet(StaticGet node, EvalConfiguration config) => |
| 154 StaticGet node, ExpressionConfiguration config) => | |
| 155 defaultExpression(node, config); | 147 defaultExpression(node, config); |
| 156 Configuration visitStaticSet( | 148 Configuration visitStaticSet(StaticSet node, EvalConfiguration config) => |
| 157 StaticSet node, ExpressionConfiguration config) => | |
| 158 defaultExpression(node, config); | 149 defaultExpression(node, config); |
| 159 | 150 |
| 160 Configuration visitStaticInvocation( | 151 Configuration visitStaticInvocation( |
| 161 StaticInvocation node, ExpressionConfiguration config) { | 152 StaticInvocation node, EvalConfiguration config) { |
| 162 if ('print' == node.name.toString()) { | 153 if ('print' == node.name.toString()) { |
| 163 var cont = new PrintContinuation(config.continuation); | 154 var cont = new PrintContinuation(config.continuation); |
| 164 return new ExpressionConfiguration( | 155 return new EvalConfiguration( |
| 165 node.arguments.positional.first, config.environment, cont); | 156 node.arguments.positional.first, config.environment, cont); |
| 166 } else { | 157 } else { |
| 167 log.info('static-invocation-${node.target.name.toString()}\n'); | 158 log.info('static-invocation-${node.target.name.toString()}\n'); |
| 168 | 159 |
| 169 List<InterpreterExpression> args = | 160 List<InterpreterExpression> args = |
| 170 _createArgumentExpressionList(node.arguments, node.target.function); | 161 _createArgumentExpressionList(node.arguments, node.target.function); |
| 171 ApplicationContinuation cont = new StaticInvocationApplication( | 162 ApplicationContinuation cont = new StaticInvocationApplication( |
| 172 node.target.function, config.continuation); | 163 node.target.function, config.continuation); |
| 173 return new ExpressionListConfiguration(args, config.environment, cont); | 164 return new EvalListConfiguration(args, config.environment, cont); |
| 174 } | 165 } |
| 175 } | 166 } |
| 176 | 167 |
| 177 Configuration visitMethodInvocation( | 168 Configuration visitMethodInvocation( |
| 178 MethodInvocation node, ExpressionConfiguration config) { | 169 MethodInvocation node, EvalConfiguration config) { |
| 179 // Currently supports only method invocation with <2 arguments and is used | 170 // Currently supports only method invocation with <2 arguments and is used |
| 180 // to evaluate implemented operators for int, double and String values. | 171 // to evaluate implemented operators for int, double and String values. |
| 181 var cont = new MethodInvocationContinuation( | 172 var cont = new MethodInvocationContinuation( |
| 182 node.arguments, node.name, config.environment, config.continuation); | 173 node.arguments, node.name, config.environment, config.continuation); |
| 183 | 174 |
| 184 return new ExpressionConfiguration(node.receiver, config.environment, cont); | 175 return new EvalConfiguration(node.receiver, config.environment, cont); |
| 185 } | 176 } |
| 186 | 177 |
| 187 Configuration visitConstructorInvocation( | 178 Configuration visitConstructorInvocation( |
| 188 ConstructorInvocation node, ExpressionConfiguration config) { | 179 ConstructorInvocation node, EvalConfiguration config) { |
| 189 ApplicationContinuation cont = | 180 ApplicationContinuation cont = |
| 190 new ConstructorInvocationApplication(node.target, config.continuation); | 181 new ConstructorInvocationApplication(node.target, config.continuation); |
| 191 var args = | 182 var args = |
| 192 _createArgumentExpressionList(node.arguments, node.target.function); | 183 _createArgumentExpressionList(node.arguments, node.target.function); |
| 193 | 184 |
| 194 return new ExpressionListConfiguration(args, config.environment, cont); | 185 return new EvalListConfiguration(args, config.environment, cont); |
| 195 } | 186 } |
| 196 | 187 |
| 197 Configuration visitNot(Not node, ExpressionConfiguration config) { | 188 Configuration visitNot(Not node, EvalConfiguration config) { |
| 198 return new ExpressionConfiguration(node.operand, config.environment, | 189 return new EvalConfiguration(node.operand, config.environment, |
| 199 new NotContinuation(config.continuation)); | 190 new NotContinuation(config.continuation)); |
| 200 } | 191 } |
| 201 | 192 |
| 202 Configuration visitLogicalExpression( | 193 Configuration visitLogicalExpression( |
| 203 LogicalExpression node, ExpressionConfiguration config) { | 194 LogicalExpression node, EvalConfiguration config) { |
| 204 if ('||' == node.operator) { | 195 if ('||' == node.operator) { |
| 205 var cont = new OrContinuation( | 196 var cont = new OrContinuation( |
| 206 node.right, config.environment, config.continuation); | 197 node.right, config.environment, config.continuation); |
| 207 return new ExpressionConfiguration(node.left, config.environment, cont); | 198 return new EvalConfiguration(node.left, config.environment, cont); |
| 208 } else { | 199 } else { |
| 209 assert('&&' == node.operator); | 200 assert('&&' == node.operator); |
| 210 var cont = new AndContinuation( | 201 var cont = new AndContinuation( |
| 211 node.right, config.environment, config.continuation); | 202 node.right, config.environment, config.continuation); |
| 212 return new ExpressionConfiguration(node.left, config.environment, cont); | 203 return new EvalConfiguration(node.left, config.environment, cont); |
| 213 } | 204 } |
| 214 } | 205 } |
| 215 | 206 |
| 216 Configuration visitConditionalExpression( | 207 Configuration visitConditionalExpression( |
| 217 ConditionalExpression node, ExpressionConfiguration config) { | 208 ConditionalExpression node, EvalConfiguration config) { |
| 218 var cont = new ConditionalContinuation( | 209 var cont = new ConditionalContinuation( |
| 219 node.then, node.otherwise, config.environment, config.continuation); | 210 node.then, node.otherwise, config.environment, config.continuation); |
| 220 return new ExpressionConfiguration( | 211 return new EvalConfiguration(node.condition, config.environment, cont); |
| 221 node.condition, config.environment, cont); | |
| 222 } | 212 } |
| 223 | 213 |
| 224 Configuration visitStringConcatenation( | 214 Configuration visitStringConcatenation( |
| 225 StringConcatenation node, ExpressionConfiguration config) { | 215 StringConcatenation node, EvalConfiguration config) { |
| 226 var cont = new StringConcatenationContinuation(config.continuation); | 216 var cont = new StringConcatenationContinuation(config.continuation); |
| 227 var expressions = node.expressions | 217 var expressions = node.expressions |
| 228 .map((Expression e) => new PositionalExpression(e)) | 218 .map((Expression e) => new PositionalExpression(e)) |
| 229 .toList(); | 219 .toList(); |
| 230 return new ExpressionListConfiguration( | 220 return new EvalListConfiguration(expressions, config.environment, cont); |
| 231 expressions, config.environment, cont); | |
| 232 } | 221 } |
| 233 | 222 |
| 234 Configuration visitThisExpression( | 223 Configuration visitThisExpression( |
| 235 ThisExpression node, ExpressionConfiguration config) { | 224 ThisExpression node, EvalConfiguration config) { |
| 236 return new ContinuationConfiguration( | 225 return new ValuePassingConfiguration( |
| 237 config.continuation, config.environment.thisInstance); | 226 config.continuation, config.environment.thisInstance); |
| 238 } | 227 } |
| 239 | 228 |
| 240 // Evaluation of BasicLiterals. | 229 // Evaluation of BasicLiterals. |
| 241 Configuration visitStringLiteral( | 230 Configuration visitStringLiteral( |
| 242 StringLiteral node, ExpressionConfiguration config) { | 231 StringLiteral node, EvalConfiguration config) { |
| 243 return new ContinuationConfiguration( | 232 return new ValuePassingConfiguration( |
| 244 config.continuation, new StringValue(node.value)); | 233 config.continuation, new StringValue(node.value)); |
| 245 } | 234 } |
| 246 | 235 |
| 247 Configuration visitIntLiteral( | 236 Configuration visitIntLiteral(IntLiteral node, EvalConfiguration config) { |
| 248 IntLiteral node, ExpressionConfiguration config) { | 237 return new ValuePassingConfiguration( |
| 249 return new ContinuationConfiguration( | |
| 250 config.continuation, new IntValue(node.value)); | 238 config.continuation, new IntValue(node.value)); |
| 251 } | 239 } |
| 252 | 240 |
| 253 Configuration visitDoubleLiteral( | 241 Configuration visitDoubleLiteral( |
| 254 DoubleLiteral node, ExpressionConfiguration config) { | 242 DoubleLiteral node, EvalConfiguration config) { |
| 255 return new ContinuationConfiguration( | 243 return new ValuePassingConfiguration( |
| 256 config.continuation, new DoubleValue(node.value)); | 244 config.continuation, new DoubleValue(node.value)); |
| 257 } | 245 } |
| 258 | 246 |
| 259 Configuration visitBoolLiteral( | 247 Configuration visitBoolLiteral(BoolLiteral node, EvalConfiguration config) { |
| 260 BoolLiteral node, ExpressionConfiguration config) { | |
| 261 Value value = node.value ? Value.trueInstance : Value.falseInstance; | 248 Value value = node.value ? Value.trueInstance : Value.falseInstance; |
| 262 return new ContinuationConfiguration(config.continuation, value); | 249 return new ValuePassingConfiguration(config.continuation, value); |
| 263 } | 250 } |
| 264 | 251 |
| 265 Configuration visitNullLiteral( | 252 Configuration visitNullLiteral(NullLiteral node, EvalConfiguration config) { |
| 266 NullLiteral node, ExpressionConfiguration config) { | 253 return new ValuePassingConfiguration( |
| 267 return new ContinuationConfiguration( | |
| 268 config.continuation, Value.nullInstance); | 254 config.continuation, Value.nullInstance); |
| 269 } | 255 } |
| 270 | 256 |
| 271 Configuration visitLet(Let node, ExpressionConfiguration config) { | 257 Configuration visitLet(Let node, EvalConfiguration config) { |
| 272 var letCont = new LetContinuation( | 258 var letCont = new LetContinuation( |
| 273 node.variable, node.body, config.environment, config.continuation); | 259 node.variable, node.body, config.environment, config.continuation); |
| 274 return new ExpressionConfiguration( | 260 return new EvalConfiguration( |
| 275 node.variable.initializer, config.environment, letCont); | 261 node.variable.initializer, config.environment, letCont); |
| 276 } | 262 } |
| 277 } | 263 } |
| 278 | 264 |
| 279 /// Represents a state for statement execution. | 265 /// Represents a state for statement execution. |
| 280 class State { | 266 class State { |
| 281 final Environment environment; | 267 final Environment environment; |
| 282 final Label labels; | 268 final Label labels; |
| 283 final StatementConfiguration statementConfiguration; | 269 final ExecConfiguration statementConfiguration; |
| 284 | 270 |
| 285 final ExpressionContinuation returnContinuation; | 271 final ExpressionContinuation returnContinuation; |
| 286 | 272 |
| 287 State(this.environment, this.labels, this.statementConfiguration, | 273 State(this.environment, this.labels, this.statementConfiguration, |
| 288 this.returnContinuation); | 274 this.returnContinuation); |
| 289 | 275 |
| 290 State.initial() : this(new Environment.empty(), null, null, null); | 276 State.initial() : this(new Environment.empty(), null, null, null); |
| 291 | 277 |
| 292 State withEnvironment(Environment env) { | 278 State withEnvironment(Environment env) { |
| 293 return new State(env, labels, statementConfiguration, returnContinuation); | 279 return new State(env, labels, statementConfiguration, returnContinuation); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 310 Label lookupLabel(LabeledStatement s) { | 296 Label lookupLabel(LabeledStatement s) { |
| 311 assert(labels != null); | 297 assert(labels != null); |
| 312 return labels.lookupLabel(s); | 298 return labels.lookupLabel(s); |
| 313 } | 299 } |
| 314 } | 300 } |
| 315 | 301 |
| 316 /// Represents a labeled statement, the corresponding continuation and the | 302 /// Represents a labeled statement, the corresponding continuation and the |
| 317 /// enclosing label. | 303 /// enclosing label. |
| 318 class Label { | 304 class Label { |
| 319 final LabeledStatement statement; | 305 final LabeledStatement statement; |
| 320 final StatementConfiguration configuration; | 306 final ExecConfiguration configuration; |
| 321 final Label enclosingLabel; | 307 final Label enclosingLabel; |
| 322 | 308 |
| 323 Label(this.statement, this.configuration, this.enclosingLabel); | 309 Label(this.statement, this.configuration, this.enclosingLabel); |
| 324 | 310 |
| 325 Label lookupLabel(LabeledStatement s) { | 311 Label lookupLabel(LabeledStatement s) { |
| 326 if (identical(s, statement)) return this; | 312 if (identical(s, statement)) return this; |
| 327 assert(enclosingLabel != null); | 313 assert(enclosingLabel != null); |
| 328 return enclosingLabel.lookupLabel(s); | 314 return enclosingLabel.lookupLabel(s); |
| 329 } | 315 } |
| 330 } | 316 } |
| 331 | 317 |
| 332 abstract class Configuration { | 318 abstract class Configuration { |
| 333 /// Executes the current and returns the next configuration. | 319 /// Executes the current and returns the next configuration. |
| 334 Configuration step(StatementExecuter executer); | 320 Configuration step(StatementExecuter executer); |
| 335 } | 321 } |
| 336 | 322 |
| 337 /// Represents the configuration for execution of statement. | 323 /// Represents the configuration for execution of statement. |
| 338 class StatementConfiguration extends Configuration { | 324 class ExecConfiguration extends Configuration { |
| 339 final Statement statement; | 325 final Statement statement; |
| 340 final State state; | 326 final State state; |
| 341 | 327 |
| 342 StatementConfiguration(this.statement, this.state); | 328 ExecConfiguration(this.statement, this.state); |
| 343 | 329 |
| 344 Configuration step(StatementExecuter executer) => | 330 Configuration step(StatementExecuter executer) => |
| 345 executer.exec(statement, state); | 331 executer.exec(statement, state); |
| 346 } | 332 } |
| 347 | 333 |
| 348 class ExitConfiguration extends StatementConfiguration { | 334 class ExitConfiguration extends ExecConfiguration { |
| 349 final ExpressionContinuation returnContinuation; | 335 final ExpressionContinuation returnContinuation; |
| 350 | 336 |
| 351 ExitConfiguration(this.returnContinuation) : super(null, null); | 337 ExitConfiguration(this.returnContinuation) : super(null, null); |
| 352 | 338 |
| 353 Configuration step(StatementExecuter _) { | 339 Configuration step(StatementExecuter _) { |
| 354 return returnContinuation(Value.nullInstance); | 340 return returnContinuation(Value.nullInstance); |
| 355 } | 341 } |
| 356 } | 342 } |
| 357 | 343 |
| 358 class NewInstanceConfiguration extends StatementConfiguration { | 344 class NewInstanceConfiguration extends ExecConfiguration { |
| 359 final ExpressionContinuation continuation; | 345 final ExpressionContinuation continuation; |
| 360 final ObjectValue newObject; | 346 final ObjectValue newObject; |
| 361 | 347 |
| 362 NewInstanceConfiguration(this.continuation, this.newObject) | 348 NewInstanceConfiguration(this.continuation, this.newObject) |
| 363 : super(null, new State.initial()); | 349 : super(null, new State.initial()); |
| 364 | 350 |
| 365 Configuration step(StatementExecuter _) { | 351 Configuration step(StatementExecuter _) { |
| 366 return continuation(newObject); | 352 return continuation(newObject); |
| 367 } | 353 } |
| 368 } | 354 } |
| 369 | 355 |
| 370 /// Represents the configuration for applying an [ExpressionContinuation]. | 356 /// Represents the configuration for applying an [ExpressionContinuation]. |
| 371 class ContinuationConfiguration extends Configuration { | 357 class ValuePassingConfiguration extends Configuration { |
| 372 final ExpressionContinuation continuation; | 358 final ExpressionContinuation continuation; |
| 373 final Value value; | 359 final Value value; |
| 374 | 360 |
| 375 ContinuationConfiguration(this.continuation, this.value); | 361 ValuePassingConfiguration(this.continuation, this.value); |
| 376 | 362 |
| 377 Configuration step(StatementExecuter _) => continuation(value); | 363 Configuration step(StatementExecuter _) => continuation(value); |
| 378 } | 364 } |
| 379 | 365 |
| 380 /// Represents the configuration for applying an [ApplicationContinuation]. | 366 /// Represents the configuration for applying an [ApplicationContinuation]. |
| 381 class ExpressionListContinuationConfiguration extends Configuration { | 367 class ApplicationConfiguration extends Configuration { |
| 382 final ApplicationContinuation continuation; | 368 final ApplicationContinuation continuation; |
| 383 final List<InterpreterValue> values; | 369 final List<InterpreterValue> values; |
| 384 | 370 |
| 385 ExpressionListContinuationConfiguration(this.continuation, this.values); | 371 ApplicationConfiguration(this.continuation, this.values); |
| 386 | 372 |
| 387 Configuration step(StatementExecuter _) => continuation(values); | 373 Configuration step(StatementExecuter _) => continuation(values); |
| 388 } | 374 } |
| 389 | 375 |
| 390 /// Represents the configuration for evaluating an [Expression]. | 376 /// Represents the configuration for evaluating an [Expression]. |
| 391 class ExpressionConfiguration extends Configuration { | 377 class EvalConfiguration extends Configuration { |
| 392 final Expression expression; | 378 final Expression expression; |
| 393 | 379 |
| 394 /// Environment in which the expression is evaluated. | 380 /// Environment in which the expression is evaluated. |
| 395 final Environment environment; | 381 final Environment environment; |
| 396 | 382 |
| 397 /// Next continuation to be applied. | 383 /// Next continuation to be applied. |
| 398 final Continuation continuation; | 384 final Continuation continuation; |
| 399 | 385 |
| 400 ExpressionConfiguration(this.expression, this.environment, this.continuation); | 386 EvalConfiguration(this.expression, this.environment, this.continuation); |
| 401 | 387 |
| 402 Configuration step(StatementExecuter executer) => | 388 Configuration step(StatementExecuter executer) => |
| 403 executer.eval(expression, this); | 389 executer.eval(expression, this); |
| 404 } | 390 } |
| 405 | 391 |
| 406 /// Represents the configuration for evaluating a list of expressions. | 392 /// Represents the configuration for evaluating a list of expressions. |
| 407 class ExpressionListConfiguration extends Configuration { | 393 class EvalListConfiguration extends Configuration { |
| 408 final List<InterpreterExpression> expressions; | 394 final List<InterpreterExpression> expressions; |
| 409 final Environment environment; | 395 final Environment environment; |
| 410 final Continuation continuation; | 396 final Continuation continuation; |
| 411 | 397 |
| 412 ExpressionListConfiguration( | 398 EvalListConfiguration(this.expressions, this.environment, this.continuation); |
| 413 this.expressions, this.environment, this.continuation); | |
| 414 | 399 |
| 415 Configuration step(StatementExecuter executer) => | 400 Configuration step(StatementExecuter executer) => |
| 416 executer.evalList(expressions, environment, continuation); | 401 executer.evalList(expressions, environment, continuation); |
| 417 } | 402 } |
| 418 | 403 |
| 419 abstract class InterpreterExpression { | 404 abstract class InterpreterExpression { |
| 420 Expression get expression; | 405 Expression get expression; |
| 421 | 406 |
| 422 InterpreterValue assignValue(Value v); | 407 InterpreterValue assignValue(Value v); |
| 423 } | 408 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 StaticInvocationApplication(this.function, this.continuation); | 519 StaticInvocationApplication(this.function, this.continuation); |
| 535 | 520 |
| 536 Configuration call(List<InterpreterValue> argValues) { | 521 Configuration call(List<InterpreterValue> argValues) { |
| 537 Environment functionEnv = | 522 Environment functionEnv = |
| 538 ApplicationContinuation.createEnvironment(function, argValues); | 523 ApplicationContinuation.createEnvironment(function, argValues); |
| 539 | 524 |
| 540 State bodyState = new State.initial() | 525 State bodyState = new State.initial() |
| 541 .withExpressionContinuation(continuation) | 526 .withExpressionContinuation(continuation) |
| 542 .withConfiguration(new ExitConfiguration(continuation)) | 527 .withConfiguration(new ExitConfiguration(continuation)) |
| 543 .withEnvironment(functionEnv); | 528 .withEnvironment(functionEnv); |
| 544 return new StatementConfiguration(function.body, bodyState); | 529 return new ExecConfiguration(function.body, bodyState); |
| 545 } | 530 } |
| 546 } | 531 } |
| 547 | 532 |
| 548 /// Represents the application continuation for constructor invocation applied | 533 /// Represents the application continuation for constructor invocation applied |
| 549 /// on the list of evaluated arguments. | 534 /// on the list of evaluated arguments. |
| 550 class ConstructorInvocationApplication extends ApplicationContinuation { | 535 class ConstructorInvocationApplication extends ApplicationContinuation { |
| 551 final Constructor constructor; | 536 final Constructor constructor; |
| 552 final ExpressionContinuation continuation; | 537 final ExpressionContinuation continuation; |
| 553 | 538 |
| 554 ConstructorInvocationApplication(this.constructor, this.continuation); | 539 ConstructorInvocationApplication(this.constructor, this.continuation); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 566 new InstanceEnvironment(newObject, ctrEnv), | 551 new InstanceEnvironment(newObject, ctrEnv), |
| 567 new NewInstanceConfiguration(continuation, newObject)); | 552 new NewInstanceConfiguration(continuation, newObject)); |
| 568 } | 553 } |
| 569 } | 554 } |
| 570 | 555 |
| 571 /// Represents the application continuation for redirecting constructor | 556 /// Represents the application continuation for redirecting constructor |
| 572 /// invocation applied on the list of evaluated arguments. | 557 /// invocation applied on the list of evaluated arguments. |
| 573 class RedirectingConstructorApplication extends ApplicationContinuation { | 558 class RedirectingConstructorApplication extends ApplicationContinuation { |
| 574 final Constructor constructor; | 559 final Constructor constructor; |
| 575 final Environment environment; | 560 final Environment environment; |
| 576 final StatementConfiguration configuration; | 561 final ExecConfiguration configuration; |
| 577 | 562 |
| 578 RedirectingConstructorApplication( | 563 RedirectingConstructorApplication( |
| 579 this.constructor, this.environment, this.configuration); | 564 this.constructor, this.environment, this.configuration); |
| 580 | 565 |
| 581 Configuration call(List<InterpreterValue> argValues) { | 566 Configuration call(List<InterpreterValue> argValues) { |
| 582 Value object = environment.thisInstance; | 567 Value object = environment.thisInstance; |
| 583 Environment ctrEnv = ApplicationContinuation.createEnvironment( | 568 Environment ctrEnv = ApplicationContinuation.createEnvironment( |
| 584 constructor.function, | 569 constructor.function, |
| 585 argValues, | 570 argValues, |
| 586 new InstanceEnvironment(object, new Environment.empty())); | 571 new InstanceEnvironment(object, new Environment.empty())); |
| 587 | 572 |
| 588 return new ObjectInitializationConfiguration( | 573 return new ObjectInitializationConfiguration( |
| 589 constructor, ctrEnv, configuration); | 574 constructor, ctrEnv, configuration); |
| 590 } | 575 } |
| 591 } | 576 } |
| 592 | 577 |
| 593 /// Represents the application continuation for super constructor | 578 /// Represents the application continuation for super constructor |
| 594 /// invocation applied on the list of evaluated arguments. | 579 /// invocation applied on the list of evaluated arguments. |
| 595 class SuperConstructorApplication extends ApplicationContinuation { | 580 class SuperConstructorApplication extends ApplicationContinuation { |
| 596 final Constructor constructor; | 581 final Constructor constructor; |
| 597 final Environment environment; | 582 final Environment environment; |
| 598 final StatementConfiguration configuration; | 583 final ExecConfiguration configuration; |
| 599 | 584 |
| 600 SuperConstructorApplication( | 585 SuperConstructorApplication( |
| 601 this.constructor, this.environment, this.configuration); | 586 this.constructor, this.environment, this.configuration); |
| 602 | 587 |
| 603 Configuration call(List<InterpreterValue> argValues) { | 588 Configuration call(List<InterpreterValue> argValues) { |
| 604 Value object = environment.thisInstance; | 589 Value object = environment.thisInstance; |
| 605 | 590 |
| 606 Environment superEnv = ApplicationContinuation.createEnvironment( | 591 Environment superEnv = ApplicationContinuation.createEnvironment( |
| 607 constructor.function, | 592 constructor.function, |
| 608 argValues, | 593 argValues, |
| 609 new InstanceEnvironment(object, new Environment.empty())); | 594 new InstanceEnvironment(object, new Environment.empty())); |
| 610 | 595 |
| 611 return new ObjectInitializationConfiguration( | 596 return new ObjectInitializationConfiguration( |
| 612 constructor, superEnv, configuration); | 597 constructor, superEnv, configuration); |
| 613 } | 598 } |
| 614 } | 599 } |
| 615 | 600 |
| 616 /// Represents the configuration for execution of initializer and | 601 /// Represents the configuration for execution of initializer and |
| 617 /// constructor body statements for initialization of a newly allocated object. | 602 /// constructor body statements for initialization of a newly allocated object. |
| 618 class ObjectInitializationConfiguration extends Configuration { | 603 class ObjectInitializationConfiguration extends Configuration { |
| 619 final Constructor constructor; | 604 final Constructor constructor; |
| 620 final Environment environment; | 605 final Environment environment; |
| 621 final StatementConfiguration configuration; | 606 final ExecConfiguration configuration; |
| 622 | 607 |
| 623 ObjectInitializationConfiguration( | 608 ObjectInitializationConfiguration( |
| 624 this.constructor, this.environment, this.configuration); | 609 this.constructor, this.environment, this.configuration); |
| 625 | 610 |
| 626 Configuration step(StatementExecuter _) { | 611 Configuration step(StatementExecuter _) { |
| 627 if (constructor.initializers.isNotEmpty && | 612 if (constructor.initializers.isNotEmpty && |
| 628 constructor.initializers.last is RedirectingInitializer) { | 613 constructor.initializers.last is RedirectingInitializer) { |
| 629 // Constructor is redirecting. | 614 // Constructor is redirecting. |
| 630 Initializer initializer = constructor.initializers.first; | 615 Initializer initializer = constructor.initializers.first; |
| 631 if (initializer is RedirectingInitializer) { | 616 if (initializer is RedirectingInitializer) { |
| 632 var app = new RedirectingConstructorApplication( | 617 var app = new RedirectingConstructorApplication( |
| 633 initializer.target, environment, configuration); | 618 initializer.target, environment, configuration); |
| 634 var args = _createArgumentExpressionList( | 619 var args = _createArgumentExpressionList( |
| 635 initializer.arguments, initializer.target.function); | 620 initializer.arguments, initializer.target.function); |
| 636 | 621 |
| 637 return new ExpressionListConfiguration(args, environment, app); | 622 return new EvalListConfiguration(args, environment, app); |
| 638 } | 623 } |
| 639 // Redirecting initializer is not the only initializer. | 624 // Redirecting initializer is not the only initializer. |
| 640 for (Initializer i in constructor.initializers.reversed.skip(1)) { | 625 for (Initializer i in constructor.initializers.reversed.skip(1)) { |
| 641 assert(i is LocalInitializer); | 626 assert(i is LocalInitializer); |
| 642 } | 627 } |
| 643 var class_ = new Class(constructor.enclosingClass.reference); | 628 var class_ = new Class(constructor.enclosingClass.reference); |
| 644 var initEnv = new Environment(environment); | 629 var initEnv = new Environment(environment); |
| 645 var cont = new InitializerContinuation( | 630 var cont = new InitializerContinuation( |
| 646 class_, initEnv, constructor.initializers, configuration); | 631 class_, initEnv, constructor.initializers, configuration); |
| 647 return new ExpressionConfiguration( | 632 return new EvalConfiguration( |
| 648 (initializer as LocalInitializer).variable.initializer, | 633 (initializer as LocalInitializer).variable.initializer, |
| 649 initEnv, | 634 initEnv, |
| 650 cont); | 635 cont); |
| 651 } | 636 } |
| 652 | 637 |
| 653 // Set head of configurations to be executed to configuration for current | 638 // Set head of configurations to be executed to configuration for current |
| 654 // constructor body. | 639 // constructor body. |
| 655 var state = new State.initial() | 640 var state = new State.initial() |
| 656 .withEnvironment(environment) | 641 .withEnvironment(environment) |
| 657 .withConfiguration(configuration); | 642 .withConfiguration(configuration); |
| 658 var bodyConfig = | 643 var bodyConfig = new ExecConfiguration(constructor.function.body, state); |
| 659 new StatementConfiguration(constructor.function.body, state); | |
| 660 | 644 |
| 661 // Initialize fields in immediately enclosing class. | 645 // Initialize fields in immediately enclosing class. |
| 662 var cont = | 646 var cont = |
| 663 new InstanceFieldsApplication(constructor, environment, bodyConfig); | 647 new InstanceFieldsApplication(constructor, environment, bodyConfig); |
| 664 var fieldExpressions = _createInstanceInitializers(constructor); | 648 var fieldExpressions = _createInstanceInitializers(constructor); |
| 665 | 649 |
| 666 return new ExpressionListConfiguration( | 650 return new EvalListConfiguration( |
| 667 fieldExpressions, new Environment.empty(), cont); | 651 fieldExpressions, new Environment.empty(), cont); |
| 668 } | 652 } |
| 669 | 653 |
| 670 /// Creates a list of expressions for instance field initializers in | 654 /// Creates a list of expressions for instance field initializers in |
| 671 /// immediately enclosing class. | 655 /// immediately enclosing class. |
| 672 static List<InterpreterExpression> _createInstanceInitializers( | 656 static List<InterpreterExpression> _createInstanceInitializers( |
| 673 Constructor ctr) { | 657 Constructor ctr) { |
| 674 Class currentClass = new Class(ctr.enclosingClass.reference); | 658 Class currentClass = new Class(ctr.enclosingClass.reference); |
| 675 List<InterpreterExpression> es = <InterpreterExpression>[]; | 659 List<InterpreterExpression> es = <InterpreterExpression>[]; |
| 676 | 660 |
| 677 for (int i = currentClass.superclass?.instanceSize ?? 0; | 661 for (int i = currentClass.superclass?.instanceSize ?? 0; |
| 678 i < currentClass.instanceSize; | 662 i < currentClass.instanceSize; |
| 679 i++) { | 663 i++) { |
| 680 Field current = currentClass.instanceFields[i]; | 664 Field current = currentClass.instanceFields[i]; |
| 681 if (current.initializer != null) { | 665 if (current.initializer != null) { |
| 682 es.add(new FieldInitializerExpression(current, current.initializer)); | 666 es.add(new FieldInitializerExpression(current, current.initializer)); |
| 683 } | 667 } |
| 684 } | 668 } |
| 685 | 669 |
| 686 return es; | 670 return es; |
| 687 } | 671 } |
| 688 } | 672 } |
| 689 | 673 |
| 690 /// Represents the application continuation applied on the list of evaluated | 674 /// Represents the application continuation applied on the list of evaluated |
| 691 /// field initializer expressions. | 675 /// field initializer expressions. |
| 692 class InstanceFieldsApplication extends ApplicationContinuation { | 676 class InstanceFieldsApplication extends ApplicationContinuation { |
| 693 final Constructor constructor; | 677 final Constructor constructor; |
| 694 final Environment environment; | 678 final Environment environment; |
| 695 final StatementConfiguration configuration; | 679 final ExecConfiguration configuration; |
| 696 | 680 |
| 697 final Class _currentClass; | 681 final Class _currentClass; |
| 698 final ObjectValue _newObject; | 682 final ObjectValue _newObject; |
| 699 | 683 |
| 700 InstanceFieldsApplication( | 684 InstanceFieldsApplication( |
| 701 this.constructor, this.environment, this.configuration) | 685 this.constructor, this.environment, this.configuration) |
| 702 : _currentClass = new Class(constructor.enclosingClass.reference), | 686 : _currentClass = new Class(constructor.enclosingClass.reference), |
| 703 _newObject = environment.thisInstance; | 687 _newObject = environment.thisInstance; |
| 704 | 688 |
| 705 Configuration call(List<InterpreterValue> fieldValues) { | 689 Configuration call(List<InterpreterValue> fieldValues) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 716 if (constructor.initializers.first is SuperInitializer) { | 700 if (constructor.initializers.first is SuperInitializer) { |
| 717 // SuperInitializer appears last in the initializer list. | 701 // SuperInitializer appears last in the initializer list. |
| 718 assert(constructor.initializers.length == 1); | 702 assert(constructor.initializers.length == 1); |
| 719 SuperInitializer current = constructor.initializers.first; | 703 SuperInitializer current = constructor.initializers.first; |
| 720 var args = _createArgumentExpressionList( | 704 var args = _createArgumentExpressionList( |
| 721 current.arguments, current.target.function); | 705 current.arguments, current.target.function); |
| 722 | 706 |
| 723 var superApp = new SuperConstructorApplication( | 707 var superApp = new SuperConstructorApplication( |
| 724 current.target, environment, configuration); | 708 current.target, environment, configuration); |
| 725 _initializeNullFields(_currentClass, _newObject); | 709 _initializeNullFields(_currentClass, _newObject); |
| 726 return new ExpressionListConfiguration(args, environment, superApp); | 710 return new EvalListConfiguration(args, environment, superApp); |
| 727 } | 711 } |
| 728 | 712 |
| 729 Class class_ = new Class(constructor.enclosingClass.reference); | 713 Class class_ = new Class(constructor.enclosingClass.reference); |
| 730 Environment initEnv = new Environment(environment); | 714 Environment initEnv = new Environment(environment); |
| 731 | 715 |
| 732 var cont = new InitializerContinuation( | 716 var cont = new InitializerContinuation( |
| 733 class_, initEnv, constructor.initializers, configuration); | 717 class_, initEnv, constructor.initializers, configuration); |
| 734 return new ExpressionConfiguration( | 718 return new EvalConfiguration( |
| 735 _getExpression(constructor.initializers.first), initEnv, cont); | 719 _getExpression(constructor.initializers.first), initEnv, cont); |
| 736 } | 720 } |
| 737 } | 721 } |
| 738 | 722 |
| 739 /// Represents the expression continuation applied on the list of evaluated | 723 /// Represents the expression continuation applied on the list of evaluated |
| 740 /// initializer expressions preceding a super call in the list. | 724 /// initializer expressions preceding a super call in the list. |
| 741 class InitializerContinuation extends ExpressionContinuation { | 725 class InitializerContinuation extends ExpressionContinuation { |
| 742 final Class currentClass; | 726 final Class currentClass; |
| 743 final Environment initializerEnvironment; | 727 final Environment initializerEnvironment; |
| 744 final List<Initializer> initializers; | 728 final List<Initializer> initializers; |
| 745 final StatementConfiguration configuration; | 729 final ExecConfiguration configuration; |
| 746 | 730 |
| 747 InitializerContinuation(this.currentClass, this.initializerEnvironment, | 731 InitializerContinuation(this.currentClass, this.initializerEnvironment, |
| 748 this.initializers, this.configuration); | 732 this.initializers, this.configuration); |
| 749 | 733 |
| 750 Configuration call(Value v) { | 734 Configuration call(Value v) { |
| 751 ObjectValue newObject = initializerEnvironment.thisInstance; | 735 ObjectValue newObject = initializerEnvironment.thisInstance; |
| 752 Initializer current = initializers.first; | 736 Initializer current = initializers.first; |
| 753 if (current is FieldInitializer) { | 737 if (current is FieldInitializer) { |
| 754 currentClass.setProperty(newObject, current.field, v); | 738 currentClass.setProperty(newObject, current.field, v); |
| 755 } else if (current is LocalInitializer) { | 739 } else if (current is LocalInitializer) { |
| 756 initializerEnvironment.expand(current.variable, v); | 740 initializerEnvironment.expand(current.variable, v); |
| 757 } else { | 741 } else { |
| 758 throw 'Assigning value $v to ${current.runtimeType}'; | 742 throw 'Assigning value $v to ${current.runtimeType}'; |
| 759 } | 743 } |
| 760 | 744 |
| 761 if (initializers.length <= 1) { | 745 if (initializers.length <= 1) { |
| 762 _initializeNullFields(currentClass, newObject); | 746 _initializeNullFields(currentClass, newObject); |
| 763 return configuration; | 747 return configuration; |
| 764 } | 748 } |
| 765 | 749 |
| 766 Initializer next = initializers[1]; | 750 Initializer next = initializers[1]; |
| 767 | 751 |
| 768 if (next is RedirectingInitializer) { | 752 if (next is RedirectingInitializer) { |
| 769 // RedirectingInitializer appears last in the initializer list. | 753 // RedirectingInitializer appears last in the initializer list. |
| 770 assert(initializers.length == 2); | 754 assert(initializers.length == 2); |
| 771 var app = new RedirectingConstructorApplication( | 755 var app = new RedirectingConstructorApplication( |
| 772 next.target, initializerEnvironment, configuration); | 756 next.target, initializerEnvironment, configuration); |
| 773 var args = | 757 var args = |
| 774 _createArgumentExpressionList(next.arguments, next.target.function); | 758 _createArgumentExpressionList(next.arguments, next.target.function); |
| 775 return new ExpressionListConfiguration(args, initializerEnvironment, app); | 759 return new EvalListConfiguration(args, initializerEnvironment, app); |
| 776 } | 760 } |
| 777 | 761 |
| 778 if (next is SuperInitializer) { | 762 if (next is SuperInitializer) { |
| 779 // SuperInitializer appears last in the initializer list. | 763 // SuperInitializer appears last in the initializer list. |
| 780 assert(initializers.length == 2); | 764 assert(initializers.length == 2); |
| 781 var args = | 765 var args = |
| 782 _createArgumentExpressionList(next.arguments, next.target.function); | 766 _createArgumentExpressionList(next.arguments, next.target.function); |
| 783 var superApp = new SuperConstructorApplication( | 767 var superApp = new SuperConstructorApplication( |
| 784 next.target, initializerEnvironment, configuration); | 768 next.target, initializerEnvironment, configuration); |
| 785 _initializeNullFields(currentClass, newObject); | 769 _initializeNullFields(currentClass, newObject); |
| 786 return new ExpressionListConfiguration( | 770 return new EvalListConfiguration(args, initializerEnvironment, superApp); |
| 787 args, initializerEnvironment, superApp); | |
| 788 } | 771 } |
| 789 | 772 |
| 790 var cont = new InitializerContinuation(currentClass, initializerEnvironment, | 773 var cont = new InitializerContinuation(currentClass, initializerEnvironment, |
| 791 initializers.skip(1).toList(), configuration); | 774 initializers.skip(1).toList(), configuration); |
| 792 return new ExpressionConfiguration( | 775 return new EvalConfiguration( |
| 793 _getExpression(next), initializerEnvironment, cont); | 776 _getExpression(next), initializerEnvironment, cont); |
| 794 } | 777 } |
| 795 } | 778 } |
| 796 | 779 |
| 797 /// Represents the application continuation called after the evaluation of all | 780 /// Represents the application continuation called after the evaluation of all |
| 798 /// argument expressions for an invocation. | 781 /// argument expressions for an invocation. |
| 799 class ValueApplication extends ApplicationContinuation { | 782 class ValueApplication extends ApplicationContinuation { |
| 800 final InterpreterValue value; | 783 final InterpreterValue value; |
| 801 final ApplicationContinuation applicationContinuation; | 784 final ApplicationContinuation applicationContinuation; |
| 802 | 785 |
| 803 ValueApplication(this.value, this.applicationContinuation); | 786 ValueApplication(this.value, this.applicationContinuation); |
| 804 | 787 |
| 805 Configuration call(List<InterpreterValue> args) { | 788 Configuration call(List<InterpreterValue> args) { |
| 806 args.add(value); | 789 args.add(value); |
| 807 return new ExpressionListContinuationConfiguration( | 790 return new ApplicationConfiguration(applicationContinuation, args); |
| 808 applicationContinuation, args); | |
| 809 } | 791 } |
| 810 } | 792 } |
| 811 | 793 |
| 812 /// Represents an expression continuation. | 794 /// Represents an expression continuation. |
| 813 abstract class ExpressionContinuation extends Continuation { | 795 abstract class ExpressionContinuation extends Continuation { |
| 814 Configuration call(Value v); | 796 Configuration call(Value v); |
| 815 } | 797 } |
| 816 | 798 |
| 817 /// Represents a continuation that returns the next [StatementConfiguration] | 799 /// Represents a continuation that returns the next [ExecConfiguration] |
| 818 /// to be executed. | 800 /// to be executed. |
| 819 class ExpressionStatementContinuation extends ExpressionContinuation { | 801 class ExpressionStatementContinuation extends ExpressionContinuation { |
| 820 final StatementConfiguration configuration; | 802 final ExecConfiguration configuration; |
| 821 | 803 |
| 822 ExpressionStatementContinuation(this.configuration); | 804 ExpressionStatementContinuation(this.configuration); |
| 823 | 805 |
| 824 Configuration call(Value _) { | 806 Configuration call(Value _) { |
| 825 return configuration; | 807 return configuration; |
| 826 } | 808 } |
| 827 } | 809 } |
| 828 | 810 |
| 829 class PrintContinuation extends ExpressionContinuation { | 811 class PrintContinuation extends ExpressionContinuation { |
| 830 final ExpressionContinuation continuation; | 812 final ExpressionContinuation continuation; |
| 831 | 813 |
| 832 PrintContinuation(this.continuation); | 814 PrintContinuation(this.continuation); |
| 833 | 815 |
| 834 Configuration call(Value v) { | 816 Configuration call(Value v) { |
| 835 log.info('print(${v.value.runtimeType}: ${v.value})\n'); | 817 log.info('print(${v.value.runtimeType}: ${v.value})\n'); |
| 836 print(v.value); | 818 print(v.value); |
| 837 return new ContinuationConfiguration(continuation, Value.nullInstance); | 819 return new ValuePassingConfiguration(continuation, Value.nullInstance); |
| 838 } | 820 } |
| 839 } | 821 } |
| 840 | 822 |
| 841 class PropertyGetContinuation extends ExpressionContinuation { | 823 class PropertyGetContinuation extends ExpressionContinuation { |
| 842 final Name name; | 824 final Name name; |
| 843 final ExpressionContinuation continuation; | 825 final ExpressionContinuation continuation; |
| 844 | 826 |
| 845 PropertyGetContinuation(this.name, this.continuation); | 827 PropertyGetContinuation(this.name, this.continuation); |
| 846 | 828 |
| 847 Configuration call(Value receiver) { | 829 Configuration call(Value receiver) { |
| 848 // TODO: CPS the invocation of the getter. | 830 // TODO: CPS the invocation of the getter. |
| 849 Value propertyValue = receiver.class_.lookupGetter(name)(receiver); | 831 Value propertyValue = receiver.class_.lookupGetter(name)(receiver); |
| 850 return new ContinuationConfiguration(continuation, propertyValue); | 832 return new ValuePassingConfiguration(continuation, propertyValue); |
| 851 } | 833 } |
| 852 } | 834 } |
| 853 | 835 |
| 854 class PropertySetContinuation extends ExpressionContinuation { | 836 class PropertySetContinuation extends ExpressionContinuation { |
| 855 final Expression value; | 837 final Expression value; |
| 856 final Name setterName; | 838 final Name setterName; |
| 857 final Environment environment; | 839 final Environment environment; |
| 858 final ExpressionContinuation continuation; | 840 final ExpressionContinuation continuation; |
| 859 | 841 |
| 860 PropertySetContinuation( | 842 PropertySetContinuation( |
| 861 this.value, this.setterName, this.environment, this.continuation); | 843 this.value, this.setterName, this.environment, this.continuation); |
| 862 | 844 |
| 863 Configuration call(Value receiver) { | 845 Configuration call(Value receiver) { |
| 864 var cont = new SetterContinuation(receiver, setterName, continuation); | 846 var cont = new SetterContinuation(receiver, setterName, continuation); |
| 865 return new ExpressionConfiguration(value, environment, cont); | 847 return new EvalConfiguration(value, environment, cont); |
| 866 } | 848 } |
| 867 } | 849 } |
| 868 | 850 |
| 869 class SetterContinuation extends ExpressionContinuation { | 851 class SetterContinuation extends ExpressionContinuation { |
| 870 final Value receiver; | 852 final Value receiver; |
| 871 final Name name; | 853 final Name name; |
| 872 final ExpressionContinuation continuation; | 854 final ExpressionContinuation continuation; |
| 873 | 855 |
| 874 SetterContinuation(this.receiver, this.name, this.continuation); | 856 SetterContinuation(this.receiver, this.name, this.continuation); |
| 875 | 857 |
| 876 Configuration call(Value v) { | 858 Configuration call(Value v) { |
| 877 Setter setter = receiver.class_.lookupSetter(name); | 859 Setter setter = receiver.class_.lookupSetter(name); |
| 878 setter(receiver, v); | 860 setter(receiver, v); |
| 879 return new ContinuationConfiguration(continuation, v); | 861 return new ValuePassingConfiguration(continuation, v); |
| 880 } | 862 } |
| 881 } | 863 } |
| 882 | 864 |
| 883 /// Represents a continuation to be called after the evaluation of an actual | 865 /// Represents a continuation to be called after the evaluation of an actual |
| 884 /// argument for function invocation. | 866 /// argument for function invocation. |
| 885 class ExpressionListContinuation extends ExpressionContinuation { | 867 class ExpressionListContinuation extends ExpressionContinuation { |
| 886 final InterpreterExpression currentExpression; | 868 final InterpreterExpression currentExpression; |
| 887 final List<InterpreterExpression> expressions; | 869 final List<InterpreterExpression> expressions; |
| 888 final Environment environment; | 870 final Environment environment; |
| 889 final ApplicationContinuation applicationContinuation; | 871 final ApplicationContinuation applicationContinuation; |
| 890 | 872 |
| 891 ExpressionListContinuation(this.currentExpression, this.expressions, | 873 ExpressionListContinuation(this.currentExpression, this.expressions, |
| 892 this.environment, this.applicationContinuation); | 874 this.environment, this.applicationContinuation); |
| 893 | 875 |
| 894 Configuration call(Value v) { | 876 Configuration call(Value v) { |
| 895 ValueApplication app = new ValueApplication( | 877 ValueApplication app = new ValueApplication( |
| 896 currentExpression.assignValue(v), applicationContinuation); | 878 currentExpression.assignValue(v), applicationContinuation); |
| 897 return new ExpressionListConfiguration(expressions, environment, app); | 879 return new EvalListConfiguration(expressions, environment, app); |
| 898 } | 880 } |
| 899 } | 881 } |
| 900 | 882 |
| 901 class MethodInvocationContinuation extends ExpressionContinuation { | 883 class MethodInvocationContinuation extends ExpressionContinuation { |
| 902 final Arguments arguments; | 884 final Arguments arguments; |
| 903 final Name methodName; | 885 final Name methodName; |
| 904 final Environment environment; | 886 final Environment environment; |
| 905 final ExpressionContinuation continuation; | 887 final ExpressionContinuation continuation; |
| 906 | 888 |
| 907 MethodInvocationContinuation( | 889 MethodInvocationContinuation( |
| 908 this.arguments, this.methodName, this.environment, this.continuation); | 890 this.arguments, this.methodName, this.environment, this.continuation); |
| 909 | 891 |
| 910 Configuration call(Value receiver) { | 892 Configuration call(Value receiver) { |
| 911 if (arguments.positional.isEmpty) { | 893 if (arguments.positional.isEmpty) { |
| 912 Value returnValue = receiver.invokeMethod(methodName); | 894 Value returnValue = receiver.invokeMethod(methodName); |
| 913 return new ContinuationConfiguration(continuation, returnValue); | 895 return new ValuePassingConfiguration(continuation, returnValue); |
| 914 } | 896 } |
| 915 var cont = new ArgumentsContinuation( | 897 var cont = new ArgumentsContinuation( |
| 916 receiver, methodName, arguments, environment, continuation); | 898 receiver, methodName, arguments, environment, continuation); |
| 917 | 899 |
| 918 return new ExpressionConfiguration( | 900 return new EvalConfiguration(arguments.positional.first, environment, cont); |
| 919 arguments.positional.first, environment, cont); | |
| 920 } | 901 } |
| 921 } | 902 } |
| 922 | 903 |
| 923 class ArgumentsContinuation extends ExpressionContinuation { | 904 class ArgumentsContinuation extends ExpressionContinuation { |
| 924 final Value receiver; | 905 final Value receiver; |
| 925 final Name methodName; | 906 final Name methodName; |
| 926 final Arguments arguments; | 907 final Arguments arguments; |
| 927 final Environment environment; | 908 final Environment environment; |
| 928 final ExpressionContinuation continuation; | 909 final ExpressionContinuation continuation; |
| 929 | 910 |
| 930 ArgumentsContinuation(this.receiver, this.methodName, this.arguments, | 911 ArgumentsContinuation(this.receiver, this.methodName, this.arguments, |
| 931 this.environment, this.continuation); | 912 this.environment, this.continuation); |
| 932 | 913 |
| 933 Configuration call(Value value) { | 914 Configuration call(Value value) { |
| 934 // Currently evaluates only one argument, for simple method invocations | 915 // Currently evaluates only one argument, for simple method invocations |
| 935 // with 1 argument. | 916 // with 1 argument. |
| 936 Value returnValue = receiver.invokeMethod(methodName, value); | 917 Value returnValue = receiver.invokeMethod(methodName, value); |
| 937 return new ContinuationConfiguration(continuation, returnValue); | 918 return new ValuePassingConfiguration(continuation, returnValue); |
| 938 } | 919 } |
| 939 } | 920 } |
| 940 | 921 |
| 941 class VariableSetContinuation extends ExpressionContinuation { | 922 class VariableSetContinuation extends ExpressionContinuation { |
| 942 final VariableDeclaration variable; | 923 final VariableDeclaration variable; |
| 943 final Environment environment; | 924 final Environment environment; |
| 944 final ExpressionContinuation continuation; | 925 final ExpressionContinuation continuation; |
| 945 | 926 |
| 946 VariableSetContinuation(this.variable, this.environment, this.continuation); | 927 VariableSetContinuation(this.variable, this.environment, this.continuation); |
| 947 | 928 |
| 948 Configuration call(Value value) { | 929 Configuration call(Value value) { |
| 949 environment.assign(variable, value); | 930 environment.assign(variable, value); |
| 950 return new ContinuationConfiguration(continuation, value); | 931 return new ValuePassingConfiguration(continuation, value); |
| 951 } | 932 } |
| 952 } | 933 } |
| 953 | 934 |
| 954 class NotContinuation extends ExpressionContinuation { | 935 class NotContinuation extends ExpressionContinuation { |
| 955 final ExpressionContinuation continuation; | 936 final ExpressionContinuation continuation; |
| 956 | 937 |
| 957 NotContinuation(this.continuation); | 938 NotContinuation(this.continuation); |
| 958 | 939 |
| 959 Configuration call(Value value) { | 940 Configuration call(Value value) { |
| 960 Value notValue = identical(Value.trueInstance, value) | 941 Value notValue = identical(Value.trueInstance, value) |
| 961 ? Value.falseInstance | 942 ? Value.falseInstance |
| 962 : Value.trueInstance; | 943 : Value.trueInstance; |
| 963 return new ContinuationConfiguration(continuation, notValue); | 944 return new ValuePassingConfiguration(continuation, notValue); |
| 964 } | 945 } |
| 965 } | 946 } |
| 966 | 947 |
| 967 class OrContinuation extends ExpressionContinuation { | 948 class OrContinuation extends ExpressionContinuation { |
| 968 final Expression right; | 949 final Expression right; |
| 969 final Environment environment; | 950 final Environment environment; |
| 970 final ExpressionContinuation continuation; | 951 final ExpressionContinuation continuation; |
| 971 | 952 |
| 972 OrContinuation(this.right, this.environment, this.continuation); | 953 OrContinuation(this.right, this.environment, this.continuation); |
| 973 | 954 |
| 974 Configuration call(Value left) { | 955 Configuration call(Value left) { |
| 975 return identical(Value.trueInstance, left) | 956 return identical(Value.trueInstance, left) |
| 976 ? new ContinuationConfiguration(continuation, Value.trueInstance) | 957 ? new ValuePassingConfiguration(continuation, Value.trueInstance) |
| 977 : new ExpressionConfiguration(right, environment, continuation); | 958 : new EvalConfiguration(right, environment, continuation); |
| 978 } | 959 } |
| 979 } | 960 } |
| 980 | 961 |
| 981 class AndContinuation extends ExpressionContinuation { | 962 class AndContinuation extends ExpressionContinuation { |
| 982 final Expression right; | 963 final Expression right; |
| 983 final Environment environment; | 964 final Environment environment; |
| 984 final ExpressionContinuation continuation; | 965 final ExpressionContinuation continuation; |
| 985 | 966 |
| 986 AndContinuation(this.right, this.environment, this.continuation); | 967 AndContinuation(this.right, this.environment, this.continuation); |
| 987 | 968 |
| 988 Configuration call(Value left) { | 969 Configuration call(Value left) { |
| 989 return identical(Value.falseInstance, left) | 970 return identical(Value.falseInstance, left) |
| 990 ? new ContinuationConfiguration(continuation, Value.falseInstance) | 971 ? new ValuePassingConfiguration(continuation, Value.falseInstance) |
| 991 : new ExpressionConfiguration(right, environment, continuation); | 972 : new EvalConfiguration(right, environment, continuation); |
| 992 } | 973 } |
| 993 } | 974 } |
| 994 | 975 |
| 995 class ConditionalContinuation extends ExpressionContinuation { | 976 class ConditionalContinuation extends ExpressionContinuation { |
| 996 final Expression then; | 977 final Expression then; |
| 997 final Expression otherwise; | 978 final Expression otherwise; |
| 998 final Environment environment; | 979 final Environment environment; |
| 999 final ExpressionContinuation continuation; | 980 final ExpressionContinuation continuation; |
| 1000 | 981 |
| 1001 ConditionalContinuation( | 982 ConditionalContinuation( |
| 1002 this.then, this.otherwise, this.environment, this.continuation); | 983 this.then, this.otherwise, this.environment, this.continuation); |
| 1003 | 984 |
| 1004 Configuration call(Value value) { | 985 Configuration call(Value value) { |
| 1005 return identical(Value.trueInstance, value) | 986 return identical(Value.trueInstance, value) |
| 1006 ? new ExpressionConfiguration(then, environment, continuation) | 987 ? new EvalConfiguration(then, environment, continuation) |
| 1007 : new ExpressionConfiguration(otherwise, environment, continuation); | 988 : new EvalConfiguration(otherwise, environment, continuation); |
| 1008 } | 989 } |
| 1009 } | 990 } |
| 1010 | 991 |
| 1011 class StringConcatenationContinuation extends ApplicationContinuation { | 992 class StringConcatenationContinuation extends ApplicationContinuation { |
| 1012 final ExpressionContinuation continuation; | 993 final ExpressionContinuation continuation; |
| 1013 | 994 |
| 1014 StringConcatenationContinuation(this.continuation); | 995 StringConcatenationContinuation(this.continuation); |
| 1015 | 996 |
| 1016 Configuration call(List<InterpreterValue> values) { | 997 Configuration call(List<InterpreterValue> values) { |
| 1017 StringBuffer result = new StringBuffer(); | 998 StringBuffer result = new StringBuffer(); |
| 1018 for (InterpreterValue v in values.reversed) { | 999 for (InterpreterValue v in values.reversed) { |
| 1019 result.write(v.value.value); | 1000 result.write(v.value.value); |
| 1020 } | 1001 } |
| 1021 return new ContinuationConfiguration( | 1002 return new ValuePassingConfiguration( |
| 1022 continuation, new StringValue(result.toString())); | 1003 continuation, new StringValue(result.toString())); |
| 1023 } | 1004 } |
| 1024 } | 1005 } |
| 1025 | 1006 |
| 1026 class LetContinuation extends ExpressionContinuation { | 1007 class LetContinuation extends ExpressionContinuation { |
| 1027 final VariableDeclaration variable; | 1008 final VariableDeclaration variable; |
| 1028 final Expression letBody; | 1009 final Expression letBody; |
| 1029 final Environment environment; | 1010 final Environment environment; |
| 1030 final ExpressionContinuation continuation; | 1011 final ExpressionContinuation continuation; |
| 1031 | 1012 |
| 1032 LetContinuation( | 1013 LetContinuation( |
| 1033 this.variable, this.letBody, this.environment, this.continuation); | 1014 this.variable, this.letBody, this.environment, this.continuation); |
| 1034 | 1015 |
| 1035 Configuration call(Value value) { | 1016 Configuration call(Value value) { |
| 1036 var letEnv = new Environment(environment); | 1017 var letEnv = new Environment(environment); |
| 1037 letEnv.expand(variable, value); | 1018 letEnv.expand(variable, value); |
| 1038 return new ExpressionConfiguration(letBody, letEnv, continuation); | 1019 return new EvalConfiguration(letBody, letEnv, continuation); |
| 1039 } | 1020 } |
| 1040 } | 1021 } |
| 1041 | 1022 |
| 1042 /// Represents the continuation for the condition expression in [WhileStatement]
. | 1023 /// Represents the continuation for the condition expression in [WhileStatement]
. |
| 1043 class WhileConditionContinuation extends ExpressionContinuation { | 1024 class WhileConditionContinuation extends ExpressionContinuation { |
| 1044 final WhileStatement node; | 1025 final WhileStatement node; |
| 1045 final State state; | 1026 final State state; |
| 1046 | 1027 |
| 1047 WhileConditionContinuation(this.node, this.state); | 1028 WhileConditionContinuation(this.node, this.state); |
| 1048 | 1029 |
| 1049 StatementConfiguration call(Value v) { | 1030 ExecConfiguration call(Value v) { |
| 1050 if (identical(v, Value.trueInstance)) { | 1031 if (identical(v, Value.trueInstance)) { |
| 1051 // Add configuration for the While statement to the linked list. | 1032 // Add configuration for the While statement to the linked list. |
| 1052 StatementConfiguration config = new StatementConfiguration(node, state); | 1033 ExecConfiguration config = new ExecConfiguration(node, state); |
| 1053 // Configuration for the body of the loop. | 1034 // Configuration for the body of the loop. |
| 1054 return new StatementConfiguration( | 1035 return new ExecConfiguration(node.body, state.withConfiguration(config)); |
| 1055 node.body, state.withConfiguration(config)); | |
| 1056 } | 1036 } |
| 1057 | 1037 |
| 1058 return state.statementConfiguration; | 1038 return state.statementConfiguration; |
| 1059 } | 1039 } |
| 1060 } | 1040 } |
| 1061 | 1041 |
| 1062 /// Represents the continuation for the condition expression in [IfStatement]. | 1042 /// Represents the continuation for the condition expression in [IfStatement]. |
| 1063 class IfConditionContinuation extends ExpressionContinuation { | 1043 class IfConditionContinuation extends ExpressionContinuation { |
| 1064 final Statement then; | 1044 final Statement then; |
| 1065 final Statement otherwise; | 1045 final Statement otherwise; |
| 1066 final State state; | 1046 final State state; |
| 1067 | 1047 |
| 1068 IfConditionContinuation(this.then, this.otherwise, this.state); | 1048 IfConditionContinuation(this.then, this.otherwise, this.state); |
| 1069 | 1049 |
| 1070 StatementConfiguration call(Value v) { | 1050 ExecConfiguration call(Value v) { |
| 1071 if (identical(v, Value.trueInstance)) { | 1051 if (identical(v, Value.trueInstance)) { |
| 1072 log.info("if-then\n"); | 1052 log.info("if-then\n"); |
| 1073 return new StatementConfiguration(then, state); | 1053 return new ExecConfiguration(then, state); |
| 1074 } else if (otherwise != null) { | 1054 } else if (otherwise != null) { |
| 1075 log.info("if-otherwise\n"); | 1055 log.info("if-otherwise\n"); |
| 1076 return new StatementConfiguration(otherwise, state); | 1056 return new ExecConfiguration(otherwise, state); |
| 1077 } | 1057 } |
| 1078 return state.statementConfiguration; | 1058 return state.statementConfiguration; |
| 1079 } | 1059 } |
| 1080 } | 1060 } |
| 1081 | 1061 |
| 1082 /// Represents the continuation for the initializer expression in | 1062 /// Represents the continuation for the initializer expression in |
| 1083 /// [VariableDeclaration]. | 1063 /// [VariableDeclaration]. |
| 1084 class VariableInitializerContinuation extends ExpressionContinuation { | 1064 class VariableInitializerContinuation extends ExpressionContinuation { |
| 1085 final VariableDeclaration variable; | 1065 final VariableDeclaration variable; |
| 1086 final Environment environment; | 1066 final Environment environment; |
| 1087 final StatementConfiguration nextConfiguration; | 1067 final ExecConfiguration nextConfiguration; |
| 1088 | 1068 |
| 1089 VariableInitializerContinuation( | 1069 VariableInitializerContinuation( |
| 1090 this.variable, this.environment, this.nextConfiguration); | 1070 this.variable, this.environment, this.nextConfiguration); |
| 1091 | 1071 |
| 1092 StatementConfiguration call(Value v) { | 1072 ExecConfiguration call(Value v) { |
| 1093 environment.expand(variable, v); | 1073 environment.expand(variable, v); |
| 1094 return nextConfiguration; | 1074 return nextConfiguration; |
| 1095 } | 1075 } |
| 1096 } | 1076 } |
| 1097 | 1077 |
| 1098 /// Executes statements. | 1078 /// Executes statements. |
| 1099 /// | 1079 /// |
| 1100 /// Execution of a statement completes in one of the following ways: | 1080 /// Execution of a statement completes in one of the following ways: |
| 1101 /// - it completes normally, in which case the execution proceeds to applying | 1081 /// - it completes normally, in which case the execution proceeds to applying |
| 1102 /// the next continuation | 1082 /// the next continuation |
| 1103 /// - it breaks with a label, in which case the corresponding continuation is | 1083 /// - it breaks with a label, in which case the corresponding continuation is |
| 1104 /// returned and applied | 1084 /// returned and applied |
| 1105 /// - it returns with or without value, TBD | 1085 /// - it returns with or without value, TBD |
| 1106 /// - it throws, TBD | 1086 /// - it throws, TBD |
| 1107 class StatementExecuter extends StatementVisitor1<Configuration, State> { | 1087 class StatementExecuter extends StatementVisitor1<Configuration, State> { |
| 1108 Evaluator evaluator = new Evaluator(); | 1088 Evaluator evaluator = new Evaluator(); |
| 1109 | 1089 |
| 1110 void trampolinedExecution(Configuration configuration) { | 1090 void trampolinedExecution(Configuration configuration) { |
| 1111 while (configuration != null) { | 1091 while (configuration != null) { |
| 1112 configuration = configuration.step(this); | 1092 configuration = configuration.step(this); |
| 1113 } | 1093 } |
| 1114 } | 1094 } |
| 1115 | 1095 |
| 1116 Configuration exec(Statement statement, State state) => | 1096 Configuration exec(Statement statement, State state) => |
| 1117 statement.accept1(this, state); | 1097 statement.accept1(this, state); |
| 1118 Configuration eval(Expression expression, ExpressionConfiguration config) => | 1098 Configuration eval(Expression expression, EvalConfiguration config) => |
| 1119 evaluator.eval(expression, config); | 1099 evaluator.eval(expression, config); |
| 1120 Configuration evalList( | 1100 Configuration evalList( |
| 1121 List<InterpreterExpression> es, Environment env, Continuation cont) => | 1101 List<InterpreterExpression> es, Environment env, Continuation cont) => |
| 1122 evaluator.evalList(es, env, cont); | 1102 evaluator.evalList(es, env, cont); |
| 1123 | 1103 |
| 1124 Configuration defaultStatement(Statement node, State state) { | 1104 Configuration defaultStatement(Statement node, State state) { |
| 1125 throw notImplemented( | 1105 throw notImplemented( |
| 1126 m: "Execution is not implemented for statement:\n$node "); | 1106 m: "Execution is not implemented for statement:\n$node "); |
| 1127 } | 1107 } |
| 1128 | 1108 |
| 1129 Configuration visitInvalidStatement(InvalidStatement node, State state) { | 1109 Configuration visitInvalidStatement(InvalidStatement node, State state) { |
| 1130 throw "Invalid statement at ${node.location}"; | 1110 throw "Invalid statement at ${node.location}"; |
| 1131 } | 1111 } |
| 1132 | 1112 |
| 1133 Configuration visitExpressionStatement( | 1113 Configuration visitExpressionStatement( |
| 1134 ExpressionStatement node, State state) { | 1114 ExpressionStatement node, State state) { |
| 1135 var cont = | 1115 var cont = |
| 1136 new ExpressionStatementContinuation(state.statementConfiguration); | 1116 new ExpressionStatementContinuation(state.statementConfiguration); |
| 1137 return new ExpressionConfiguration( | 1117 return new EvalConfiguration(node.expression, state.environment, cont); |
| 1138 node.expression, state.environment, cont); | |
| 1139 } | 1118 } |
| 1140 | 1119 |
| 1141 Configuration visitBlock(Block node, State state) { | 1120 Configuration visitBlock(Block node, State state) { |
| 1142 if (node.statements.isEmpty) { | 1121 if (node.statements.isEmpty) { |
| 1143 return state.statementConfiguration; | 1122 return state.statementConfiguration; |
| 1144 } | 1123 } |
| 1145 State blockState = | 1124 State blockState = |
| 1146 state.withEnvironment(new Environment(state.environment)); | 1125 state.withEnvironment(new Environment(state.environment)); |
| 1147 StatementConfiguration configuration = state.statementConfiguration; | 1126 ExecConfiguration configuration = state.statementConfiguration; |
| 1148 for (Statement s in node.statements.reversed) { | 1127 for (Statement s in node.statements.reversed) { |
| 1149 configuration = new StatementConfiguration( | 1128 configuration = |
| 1150 s, blockState.withConfiguration(configuration)); | 1129 new ExecConfiguration(s, blockState.withConfiguration(configuration)); |
| 1151 } | 1130 } |
| 1152 return configuration; | 1131 return configuration; |
| 1153 } | 1132 } |
| 1154 | 1133 |
| 1155 Configuration visitEmptyStatement(EmptyStatement node, State state) { | 1134 Configuration visitEmptyStatement(EmptyStatement node, State state) { |
| 1156 return state.statementConfiguration; | 1135 return state.statementConfiguration; |
| 1157 } | 1136 } |
| 1158 | 1137 |
| 1159 Configuration visitIfStatement(IfStatement node, State state) { | 1138 Configuration visitIfStatement(IfStatement node, State state) { |
| 1160 var cont = new IfConditionContinuation(node.then, node.otherwise, state); | 1139 var cont = new IfConditionContinuation(node.then, node.otherwise, state); |
| 1161 | 1140 |
| 1162 return new ExpressionConfiguration(node.condition, state.environment, cont); | 1141 return new EvalConfiguration(node.condition, state.environment, cont); |
| 1163 } | 1142 } |
| 1164 | 1143 |
| 1165 Configuration visitLabeledStatement(LabeledStatement node, State state) { | 1144 Configuration visitLabeledStatement(LabeledStatement node, State state) { |
| 1166 return new StatementConfiguration(node.body, state.withBreak(node)); | 1145 return new ExecConfiguration(node.body, state.withBreak(node)); |
| 1167 } | 1146 } |
| 1168 | 1147 |
| 1169 Configuration visitBreakStatement(BreakStatement node, State state) { | 1148 Configuration visitBreakStatement(BreakStatement node, State state) { |
| 1170 return state.lookupLabel(node.target).configuration; | 1149 return state.lookupLabel(node.target).configuration; |
| 1171 } | 1150 } |
| 1172 | 1151 |
| 1173 Configuration visitWhileStatement(WhileStatement node, State state) { | 1152 Configuration visitWhileStatement(WhileStatement node, State state) { |
| 1174 var cont = new WhileConditionContinuation(node, state); | 1153 var cont = new WhileConditionContinuation(node, state); |
| 1175 | 1154 |
| 1176 return new ExpressionConfiguration(node.condition, state.environment, cont); | 1155 return new EvalConfiguration(node.condition, state.environment, cont); |
| 1177 } | 1156 } |
| 1178 | 1157 |
| 1179 Configuration visitDoStatement(DoStatement node, State state) { | 1158 Configuration visitDoStatement(DoStatement node, State state) { |
| 1180 WhileStatement whileStatement = | 1159 WhileStatement whileStatement = |
| 1181 new WhileStatement(node.condition, node.body); | 1160 new WhileStatement(node.condition, node.body); |
| 1182 StatementConfiguration configuration = | 1161 ExecConfiguration configuration = |
| 1183 new StatementConfiguration(whileStatement, state); | 1162 new ExecConfiguration(whileStatement, state); |
| 1184 | 1163 |
| 1185 return new StatementConfiguration( | 1164 return new ExecConfiguration( |
| 1186 node.body, state.withConfiguration(configuration)); | 1165 node.body, state.withConfiguration(configuration)); |
| 1187 } | 1166 } |
| 1188 | 1167 |
| 1189 Configuration visitReturnStatement(ReturnStatement node, State state) { | 1168 Configuration visitReturnStatement(ReturnStatement node, State state) { |
| 1190 assert(state.returnContinuation != null); | 1169 assert(state.returnContinuation != null); |
| 1191 log.info('return\n'); | 1170 log.info('return\n'); |
| 1192 if (node.expression == null) { | 1171 if (node.expression == null) { |
| 1193 return new ContinuationConfiguration( | 1172 return new ValuePassingConfiguration( |
| 1194 state.returnContinuation, Value.nullInstance); | 1173 state.returnContinuation, Value.nullInstance); |
| 1195 } | 1174 } |
| 1196 | 1175 |
| 1197 return new ExpressionConfiguration( | 1176 return new EvalConfiguration( |
| 1198 node.expression, state.environment, state.returnContinuation); | 1177 node.expression, state.environment, state.returnContinuation); |
| 1199 } | 1178 } |
| 1200 | 1179 |
| 1201 Configuration visitVariableDeclaration( | 1180 Configuration visitVariableDeclaration( |
| 1202 VariableDeclaration node, State state) { | 1181 VariableDeclaration node, State state) { |
| 1203 if (node.initializer != null) { | 1182 if (node.initializer != null) { |
| 1204 var cont = new VariableInitializerContinuation( | 1183 var cont = new VariableInitializerContinuation( |
| 1205 node, state.environment, state.statementConfiguration); | 1184 node, state.environment, state.statementConfiguration); |
| 1206 return new ExpressionConfiguration( | 1185 return new EvalConfiguration(node.initializer, state.environment, cont); |
| 1207 node.initializer, state.environment, cont); | |
| 1208 } | 1186 } |
| 1209 state.environment.expand(node, Value.nullInstance); | 1187 state.environment.expand(node, Value.nullInstance); |
| 1210 return state.statementConfiguration; | 1188 return state.statementConfiguration; |
| 1211 } | 1189 } |
| 1212 } | 1190 } |
| 1213 | 1191 |
| 1214 // ------------------------------------------------------------------------ | 1192 // ------------------------------------------------------------------------ |
| 1215 // VALUES | 1193 // VALUES |
| 1216 // ------------------------------------------------------------------------ | 1194 // ------------------------------------------------------------------------ |
| 1217 | 1195 |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1498 void _initializeNullFields(Class class_, ObjectValue newObject) { | 1476 void _initializeNullFields(Class class_, ObjectValue newObject) { |
| 1499 int superClassSize = class_.superclass?.instanceSize ?? 0; | 1477 int superClassSize = class_.superclass?.instanceSize ?? 0; |
| 1500 for (int i = superClassSize; i < class_.instanceSize; i++) { | 1478 for (int i = superClassSize; i < class_.instanceSize; i++) { |
| 1501 Field field = class_.instanceFields[i]; | 1479 Field field = class_.instanceFields[i]; |
| 1502 if (class_.getProperty(newObject, field) == null) { | 1480 if (class_.getProperty(newObject, field) == null) { |
| 1503 assert(field.initializer == null); | 1481 assert(field.initializer == null); |
| 1504 class_.setProperty(newObject, field, Value.nullInstance); | 1482 class_.setProperty(newObject, field, Value.nullInstance); |
| 1505 } | 1483 } |
| 1506 } | 1484 } |
| 1507 } | 1485 } |
| OLD | NEW |