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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# ## Actinide Types
from collections import namedtuple
from decimal import Decimal, InvalidOperation
# Circular import. Hard to avoid: Procedure calls `eval`, `eval` calls
# `lambda_`, `lambda_` eventually calls `Procedure`. We indirect the call
# through the module object to avoid problems with import order.
from . import evaluator as e
from .environment import *
from .builtin import make_registry
ACTINIDE_BINDINGS, ACTINIDE_VOIDS, ACTINIDE_FNS, ACTINIDE_BUILTINS, bind, void, fn, builtin = make_registry()
# ### Nil
#
# Nil is a type with a single value, usually taken to denote no value.
nil = bind('nil', None)
@fn
def nil_p(value):
return value is None
@fn
def read_nil(value):
return nil
@fn
def display_nil(value):
return '()'
# ### Booleans
#
# The true and false values.
true = bind('#t', True)
false = bind('#f', False)
@fn
def boolean_p(value):
return value is true or value is false
@fn
def read_boolean(value):
if value == '#t':
return true
if value == '#f':
return false
return None
@fn
def display_boolean(value):
return '#t' if value else '#f'
# ### Integers
#
# These are fixed-precision numbers with no decimal part, obeying common notions
# of machine integer arithmetic. They support large values.
integer = bind('integer', int)
@fn
def integer_p(value):
return isinstance(value, integer)
@fn
def read_integer(value):
try:
return integer(value)
except ValueError:
return nil
@fn
def display_integer(value):
return str(value)
# ### Decimals
#
# These are variable-precision numbers, which may have a decimal part.
decimal = bind('decimal', Decimal)
@fn
def decimal_p(value):
return isinstance(value, decimal)
@fn
def read_decimal(value):
try:
return decimal(value)
except InvalidOperation:
return nil
@fn
def display_decimal(value):
return str(value)
# ### Strings
#
# Sequences of characters.
string = bind('string', str)
@fn
def string_p(value):
return not symbol_p(value) and isinstance(value, string)
@fn
def read_string(value):
value = value[1:-1]
value = value.replace('\\"', '"')
value = value.replace('\\\\', '\\')
return value
@fn
def display_string(value):
value = value.replace('\\', '\\\\')
value = value.replace('"', '\\"')
return f'"{value}"'
# ### Symbols
#
# Short, interned strings used as identifiers. Interning is handled by a
# SymbolTable.
class Symbol(object):
def __init__(self, value):
self.value = value
def __hash__(self):
return hash(self.value)
def __str__(self):
return f"{self.value}"
def __repr__(self):
return f'Symbol({repr(self.value)})'
# bind manually, fix the symbol table
def symbol(string, symbol_table):
return symbol_table[string]
@fn
def symbol_p(value):
return isinstance(value, Symbol)
@fn
def read_symbol(value, symbol_table):
return symbol(value, symbol_table)
@fn
def display_symbol(value):
return str(value)
# ### Conses
#
# Pairs.
Cons = namedtuple('Cons', 'head tail')
@fn
def cons(head, tail):
return Cons(head, tail)
@fn
def cons_p(value):
return isinstance(value, Cons)
@fn
def head(cons):
return cons.head
@fn
def tail(cons):
return cons.tail
@fn
def display_cons(value):
parts = []
while cons_p(value):
parts.append(display(head(value)))
value = tail(value)
if not nil_p(value):
parts.append('.')
parts.append(display(value))
return '(' + ' '.join(parts) + ')'
# ### Lists
@fn
def list(*elems):
if elems:
head, *tail = elems
return cons(head, list(*tail))
else:
return nil
@fn
def list_p(value):
return nil_p(value) or cons_p(value) and list_p(tail(value))
def flatten(list):
r = []
while not nil_p(list):
r.append(head(list))
list = tail(list)
return r
# ### Procedures
class Procedure(object):
def __init__(self, body, formals, environment, symbols):
self.body = body
self.continuation = e.eval(body, symbols, None)
self.formals = formals
self.environment = environment
def __call__(self, *args):
call_env = self.invocation_environment(*args)
return e.run(self.continuation, call_env, ())
def invocation_environment(self, *args):
return Environment(zip(self.formals, args), self.environment)
@fn
def procedure_p(value):
return callable(value)
@fn
def display_procedure(proc):
if isinstance(proc, Procedure):
formals = ' '.join(display(formal) for formal in proc.formals)
body = display(proc.body)
return f'<procedure: (lambda ({formals}) {body})>'
return f'<builtin: {proc.__name__}>'
# ### General-purpose functions
@fn
def display(value):
if cons_p(value):
return display_cons(value)
if symbol_p(value):
return display_symbol(value)
if string_p(value):
return display_string(value)
if nil_p(value):
return display_nil(value)
if boolean_p(value):
return display_boolean(value)
if integer_p(value):
return display_integer(value)
if decimal_p(value):
return display_decimal(value)
if procedure_p(value):
return display_procedure(value)
# Give up and use repr to avoid printing `None`.
return repr(value)
|