-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
161 lines (153 loc) · 6.75 KB
/
lexer.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
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
class Tokenize:
def __init__(self, code):
self.code = code
self.position = 0
self.tokens = []
class TokenizeException(Exception):
pass
def tokenize_number(self):
digits = ''
is_float = False
while (self.position < len(self.code) and self.code[self.position].isdigit()) or (self.position < len(self.code) and self.code[self.position] == "."):
if self.code[self.position] == ".":
is_float = True
digits += self.code[self.position]
self.position += 1
if len(digits) > 0:
if is_float:
self.tokens.append(('FLOAT', digits))
else:
self.tokens.append(('INTEGER', digits))
def tokenize_reserved_word_or_identifier(self):
name = ''
if self.position < len(self.code) and self.code[self.position].isalpha():
name += self.code[self.position]
self.position += 1
while self.position < len(self.code) and self.code[self.position].isalnum():
name += self.code[self.position]
self.position += 1
if len(name) > 0:
if name == 'if':
self.tokens.append(('IfToken', name))
elif name == 'while':
self.tokens.append(('WhileToken', name))
elif name == 'for':
self.tokens.append(('ForToken', name))
elif name == 'var':
self.tokens.append(('VarToken', name))
elif name == 'def':
self.tokens.append(('DefToken', name))
elif name == 'print':
self.tokens.append(('PrintToken', name))
elif name == 'pair':
self.tokens.append(('PairToken', name))
# elif name == 'op':
# self.tokens.append(('OpToken', name))
elif name == 'fst':
self.tokens.append(('FSTToken', name))
elif name == 'snd':
self.tokens.append(('SNDToken', name))
elif name == 'return':
self.tokens.append(('ReturnToken', name))
elif name == 'break':
self.tokens.append(('BreakToken', name))
elif name == 'true':
self.tokens.append(("TrueToken", name))
elif name == 'false':
self.tokens.append(("FalseToken", name))
elif name == "int":
self.tokens.append(("IntTypeToken", name))
elif name == "boolean":
self.tokens.append(("BooleanTypeToken", name))
elif name == "string":
self.tokens.append(("StringTypeToken", name))
elif name == "float":
self.tokens.append(("FloatTypeToken", name))
elif name == "T":
self.tokens.append(("TTypeToken", name))
elif name == "in":
self.tokens.append(("InToken",name))
elif name == "else":
self.tokens.append(("ElseToken", name))
else:
self.tokens.append(('IDENTIFIER', name))#this will handle identifiers and also strings so just be careful
def tokenize_symbol(self):
if self.position + 1 < len(self.code):
if self.code[self.position:self.position + 2] == '==':
self.tokens.append(('DoubleEqualToken', '=='))
self.position += 2
elif self.code[self.position:self.position + 2] == '>=':
self.tokens.append(('GreaterOrEqualToken', '>='))
self.position += 2
elif self.code[self.position:self.position + 2] == '<=':
self.tokens.append(('LessOrEqualToken', '<='))
self.position += 2
elif self.code[self.position:self.position + 2] == '&&':
self.tokens.append(('LogicalAndToken', '&&'))
self.position += 2
elif self.code[self.position:self.position + 2] == '||':
self.tokens.append(('LogicalOrToken', '||'))
self.position += 2
if self.code[self.position] == '(':
self.tokens.append(('LeftParenToken', '('))
self.position += 1
elif self.code[self.position] == ')':
self.tokens.append(('RightParenToken', ')'))
self.position += 1
elif self.code[self.position] == '=':
self.tokens.append(('SingleEqualToken', '='))
self.position += 1
elif self.code[self.position] == '{':
self.tokens.append(('LeftBraceToken', '{'))
self.position += 1
elif self.code[self.position] == '}':
self.tokens.append(('RightBraceToken', '}'))
self.position += 1
elif self.code[self.position] == '<':
self.tokens.append(('LessThanToken', '<'))
self.position += 1
elif self.code[self.position] == '>':
self.tokens.append(('GreaterThanToken', '>'))
self.position += 1
elif self.code[self.position] == ':':
self.tokens.append(('ColonToken', ':'))
self.position += 1
elif self.code[self.position] == '+':
self.tokens.append(('PlusToken', '+'))
self.position += 1
elif self.code[self.position] == '-':
self.tokens.append(('MinusToken', '-'))
self.position += 1
elif self.code[self.position] == '*':
self.tokens.append(('MultiplicationToken', '*'))
self.position += 1
elif self.code[self.position] == '/':
self.tokens.append(('DivisionToken', '/'))
self.position += 1
elif self.code[self.position] == '//':
self.tokens.append(('DoubleDivisionToken', '/'))
self.position += 1
elif self.code[self.position] == '%':
self.tokens.append(('ModToken', '%'))
self.position += 1
elif self.code[self.position] == '\"':
self.tokens.append(('DoubleQuoteToken', '\"'))
self.position += 1
# def tokenizeStatement(self):
def tokenize(self):
while self.position < len(self.code):
cur_pos = self.position
if self.code[self.position] == ' ':
self.position += 1
elif self.code[self.position].isdigit():
self.tokenize_number()
elif self.code[self.position].isalpha():
self.tokenize_reserved_word_or_identifier()
else:
self.tokenize_symbol()
if cur_pos == self.position:
raise self.TokenizeException(f'Couldn\'t parse this character {self.code[self.position]}')
return self.tokens
myProgram = 'def main ( int hehehaha ) { ( = varone 30 ) }'
tokens = Tokenize(myProgram).tokenize()
print("result")