-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
239 lines (217 loc) · 10.4 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
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
class Token:
def __init__(self, type_, value):
self.type = type_
self.value = value
def __str__(self):
return f'Token({self.type}, {repr(self.value)})'
def __repr__(self):
return self.__str__()
class Lexer:
def __init__(self, text):
self.text = []
lines = text.split('\n')
for line in lines:
line = line.rstrip()
if line != '':
self.text.append('\n')
self.text.append(line)
self.text = '\n'.join(self.text)
self.pos = 0
self.current_char = self.text[0] if text else None
def advance(self):
self.pos += 1
if self.pos > len(self.text) - 1:
self.current_char = None
else:
self.current_char = self.text[self.pos]
def unadvance(self):
self.pos -= 1
self.current_char = self.text[self.pos]
def skip_whitespace(self):
while self.current_char and self.current_char.isspace():
if self.current_char == '\n':
self.advance()
return Token('NEWLINE', '\n')
self.advance()
return None
def get_number(self):
result = ''
while self.current_char is not None and self.current_char.isdigit():
result += self.current_char
self.advance()
return int(result)
def get_identifier(self):
result = ''
while self.current_char is not None and self.current_char.isalnum():
result += self.current_char
self.advance()
return result
def get_string(self):
self.advance()
result = ''
while self.current_char is not None and self.current_char != '"':
result += self.current_char
self.advance()
self.advance()
return result
def tokenize(self):
tokens = []
while self.current_char:
if self.current_char.isspace():
if self.current_char == '\n':
if self.pos != 0:
tokens.append(Token('NEWLINE', '\n'))
spaces = 0
while self.current_char and self.current_char.isspace():
spaces += 1
self.advance()
self.unadvance()
indent_level = spaces // 4
tokens.append(Token('INDENT', indent_level))
else:
self.skip_whitespace()
elif self.current_char.isdigit():
tokens.append(Token('NUMBER', self.get_number()))
elif self.current_char == '"':
tokens.append(Token('STRING', self.get_string()))
elif self.current_char.isalpha():
identifier = self.get_identifier()
match identifier:
case 'Comment':
while self.current_char != '\n':
self.advance()
self.advance()
case 'plus':
tokens.append(Token('OPERATOR', '+'))
case 'minus':
tokens.append(Token('OPERATOR', '-'))
case 'times':
tokens.append(Token('OPERATOR', '*'))
case 'divide':
tokens.append(Token('OPERATOR', '/'))
case 'true':
tokens.append(Token('BOOLEAN', True))
case 'false':
tokens.append(Token('BOOLEAN', False))
case 'is':
self.skip_whitespace()
if self.current_char and self.current_char.isalpha():
identifier = self.get_identifier()
if identifier == 'now':
tokens.append(Token('KEYWORD', 'assign'))
else:
tokens.append(Token('VARIABLE', 'is'))
continue
case 'create':
tokens.append(Token('KEYWORD', 'create_function'))
self.skip_whitespace()
tokens.append(Token('FUNCTION', self.get_identifier()))
case 'run':
tokens.append(Token('KEYWORD', 'run_function'))
self.skip_whitespace()
tokens.append(Token('FUNCTION', self.get_identifier()))
case 'if':
tokens.append(Token('KEYWORD', 'if'))
case 'otherwise':
tokens.append(Token('KEYWORD', 'else'))
case 'output':
tokens.append(Token('KEYWORD', 'print'))
case 'input':
tokens.append(Token('KEYWORD', 'input'))
case 'repeat':
self.skip_whitespace()
if self.current_char and self.current_char.isdigit():
number = self.get_number()
self.skip_whitespace()
identifier = self.get_identifier()
if identifier == 'times':
tokens.append(Token('KEYWORD', 'for_loop'))
tokens.append(Token('NUMBER', number))
else:
tokens.append(Token('VARIABLE', 'repeat'))
tokens.append(Token('NUMBER', number))
tokens.append(Token('VARIABLE', identifier))
elif self.current_char and self.current_char.isalpha():
identifier = self.get_identifier()
if identifier == 'until':
tokens.append(Token('KEYWORD', 'while_loop'))
self.skip_whitespace()
continue
else:
self.skip_whitespace()
next_identifier = self.get_identifier()
if next_identifier == 'times':
tokens.append(Token('KEYWORD', 'for_loop'))
tokens.append(Token('VARIABLE', identifier))
else:
tokens.append(Token('VARIABLE', 'repeat'))
tokens.append(Token('VARIABLE', identifier))
else:
tokens.append(Token('VARIABLE', 'repeat'))
continue
case 'number':
self.skip_whitespace()
identifier = self.get_identifier()
if identifier == 'between':
self.skip_whitespace()
if self.current_char and (self.current_char.isdigit() or self.current_char.isalpha()):
if self.current_char.isdigit():
bound1 = Token('NUMBER', self.get_number())
else:
bound1 = Token('VARIABLE', self.get_identifier())
self.skip_whitespace()
identifier = self.get_identifier()
if identifier == 'and':
self.skip_whitespace()
if self.current_char and (self.current_char.isdigit() or self.current_char.isalpha()):
if self.current_char.isdigit():
bound2 = Token('NUMBER', self.get_number())
else:
bound2 = Token('VARIABLE', self.get_identifier())
tokens.append(Token('KEYWORD', 'random'))
tokens.append(bound1)
tokens.append(bound2)
else:
tokens.extend([Token('VARIABLE', 'number'), Token('VARIABLE', 'between'), bound1])
self.unadvance()
else:
tokens.extend([Token('VARIABLE', 'number'), Token('VARIABLE', 'between'), bound1, Token('VARIABLE', identifier)])
else:
tokens.append(Token('VARIABLE', 'number'))
tokens.append(Token('VARIABLE', 'between'))
self.unadvance()
else:
tokens.append(Token('VARIABLE', 'number'))
tokens.append(Token('VARIABLE', identifier))
case _:
tokens.append(Token('VARIABLE', identifier))
elif self.current_char in ['=', '>', '<', '!']:
compare = self.current_char
self.advance()
if self.current_char in ['=', '>', '<', '!']:
compare += self.current_char
else:
self.unadvance()
match compare:
case '=' | '==':
tokens.append(Token('COMPARE', '=='))
case '!=' | '=!':
tokens.append(Token('COMPARE', '!='))
case '>=' | '=>':
tokens.append(Token('COMPARE', '>='))
case '<=' | '=<':
tokens.append(Token('COMPARE', '<='))
case '>':
tokens.append(Token('COMPARE', '>'))
case '<':
tokens.append(Token('COMPARE', '<'))
self.advance()
self.advance()
return tokens
if __name__ == '__main__':
input_text = '''
x is now true
'''
lexer = Lexer(input_text)
tokens = lexer.tokenize()
print("Tokens:", tokens)