-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.py
364 lines (314 loc) · 13 KB
/
tokens.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import traceback
import re
tokens = [
"ADD",
"SUBTRACT",
#"MULTIPLY",
#"DIVIDE",
"IF",
"ELSE",
#"WHILE",
"PRINT",
"OPEN_FILE",
"CLOSE_FILE",
"CALC",
"INPUT",
"LOOP",
"WRITE_FILE",
"READ_FILE",
]
functions = []
#General variable storage. We have no concept of those fancy things like "global/private/public" variables. Pointers? Did you think this was C or something?
r_vars = {}
#storage for functions in the yaml file. Classes? The fuck are those? OOP? Sir, I'm way too innebriated for that.
r_funcs = {}
def START(token):
''' ICH, NI, SAN... STARTO! '''
#print(token)
for t in token.items():
r_funcs[t[0]] = t
functions.append(t[0])
process_tokens(r_funcs["START"][1:][0])
def FUNC(func):
''' WHERRRRRE IS THE FUNC... GOTTA HAVE THAT FUNC... OWWWW'''
#print(func)
if func in r_funcs:
#print(r_funcs[func][1:][0])
process_tokens(r_funcs[func][1:][0])
def INPUT(r):
''' It gets input from the user, duh. '''
line = input()
r_vars[r] = line
def PRINT(line):
''' It... wait for it... PRINTS stuff to the screen. '''
complete_line = ""
for l in line:
if l[0] == "$":
#print(r_vars[l])
complete_line += str(r_vars[l])
else:
complete_line += str(l)
print(complete_line)
def ADD(numbers):
''' Can you put 2 and 2 together and figure out what this does? '''
num = list()
for n in numbers:
if type(n) is not int and type(n) is not float:
if n[0] == "$":
num.append(r_vars[n])
else:
num.append(n)
#print(numbers)
return sum(num)
def SUBTRACT(numbers):
''' When opposites subtract... or is it attract... something like that '''
#print(numbers)
total = numbers[0]
for n in numbers[1:]:
total -= n
return total
def CALC(string):
''' Seems like a calculating function. FYI this is just a straight eval(string) line. No really, it is. '''
return eval(string)
#Loops here will always have the 'line' iterator that they can reference for now. I'm too lazy to make custom iterators atm.
def LOOP(token, item):
''' Fruity Loops. Except without the music. For now atleast. Maybe I'll throw some magical fucking AI shit in here. '''
#print("item is: " + str(item))
for l in item:
x = list()
#print(l)
for t in token:
#print("token is:")
#print(t)
if type(t) == list:
if t[0]== "IF":
x.append(t)
elif type(t[0]) == list:
if t[1][0] != "$line":
x.append(t)
#process_tokens(t)
else:
#print([t[0], l])
x.append([t[0], [l]])
#print(x)
process_tokens(x)
def OPEN_FILE(file, var):
''' OPEN SESAME SEEDS... or says a me, or says me... Aladdin help ya boy out. OPEN's a file pointer and throws it into a variable, if the file doesn't exist, it makes one for you, neat huh?.'''
r_vars[var] = open(file,"a+")
def READ_FILE(file,var):
''' What? You think this is reading rainbow? It reads a file dumbass. Keep in mind you have to OPEN it first. '''
f = GET_VAR(file)
f.seek(0)
lines = f.readlines()
r_vars[var] = lines
return True
def WRITE_FILE(file, data):
''' Is it WRITE or is it wrong? Write's shit to a file. Gotta have it OPEN first though. '''
f = GET_VAR(file)
for d in data:
f.write(d)
return True
def CLOSE_FILE(file):
''' if I need to explain this to you we've got some problems.'''
f = GET_VAR(file)
f.close()
def GET_VAR(item):
''' Gets the Vars from r_vars ya har?'''
if item[0] == "$":
i = r_vars[item]
if i is not None:
return i
else:
return False
def VAR(name, data):
''' Stores a variable in r_vars '''
r_vars["$"+name] = data
return True
#This is really fucking ugly, but it gets the job done for now with integer/float comparisons and true/false statements.
def IF(s):
''' No ifs ands or buts about it '''
t = ["LT","GT","EQ","TRUE","FALSE","LTEQ","GTEQ","CONTAIN","NOTCONTAIN"]
process_else = True
if s[2] in t:
if s[2] == "LT":
try:
if type(s[1]) == str and type[s[3]] == float:
#print(r_vars[s[1]])
if float(r_vars[s[1]]) < float(s[3]):
process_else = process_if_tokens(s,t)
elif type(s[1]) == float and type(s[3]) == str:
if float(s[1]) < float(r_vars[s[3]]):
process_else = process_if_tokens(s,t)
elif type(s[1]) == str and type(s[3]) == str:
if float(r_vars[s[1]]) < float(r_vars[s[3]]):
process_else = process_if_tokens(s,t)
else:
if float(s[1]) < float(s[3]):
process_else = process_if_tokens(s,t)
except Exception as e:
print(traceback.format_exc())
if s[2] == "GT":
try:
if type(s[1]) == str and type[s[3]] == float:
#print(r_vars[s[1]])
if float(r_vars[s[1]]) > float(s[3]):
process_else = process_if_tokens(s,t)
elif type(s[1]) == float and type(s[3]) == str:
if float(s[1]) > float(r_vars[s[3]]):
process_else = process_if_tokens(s,t)
elif type(s[1]) == str and type(s[3]) == str:
if float(r_vars[s[1]]) > float(r_vars[s[3]]):
process_else = process_if_tokens(s,t)
else:
if float(s[1]) > float(s[3]):
process_else = process_if_tokens(s,t)
except Exception as e:
print(traceback.format_exc())
if s[2] == "EQ" or s[2] == "TRUE":
try:
if type(s[1]) == str and type[s[3]] == float:
#print(r_vars[s[1]])
if float(r_vars[s[1]]) == float(s[3]):
process_else = process_if_tokens(s,t)
elif type(s[1]) == float and type(s[3]) == str:
if float(s[1]) == float(r_vars[s[3]]):
process_else = process_if_tokens(s,t)
elif type(s[1]) == str and type(s[3]) == str:
if r_vars[s[1]] == r_vars[s[3]]:
process_else = process_if_tokens(s,t)
else:
if float(s[1]) == float(s[3]):
process_else = process_if_tokens(s,t)
except Exception as e:
print(traceback.format_exc())
if s[2] == "GTEQ":
try:
if type(s[1]) == str and type[s[3]] == float:
#print(r_vars[s[1]])
if float(r_vars[s[1]]) >= float(s[3]):
process_else = process_if_tokens(s,t)
elif type(s[1]) == float and type(s[3]) == str:
if float(s[1]) >= float(r_vars[s[3]]):
process_else = process_if_tokens(s,t)
elif type(s[1]) == str and type(s[3]) == str:
if r_vars[s[1]] >= r_vars[s[3]]:
process_else = process_if_tokens(s,t)
else:
if float(s[1]) >= float(s[3]):
process_else = process_if_tokens(s,t)
except Exception as e:
print(traceback.format_exc())
if s[2] == "LTEQ":
try:
if type(s[1]) == str and type[s[3]] == float:
#print(r_vars[s[1]])
if float(r_vars[s[1]]) <= float(s[3]):
process_else = process_if_tokens(s,t)
elif type(s[1]) == float and type(s[3]) == str:
if float(s[1]) <= float(r_vars[s[3]]):
process_else = process_if_tokens(s,t)
elif type(s[1]) == str and type(s[3]) == str:
if float(r_vars[s[1]]) <= float(r_vars[s[3]]):
process_else = process_if_tokens(s,t)
else:
if float(s[1]) <= float(s[3]):
process_else = process_if_tokens(s,t)
except Exception as e:
print(traceback.format_exc())
if s[2] == "CONTAIN":
try:
if type(s[1]) != int and type(s[3]) != int:
if str(s[1][0]) == "$":
if str(s[3][0]) == "$":
if re.search(str(r_vars[s[3]]),str(r_vars[s[1]])):
process_else = process_if_tokens(s,t)
else:
if re.search(str(s[3]),str(r_vars[s[1]])):
process_else = process_if_tokens(s,t)
else:
if str(s[3][0]) == "$":
if re.search(str(r_vars[s[3]]),str(s[1])):
process_else = process_if_tokens(s,t)
else:
if re.search(str(s[3]),str(s[1])):
process_else = process_if_tokens(s,t)
except Exception as e:
print(traceback.format_exc())
if s[2] == "NOTCONTAIN":
try:
if type(s[1]) != int and type(s[3]) != int:
if str(s[1][0]) == "$":
if str(s[3][0]) == "$":
if re.search(str(r_vars[s[3]]),str(r_vars[s[1]])) == None:
process_else = process_if_tokens(s,t)
else:
if re.search(str(s[3]),str(r_vars[s[1]])) == None:
process_else = process_if_tokens(s,t)
else:
if str(s[3][0]) == "$":
if re.search(str(r_vars[s[3]]),str(s[1])) == None:
process_else = process_if_tokens(s,t)
else:
if re.search(str(s[3]),str(s[1])) == None:
process_else = process_if_tokens(s,t)
except Exception as e:
print(traceback.format_exc())
if len(s) >= 6 and process_else == True:
try:
process_else_tokens(s, t)
except Exception as e:
print(traceback.format_exc())
def process_if_tokens(s, t,):
for t in s[4]:
if t[0] in tokens or t[0] in functions:
#print(s[4][1])
process_tokens([t])
return False
def process_else_tokens(s, t):
for t in s[5]:
if t[0] in tokens or t[0] in functions:
#print(s[4][1])
process_else = False
process_tokens([t])
def process_tokens(token):
''' The magic sauce. The main loop. The head of the heads. You get the idea... maybe...? '''
#print(token)
for s in token:
f = s[0]
#print(f)
if f in tokens:
if str(f) == "ADD":
total = ADD(s[1])
#print(total)
#print(s[-1])
r_vars[s[-1]]= total
if f == "SUBTRACT":
total = SUBTRACT(s[1])
r_vars[s[-1]]= total
if str(f) == "PRINT":
#print(s[1])
PRINT(s[1])
if f == "CALC":
total = CALC(s[1])
r_vars[s[-1]]= total
if f == "OPEN_FILE":
OPEN_FILE(s[1], s[2])
if f == "IF":
#print(s)
IF(s)
if f == "INPUT":
INPUT(s[1])
if f == "LOOP":
var = GET_VAR(s[1])
if var:
LOOP(s[2],var)
if f == "WRITE_FILE":
WRITE_FILE(s[1],s[2])
if f == "READ_FILE":
READ_FILE(s[1],s[2])
if f == "VAR":
VAR([1],s[2])
if f in functions:
#print("function run: ")
#print(f)
FUNC(f)