Remove command line support. Depend on CDN for polyfills

This commit is contained in:
Joshua Bell 2016-03-08 20:31:24 -08:00
parent fa1695d752
commit a8f92fad0a
8 changed files with 5 additions and 162 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "polyfill"]
path = polyfill
url = https://github.com/inexorabletash/polyfill.git

View File

@ -3,8 +3,6 @@ jsbasic - Applesoft BASIC in JavaScript
This is hosted for playing with at http://inexorabletash.github.io/jsbasic/
Use `git clone --recursive` to get [polyfill](http://github.com/inexorabletash/polyfill) for older browsers.
Notes & Known Issues
--------------------
* The BASIC program is compiled to JavaScript before execution. Syntax errors are therefore detected at compile-time rather than at run-time as on a traditional interpreter. For example, the following program would run without errors on an Apple since the erroneous second statement is never reached. `10 END : CHR$(PRINT)`
@ -23,8 +21,3 @@ Notes & Known Issues
* `CHR$()` values > 255 do interesting things
* `HSCRN(x, y)` allows probing the hi-res screen
* hexadecimal literals e.g. `$C010` can be used as numbers
You can run your basic programs from the command line (with only basic text input and output, and no graphics or DOS commands):
* Clone the repository locally
* On Windows, run from a command prompt via: `cscript.exe cbasic.js your_program.txt`
* On Mac/Linux, install Mozilla Rhino, run from the command prompt via: `java -jar PATH_TO/js.jar cbasic.js your_program.txt`

View File

@ -28,9 +28,6 @@
// // driver will also be called after input is unblocked
// // driver may want to yield via setTimeout() after N steps
// ES6 polyfill:
if (!String.prototype.repeat) { String.prototype.repeat = function(n){return Array(n+1).join(this); }; }
this.basic = (function() {
var basic = {

143
cbasic.js
View File

@ -1,143 +0,0 @@
// Console:
// For Mozilla Rhino: rhino cbasic.js your_basic_program.txt
// For Windows CScript: cscript.exe cbasic.js your_basic_program.txt
if(!Object.keys){Object.keys=function(o){if(o!==Object(o))throw new TypeError();var ret=[],p;for(p in o)if(Object.prototype.hasOwnProperty.call(o,p))ret.push(p);return ret;};}
if(!Array.prototype.forEach){Array.prototype.forEach=function(fun){if(this===void 0||this===null){throw new TypeError();}var t=Object(this);var len=t.length>>>0;if(typeof fun!=="function"){throw new TypeError();}var thisp=arguments[1],i;for(i=0;i<len;i++){if(i in t){fun.call(thisp,t[i],i,t);}}};}
if(!Array.prototype.map){Array.prototype.map=function(fun){if(this===void 0||this===null){throw new TypeError();}var t=Object(this);var len=t.length>>>0;if(typeof fun!=="function"){throw new TypeError();}var res=[];res.length=len;var thisp=arguments[1],i;for(i=0;i<len;i++){if(i in t){res[i]=fun.call(thisp,t[i],i,t);}}return res;};}
if(!Array.prototype.reduce){Array.prototype.reduce=function(fun){if(this===void 0||this===null){throw new TypeError();}var t=Object(this);var len=t.length>>>0;if(typeof fun!=="function"){throw new TypeError();}if(len===0&&arguments.length===1){throw new TypeError();}var k=0;var accumulator;if(arguments.length>=2){accumulator=arguments[1];}else{do{if(k in t){accumulator=t[k++];break;}if(++k>=len){throw new TypeError();}}while(true);}while(k<len){if(k in t){accumulator=fun.call(undefined,accumulator,t[k],k,t);}k++;}return accumulator;};}
var arguments, args = arguments;
var host = (function() {
if (typeof WScript === 'object') {
// Microsoft Windows Scripting engine
args = (function() {
var a = [];
for (var i = 0; i < WScript.Arguments.length; ++i) {
a.push(WScript.Arguments(i));
}
return a;
}());
return {
name: "cscript.exe",
fetch: function(filename) {
var file = new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(new ActiveXObject("WScript.Shell").CurrentDirectory + "\\" + filename),
data = file.ReadAll();
file.Close();
return data;
},
console: {
gets: function() { return WScript.StdIn.ReadLine(); },
getc: function() { return WScript.StdIn.ReadLine().substring(0, 1); },
puts: function(s) { WScript.StdOut.Write(s); },
putc: function(c) { WScript.StdOut.Write(c); },
errs: function(s) { WScript.StdErr.Write(s); }
},
quit: function(s) { WScript.Quit(s); }
};
}
if (typeof java === 'object') {
// Mozilla Rhino
return {
name: "rhino",
fetch: function(filename) {
var r = new java.io.BufferedReader(new java.io.FileReader(new java.io.File(filename))),
sb = new java.lang.StringBuilder(), s;
do {
s = r.readLine();
if (s !== null) {
sb.append(s).append('\n');
}
} while (s !== null);
return String(sb);
},
console: (function() {
var stdin = new java.io.BufferedReader(new java.io.InputStreamReader(java.lang.System['in']));
return {
gets: function() { return String(stdin.readLine()); },
getc: function() { return String(stdin.readLine()).substring(0, 1); },
puts: function(s) { java.lang.System.out.print(s); },
putc: function(c) { java.lang.System.out.print(c); },
errs: function(s) { java.lang.System.err.print(s); }
};
}()),
quit: function(s) { quit(s); }
};
}
throw 'Unknown script host';
}());
if (args.length !== 1) {
host.console.errs("Usage: " + host + " cbasic.js program_name\n");
host.quit(1);
}
var filename = args[0];
eval(host.fetch("basic.js"));
// Compile
var program = (function() {
host.console.puts('Compiling...\n');
try {
var source = host.fetch(filename);
return basic.compile(source);
} catch (pe) {
if (pe instanceof basic.ParseError) {
host.console.errs(pe.message + ' (source line: ' + pe.line + ', column: ' + pe.column + ')\n');
host.quit(1);
} else {
throw pe;
}
}
}());
// Run
(function() {
host.console.puts('Running...\n');
program.init({
tty: {
getCursorPosition: function() { return { x: 0, y: 0 }; },
setCursorPosition: function() { },
getScreenSize: function() { return { width: 80, height: 24 }; },
writeChar: function(ch) { host.console.putc(ch.replace(/\r/g, '\n')); },
writeString: function(string) { host.console.puts(string.replace(/\r/g, '\n')); },
readChar: function(callback) {
callback(host.console.getc());
},
readLine: function(callback, prompt) {
host.console.puts(prompt);
callback(host.console.gets().replace(/[\r\n]*/, ''));
}
}
});
var state;
try {
do {
state = program.step();
} while (state !== basic.STATE_STOPPED);
} catch (rte) {
if (rte instanceof basic.RuntimeError) {
host.console.errs(rte.message + '\n');
host.console.quit(1);
} else {
throw rte;
}
}
}());

View File

@ -9,8 +9,8 @@
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="display.css">
<script src="polyfill/polyfill.min.js"></script>
<script src="polyfill/keyboard.js"></script>
<script src="https://cdn.rawgit.com/inexorabletash/polyfill/v0.1.16/polyfill.min.js"></script>
<script src="https://cdn.rawgit.com/inexorabletash/polyfill/v0.1.16/keyboard.js"></script>
<!-- CodeMirror syntax highlighting - this is optional -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.12.0/codemirror.min.js"></script>

@ -1 +0,0 @@
Subproject commit 51e0b2372803abd1c72f740d9fa15ee398b9a5a2

View File

@ -603,7 +603,7 @@ function which implements the logic for walking over the array.<p>
</ol>
</section>
<script src="polyfill/polyfill.min.js"></script>
<script src="https://cdn.rawgit.com/inexorabletash/polyfill/v0.1.16/polyfill.min.js"></script>
<script>
(function buildTOC(el) {
var html = [];

View File

@ -8,8 +8,8 @@
document.write('<link rel="stylesheet" href="' + baseURL + 'display.css">');
function load(url) { document.write('<script src="' + baseURL + url + '"></script>'); }
load('polyfill/polyfill.js');
load('polyfill/keyboard.js');
load('https://cdn.rawgit.com/inexorabletash/polyfill/v0.1.16/polyfill.min.js');
load('https://cdn.rawgit.com/inexorabletash/polyfill/v0.1.16/keyboard.js');
load('tty.js');
load('lores.js');
load('hires.js');