diff options
| author | Joel Martin <github@martintribe.org> | 2015-01-08 23:25:40 -0600 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-01-08 23:25:40 -0600 |
| commit | 9d42904e47c50c5ff2306da04993b2a32bc9cd16 (patch) | |
| tree | e1b2d46a232e6573dc2c185967ebe988be3db973 /lua/printer.lua | |
| parent | fd888612ca589d7e1a46c36fc3fe12aed126f6a8 (diff) | |
| download | mal-9d42904e47c50c5ff2306da04993b2a32bc9cd16.tar.gz mal-9d42904e47c50c5ff2306da04993b2a32bc9cd16.zip | |
Lua: all steps and self-hosting.
Also some misc docs/TODO updates.
Diffstat (limited to 'lua/printer.lua')
| -rw-r--r-- | lua/printer.lua | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lua/printer.lua b/lua/printer.lua new file mode 100644 index 0000000..8c1cdad --- /dev/null +++ b/lua/printer.lua @@ -0,0 +1,55 @@ +local string = require('string') +local table = require('table') +local types = require('types') +local utils = require('utils') + +local M = {} + +function M._pr_str(obj, print_readably) + local _r = print_readably + if utils.instanceOf(obj, types.Symbol) then + return obj.val + elseif types._list_Q(obj) then + return "(" .. table.concat(utils.map(function(e) + return M._pr_str(e,_r) end, obj), " ") .. ")" + elseif types._vector_Q(obj) then + return "[" .. table.concat(utils.map(function(e) + return M._pr_str(e,_r) end, obj), " ") .. "]" + elseif types._hash_map_Q(obj) then + local res = {} + for k,v in pairs(obj) do + res[#res+1] = M._pr_str(k, _r) + res[#res+1] = M._pr_str(v, _r) + end + return "{".. table.concat(res, " ").."}" + elseif type(obj) == 'string' then + if string.sub(obj,1,1) == "\177" then + return ':' .. string.sub(obj,2) + else + if _r then + local sval = obj:gsub('\\', '\\\\') + sval = sval:gsub('"', '\\"') + sval = sval:gsub('\n', '\\n') + return '"' .. sval .. '"' + else + return obj + end + end + elseif obj == types.Nil then + return "nil" + elseif obj == true then + return "true" + elseif obj == false then + return "false" + elseif types._malfunc_Q(obj) then + return "(fn* "..M._pr_str(obj.params).." "..M._pr_str(obj.ast)..")" + elseif types._atom_Q(obj) then + return "(atom "..M._pr_str(obj.val)..")" + elseif type(obj) == 'function' then + return "#<function>" + else + return string.format("%s", obj) + end +end + +return M |
