2014-04-17 07:42:17 +04:00
|
|
|
import sys, copy, types as pytypes
|
2015-01-07 05:58:33 +03:00
|
|
|
|
|
|
|
# python 3.0 differences
|
|
|
|
if sys.hexversion > 0x3000000:
|
2015-12-17 18:20:16 +03:00
|
|
|
_u = lambda x: x
|
|
|
|
_s2u = lambda x: x
|
2015-01-07 05:58:33 +03:00
|
|
|
else:
|
|
|
|
import codecs
|
2015-12-17 18:20:16 +03:00
|
|
|
_u = lambda x: codecs.unicode_escape_decode(x)[0]
|
|
|
|
_s2u = lambda x: unicode(x)
|
2015-01-07 05:58:33 +03:00
|
|
|
|
2014-04-17 07:42:17 +04:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
str_types = [str]
|
|
|
|
else:
|
|
|
|
str_types = [str, unicode]
|
|
|
|
|
2014-03-25 01:32:24 +04:00
|
|
|
# General functions
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
def _equal_Q(a, b):
|
2014-03-25 01:32:24 +04:00
|
|
|
ota, otb = type(a), type(b)
|
2016-01-27 21:02:14 +03:00
|
|
|
if _string_Q(a) and _string_Q(b):
|
|
|
|
return a == b
|
2014-04-03 07:23:37 +04:00
|
|
|
if not (ota == otb or (_sequential_Q(a) and _sequential_Q(b))):
|
2014-03-25 01:32:24 +04:00
|
|
|
return False;
|
2014-04-03 07:23:37 +04:00
|
|
|
if _symbol_Q(a):
|
2014-03-25 01:32:24 +04:00
|
|
|
return a == b
|
2014-04-03 07:23:37 +04:00
|
|
|
elif _list_Q(a) or _vector_Q(a):
|
2014-03-25 01:32:24 +04:00
|
|
|
if len(a) != len(b): return False
|
|
|
|
for i in range(len(a)):
|
2014-04-03 07:23:37 +04:00
|
|
|
if not _equal_Q(a[i], b[i]): return False
|
2014-03-25 01:32:24 +04:00
|
|
|
return True
|
2014-04-03 07:23:37 +04:00
|
|
|
elif _hash_map_Q(a):
|
2014-03-25 01:32:24 +04:00
|
|
|
akeys = a.keys()
|
|
|
|
bkeys = b.keys()
|
|
|
|
if len(akeys) != len(bkeys): return False
|
2017-09-12 22:15:21 +03:00
|
|
|
akeys.sort()
|
|
|
|
bkeys.sort()
|
2014-03-25 01:32:24 +04:00
|
|
|
for i in range(len(akeys)):
|
|
|
|
if akeys[i] != bkeys[i]: return False
|
2015-11-10 20:00:56 +03:00
|
|
|
if not _equal_Q(a[akeys[i]], b[bkeys[i]]): return False
|
2014-03-25 01:32:24 +04:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return a == b
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
def _sequential_Q(seq): return _list_Q(seq) or _vector_Q(seq)
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-17 07:42:17 +04:00
|
|
|
def _clone(obj):
|
|
|
|
#if type(obj) == type(lambda x:x):
|
|
|
|
if type(obj) == pytypes.FunctionType:
|
|
|
|
if obj.__code__:
|
|
|
|
return pytypes.FunctionType(
|
|
|
|
obj.__code__, obj.__globals__, name = obj.__name__,
|
|
|
|
argdefs = obj.__defaults__, closure = obj.__closure__)
|
|
|
|
else:
|
|
|
|
return pytypes.FunctionType(
|
|
|
|
obj.func_code, obj.func_globals, name = obj.func_name,
|
|
|
|
argdefs = obj.func_defaults, closure = obj.func_closure)
|
|
|
|
else:
|
|
|
|
return copy.copy(obj)
|
|
|
|
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Scalars
|
|
|
|
def _nil_Q(exp): return exp is None
|
|
|
|
def _true_Q(exp): return exp is True
|
|
|
|
def _false_Q(exp): return exp is False
|
2016-02-15 18:30:16 +03:00
|
|
|
def _string_Q(exp):
|
|
|
|
if type(exp) in str_types:
|
|
|
|
return len(exp) == 0 or exp[0] != _u("\u029e")
|
|
|
|
else:
|
|
|
|
return False
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Symbols
|
2014-03-25 01:32:24 +04:00
|
|
|
class Symbol(str): pass
|
2014-04-03 07:23:37 +04:00
|
|
|
def _symbol(str): return Symbol(str)
|
|
|
|
def _symbol_Q(exp): return type(exp) == Symbol
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-12-19 05:33:49 +03:00
|
|
|
# Keywords
|
|
|
|
# A specially prefixed string
|
|
|
|
def _keyword(str):
|
2015-12-17 18:20:16 +03:00
|
|
|
if str[0] == _u("\u029e"): return str
|
2016-02-15 18:30:16 +03:00
|
|
|
else: return _u("\u029e") + str
|
2015-01-07 05:58:33 +03:00
|
|
|
def _keyword_Q(exp):
|
2016-02-15 18:30:16 +03:00
|
|
|
if type(exp) in str_types:
|
|
|
|
return len(exp) != 0 and exp[0] == _u("\u029e")
|
|
|
|
else:
|
|
|
|
return False
|
2014-12-19 05:33:49 +03:00
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Functions
|
2014-04-10 08:58:27 +04:00
|
|
|
def _function(Eval, Env, ast, env, params):
|
2014-04-17 07:42:17 +04:00
|
|
|
def fn(*args):
|
2016-01-29 00:06:09 +03:00
|
|
|
return Eval(ast, Env(env, params, List(args)))
|
2014-04-17 07:42:17 +04:00
|
|
|
fn.__meta__ = None
|
|
|
|
fn.__ast__ = ast
|
|
|
|
fn.__gen_env__ = lambda args: Env(env, params, args)
|
|
|
|
return fn
|
2014-04-03 07:23:37 +04:00
|
|
|
def _function_Q(f): return type(f) == type(function_Q)
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
# lists
|
|
|
|
class List(list):
|
|
|
|
def __add__(self, rhs): return List(list.__add__(self, rhs))
|
|
|
|
def __getitem__(self, i):
|
|
|
|
if type(i) == slice: return List(list.__getitem__(self, i))
|
|
|
|
elif i >= len(self): return None
|
|
|
|
else: return list.__getitem__(self, i)
|
|
|
|
def __getslice__(self, *a): return List(list.__getslice__(self, *a))
|
2014-04-03 07:23:37 +04:00
|
|
|
def _list(*vals): return List(vals)
|
|
|
|
def _list_Q(exp): return type(exp) == List
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
|
|
|
|
# vectors
|
|
|
|
class Vector(list):
|
|
|
|
def __add__(self, rhs): return Vector(list.__add__(self, rhs))
|
|
|
|
def __getitem__(self, i):
|
|
|
|
if type(i) == slice: return Vector(list.__getitem__(self, i))
|
|
|
|
elif i >= len(self): return None
|
|
|
|
else: return list.__getitem__(self, i)
|
|
|
|
def __getslice__(self, *a): return Vector(list.__getslice__(self, *a))
|
2014-04-03 07:23:37 +04:00
|
|
|
def _vector(*vals): return Vector(vals)
|
|
|
|
def _vector_Q(exp): return type(exp) == Vector
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Hash maps
|
|
|
|
class Hash_Map(dict): pass
|
|
|
|
def _hash_map(*key_vals):
|
|
|
|
hm = Hash_Map()
|
|
|
|
for i in range(0,len(key_vals),2): hm[key_vals[i]] = key_vals[i+1]
|
|
|
|
return hm
|
|
|
|
def _hash_map_Q(exp): return type(exp) == Hash_Map
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
# atoms
|
|
|
|
class Atom(object):
|
|
|
|
def __init__(self, val):
|
|
|
|
self.val = val
|
2014-04-03 07:23:37 +04:00
|
|
|
def _atom(val): return Atom(val)
|
|
|
|
def _atom_Q(exp): return type(exp) == Atom
|
2016-05-09 08:39:45 +03:00
|
|
|
|
|
|
|
def py_to_mal(obj):
|
|
|
|
if type(obj) == list: return List(obj)
|
|
|
|
if type(obj) == tuple: return List(obj)
|
|
|
|
elif type(obj) == dict: return Hash_Map(obj)
|
|
|
|
else: return obj
|