aboutsummaryrefslogtreecommitdiff
path: root/nim/printer.nim
diff options
context:
space:
mode:
Diffstat (limited to 'nim/printer.nim')
-rw-r--r--nim/printer.nim15
1 files changed, 9 insertions, 6 deletions
diff --git a/nim/printer.nim b/nim/printer.nim
index eb26671..b6bf4c7 100644
--- a/nim/printer.nim
+++ b/nim/printer.nim
@@ -1,5 +1,11 @@
import strutils, sequtils, tables, types
+proc str_handle(x: string, pr = true): string =
+ if x.len > 0 and x[0] == '\xff':
+ result = ":" & x[1 .. x.high]
+ elif pr: result = "\"" & x.replace("\"", "\\\"") & "\""
+ else: result = x
+
proc pr_str*(m: MalType, pr = true): string =
case m.kind
of Nil: result = "nil"
@@ -7,12 +13,9 @@ proc pr_str*(m: MalType, pr = true): string =
of False: result = "false"
of Fun: result = "#<function>"
of MalFun: result = "#<malfun>"
+ of Atom: result = "(atom " & m.val[].pr_str & ")"
of Symbol: result = m.str
- of String:
- if m.str.len > 0 and m.str[0] == '\xff':
- result = ":" & m.str[1 .. m.str.high]
- elif pr: result = "\"" & m.str.replace("\"", "\\\"") & "\""
- else: result = m.str
+ of String: result = m.str.str_handle(pr)
of Number: result = $m.number
of List: result = "(" & m.list.mapIt(string, it.pr_str(pr)).join(" ") & ")"
of Vector: result = "[" & m.list.mapIt(string, it.pr_str(pr)).join(" ") & "]"
@@ -20,5 +23,5 @@ proc pr_str*(m: MalType, pr = true): string =
result = "{"
for key, val in m.hash_map.pairs:
if result.len > 1: result.add " "
- result.add key & " " & val.pr_str(pr)
+ result.add key.str_handle & " " & val.pr_str(pr)
result.add "}"