-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
69 lines (62 loc) · 1.77 KB
/
parser.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
from cells import *
class ParseError(Exception):
pass
class Parser:
def parse(self, tokens):
self.tokens = tokens
res = []
while self.tokens and self.tokens[0] is not None:
res.append(self.S())
return res
def next(self):
try:
self.current = self.tokens.pop(0)
except:
self.current = None
def S(self, getnext=True):
if getnext:
self.next()
if self.current.toknum == 4 or self.current.toknum == 6: # added 6 for BQUOTE
C = Cell(self.current, Cell(self.S(), Nil())) # quote takes precisely one s-expression
return C
elif self.current.toknum in [7, 17]: #splicing for macros
C = Cell(self.current, Cell(self.S(), Nil()))
return C
if self.current.toknum == 0:
self.next()
if self.current.toknum == 1:
return Nil()
C = self.L(False)
if self.current.toknum == 1:
return C
else:
raise ParseError
else:
return self.current
def L(self, getnext=True):
if getnext:
self.next()
res = Cell(None, None)
C = res
P = res
while True:
C.first = self.S(False)
self.next()
if self.current.toknum == 1:
C.rest = Nil()
break
else:
new = Cell(None, None)
C.rest = new
C = new
return res
if __name__ == '__main__':
from lexer import *
T = Lexer()
t = T.tokenize("""(define (fib-iter n a b)
(if (= n 0)
a
(fib-iter (- n 1) b (+ a b))))""")
P = Parser()
L = P.parse(t)
print(L)