diff options
Diffstat (limited to 'js')
| -rw-r--r-- | js/core.js | 10 | ||||
| -rw-r--r-- | js/step3_env.js | 9 | ||||
| -rw-r--r-- | js/step4_if_fn_do.js | 6 | ||||
| -rw-r--r-- | js/step5_tco.js | 6 | ||||
| -rw-r--r-- | js/step6_file.js | 12 | ||||
| -rw-r--r-- | js/step7_quote.js | 12 | ||||
| -rw-r--r-- | js/step8_macros.js | 14 | ||||
| -rw-r--r-- | js/step9_interop.js | 14 | ||||
| -rw-r--r-- | js/stepA_more.js | 15 |
9 files changed, 40 insertions, 58 deletions
@@ -4,6 +4,8 @@ if (typeof module === 'undefined') { var exports = core; } else { var types = require('./types'), + readline = require('./node_readline'), + reader = require('./reader'), printer = require('./printer'); } @@ -36,6 +38,10 @@ function println() { })); } +function slurp(f) { + return require('fs').readFileSync(f, 'utf-8'); +} + // Hash Map functions function assoc(src_hm) { @@ -144,10 +150,14 @@ var ns = {'type': types._obj_type, 'false?': types._false_Q, 'symbol': types._symbol, 'symbol?': types._symbol_Q, + 'pr-str': pr_str, 'str': str, 'prn': prn, 'println': println, + 'readline': readline.readline, + 'read-string': reader.read_str, + 'slurp': slurp, '<' : function(a,b){return a<b;}, '<=' : function(a,b){return a<=b;}, '>' : function(a,b){return a>b;}, diff --git a/js/step3_env.js b/js/step3_env.js index 86981d2..7dbefc4 100644 --- a/js/step3_env.js +++ b/js/step3_env.js @@ -69,12 +69,11 @@ function PRINT(exp) { // repl var repl_env = new Env(); var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; -_ref = function (k,v) { repl_env.set(k, v); } -_ref('+', function(a,b){return a+b;}); -_ref('-', function(a,b){return a-b;}); -_ref('*', function(a,b){return a*b;}); -_ref('/', function(a,b){return a/b;}); +repl_env.set('+', function(a,b){return a+b;}); +repl_env.set('-', function(a,b){return a-b;}); +repl_env.set('*', function(a,b){return a*b;}); +repl_env.set('/', function(a,b){return a/b;}); if (typeof require === 'undefined') { // Asynchronous browser mode diff --git a/js/step4_if_fn_do.js b/js/step4_if_fn_do.js index a80190e..7d679a9 100644 --- a/js/step4_if_fn_do.js +++ b/js/step4_if_fn_do.js @@ -84,12 +84,12 @@ function PRINT(exp) { // repl var repl_env = new Env(); var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; -_ref = function (k,v) { repl_env.set(k, v); } -// Import core functions +// core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } +repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); -// Defined using the language itself +// core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); if (typeof require === 'undefined') { diff --git a/js/step5_tco.js b/js/step5_tco.js index dbeaa90..320807c 100644 --- a/js/step5_tco.js +++ b/js/step5_tco.js @@ -93,12 +93,12 @@ function PRINT(exp) { // repl var repl_env = new Env(); var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; -_ref = function (k,v) { repl_env.set(k, v); } -// Import core functions +// core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } +repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); -// Defined using the language itself +// core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); if (typeof require === 'undefined') { diff --git a/js/step6_file.js b/js/step6_file.js index ee39501..aed1825 100644 --- a/js/step6_file.js +++ b/js/step6_file.js @@ -93,18 +93,12 @@ function PRINT(exp) { // repl var repl_env = new Env(); var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; -_ref = function (k,v) { repl_env.set(k, v); } -// Import core functions +// core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } +repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); -_ref('read-string', reader.read_str); -_ref('eval', function(ast) { return EVAL(ast, repl_env); }); -_ref('slurp', function(f) { - return require('fs').readFileSync(f, 'utf-8'); -}); - -// Defined using the language itself +// core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); diff --git a/js/step7_quote.js b/js/step7_quote.js index 6782647..a7f4535 100644 --- a/js/step7_quote.js +++ b/js/step7_quote.js @@ -117,18 +117,12 @@ function PRINT(exp) { // repl var repl_env = new Env(); var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; -_ref = function (k,v) { repl_env.set(k, v); } -// Import core functions +// core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } +repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); -_ref('read-string', reader.read_str); -_ref('eval', function(ast) { return EVAL(ast, repl_env); }); -_ref('slurp', function(f) { - return require('fs').readFileSync(f, 'utf-8'); -}); - -// Defined using the language itself +// core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); diff --git a/js/step8_macros.js b/js/step8_macros.js index f50bba4..1268a4d 100644 --- a/js/step8_macros.js +++ b/js/step8_macros.js @@ -141,20 +141,16 @@ function PRINT(exp) { // repl var repl_env = new Env(); var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; -_ref = function (k,v) { repl_env.set(k, v); } -// Import core functions +// core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } +repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); -_ref('read-string', reader.read_str); -_ref('eval', function(ast) { return EVAL(ast, repl_env); }); -_ref('slurp', function(f) { - return require('fs').readFileSync(f, 'utf-8'); -}); - -// Defined using the language itself +// core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); +rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"); +rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"); if (typeof process !== 'undefined' && process.argv.length > 2) { for (var i=2; i < process.argv.length; i++) { diff --git a/js/step9_interop.js b/js/step9_interop.js index 4987931..1fd07d4 100644 --- a/js/step9_interop.js +++ b/js/step9_interop.js @@ -147,20 +147,16 @@ function PRINT(exp) { // repl var repl_env = new Env(); var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; -_ref = function (k,v) { repl_env.set(k, v); } -// Import core functions +// core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } +repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); -_ref('read-string', reader.read_str); -_ref('eval', function(ast) { return EVAL(ast, repl_env); }); -_ref('slurp', function(f) { - return require('fs').readFileSync(f, 'utf-8'); -}); - -// Defined using the language itself +// core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); +rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"); +rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"); if (typeof process !== 'undefined' && process.argv.length > 2) { for (var i=2; i < process.argv.length; i++) { diff --git a/js/stepA_more.js b/js/stepA_more.js index a4fe21a..06eb43d 100644 --- a/js/stepA_more.js +++ b/js/stepA_more.js @@ -158,23 +158,16 @@ function PRINT(exp) { // repl var repl_env = new Env(); var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; -_ref = function (k,v) { repl_env.set(k, v); } -// Import core functions +// core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } +repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); -_ref('readline', readline.readline) -_ref('read-string', reader.read_str); -_ref('eval', function(ast) { return EVAL(ast, repl_env); }); -_ref('slurp', function(f) { - return require('fs').readFileSync(f, 'utf-8'); -}); - -// Defined using the language itself +// core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); +rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"); rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"); -rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); if (typeof process !== 'undefined' && process.argv.length > 2) { for (var i=2; i < process.argv.length; i++) { |
