From 31b4416181158b0e49e0a09da8056298b8522ba2 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Sun, 6 Apr 2014 16:40:55 -0500 Subject: JS: fix web interface. --- js/core.js | 4 ++-- js/josh.js | 1 + js/josh_readline.js | 24 +++++++++++++++++++++--- js/node_readline.js | 3 ++- js/printer.js | 11 +++++++++-- js/reader.js | 4 +++- js/step0_repl.js | 9 ++++++--- js/step1_read_print.js | 14 ++++++++------ js/step2_eval.js | 14 ++++++++------ js/step3_env.js | 16 +++++++++------- js/step4_if_fn_do.js | 18 ++++++++++-------- js/step5_tco.js | 20 +++++++++++--------- js/step6_file.js | 20 +++++++++++--------- js/step7_quote.js | 20 +++++++++++--------- js/step8_macros.js | 20 +++++++++++--------- js/step9_interop.js | 20 +++++++++++--------- js/stepA_more.js | 20 +++++++++++--------- js/types.js | 3 --- 18 files changed, 145 insertions(+), 96 deletions(-) create mode 160000 js/josh.js (limited to 'js') diff --git a/js/core.js b/js/core.js index 48bbe16..b8b0062 100644 --- a/js/core.js +++ b/js/core.js @@ -25,13 +25,13 @@ function str() { } function prn() { - printer.print.apply({}, Array.prototype.map.call(arguments,function(exp) { + printer.println.apply({}, Array.prototype.map.call(arguments,function(exp) { return printer._pr_str(exp, true); })); } function println() { - printer.print.apply({}, Array.prototype.map.call(arguments,function(exp) { + printer.println.apply({}, Array.prototype.map.call(arguments,function(exp) { return printer._pr_str(exp, false); })); } diff --git a/js/josh.js b/js/josh.js new file mode 160000 index 0000000..2572375 --- /dev/null +++ b/js/josh.js @@ -0,0 +1 @@ +Subproject commit 257237550ce9c02cfc426918845e636c763c49bc diff --git a/js/josh_readline.js b/js/josh_readline.js index ff4d201..3d1a3d4 100644 --- a/js/josh_readline.js +++ b/js/josh_readline.js @@ -90,6 +90,7 @@ var Josh = Josh || {}; help: _.template("
Commands:
<% _.each(commands, function(cmd) { %>
 <%- cmd %>
<% }); %>
"), bad_command: _.template('
Unrecognized command: <%=cmd%>
'), input_cmd: _.template('
 
'), + empty_input_cmd: _.template('
'), input_search: _.template('
(reverse-i-search)`\': 
'), suggest: _.template("
<% _.each(suggestions, function(suggestion) { %>
<%- suggestion %>
<% }); %>
") }, @@ -168,6 +169,19 @@ var Josh = Josh || {}; _console.log('refreshed ' + _input_id); }, + println: function(text) { + var lines = text.split(/\n/); + for (var i=0; i"); + promptCounter++; + callback("user>"); }); shell.activate(); + + // map output/print to josh.js output + readline.println = function () { + shell.println(Array.prototype.slice.call(arguments).join(" ")); + }; } diff --git a/js/node_readline.js b/js/node_readline.js index bfd1982..f91bbaf 100644 --- a/js/node_readline.js +++ b/js/node_readline.js @@ -35,4 +35,5 @@ exports.readline = rlwrap.readline = function(prompt) { } return line; -} +}; +var readline = exports; diff --git a/js/printer.js b/js/printer.js index 0d84cc6..575072e 100644 --- a/js/printer.js +++ b/js/printer.js @@ -3,9 +3,14 @@ var printer = {}; if (typeof module !== 'undefined') { var types = require('./types'); // map output/print to console.log - var print = exports.print = function () { console.log.apply(console, arguments); }; + printer.println = exports.println = function () { + console.log.apply(console, arguments); + }; } else { var exports = printer; + printer.println = function() { + readline.println.apply(null, arguments); // josh_readline.js + } } function _pr_str(obj, print_readably) { @@ -27,7 +32,9 @@ function _pr_str(obj, print_readably) { return "{" + ret.join(' ') + "}"; case 'string': if (print_readably) { - return '"' + obj.replace(/\\/, "\\\\").replace(/"/g, '\\"') + '"'; + return '"' + obj.replace(/\\/, "\\\\") + .replace(/"/g, '\\"') + .replace(/\n/g, "\\n") + '"'; // string } else { return obj; } diff --git a/js/reader.js b/js/reader.js index f19010d..3f2f6ca 100644 --- a/js/reader.js +++ b/js/reader.js @@ -32,7 +32,9 @@ function read_atom (reader) { } else if (token.match(/^-?[0-9][0-9.]*$/)) { return parseFloat(token,10); // float } else if (token[0] === "\"") { - return token.slice(1,token.length-1).replace(/\\"/g, '"'); // string + return token.slice(1,token.length-1) + .replace(/\\"/g, '"') + .replace(/\\n/g, "\n"); // string } else if (token === "nil") { return null; } else if (token === "true") { diff --git a/js/step0_repl.js b/js/step0_repl.js index 5fa10f2..1d2bbfb 100644 --- a/js/step0_repl.js +++ b/js/step0_repl.js @@ -24,7 +24,8 @@ if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, function(exc) { - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } }); } else if (require.main === module) { // Synchronous node.js commandline mode @@ -32,9 +33,11 @@ if (typeof require === 'undefined') { var line = readline.readline("user> "); if (line === null) { break; } try { - if (line) { console.log(rep(line)); } + if (line) { printer.println(rep(line)); } } catch (exc) { - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } } } } else { diff --git a/js/step1_read_print.js b/js/step1_read_print.js index 264b6c6..ef9b6bc 100644 --- a/js/step1_read_print.js +++ b/js/step1_read_print.js @@ -1,8 +1,8 @@ -var types = require('./types'); -var reader = require('./reader'); -var printer = require('./printer'); if (typeof module !== 'undefined') { + var types = require('./types'); var readline = require('./node_readline'); + var reader = require('./reader'); + var printer = require('./printer'); } // read @@ -29,7 +29,8 @@ if (typeof require === 'undefined') { readline.rlwrap(function(line) { return rep(line); }, function(exc) { if (exc instanceof reader.BlankException) { return; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } }); } else if (require.main === module) { // Synchronous node.js commandline mode @@ -37,10 +38,11 @@ if (typeof require === 'undefined') { var line = readline.readline("user> "); if (line === null) { break; } try { - if (line) { console.log(rep(line)); } + if (line) { printer.println(rep(line)); } } catch (exc) { if (exc instanceof reader.BlankException) { continue; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } } } } else { diff --git a/js/step2_eval.js b/js/step2_eval.js index f5efa2c..f111a58 100644 --- a/js/step2_eval.js +++ b/js/step2_eval.js @@ -1,8 +1,8 @@ -var types = require('./types'); -var reader = require('./reader'); -var printer = require('./printer'); if (typeof module !== 'undefined') { + var types = require('./types'); var readline = require('./node_readline'); + var reader = require('./reader'); + var printer = require('./printer'); } // read @@ -65,7 +65,8 @@ if (typeof require === 'undefined') { readline.rlwrap(function(line) { return rep(line); }, function(exc) { if (exc instanceof reader.BlankException) { return; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } }); } else if (require.main === module) { // Synchronous node.js commandline mode @@ -73,10 +74,11 @@ if (typeof require === 'undefined') { var line = readline.readline("user> "); if (line === null) { break; } try { - if (line) { console.log(rep(line)); } + if (line) { printer.println(rep(line)); } } catch (exc) { if (exc instanceof reader.BlankException) { continue; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } } } } else { diff --git a/js/step3_env.js b/js/step3_env.js index 41e21c1..1000437 100644 --- a/js/step3_env.js +++ b/js/step3_env.js @@ -1,9 +1,9 @@ -var types = require('./types'); -var reader = require('./reader'); -var printer = require('./printer'); -var Env = require('./env').Env; if (typeof module !== 'undefined') { + var types = require('./types'); var readline = require('./node_readline'); + var reader = require('./reader'); + var printer = require('./printer'); + var Env = require('./env').Env; } // read @@ -80,7 +80,8 @@ if (typeof require === 'undefined') { readline.rlwrap(function(line) { return rep(line); }, function(exc) { if (exc instanceof reader.BlankException) { return; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } }); } else if (require.main === module) { // Synchronous node.js commandline mode @@ -88,10 +89,11 @@ if (typeof require === 'undefined') { var line = readline.readline("user> "); if (line === null) { break; } try { - if (line) { console.log(rep(line)); } + if (line) { printer.println(rep(line)); } } catch (exc) { if (exc instanceof reader.BlankException) { continue; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } } } } else { diff --git a/js/step4_if_fn_do.js b/js/step4_if_fn_do.js index 37803ef..0bf988d 100644 --- a/js/step4_if_fn_do.js +++ b/js/step4_if_fn_do.js @@ -1,10 +1,10 @@ -var types = require('./types'); -var reader = require('./reader'); -var printer = require('./printer'); -var Env = require('./env').Env; -var core = require('./core'); if (typeof module !== 'undefined') { + var types = require('./types'); var readline = require('./node_readline'); + var reader = require('./reader'); + var printer = require('./printer'); + var Env = require('./env').Env; + var core = require('./core'); } // read @@ -96,7 +96,8 @@ if (typeof require === 'undefined') { readline.rlwrap(function(line) { return rep(line); }, function(exc) { if (exc instanceof reader.BlankException) { return; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } }); } else if (require.main === module) { // Synchronous node.js commandline mode @@ -104,10 +105,11 @@ if (typeof require === 'undefined') { var line = readline.readline("user> "); if (line === null) { break; } try { - if (line) { console.log(rep(line)); } + if (line) { printer.println(rep(line)); } } catch (exc) { if (exc instanceof reader.BlankException) { continue; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } } } } else { diff --git a/js/step5_tco.js b/js/step5_tco.js index 2d1793d..d61e243 100644 --- a/js/step5_tco.js +++ b/js/step5_tco.js @@ -1,10 +1,10 @@ -var types = require('./types'); -var reader = require('./reader'); -var printer = require('./printer'); -var Env = require('./env').Env; -var core = require('./core'); if (typeof module !== 'undefined') { + var types = require('./types'); var readline = require('./node_readline'); + var reader = require('./reader'); + var printer = require('./printer'); + var Env = require('./env').Env; + var core = require('./core'); } // read @@ -36,7 +36,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //console.log("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", types._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -106,7 +106,8 @@ if (typeof require === 'undefined') { readline.rlwrap(function(line) { return rep(line); }, function(exc) { if (exc instanceof reader.BlankException) { return; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } }); } else if (require.main === module) { // Synchronous node.js commandline mode @@ -114,10 +115,11 @@ if (typeof require === 'undefined') { var line = readline.readline("user> "); if (line === null) { break; } try { - if (line) { console.log(rep(line)); } + if (line) { printer.println(rep(line)); } } catch (exc) { if (exc instanceof reader.BlankException) { continue; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } } } } else { diff --git a/js/step6_file.js b/js/step6_file.js index df216da..7bbb1b4 100644 --- a/js/step6_file.js +++ b/js/step6_file.js @@ -1,10 +1,10 @@ -var types = require('./types'); -var reader = require('./reader'); -var printer = require('./printer'); -var Env = require('./env').Env; -var core = require('./core'); if (typeof module !== 'undefined') { + var types = require('./types'); var readline = require('./node_readline'); + var reader = require('./reader'); + var printer = require('./printer'); + var Env = require('./env').Env; + var core = require('./core'); } // read @@ -36,7 +36,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //console.log("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", types._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -117,7 +117,8 @@ if (typeof process !== 'undefined' && process.argv.length > 2) { readline.rlwrap(function(line) { return rep(line); }, function(exc) { if (exc instanceof reader.BlankException) { return; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } }); } else if (require.main === module) { // Synchronous node.js commandline mode @@ -125,10 +126,11 @@ if (typeof process !== 'undefined' && process.argv.length > 2) { var line = readline.readline("user> "); if (line === null) { break; } try { - if (line) { console.log(rep(line)); } + if (line) { printer.println(rep(line)); } } catch (exc) { if (exc instanceof reader.BlankException) { continue; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } } } } else { diff --git a/js/step7_quote.js b/js/step7_quote.js index 9721d59..60ae0e8 100644 --- a/js/step7_quote.js +++ b/js/step7_quote.js @@ -1,10 +1,10 @@ -var types = require('./types'); -var reader = require('./reader'); -var printer = require('./printer'); -var Env = require('./env').Env; -var core = require('./core'); if (typeof module !== 'undefined') { + var types = require('./types'); var readline = require('./node_readline'); + var reader = require('./reader'); + var printer = require('./printer'); + var Env = require('./env').Env; + var core = require('./core'); } // read @@ -52,7 +52,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //console.log("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", types._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -137,7 +137,8 @@ if (typeof process !== 'undefined' && process.argv.length > 2) { readline.rlwrap(function(line) { return rep(line); }, function(exc) { if (exc instanceof reader.BlankException) { return; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } }); } else if (require.main === module) { // Synchronous node.js commandline mode @@ -145,10 +146,11 @@ if (typeof process !== 'undefined' && process.argv.length > 2) { var line = readline.readline("user> "); if (line === null) { break; } try { - if (line) { console.log(rep(line)); } + if (line) { printer.println(rep(line)); } } catch (exc) { if (exc instanceof reader.BlankException) { continue; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } } } } else { diff --git a/js/step8_macros.js b/js/step8_macros.js index 3ad3e31..5e39b5b 100644 --- a/js/step8_macros.js +++ b/js/step8_macros.js @@ -1,10 +1,10 @@ -var types = require('./types'); -var reader = require('./reader'); -var printer = require('./printer'); -var Env = require('./env').Env; -var core = require('./core'); if (typeof module !== 'undefined') { + var types = require('./types'); var readline = require('./node_readline'); + var reader = require('./reader'); + var printer = require('./printer'); + var Env = require('./env').Env; + var core = require('./core'); } // read @@ -67,7 +67,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //console.log("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", types._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -161,7 +161,8 @@ if (typeof process !== 'undefined' && process.argv.length > 2) { readline.rlwrap(function(line) { return rep(line); }, function(exc) { if (exc instanceof reader.BlankException) { return; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } }); } else if (require.main === module) { // Synchronous node.js commandline mode @@ -169,10 +170,11 @@ if (typeof process !== 'undefined' && process.argv.length > 2) { var line = readline.readline("user> "); if (line === null) { break; } try { - if (line) { console.log(rep(line)); } + if (line) { printer.println(rep(line)); } } catch (exc) { if (exc instanceof reader.BlankException) { continue; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } } } } else { diff --git a/js/step9_interop.js b/js/step9_interop.js index 3c83e51..8aa8281 100644 --- a/js/step9_interop.js +++ b/js/step9_interop.js @@ -1,10 +1,10 @@ -var types = require('./types'); -var reader = require('./reader'); -var printer = require('./printer'); -var Env = require('./env').Env; -var core = require('./core'); if (typeof module !== 'undefined') { + var types = require('./types'); var readline = require('./node_readline'); + var reader = require('./reader'); + var printer = require('./printer'); + var Env = require('./env').Env; + var core = require('./core'); } // read @@ -67,7 +67,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //console.log("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", types._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -167,7 +167,8 @@ if (typeof process !== 'undefined' && process.argv.length > 2) { readline.rlwrap(function(line) { return rep(line); }, function(exc) { if (exc instanceof reader.BlankException) { return; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } }); } else if (require.main === module) { // Synchronous node.js commandline mode @@ -175,10 +176,11 @@ if (typeof process !== 'undefined' && process.argv.length > 2) { var line = readline.readline("user> "); if (line === null) { break; } try { - if (line) { console.log(rep(line)); } + if (line) { printer.println(rep(line)); } } catch (exc) { if (exc instanceof reader.BlankException) { continue; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } } } } else { diff --git a/js/stepA_more.js b/js/stepA_more.js index a4e1bda..c37668a 100644 --- a/js/stepA_more.js +++ b/js/stepA_more.js @@ -1,10 +1,10 @@ -var types = require('./types'); -var reader = require('./reader'); -var printer = require('./printer'); -var Env = require('./env').Env; -var core = require('./core'); if (typeof module !== 'undefined') { + var types = require('./types'); var readline = require('./node_readline'); + var reader = require('./reader'); + var printer = require('./printer'); + var Env = require('./env').Env; + var core = require('./core'); } // read @@ -67,7 +67,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //console.log("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", types._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -181,7 +181,8 @@ if (typeof process !== 'undefined' && process.argv.length > 2) { readline.rlwrap(function(line) { return rep(line); }, function(exc) { if (exc instanceof reader.BlankException) { return; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } }); } else if (require.main === module) { // Synchronous node.js commandline mode @@ -189,10 +190,11 @@ if (typeof process !== 'undefined' && process.argv.length > 2) { var line = readline.readline("user> "); if (line === null) { break; } try { - if (line) { console.log(rep(line)); } + if (line) { printer.println(rep(line)); } } catch (exc) { if (exc instanceof reader.BlankException) { continue; } - if (exc.stack) { console.log(exc.stack); } else { console.log(exc); } + if (exc.stack) { printer.println(exc.stack); } + else { printer.println(exc); } } } } else { diff --git a/js/types.js b/js/types.js index 7fd2962..6d7de0f 100644 --- a/js/types.js +++ b/js/types.js @@ -2,9 +2,6 @@ var types = {}; if (typeof module === 'undefined') { var exports = types; -} else { - // map output/print to console.log - var print = exports.print = function () { console.log.apply(console, arguments); }; } // General fucnctions -- cgit v1.2.3