Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix variable unboxing issue, move all unboxing to single method #827

Merged
merged 1 commit into from
Aug 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions experiments/modern/src/maki-interpreter/virtualMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ function coerceTypes(var1, var2, val1, val2) {
function* interpret(start, program, stack = []) {
const { commands, methods, variables, classes } = program;

function popStackValue() {
const v = stack.pop();
return v instanceof Variable ? v.getValue() : v;
}

function twoArgCoercingOperator(operator) {
const a = stack.pop();
const b = stack.pop();
Expand All @@ -33,10 +38,8 @@ function* interpret(start, program, stack = []) {
}

function twoArgOperator(operator) {
const a = stack.pop();
const b = stack.pop();
const aValue = a instanceof Variable ? a.getValue() : a;
const bValue = b instanceof Variable ? b.getValue() : b;
const aValue = popStackValue();
const bValue = popStackValue();

stack.push(operator(bValue, aValue));
}
Expand Down Expand Up @@ -65,8 +68,7 @@ function* interpret(start, program, stack = []) {
}
// popTo
case 3: {
const a = stack.pop();
let aValue = a instanceof Variable ? a.getValue() : a;
const aValue = popStackValue();
const offsetIntoVariables = command.arg;
const toVar = variables[offsetIntoVariables];
toVar.setValue(aValue);
Expand Down Expand Up @@ -104,7 +106,7 @@ function* interpret(start, program, stack = []) {
}
// jumpIf
case 16: {
const value = stack.pop();
const value = popStackValue();
// This seems backwards. Seems like we're doing a "jump if not"
if (value) {
break;
Expand All @@ -114,7 +116,7 @@ function* interpret(start, program, stack = []) {
}
// jumpIfNot
case 17: {
const value = stack.pop();
const value = popStackValue();
// This seems backwards. Same as above
if (!value) {
break;
Expand Down Expand Up @@ -148,12 +150,10 @@ function* interpret(start, program, stack = []) {

const methodArgs = [];
while (argCount--) {
const a = stack.pop();
const aValue = a instanceof Variable ? a.getValue() : a;
const aValue = popStackValue();
methodArgs.push(aValue);
}
const variable = stack.pop();
const obj = variable instanceof Variable ? variable.getValue() : variable;
const obj = popStackValue();
stack.push(obj[methodName](...methodArgs));
break;
}
Expand All @@ -169,8 +169,7 @@ function* interpret(start, program, stack = []) {
}
// return
case 33: {
const a = stack.pop();
const aValue = a instanceof Variable ? a.getValue() : a;
const aValue = popStackValue();
return aValue;
}
// mov
Expand Down Expand Up @@ -262,15 +261,13 @@ function* interpret(start, program, stack = []) {
}
// ! (not)
case 74: {
const a = stack.pop();
const aValue = a instanceof Variable ? a.getValue() : a;
const aValue = popStackValue();
stack.push(aValue ? 0 : 1);
break;
}
// - (negative)
case 76: {
const a = stack.pop();
const aValue = a instanceof Variable ? a.getValue() : a;
const aValue = popStackValue();
stack.push(-aValue);
break;
}
Expand Down