1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 10:07:45 +03:00
mal/julia/printer.jl

37 lines
943 B
Julia
Raw Normal View History

2015-03-29 01:44:31 +03:00
module printer
2015-03-30 01:55:11 +03:00
import types
2015-03-29 01:44:31 +03:00
export pr_str
function pr_str(obj, print_readably=true)
_r = print_readably
if isa(obj, Array)
"($(join([pr_str(o, _r) for o=obj], " ")))"
elseif isa(obj, Tuple)
"[$(join([pr_str(o, _r) for o=obj], " "))]"
elseif isa(obj, Dict)
2015-04-01 05:23:22 +03:00
"{$(join(["$(pr_str(o[1],_r)) $(pr_str(o[2],_r))" for o=obj], " "))}"
2015-03-29 01:44:31 +03:00
elseif isa(obj, String)
2015-04-01 05:23:22 +03:00
if length(obj) > 0 && obj[1] == '\u029e'
":$(obj[2:end])"
elseif _r
2015-03-29 01:44:31 +03:00
str = replace(replace(replace(obj,
"\\", "\\\\"),
"\"", "\\\""),
"\n", "\\n")
"\"$(str)\""
else
obj
end
elseif obj == nothing
"nil"
2015-03-30 01:55:11 +03:00
elseif typeof(obj) == types.MalFunc
"(fn* $(pr_str(obj.params,true)) $(pr_str(obj.ast,true)))"
2015-03-29 01:44:31 +03:00
else
string(obj)
end
end
end