-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
162 lines (137 loc) · 3.83 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
from tokens import Token, TokenType
import re
WHITESPACE = ' \n\t'
DIGITS = '0123456789.'
OPERATOR ='[iIfF]'
CONTAINER = '\{\}'
SIGN= '[=<>]'
VARIABLES = '[a-zA-Z_]'
comma_count = 0
compy =None
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):
global comma_count
while self.current_char != None:
v = re.findall( VARIABLES, self.current_char)
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 in OPERATOR:
yield self.confirm_ternary()
elif v:
yield self.var_name()
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()
comma_count+= 1
yield Token(TokenType.RPAREN)
elif self.current_char == '{':
self.advance()
yield Token(TokenType.INPUTSTART)
elif self.current_char == '}':
self.advance()
yield Token(TokenType.INPUTEND)
elif self.current_char == '=':
yield self.check_equal_or_assign()
elif self.current_char == ',':
checker = self.check_if_else()
if checker != None:
yield checker
else:
self.advance()
elif self.current_char == ':':
self.advance()
yield Token(TokenType.IS_ASSIGNED)
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, int(number_str))
def var_name(self):
var_str = self.current_char
p = re.findall( VARIABLES, self.current_char)
self.advance()
while self.current_char is not None and (p or self.current_char in DIGITS):
p = re.findall( VARIABLES, self.current_char)
var_str += self.current_char
self.advance()
return Token(TokenType.VARIABLE, var_str)
def confirm_ternary(self):
global comma_count
str_count = 0
x = re.findall( OPERATOR, self.current_char)
while self.current_char != None and x:
if x:
str_count += 1
self.advance()
y = re.findall( OPERATOR, self.current_char)
if y:
str_count += 1
self.advance()
if str_count > 1:
break
comma_count = 0
return Token(TokenType.STATEMENT)
def check_if_else(self):
global comma_count
global compy
if self.current_char is not None and self.current_char == ',' and compy=='equate':
if comma_count == 0:
self.advance()
comma_count += 1
return Token(TokenType.TRUTHY)
if comma_count == 1:
self.advance()
comma_count = 0
return Token(TokenType.FALSY)
def check_equal_or_assign(self):
eq_count = 0
global compy
m = re.findall( SIGN, self.current_char)
while self.current_char != None and m:
if self.current_char == '=':
eq_count += 1
self.advance()
if eq_count == 1 and self.current_char == '=':
self.advance()
compy = 'equate'
eq_count += 1
return Token(TokenType.EQUALTO)
if eq_count > 1:
break