This repository was archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVisitor.py
More file actions
1121 lines (911 loc) · 50.6 KB
/
Visitor.py
File metadata and controls
1121 lines (911 loc) · 50.6 KB
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from re import findall
from math import sin, cos, tan
from colorama import init, Style, Fore
from MineScriptVisitor import MineScriptVisitor
from MineScriptParser import MineScriptParser
import tags
approved_attrs = [
"append",
"pop",
"remove",
"sort"
]
error = f"{Fore.RED}Traceback:\nFile %s, line %i\n%s\n"
end = f"{Style.RESET_ALL}"
typeerror1 = "TypeError: Variable '%s' should be of type int or float, not %s"
typeerror2 = "TypeError: Object of type %s has no length"
typeerror3 = "TypeError: Indices should be of type int, not %s"
typeerror4 = "TypeError: Object of type %s is not callable"
typeerror5 = "TypeError: Unsupported operand types for %s: '%s' and '%s'"
typeerror6 = "TypeError: '%s' not supported between instances of '%s' and '%s'"
typeerror7 = "TypeError: float() argument must be a string or a number, not '%s'"
typeerror8 = "TypeError: Function argument must be a string or a number, not '%s'"
typeerror9 = "TypeError: Invalid type"
typeerror10 = "TypeError: Function %s() requires %s arguments, but %s were given"
nameerror = "NameError: Variable '%s' is not defined"
syntaxerror1 = "SyntaxError: invalid syntax"
syntaxerror2 = "SyntaxError: 'return' outside of function"
indexerror = "IndexError: List index %i is out of range"
attributeerror = "AttributeError: Object of type %s has no attribute %s"
valueerror1 = "ValueError: Could not convert to float: '%s'"
valueerror2 = "ValueError: Could not convert to int: '%s'"
valueerror3 = "ValueError: Selector not formatted correctly"
def add_to_selector(selector, args):
if len(selector) < 3:
selector = selector + "[]"
tp = selector[:2]
attributes = findall("\[([^]]+)\]", selector)
if len(attributes): attributes = attributes[0].split(",")
attributes = [attr.strip() for attr in attributes]
for arg in args:
if arg not in attributes:
attributes.append(arg)
return tp + "[" + ','.join(attributes) + "]"
class Visitor(MineScriptVisitor):
def __init__(self, name, code, file):
self.code = code # The actual code
self.file = file # Filename
self.datapack_name = name # Name of the datapack
self._commands = [] # List of commands to be added to the current function
self.warnings = [] # List of warnings
self.memory = {} # Stores variables
self.localmemory = {} # Stores local variables
self.functionargs = {} # Stores the args a function takes
self.func = None # Current function
self.r_value = None # Stores the return variable of the current function
self.igmemory = [] # Stores the in-game variable names
self.igfunctionargs = {} # Stores the args an igfunction takes
self.igfunctionreturn = {} # Stores the name of the return variable of a function
self.igfunctions = {} # Stores the functions to be turned into .mcfunction files
self.igfunc = None # Current igfunction
self.vars = [] # Stores all the temporary variables currently in use
self.nreusable = [] # Stores all non reusable variables
self.igloops = {} # Stores the loops to be turned into .mcfunction files
self.loop = [] # Keeps track of loops
self.prefixes = [] # Keeps track of if/else and execute statements
self.loops = 0 # Loop ID
self.tag = 0 # Tag ID
self.get_tags() # Get all tags from file
self.memory["dpname"] = name
def add_var(self, name): # Add a new in-game variable
if name not in self.igmemory:
self.igmemory.append(name)
def mark_unused(self, var): # Mark an in-game variable as unused
name = int(var[4:])
if var not in self.nreusable and name in self.vars:
self.vars.remove(name)
def mark_not_reusable(self, var): # Mark an in-game variable as not reusable
if var not in self.nreusable:
self.nreusable.append(var)
def mark_reusable(self, var): # Mark an in-game variable as reusable
if var in self.nreusable:
self.nreusable.remove(var)
def get_var(self): # Generate and return a new temporary variable
n = self.get_next_var_id()
name = f"_var{n}"
self.add_var(name)
self.vars.append(n)
return name
def get_next_var_id(self): # Get the id of the next available variable
if len(self.vars):
for i in range(len(self.vars)):
if self.vars[i] != i:
self.vars.insert(i, i)
return i
return len(self.vars)
return 0
def add_func_arg(self, func, var, arg):
self.igfunctionargs[func][0].append(var)
self.igfunctionargs[func][1].append(arg)
def add_warning(self, warning): # Add a warning
if warning not in self.warnings:
self.warnings.append(warning)
def get_tags(self): # Get and load all tags
for tag in tags.tags:
self.memory[tag] = tags.tags[tag]
def add_cmd(self, command, func=None): # Add a command to the current function
if self.prefixes != []:
command = "execute " + ' '.join(self.prefixes) + " run " + command
if self.loop != []:
self.igloops[self.loop[-1]].append(command)
elif func:
self.igfunctions[func].append(command)
else:
self._commands.append(command)
def add_loop(self, tp): # Add a loop
self.loops += 1
self.loop.append(f"_{tp}{self.loops}")
self.igloops[self.loop[-1]] = []
def pop_loop(self): # Pop latest loop
self.loop.pop()
def get_var_name(self, name):
if self.igfunc:
if name in self.igfunctionargs[self.igfunc][1]:
index = self.igfunctionargs[self.igfunc][1].index(name)
return self.igfunctionargs[self.igfunc][0][index]
else:
return name
else:
return name
def add_prefix(self, cmd): # Add prefix (if/else/execute statement)
self.prefixes.append(cmd)
def pop_prefix(self): # Pop latest prefix
self.prefixes.pop()
def set_var(self, name, value): # Set an in-game variable
self.add_cmd(f"scoreboard players set MineScript {name} {value}")
def igBoolOp(self, operation, left, right):
unused = []
if left[1] == "rt":
l = self.get_var()
unused.append(l)
self.set_var(l, int(left[0]))
elif left[1] == "ig": l = left[0]
if right[1] == "rt":
r = self.get_var()
unused.append(r)
self.set_var(r, int(right[0]))
elif right[1] == "ig": r = right[0]
result = self.get_var()
self.set_var(result, 0)
if operation == "&&":
self.add_cmd(f"execute if score MineScript {l} matches 1.. if score MineScript {r} matches 1.. run scoreboard players set MineScript {result} 1")
elif operation == "||":
self.add_cmd(f"execute if score MineScript {l} matches 1.. run scoreboard players set MineScript {result} 1")
self.add_cmd(f"execute if score MineScript {r} matches 1.. run scoreboard players set MineScript {result} 1")
for var in unused:
self.mark_unused(var)
return result
def igComparison(self, comparison, left, right): # genexpr (< > <= >= == !=) genexpr
unused = []
if left[1] == "rt":
l = self.get_var()
unused.append(l)
self.set_var(l, int(left[0]))
elif left[1] == "ig": l = left[0]
if right[1] == "rt":
r = self.get_var()
unused.append(r)
self.set_var(r, int(right[0]))
elif right[1] == "ig": r = right[0]
result = self.get_var()
if comparison == "==": comparison = "="
if comparison == "!=":
self.set_var(result, 0)
self.add_cmd(f"execute unless score MineScript {l} = MineScript {r} run scoreboard players set MineScript {result} 1")
else:
self.set_var(result, 0)
self.add_cmd(f"execute if score MineScript {l} {comparison} MineScript {r} run scoreboard players set MineScript {result} 1")
for var in unused:
self.mark_unused(var)
return result
def igOperation(self, operation, left, right, result=None): # genexpr (+-/*%) genexpr
unused = []
if operation == "^":
if result is None: result = self.get_var()
if right[1] == "rt" and left[1] == "ig":
if right[0] == 0: self.set_var(result, 1)
else:
self.add_cmd(f"scoreboard players operation MineScript {result} = MineScript {left[0]}")
for _ in range(right[0]-1):
self.add_cmd(f"scoreboard players operation MineScript {result} *= MineScript {left[0]}")
elif right[1] == "ig":
r = right[0]
if left[1] == "rt":
l = self.get_var()
unused.append(l)
self.add_cmd(f"scoreboard players set MineScript {l} {int(left[0])}")
elif left[1] == "ig": l = left[0]
self.add_cmd(f"scoreboard players operation MineScript {result} = MineScript {l}")
count = self.get_var()
negative = self.get_var()
unused.append(count)
unused.append(negative)
self.set_var(negative, -1)
self.add_cmd(f"scoreboard players operation MineScript {count} = MineScript {r}")
self.add_cmd(f"execute if score MineScript {count} matches ..-1 run scoreboard players operation MineScript {count} *= MineScript {negative}")
self.add_cmd(f"scoreboard players remove MineScript {count} 1")
loop = "_pow" + str(self.loops+1)
self.add_cmd(f"execute if score MineScript {count} matches 1.. run function {self.datapack_name}:{loop}")
self.add_loop("pow")
self.add_cmd(f"scoreboard players operation MineScript {result} *= MineScript {l}")
self.add_cmd(f"scoreboard players remove MineScript {count} 1")
self.add_cmd(f"execute if score MineScript {count} matches 1.. run function {self.datapack_name}:{loop}")
self.pop_loop()
self.add_cmd(f"execute if score MineScript {r} matches 0 run scoreboard players set MineScript {result} 1")
unit = self.get_var()
unused.append(unit)
self.set_var(unit, 1)
self.add_cmd(f"execute if score MineScript {r} matches ..-1 run scoreboard players operation MineScript {unit} /= MineScript {result}")
self.add_cmd(f"execute if score MineScript {r} matches ..-1 run scoreboard players operation MineScript {result} = MineScript {unit}")
else:
if left[1] == "rt":
l = self.get_var()
unused.append(l)
self.set_var(l, int(left[0]))
elif left[1] == "ig": l = left[0]
if right[1] == "rt":
r = self.get_var()
unused.append(r)
self.set_var(r, int(right[0]))
elif right[1] == "ig": r = right[0]
if result is None: result = self.get_var()
self.add_cmd(f"scoreboard players operation MineScript {result} = MineScript {l}")
self.add_cmd(f"scoreboard players operation MineScript {result} {operation}= MineScript {r}")
for var in unused:
self.mark_unused(var)
return result
def visitIgAssign(self, ctx): # Expression of type $var = expression
name = ctx.ID().getText()
value = self.visitChildren(ctx)
if type(value) == int or type(value) == float:
self.add_var(name)
self.add_cmd(f"scoreboard players set MineScript {name} {round(value)}")
else:
print((error+typeerror+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, value.__class__.__name__))
raise Exception("Abort")
return name
def visitIgAssignIg(self, ctx): # Expression of type $var = $expression
name1 = self.get_var_name(ctx.ID().getText())
name2 = self.get_var_name(self.visitChildren(ctx))
if not name1.startswith("_") and name1 not in self.igmemory:
self.add_warning(f"{Fore.YELLOW}Warning:\nFile {self.file}, line {ctx.start.line}\nVariable '{name1}' referenced without assignement{Style.RESET_ALL}")
if not name2.startswith("_") and name2 not in self.igmemory:
self.add_warning(f"{Fore.YELLOW}Warning:\nFile {self.file}, line {ctx.start.line}\nVariable '{name2}' referenced without assignement{Style.RESET_ALL}")
self.add_var(name1)
self.add_cmd(f"scoreboard players operation MineScript {name1} = MineScript {name2}")
if name2.startswith("_"): self.mark_unused(name2)
return name1
def visitIgAssignUnit(self, ctx): # Expression of type $var++
name = self.get_var_name(ctx.ID().getText())
if not name.startswith("_") and name not in self.igmemory:
self.add_warning(f"{Fore.YELLOW}Warning:\nFile {self.file}, line {ctx.start.line}\nVariable '{name}' referenced without assignement{Style.RESET_ALL}")
if ctx.op.type == MineScriptParser.USUM: self.add_cmd(f"scoreboard players add MineScript {name} 1")
elif ctx.op.type == MineScriptParser.USUB: self.add_cmd(f"scoreboard players remove MineScript {name} 1")
return name
def visitIgAssignOp(self, ctx): # Expression of type $var (*/+-%)= expression
name = self.get_var_name(ctx.ID().getText())
value = self.visitChildren(ctx)
if not name.startswith("_") and name not in self.igmemory:
self.add_warning(f"{Fore.YELLOW}Warning:\nFile {self.file}, line {ctx.start.line}\nVariable '{name}' referenced without assignement{Style.RESET_ALL}")
if not(type(value) == int or type(value) == float):
print((error+typeerror5+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), ctx.op.text, "int", value.__class__.__name__))
raise Exception("Abort")
self.igOperation(ctx.op.text[0], (name, "ig"), (value, "rt"), name)
return name
def visitIgAssignIgOp(self, ctx): # Expression of type $var (*/+-%)= $expression
name1 = self.get_var_name(ctx.ID().getText())
name2 = self.get_var_name(self.visitChildren(ctx))
if not name1.startswith("_") and name1 not in self.igmemory:
self.add_warning(f"{Fore.YELLOW}Warning:\nFile {self.file}, line {ctx.start.line}\nVariable '{name1}' referenced without assignement{Style.RESET_ALL}")
if not name2.startswith("_") and name2 not in self.igmemory:
self.add_warning(f"{Fore.YELLOW}Warning:\nFile {self.file}, line {ctx.start.line}\nVariable '{name2}' referenced without assignement{Style.RESET_ALL}")
self.igOperation(ctx.op.text[0], (name1, "ig"), (name2, "ig"), name1)
if name2.startswith("_"): self.mark_unused(name2)
def visitIgParens(self, ctx): # Expression of type ( $expression )
return self.visit(ctx.igexpr())
def visitIgOpIg(self, ctx): # Expression of type $expression (*/+-%^) $expression
name1 = self.get_var_name(self.visit(ctx.igexpr(0)))
name2 = self.get_var_name(self.visit(ctx.igexpr(1)))
if not name1.startswith("_") and name1 not in self.igmemory:
self.add_warning(f"{Fore.YELLOW}Warning:\nFile {self.file}, line {ctx.start.line}\nVariable '{name1}' referenced without assignement{Style.RESET_ALL}")
if not name2.startswith("_") and name2 not in self.igmemory:
self.add_warning(f"{Fore.YELLOW}Warning:\nFile {self.file}, line {ctx.start.line}\nVariable '{name2}' referenced without assignement{Style.RESET_ALL}")
result = self.igOperation(ctx.op.text, (name1, "ig"), (name2, "ig"))
if name1.startswith("_"): self.mark_unused(name1)
if name2.startswith("_"): self.mark_unused(name2)
return result
def visitIgOp(self, ctx, reverse=False): # Expression of type $expression (*/+-%^) expression
name = self.get_var_name(self.visit(ctx.igexpr()))
value = self.visit(ctx.expr())
if not(type(value) == int or type(value) == float):
print((error+typeerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, value.__class__.__name__))
raise Exception("Abort")
if not reverse: result = self.igOperation(ctx.op.text, (name, "ig"), (value, "rt"))
else: result = self.igOperation(ctx.op.text, (value, "rt"), (name, "ig"))
if name.startswith("_"): self.mark_unused(name)
return result
def visitIgOpM(self, ctx): # Expression of type expression (*/+-%^) $var
return self.visitIgOp(ctx, True)
def visitIgNot(self, ctx): # Expression of type !$expression
name = self.get_var_name(self.visit(ctx.igexpr()))
result = self.get_var()
self.set_var(result, 0)
self.add_cmd(f"execute unless score MineScript {name} matches 1.. run scoreboard players set MineScript {result} 1")
if name.startswith("_"): self.mark_unused(name)
return result
def visitIgBoolOp(self, ctx): # Expression of type genexpr &&/|| genexpr
if len(ctx.igexpr()) == 1:
expr1 = self.visit(ctx.igexpr(0)), "ig"
expr2 = self.visit(ctx.expr()), "rt"
else:
expr1 = self.visit(ctx.igexpr(0)), "ig"
expr2 = self.visit(ctx.igexpr(1)), "ig"
result = self.igBoolOp(ctx.op.text, expr1, expr2)
if expr1[0].startswith("_"): self.mark_unused(expr1[0])
if len(ctx.igexpr()) > 1 and expr2[0].startswith("_"): self.mark_unused(expr2[0])
return result
def visitIgComparison(self, ctx, reverse=False): # Expression of type $expression (> < <= >= != ==) expression
name = self.get_var_name(self.visit(ctx.igexpr()))
value = self.visit(ctx.expr())
if not(type(value) == int or type(value) == float):
print((error+typeerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, value.__class__.__name__))
raise Exception("Abort")
if not reverse:
result = self.igComparison(ctx.op.text, (name, "ig"), (value, "rt"))
else:
result = self.igComparison(ctx.op.text, (value, "rt"), (name, "ig"))
if name.startswith("_"): self.mark_unused(name)
return result
def visitIgComparisonM(self, ctx): # Expression of type expression (> < <= >= != ==) $expression
return self.visitIgComparison(ctx, reverse=True)
def visitExecute(self, ctx): # Expression of type $execute(execute){ stat }
execute = str(self.visit(ctx.expr()))
stat = ctx.stat()
self.add_prefix(execute)
self.visit(stat)
self.pop_prefix()
def visitIgComparisonIg(self, ctx): # Expression of type $expression (> < <= >= != ==) $expression
name1 = self.get_var_name(self.visit(ctx.igexpr(0)))
name2 = self.get_var_name(self.visit(ctx.igexpr(1)))
if not name1.startswith("_") and name1 not in self.igmemory:
self.add_warning(f"{Fore.YELLOW}Warning:\nFile {self.file}, line {ctx.start.line}\nVariable '{name1}' referenced without assignement{Style.RESET_ALL}")
if not name2.startswith("_") and name2 not in self.igmemory:
self.add_warning(f"{Fore.YELLOW}Warning:\nFile {self.file}, line {ctx.start.line}\nVariable '{name2}' referenced without assignement{Style.RESET_ALL}")
result = self.igComparison(ctx.op.text, (name1, "ig"), (name2, "ig"))
if name1.startswith("_"): self.mark_unused(name1)
if name2.startswith("_"): self.mark_unused(name2)
return result
def visitIgIfElse(self, ctx): # Expression of type $if ($expression) { stat } ($else { stat })?
name = self.get_var_name(self.visit(ctx.igexpr()))
self.add_prefix(f"if score MineScript {name} matches 1..")
self.visit(ctx.stat(0))
self.pop_prefix()
if ctx.stat(1) is not None:
self.add_prefix(f"unless score MineScript {name} matches 1..")
self.visit(ctx.stat(1))
self.pop_prefix()
if name.startswith("_"): self.mark_unused(name)
def visitGetPos(self, ctx): # Expression of type $pos(selector, index)
selector = str(self.visit(ctx.expr(0)))
if selector not in ["@s", "@r", "@p"]:
selector = add_to_selector(selector, ["limit=1"])
coord = self.visit(ctx.expr(1))
if not type(coord) == int:
print((error+typeerror3+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), coord.__class__.__name__))
raise Exception("Abort")
if ctx.expr(2) is not None:
scale = self.visit(ctx.expr(2))
else:
scale = None
if scale is not None and not type(scale) == int and not type(scale) == float:
print((error+typeerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), scale.__class__.__name__))
raise Exception("Abort")
result = self.get_var()
self.set_var(result, 0)
if scale is not None: scale = f" {scale}"
else: scale = ""
if coord == 0: self.add_cmd(f"execute store result score MineScript {result} run data get entity {selector} Pos[0]" + scale)
elif coord == 1: self.add_cmd(f"execute store result score MineScript {result} run data get entity {selector} Pos[1]" + scale)
elif coord == 2: self.add_cmd(f"execute store result score MineScript {result} run data get entity {selector} Pos[2]" + scale)
return result
def visitGetData(self, ctx): # Expression of type $getdata(selector, path, scale?)
selector = str(self.visit(ctx.expr(0)))
path = str(self.visit(ctx.expr(1)))
result = self.get_var()
self.set_var(result, 0)
if ctx.expr(2) is not None:
scale = self.visit(ctx.expr(2))
if type(scale) == int or type(scale) == float:
value = int(scale)
else:
print((error+s+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1]))
raise Exception("Abort")
self.add_cmd(f"execute store result score MineScript {result} run data get entity {selector} {path} {scale}")
else:
self.add_cmd(f"execute store result score MineScript {result} run data get entity {selector} {path}")
return result
def visitSetData(self, ctx):
selector = str(self.visit(ctx.expr(0)))
if selector not in ["@s", "@r", "@p"]:
selector = add_to_selector(selector, ["limit=1"])
path = str(self.visit(ctx.expr(1)))
if ctx.genexpr().expr() is not None:
value = self.visit(ctx.genexpr().expr())
self.add_cmd(f"data modify entity {selector} {path} set value {value}")
else:
name = self.get_var_name(self.visit(ctx.genexpr().igexpr()))
self.add_cmd(f"execute store result entity {selector} {path} double 1 run scoreboard players get MineScript {name}")
if name.startswith("_"): self.mark_unused(name)
def visitIsBlock(self, ctx): # Expression of type $isblock(selector, pos, block)
selector = str(self.visit(ctx.expr(0)))
relpos = str(self.visit(ctx.expr(1)))
block = str(self.visit(ctx.expr(2)))
result = self.get_var()
self.set_var(result, 0)
self.add_cmd(f"execute at {selector} if block {relpos} {block} run scoreboard players set MineScript {result} 1")
return result
def visitAddObj(self, ctx):
name = str(self.visit(ctx.expr(0)))
tp = str(self.visit(ctx.expr(1)))
self.add_cmd(f"scoreboard objectives add {name} {tp}")
def visitRename(self, ctx):
selector = str(self.visit(ctx.expr(0)))
name = str(self.visit(ctx.expr(1))).replace("\"", "\\\"")
print(f"data modify entity {selector} CustomName set value \"{name}\"")
self.add_cmd(f"data modify entity {selector} CustomName set value \"{name}\"")
def visitEnableTrigger(self, ctx):
selector = str(self.visit(ctx.expr(0)))
name = str(self.visit(ctx.expr(1)))
self.add_cmd(f"scoreboard players enable {selector} {name}")
def visitGetScore(self, ctx): # Expression of type $getscore(selector, name)
selector = str(self.visit(ctx.expr(0)))
name = str(self.visit(ctx.expr(1)))
result = self.get_var()
self.add_cmd(f"scoreboard players operation MineScript {result} = {selector} {name}")
return result
def visitSetScore(self, ctx): # Expression of type $setscore(selector, name, value)
selector = str(self.visit(ctx.expr(0)))
name = str(self.visit(ctx.expr(1)))
ge = ctx.genexpr()
if ge.expr() is not None:
value = self.visit(ge.expr())
if not type(value) == int:
print((error+typeerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, value.__class__.__name__))
raise Exception("Abort")
v = self.get_var()
self.add_cmd(f"scoreboard players set MineScript {v} {value}")
self.add_cmd(f"scoreboard players operation {selector} {name} = MineScript {v}")
elif ge.igexpr() is not None:
name2 = self.get_var_name(self.visit(ge.igexpr()))
self.add_cmd(f"scoreboard players operation {selector} {name} = MineScript {name2}")
if name2.startswith("_"): self.mark_unused(name2)
def visitAddTag(self, ctx): # Expression of type $addtag(selector, tag)
selector = str(self.visit(ctx.expr(0)))
name = str(self.visit(ctx.expr(1)))
self.add_cmd(f"tag {selector} add {name}")
def visitRemTag(self, ctx): # Expression of type $remtag(selector, tag)
selector = str(self.visit(ctx.expr(0)))
name = str(self.visit(ctx.expr(1)))
self.add_cmd(f"tag {selector} remove {name}")
def visitHasTag(self, ctx): # Expression of type $hastag(selector, tag)
selector = str(self.visit(ctx.expr(0)))
name = str(self.visit(ctx.expr(1)))
result = self.get_var()
self.set_var(result, 0)
self.add_cmd(f"scoreboard players set MineScript {result} 0")
self.add_cmd(f"execute as {selector} at @s[tag={name}] run scoreboard players set MineScript {result} 1")
return result
def visitCount(self, ctx): # Expression of type $count(selector)
selector = str(self.visit(ctx.expr()))
if not selector.startswith("@"):
print((error+valueerror3+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip()))
raise Exception("Abort")
result = self.get_var()
self.set_var(result, 0)
self.add_cmd(f"execute as {selector} run scoreboard players add MineScript {result} 1")
return result
def visitIgWhile(self, ctx): # Expression of type $while (genexpr) { stat }
stats = ctx.stat()
expr = ctx.genexpr()
if expr.expr() is not None:
value = self.visit(expr.expr())
if type(value) == int or type(value) == float or type(value) == bool:
value = int(value)
else:
print((error+s+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1]))
raise Exception("Abort")
v = self.get_var()
self.add_cmd(f"scoreboard players set MineScript {v} {value}")
expr_n = self.visit(expr.expr())
loop = "_while" + str(self.loops+1)
self.add_cmd(f"execute if score MineScript {expr_n} matches 1.. run function {self.datapack_name}:{loop}")
self.add_loop("while")
self.visit(stats)
elif expr.igexpr() is not None:
expr_n = self.get_var_name(self.visit(expr.igexpr()))
loop = "_while" + str(self.loops+1)
self.add_cmd(f"execute if score MineScript {expr_n} matches 1.. run function {self.datapack_name}:{loop}")
self.add_loop("while")
self.visit(stats)
expr_n = self.visit(expr.igexpr())
self.add_cmd(f"execute if score MineScript {expr_n} matches 1.. run function {self.datapack_name}:{loop}")
self.pop_loop()
if expr.igexpr() is not None and expr_n.startswith("_"): self.mark_unused(expr_n)
def visitIgFor(self, ctx): # Expression of type $for ($forInit; $forTest; $forUpdate) { stat }
stats = ctx.stat()
init = ctx.igForControl().igForInit()
expr = ctx.igForControl().igexpr()
update = ctx.igForControl().igForUpdate()
init_n = self.visit(init)
expr_n = self.get_var_name(self.visit(expr))
loop = "_for" + str(self.loops+1)
self.add_cmd(f"execute if score MineScript {expr_n} matches 1.. run function {self.datapack_name}:{loop}")
self.add_loop("for")
self.visit(stats)
self.visit(update)
expr_n = self.visit(expr)
self.add_cmd(f"execute if score MineScript {expr_n} matches 1.. run function {self.datapack_name}:{loop}")
self.pop_loop()
if expr_n.startswith("_"): self.mark_unused(expr_n)
def visitIgForEntity(self, ctx): # Expression of type $forentity(selector; new_var) { stat }
unused = []
selector = str(self.visit(ctx.expr()))
name = ctx.ID().getText()
stats = ctx.stat()
self.tag += 1
value = add_to_selector(selector, ["limit=1", f"tag=!_tag{self.tag}"])
self.memory[name] = value
count_d = self.get_var()
unused.append(count_d)
self.add_cmd(f"scoreboard players set MineScript {count_d} 0")
self.add_cmd(f"execute as {selector} run scoreboard players add MineScript {count_d} 1")
count = self.get_var()
unused.append(count)
self.add_cmd(f"scoreboard players set MineScript {count} 0")
self.add_cmd(f"scoreboard players operation MineScript {count} = MineScript {count_d}")
loop = "_for" + str(self.loops+1)
self.add_cmd(f"execute if score MineScript {count} matches 1.. run function {self.datapack_name}:{loop}")
self.add_loop("for")
self.visit(stats)
self.add_cmd(f"tag {value} add _tag{self.tag}")
self.add_cmd(f"scoreboard players remove MineScript {count} 1")
self.add_cmd(f"execute if score MineScript {count} matches 1.. run function {self.datapack_name}:{loop}")
self.pop_loop()
self.add_cmd(f"tag {selector} remove _tag{self.tag}")
for var in unused:
self.mark_unused(var)
def visitSetDisplay(self, ctx): # Expression of type $setdisplay(var, mode)
name = self.get_var_name(self.visit(ctx.igexpr()))
mode = ctx.DSPL_MODE().getText()
self.add_cmd(f"scoreboard objectives setdisplay {mode} {name}")
if name.startswith("_"): self.mark_unused(name)
def visitCommand(self, ctx): # Expression of type $mc(expression)
exp = str(self.visit(ctx.expr()))
self.add_cmd(exp)
def visitIgPrint(self, ctx): # Expression of type $print(genexpression,...| COLOR)
text = []
for child in ctx.igPrintControl().igPrintArg():
e = child.genexpr()
if child.COLOR() is not None: color = child.COLOR().getText()
else: color = "white"
if e.expr() is not None:
value = str(self.visit(e.expr()))
text.append("{\"text\":\"%s\",\"color\":\"%s\"}"%(value, color))
elif e.igexpr() is not None:
name = self.get_var_name(self.visit(e.igexpr()))
text.append("{\"score\":{\"name\":\"MineScript\",\"objective\":\"%s\"},\"color\":\"%s\"}"%(name, color))
if name.startswith("_"): self.mark_unused(name)
self.add_cmd("tellraw @a ["+','.join(text)+"]")
def visitTeleport(self, ctx): # Expression of type $tp(selector, pos)
selector = str(self.visit(ctx.expr(0)))
pos = str(self.visit(ctx.expr(1)))
self.add_cmd(f"execute as {selector} at @s run tp @s {pos}")
def visitIgFuncDef(self, ctx): # Expression of type $function func { stat }
name = ctx.ID(0).getText()
stats = ctx.stat()
args = ctx.ID()[1:]
self.igfunctionargs[name] = [[], []]
r_var = self.get_var()
self.mark_not_reusable(r_var)
self.igfunctionreturn[name] = r_var
for arg in args:
var = self.get_var()
self.mark_not_reusable(var)
self.add_func_arg(name, var, arg.getText())
self.igfunc = name
self.visit(stats)
self.igfunc = None
self.igfunctions[name] = self._commands[:]
self._commands = []
def visitIgFuncCall(self, ctx): # Expression of type $func()
name = ctx.ID().getText()
variables = ctx.genexpr()
if not name in self.igfunctions:
print((error+nameerror+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name))
raise Exception("Abort")
if len(variables) != len(self.igfunctionargs[name][0]):
print((error+typeerror10+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, len(self.igfunctionargs[name]), len(variables)))
raise Exception("Abort")
for var in range(len(variables)):
if variables[var].expr():
self.set_var(self.igfunctionargs[name][0][var], int(self.visit(variables[var])))
else:
n = self.visit(variables[var])
if not n.startswith("_") and n not in self.igmemory:
self.add_warning(f"{Fore.YELLOW}Warning:\nFile {self.file}, line {ctx.start.line}\nVariable '{n}' referenced without assignement{Style.RESET_ALL}")
self.add_cmd(f"scoreboard players operation MineScript {self.igfunctionargs[name][0][var]} = MineScript {n}")
self.add_cmd(f"function {self.datapack_name}:{name}")
return self.igfunctionreturn[name]
def visitIgReturn(self, ctx):
if self.igfunc:
r_value = self.get_var_name(self.visit(ctx.igexpr()))
self.add_cmd(f"scoreboard players operation MineScript {self.igfunctionreturn[self.igfunc]} = MineScript {r_value}")
else:
print((error+syntaxerror2+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip()))
raise Exception("Abort")
def visitIgId(self, ctx): # Expression of type $var
name = self.get_var_name(ctx.ID().getText())
return name
def visitAssign(self, ctx): # Expression of type var = expression
name = ctx.ID().getText()
value = self.visit(ctx.expr())
if self.func: self.localmemory[name] = value
else: self.memory[name] = value
return value
def visitAssignUnit(self, ctx): # Expression of type var++
name = ctx.ID().getText()
try:
if ctx.op.type == MineScriptParser.USUM: self.localmemory[name] += 1
elif ctx.op.type == MineScriptParser.USUB: self.localmemory[name] -= 1
except TypeError:
print((error+typeerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, self.localmemory[name].__class__.__name__))
raise Exception("Abort")
except KeyError:
try:
if ctx.op.type == MineScriptParser.USUM: self.memory[name] += 1
elif ctx.op.type == MineScriptParser.USUB: self.memory[name] -= 1
except TypeError:
print((error+typeerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, self.memory[name].__class__.__name__))
raise Exception("Abort")
except KeyError:
print((error+nameerror+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name))
raise Exception("Abort")
def visitAssignOp(self, ctx): # Expression of type var (*/+-%^)= expression
name = ctx.ID().getText()
value = self.visit(ctx.expr())
try:
if ctx.op.type == MineScriptParser.PE: self.localmemory[name] += value
elif ctx.op.type == MineScriptParser.SE: self.localmemory[name] -= value
elif ctx.op.type == MineScriptParser.MLE: self.localmemory[name] *= value
elif ctx.op.type == MineScriptParser.DE: self.localmemory[name] /= value
elif ctx.op.type == MineScriptParser.MDE: self.localmemory[name] %= value
elif ctx.op.type == MineScriptParser.PWE: self.localmemory[name] = self.localmemory[name] ** value
except TypeError:
print((error+typeerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, self.localmemory[name].__class__.__name__))
raise Exception("Abort")
except KeyError:
try:
if ctx.op.type == MineScriptParser.PE: self.memory[name] += value
elif ctx.op.type == MineScriptParser.SE: self.memory[name] -= value
elif ctx.op.type == MineScriptParser.MLE: self.memory[name] *= value
elif ctx.op.type == MineScriptParser.DE: self.memory[name] /= value
elif ctx.op.type == MineScriptParser.MDE: self.memory[name] %= value
elif ctx.op.type == MineScriptParser.PWE: self.memory[name] = self.memory[name] ** value
except TypeError:
print((error+typeerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, self.memory[name].__class__.__name__))
raise Exception("Abort")
except KeyError:
print((error+nameerror+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name))
raise Exception("Abort")
def visitNegative(self, ctx):
return -int(ctx.number().getText())
def visitNot(self, ctx): # Expression of type !var
value = self.visit(ctx.expr())
return not value
def visitFuncDef(self, ctx): # Expression of type function func() { stat }
name = ctx.ID(0).getText()
value = ctx.stat()
self.functionargs[name] = []
for arg in ctx.ID()[1:]:
self.functionargs[name].append(arg.getText())
if self.func: self.localmemory[name] = value
else: self.memory[name] = value
def visitFuncCall(self, ctx): # Expression of type func()
self.r_value = None
name = ctx.ID().getText()
args = ctx.expr()
if len(args) != len(self.functionargs[name]):
print((error+typeerror10+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, len(self.functionargs[name]), len(args)))
raise Exception("Abort")
for arg in range(len(args)):
self.localmemory[self.functionargs[name][arg]] = self.visit(args[arg])
if name in self.memory:
self.func = name
r = self.visit(self.memory[name])
self.localmemory = {}
self.func = None
return self.r_value
else:
print((error+nameerror+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name))
raise Exception("Abort")
def visitReturn(self, ctx):
if self.func:
self.r_value = self.visit(ctx.expr())
return self.r_value
else:
print((error+syntaxerror2+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip()))
raise Exception("Abort")
def visitPrint(self, ctx): # Expression of type print(expression,...)
exprs = ctx.expr()
values = []
for expr in exprs:
values.append(str(self.visit(expr)))
print(' '.join(values))
def visitLen(self, ctx): # Expression of type len(expression)
value = self.visit(ctx.expr())
try:
return len(value)
except TypeError:
print((error+typeerror2+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), value.__class__.__name__))
raise Exception("Abort")
def visitAbs(self, ctx): # Expression of type abs(expression)
value = self.visit(ctx.expr())
try:
return abs(value)
except TypeError:
print((error+typeerror8+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), value.__class__.__name__))
raise Exception("Abort")
def visitInt(self, ctx): # Expression of type int(expression)
value = self.visit(ctx.expr())
try:
return int(value)
except TypeError:
print((error+typeerror8+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), value.__class__.__name__))
raise Exception("Abort")
except ValueError:
print((error+valueerror2+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), value))
raise Exception("Abort")
def visitFloat(self, ctx): # Expression of type float(expression)
value = self.visit(ctx.expr())
try:
return float(value)
except TypeError:
print((error+typeerror7+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), value.__class__.__name__))
raise Exception("Abort")
except ValueError:
print((error+valueerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), value))
raise Exception("Abort")
def visitStr(self, ctx): # Expression of type str(expression)
value = self.visit(ctx.expr())
return str(value)
def visitSin(self, ctx):
value = self.visit(ctx.expr())
if not(type(value) == int or type(value) == float):
print((error+typeerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, value.__class__.__name__))
raise Exception("Abort")
return sin(value)
def visitCos(self, ctx):
value = self.visit(ctx.expr())
if not(type(value) == int or type(value) == float):
print((error+typeerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, value.__class__.__name__))
raise Exception("Abort")
return cos(value)
def visitTan(self, ctx):
value = self.visit(ctx.expr())
if not(type(value) == int or type(value) == float):
print((error+typeerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip(), name, value.__class__.__name__))
raise Exception("Abort")
return tan(value)
def visitConstant(self, ctx): # Expression of type int, float, string or boolean
value = ctx.literal().getText()
if value.isdigit():
return int(value)
elif value == "true":
return True
elif value == "false":
return False
elif value[0] == "\"" and value[-1] == "\"":
return value[1:-1]
else:
try:
value = float(value)
return value
except ValueError:
print((error+syntaxerror1+end)%(self.file, ctx.start.line, self.code[ctx.start.line-1].strip()))
raise Exception("Abort")
def visitConstantArray(self, ctx): # Expression of type array (=list) [expression,...]
values = []
exprs = ctx.array().expr()
for expr in exprs:
values.append(self.visit(expr))
return values