-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
124 lines (89 loc) · 2.19 KB
/
environment.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from spexception import *
def primitive_add(args):
result = args[0]
for a in args[1:]:
result += a
return result
def primitive_substract(args):
result = args[0]
for a in args[1:]:
result -= a
return result
def primitive_multiply(args):
result = args[0]
for a in args[1:]:
result *= a
return result
def primitive_divide(args):
result = args[0]
for a in args[1:]:
result /= a
return result
def primitive_mod(args):
assert len(args) == 2
n1, n2 = args
return n1 % n2
def primitive_equal(args):
a0 = args[0]
for a1 in args[1:]:
if not a0 == a1:
return False
a0 = a1
return True
def primitive_less_than(args):
a0 = args[0]
for a1 in args[1:]:
if not a0 <= a1:
return False
a0 = a1
return True
def primitive_cons(args):
exp, s_exp = args
return [exp] + s_exp
def primitive_car(args):
s_exp = args[0]
return s_exp[0]
def primitive_cdr(args):
s_exp = args[0]
return s_exp[1:]
def primitive_list(args):
return list(args)
def primitive_begin(args):
return args[-1]
def primitive_exit(args):
exit(0)
built_ins = {
'+': primitive_add,
'-': primitive_substract,
'*': primitive_multiply,
'/': primitive_divide,
'mod': primitive_mod,
'=': primitive_equal,
'eq?': primitive_equal,
'<=': primitive_less_than,
'cons': primitive_cons,
'car': primitive_car,
'cdr': primitive_cdr,
'list': primitive_list,
'begin': primitive_begin,
'exit': primitive_exit
}
class Environment:
def __init__(self, dict, parent=None):
self.env = dict
self.parent = parent
def lookup(self, symbol):
return self.find_env(symbol)[symbol]
def find_env(self, symbol):
if symbol in self.env:
return self.env
else:
if self.parent is None:
raise PieUnresolvedSymbolError(symbol)
return self.parent.find_env(symbol)
def set(self, name, value):
self.env[name] = value
def nested(self):
return Environment({}, self)
def __str__(self):
return str(self.env)