2012-02-09 03:13:44 +00:00
|
|
|
//
|
2013-08-22 06:07:46 +00:00
|
|
|
// BASIC Compiler and Executor
|
2012-02-09 03:13:44 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
// API:
|
|
|
|
// var program = basic.compile(source);
|
|
|
|
// // may throw basic.ParseError
|
|
|
|
//
|
|
|
|
// program.init({tty: ..., hires: .e.., lores: ...})
|
|
|
|
//
|
|
|
|
// // if TTY input is blocking:
|
|
|
|
//
|
|
|
|
// var state;
|
|
|
|
// do {
|
|
|
|
// state = program.step();
|
|
|
|
// // may throw basic.RuntimeError
|
|
|
|
// } while (state !== basic.STATE_STOPPED);
|
|
|
|
//
|
|
|
|
// // if TTY input is non-blocking:
|
|
|
|
// function driver() {
|
|
|
|
// var state;
|
|
|
|
// do {
|
|
|
|
// state = program.step(driver);
|
|
|
|
// // may throw basic.RuntimeError
|
|
|
|
// } while (state === basic.STATE_RUNNING);
|
|
|
|
// }
|
|
|
|
// driver(); // step until done or blocked
|
|
|
|
// // driver will also be called after input is unblocked
|
|
|
|
// // driver may want to yield via setTimeout() after N steps
|
|
|
|
|
2013-08-17 04:00:47 +00:00
|
|
|
this.basic = (function() {
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
var basic = {
|
|
|
|
STATE_STOPPED: 0,
|
|
|
|
STATE_RUNNING: 1,
|
|
|
|
STATE_BLOCKED: 2
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Thrown if parsing fails
|
|
|
|
//
|
|
|
|
basic.ParseError = function(msg, line, column) {
|
|
|
|
this.name = 'ParseError';
|
|
|
|
this.message = msg || '';
|
|
|
|
this.line = line;
|
|
|
|
this.column = column;
|
|
|
|
};
|
|
|
|
basic.ParseError.prototype = new Error();
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Thrown when a program is running; can be caught by ONERR
|
|
|
|
//
|
|
|
|
basic.RuntimeError = function(msg, code) {
|
|
|
|
this.name = 'RuntimeError';
|
|
|
|
this.message = msg;
|
|
|
|
this.code = code;
|
|
|
|
};
|
|
|
|
basic.RuntimeError.prototype = new Error();
|
|
|
|
|
|
|
|
function runtime_error(msg) {
|
|
|
|
if (typeof msg === 'object' && msg.length && msg.length >= 2) {
|
|
|
|
throw new basic.RuntimeError(msg[1], msg[0]);
|
|
|
|
} else {
|
|
|
|
throw new basic.RuntimeError(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ERRORS = {
|
|
|
|
NEXT_WITHOUT_FOR: [0, "Next without for"],
|
|
|
|
SYNTAX_ERROR: [16, "Syntax error"],
|
|
|
|
RETURN_WITHOUT_GOSUB: [22, "Return without gosub"],
|
|
|
|
OUT_OF_DATA: [42, "Out of data"],
|
|
|
|
ILLEGAL_QUANTITY: [53, "Illegal quantity"],
|
|
|
|
OVERFLOW: [69, "Overflow"],
|
|
|
|
OUT_OF_MEMORY: [77, "Out of memory"],
|
|
|
|
UNDEFINED_STATEMENT: [90, "Undefined statement"],
|
|
|
|
BAD_SUBSCRIPT: [107, "Bad subscript"],
|
|
|
|
REDIMED_ARRAY: [120, "Redimensioned array"],
|
|
|
|
DIVISION_BY_ZERO: [133, "Division by zero"],
|
|
|
|
TYPE_MISMATCH: [163, "Type mismatch"],
|
|
|
|
STRING_TOO_LONG: [176, "String too long"],
|
|
|
|
FORMULA_TOO_COMPLEX: [191, "Formula too complex"],
|
|
|
|
UNDEFINED_FUNCTION: [224, "Undefined function"],
|
|
|
|
REENTER: [254, "Re-enter"],
|
|
|
|
INTERRUPT: [255, "Break"]
|
|
|
|
};
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
// Keyword table - these can be altered (e.g. localized) here
|
|
|
|
var kws = {
|
|
|
|
ABS: "ABS",
|
|
|
|
AND: "AND",
|
|
|
|
ASC: "ASC",
|
|
|
|
ATN: "ATN",
|
|
|
|
AT: "AT",
|
|
|
|
CALL: "CALL",
|
|
|
|
CHR$: "CHR$",
|
|
|
|
CLEAR: "CLEAR",
|
|
|
|
COLOR: "COLOR=",
|
|
|
|
CONT: "CONT",
|
|
|
|
COS: "COS",
|
|
|
|
DATA: "DATA",
|
|
|
|
DEF: "DEF",
|
|
|
|
DEL: "DEL",
|
|
|
|
DIM: "DIM",
|
|
|
|
DRAW: "DRAW",
|
|
|
|
END: "END",
|
|
|
|
EXP: "EXP",
|
|
|
|
FLASH: "FLASH",
|
|
|
|
FN: "FN",
|
|
|
|
FOR: "FOR",
|
|
|
|
FRE: "FRE",
|
|
|
|
GET: "GET",
|
|
|
|
GOSUB: "GOSUB",
|
|
|
|
GOTO: "GOTO",
|
|
|
|
GR: "GR",
|
|
|
|
HCOLOR: "HCOLOR=",
|
|
|
|
HGR2: "HGR2",
|
|
|
|
HGR: "HGR",
|
|
|
|
HIMEM: "HIMEM:",
|
|
|
|
HLIN: "HLIN",
|
|
|
|
HOME: "HOME",
|
|
|
|
HPLOT: "HPLOT",
|
|
|
|
HTAB: "HTAB",
|
|
|
|
IF: "IF",
|
|
|
|
IN: "IN#",
|
|
|
|
INPUT: "INPUT",
|
|
|
|
INT: "INT",
|
|
|
|
INVERSE: "INVERSE",
|
|
|
|
LEFT$: "LEFT$",
|
|
|
|
LEN: "LEN",
|
|
|
|
LET: "LET",
|
|
|
|
LIST: "LIST",
|
|
|
|
LOAD: "LOAD",
|
|
|
|
LOG: "LOG",
|
|
|
|
LOMEM: "LOMEM:",
|
|
|
|
MID$: "MID$",
|
|
|
|
NEW: "NEW",
|
|
|
|
NEXT: "NEXT",
|
|
|
|
NORMAL: "NORMAL",
|
|
|
|
NOTRACE: "NOTRACE",
|
|
|
|
NOT: "NOT",
|
|
|
|
ONERR: "ONERR",
|
|
|
|
ON: "ON",
|
|
|
|
OR: "OR",
|
|
|
|
PDL: "PDL",
|
|
|
|
PEEK: "PEEK",
|
|
|
|
PLOT: "PLOT",
|
|
|
|
POKE: "POKE",
|
|
|
|
POP: "POP",
|
|
|
|
POS: "POS",
|
|
|
|
PRINT: "PRINT",
|
|
|
|
PR: "PR#",
|
|
|
|
READ: "READ",
|
|
|
|
RECALL: "RECALL",
|
|
|
|
REM: "REM",
|
|
|
|
RESTORE: "RESTORE",
|
|
|
|
RESUME: "RESUME",
|
|
|
|
RETURN: "RETURN",
|
|
|
|
RIGHT$: "RIGHT$",
|
|
|
|
RND: "RND",
|
|
|
|
ROT: "ROT=",
|
|
|
|
RUN: "RUN",
|
|
|
|
SAVE: "SAVE",
|
|
|
|
SCALE: "SCALE=",
|
|
|
|
SCRN: "SCRN",
|
|
|
|
SGN: "SGN",
|
|
|
|
SHLOAD: "SHLOAD",
|
|
|
|
SIN: "SIN",
|
|
|
|
SPC: "SPC",
|
|
|
|
SPEED: "SPEED=",
|
|
|
|
SQR: "SQR",
|
|
|
|
STEP: "STEP",
|
|
|
|
STOP: "STOP",
|
|
|
|
STORE: "STORE",
|
|
|
|
STR$: "STR$",
|
|
|
|
TAB: "TAB",
|
|
|
|
TAN: "TAN",
|
|
|
|
TEXT: "TEXT",
|
|
|
|
THEN: "THEN",
|
|
|
|
TO: "TO",
|
|
|
|
TRACE: "TRACE",
|
|
|
|
USR: "USR",
|
|
|
|
VAL: "VAL",
|
|
|
|
VLIN: "VLIN",
|
|
|
|
VTAB: "VTAB",
|
|
|
|
WAIT: "WAIT",
|
|
|
|
XDRAW: "XDRAW",
|
|
|
|
AMPERSAND: "&",
|
|
|
|
QUESTION: "?",
|
|
|
|
HSCRN: "HSCRN"
|
|
|
|
};
|
|
|
|
|
2012-02-09 03:13:44 +00:00
|
|
|
//
|
|
|
|
// Runtime flow control
|
|
|
|
//
|
|
|
|
function EndProgram() { }
|
|
|
|
function GoToLine(n) { this.line = n; }
|
|
|
|
function NextLine() { }
|
|
|
|
function BlockingInput(method, callback) {
|
|
|
|
this.method = method;
|
|
|
|
this.callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adapted from:
|
|
|
|
// http://stackoverflow.com/questions/424292/how-to-create-my-own-javascript-random-number-generator-that-i-can-also-set-the-s
|
|
|
|
function PRNG() {
|
|
|
|
var S = 2345678901, // seed
|
|
|
|
A = 48271, // const
|
|
|
|
M = 2147483647, // const
|
|
|
|
Q = M / A, // const
|
|
|
|
R = M % A; // const
|
|
|
|
|
|
|
|
this.next = function PRNG_next() {
|
|
|
|
var hi = S / Q,
|
|
|
|
lo = S % Q,
|
|
|
|
t = A * lo - R * hi;
|
|
|
|
S = (t > 0) ? t : t + M;
|
|
|
|
this.last = S / M;
|
|
|
|
return this.last;
|
|
|
|
};
|
|
|
|
this.seed = function PRNG_seed(x) {
|
|
|
|
S = Math.floor(Math.abs(x));
|
|
|
|
};
|
|
|
|
this.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multidimensional array, with auto-dimensioning on first access
|
|
|
|
function BASICArray(type, dims) {
|
|
|
|
|
|
|
|
var array, dimensions;
|
|
|
|
|
|
|
|
function offset(dims, subscripts) {
|
|
|
|
if (subscripts.length !== dimensions.length) {
|
|
|
|
runtime_error(ERRORS.BAD_SUBSCRIPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
var k, l, s = 0, p, ss;
|
|
|
|
for (k = 0; k < dims.length; k += 1) {
|
|
|
|
|
|
|
|
ss = subscripts[k];
|
|
|
|
if (ss < 0) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
ss = ss >> 0;
|
|
|
|
if (ss >= dims[k]) {
|
|
|
|
runtime_error(ERRORS.BAD_SUBSCRIPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
p = 1;
|
|
|
|
for (l = k + 1; l < dims.length; l += 1) {
|
|
|
|
p *= dims[l];
|
|
|
|
}
|
|
|
|
s += p * ss;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dim = function dim(dims) {
|
|
|
|
if (array) {
|
|
|
|
runtime_error(ERRORS.REDIMED_ARRAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
dimensions = dims.map(function(n) { return (Number(n) >> 0) + 1; });
|
|
|
|
|
|
|
|
var i, len = dimensions.reduce(function(a, b) { return a * b; }),
|
2013-08-17 02:58:09 +00:00
|
|
|
defval = (type === 'string') ? '' : 0;
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
array = [];
|
|
|
|
for (i = 0; i < len; i += 1) {
|
|
|
|
array[i] = defval;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
this.get = function get(subscripts) {
|
2012-02-09 03:13:44 +00:00
|
|
|
if (!array) {
|
|
|
|
this.dim(subscripts.map(function() { return 10; }));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return array[offset(dimensions, subscripts)];
|
|
|
|
};
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
this.set = function set(subscripts, value) {
|
2012-02-09 03:13:44 +00:00
|
|
|
if (!array) {
|
|
|
|
this.dim(subscripts.map(function() { return 10; }));
|
|
|
|
}
|
|
|
|
|
|
|
|
array[offset(dimensions, subscripts)] = value;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.toJSON = function toJSON() {
|
|
|
|
return { type: type, dimensions: dimensions, array: array };
|
|
|
|
};
|
|
|
|
|
|
|
|
if (dims) {
|
|
|
|
this.dim(dims);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream API, for parsing source and INPUT entry
|
|
|
|
function Stream(string) {
|
|
|
|
this.line = 0;
|
|
|
|
this.column = 0;
|
|
|
|
|
|
|
|
this.match = function match(re) {
|
|
|
|
var m = string.match(re), lines;
|
|
|
|
if (m) {
|
|
|
|
string = string.substring(m[0].length);
|
|
|
|
lines = m[0].split('\n');
|
|
|
|
if (lines.length > 1) {
|
|
|
|
this.line += lines.length - 1;
|
|
|
|
this.column = lines[lines.length - 1].length;
|
|
|
|
} else {
|
|
|
|
this.column += m[0].length;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastMatch = m;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
return (void 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.eof = function eof() {
|
|
|
|
return string.length === 0;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used for DATA (compile-time) and INPUT (runtime)
|
|
|
|
// Parses source string for optionally quoted, comma-
|
|
|
|
// separated values and adds them to items. Returns
|
|
|
|
// the substring consumed.
|
|
|
|
var parseDataInput = (function() {
|
|
|
|
|
2013-08-17 01:38:29 +00:00
|
|
|
var regexWhitespace = /^[ \t]+/,
|
|
|
|
regexQuotedString = /^"([^"]*?)"/,
|
2013-08-17 02:53:09 +00:00
|
|
|
regexUnquotedString = /^[^:,\r\n]*/,
|
2013-08-17 01:38:29 +00:00
|
|
|
regexComma = /^,/;
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
return function parseDataInput(stream, items) {
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
stream.match(regexWhitespace);
|
|
|
|
|
|
|
|
if (stream.match(regexQuotedString)) {
|
|
|
|
// quoted string
|
|
|
|
items.push(stream.lastMatch[1]);
|
|
|
|
} else if (stream.match(regexUnquotedString)) {
|
|
|
|
// unquoted string
|
2013-08-17 02:53:09 +00:00
|
|
|
items.push(stream.lastMatch[0]);
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
} while (stream.match(regexComma));
|
|
|
|
};
|
|
|
|
} ());
|
|
|
|
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
basic.compile = function compile(source) {
|
2012-02-09 03:13:44 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function vartype(name) {
|
|
|
|
var s = name.charAt(name.length - 1);
|
|
|
|
return s === '$' ? 'string' : s === '%' ? 'int' : 'float';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Runtime Environment (bound to compiled program)
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
var env, // Environment - passed in to program, contains tty, graphics, etc.
|
2013-08-17 02:58:09 +00:00
|
|
|
state, // Program state - initialized at runtime
|
|
|
|
lib, // Statement Library (closure over state and env)
|
|
|
|
funlib, // Function Library (closure over state and env)
|
|
|
|
peek_table, // Native memory access shims (PEEK, POKE, CALL)
|
|
|
|
poke_table,
|
|
|
|
call_table;
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// NOTE: tempting to make these part of env but some access/modify program state,
|
|
|
|
// e.g. onerr
|
|
|
|
//
|
|
|
|
peek_table = {
|
|
|
|
// Text window
|
|
|
|
0x0020: function() { return env.tty.textWindow ? env.tty.textWindow.left : 0; },
|
|
|
|
0x0021: function() { return env.tty.textWindow ? env.tty.textWindow.width : 80; },
|
|
|
|
0x0022: function() { return env.tty.textWindow ? env.tty.textWindow.top : 0; },
|
|
|
|
0x0023: function() { return env.tty.textWindow ? env.tty.textWindow.top + env.tty.textWindow.height : 24; },
|
|
|
|
0x0024: function() { return env.tty.getCursorPosition().x; },
|
|
|
|
0x0025: function() { return env.tty.getCursorPosition().y; },
|
|
|
|
|
|
|
|
// Random number field
|
|
|
|
0x004e: function() { return (Math.random() * 256) & 0xff; },
|
|
|
|
0x004f: function() { return (Math.random() * 256) & 0xff; },
|
|
|
|
|
|
|
|
// Last error code
|
|
|
|
0x00de: function() { return state.onerr_code; },
|
|
|
|
|
|
|
|
// Hires Plotting Page (32=1, 64=2, 96=3)
|
|
|
|
0x00e6: function() { return env.display ? (env.display.hires_plotting_page === 2 ? 64 : 32) : 0; },
|
|
|
|
|
|
|
|
// TODO: 0x3D0 = 0x4C if DOS is present.
|
|
|
|
|
|
|
|
// Keyboard
|
|
|
|
0xC000: function() { return env.tty.getKeyboardRegister ? env.tty.getKeyboardRegister() : 0; },
|
|
|
|
0xC010: function() { return env.tty.clearKeyboardStrobe ? env.tty.clearKeyboardStrobe() : 0; },
|
|
|
|
|
|
|
|
// Speaker toggle
|
|
|
|
0xC030: function() { return 0; },
|
|
|
|
|
|
|
|
// Buttons
|
|
|
|
0xC060: function() { return env.tty.getButtonState ? env.tty.getButtonState(3) : 0; },
|
|
|
|
0xC061: function() { return env.tty.getButtonState ? env.tty.getButtonState(0) : 0; },
|
|
|
|
0xC062: function() { return env.tty.getButtonState ? env.tty.getButtonState(1) : 0; },
|
|
|
|
0xC063: function() { return env.tty.getButtonState ? env.tty.getButtonState(2) : 0; }
|
|
|
|
};
|
|
|
|
|
|
|
|
poke_table = {
|
|
|
|
// Text window
|
|
|
|
0x0020: function(v) { if (env.tty.textWindow) { env.tty.textWindow.left = v; } },
|
|
|
|
0x0021: function(v) { if (env.tty.textWindow) { env.tty.textWindow.width = v; } },
|
|
|
|
0x0022: function(v) { if (env.tty.textWindow) { env.tty.textWindow.top = v; } },
|
|
|
|
0x0023: function(v) { if (env.tty.textWindow) { env.tty.textWindow.height = v - env.tty.textWindow.top; } },
|
|
|
|
0x0024: function(v) { env.tty.setCursorPosition(v, void 0); },
|
|
|
|
0x0025: function(v) { env.tty.setCursorPosition(void 0, v); },
|
|
|
|
|
|
|
|
// ONERR flag
|
|
|
|
0x00D8: function(v) { if (v < 0x80) { state.onerr_handler = (void 0); } },
|
|
|
|
|
|
|
|
// Hires Plotting Page (32=1, 64=2, 96=3)
|
|
|
|
0x00E6: function(v) { if (env.display) { env.display.hires_plotting_page = (v === 64 ? 2 : 1); } },
|
|
|
|
|
|
|
|
// Keyboard strobe
|
2013-12-26 22:38:15 +00:00
|
|
|
0xC010: function() { if (env.tty.clearKeyboardStrobe) { env.tty.clearKeyboardStrobe(); } },
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
// Display switches
|
2013-12-26 22:38:15 +00:00
|
|
|
0xC050: function() { if (env.display) { env.display.setState("graphics", true); } }, // Graphics
|
|
|
|
0xC051: function() { if (env.display) { env.display.setState("graphics", false); } }, // Text
|
|
|
|
0xC052: function() { if (env.display) { env.display.setState("full", true); } }, // Full Graphics
|
|
|
|
0xC053: function() { if (env.display) { env.display.setState("full", false); } }, // Split Screen
|
|
|
|
0xC054: function() { if (env.display) { env.display.setState("page1", true); } }, // Page 1
|
|
|
|
0xC055: function() { if (env.display) { env.display.setState("page1", false); } }, // Page 2
|
|
|
|
0xC056: function() { if (env.display) { env.display.setState("lores", true); } }, // Lo-Res
|
|
|
|
0xC057: function() { if (env.display) { env.display.setState("lores", false); } }, // Hi-Res
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
// Speaker toggle
|
2013-12-26 22:38:15 +00:00
|
|
|
0xC030: function() { } // no-op
|
2012-02-09 03:13:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
call_table = {
|
2014-12-28 00:51:21 +00:00
|
|
|
0xD683: function() { // Clear stack
|
|
|
|
state.stack = [];
|
|
|
|
},
|
|
|
|
0xF328: function() { // Pop error entry off stack
|
|
|
|
var stack_record = state.stack.pop();
|
|
|
|
if (!{}.hasOwnProperty.call(stack_record, 'resume_stmt_index')) {
|
|
|
|
runtime_error(ERRORS.SYNTAX_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2012-02-09 03:13:44 +00:00
|
|
|
0xF3E4: function() { // Reveal hi-res page 1
|
|
|
|
if (!env.hires) { runtime_error('Hires graphics not supported'); }
|
|
|
|
env.display.setState('graphics', true, 'full', true, 'page1', true, 'lores', false);
|
|
|
|
},
|
|
|
|
0xF3F2: function() { // Clear hi-res screen to black
|
|
|
|
var hires = env.display.hires_plotting_page === 2 ? env.hires2 : env.hires;
|
|
|
|
if (!hires) { runtime_error('Hires graphics not supported'); }
|
|
|
|
hires.clear();
|
|
|
|
},
|
|
|
|
0xF3F6: function() { // Clear hi-res screen to last color Hplotted
|
|
|
|
var hires = env.display.hires_plotting_page === 2 ? env.hires2 : env.hires;
|
|
|
|
if (!hires) { runtime_error('Hires graphics not supported'); }
|
|
|
|
hires.clear(hires.color);
|
|
|
|
},
|
2014-11-09 00:36:31 +00:00
|
|
|
0xFBF4: function() { // Move cursor right
|
|
|
|
if (env.tty.cursorRight) { env.tty.cursorRight(); }
|
|
|
|
},
|
|
|
|
0xFC10: function() { // Move cursor left
|
|
|
|
if (env.tty.cursorLeft) { env.tty.cursorLeft(); }
|
|
|
|
},
|
|
|
|
0xFC1A: function() { // Move cursor up
|
|
|
|
if (env.tty.cursorUp) { env.tty.cursorUp(); }
|
|
|
|
},
|
|
|
|
0xFC42: function() { // Clear text from cursor to bottom
|
|
|
|
if (env.tty.clearEOS) { env.tty.clearEOS(); }
|
|
|
|
},
|
|
|
|
0xFC66: function() { // Move cursor down
|
|
|
|
if (env.tty.cursorDown) { env.tty.cursorDown(); }
|
|
|
|
},
|
2012-02-09 03:13:44 +00:00
|
|
|
0xFC9C: function() { // Clear from cursor to right
|
|
|
|
if (env.tty.clearEOL) { env.tty.clearEOL(); }
|
2017-08-15 17:10:30 +00:00
|
|
|
},
|
|
|
|
0xFD0C: function() { // Wait for key press
|
|
|
|
throw new BlockingInput(env.tty.readChar, function(_){});
|
|
|
|
},
|
|
|
|
0xFE84: function() { // Normal
|
|
|
|
if (env.tty.setTextStyle) { env.tty.setTextStyle(env.tty.TEXT_STYLE_NORMAL); }
|
|
|
|
},
|
|
|
|
0xFE80: function() { // Inverse
|
|
|
|
if (env.tty.setTextStyle) { env.tty.setTextStyle(env.tty.TEXT_STYLE_INVERSE); }
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
lib = {
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Variable Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
'clear': function CLEAR() {
|
|
|
|
state.clear();
|
|
|
|
},
|
|
|
|
|
|
|
|
'dim': function DIM(name, subscripts) {
|
|
|
|
state.arrays[name].dim(subscripts);
|
|
|
|
},
|
|
|
|
|
|
|
|
'def': function DEF(name, func) {
|
|
|
|
state.functions[name] = func;
|
|
|
|
},
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Flow Control Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
'goto': function GOTO(line) {
|
|
|
|
throw new GoToLine(line);
|
|
|
|
},
|
|
|
|
|
2013-12-26 22:38:15 +00:00
|
|
|
'on_goto': function ON_GOTO(index /* , ...lines */) {
|
2018-08-02 01:43:57 +00:00
|
|
|
index = Math.floor(index);
|
2016-07-26 02:14:33 +00:00
|
|
|
if (index < 0 || index > 255) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
--index;
|
2012-02-09 03:13:44 +00:00
|
|
|
var lines = Array.prototype.slice.call(arguments, 1);
|
|
|
|
|
2016-07-26 02:14:33 +00:00
|
|
|
if (index >= 0 && index < lines.length) {
|
|
|
|
throw new GoToLine(lines[index]);
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'gosub': function GOSUB(line) {
|
|
|
|
state.stack.push({
|
|
|
|
gosub_return: state.stmt_index,
|
|
|
|
line_number: state.line_number
|
|
|
|
});
|
|
|
|
throw new GoToLine(line);
|
|
|
|
},
|
|
|
|
|
2013-12-26 22:38:15 +00:00
|
|
|
'on_gosub': function ON_GOSUB(index /* , ...lines */) {
|
2018-08-02 01:43:57 +00:00
|
|
|
index = Math.floor(index);
|
2016-07-26 02:14:33 +00:00
|
|
|
if (index < 0 || index > 255) {
|
2012-02-09 03:13:44 +00:00
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
2016-07-26 02:14:33 +00:00
|
|
|
--index;
|
|
|
|
var lines = Array.prototype.slice.call(arguments, 1);
|
|
|
|
if (index >= 0 && index < lines.length) {
|
|
|
|
state.stack.push({
|
|
|
|
gosub_return: state.stmt_index,
|
|
|
|
line_number: state.line_number
|
|
|
|
});
|
|
|
|
throw new GoToLine(lines[index]);
|
|
|
|
}
|
2012-02-09 03:13:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
'return': function RETURN() {
|
|
|
|
var stack_record;
|
|
|
|
while (state.stack.length) {
|
|
|
|
stack_record = state.stack.pop();
|
|
|
|
if ({}.hasOwnProperty.call(stack_record, 'gosub_return')) {
|
|
|
|
state.stmt_index = stack_record.gosub_return;
|
|
|
|
state.line_number = stack_record.line_number;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
runtime_error(ERRORS.RETURN_WITHOUT_GOSUB);
|
|
|
|
},
|
|
|
|
|
|
|
|
'pop': function POP() {
|
2014-12-28 00:51:21 +00:00
|
|
|
var stack_record = state.stack.pop();
|
|
|
|
if (!{}.hasOwnProperty.call(stack_record, 'gosub_return')) {
|
|
|
|
runtime_error(ERRORS.RETURN_WITHOUT_GOSUB);
|
|
|
|
return;
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'for': function FOR(varname, from, to, step) {
|
|
|
|
state.variables[varname] = from;
|
|
|
|
state.stack.push({
|
|
|
|
index: varname,
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
step: step,
|
|
|
|
for_next: state.stmt_index,
|
|
|
|
line_number: state.line_number
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-12-26 22:38:15 +00:00
|
|
|
'next': function NEXT(/* ...varnames */) {
|
2012-02-09 03:13:44 +00:00
|
|
|
var varnames = Array.prototype.slice.call(arguments),
|
|
|
|
varname, stack_record, value;
|
|
|
|
do {
|
|
|
|
varname = varnames.shift();
|
|
|
|
|
|
|
|
do {
|
|
|
|
stack_record = state.stack.pop();
|
|
|
|
if (!stack_record || !{}.hasOwnProperty.call(stack_record, 'for_next')) {
|
|
|
|
runtime_error(ERRORS.NEXT_WITHOUT_FOR);
|
|
|
|
}
|
|
|
|
} while (varname !== (void 0) && stack_record.index !== varname);
|
|
|
|
|
|
|
|
value = state.variables[stack_record.index];
|
|
|
|
|
|
|
|
value = value + stack_record.step;
|
|
|
|
state.variables[stack_record.index] = value;
|
|
|
|
|
|
|
|
if (!(stack_record.step > 0 && value > stack_record.to) &&
|
|
|
|
!(stack_record.step < 0 && value < stack_record.to) &&
|
|
|
|
!(stack_record.step === 0 && value === stack_record.to)) {
|
|
|
|
state.stack.push(stack_record);
|
|
|
|
state.stmt_index = stack_record.for_next;
|
|
|
|
state.line_number = stack_record.line_number;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (varnames.length);
|
|
|
|
},
|
|
|
|
|
|
|
|
'if': function IF(value) {
|
|
|
|
if (!value) {
|
|
|
|
throw new NextLine();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'stop': function STOP() {
|
|
|
|
runtime_error(ERRORS.INTERRUPT);
|
|
|
|
},
|
|
|
|
|
|
|
|
'end': function END() {
|
|
|
|
throw new EndProgram();
|
|
|
|
},
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Error Handling Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
'onerr_goto': function ONERR_GOTO(line) {
|
|
|
|
state.onerr_handler = line;
|
2015-12-06 23:47:15 +00:00
|
|
|
throw new NextLine();
|
2012-02-09 03:13:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
'resume': function RESUME() {
|
2014-12-28 00:51:21 +00:00
|
|
|
var stack_record = state.stack.pop();
|
|
|
|
if (!{}.hasOwnProperty.call(stack_record, 'resume_stmt_index')) {
|
|
|
|
runtime_error(ERRORS.SYNTAX_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
state.line_number = stack_record.resume_line_number;
|
|
|
|
state.stmt_index = stack_record.resume_stmt_index;
|
2012-02-09 03:13:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Inline Data Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
'restore': function RESTORE() {
|
|
|
|
state.data_index = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
// PERF: optimize by turning into a function, e.g. "state.parsevar(name, lib.read())"
|
2013-12-26 22:38:15 +00:00
|
|
|
'read': function READ(/* ...lvalues */) {
|
2012-02-09 03:13:44 +00:00
|
|
|
var lvalues = Array.prototype.slice.call(arguments);
|
|
|
|
while (lvalues.length) {
|
|
|
|
if (state.data_index >= state.data.length) {
|
|
|
|
runtime_error(ERRORS.OUT_OF_DATA);
|
|
|
|
}
|
|
|
|
(lvalues.shift())(state.data[state.data_index]);
|
|
|
|
state.data_index += 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// I/O Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-12-26 22:38:15 +00:00
|
|
|
'print': function PRINT(/* ...strings */) {
|
2012-02-09 03:13:44 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments), arg;
|
|
|
|
while (args.length) {
|
|
|
|
arg = args.shift();
|
|
|
|
if (typeof arg === 'function') {
|
|
|
|
arg = arg();
|
|
|
|
}
|
|
|
|
env.tty.writeString(String(arg));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'comma': function COMMA() {
|
|
|
|
return function() {
|
|
|
|
var cur = env.tty.getCursorPosition().x,
|
|
|
|
pos = (cur + 16) - (cur % 16);
|
|
|
|
if (pos >= env.tty.getScreenSize().width) {
|
|
|
|
return '\r';
|
|
|
|
} else {
|
|
|
|
return ' '.repeat(pos - cur);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
'spc': function SPC(n) {
|
|
|
|
n = n >> 0;
|
|
|
|
if (n < 0 || n > 255) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
return function() {
|
|
|
|
return ' '.repeat(n);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
'tab': function TAB(n) {
|
|
|
|
n = n >> 0;
|
|
|
|
if (n < 0 || n > 255) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
if (n === 0) { n = 256; }
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
var pos = env.tty.getCursorPosition().x + 1;
|
|
|
|
return ' '.repeat(pos >= n ? 0 : n - pos);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
'get': function GET(lvalue) {
|
2017-08-15 17:10:30 +00:00
|
|
|
throw new BlockingInput(env.tty.readChar, function(entry) { lvalue(entry); });
|
2012-02-09 03:13:44 +00:00
|
|
|
},
|
|
|
|
|
2013-12-26 22:38:15 +00:00
|
|
|
'input': function INPUT(prompt /* , ...varlist */) {
|
2012-02-09 03:13:44 +00:00
|
|
|
var varlist = Array.prototype.slice.call(arguments, 1); // copy for closure
|
2017-08-15 17:10:30 +00:00
|
|
|
var im = function(cb) { return env.tty.readLine(cb, prompt); };
|
|
|
|
var ih = function(entry) {
|
2012-02-09 03:13:44 +00:00
|
|
|
var parts = [],
|
2017-08-15 17:10:30 +00:00
|
|
|
stream = new Stream(entry);
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
parseDataInput(stream, parts);
|
|
|
|
|
|
|
|
while (varlist.length && parts.length) {
|
|
|
|
try {
|
|
|
|
varlist.shift()(parts.shift());
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof basic.RuntimeError &&
|
2017-08-15 17:10:30 +00:00
|
|
|
e.code === ERRORS.TYPE_MISMATCH[0]) {
|
2012-02-09 03:13:44 +00:00
|
|
|
e.code = ERRORS.REENTER[0];
|
|
|
|
e.message = ERRORS.REENTER[1];
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (varlist.length) {
|
|
|
|
prompt = '??';
|
|
|
|
throw new BlockingInput(im, ih);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parts.length) {
|
|
|
|
env.tty.writeString('?EXTRA IGNORED\r');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
throw new BlockingInput(im, ih);
|
|
|
|
},
|
|
|
|
|
|
|
|
'home': function HOME() {
|
|
|
|
if (env.tty.clearScreen) { env.tty.clearScreen(); }
|
|
|
|
},
|
|
|
|
|
|
|
|
'htab': function HTAB(pos) {
|
|
|
|
if (pos < 1 || pos >= env.tty.getScreenSize().width + 1) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (env.tty.textWindow) {
|
|
|
|
pos += env.tty.textWindow.left;
|
|
|
|
}
|
|
|
|
|
|
|
|
env.tty.setCursorPosition(pos - 1, void 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
'vtab': function VTAB(pos) {
|
|
|
|
if (pos < 1 || pos >= env.tty.getScreenSize().height + 1) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
env.tty.setCursorPosition(void 0, pos - 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
'inverse': function INVERSE() {
|
|
|
|
if (env.tty.setTextStyle) { env.tty.setTextStyle(env.tty.TEXT_STYLE_INVERSE); }
|
|
|
|
},
|
|
|
|
'flash': function FLASH() {
|
|
|
|
if (env.tty.setTextStyle) { env.tty.setTextStyle(env.tty.TEXT_STYLE_FLASH); }
|
|
|
|
},
|
|
|
|
'normal': function NORMAL() {
|
|
|
|
if (env.tty.setTextStyle) { env.tty.setTextStyle(env.tty.TEXT_STYLE_NORMAL); }
|
|
|
|
},
|
|
|
|
'text': function TEXT() {
|
|
|
|
if (env.display) {
|
|
|
|
env.display.setState("graphics", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (env.tty.textWindow) {
|
|
|
|
// Reset text window
|
|
|
|
env.tty.textWindow = {
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
width: env.tty.getScreenSize().width,
|
|
|
|
height: env.tty.getScreenSize().height
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Miscellaneous Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
'notrace': function NOTRACE() {
|
|
|
|
state.trace_mode = false;
|
|
|
|
},
|
|
|
|
'trace': function TRACE() {
|
|
|
|
state.trace_mode = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Lores Graphics
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
'gr': function GR() {
|
|
|
|
if (!env.lores) { runtime_error('Lores graphics not supported'); }
|
|
|
|
env.display.setState("lores", true, "full", false, "graphics", true);
|
|
|
|
env.lores.clear();
|
|
|
|
|
|
|
|
env.tty.setCursorPosition(0, env.tty.getScreenSize().height);
|
|
|
|
},
|
|
|
|
|
|
|
|
'color': function COLOR(n) {
|
|
|
|
if (!env.lores) { runtime_error('Lores graphics not supported'); }
|
|
|
|
|
|
|
|
n = n >> 0;
|
|
|
|
if (n < 0) { runtime_error(ERRORS.ILLEGAL_QUANTITY); }
|
|
|
|
|
|
|
|
env.lores.setColor(n);
|
|
|
|
},
|
|
|
|
|
|
|
|
'plot': function PLOT(x, y) {
|
|
|
|
if (!env.lores) { runtime_error('Lores graphics not supported'); }
|
|
|
|
|
|
|
|
x = x >> 0;
|
|
|
|
y = y >> 0;
|
|
|
|
|
|
|
|
var size = env.lores.getScreenSize();
|
|
|
|
if (x < 0 || y < 0 || x >= size.width || y >= size.height) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
env.lores.plot(x, y);
|
|
|
|
},
|
|
|
|
|
|
|
|
'hlin': function HLIN(x1, x2, y) {
|
|
|
|
if (!env.lores) { runtime_error('Lores graphics not supported'); }
|
|
|
|
|
|
|
|
x1 = x1 >> 0;
|
|
|
|
x2 = x2 >> 0;
|
|
|
|
y = y >> 0;
|
|
|
|
|
|
|
|
var size = env.lores.getScreenSize();
|
|
|
|
if (x1 < 0 || x2 < 0 || y < 0 ||
|
|
|
|
x1 >= size.width || x2 >= size.width || y >= size.height) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
env.lores.hlin(x1, x2, y);
|
|
|
|
},
|
|
|
|
|
|
|
|
'vlin': function VLIN(y1, y2, x) {
|
|
|
|
if (!env.lores) { runtime_error('Lores graphics not supported'); }
|
|
|
|
|
|
|
|
y1 = y1 >> 0;
|
|
|
|
y2 = y2 >> 0;
|
|
|
|
x = x >> 0;
|
|
|
|
|
|
|
|
var size = env.lores.getScreenSize();
|
|
|
|
if (x < 0 || y1 < 0 || y2 < 0 ||
|
|
|
|
x >= size.width || y1 >= size.height || y2 >= size.height) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
env.lores.vlin(y1, y2, x);
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Hires Graphics
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
'hgr': function HGR() {
|
|
|
|
if (!env.hires) { runtime_error('Hires graphics not supported'); }
|
|
|
|
env.display.setState("lores", false, "full", false, "page1", true, "graphics", true);
|
|
|
|
env.display.hires_plotting_page = 1;
|
|
|
|
env.hires.clear();
|
|
|
|
},
|
|
|
|
|
|
|
|
'hgr2': function HGR2() {
|
|
|
|
if (!env.hires) { runtime_error('Hires graphics not supported'); }
|
|
|
|
env.display.setState("lores", false, "full", true, "page1", false, "graphics", true);
|
|
|
|
env.display.hires_plotting_page = 2;
|
|
|
|
env.hires2.clear();
|
|
|
|
},
|
|
|
|
|
|
|
|
'hcolor': function HCOLOR(n) {
|
|
|
|
if (!env.hires) { runtime_error('Hires graphics not supported'); }
|
|
|
|
n = n >> 0;
|
|
|
|
if (n < 0 || n > 7) { runtime_error(ERRORS.ILLEGAL_QUANTITY); }
|
|
|
|
env.hires.setColor(n);
|
|
|
|
if (env.hires2) { env.hires2.setColor(n); }
|
|
|
|
},
|
|
|
|
|
2013-12-26 22:38:15 +00:00
|
|
|
'hplot': function HPLOT(/* ...coords */) {
|
2012-02-09 03:13:44 +00:00
|
|
|
var hires = env.display.hires_plotting_page === 2 ? env.hires2 : env.hires;
|
|
|
|
if (!hires) { runtime_error('Hires graphics not supported'); }
|
|
|
|
|
|
|
|
var coords = Array.prototype.slice.call(arguments),
|
|
|
|
size = hires.getScreenSize(),
|
|
|
|
x, y;
|
|
|
|
|
|
|
|
x = coords.shift() >> 0;
|
|
|
|
y = coords.shift() >> 0;
|
|
|
|
|
|
|
|
if (x < 0 || y < 0 || x >= size.width || y >= size.height) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
hires.plot(x, y);
|
|
|
|
while (coords.length) {
|
|
|
|
x = coords.shift() >> 0;
|
|
|
|
y = coords.shift() >> 0;
|
|
|
|
if (x < 0 || y < 0 || x >= size.width || y >= size.height) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
hires.plot_to(x, y);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-12-26 22:38:15 +00:00
|
|
|
'hplot_to': function HPLOT_TO(/* ...coords */) {
|
2012-02-09 03:13:44 +00:00
|
|
|
var hires = env.display.hires_plotting_page === 2 ? env.hires2 : env.hires;
|
|
|
|
if (!hires) { runtime_error('Hires graphics not supported'); }
|
|
|
|
|
|
|
|
var coords = Array.prototype.slice.call(arguments),
|
2017-08-15 17:10:30 +00:00
|
|
|
size = hires.getScreenSize(), x, y;
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
while (coords.length) {
|
|
|
|
x = coords.shift() >> 0;
|
|
|
|
y = coords.shift() >> 0;
|
|
|
|
|
|
|
|
if (x < 0 || y < 0 || x >= size.width || y >= size.height) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
hires.plot_to(x, y);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Compatibility shims
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
'pr#': function PR(slot) {
|
|
|
|
if (slot === 0) {
|
|
|
|
if (env.tty.setFirmwareActive) { env.tty.setFirmwareActive(false); }
|
|
|
|
} else if (slot === 3) {
|
|
|
|
if (env.tty.setFirmwareActive) { env.tty.setFirmwareActive(true); }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'poke': function POKE(address, value) {
|
|
|
|
address = address & 0xffff;
|
|
|
|
|
|
|
|
value = value >> 0;
|
|
|
|
if (value < 0 || value > 255) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!({}.hasOwnProperty.call(poke_table, address))) {
|
|
|
|
runtime_error("Unsupported POKE location: " + address);
|
|
|
|
}
|
|
|
|
|
|
|
|
poke_table[address](value);
|
|
|
|
},
|
|
|
|
|
|
|
|
'call': function CALL(address) {
|
|
|
|
address = address & 0xffff;
|
|
|
|
|
|
|
|
if (!({}.hasOwnProperty.call(call_table, address))) {
|
|
|
|
runtime_error("Unsupported POKE location: " + address);
|
|
|
|
}
|
|
|
|
|
|
|
|
call_table[address]();
|
|
|
|
},
|
|
|
|
|
|
|
|
'speed': function SPEED(n) {
|
|
|
|
n = n >> 0;
|
|
|
|
if (n < 0 || n > 255) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
env.tty.speed = n;
|
|
|
|
},
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Referenced by compiled functions
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
'div': function div(n, d) {
|
2012-02-09 03:13:44 +00:00
|
|
|
var r = n / d;
|
|
|
|
if (!isFinite(r)) { runtime_error(ERRORS.DIVISION_BY_ZERO); }
|
|
|
|
return r;
|
|
|
|
},
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
'fn': function fn(name, arg) {
|
2012-02-09 03:13:44 +00:00
|
|
|
if (!{}.hasOwnProperty.call(state.functions, name)) {
|
|
|
|
runtime_error(ERRORS.UNDEFINED_FUNCTION);
|
|
|
|
}
|
|
|
|
return state.functions[name](arg);
|
|
|
|
},
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
'checkFinite': function checkFinite(n) {
|
2012-02-09 03:13:44 +00:00
|
|
|
if (!isFinite(n)) { runtime_error(ERRORS.OVERFLOW); }
|
|
|
|
return n;
|
|
|
|
},
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
'toint': function toint(n) {
|
2012-02-09 03:13:44 +00:00
|
|
|
n = n >> 0;
|
|
|
|
if (n > 0x7fff || n < -0x8000) { runtime_error(ERRORS.ILLEGAL_QUANTITY); }
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Apply a signature [return_type, arg0_type, arg1_type, ...] to a function
|
2013-12-26 22:38:15 +00:00
|
|
|
function funcsign(func /*, return_type, ...arg_types */) {
|
2012-02-09 03:13:44 +00:00
|
|
|
func.signature = Array.prototype.slice.call(arguments, 1);
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
funlib = {};
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Functions
|
|
|
|
//
|
|
|
|
// name: [ impl, returntype, [arg0type [, arg1type [, ... ] ]
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
funlib[kws.ABS] = funcsign(Math.abs, 'number', 'number');
|
|
|
|
funlib[kws.ASC] = funcsign(function(s) {
|
2012-02-09 03:13:44 +00:00
|
|
|
if (s.length < 1) { runtime_error(ERRORS.ILLEGAL_QUANTITY); }
|
|
|
|
return s.charCodeAt(0);
|
2012-06-20 15:54:32 +00:00
|
|
|
}, 'number', 'string');
|
|
|
|
funlib[kws.ATN] = funcsign(Math.atan, 'number', 'number');
|
|
|
|
funlib[kws.CHR$] = funcsign(String.fromCharCode, 'string', 'number');
|
|
|
|
funlib[kws.COS] = funcsign(Math.cos, 'number', 'number');
|
|
|
|
funlib[kws.EXP] = funcsign(Math.exp, 'number', 'number');
|
|
|
|
funlib[kws.INT] = funcsign(Math.floor, 'number', 'number');
|
|
|
|
funlib[kws.LEN] = funcsign(function LEN(s) { return s.length; }, 'number', 'string');
|
|
|
|
funlib[kws.LOG] = funcsign(Math.log, 'number', 'number');
|
|
|
|
funlib[kws.SGN] = funcsign(function SGN(n) { return n > 0 ? 1 : n < 0 ? -1 : 0; }, 'number', 'number');
|
|
|
|
funlib[kws.SIN] = funcsign(Math.sin, 'number', 'number');
|
|
|
|
funlib[kws.SQR] = funcsign(Math.sqrt, 'number', 'number');
|
|
|
|
funlib[kws.STR$] = funcsign(function STR$(n) { return n.toString(); }, 'string', 'number');
|
|
|
|
funlib[kws.TAN] = funcsign(Math.tan, 'number', 'number');
|
|
|
|
funlib[kws.VAL] = funcsign(function VAL(s) {
|
2012-02-09 03:13:44 +00:00
|
|
|
var n = parseFloat(s);
|
|
|
|
return isFinite(n) ? n : 0;
|
2012-06-20 15:54:32 +00:00
|
|
|
}, 'number', 'string');
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
funlib[kws.RND] = funcsign(function RND(n) {
|
2012-02-09 03:13:44 +00:00
|
|
|
if (n > 0) {
|
|
|
|
// Next in PRNG sequence
|
|
|
|
return state.prng.next();
|
|
|
|
} else if (n < 0) {
|
|
|
|
// Re-seed. NOTE: Not predictable as in Applesoft
|
|
|
|
state.prng.seed(n);
|
|
|
|
return state.prng.next();
|
|
|
|
}
|
|
|
|
return state.prng.last;
|
2012-06-20 15:54:32 +00:00
|
|
|
}, 'number', 'number');
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
funlib[kws.LEFT$] = funcsign(function LEFT$(s, n) { return s.substring(0, n); }, 'string', 'string', 'number');
|
|
|
|
funlib[kws.MID$] = funcsign(function MID$(s, n, n2) { return n2 === (void 0) ? s.substring(n - 1) : s.substring(n - 1, n + n2 - 1); }, 'string', 'string', 'number', 'number?');
|
|
|
|
funlib[kws.RIGHT$] = funcsign(function RIGHT$(s, n) { return s.length < n ? s : s.substring(s.length - n); }, 'string', 'string', 'number');
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2013-08-17 02:53:09 +00:00
|
|
|
funlib[kws.POS] = funcsign(function POS(n) { return env.tty.getCursorPosition().x; }, 'number', 'number');
|
2012-06-20 15:54:32 +00:00
|
|
|
funlib[kws.SCRN] = funcsign(function SCRN(x, y) {
|
2012-02-09 03:13:44 +00:00
|
|
|
if (!env.lores) { runtime_error("Graphics not supported"); }
|
|
|
|
x = x >> 0;
|
|
|
|
y = y >> 0;
|
|
|
|
var size = env.lores.getScreenSize();
|
|
|
|
if (x < 0 || y < 0 || x >= size.width || y >= size.height) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
return env.lores.getPixel(x, y);
|
2012-06-20 15:54:32 +00:00
|
|
|
}, 'number', 'number', 'number');
|
|
|
|
funlib[kws.HSCRN] = funcsign(function HSCRN(x, y) {
|
2012-02-09 03:13:44 +00:00
|
|
|
var hires = env.display.hires_plotting_page === 2 ? env.hires2 : env.hires;
|
|
|
|
if (!hires) { runtime_error("Graphics not supported"); }
|
|
|
|
|
|
|
|
x = x >> 0;
|
|
|
|
y = y >> 0;
|
|
|
|
var size = hires.getScreenSize();
|
|
|
|
if (x < 0 || y < 0 || x >= size.width || y >= size.height) {
|
|
|
|
runtime_error(ERRORS.ILLEGAL_QUANTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hires.getPixel(x, y);
|
2012-06-20 15:54:32 +00:00
|
|
|
}, 'number', 'number', 'number');
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
funlib[kws.PDL] = funcsign(function PDL(n) {
|
2012-02-09 03:13:44 +00:00
|
|
|
if (env.paddle) {
|
|
|
|
return (env.paddle(n) * 255) & 0xff;
|
|
|
|
} else {
|
2012-06-20 15:54:32 +00:00
|
|
|
return runtime_error('Paddles not attached');
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
2012-06-20 15:54:32 +00:00
|
|
|
}, 'number', 'number');
|
|
|
|
funlib[kws.FRE] = funcsign(function FRE(n) {
|
2012-02-09 03:13:44 +00:00
|
|
|
return JSON ? JSON.stringify([state.variables, state.arrays, state.functions]).length : 0;
|
2012-06-20 15:54:32 +00:00
|
|
|
}, 'number', 'number');
|
|
|
|
funlib[kws.PEEK] = funcsign(function PEEK(address) {
|
2012-02-09 03:13:44 +00:00
|
|
|
address = address & 0xffff;
|
|
|
|
if (!{}.hasOwnProperty.call(peek_table, address)) {
|
|
|
|
runtime_error("Unsupported PEEK location: " + address);
|
|
|
|
}
|
|
|
|
return peek_table[address]();
|
2012-06-20 15:54:32 +00:00
|
|
|
}, 'number', 'number');
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
// Not supported
|
2013-08-17 02:53:09 +00:00
|
|
|
funlib[kws.USR] = funcsign(function USR(n) { runtime_error("USR Function not supported"); }, 'number', 'number');
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Parser / Compiler
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
var program = (function() {
|
|
|
|
|
|
|
|
var identifiers = {
|
|
|
|
variables: {},
|
|
|
|
arrays: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Lexical Analysis
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
var match, test, endOfStatement, endOfProgram,
|
2017-08-15 17:10:30 +00:00
|
|
|
currLine = 0, currColumn = 0,
|
|
|
|
currLineNumber = 0;
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
function parse_error(msg) {
|
2013-08-17 23:00:23 +00:00
|
|
|
return new basic.ParseError(msg + " in line " + currLineNumber,
|
|
|
|
currLine, currColumn);
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
(function(source) {
|
|
|
|
function munge(kw) {
|
|
|
|
// Escape special characters
|
2013-12-26 22:38:15 +00:00
|
|
|
function escape(c) { return (/[\[\]\\\^\$\.\|\?\*\+\(\)]/).test(c) ? '\\' + c : c; }
|
2012-02-09 03:13:44 +00:00
|
|
|
// Allow linear whitespace between characters
|
|
|
|
//return kw.split('').map(escape).join('[ \\t]*');
|
|
|
|
|
|
|
|
// Allow linear whitespace in HCOLOR=, HIMEM:, CHR$, etc
|
|
|
|
return kw.split(/(?=\W)/).map(escape).join('[ \\t]*');
|
|
|
|
}
|
|
|
|
|
|
|
|
var RESERVED_WORDS = [
|
2014-12-29 02:45:40 +00:00
|
|
|
kws.ABS, kws.AND, kws.ASC, kws.ATN, kws.AT, kws.CALL,
|
|
|
|
kws.CHR$, kws.CLEAR, kws.COLOR, kws.CONT, kws.COS,
|
|
|
|
/*kws.DATA,*/ kws.DEF, kws.DEL, kws.DIM, kws.DRAW, kws.END,
|
|
|
|
kws.EXP, kws.FLASH, kws.FN, kws.FOR, kws.FRE, kws.GET,
|
|
|
|
kws.GOSUB, kws.GOTO, kws.GR, kws.HCOLOR, kws.HGR2, kws.HGR,
|
|
|
|
kws.HIMEM, kws.HLIN, kws.HOME, kws.HPLOT, kws.HTAB, kws.IF,
|
|
|
|
kws.IN, kws.INPUT, kws.INT, kws.INVERSE, kws.LEFT$, kws.LEN,
|
|
|
|
kws.LET, kws.LIST, kws.LOAD, kws.LOG, kws.LOMEM, kws.MID$,
|
|
|
|
kws.NEW, kws.NEXT, kws.NORMAL, kws.NOTRACE, kws.NOT,
|
|
|
|
kws.ONERR, kws.ON, kws.OR, kws.PDL, kws.PEEK, kws.PLOT,
|
|
|
|
kws.POKE, kws.POP, kws.POS, kws.PRINT, kws.PR, kws.READ,
|
|
|
|
kws.RECALL, /*kws.REM,*/ kws.RESTORE, kws.RESUME,
|
|
|
|
kws.RETURN, kws.RIGHT$, kws.RND, kws.ROT, kws.RUN, kws.SAVE,
|
|
|
|
kws.SCALE, kws.SCRN, kws.SGN, kws.SHLOAD, kws.SIN, kws.SPC,
|
|
|
|
kws.SPEED, kws.SQR, kws.STEP, kws.STOP, kws.STORE, kws.STR$,
|
|
|
|
kws.TAB, kws.TAN, kws.TEXT, kws.THEN, kws.TO, kws.TRACE,
|
|
|
|
kws.USR, kws.VAL, kws.VLIN, kws.VTAB, kws.WAIT, kws.XDRAW,
|
|
|
|
kws.AMPERSAND, kws.QUESTION, kws.HSCRN
|
2012-06-20 15:54:32 +00:00
|
|
|
];
|
2012-02-09 03:13:44 +00:00
|
|
|
// NOTE: keywords that are stems of other words need to go after (e.g. "NOTRACE", "NOT)
|
2012-06-20 15:54:32 +00:00
|
|
|
RESERVED_WORDS.sort();
|
|
|
|
RESERVED_WORDS.reverse();
|
|
|
|
|
|
|
|
var regexReservedWords = new RegExp("^(" + RESERVED_WORDS.map(munge).join("|") + ")", "i"),
|
2013-08-17 01:38:29 +00:00
|
|
|
regexIdentifier = /^([A-Za-z][A-Za-z0-9]?)[A-Za-z0-9]*(\$|%)?/,
|
|
|
|
regexStringLiteral = /^"([^"]*?)(?:"|(?=\n|\r|$))/,
|
2013-08-17 02:53:09 +00:00
|
|
|
regexNumberLiteral = /^[0-9]*\.?[0-9]+(?:[eE]\s*[\-+]?\s*[0-9]+)?/,
|
|
|
|
regexHexLiteral = /^\$[0-9A-Fa-f]+/,
|
2013-08-17 23:00:23 +00:00
|
|
|
regexOperator = /^[;=<>+\-*\/\^(),]/,
|
2012-06-20 15:54:32 +00:00
|
|
|
|
2013-08-17 02:53:09 +00:00
|
|
|
regexLineNumber = /^[0-9]+/,
|
|
|
|
regexSeparator = /^:/,
|
2012-06-20 15:54:32 +00:00
|
|
|
|
|
|
|
regexRemark = new RegExp('^(' + munge(kws.REM) + '([^\r\n]*))', 'i'),
|
|
|
|
regexData = new RegExp('^(' + munge(kws.DATA) + ')', 'i'),
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2013-08-17 01:38:29 +00:00
|
|
|
regexLinearWhitespace = /^[ \t]+/,
|
|
|
|
regexNewline = /^\r?\n/;
|
2013-08-17 02:53:09 +00:00
|
|
|
|
2012-02-09 03:13:44 +00:00
|
|
|
// Token types:
|
|
|
|
// lineNumber - start of a new line
|
|
|
|
// separator - separates statements on same line
|
|
|
|
// reserved - reserved keyword (command, function, etc)
|
|
|
|
// identifier - variable name
|
|
|
|
// string - string literal
|
|
|
|
// number - number literal
|
|
|
|
// operator - operator
|
|
|
|
// remark - REM blah
|
|
|
|
// data - DATA blah,"blah",blah
|
|
|
|
|
|
|
|
var start = true,
|
2017-08-15 17:10:30 +00:00
|
|
|
stream = new Stream(source);
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
function nextToken() {
|
|
|
|
var token = {}, newline = start, ws;
|
|
|
|
start = false;
|
|
|
|
|
|
|
|
currLine = stream.line + 1;
|
|
|
|
currColumn = stream.column + 1;
|
|
|
|
|
|
|
|
// Consume whitespace
|
|
|
|
do {
|
|
|
|
ws = false;
|
|
|
|
if (stream.match(regexLinearWhitespace)) {
|
|
|
|
ws = true;
|
|
|
|
} else if (stream.match(regexNewline)) {
|
|
|
|
ws = true;
|
|
|
|
newline = true;
|
|
|
|
}
|
|
|
|
} while (ws);
|
|
|
|
|
|
|
|
if (stream.eof()) {
|
|
|
|
return (void 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newline) {
|
|
|
|
if (stream.match(regexLineNumber)) {
|
2013-08-17 02:53:09 +00:00
|
|
|
token.lineNumber = Number(stream.lastMatch[0]);
|
2012-02-09 03:13:44 +00:00
|
|
|
} else if (stream.match(regexSeparator)) {
|
|
|
|
// Extension - allow leading : to continue previous line
|
2013-08-17 02:53:09 +00:00
|
|
|
token.separator = stream.lastMatch[0];
|
2012-02-09 03:13:44 +00:00
|
|
|
} else {
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Syntax error: Expected line number or separator");
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
} else if (stream.match(regexRemark)) {
|
|
|
|
token.remark = stream.lastMatch[2];
|
|
|
|
} else if (stream.match(regexData)) {
|
|
|
|
token.data = [];
|
|
|
|
parseDataInput(stream, token.data);
|
|
|
|
} else if (stream.match(regexReservedWords)) {
|
|
|
|
token.reserved = stream.lastMatch[1].toUpperCase().replace(/\s+/g, '');
|
2012-06-20 15:54:32 +00:00
|
|
|
if (token.reserved === kws.QUESTION) { token.reserved = kws.PRINT; } // HACK
|
2012-02-09 03:13:44 +00:00
|
|
|
} else if (stream.match(regexIdentifier)) {
|
|
|
|
token.identifier = stream.lastMatch[1].toUpperCase() + (stream.lastMatch[2] || ''); // Canonicalize identifier name
|
|
|
|
} else if (stream.match(regexStringLiteral)) {
|
|
|
|
token.string = stream.lastMatch[1];
|
|
|
|
} else if (stream.match(regexNumberLiteral)) {
|
2013-08-17 02:53:09 +00:00
|
|
|
token.number = parseFloat(stream.lastMatch[0].replace(/\s+/g, ''));
|
|
|
|
} else if (stream.match(regexHexLiteral)) {
|
|
|
|
token.number = parseInt(stream.lastMatch[0].substring(1), 16);
|
2012-02-09 03:13:44 +00:00
|
|
|
} else if (stream.match(regexOperator)) {
|
2013-08-17 02:53:09 +00:00
|
|
|
token.operator = stream.lastMatch[0].replace(/\s+/g, '');
|
2012-02-09 03:13:44 +00:00
|
|
|
} else if (stream.match(regexSeparator)) {
|
2013-08-17 02:53:09 +00:00
|
|
|
token.separator = stream.lastMatch[0];
|
2012-02-09 03:13:44 +00:00
|
|
|
} else {
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Syntax error: Unexpected '" + source.substr(0, 40) + "'");
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
var lookahead = nextToken();
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
match = function match(type, value) {
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
if (!lookahead) {
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Syntax error: Expected " + type + ", saw end of file");
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var token = lookahead;
|
|
|
|
if ('lineNumber' in token) {
|
|
|
|
currLineNumber = token.lineNumber;
|
|
|
|
}
|
|
|
|
lookahead = nextToken();
|
|
|
|
|
|
|
|
if (!{}.hasOwnProperty.call(token, type)) {
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Syntax error: Expected " + type + ", saw " + JSON.stringify(token));
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value !== (void 0) && token[type] !== value) {
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Syntax error: Expected '" + value + "', saw " + JSON.stringify(token));
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return token[type];
|
|
|
|
};
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
test = function test(type, value, consume) {
|
2012-02-09 03:13:44 +00:00
|
|
|
if (lookahead && {}.hasOwnProperty.call(lookahead, type) &&
|
|
|
|
(value === (void 0) || lookahead[type] === value)) {
|
|
|
|
|
|
|
|
if (consume) {
|
|
|
|
var token = lookahead;
|
|
|
|
if ('lineNumber' in token) {
|
|
|
|
currLineNumber = token.lineNumber;
|
|
|
|
}
|
|
|
|
lookahead = nextToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
endOfStatement = function endOfStatement() {
|
2012-02-09 03:13:44 +00:00
|
|
|
return !lookahead ||
|
|
|
|
{}.hasOwnProperty.call(lookahead, 'separator') ||
|
|
|
|
{}.hasOwnProperty.call(lookahead, 'lineNumber');
|
|
|
|
};
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
endOfProgram = function endOfProgram() {
|
2012-02-09 03:13:44 +00:00
|
|
|
return !lookahead;
|
|
|
|
};
|
|
|
|
|
|
|
|
} (source));
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Compiler utility functions
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function quote(string) {
|
2017-08-15 17:10:30 +00:00
|
|
|
return JSON.stringify(string);
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Recursive Descent Parser
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
var parseExpression, parseSubscripts;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Type Checking
|
|
|
|
//
|
|
|
|
|
|
|
|
function parseAnyExpression() {
|
|
|
|
var expr = parseExpression();
|
|
|
|
return expr.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
function enforce_type(actual, expected) {
|
|
|
|
if (actual !== expected) {
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error('Type mismatch error: Expected ' + expected);
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseStringExpression() {
|
|
|
|
var expr = parseExpression();
|
|
|
|
enforce_type(expr.type, 'string');
|
|
|
|
return expr.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseNumericExpression() {
|
|
|
|
var expr = parseExpression();
|
|
|
|
enforce_type(expr.type, 'number');
|
|
|
|
return expr.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Variables
|
|
|
|
//
|
|
|
|
|
|
|
|
parseSubscripts = function() {
|
|
|
|
var subscripts; // undefined = no subscripts
|
|
|
|
|
|
|
|
if (test('operator', '(', true)) {
|
|
|
|
|
|
|
|
subscripts = [];
|
|
|
|
|
|
|
|
do {
|
|
|
|
subscripts.push(parseNumericExpression());
|
|
|
|
} while (test('operator', ',', true));
|
|
|
|
|
|
|
|
match("operator", ")");
|
|
|
|
|
|
|
|
return subscripts.join(',');
|
|
|
|
}
|
|
|
|
return (void 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
function parsePValue() {
|
|
|
|
var name = match('identifier'),
|
2017-08-15 17:10:30 +00:00
|
|
|
subscripts = parseSubscripts();
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
if (subscripts) {
|
|
|
|
identifiers.arrays[name] = true;
|
|
|
|
return '(function (value){state.parsevar(' +
|
|
|
|
quote(name) + ',[' + subscripts + '],value);})';
|
|
|
|
} else {
|
|
|
|
identifiers.variables[name] = true;
|
|
|
|
return '(function (value){state.parsevar(' +
|
|
|
|
quote(name) + ',value);})';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Expressions
|
|
|
|
//
|
|
|
|
|
|
|
|
parseExpression = (function() {
|
|
|
|
// closure to keep things tidy
|
|
|
|
|
|
|
|
function parseUserfunction() {
|
|
|
|
var name = match('identifier'),
|
2017-08-15 17:10:30 +00:00
|
|
|
type = vartype(name) === 'string' ? 'string' : 'number',
|
|
|
|
expr;
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
// FUTURE: Allow differing argument type and return type
|
|
|
|
// (may require runtime type checks)
|
|
|
|
|
|
|
|
// Determine the function argument
|
|
|
|
match("operator", "(");
|
|
|
|
expr = type === 'string' ? parseStringExpression() : parseNumericExpression();
|
|
|
|
match("operator", ")");
|
|
|
|
|
|
|
|
return { source: 'lib.fn(' + quote(name) + ',' + expr + ')', type: type };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function parsefunction(name) {
|
|
|
|
if (!{}.hasOwnProperty.call(funlib, name)) {
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Undefined function: " + name);
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match("operator", "(");
|
|
|
|
|
|
|
|
var func = funlib[name],
|
2017-08-15 17:10:30 +00:00
|
|
|
funcdesc = func.signature.slice(),
|
|
|
|
rtype = funcdesc.shift(),
|
|
|
|
args = [],
|
|
|
|
atype;
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
while (funcdesc.length) {
|
|
|
|
atype = funcdesc.shift();
|
|
|
|
|
|
|
|
if (/\?$/.test(atype)) {
|
|
|
|
if (test('operator', ')')) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
atype = atype.substring(0, atype.length - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (args.length) {
|
|
|
|
match("operator", ",");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atype === 'string') {
|
|
|
|
args.push(parseStringExpression());
|
|
|
|
} else if (atype === 'number') {
|
|
|
|
args.push(parseNumericExpression());
|
|
|
|
} else {
|
|
|
|
throw new Error("Invalid function definition");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match("operator", ")");
|
|
|
|
|
|
|
|
return { source: 'funlib.' + name + '(' + args.join(',') + ')', type: rtype };
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseFinalExpression() {
|
|
|
|
if (test('number')) {
|
|
|
|
return { source: String(match('number')), type: 'number' };
|
|
|
|
} else if (test('string')) {
|
|
|
|
return { source: quote(match('string')), type: 'string' };
|
2012-06-20 15:54:32 +00:00
|
|
|
} else if (test('reserved', kws.FN, true)) {
|
2012-02-09 03:13:44 +00:00
|
|
|
return parseUserfunction();
|
|
|
|
} else if (test('reserved')) {
|
|
|
|
return parsefunction(match('reserved'));
|
|
|
|
} else if (test('identifier')) {
|
|
|
|
var name = match('identifier'),
|
2017-08-15 17:10:30 +00:00
|
|
|
type = vartype(name) === 'string' ? 'string' : 'number',
|
|
|
|
subscripts = parseSubscripts();
|
2012-02-09 03:13:44 +00:00
|
|
|
if (subscripts) {
|
|
|
|
identifiers.arrays[name] = true;
|
|
|
|
return { source: 'state.arrays[' + quote(name) + '].get([' + subscripts + '])', type: type };
|
|
|
|
} else {
|
|
|
|
identifiers.variables[name] = true;
|
|
|
|
return { source: 'state.variables[' + quote(name) + ']', type: type };
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match("operator", "(");
|
|
|
|
var expr = parseExpression();
|
|
|
|
match("operator", ")");
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseUnaryExpression() {
|
|
|
|
var rhs, op;
|
|
|
|
|
|
|
|
if (test('operator', '+') || test('operator', '-')) {
|
|
|
|
op = match('operator');
|
2012-06-20 15:54:32 +00:00
|
|
|
} else if (test('reserved', kws.NOT)) {
|
2012-02-09 03:13:44 +00:00
|
|
|
op = match('reserved');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op) {
|
|
|
|
rhs = parseUnaryExpression();
|
|
|
|
|
|
|
|
enforce_type(rhs.type, 'number');
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case "+": return rhs;
|
|
|
|
case "-": return { source: '(-' + rhs.source + ')', type: 'number' };
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.NOT: return { source: '((!' + rhs.source + ')?1:0)', type: 'number' };
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return parseFinalExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
function parsePowerExpression() {
|
|
|
|
var lhs = parseUnaryExpression(), rhs;
|
|
|
|
while (test('operator', '^', true)) {
|
|
|
|
rhs = parseUnaryExpression();
|
|
|
|
|
|
|
|
enforce_type(lhs.type, 'number');
|
|
|
|
enforce_type(rhs.type, 'number');
|
|
|
|
|
|
|
|
lhs = { source: 'Math.pow(' + lhs.source + ',' + rhs.source + ')', type: 'number' };
|
|
|
|
}
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseMultiplicativeExpression() {
|
|
|
|
var lhs = parsePowerExpression(), rhs, op;
|
|
|
|
while (test('operator', '*') || test('operator', '/')) {
|
|
|
|
op = match('operator');
|
|
|
|
rhs = parsePowerExpression();
|
|
|
|
|
|
|
|
enforce_type(lhs.type, 'number');
|
|
|
|
enforce_type(rhs.type, 'number');
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case "*": lhs = { source: '(' + lhs.source + '*' + rhs.source + ')', type: 'number' }; break;
|
|
|
|
case "/": lhs = { source: 'lib.div(' + lhs.source + ',' + rhs.source + ')', type: 'number' }; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseAdditiveExpression() {
|
|
|
|
var lhs = parseMultiplicativeExpression(), rhs, op;
|
|
|
|
while (test('operator', '+') || test('operator', '-')) {
|
|
|
|
op = match('operator');
|
|
|
|
rhs = parseMultiplicativeExpression();
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case "+":
|
|
|
|
enforce_type(rhs.type, lhs.type);
|
|
|
|
lhs = { source: '(' + lhs.source + '+' + rhs.source + ')', type: lhs.type }; break;
|
|
|
|
case "-":
|
|
|
|
enforce_type(lhs.type, 'number');
|
|
|
|
enforce_type(rhs.type, 'number');
|
|
|
|
lhs = { source: '(' + lhs.source + '-' + rhs.source + ')', type: lhs.type }; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseRelationalExpression() {
|
|
|
|
var lhs = parseAdditiveExpression(), rhs, op;
|
2013-08-17 23:00:23 +00:00
|
|
|
while (test('operator', '<') || test('operator', '>') || test('operator', '=')) {
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
op = match('operator');
|
2013-08-17 23:00:23 +00:00
|
|
|
switch (op) {
|
|
|
|
case '<':
|
|
|
|
if (test('operator', '=', true)) { op = '<='; break; }
|
|
|
|
if (test('operator', '>', true)) { op = '!=='; break; }
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
if (test('operator', '=', true)) { op = '>='; break; }
|
|
|
|
if (test('operator', '<', true)) { op = '!=='; break; }
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
if (test('operator', '<', true)) { op = '<='; break; }
|
|
|
|
if (test('operator', '>', true)) { op = '>='; break; }
|
|
|
|
if (test('operator', '=', true)) { op = '==='; break; }
|
|
|
|
op = '===';
|
|
|
|
}
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
rhs = parseAdditiveExpression();
|
|
|
|
|
|
|
|
enforce_type(rhs.type, lhs.type);
|
2013-08-17 23:00:23 +00:00
|
|
|
lhs = lhs = { source: '((' + lhs.source + op + rhs.source + ')?1:0)', type: 'number' };
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseAndExpression() {
|
|
|
|
var lhs = parseRelationalExpression(), rhs;
|
2012-06-20 15:54:32 +00:00
|
|
|
while (test('reserved', kws.AND, true)) {
|
2012-02-09 03:13:44 +00:00
|
|
|
rhs = parseRelationalExpression();
|
|
|
|
|
|
|
|
enforce_type(lhs.type, 'number');
|
|
|
|
enforce_type(rhs.type, 'number');
|
|
|
|
|
|
|
|
lhs = {
|
|
|
|
source: '((' + lhs.source + '&&' + rhs.source + ')?1:0)',
|
|
|
|
type: 'number'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseOrExpression() {
|
|
|
|
var lhs = parseAndExpression(), rhs;
|
2012-06-20 15:54:32 +00:00
|
|
|
while (test('reserved', kws.OR, true)) {
|
2012-02-09 03:13:44 +00:00
|
|
|
rhs = parseAndExpression();
|
|
|
|
|
|
|
|
enforce_type(lhs.type, 'number');
|
|
|
|
enforce_type(rhs.type, 'number');
|
|
|
|
|
|
|
|
lhs = {
|
|
|
|
source: '((' + lhs.source + '||' + rhs.source + ')?1:0)',
|
|
|
|
type: 'number'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseOrExpression;
|
|
|
|
} ());
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Statements
|
|
|
|
//
|
|
|
|
|
|
|
|
function parseCommand() {
|
|
|
|
|
2013-12-26 22:38:15 +00:00
|
|
|
function slib(name /* , ...args */) {
|
2012-02-09 03:13:44 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
return 'lib[' + quote(name) + '](' + args.join(',') + ');';
|
|
|
|
}
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
var keyword = test('identifier') ? kws.LET : match('reserved'),
|
2017-08-15 17:10:30 +00:00
|
|
|
name, type, subscripts, is_to, expr, param, args, prompt, trailing, js;
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
switch (keyword) {
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Variable Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.CLEAR: // Clear all variables
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('clear');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.LET: // Assign a variable, LET x = expr
|
2012-02-09 03:13:44 +00:00
|
|
|
name = match('identifier');
|
|
|
|
subscripts = parseSubscripts();
|
|
|
|
match('operator', '=');
|
|
|
|
|
|
|
|
type = vartype(name);
|
|
|
|
if (type === 'int') {
|
|
|
|
expr = 'lib.toint(lib.checkFinite(' + parseNumericExpression() + '))';
|
|
|
|
} else if (type === 'float') {
|
|
|
|
expr = 'lib.checkFinite(' + parseNumericExpression() + ')';
|
|
|
|
} else { // type === 'string')
|
|
|
|
expr = parseStringExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!subscripts) {
|
|
|
|
identifiers.variables[name] = true;
|
|
|
|
return 'state.variables[' + quote(name) + '] = ' + expr;
|
|
|
|
}
|
2013-12-26 22:38:15 +00:00
|
|
|
identifiers.arrays[name] = true;
|
|
|
|
return 'state.arrays[' + quote(name) + '].set([' + subscripts + '], ' + expr + ')';
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.DIM:
|
2012-02-09 03:13:44 +00:00
|
|
|
js = '';
|
|
|
|
do {
|
|
|
|
name = match('identifier');
|
|
|
|
subscripts = parseSubscripts();
|
|
|
|
identifiers.arrays[name] = true;
|
|
|
|
js += slib('dim', quote(name), '[' + subscripts + ']');
|
|
|
|
} while (test('operator', ',', true));
|
|
|
|
return js;
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.DEF: // DEF FN A(X) = expr
|
|
|
|
match("reserved", kws.FN);
|
2012-02-09 03:13:44 +00:00
|
|
|
name = match('identifier');
|
|
|
|
match("operator", "(");
|
|
|
|
param = match('identifier');
|
|
|
|
match("operator", ")");
|
|
|
|
match("operator", "=");
|
|
|
|
|
|
|
|
if (vartype(name) !== vartype(param)) {
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("DEF FN function type and argument type must match");
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-26 22:38:15 +00:00
|
|
|
expr = vartype(name) === 'string' ?
|
|
|
|
parseStringExpression() : parseNumericExpression();
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
return slib('def', quote(name),
|
|
|
|
'function (arg){' +
|
|
|
|
// Save the current context/variable so we can evaluate
|
|
|
|
'var rv,ov=state.variables[' + quote(param) + '];' +
|
|
|
|
// Swap in the argument
|
|
|
|
'state.variables[' + quote(param) + ']=arg;' +
|
|
|
|
// Evaluate the user-function expression
|
|
|
|
'rv=' + expr + ';' +
|
|
|
|
// Restore
|
|
|
|
'state.variables[' + quote(param) + ']=ov;' +
|
|
|
|
'return rv;' +
|
|
|
|
'}');
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Flow Control Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.GOTO: // GOTO linenum
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('goto', match("number"));
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.ON: // ON expr (GOTO|GOSUB) linenum[,linenum ... ]
|
2012-02-09 03:13:44 +00:00
|
|
|
expr = parseNumericExpression();
|
|
|
|
|
|
|
|
keyword = match('reserved');
|
2012-06-20 15:54:32 +00:00
|
|
|
if (keyword !== kws.GOTO && keyword !== kws.GOSUB) {
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Syntax error: Expected " + kws.GOTO + " or " + kws.GOSUB);
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args = [];
|
|
|
|
do {
|
|
|
|
args.push(match("number"));
|
|
|
|
} while (test("operator", ",", true));
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
return slib(keyword === kws.GOSUB ? 'on_gosub' : 'on_goto', expr, args.join(','));
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.GOSUB: // GOSUB linenum
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('gosub', match("number"));
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.RETURN: // Return from the last GOSUB
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('return');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.POP: // Turn last GOSUB into a GOTO
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('pop');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.FOR: // FOR i = m TO n STEP s
|
2012-02-09 03:13:44 +00:00
|
|
|
name = match('identifier');
|
|
|
|
if (vartype(name) !== 'float') {
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Syntax error: Expected floating point variable");
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
identifiers.variables[name] = true;
|
|
|
|
|
|
|
|
return slib('for',
|
|
|
|
quote(name),
|
|
|
|
match("operator", "=") && parseNumericExpression(),
|
2012-06-20 15:54:32 +00:00
|
|
|
match("reserved", kws.TO) && parseNumericExpression(),
|
|
|
|
test('reserved', kws.STEP, true) ? parseNumericExpression() : '1');
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.NEXT: // NEXT [i [,j ... ] ]
|
2012-02-09 03:13:44 +00:00
|
|
|
args = [];
|
|
|
|
if (test('identifier')) {
|
|
|
|
args.push(quote(match('identifier')));
|
|
|
|
while (test("operator", ",", true)) {
|
|
|
|
args.push(quote(match('identifier')));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return slib('next', args.join(','));
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.IF: // IF expr (GOTO linenum|THEN linenum|THEN statement [:statement ... ]
|
2012-02-09 03:13:44 +00:00
|
|
|
expr = parseAnyExpression();
|
|
|
|
|
|
|
|
js = slib('if', expr);
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
if (test('reserved', kws.GOTO, true)) {
|
2012-02-09 03:13:44 +00:00
|
|
|
// IF expr GOTO linenum
|
|
|
|
return js + slib('goto', match('number'));
|
|
|
|
}
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
match('reserved', kws.THEN);
|
2012-02-09 03:13:44 +00:00
|
|
|
if (test('number')) {
|
|
|
|
// IF expr THEN linenum
|
|
|
|
return js + slib('goto', match('number'));
|
|
|
|
}
|
2013-12-26 22:38:15 +00:00
|
|
|
// IF expr THEN statement
|
|
|
|
return js + parseCommand(); // recurse
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.END: // End program
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('end');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.STOP: // Break, like an error
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('stop');
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Error Handling Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.ONERR: // ONERR GOTO linenum
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('onerr_goto',
|
2012-06-20 15:54:32 +00:00
|
|
|
match("reserved", kws.GOTO) && match("number"));
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.RESUME:
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('resume');
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Inline Data Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.RESTORE:
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('restore');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.READ:
|
2012-02-09 03:13:44 +00:00
|
|
|
args = [];
|
|
|
|
do {
|
|
|
|
args.push(parsePValue());
|
|
|
|
} while (test("operator", ",", true));
|
|
|
|
|
|
|
|
return slib('read', args.join(','));
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// I/O Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.PRINT: // Output to the screen
|
2012-02-09 03:13:44 +00:00
|
|
|
args = [];
|
|
|
|
trailing = true;
|
|
|
|
while (!endOfStatement()) {
|
|
|
|
if (test('operator', ';', true)) {
|
|
|
|
trailing = false;
|
|
|
|
} else if (test('operator', ',', true)) {
|
|
|
|
trailing = false;
|
|
|
|
args.push('lib.comma()');
|
2012-06-20 15:54:32 +00:00
|
|
|
} else if (test('reserved', kws.SPC) || test('reserved', kws.TAB)) {
|
2012-02-09 03:13:44 +00:00
|
|
|
trailing = true;
|
|
|
|
keyword = match('reserved');
|
|
|
|
match("operator", "(");
|
|
|
|
expr = parseNumericExpression();
|
|
|
|
match("operator", ")");
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
args.push('lib.' + (keyword === kws.SPC ? 'spc' : 'tab') + '(' + expr + ')');
|
2012-02-09 03:13:44 +00:00
|
|
|
} else {
|
|
|
|
trailing = true;
|
|
|
|
args.push(parseAnyExpression());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (trailing) {
|
|
|
|
args.push(quote('\r'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return slib('print', args.join(','));
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.INPUT: // Read input from keyboard
|
2012-02-09 03:13:44 +00:00
|
|
|
prompt = '?';
|
|
|
|
if (test('string')) {
|
|
|
|
prompt = match('string');
|
|
|
|
match("operator", ";");
|
|
|
|
}
|
|
|
|
|
|
|
|
args = [];
|
|
|
|
|
|
|
|
do {
|
|
|
|
args.push(parsePValue());
|
|
|
|
} while (test("operator", ",", true));
|
|
|
|
|
|
|
|
return slib('input', quote(prompt), args.join(','));
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.GET: // Read character from keyboard
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('get', parsePValue());
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.HOME: // Clear text screen
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('home');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.HTAB: // Set horizontal cursor position
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('htab', parseNumericExpression());
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.VTAB: // Set vertical cursor position
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('vtab', parseNumericExpression());
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.INVERSE: // Inverse text
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('inverse');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.FLASH: // Flashing text
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('flash');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.NORMAL: // Normal text
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('normal');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.TEXT: // Set display mode to text
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('text');
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Miscellaneous Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.NOTRACE: // Turn off line tracing
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('notrace');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.TRACE: // Turn on line tracing
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('trace');
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Lores Graphics
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.GR: // Set display mode to lores graphics, clear screen
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('gr');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.COLOR: // Set lores color
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('color', parseNumericExpression());
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.PLOT: // Plot lores point
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('plot',
|
|
|
|
parseNumericExpression(),
|
|
|
|
match("operator", ",") && parseNumericExpression());
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.HLIN: // Draw lores horizontal line
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('hlin',
|
|
|
|
parseNumericExpression(),
|
|
|
|
match("operator", ",") && parseNumericExpression(),
|
2012-06-20 15:54:32 +00:00
|
|
|
match("reserved", kws.AT) && parseNumericExpression());
|
2012-02-09 03:13:44 +00:00
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.VLIN: // Draw lores vertical line
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('vlin',
|
|
|
|
parseNumericExpression(),
|
|
|
|
match("operator", ",") && parseNumericExpression(),
|
2012-06-20 15:54:32 +00:00
|
|
|
match("reserved", kws.AT) && parseNumericExpression());
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Hires Graphics
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Hires Display Routines
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.HGR: // Set display mode to hires graphics, clear screen
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('hgr');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.HGR2: // Set display mode to hires graphics, page 2, clear screen
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('hgr2');
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.HCOLOR: // Set hires color
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('hcolor', parseNumericExpression());
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.HPLOT: // Draw hires line
|
|
|
|
is_to = test('reserved', kws.TO, true);
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
args = [];
|
|
|
|
do {
|
|
|
|
args.push(parseNumericExpression());
|
|
|
|
match("operator", ",");
|
|
|
|
args.push(parseNumericExpression());
|
2012-06-20 15:54:32 +00:00
|
|
|
} while (test('reserved', kws.TO, true));
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
return slib(is_to ? 'hplot_to' : 'hplot', args.join(','));
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Compatibility shims
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.PR: // Direct output to slot
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('pr#', parseNumericExpression());
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.CALL: // Call native routine
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('call', parseNumericExpression());
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.POKE: // Set memory value
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('poke',
|
|
|
|
parseNumericExpression(),
|
|
|
|
match("operator", ",") && parseNumericExpression());
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.SPEED: // Output speed
|
2012-02-09 03:13:44 +00:00
|
|
|
return slib('speed', parseNumericExpression());
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// INTROSPECTION
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.LIST: // List program statements
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Introspection statement not supported: " + keyword);
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Statements that will never be implemented
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Shape tables
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.ROT: // Set rotation angle for hires shape
|
|
|
|
case kws.SCALE: // Set rotation angle for hires shape
|
|
|
|
case kws.DRAW: // Draw hires shape
|
|
|
|
case kws.XDRAW: // XOR draw hires shape
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Display statement not supported: " + keyword);
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
// Interpreter Routines
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.CONT: // Continue stopped program (immediate mode)
|
|
|
|
case kws.DEL: // Deletes program statements
|
|
|
|
case kws.NEW: // Wipe program
|
|
|
|
case kws.RUN: // Execute program
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Interpreter statement not supported: " + keyword);
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
// Native Routines
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.HIMEM: // Set upper bound of variable memory
|
|
|
|
case kws.IN: // Direct input from slot
|
|
|
|
case kws.LOMEM: // Set low bound of variable memory
|
|
|
|
case kws.WAIT: // Wait for memory value to match a condition
|
|
|
|
case kws.AMPERSAND: // Command hook
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Native interop statement not supported: " + keyword);
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
// Tape Routines
|
2012-06-20 15:54:32 +00:00
|
|
|
case kws.LOAD: // Load program from cassette port
|
|
|
|
case kws.RECALL: // Load array from cassette port
|
|
|
|
case kws.SAVE: // Save program to cassette port
|
|
|
|
case kws.STORE: // Store array to cassette port
|
|
|
|
case kws.SHLOAD: // Load shape table from cassette port
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Tape statement not supported: " + keyword);
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// NYI Statements
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Parts of other statements - AT, FN, STEP, TO, THEN, etc.
|
|
|
|
default:
|
2013-08-17 23:00:23 +00:00
|
|
|
throw parse_error("Syntax error: " + keyword);
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Top-level Program Structure
|
|
|
|
//
|
|
|
|
|
|
|
|
var parseProgram = function() {
|
|
|
|
|
|
|
|
var program = {
|
|
|
|
statements: [], // array of: [ line-number | statement-function ]
|
|
|
|
data: [], // array of [ string | number ]
|
|
|
|
jump: [] // map of: { line-number: statement-index }
|
|
|
|
};
|
|
|
|
|
|
|
|
function mkfun(js) {
|
|
|
|
var fun; // NOTE: for IE; would prefer Function()
|
|
|
|
eval('fun = (function (){' + js + '});');
|
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
|
|
|
function empty_statement() { }
|
|
|
|
|
|
|
|
// Statement = data-declaration | remark | Command | EmptyStatement
|
|
|
|
// Command = identifier /*...*/ | reserved /*...*/
|
|
|
|
function parseStatement() {
|
|
|
|
if (test('data')) {
|
|
|
|
program.data = program.data.concat(match('data'));
|
2014-12-29 03:55:23 +00:00
|
|
|
return undefined;
|
2012-02-09 03:13:44 +00:00
|
|
|
} else if (test('remark', void 0, true)) {
|
2014-12-29 03:55:23 +00:00
|
|
|
return undefined;
|
2012-02-09 03:13:44 +00:00
|
|
|
} else if (test('reserved') || test('identifier')) {
|
2014-12-29 03:55:23 +00:00
|
|
|
return mkfun(parseCommand());
|
2012-02-09 03:13:44 +00:00
|
|
|
} else {
|
|
|
|
// So TRACE output is correct
|
2014-12-29 03:55:23 +00:00
|
|
|
return empty_statement;
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Line = line-number Statement { separator Statement }
|
|
|
|
function parseLine() {
|
2014-12-29 03:55:23 +00:00
|
|
|
var num = match('lineNumber');
|
|
|
|
var statements = [];
|
|
|
|
var statement = parseStatement();
|
|
|
|
if (statement) statements.push(statement);
|
2012-02-09 03:13:44 +00:00
|
|
|
while (test('separator', ':', true)) {
|
2014-12-29 03:55:23 +00:00
|
|
|
statement = parseStatement();
|
|
|
|
if (statement) statements.push(statement);
|
|
|
|
}
|
|
|
|
insertLine(num, statements);
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertLine(number, statements) {
|
|
|
|
var remove = 0;
|
|
|
|
for (var i = 0, len = program.statements.length; i < len; ++i) {
|
|
|
|
if (typeof program.statements[i] !== 'number')
|
|
|
|
continue;
|
|
|
|
if (program.statements[i] < number)
|
|
|
|
continue;
|
|
|
|
if (program.statements[i] === number) {
|
|
|
|
var n = i;
|
|
|
|
do {
|
|
|
|
++n;
|
|
|
|
++remove;
|
|
|
|
} while (n < len && typeof program.statements[n] !== 'number');
|
|
|
|
}
|
|
|
|
break;
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
2014-12-29 03:55:23 +00:00
|
|
|
var args = [i, remove, number].concat(statements);
|
|
|
|
program.statements.splice.apply(program.statements, args);
|
2012-02-09 03:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Program = Line { Line }
|
|
|
|
while (!endOfProgram()) {
|
|
|
|
parseLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Produce jump table
|
|
|
|
program.statements.forEach(function(stmt, index) {
|
|
|
|
if (typeof stmt === 'number') {
|
|
|
|
program.jump[stmt] = index;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
program.variable_identifiers = Object.keys(identifiers.variables);
|
|
|
|
program.array_identifiers = Object.keys(identifiers.arrays);
|
|
|
|
|
|
|
|
return program;
|
|
|
|
};
|
|
|
|
|
|
|
|
return parseProgram();
|
|
|
|
} ());
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
program.init = function init(environment) {
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
// stuff these into runtime library closure/binding
|
|
|
|
env = environment;
|
|
|
|
state = {
|
|
|
|
variables: {},
|
|
|
|
arrays: {},
|
|
|
|
functions: {},
|
|
|
|
data: this.data,
|
|
|
|
data_index: 0,
|
|
|
|
stmt_index: 0,
|
|
|
|
line_number: 0,
|
|
|
|
stack: [],
|
|
|
|
prng: new PRNG(),
|
|
|
|
|
|
|
|
onerr_code: 255,
|
|
|
|
onerr_handler: void 0,
|
|
|
|
trace_mode: false,
|
|
|
|
|
|
|
|
input_continuation: null,
|
|
|
|
|
|
|
|
clear: function() {
|
|
|
|
program.variable_identifiers.forEach(function(identifier) {
|
|
|
|
state.variables[identifier] = vartype(identifier) === 'string' ? '' : 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
program.array_identifiers.forEach(function(identifier) {
|
|
|
|
state.arrays[identifier] = new BASICArray(vartype(identifier));
|
|
|
|
});
|
|
|
|
|
|
|
|
state.functions = {};
|
|
|
|
state.data_index = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
state.clear();
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
state.parsevar = function parsevar(name, subscripts, input) {
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
input = arguments[1];
|
|
|
|
subscripts = void 0;
|
|
|
|
}
|
|
|
|
var value;
|
|
|
|
|
|
|
|
switch (vartype(name)) {
|
|
|
|
case 'string':
|
|
|
|
value = input;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'int':
|
|
|
|
value = Number(input);
|
|
|
|
if (!isFinite(value)) { runtime_error(ERRORS.TYPE_MISMATCH); }
|
|
|
|
value = lib.toint(value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'float':
|
|
|
|
value = Number(input);
|
|
|
|
if (!isFinite(value)) { runtime_error(ERRORS.TYPE_MISMATCH); }
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subscripts) {
|
|
|
|
state.arrays[name].set(subscripts, value);
|
|
|
|
} else {
|
|
|
|
state.variables[name] = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-08-17 22:30:46 +00:00
|
|
|
program.step = function step(driver) {
|
2012-02-09 03:13:44 +00:00
|
|
|
|
|
|
|
function gotoline(line) {
|
|
|
|
if (!{}.hasOwnProperty.call(program.jump, line)) {
|
|
|
|
runtime_error(ERRORS.UNDEFINED_STATEMENT);
|
|
|
|
}
|
|
|
|
state.stmt_index = program.jump[line];
|
|
|
|
}
|
|
|
|
|
|
|
|
var stmt;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// for RuntimeError
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (state.input_continuation) {
|
|
|
|
var cont = state.input_continuation;
|
|
|
|
state.input_continuation = null;
|
|
|
|
cont(state.input_buffer);
|
|
|
|
} else if (state.stmt_index >= program.statements.length) {
|
|
|
|
return basic.STATE_STOPPED;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
stmt = program.statements[state.stmt_index];
|
|
|
|
|
|
|
|
if (typeof stmt === 'number') {
|
|
|
|
state.line_number = stmt;
|
|
|
|
} else if (typeof stmt === 'function') {
|
|
|
|
if (state.trace_mode) {
|
|
|
|
env.tty.writeString('#' + state.line_number + ' ');
|
|
|
|
}
|
|
|
|
stmt();
|
|
|
|
} else {
|
|
|
|
throw "WTF?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.stmt_index += 1;
|
|
|
|
return basic.STATE_RUNNING;
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
// These may throw RuntimeError
|
|
|
|
if (e instanceof basic.RuntimeError) {
|
|
|
|
throw e; // let outer catch block handle it
|
|
|
|
} else if (e instanceof GoToLine) {
|
|
|
|
gotoline(e.line);
|
|
|
|
return basic.STATE_RUNNING;
|
|
|
|
} else if (e instanceof NextLine) {
|
|
|
|
while (state.stmt_index < program.statements.length &&
|
|
|
|
typeof program.statements[state.stmt_index] !== 'number') {
|
|
|
|
state.stmt_index += 1;
|
|
|
|
}
|
|
|
|
return basic.STATE_RUNNING;
|
|
|
|
} else if (e instanceof BlockingInput) {
|
|
|
|
// what to call on next step() after input is handled
|
|
|
|
state.input_continuation = e.callback;
|
|
|
|
|
|
|
|
// call input method to prepare async input
|
|
|
|
e.method(function(v) {
|
|
|
|
state.input_buffer = v;
|
|
|
|
if (driver) { driver(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
return basic.STATE_BLOCKED;
|
|
|
|
} else if (e instanceof EndProgram) {
|
|
|
|
return basic.STATE_STOPPED;
|
|
|
|
} else if (e instanceof Error && /stack|recursion/i.test(e.message)) {
|
|
|
|
// IE: Error "Out of stack space"
|
|
|
|
// Firefox: InternalError "too much recursion"
|
|
|
|
// Safari: RangeError "Maximum call stack size exceeded"
|
|
|
|
// Chrome: RangeError "Maximum call stack size exceeded"
|
|
|
|
// Opera: Error "Maximum recursion depth exceeded"
|
|
|
|
runtime_error(ERRORS.FORMULA_TOO_COMPLEX);
|
|
|
|
} else if (e instanceof Error && /memory|overflow/i.test(e.message)) {
|
|
|
|
// IE: Error "Out of memory"
|
|
|
|
// Firefox: InternalError "allocation size overflow"
|
|
|
|
// Safari: Error "Out of memory"
|
|
|
|
// Chrome: (not catchable)
|
|
|
|
// Opera: (not catchable)
|
|
|
|
runtime_error(ERRORS.OUT_OF_MEMORY);
|
|
|
|
// NOTE: not reliably generated; don't unit test
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (rte) {
|
|
|
|
if (rte instanceof basic.RuntimeError) {
|
|
|
|
state.onerr_code = rte.code || 0;
|
|
|
|
if (state.onerr_handler !== void 0) {
|
2014-12-28 00:51:21 +00:00
|
|
|
state.stack.push({
|
|
|
|
resume_stmt_index: state.stmt_index,
|
|
|
|
resume_line_number: state.line_number
|
|
|
|
});
|
2012-02-09 03:13:44 +00:00
|
|
|
gotoline(state.onerr_handler);
|
|
|
|
return basic.STATE_RUNNING;
|
|
|
|
} else if (rte.code === ERRORS.REENTER[0]) {
|
|
|
|
env.tty.writeString('?REENTER\r');
|
|
|
|
return basic.STATE_RUNNING;
|
|
|
|
} else {
|
|
|
|
// annotate and report to the user
|
|
|
|
rte.message += " in line " + state.line_number;
|
|
|
|
throw rte;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw rte;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return program;
|
|
|
|
};
|
|
|
|
|
|
|
|
return basic;
|
|
|
|
|
|
|
|
} ());
|
|
|
|
|
|
|
|
// TODO: Unit tests for compile errors
|