-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.py
66 lines (52 loc) · 1.69 KB
/
tokens.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
class Token:
def __init__(self, lexval, toknum):
"""lexval is the actual string of symbols that the token represents.
toknum is the number that the parser will read."""
self.lexval = lexval
self.toknum = toknum
def __repr__(self):
return str(self.lexval)
## useful for when you're testing and want to see legit tokens
# def __repr__(self):
# res = []
# for x in [i for i in dir(self) if not i.startswith('__')]:
# res.append(x+'='+str(self.__getattribute__(x)))
# res = '<' + ', '.join(res) + '>'
# return res
class LParen(Token):
def __init__(self):
Token.__init__(self, '(', 0)
class RParen(Token):
def __init__(self):
Token.__init__(self, ')', 1)
class ID(Token):
def __init__(self, lexval):
Token.__init__(self, lexval, 2)
class Int(Token):
def __init__(self, lexval):
Token.__init__(self, int(lexval), 3)
class Quote(Token):
def __init__(self):
Token.__init__(self, "'", 4)
class DQuote(Token):
def __init__(self):
Token.__init__(self, '"', 5)
class BQuote(Token):
def __init__(self):
Token.__init__(self, '`', 6)
class Comma(Token):
def __init__(self):
Token.__init__(self, ',', 7)
class UnSyntaxSplicing(Token):
def __init__(self):
Token.__init__(self, ',@', 17)
class String(Token):
def __init__(self, lexval):
Token.__init__(self, lexval, 18)
class Float(Token):
"""Token number is so high because it was implemented later."""
def __init__(self, lexval):
Token.__init__(self, float(lexval), 19)
class GenSym(ID):
def __init__(self):
ID.__init__(self, 'gensym')