aboutsummaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua')
-rw-r--r--lua/core.lua2
-rw-r--r--lua/readline.lua9
-rwxr-xr-xlua/step1_read_print.lua4
-rwxr-xr-xlua/step2_eval.lua4
-rwxr-xr-xlua/step3_env.lua4
-rwxr-xr-xlua/step4_if_fn_do.lua4
-rwxr-xr-xlua/step5_tco.lua4
-rwxr-xr-xlua/step6_file.lua5
-rwxr-xr-xlua/step7_quote.lua5
-rwxr-xr-xlua/step8_macros.lua5
-rwxr-xr-xlua/step9_try.lua31
-rwxr-xr-xlua/stepA_mal.lua5
12 files changed, 69 insertions, 13 deletions
diff --git a/lua/core.lua b/lua/core.lua
index 3f46aeb..279a6d6 100644
--- a/lua/core.lua
+++ b/lua/core.lua
@@ -23,12 +23,14 @@ end
function prn(...)
print(table.concat(
utils.map(function(e) return _pr_str(e, true) end, arg), " "))
+ io.flush()
return Nil
end
function println(...)
print(table.concat(
utils.map(function(e) return _pr_str(e, false) end, arg), " "))
+ io.flush()
return Nil
end
diff --git a/lua/readline.lua b/lua/readline.lua
index a75f4ff..5acdb54 100644
--- a/lua/readline.lua
+++ b/lua/readline.lua
@@ -5,6 +5,8 @@ local M = {}
local history_loaded = false
local history_file = os.getenv("HOME") .. "/.mal-history"
+M.raw = false
+
function M.readline(prompt)
if not history_loaded then
history_loaded = true
@@ -13,7 +15,12 @@ function M.readline(prompt)
end
end
- line = LN.linenoise(prompt)
+ if M.raw then
+ io.write(prompt); io.flush();
+ line = io.read()
+ else
+ line = LN.linenoise(prompt)
+ end
if line then
LN.historyadd(line)
local f = io.open(history_file, "a")
diff --git a/lua/step1_read_print.lua b/lua/step1_read_print.lua
index abd555d..46d71f5 100755
--- a/lua/step1_read_print.lua
+++ b/lua/step1_read_print.lua
@@ -25,6 +25,10 @@ function rep(str)
return PRINT(EVAL(READ(str),""))
end
+if #arg > 0 and arg[1] == "--raw" then
+ readline.raw = true
+end
+
while true do
line = readline.readline("user> ")
if not line then break end
diff --git a/lua/step2_eval.lua b/lua/step2_eval.lua
index 7487064..22ac8cf 100755
--- a/lua/step2_eval.lua
+++ b/lua/step2_eval.lua
@@ -58,6 +58,10 @@ function rep(str)
return PRINT(EVAL(READ(str),repl_env))
end
+if #arg > 0 and arg[1] == "--raw" then
+ readline.raw = true
+end
+
while true do
line = readline.readline("user> ")
if not line then break end
diff --git a/lua/step3_env.lua b/lua/step3_env.lua
index ed2e62a..dbdb879 100755
--- a/lua/step3_env.lua
+++ b/lua/step3_env.lua
@@ -71,6 +71,10 @@ repl_env:set(types.Symbol:new('-'), function(a,b) return a-b end)
repl_env:set(types.Symbol:new('*'), function(a,b) return a*b end)
repl_env:set(types.Symbol:new('/'), function(a,b) return math.floor(a/b) end)
+if #arg > 0 and arg[1] == "--raw" then
+ readline.raw = true
+end
+
while true do
line = readline.readline("user> ")
if not line then break end
diff --git a/lua/step4_if_fn_do.lua b/lua/step4_if_fn_do.lua
index e211973..65b3a0a 100755
--- a/lua/step4_if_fn_do.lua
+++ b/lua/step4_if_fn_do.lua
@@ -89,6 +89,10 @@ end
-- core.mal: defined using mal
rep("(def! not (fn* (a) (if a false true)))")
+if #arg > 0 and arg[1] == "--raw" then
+ readline.raw = true
+end
+
while true do
line = readline.readline("user> ")
if not line then break end
diff --git a/lua/step5_tco.lua b/lua/step5_tco.lua
index fa4e41f..237f5ea 100755
--- a/lua/step5_tco.lua
+++ b/lua/step5_tco.lua
@@ -97,6 +97,10 @@ end
-- core.mal: defined using mal
rep("(def! not (fn* (a) (if a false true)))")
+if #arg > 0 and arg[1] == "--raw" then
+ readline.raw = true
+end
+
while true do
line = readline.readline("user> ")
if not line then break end
diff --git a/lua/step6_file.lua b/lua/step6_file.lua
index b109a90..7992888 100755
--- a/lua/step6_file.lua
+++ b/lua/step6_file.lua
@@ -101,6 +101,11 @@ repl_env:set(types.Symbol:new('*ARGV*'), types.List:new(types.slice(arg,2)))
rep("(def! not (fn* (a) (if a false true)))")
rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
+if #arg > 0 and arg[1] == "--raw" then
+ readline.raw = true
+ table.remove(arg,1)
+end
+
if #arg > 0 then
rep("(load-file \""..arg[1].."\")")
os.exit(0)
diff --git a/lua/step7_quote.lua b/lua/step7_quote.lua
index 974e342..32bf60c 100755
--- a/lua/step7_quote.lua
+++ b/lua/step7_quote.lua
@@ -127,6 +127,11 @@ repl_env:set(types.Symbol:new('*ARGV*'), types.List:new(types.slice(arg,2)))
rep("(def! not (fn* (a) (if a false true)))")
rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
+if #arg > 0 and arg[1] == "--raw" then
+ readline.raw = true
+ table.remove(arg,1)
+end
+
if #arg > 0 then
rep("(load-file \""..arg[1].."\")")
os.exit(0)
diff --git a/lua/step8_macros.lua b/lua/step8_macros.lua
index 46a5881..25a9238 100755
--- a/lua/step8_macros.lua
+++ b/lua/step8_macros.lua
@@ -156,6 +156,11 @@ 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 #arg > 0 and arg[1] == "--raw" then
+ readline.raw = true
+ table.remove(arg,1)
+end
+
if #arg > 0 then
rep("(load-file \""..arg[1].."\")")
os.exit(0)
diff --git a/lua/step9_try.lua b/lua/step9_try.lua
index f3d8cce..315d698 100755
--- a/lua/step9_try.lua
+++ b/lua/step9_try.lua
@@ -174,23 +174,30 @@ 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))))))))")
+function print_exception(exc)
+ if exc then
+ if types._malexception_Q(exc) then
+ exc = printer._pr_str(exc.val, true)
+ end
+ print("Error: " .. exc)
+ print(debug.traceback())
+ end
+end
+
+if #arg > 0 and arg[1] == "--raw" then
+ readline.raw = true
+ table.remove(arg,1)
+end
+
if #arg > 0 then
- rep("(load-file \""..arg[1].."\")")
+ xpcall(function() rep("(load-file \""..arg[1].."\")") end,
+ print_exception)
os.exit(0)
end
while true do
line = readline.readline("user> ")
if not line then break end
- xpcall(function()
- print(rep(line))
- end, function(exc)
- if exc then
- if types._malexception_Q(exc) then
- exc = printer._pr_str(exc.val, true)
- end
- print("Error: " .. exc)
- print(debug.traceback())
- end
- end)
+ xpcall(function() print(rep(line)) end,
+ print_exception)
end
diff --git a/lua/stepA_mal.lua b/lua/stepA_mal.lua
index 430fffc..db31789 100755
--- a/lua/stepA_mal.lua
+++ b/lua/stepA_mal.lua
@@ -185,6 +185,11 @@ function print_exception(exc)
end
end
+if #arg > 0 and arg[1] == "--raw" then
+ readline.raw = true
+ table.remove(arg,1)
+end
+
if #arg > 0 then
xpcall(function() rep("(load-file \""..arg[1].."\")") end,
print_exception)