1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-11 00:52:44 +03:00
mal/logo/printer.lg

55 lines
1.4 KiB
Plaintext
Raw Normal View History

load "../logo/types.lg
to pr_str :exp :readable
if emptyp :exp [output []]
output case obj_type :exp [
[[nil] "nil]
[[true] "true]
[[false] "false]
[[number] obj_val :exp]
[[symbol] obj_val :exp]
[[keyword] word ": obj_val :exp]
[[string] print_string :exp :readable]
[[list] pr_seq obj_val :exp :readable "\( "\) :space_char]
[[vector] pr_seq obj_val :exp :readable "\[ "\] :space_char]
[[hashmap] pr_seq obj_val :exp :readable "\{ "\} :space_char]
[[atom] (word "\(atom :space_char pr_str obj_val :exp :readable "\) ) ]
[[nativefn] (word "#<NativeFunction: obj_val :exp ">) ]
[[fn] (word "#<Function:args= pr_str fn_args :exp :readable ", :space_char "macro= fn_is_macro :exp ">) ]
[else (throw "error (sentence [unknown type] obj_type :exp))]
]
end
to escape_string :s
localmake "i 1
localmake "res "
while [:i <= count :s] [
localmake "c item :i :s
make "res word :res cond [
[[ :c = "\\ ] "\\\\ ]
[[ :c = char 10 ] "\\n ]
[[ :c = "\" ] "\\\" ]
[else :c ]
]
make "i (:i + 1)
]
output :res
end
to print_string :exp :readable
ifelse :readable [
output (word "\" escape_string obj_val :exp "\" )
] [
output obj_val :exp
]
end
to pr_seq :seq :readable :start_char :end_char :delim_char
localmake "res :start_char
foreach :seq [
if # > 1 [make "res word :res :delim_char]
make "res word :res pr_str ? :readable
]
output word :res :end_char
end