-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.py
More file actions
37 lines (34 loc) · 1.24 KB
/
printer.py
File metadata and controls
37 lines (34 loc) · 1.24 KB
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
35
36
37
import mal_types
def pr_str(mal_obj, print_readably = True):
if isinstance(mal_obj, int):
return str(mal_obj)
elif callable(mal_obj) or isinstance(mal_obj, mal_types.Function):
return "#<function>"
elif isinstance(mal_obj, mal_types.Symbol):
return mal_obj.name
elif isinstance(mal_obj, str):
# keyword special
if mal_obj[:5] == "0x29E":
return mal_obj[5:]
# else
if print_readably:
string_value = mal_obj.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
return '"' + string_value + '"'
else:
return mal_obj
elif isinstance(mal_obj, mal_types.Array):
if mal_obj.type == "list":
return "(" + " ".join([pr_str(x) for x in mal_obj]) + ")"
elif mal_obj.type == "vector":
return "[" + " ".join([pr_str(x) for x in mal_obj]) + "]"
elif mal_obj.type == "hash-map":
return "{" + " ".join([pr_str(x) for x in mal_obj]) + "}"
elif mal_obj is None:
return "nil"
elif isinstance(mal_obj, mal_types.bool):
if mal_obj.value:
return "true"
else:
return "false"
else:
return str(mal_obj)