-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.py
65 lines (55 loc) · 1.76 KB
/
env.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import division
import math, operator
try: from functools import reduce
except ImportError:
pass
from primitives import islist, isbool, isatom, isnumber
class Env(dict):
def __init__(self, outer=None):
"""
Make an empty environment with the outer environment specified
"""
self['outer'] = outer
def find(self, sym):
"""
Find a symbol in env
If symbol not in env or any of its outer, return None
"""
try:
return self[sym]
except KeyError:
try: return self['outer'].find(sym)
except AttributeError:
# once hit here, sym is nowhere to be found
raise NameError('Undefined atom %s' % sym)
def makeglobalenv():
env = Env()
def primitivequit():
raise KeyboardInterrupt
env.update(vars(math))
env.update({
'quit': primitivequit,
'+': lambda *args: sum(args),
'*': lambda *args: reduce(lambda x, y: x * y, args, 1),
'-': lambda *args: -args[0] if len(args) == 0 else args[0] - sum(args[1:]),
'/': lambda *args: reduce(lambda x, y: x / y, args[1:], args[0]),
'<': operator.lt,
'>': operator.gt,
'=': operator.eq,
'eq?': operator.is_,
'zero?': lambda x: x == 0,
'null?': lambda x: x == [],
'atom?': isatom,
'number?': isnumber,
'list?': islist,
'not': operator.not_,
'cons': lambda x, l: [x] + l,
'car': lambda l: l[0],
'cdr': lambda l: l[1:],
'list': lambda *args: list(args),
'map': lambda *args: list(map(*args)),
'reduce': reduce,
'filter': lambda *args: list(filter(*args)),
'else': True
})
return env