fdf28b76 |
1 | require "./types" |
2 | |
448769e3 |
3 | def pr_str(value, print_readably = true) |
fdf28b76 |
4 | case value |
436f169c |
5 | when Nil then "nil" |
6 | when Bool then value.to_s |
7 | when Int32 then value.to_s |
8 | when Mal::List then "(#{value.map{|v| pr_str(v, print_readably) as String}.join(" ")})" |
9 | when Mal::Vector then "[#{value.map{|v| pr_str(v, print_readably) as String}.join(" ")}]" |
10 | when Mal::Symbol then value.str.to_s |
11 | when Mal::Func then "<function>" |
12 | when Mal::Closure then "<closure>" |
afc3a8d5 |
13 | when Mal::HashMap |
436f169c |
14 | "{#{value.map{|k, v| "#{pr_str(k, print_readably)} #{pr_str(v, print_readably)}"}.join(" ")}}" |
afc3a8d5 |
15 | when String |
16 | case |
17 | when value.empty?() |
18 | print_readably ? value.inspect : value |
19 | when value[0] == '\u029e' |
20 | ":#{value[1..-1]}" |
21 | else |
22 | print_readably ? value.inspect : value |
23 | end |
6e7390b7 |
24 | when Mal::Atom |
25 | "(atom #{pr_str(value.val, print_readably)})" |
afc3a8d5 |
26 | else |
27 | raise "invalid MalType: #{value.to_s}" |
fdf28b76 |
28 | end |
29 | end |
7fe6282e |
30 | |
31 | def pr_str(t : Mal::Type, print_readably = true) |
9bbb8ccc |
32 | pr_str(t.unwrap, print_readably) + (t.macro? ? " (macro)" : "") |
7fe6282e |
33 | end |