forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.cr
34 lines (32 loc) · 1.09 KB
/
printer.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require "./types"
def pr_str(value, print_readably = true)
case value
when Nil then "nil"
when Bool then value.to_s
when Int64 then value.to_s
when Mal::List then "(#{value.map{|v| pr_str(v, print_readably) as String}.join(" ")})"
when Mal::Vector then "[#{value.map{|v| pr_str(v, print_readably) as String}.join(" ")}]"
when Mal::Symbol then value.str.to_s
when Mal::Func then "<function>"
when Mal::Closure then "<closure>"
when Mal::HashMap
# step1_read_print.cr requires specifying type
"{#{value.map{|k, v| "#{pr_str(k, print_readably)} #{pr_str(v, print_readably)}" as String}.join(" ")}}"
when String
case
when value.empty?()
print_readably ? value.inspect : value
when value[0] == '\u029e'
":#{value[1..-1]}"
else
print_readably ? value.inspect : value
end
when Mal::Atom
"(atom #{pr_str(value.val, print_readably)})"
else
raise "invalid MalType: #{value.to_s}"
end
end
def pr_str(t : Mal::Type, print_readably = true)
pr_str(t.unwrap, print_readably) + (t.macro? ? " (macro)" : "")
end