forked from alex-s168/SimpleScriptLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpret.py
628 lines (535 loc) · 27.4 KB
/
interpret.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import time
import sys
time_total_start = time.time()
blocknames = []
blocks = []
varlist = []
varlistv = []
vartemp = None
subroutine_params = []
total_delay = 0
total_cinput_delay = 0
imported_blocks = []
tempvaramount = 0
file = "test.ssl"
for i in range(tempvaramount):
varlist.append("tmp"+str(i))
varlistv.append(None)
def store(where, val,bl):
if where == "vtemp":
vartemp = parsearg(val,bl)
elif where[0] == "v":
if where[1:] in varlist:
varlistv[varlist.index(where[1:])] = parsearg(val,bl)
else:
print("ERROR: Variable " + where[1:] + " needs to be declared before it can be used!",bl)
sys.exit(-1)
elif where == "_pass":
pass
else:
print("ERROR: No storage named ",where , "!",bl)
sys.exit(-1)
def storeb(where, val,bl):
if where == "vtemp":
vartemp = val
elif where[0] == "v":
if where[1:] in varlist:
varlistv[varlist.index(where[1:])] = val
else:
print("ERROR: Variable " + where[1:] + " needs to be declared before it can be used!",bl)
sys.exit(-1)
elif where == "_pass":
pass
else:
print("ERROR: No storage named ",where , "!",bl)
sys.exit(-1)
def arrize(r,bl):
x = r.split(",")
n = []
for i in x:
n.append(parsearg(i))
return n
def arrizeb(r,bl):
x = r.split(",")
n = []
for i in x:
n.append(i)
return n
def parsearg(arg,bl):
global varlist
global varlistv
b = arg[0]
r = arg[1:]
if arg == "_space":
return " "
if arg == "vtemp":
return vartemp
if b == "v":
if r in varlist:
return varlistv[varlist.index(r)]
else:
print("ERROR: Variable " + r + " not declared!",bl)
sys.exit(-1)
if b == "s":
return str(r)
if b == "i":
return int(r)
if b == "f":
return float(r)
if b == "b":
return bool(r)
if b == "a":
return arrize(r)
print("ERROR: Datatype " + b + " does not exist!",bl)
sys.exit(-1)
def getindexname(arr2, search):
x = 0
o = None
for i in arr2:
if i == search:
o = x
break
x += 1
return o
def blockconvert(file):
bl = []
bn = []
bl.append([])
bn.append("__main__")
print("\nBLOCKIFIER: Starting block conversion of file: "+file)
with open(file) as file_in:
curr_block = "__main__"
for line in file_in:
if line.startswith(":"):
xc = ((line+"#").split("#")[0]).rstrip().lstrip()
bl.append([])
bn.append(str(xc[1:]))
curr_block = xc[1:]
else:
xc = (line+"#").split("#")[0]
bl[getindexname(bn, curr_block)].append(xc.split(" "))
print("BLOCKIFIER: Block conversion finished!\nBLOCKIFIER: Added",len(bl),"blocks!\n")
return [bl, bn]
time_dec_start = time.time()
t = blockconvert(file)
blocks = t[0]
blocknames = t[1]
time_dec_end = time.time()
def insext(list: list, list2: list, pos: int):
l = list
c = pos
for i in list2:
l.insert(c, i)
c+= 1
return l
def getblock(name):
global blocknames
global blocks
try:
return blocks[getindexname(blocknames, str(name))]
except:
print("ERROR: Block",name, "not found!")
sys.exit(-1)
def decodeblock(name):
global total_cinput_delay
global subroutine_params
global varlist
global varlistv
global total_delay
global tempvaramount
global blocks
global imported_blocks
contdec = True
line = 0
while contdec:
# todo!
if False:
pass
else:
com = getblock(name)[line]
bl = "File: "+file+" Block: "+str(name)+" Line: "+str(line+1)+" Command: "+com[0].rstrip()
x = 0
for i in com:
com[x] = i.rstrip()
x += 1
match com[0]:
# uncomment later when block naming system is finished
#case "lcfb": # load code file blocks lcfb [filename] [insert begining] [?from] ?[to]
# if len(com)-1 > 4 or len(com)-1 < 2:
# print("ERROR: missing arguments / too much arguments!",bl)
# sys.exit(-1)
# a_filename = parsearg(com[1],bl)
# a_insertbeginning = parsearg(com[2],bl)
#
# try:
# if any(c.isalpha() for c in com[3]):
# a_from = parsearg(com[3],bl)
# a_to = parsearg(com[4],bl)
#
# imported_blocks = insext(imported_blocks, blockconvert(a_filename)[a_from:a_to], a_insertbeginning)
#
# except: pass
#
# else:
# imported_blocks = insext(imported_blocks, blockconvert(a_filename), a_insertbeginning)
case "var": # creates a variable var [name] ?[value]
if len(com)-1 > 2 or len(com)-1 < 1:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
varlist.append(com[1])
varlistv.append("")
try:
if any(c.isalpha() for c in com[2]):
store("v"+com[1], com[2], bl)
except:
pass
case "sto": # stores something sto [what] [where]
if not len(com)-1 == 2:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
store(com[2], com[1],bl)
case "log": # log to console log [val]
if not len(com)-1 == 1:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
print("LOG: ", parsearg(com[1],bl))
case "end": # ends the script end
if not len(com)-1 < 1:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
contdec = False
return
case "jmp": # jumps to a code block and ends decoding of this block jmp [block]
if not len(com)-1 == 1:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
contdec = False
decodeblock(com[1])
case "jsr": # jumps to a codeblock but continues decoding after the codeblock is finished and stores the ret value jsr [block] ?[out]
if not len(com)-1 <= 1:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
t = decodeblock(com[1])
try:
if any(c.isalpha() for c in com[2]):
store(com[2], t,bl)
else:
store("vtemp", t,bl)
except:
store("vtemp", t,bl)
case "jvsr": # like jsr but also sends args to subroutine jvsr [block] [out] [val1] ?[val2] ?[val3] ....
if not len(com)-1 <= 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
t = decodeblock(com[1])
store(com[2], t,bl)
subroutine_params = []
for i in com[2:]:
if any(c.isalpha() for c in com[2]):
subroutine_params.append(parsearg(i,bl))
case "ret": # returns a value when block is called from subroutine (to the subroutine command) also stops exec of block ret ?[val]
if len(com)-1 > 1:
print("ERROR: too much arguments!",bl)
sys.exit(-1)
contdec = False
try:
if any(c.isalpha() for c in com[1]):
return com[1]
except:pass
return
case "grv": # gets subroutine values (when subroutine is called via a value-subroutine command) grv [out1] ?[out2] ?[out3] ?[out4] ....
c = 0
for i in subroutine_params:
try:
storeb(com[1+c], i,bl)
except: pass
c += 1
subroutine_params = []
case "iter": # iterates over a given list. calls a subroutine with two args: [id] [elem] iter [arr] [block]
if not len(com)-1 == 2:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
l = parsearg(com[1],bl)
c = 0
for i in l:
subroutine_params = [c, i]
decodeblock(com[2])
c += 1
case "loop": # loops X times. calls a subroutine with one arg: [iteration] loop [amount] [block]
if not len(com)-1 == 2:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
l = parsearg(com[1],bl)
c = 0
for i in range(l):
subroutine_params = [c]
decodeblock(com[2])
c += 1
case "scmb": # Combines multiple strings scmb [out] [in1: s] [in2: s] ?[in3: s] ....
if not len(com)-1 <= 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
s = ""
for i in com[2:]:
try:
s = s + parsearg(i)
except:
print("ERROR: Can only combine strings!",bl)
sys.exit(-1)
storeb(com[1], s,bl)
case "conv": # Converts a value conv [in] [type] [out]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
i = parsearg(com[1],bl)
o = None
match com[2]:
case "s":
o = str(i)
case "i":
o = int(i)
case "b":
o = bool(i)
case "a":
o = arrizeb(i,bl)
case "f":
o = float(i)
case _:
print("ERROR: Type ",com[2], " not found!",bl)
sys.exit(-1)
storeb(com[3], o,bl)
case "aapp": # Appends a value to a array aap [in] [element] [out]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
il = parsearg(com[1],bl)
ie = parsearg(com[2],bl)
nl = il
nl.append(ie)
storeb(com[3], nl,bl)
case "spaceify": # replaces all X with spaces of an string spaceify [in] [what] [out]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
o = parsearg(com[1],bl).replace(parsearg(com[2],bl), " ")
storeb(com[3], o, bl)
case "sarrify": # converst string to a char-array sarrify [in] [out]
if not len(com)-1 == 2:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
storeb(com[2], [*parsearg(com[1],bl)], bl)
case "aget": # gets a value of an array aget [in] [pos] [out]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
il = parsearg(com[1],bl)
ip = parsearg(com[2],bl)
o = il[ip]
storeb(com[3], o,bl)
case "aset": # sets a value of an array aset [in] [pos] [val] [out]
if not len(com)-1 == 4:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
il = parsearg(com[1],bl)
ip = parsearg(com[2],bl)
iv = parsearg(com[3],bl)
il[ip] = iv
storeb(com[4], il,bl)
case "alen": # gets the length of an array alen [in] [out]
if not len(com)-1 == 2:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
il = parsearg(com[1],bl)
o = len(il)
storeb(com[2], o,bl)
case "aext": # extends a array with another array aext [in] [in2] [out]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
il = parsearg(com[1],bl)
ie = parsearg(com[2],bl)
nl = il
nl.extend(ie)
storeb(com[3], nl,bl)
case "asplit": # splits a string and returns a array asplit [in] [at] [out]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
i1 = parsearg(com[1],bl)
i2 = parsearg(com[2],bl)
nl = i1.split(i2)
storeb(com[3], nl,bl)
case "clv": # Clears a value (sets to none) clv [out]
if not len(com)-1 == 1:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
storeb(com[1], None,bl)
case "add": # do simple math stuff (add) add [in1] [in2] [out]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
i1 = parsearg(com[1],bl)
i2 = parsearg(com[2],bl)
o = i1 + i2
storeb(com[3], o,bl)
case "sub": # do simple math stuff (sub) sub [in1] [in2] [out]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
i1 = parsearg(com[1],bl)
i2 = parsearg(com[2],bl)
o = i1 - i2
storeb(com[3], o,bl)
case "mul": # do simple math stuff (mul) mul [in1] [in2] [out]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
i1 = parsearg(com[1],bl)
i2 = parsearg(com[2],bl)
o = i1 * i2
storeb(com[3], o,bl)
case "div": # do simple math stuff (div) div [in1] [in2] [out]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
i1 = parsearg(com[1],bl)
i2 = parsearg(com[2],bl)
o = i1 / i2
storeb(com[3], o,bl)
case "st": # set type of variable st [out] [type]
if not len(com)-1 == 2:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
o = None
match com[2]:
case "s":
o = ""
case "i":
o = 0
case "b":
o = False
case "a":
o = []
case "f":
o = 0.0
case _:
print("ERROR: Type ",com[2], " not found!",bl)
sys.exit(-1)
storeb(com[1], o,bl)
case "delay": # waits for X seconds, then continues delay [ms]
if not len(com)-1 == 1:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
total_delay += parsearg(com[1],bl)/1000
time.sleep(parsearg(com[1],bl)/1000)
case "gentemp": # generates temp variables gentemp [amount] ?[type]
if not len(com)-1 <= 2:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
if any(c.isalpha() for c in com[2]):
o = None
match com[2]:
case "s":
o = ""
case "i":
o = 0
case "b":
o = False
case "a":
o = []
case "f":
o = 0.0
case _:
print("ERROR: Type ",com[2], " not found!",bl)
sys.exit(-1)
for i in range(parsearg(com[1],bl )):
varlist.append("tmp"+str(i+tempvaramount))
varlistv.append(None)
storeb("vtmp"+str(i+tempvaramount), o,bl)
tempvaramount += parsearg(com[1],bl)
else:
for i in range(parsearg(com[1],bl)):
varlist.append("tmp"+str(i+tempvaramount))
varlistv.append(None)
case "repl": # replaces a element (string) in a string repl [what] [with] [string] [out]
if not len(com)-1 == 4:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
s = parsearg(com[3],bl)
o = s.replace(parsearg(com[1],bl), parsearg(com[2],bl))
storeb(com[4], o,bl)
case "eq": # Jumps to a block if both inputs are equal eq [block] [in1] [in2]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
if parsearg(com[2],bl) == parsearg(com[3],bl):
contdec = False
decodeblock(com[1])
case "neq": # Jumps to a block if both inputs are not equal neq [block] [in1] [in2]
if not len(com)-1 == 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
if not parsearg(com[2],bl) == parsearg(com[3],bl):
contdec = False
decodeblock(com[1])
case "seq": # Jumps to a subroutine if both inputs are equal seq [block] [in1] [in2] ?[out]
if not len(com)-1 <= 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
if parsearg(com[2],bl) == parsearg(com[3],bl):
t = decodeblock(com[1])
try:
if any(c.isalpha() for c in com[4]):
store(com[4], t,bl)
else:
store("vtemp", t,bl)
except:
store("vtemp", t,bl)
case "sneq": # Jumps to a subroutine if both inputs are not equal sneq [block] [in1] [in2] ?[out]
if not len(com)-1 <= 3:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
if not parsearg(com[2],bl) == parsearg(com[3],bl):
t = decodeblock(com[1])
try:
if any(c.isalpha() for c in com[4]):
store(com[4], t,bl)
else:
store("vtemp", t,bl)
except:
store("vtemp", t,bl)
case "cinput": # gets input from console cinput [out]
if not len(com)-1 == 1:
print("ERROR: missing arguments / too much arguments!",bl)
sys.exit(-1)
x = time.time()
storeb(com[1],input(),bl)
total_cinput_delay += time.time() - x
case _:
if any(c.isalpha() for c in com[0]):
print("ERROR: command not found!",bl)
sys.exit(-1)
line += 1
time_run_start = time.time()
print(blocknames)
decodeblock("__main__")
time_run_end = time.time()
time_total_end = time.time()
import os, psutil; used_mem = psutil.Process(os.getpid()).memory_info().rss / 1024 ** 2 * 1.049
print("\nProgramm Finished!\n")
print("dev data:")
print("runtime: PYTHON-3-STANDARD-INTERPRETER")
print("version: alpha.0.10 (12.01.2023)")
print("total variables: ",len(varlist))
print("- user variables: ",len(varlist)-tempvaramount)
print("- temp variables: ",tempvaramount)
print("total blocks: ",len(blocks)+len(imported_blocks))
print("- local blocks: ",len(blocks))
print("- imported blocks: ",len(imported_blocks))
print("total time: ",(time_total_end-time_total_start)* 1000," ms")
print("- decode time: ",(time_dec_end-time_dec_start)* 1000," ms")
print("- run time: ",(time_run_end-time_run_start)* 1000," ms")
print("- - run time (no delay): ",(time_run_end-time_run_start-total_delay)* 1000," ms (might be unaccurate when delay is used)")
print("- - run time (no delay + no cinput): ",(time_run_end-time_run_start-total_delay-total_cinput_delay)* 1000," ms (might be unaccurate when cinput or delay is used)")
print("used memory: ",used_mem," MB")
print("- runtime: ",14," MB (inaccurate)")
print("- interpreter: ",used_mem-14," MB (inaccurate)")