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 |
725ee0bd |
14 | # step1_read_print.cr requires specifying type |
15 | "{#{value.map{|k, v| "#{pr_str(k, print_readably)} #{pr_str(v, print_readably)}" as String}.join(" ")}}" |
afc3a8d5 |
16 | when String |
17 | case |
18 | when value.empty?() |
19 | print_readably ? value.inspect : value |
20 | when value[0] == '\u029e' |
21 | ":#{value[1..-1]}" |
22 | else |
23 | print_readably ? value.inspect : value |
24 | end |
6e7390b7 |
25 | when Mal::Atom |
26 | "(atom #{pr_str(value.val, print_readably)})" |
afc3a8d5 |
27 | else |
28 | raise "invalid MalType: #{value.to_s}" |
fdf28b76 |
29 | end |
30 | end |
7fe6282e |
31 | |
32 | def pr_str(t : Mal::Type, print_readably = true) |
9bbb8ccc |
33 | pr_str(t.unwrap, print_readably) + (t.macro? ? " (macro)" : "") |
7fe6282e |
34 | end |