2014-04-03 07:23:37 +04:00
|
|
|
import mal_types as types
|
|
|
|
|
2015-10-31 06:05:49 +03:00
|
|
|
def _escape(s):
|
|
|
|
return s.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
def _pr_str(obj, print_readably=True):
|
|
|
|
_r = print_readably
|
|
|
|
if types._list_Q(obj):
|
|
|
|
return "(" + " ".join(map(lambda e: _pr_str(e,_r), obj)) + ")"
|
|
|
|
elif types._vector_Q(obj):
|
|
|
|
return "[" + " ".join(map(lambda e: _pr_str(e,_r), obj)) + "]"
|
|
|
|
elif types._hash_map_Q(obj):
|
|
|
|
ret = []
|
|
|
|
for k in obj.keys():
|
|
|
|
ret.extend((_pr_str(k), _pr_str(obj[k],_r)))
|
|
|
|
return "{" + " ".join(ret) + "}"
|
2016-02-15 18:30:16 +03:00
|
|
|
elif type(obj) in types.str_types:
|
2015-12-17 18:20:16 +03:00
|
|
|
if len(obj) > 0 and obj[0] == types._u('\u029e'):
|
2014-12-19 05:33:49 +03:00
|
|
|
return ':' + obj[1:]
|
|
|
|
elif print_readably:
|
2015-10-31 06:05:49 +03:00
|
|
|
return '"' + _escape(obj) + '"'
|
2014-04-03 07:23:37 +04:00
|
|
|
else:
|
|
|
|
return obj
|
|
|
|
elif types._nil_Q(obj):
|
|
|
|
return "nil"
|
|
|
|
elif types._true_Q(obj):
|
|
|
|
return "true"
|
|
|
|
elif types._false_Q(obj):
|
|
|
|
return "false"
|
|
|
|
elif types._atom_Q(obj):
|
|
|
|
return "(atom " + _pr_str(obj.val,_r) + ")"
|
|
|
|
else:
|
|
|
|
return obj.__str__()
|
|
|
|
|