-
Notifications
You must be signed in to change notification settings - Fork 13
/
lexer.py
63 lines (54 loc) · 1.59 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
from tokens import Token, TokenType
WHITESPACE = ' \n\t'
DIGITS = '0123456789'
class Lexer:
def __init__(self, text):
self.text = iter(text)
self.advance()
def advance(self):
try:
self.current_char = next(self.text)
except StopIteration:
self.current_char = None
def generate_tokens(self):
while self.current_char != None:
if self.current_char in WHITESPACE:
self.advance()
elif self.current_char == '.' or self.current_char in DIGITS:
yield self.generate_number()
elif self.current_char == '+':
self.advance()
yield Token(TokenType.PLUS)
elif self.current_char == '-':
self.advance()
yield Token(TokenType.MINUS)
elif self.current_char == '*':
self.advance()
yield Token(TokenType.MULTIPLY)
elif self.current_char == '/':
self.advance()
yield Token(TokenType.DIVIDE)
elif self.current_char == '(':
self.advance()
yield Token(TokenType.LPAREN)
elif self.current_char == ')':
self.advance()
yield Token(TokenType.RPAREN)
else:
raise Exception(f"Illegal character '{self.current_char}'")
def generate_number(self):
decimal_point_count = 0
number_str = self.current_char
self.advance()
while self.current_char != None and (self.current_char == '.' or self.current_char in DIGITS):
if self.current_char == '.':
decimal_point_count += 1
if decimal_point_count > 1:
break
number_str += self.current_char
self.advance()
if number_str.startswith('.'):
number_str = '0' + number_str
if number_str.endswith('.'):
number_str += '0'
return Token(TokenType.NUMBER, float(number_str))