-
Notifications
You must be signed in to change notification settings - Fork 0
/
whl.py
255 lines (216 loc) · 8.3 KB
/
whl.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# region constants
DIGITS = "0123456789"
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
alphabet = "abcdefghijklmnopqrstuvwxyz"
operators = "^*/%+-"
quotes = '"' + "'"
TTSTR = "STRING"
TTINT = "INT"
TTFLOAT = "FLOAT"
TTBOOL = "BOOL"
TTVAR = "VAR"
TTCRVAR = "CREATE VAR"
TTCOM = "COMMENT"
TTOP = "OPERATOR"
TTBOOLOP = "BOOL OP"
TTPRINT = "PRINT"
TTIF = "IF"
TTELIF = "ELIF"
TTELSE = "ELSE"
TTENDLINE = "END LINE"
VAR_VALUES = [TTSTR, TTINT, TTFLOAT]
# endregion
vars = []
def get_type(value):
if str(value)[0] in DIGITS + "-":
for digit in str(value):
if digit == '.':
return TTFLOAT
return TTINT
elif str(value) in ["True", "False"]:
return TTBOOL
else:
return TTSTR
class Var:
def __init__(self, name, value=None, type_=None):
self.name = name
self.value = value
self.type = type_
if type_ is None and value is not None:
self.type = get_type(self.value)
def __repr__(self):
return f'{self.value}'
class Token:
def __init__(self, type_, value=None):
self.type = type_
self.value = value
def __repr__(self):
return f'Type: {self.type} Value: {self.value}'
# expression, and can calculate itself
class Exp:
def __init__(self, tokens, type_):
self.type = type_
self.tokens = tokens
def calc(self):
if self.type == "^":
return self.tokens[0].value ** self.tokens[1].value
elif self.type == "*":
return self.tokens[0].value * self.tokens[1].value
elif self.type == "/":
return self.tokens[0].value / self.tokens[1].value
elif self.type == "+":
return self.tokens[0].value + self.tokens[1].value
elif self.type == "-":
return self.tokens[0].value - self.tokens[1].value
# making a list of tokens, for the executioner to recognize and work with
class Lexer:
def __init__(self, text):
self.code = text
self.tokens = []
self.analyze()
def analyze(self):
for line in self.code.split("\n"):
type_ = ""
str_out = ""
str_token = ""
# region comment
if line[0] == "#" and type_ == "":
type_ = TTCOM
if type_ == TTCOM:
type_ = ""
continue
# endregion
for word in line.split():
# region string
# if still string
if type_ == TTSTR:
str_out += word
# starting string
elif not type_ == TTSTR and word[0] in quotes:
type_ = TTSTR
str_token = word[0]
str_out += word[1:]
# ending the string
if type_ == TTSTR and word[len(word) - 1] == str_token and not str_out[len(str_out) - 5] == "/":
str_out = str_out[:len(str_out) - 1]
str_token = ""
type_ = ""
if type_ == TTSTR:
str_out += " "
if not type_ == TTSTR and str_out != "":
self.tokens.append(Token(TTSTR, str_out))
str_out = ""
continue
# endregion
# region int
if type_ == "" and word[0] in DIGITS + "-":
type_ = TTINT
if type_ == TTINT:
dec = 0
num_out = 0
for digit in word:
if digit == ".":
type_ = TTFLOAT
dec = 0.1
elif digit == "-":
continue
elif type_ == TTINT:
num_out *= 10
num_out += int(digit)
if type_ == TTFLOAT and digit != ".":
num_out += int(digit) * dec
dec /= 10
if word[0] == "-":
num_out *= -1
self.tokens.append(Token(type_, num_out))
type_ = ""
continue
# endregion
# region var
if type_ == "" and word[0] == "$":
# if the var is in the memory
for v in vars:
if v.name == word[1:]:
type_ = TTVAR
self.tokens.append(Token(type_, v))
# if the var wasn't found in the memory
if type_ != TTVAR:
type_ = TTCRVAR
self.tokens.append(Token(type_, word[1:]))
type_ = ""
type_ = ""
# endregion
# region operators
if type_ == "" and ((len(word) == 1 and word in operators + '=') or (len(word) == 2 and word[1] == '=' and word[0] in operators)):
self.tokens.append(Token(TTOP, word))
# endregion
# region bool operators
if type_ == "" and len(word) == 2 and word[1] == "=" and not word[0] in operators:
self.tokens.append(Token(TTBOOLOP, word))
#endregion
# region saved words
if type_ == "":
if word == "print":
self.tokens.append(Token(TTPRINT))
elif word == "if":
self.tokens.append(Token(TTIF))
#endregion
self.tokens.append(Token(TTENDLINE))
print(self.tokens)
class Executioner:
def __init__(self, tokens):
self.tokens = tokens
self.execute()
def expression(self, token_array):
todo = []
for t in token_array:
if t.type == TTVAR:
var = self.find_var(t.value.name)
if var is not None:
todo.append(Token(var.type, var.value))
else:
print(f'Error! There is no var named {t.value}')
else:
todo.append(t)
for op in operators:
for t in todo:
if t.type == TTOP and t.value == op:
operands = [todo[todo.index(t) - 1], todo[todo.index(t) + 1]]
del todo[todo.index(operands[0])+1:todo.index(operands[1])+1]
express = Exp(operands, op).calc()
todo[todo.index(operands[0])] = Token(get_type(express), express)
return todo[0]
def find_var(self, search, is_value=False):
for v in vars:
if (not is_value and v.name == search) or (is_value and v.value == search):
return v
return None
def execute(self):
i = 0
t = None
while i < len(self.tokens):
j = None
for j in self.tokens[i:]:
if j.type == TTENDLINE:
break
endline = self.tokens.index(j)
for t in self.tokens[i:endline]:
if t.type == TTCRVAR:
if self.tokens[i + 1].value == "=":
value = self.expression(self.tokens[i + 2:endline]).value
vars.append(Var(t.value, value, get_type(value)))
elif self.tokens[i + 1].type == TTENDLINE:
vars.append(Var(t.value))
elif t.type == TTVAR:
if self.tokens[i+1].value == "=":
vars[vars.index(self.find_var(t.value.name))] = Var(t.value.name, self.expression(self.tokens[i+2:endline]).value, get_type(self.expression(self.tokens[i+2:endline]).value))
elif self.tokens[i+1].type == TTOP and len(self.tokens[i+1].value) == 2:
vars[vars.index(self.find_var(t.value.name))] = Var(t.value.name, Exp([vars[vars.index(self.find_var(t.value.name))], self.expression(self.tokens[i+2:endline])], self.tokens[i+1].value[0]).calc())
elif t.type == TTPRINT:
print(self.expression(self.tokens[i + 1:endline]))
i = endline + 1
def comp(code):
exec = Executioner(Lexer(code).tokens)
while True:
text = input("whale >>")
comp(text)