Commit | Line | Data |
---|---|---|
9d42904e JM |
1 | local string = require('string') |
2 | local table = require('table') | |
3 | local types = require('types') | |
4 | local utils = require('utils') | |
5 | ||
6 | local M = {} | |
7 | ||
8 | function M._pr_str(obj, print_readably) | |
9 | local _r = print_readably | |
10 | if utils.instanceOf(obj, types.Symbol) then | |
11 | return obj.val | |
12 | elseif types._list_Q(obj) then | |
13 | return "(" .. table.concat(utils.map(function(e) | |
14 | return M._pr_str(e,_r) end, obj), " ") .. ")" | |
15 | elseif types._vector_Q(obj) then | |
16 | return "[" .. table.concat(utils.map(function(e) | |
17 | return M._pr_str(e,_r) end, obj), " ") .. "]" | |
18 | elseif types._hash_map_Q(obj) then | |
19 | local res = {} | |
20 | for k,v in pairs(obj) do | |
21 | res[#res+1] = M._pr_str(k, _r) | |
22 | res[#res+1] = M._pr_str(v, _r) | |
23 | end | |
24 | return "{".. table.concat(res, " ").."}" | |
25 | elseif type(obj) == 'string' then | |
26 | if string.sub(obj,1,1) == "\177" then | |
27 | return ':' .. string.sub(obj,2) | |
28 | else | |
29 | if _r then | |
30 | local sval = obj:gsub('\\', '\\\\') | |
31 | sval = sval:gsub('"', '\\"') | |
32 | sval = sval:gsub('\n', '\\n') | |
33 | return '"' .. sval .. '"' | |
34 | else | |
35 | return obj | |
36 | end | |
37 | end | |
38 | elseif obj == types.Nil then | |
39 | return "nil" | |
40 | elseif obj == true then | |
41 | return "true" | |
42 | elseif obj == false then | |
43 | return "false" | |
44 | elseif types._malfunc_Q(obj) then | |
45 | return "(fn* "..M._pr_str(obj.params).." "..M._pr_str(obj.ast)..")" | |
46 | elseif types._atom_Q(obj) then | |
47 | return "(atom "..M._pr_str(obj.val)..")" | |
109678d4 | 48 | elseif type(obj) == 'function' or types._functionref_Q(obj) then |
9d42904e JM |
49 | return "#<function>" |
50 | else | |
51 | return string.format("%s", obj) | |
52 | end | |
53 | end | |
54 | ||
55 | return M |