Repository navigation
Expand file tree
/
Copy pathlambda.py
More file actions
487 lines (427 loc) · 16.2 KB
/
Copy pathlambda.py
File metadata and controls
487 lines (427 loc) · 16.2 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env python3
"""
A compiler for lambda/0.
lambda/0 is a small functional language, in the style of ML. Every
construct is an expression with a value, variables never change once
bound, and functions are values like any other:
let add x y = x + y in
let add5 = add 5 in # a new function, made from add
print (add5 10) # 15
The grammar:
program = sequence .
sequence = expr { ";" expr } .
expr = "let" [ "rec" ] name { name } "=" expr "in" sequence
| "fun" name { name } "->" sequence
| "if" expr "then" expr "else" expr
| "print" expr
| comparison .
comparison = sum [ ( "<" | ">" | "=" | "<>" | "<=" | ">=" ) sum ] .
sum = product { ( "+" | "-" ) product } .
product = unary { ( "*" | "/" | "%" ) unary } .
unary = "-" unary | application .
application = atom { atom } .
atom = number | name | "(" sequence ")" .
Every function takes exactly one argument. 'fun x y -> e' is short for
'fun x -> fun y -> e', and 'f a b' means '(f a) b': applying a
function of two arguments to one gives back a function waiting for
the second. This is called currying, after Haskell Curry.
'let f x = e' is short for 'let f = fun x -> e'. 'let rec' lets a
function refer to itself. 'print e' prints e and has e as its value.
'#' starts a comment.
usage: lambda.py [-tree] program.lam
"""
import sys
class CompileError(Exception):
def __init__(self, message, token):
super().__init__(message)
self.line = token.line
self.col = token.col
# Scanning.
KEYWORDS = {"let", "rec", "in", "fun", "if", "then", "else", "print"}
SYMBOLS = ["->", "<>", "<=", ">=", "=", "<", ">", "+", "-", "*", "/", "%", "(", ")", ";"]
class Token:
def __init__(self, kind, text, line, col):
self.kind, self.text, self.line, self.col = kind, text, line, col
def scan(text):
tokens = []
for line_no, line in enumerate(text.splitlines(), 1):
i = 0
while i < len(line):
c = line[i]
if c.isspace():
i += 1
elif c == "#":
break
elif c.isalpha() or c == "_":
start = i
while i < len(line) and (line[i].isalnum() or line[i] in "_'"):
i += 1
word = line[start:i]
tokens.append(Token(word if word in KEYWORDS else "name", word, line_no, start + 1))
elif c.isdigit():
start = i
while i < len(line) and line[i].isdigit():
i += 1
tokens.append(Token("number", line[start:i], line_no, start + 1))
else:
for symbol in SYMBOLS:
if line.startswith(symbol, i):
tokens.append(Token(symbol, symbol, line_no, i + 1))
i += len(symbol)
break
else:
raise CompileError(f"unexpected character {c!r}", Token(c, c, line_no, i + 1))
if tokens:
last = tokens[-1]
tokens.append(Token("end", "", last.line, last.col + len(last.text)))
else:
tokens.append(Token("end", "", 1, 1))
return tokens
# The syntax tree. Each kind of node is a tuple whose first element
# names it, and whose last is the token it started at:
#
# ("num", value, tok) ("var", name, tok)
# ("neg", e, tok) ("op", op, left, right, tok)
# ("if", c, then, else, tok) ("print", e, tok)
# ("fun", param, body, tok) ("app", function, argument, tok)
# ("let", name, value, body, recursive, tok)
# ("seq", [e1, e2, ...], tok)
class Parser:
def __init__(self, tokens):
self.tokens = tokens
self.i = 0
def peek(self):
return self.tokens[self.i]
def at(self, *kinds):
return self.peek().kind in kinds
def take(self, kind):
token = self.peek()
if token.kind != kind:
found = "the end of the program" if token.kind == "end" else repr(token.text)
wanted = "a name" if kind == "name" else repr(kind)
raise CompileError(f"expected {wanted}, found {found}", token)
self.i += 1
return token
def program(self):
node = self.sequence()
self.take("end")
return node
def sequence(self):
token = self.peek()
items = [self.expr()]
while self.at(";"):
self.take(";")
items.append(self.expr())
return items[0] if len(items) == 1 else ("seq", items, token)
def expr(self):
token = self.peek()
if self.at("let"):
self.take("let")
recursive = self.at("rec")
if recursive:
self.take("rec")
name = self.take("name")
params = []
while self.at("name"):
params.append(self.take("name"))
self.take("=")
value = self.expr()
for p in reversed(params):
value = ("fun", p.text, value, p)
if recursive and value[0] != "fun":
raise CompileError("only a function can be 'let rec'", name)
self.take("in")
# The body reaches as far to the right as it can, past
# semicolons: everything after 'in' is in the scope of name.
return ("let", name.text, value, self.sequence(), recursive, name)
if self.at("fun"):
self.take("fun")
params = [self.take("name")]
while self.at("name"):
params.append(self.take("name"))
self.take("->")
body = self.sequence()
for p in reversed(params):
body = ("fun", p.text, body, p)
return body
if self.at("if"):
self.take("if")
condition = self.expr()
self.take("then")
then = self.expr()
self.take("else")
return ("if", condition, then, self.expr(), token)
if self.at("print"):
self.take("print")
return ("print", self.expr(), token)
return self.comparison()
def comparison(self):
left = self.sum()
if self.at("<", ">", "=", "<>", "<=", ">="):
op = self.take(self.peek().kind)
return ("op", op.kind, left, self.sum(), op)
return left
def sum(self):
node = self.product()
while self.at("+", "-"):
op = self.take(self.peek().kind)
node = ("op", op.kind, node, self.product(), op)
return node
def product(self):
node = self.unary()
while self.at("*", "/", "%"):
op = self.take(self.peek().kind)
node = ("op", op.kind, node, self.unary(), op)
return node
def unary(self):
if self.at("-"):
token = self.take("-")
return ("neg", self.unary(), token)
return self.application()
def application(self):
node = self.atom()
while self.at("number", "name", "("):
token = self.peek()
node = ("app", node, self.atom(), token)
return node
def atom(self):
token = self.peek()
if self.at("number"):
self.take("number")
if int(token.text) > 2**31 - 1:
raise CompileError(f"number too large: {token.text}", token)
return ("num", int(token.text), token)
if self.at("name"):
self.take("name")
return ("var", token.text, token)
if self.at("("):
self.take("(")
node = self.sequence()
self.take(")")
return node
found = "the end of the program" if token.kind == "end" else repr(token.text)
raise CompileError(f"expected a value, found {found}", token)
# Free variables: the variables an expression uses but does not bind.
# A function must carry the values of its free variables with it,
# since it may be called long after the place that bound them is gone.
def free(node):
kind = node[0]
if kind == "num":
return set()
if kind == "var":
return {node[1]}
if kind in ("neg", "print"):
return free(node[1])
if kind == "op":
return free(node[2]) | free(node[3])
if kind == "if":
return free(node[1]) | free(node[2]) | free(node[3])
if kind == "app":
return free(node[1]) | free(node[2])
if kind == "fun":
return free(node[2]) - {node[1]}
if kind == "let":
_, name, value, body, recursive, _ = node
if recursive:
return (free(value) | free(body)) - {name}
return free(value) | (free(body) - {name})
if kind == "seq":
return set().union(*(free(e) for e in node[1]))
raise ValueError(kind)
def first_use(node, name):
"""The token where name is first used in node, for error messages."""
if node[0] == "var":
return node[2] if node[1] == name else None
children = node[1] if node[0] == "seq" else [c for c in node[1:-1] if isinstance(c, tuple)]
for child in children:
found = first_use(child, name)
if found:
return found
return None
# Code generation.
#
# A function is compiled into code that expects two locals: local 0 is
# its own closure, local 1 its argument. A closure is a block:
#
# field 0 the address of the function's code
# field 1 ... the values of its free variables, in sorted order
#
# To call a closure c with an argument a: push c, push a, take field 0
# of c, and CALLI 2. The called code finds c and a as locals 0 and 1.
#
# Where each variable is, during code generation:
#
# ("local", k) local k of the current frame: LOAD k
# ("captured", i) field i of the current closure: LOAD 0, PUSH i, GETF
#
# 'depth' counts the values above the frame pointer at each point, so
# that a 'let' knows which local its value will be.
COMPARISONS = {
"=": ["EQ"], "<>": ["EQ", "PUSH 0", "EQ"], "<": ["LT"], ">": ["SWAP", "LT"],
"<=": ["SWAP", "LT", "PUSH 0", "EQ"], ">=": ["LT", "PUSH 0", "EQ"],
}
ARITHMETIC = {"+": "ADD", "-": "SUB", "*": "MUL", "/": "DIV", "%": "MOD"}
class Generator:
def __init__(self):
self.out = []
self.functions = [] # (label, fun node, free variables), waiting to be compiled
self.labels = 0
def emit(self, instruction):
self.out.append(f" {instruction}")
def new_label(self, stem="L"):
self.labels += 1
return f"{stem}{self.labels}"
def program(self, node):
self.out.append("main:")
self.gen(node, {}, 0)
self.emit("DROP")
self.emit("HALT")
while self.functions:
label, fun, names = self.functions.pop(0)
self.out.append("")
self.out.append(f"; fun {fun[1]} -> ..., capturing: {' '.join(names) or 'nothing'}")
self.out.append(f"{label}:")
env = {fun[1]: ("local", 1)}
for i, name in enumerate(names):
env.setdefault(name, ("captured", i + 1))
self.gen(fun[2], env, 2)
self.emit("RET")
return "\n".join(self.out) + "\n"
def variable(self, name, env, token):
if name not in env:
raise CompileError(f"'{name}' is not defined", token)
where, k = env[name]
if where == "local":
self.emit(f"LOAD {k}")
else:
self.emit("LOAD 0")
self.emit(f"PUSH {k}")
self.emit("GETF")
def closure(self, fun, env, depth, itself=None):
"""Make a closure for fun. If itself is a name, fun is being
bound to that name by 'let rec', and may refer to itself."""
names = sorted(free(fun))
for name in names:
if name != itself and name not in env:
raise CompileError(f"'{name}' is not defined", first_use(fun, name))
label = self.new_label("fun_")
self.functions.append((label, fun, names))
self.emit(f"PUSH {1 + len(names)}")
self.emit("NEW")
self.emit("DUP")
self.emit("PUSH 0")
self.emit(f"PUSH {label}")
self.emit("SETF")
for i, name in enumerate(names):
self.emit("DUP")
self.emit(f"PUSH {i + 1}")
if name == itself:
self.emit("OVER") # the closure being made
else:
self.variable(name, env, fun[3])
self.emit("SETF")
def gen(self, node, env, depth):
"""Generate code that leaves the value of node on the stack."""
kind = node[0]
if kind == "num":
self.emit(f"PUSH {node[1]}")
elif kind == "var":
self.variable(node[1], env, node[2])
elif kind == "neg":
self.emit("PUSH 0")
self.gen(node[1], env, depth + 1)
self.emit("SUB")
elif kind == "op":
_, op, left, right, _ = node
self.gen(left, env, depth)
self.gen(right, env, depth + 1)
for instruction in COMPARISONS.get(op, [ARITHMETIC.get(op)]):
self.emit(instruction)
elif kind == "if":
_, condition, then, otherwise, _ = node
other, done = self.new_label(), self.new_label()
self.gen(condition, env, depth)
self.emit(f"JZ {other}")
self.gen(then, env, depth)
self.emit(f"JMP {done}")
self.out.append(f"{other}:")
self.gen(otherwise, env, depth)
self.out.append(f"{done}:")
elif kind == "print":
self.gen(node[1], env, depth)
self.emit("DUP")
self.emit("PRINT")
elif kind == "fun":
self.closure(node, env, depth)
elif kind == "app":
_, function, argument, _ = node
self.gen(function, env, depth)
self.gen(argument, env, depth + 1)
self.emit("OVER")
self.emit("PUSH 0")
self.emit("GETF") # the code address of the closure
self.emit("CALLI 2")
elif kind == "let":
_, name, value, body, recursive, _ = node
if recursive:
self.closure(value, env, depth, itself=name)
else:
self.gen(value, env, depth)
inner = dict(env)
inner[name] = ("local", depth)
self.gen(body, inner, depth + 1)
self.emit("SWAP")
self.emit("DROP") # the let's local, under the result
elif kind == "seq":
for k, item in enumerate(node[1]):
self.gen(item, env, depth)
if k < len(node[1]) - 1:
self.emit("DROP")
def show(node):
kind = node[0]
if kind == "num":
return str(node[1])
if kind == "var":
return node[1]
if kind == "neg":
return f"(neg {show(node[1])})"
if kind == "op":
return f"({node[1]} {show(node[2])} {show(node[3])})"
if kind == "if":
return f"(if {show(node[1])} {show(node[2])} {show(node[3])})"
if kind == "print":
return f"(print {show(node[1])})"
if kind == "fun":
return f"(fun {node[1]} {show(node[2])})"
if kind == "app":
return f"({show(node[1])} {show(node[2])})"
if kind == "let":
return f"(let{' rec' if node[4] else ''} {node[1]} {show(node[2])} {show(node[3])})"
return "(seq " + " ".join(show(e) for e in node[1]) + ")"
def report(path, text, error):
lines = text.splitlines()
source = lines[error.line - 1] if 0 < error.line <= len(lines) else ""
print(f"{path}:{error.line}:{error.col}: {error}", file=sys.stderr)
print(f" {source}", file=sys.stderr)
print(f" {' ' * (error.col - 1)}^", file=sys.stderr)
def main():
args = sys.argv[1:]
tree = args[:1] == ["-tree"]
if tree:
args.pop(0)
if len(args) != 1:
sys.exit("usage: lambda.py [-tree] program.lam")
path = args[0]
with open(path) as f:
text = f.read()
try:
node = Parser(scan(text)).program()
if tree:
print(show(node))
return
sys.stdout.write(Generator().program(node))
except CompileError as error:
report(path, text, error)
sys.exit(1)
if __name__ == "__main__":
main()