Struct functions (at least) actually work

This commit is contained in:
Mitchell Plamann
2015-04-06 02:56:54 -04:00
parent be1848a055
commit 00bf6e8243
2 changed files with 16 additions and 1 deletions

View File

@@ -78,6 +78,8 @@ ProgramState.prototype.push = function(val) {
} }
ProgramState.prototype.pop = function() { ProgramState.prototype.pop = function() {
if (this.frame.stack === [])
throw "Tried to pop from an empty stack!";
return this.frame.stack.pop(); return this.frame.stack.pop();
} }
@@ -372,7 +374,7 @@ ProgramState.prototype.step = function() {
// Read offset into a struct // Read offset into a struct
var offset = this.frame.program[this.frame.pc + 1]; var offset = this.frame.program[this.frame.pc + 1];
var index = this.pop(); var index = this.pop();
this.push(this.heap[index + offset]); this.push(index + offset);
this.frame.pc += 2; this.frame.pc += 2;
break; break;
@@ -407,6 +409,7 @@ ProgramState.prototype.step = function() {
for (var i = 0; i < 4; i++) for (var i = 0; i < 4; i++)
this.heap[addr + i] = array[i]; this.heap[addr + i] = array[i];
this.frame.pc++; this.frame.pc++;
break;
case op.AMLOAD: case op.AMLOAD:
var addr = this.pop(); var addr = this.pop();
@@ -460,6 +463,16 @@ function execute(file, callbacks, v) {
var val = state.step(); var val = state.step();
if (val !== undefined) return val; if (val !== undefined) return val;
if (verbose) {
console.log("Machine state:");
console.log(" Current Stack Frame:");
console.log(" Stack: " + state.frame.stack);
console.log(" PC: " + state.frame.pc);
console.log(" Vars: " + state.frame.variables);
console.log(" Code: " + state.frame.program);
console.log(" Heap: " + state.heap);
}
// if (at_breakpoint) { // if (at_breakpoint) {
// save state (maybe in a global in this file?) // save state (maybe in a global in this file?)
// return; // return;

View File

@@ -13,9 +13,11 @@ c0ffi = require("./c0ffi.js");
callbacks = {}; callbacks = {};
callbacks[c0ffi.NATIVE_PRINT] = function(args) { callbacks[c0ffi.NATIVE_PRINT] = function(args) {
console.log("Print function says: " + args[0]); console.log("Print function says: " + args[0]);
return 0;
} }
callbacks[c0ffi.NATIVE_PRINTINT] = function(args) { callbacks[c0ffi.NATIVE_PRINTINT] = function(args) {
console.log("Printint function says: " + args[0]); console.log("Printint function says: " + args[0]);
return 0;
} }
console.log(callbacks); console.log(callbacks);