From 90f618cbe7ac7740accf501a75be6972bd95be1a Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Sat, 28 Feb 2015 11:09:54 -0600 Subject: All: rename stepA_interop to stepA_mal Also, add missed postscript interop tests. --- lua/Makefile | 2 +- lua/stepA_interop.lua | 200 -------------------------------------------------- lua/stepA_mal.lua | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 201 deletions(-) delete mode 100755 lua/stepA_interop.lua create mode 100755 lua/stepA_mal.lua (limited to 'lua') diff --git a/lua/Makefile b/lua/Makefile index d1498bd..169c587 100644 --- a/lua/Makefile +++ b/lua/Makefile @@ -1,7 +1,7 @@ TESTS = SOURCES_BASE = utils.lua types.lua reader.lua printer.lua -SOURCES_LISP = env.lua core.lua stepA_interop.lua +SOURCES_LISP = env.lua core.lua stepA_mal.lua SOURCES = $(SOURCES_BASE) $(SOURCES_LISP) all: libs diff --git a/lua/stepA_interop.lua b/lua/stepA_interop.lua deleted file mode 100755 index 430fffc..0000000 --- a/lua/stepA_interop.lua +++ /dev/null @@ -1,200 +0,0 @@ -#!/usr/bin/env lua - -local table = require('table') - -local readline = require('readline') -local utils = require('utils') -local types = require('types') -local reader = require('reader') -local printer = require('printer') -local Env = require('env') -local core = require('core') -local List, Vector, HashMap = types.List, types.Vector, types.HashMap - --- read -function READ(str) - return reader.read_str(str) -end - --- eval -function is_pair(x) - return types._sequential_Q(x) and #x > 0 -end - -function quasiquote(ast) - if not is_pair(ast) then - return types.List:new({types.Symbol:new("quote"), ast}) - elseif types._symbol_Q(ast[1]) and ast[1].val == 'unquote' then - return ast[2] - elseif is_pair(ast[1]) and - types._symbol_Q(ast[1][1]) and - ast[1][1].val == 'splice-unquote' then - return types.List:new({types.Symbol:new("concat"), - ast[1][2], - quasiquote(ast:slice(2))}) - else - return types.List:new({types.Symbol:new("cons"), - quasiquote(ast[1]), - quasiquote(ast:slice(2))}) - end -end - -function is_macro_call(ast, env) - if types._list_Q(ast) and - types._symbol_Q(ast[1]) and - env:find(ast[1]) then - local f = env:get(ast[1]) - return types._malfunc_Q(f) and f.ismacro - end -end - -function macroexpand(ast, env) - while is_macro_call(ast, env) do - local mac = env:get(ast[1]) - ast = mac.fn(unpack(ast:slice(2))) - end - return ast -end - -function eval_ast(ast, env) - if types._symbol_Q(ast) then - return env:get(ast) - elseif types._list_Q(ast) then - return List:new(utils.map(function(x) return EVAL(x,env) end,ast)) - elseif types._vector_Q(ast) then - return Vector:new(utils.map(function(x) return EVAL(x,env) end,ast)) - elseif types._hash_map_Q(ast) then - local new_hm = {} - for k,v in pairs(ast) do - new_hm[EVAL(k, env)] = EVAL(v, env) - end - return HashMap:new(new_hm) - else - return ast - end -end - -function EVAL(ast, env) - while true do - --print("EVAL: "..printer._pr_str(ast,true)) - if not types._list_Q(ast) then return eval_ast(ast, env) end - - -- apply list - ast = macroexpand(ast, env) - if not types._list_Q(ast) then return ast end - - local a0,a1,a2,a3 = ast[1], ast[2],ast[3],ast[4] - local a0sym = types._symbol_Q(a0) and a0.val or "" - if 'def!' == a0sym then - return env:set(a1, EVAL(a2, env)) - elseif 'let*' == a0sym then - local let_env = Env:new(env) - for i = 1,#a1,2 do - let_env:set(a1[i], EVAL(a1[i+1], let_env)) - end - env = let_env - ast = a2 -- TCO - elseif 'quote' == a0sym then - return a1 - elseif 'quasiquote' == a0sym then - ast = quasiquote(a1) -- TCO - elseif 'defmacro!' == a0sym then - local mac = EVAL(a2, env) - mac.ismacro = true - return env:set(a1, mac) - elseif 'macroexpand' == a0sym then - return macroexpand(a1, env) - elseif 'try*' == a0sym then - local exc, result = nil, nil - xpcall(function() - result = EVAL(a1, env) - end, function(err) - exc = err - end) - if exc ~= nil then - if types._malexception_Q(exc) then - exc = exc.val - end - if a2 and a2[1].val == 'catch*' then - result = EVAL(a2[3], Env:new(env, {a2[2]}, {exc})) - else - types.throw(exc) - end - end - return result - elseif 'do' == a0sym then - local el = eval_ast(ast:slice(2,#ast-1), env) - ast = ast[#ast] -- TCO - elseif 'if' == a0sym then - local cond = EVAL(a1, env) - if cond == types.Nil or cond == false then - if a3 then ast = a3 else return types.Nil end -- TCO - else - ast = a2 -- TCO - end - elseif 'fn*' == a0sym then - return types.MalFunc:new(function(...) - return EVAL(a2, Env:new(env, a1, arg)) - end, a2, env, a1) - else - local args = eval_ast(ast, env) - local f = table.remove(args, 1) - if types._malfunc_Q(f) then - ast = f.ast - env = Env:new(f.env, f.params, args) -- TCO - else - return f(unpack(args)) - end - end - end -end - --- print -function PRINT(exp) - return printer._pr_str(exp, true) -end - --- repl -local repl_env = Env:new() -function rep(str) - return PRINT(EVAL(READ(str),repl_env)) -end - --- core.lua: defined using Lua -for k,v in pairs(core.ns) do - repl_env:set(types.Symbol:new(k), v) -end -repl_env:set(types.Symbol:new('eval'), - function(ast) return EVAL(ast, repl_env) end) -repl_env:set(types.Symbol:new('*ARGV*'), types.List:new(types.slice(arg,2))) - --- core.mal: defined using mal -rep("(def! *host-language* \"lua\")") -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))))))))") - -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 then - xpcall(function() rep("(load-file \""..arg[1].."\")") end, - print_exception) - os.exit(0) -end - -rep("(println (str \"Mal [\" *host-language* \"]\"))") -while true do - line = readline.readline("user> ") - if not line then break end - xpcall(function() print(rep(line)) end, - print_exception) -end diff --git a/lua/stepA_mal.lua b/lua/stepA_mal.lua new file mode 100755 index 0000000..430fffc --- /dev/null +++ b/lua/stepA_mal.lua @@ -0,0 +1,200 @@ +#!/usr/bin/env lua + +local table = require('table') + +local readline = require('readline') +local utils = require('utils') +local types = require('types') +local reader = require('reader') +local printer = require('printer') +local Env = require('env') +local core = require('core') +local List, Vector, HashMap = types.List, types.Vector, types.HashMap + +-- read +function READ(str) + return reader.read_str(str) +end + +-- eval +function is_pair(x) + return types._sequential_Q(x) and #x > 0 +end + +function quasiquote(ast) + if not is_pair(ast) then + return types.List:new({types.Symbol:new("quote"), ast}) + elseif types._symbol_Q(ast[1]) and ast[1].val == 'unquote' then + return ast[2] + elseif is_pair(ast[1]) and + types._symbol_Q(ast[1][1]) and + ast[1][1].val == 'splice-unquote' then + return types.List:new({types.Symbol:new("concat"), + ast[1][2], + quasiquote(ast:slice(2))}) + else + return types.List:new({types.Symbol:new("cons"), + quasiquote(ast[1]), + quasiquote(ast:slice(2))}) + end +end + +function is_macro_call(ast, env) + if types._list_Q(ast) and + types._symbol_Q(ast[1]) and + env:find(ast[1]) then + local f = env:get(ast[1]) + return types._malfunc_Q(f) and f.ismacro + end +end + +function macroexpand(ast, env) + while is_macro_call(ast, env) do + local mac = env:get(ast[1]) + ast = mac.fn(unpack(ast:slice(2))) + end + return ast +end + +function eval_ast(ast, env) + if types._symbol_Q(ast) then + return env:get(ast) + elseif types._list_Q(ast) then + return List:new(utils.map(function(x) return EVAL(x,env) end,ast)) + elseif types._vector_Q(ast) then + return Vector:new(utils.map(function(x) return EVAL(x,env) end,ast)) + elseif types._hash_map_Q(ast) then + local new_hm = {} + for k,v in pairs(ast) do + new_hm[EVAL(k, env)] = EVAL(v, env) + end + return HashMap:new(new_hm) + else + return ast + end +end + +function EVAL(ast, env) + while true do + --print("EVAL: "..printer._pr_str(ast,true)) + if not types._list_Q(ast) then return eval_ast(ast, env) end + + -- apply list + ast = macroexpand(ast, env) + if not types._list_Q(ast) then return ast end + + local a0,a1,a2,a3 = ast[1], ast[2],ast[3],ast[4] + local a0sym = types._symbol_Q(a0) and a0.val or "" + if 'def!' == a0sym then + return env:set(a1, EVAL(a2, env)) + elseif 'let*' == a0sym then + local let_env = Env:new(env) + for i = 1,#a1,2 do + let_env:set(a1[i], EVAL(a1[i+1], let_env)) + end + env = let_env + ast = a2 -- TCO + elseif 'quote' == a0sym then + return a1 + elseif 'quasiquote' == a0sym then + ast = quasiquote(a1) -- TCO + elseif 'defmacro!' == a0sym then + local mac = EVAL(a2, env) + mac.ismacro = true + return env:set(a1, mac) + elseif 'macroexpand' == a0sym then + return macroexpand(a1, env) + elseif 'try*' == a0sym then + local exc, result = nil, nil + xpcall(function() + result = EVAL(a1, env) + end, function(err) + exc = err + end) + if exc ~= nil then + if types._malexception_Q(exc) then + exc = exc.val + end + if a2 and a2[1].val == 'catch*' then + result = EVAL(a2[3], Env:new(env, {a2[2]}, {exc})) + else + types.throw(exc) + end + end + return result + elseif 'do' == a0sym then + local el = eval_ast(ast:slice(2,#ast-1), env) + ast = ast[#ast] -- TCO + elseif 'if' == a0sym then + local cond = EVAL(a1, env) + if cond == types.Nil or cond == false then + if a3 then ast = a3 else return types.Nil end -- TCO + else + ast = a2 -- TCO + end + elseif 'fn*' == a0sym then + return types.MalFunc:new(function(...) + return EVAL(a2, Env:new(env, a1, arg)) + end, a2, env, a1) + else + local args = eval_ast(ast, env) + local f = table.remove(args, 1) + if types._malfunc_Q(f) then + ast = f.ast + env = Env:new(f.env, f.params, args) -- TCO + else + return f(unpack(args)) + end + end + end +end + +-- print +function PRINT(exp) + return printer._pr_str(exp, true) +end + +-- repl +local repl_env = Env:new() +function rep(str) + return PRINT(EVAL(READ(str),repl_env)) +end + +-- core.lua: defined using Lua +for k,v in pairs(core.ns) do + repl_env:set(types.Symbol:new(k), v) +end +repl_env:set(types.Symbol:new('eval'), + function(ast) return EVAL(ast, repl_env) end) +repl_env:set(types.Symbol:new('*ARGV*'), types.List:new(types.slice(arg,2))) + +-- core.mal: defined using mal +rep("(def! *host-language* \"lua\")") +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))))))))") + +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 then + xpcall(function() rep("(load-file \""..arg[1].."\")") end, + print_exception) + os.exit(0) +end + +rep("(println (str \"Mal [\" *host-language* \"]\"))") +while true do + line = readline.readline("user> ") + if not line then break end + xpcall(function() print(rep(line)) end, + print_exception) +end -- cgit v1.2.3 From 3e0b36dcee99ddfd7ea0e04a382c1ad3858fc45f Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Sat, 28 Feb 2015 15:58:35 -0600 Subject: Lua: fix with new runtest.py --- lua/core.lua | 2 ++ lua/readline.lua | 9 ++++++++- lua/step1_read_print.lua | 4 ++++ lua/step2_eval.lua | 4 ++++ lua/step3_env.lua | 4 ++++ lua/step4_if_fn_do.lua | 4 ++++ lua/step5_tco.lua | 4 ++++ lua/step6_file.lua | 5 +++++ lua/step7_quote.lua | 5 +++++ lua/step8_macros.lua | 5 +++++ lua/step9_try.lua | 31 +++++++++++++++++++------------ lua/stepA_mal.lua | 5 +++++ 12 files changed, 69 insertions(+), 13 deletions(-) (limited to 'lua') 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) -- cgit v1.2.3