plpgsql: IO using stream table. Add keywords.
[jackhill/mal.git] / python / printer.py
1 import mal_types as types
2
3 def _escape(s):
4 return s.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
5
6 def _pr_str(obj, print_readably=True):
7 _r = print_readably
8 if types._list_Q(obj):
9 return "(" + " ".join(map(lambda e: _pr_str(e,_r), obj)) + ")"
10 elif types._vector_Q(obj):
11 return "[" + " ".join(map(lambda e: _pr_str(e,_r), obj)) + "]"
12 elif types._hash_map_Q(obj):
13 ret = []
14 for k in obj.keys():
15 ret.extend((_pr_str(k), _pr_str(obj[k],_r)))
16 return "{" + " ".join(ret) + "}"
17 elif type(obj) in types.str_types:
18 if len(obj) > 0 and obj[0] == types._u('\u029e'):
19 return ':' + obj[1:]
20 elif print_readably:
21 return '"' + _escape(obj) + '"'
22 else:
23 return obj
24 elif types._nil_Q(obj):
25 return "nil"
26 elif types._true_Q(obj):
27 return "true"
28 elif types._false_Q(obj):
29 return "false"
30 elif types._atom_Q(obj):
31 return "(atom " + _pr_str(obj.val,_r) + ")"
32 else:
33 return obj.__str__()
34