From 9a50a92466df28a3e50d19299fa728693c7c20c8 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Tue, 23 Jul 2019 23:44:21 -0700 Subject: [PATCH] Clean up and document the generator approach --- .../modern/src/maki-interpreter/virtualMachine.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/experiments/modern/src/maki-interpreter/virtualMachine.js b/experiments/modern/src/maki-interpreter/virtualMachine.js index 5919d682..f81a3685 100644 --- a/experiments/modern/src/maki-interpreter/virtualMachine.js +++ b/experiments/modern/src/maki-interpreter/virtualMachine.js @@ -41,14 +41,15 @@ function* interpret(start, program, stack = []) { stack.push(operator(bValue, aValue)); } - let i = start; - while (i < commands.length) { - const command = commands[i]; + // Instruction Pointer + let ip = start; + while (ip < commands.length) { + const command = commands[ip]; // This probably incurrs some perf cost. If it does, we can pass in a flag // to enable it only when we are debugging. When we are not, (in prod) we // can just jump right over it and we will execute straight through to the // return value on the first call to `.next()`. - yield { i, command, stack, variables, commands }; + yield { i: ip, command, stack, variables, commands }; switch (command.opcode) { // push @@ -290,7 +291,7 @@ function* interpret(start, program, stack = []) { throw new Error(`Unhandled opcode ${command.opcode}`); } - i++; + ip++; } }