basica
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -51,5 +51,14 @@ module.exports = Machine; | ||
} else { | ||
this.evalStatements(parsed.statements); | ||
// TODO: this is all wrong; needs to be turned into a Program and executed. | ||
// (so that immediate code can support WHILE, FOR) | ||
for (var i = 0; i < parsed.statements.length; ++i) { | ||
this.execStatement(parsed.statements[i]); | ||
// if we're running after executing a statement it means | ||
// a jump was encountered. | ||
if (this.running) { | ||
break; | ||
} | ||
} | ||
if (!this.running) { | ||
this.newCommand(); | ||
this.newCommand(); | ||
} | ||
@@ -93,4 +102,2 @@ } | ||
this.jumped = true; | ||
if (!this.running) { | ||
@@ -114,18 +121,19 @@ this.start(); | ||
if (!this.running) { | ||
this.newCommand(); | ||
return; | ||
var batch = 20; | ||
while (this.running && batch--) { | ||
var stmt = this.program.getStatementAtIndex(this.pc++); | ||
if (!stmt) { | ||
this.running = false; | ||
} else { | ||
this.execStatement(stmt); | ||
} | ||
} | ||
this.jumped = false; | ||
var stmts = this.program.getLineAtIndex(this.pc++); | ||
if (!stmts) { | ||
this.running = false; | ||
if (this.running) { | ||
setTimeout(_tick, 0); | ||
} else { | ||
this.evalStatements(stmts); | ||
this.newCommand(); | ||
} | ||
setTimeout(_tick, 0); | ||
}.bind(this); | ||
@@ -149,10 +157,5 @@ | ||
Machine.prototype.evalStatements = function(stmts) { | ||
try { | ||
for (var i = 0; i < stmts.length; ++i) { | ||
this.evalStatement(stmts[i]); | ||
if (this.jumped) { | ||
return; | ||
} | ||
} | ||
Machine.prototype.execStatement = function(stmt) { | ||
try { | ||
this.evalStatement(stmt); | ||
} catch (e) { | ||
@@ -159,0 +162,0 @@ if (e === E.SYNTAX_ERROR || e === E.TYPE_MISMATCH || e === E.NO_SUCH_LINE) { |
@@ -5,4 +5,7 @@ module.exports = Program; | ||
var splice = Array.prototype.splice; | ||
function Program() { | ||
this.lines = []; | ||
this.numbers = []; | ||
this.statements = []; | ||
this.lineMax = -1; | ||
@@ -16,20 +19,33 @@ this.index = null; | ||
if (number > this.lineMax) { | ||
this.lines.push([number, statements]); | ||
this._insertLineAtIndex(this.statements.length, 0, number, statements); | ||
this.lineMax = number; | ||
} else if (number === this.lineMax) { | ||
this.lines[this.lines.length-1][1] = statements; | ||
} else { | ||
// TODO: binary search! | ||
for (var i = 0; i < this.lines.length; ++i) { | ||
if (number === this.lines[i][0]) { | ||
this.lines[i][1] = statements; | ||
var i = 0, remove = 0; | ||
while (i < this.numbers.length) { | ||
if (this.numbers[i] === number) { | ||
var j = i + 1; | ||
while (j < this.numbers.length && this.numbers[j] === null) ++j; | ||
remove = j - i; | ||
break; | ||
} else if (number < this.lines[i][0]) { | ||
this.lines.splice(i, 0, [number, statements]); | ||
} else if (number < this.numbers[i]) { | ||
break; | ||
} | ||
i++; | ||
} | ||
this._insertLineAtIndex(i, remove, number, statements); | ||
} | ||
console.log(this); | ||
} | ||
Program.prototype._insertLineAtIndex = function(ix, remove, lineNumber, statements) { | ||
var numbers = statements.map(function(s) { return null; }); | ||
numbers[0] = lineNumber; | ||
splice.apply(this.numbers, [ix, remove].concat(numbers)); | ||
splice.apply(this.statements, [ix, remove].concat(statements)); | ||
} | ||
Program.prototype.indexOfLine = function(line) { | ||
@@ -43,5 +59,4 @@ var ix = this.index[line]; | ||
Program.prototype.getLineAtIndex = function(index) { | ||
var line = this.lines[index]; | ||
return line ? line[1] : null; | ||
Program.prototype.getStatementAtIndex = function(index) { | ||
return this.statements[index]; | ||
} | ||
@@ -52,4 +67,4 @@ | ||
this.index = {}; | ||
this.lines.forEach(function(ln, ix) { | ||
this.index[ln[0]] = ix; | ||
this.numbers.forEach(function(ln, ix) { | ||
if (ln) this.index[ln] = ix; | ||
}, this); | ||
@@ -56,0 +71,0 @@ |
{ | ||
"name": "basica", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "BASIC interpreter", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,2 +16,2 @@ # basica | ||
* WHILE | ||
* expression parsing | ||
* <del>expression parsing</del> |
Sorry, the diff of this file is too big to display
232264
6845