-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
129 lines (95 loc) · 3.34 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
class Lexer:
# while <expr> do <statement>
digits = "0123456789"
letters = "abcdefghijklmnopqrstuvwxyz"
operations = "+-/*%()="
stopwords = [" "]
declarations = ["make"]
boolean = ["and", "or", "not"]
comparisons = [">", "<", ">=", "<=", "?="]
specialCharacters = "><=?"
reserved = ["if", "elif", "else", "do", "while"]
def __init__(self, text):
self.text = text
self.idx = 0
self.tokens = []
self.char = self.text[self.idx]
self.token = None
def tokenize(self):
while self.idx < len(self.text):
if self.char in Lexer.digits:
self.token = self.extract_number()
elif self.char in Lexer.operations:
self.token = Operation(self.char)
self.move()
elif self.char in Lexer.stopwords:
self.move()
continue
elif self.char in Lexer.letters:
word = self.extract_word()
if word in Lexer.declarations:
self.token = Declaration(word)
elif word in Lexer.boolean:
self.token = Boolean(word)
elif word in Lexer.reserved:
self.token = Reserved(word)
else:
self.token = Variable(word)
elif self.char in Lexer.specialCharacters:
comparisonOperator = ""
while self.char in Lexer.specialCharacters and self.idx < len(self.text):
comparisonOperator += self.char
self.move()
self.token = Comparison(comparisonOperator)
self.tokens.append(self.token)
return self.tokens
def extract_number(self):
number = ""
isFloat = False
while (self.char in Lexer.digits or self.char == ".") and (self.idx < len(self.text)):
if self.char == ".":
isFloat = True
number += self.char
self.move()
return Integer(number) if not isFloat else Float(number)
def extract_word(self):
word = ""
while self.char in Lexer.letters and self.idx < len(self.text):
word += self.char
self.move()
return word
def move(self):
self.idx += 1
if self.idx < len(self.text):
self.char = self.text[self.idx]
class Token:
def __init__(self, type, value):
self.type = type
self.value = value
def __repr__(self):
return str(self.value)
class Integer(Token):
def __init__(self, value):
super().__init__("INT", value)
class Float(Token):
def __init__(self, value):
super().__init__("FLT", value)
class Operation(Token):
def __init__(self, value):
super().__init__("OP", value)
class Declaration(Token):
def __init__(self, value):
super().__init__("DECL", value)
class Variable(Token):
def __init__(self, value):
super().__init__("VAR(?)", value) # Variable name, VAR, data type
# make a = 5 # VAR(?)
class Boolean(Token):
def __init__(self, value):
super().__init__("BOOL", value)
class Comparison(Token):
def __init__(self, value):
super().__init__("COMP", value)
class Reserved(Token):
def __init__(self, value):
super().__init__("RSV", value)