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 pathMappingVisitor.py
More file actions
275 lines (224 loc) · 10.9 KB
/
Copy pathMappingVisitor.py
File metadata and controls
275 lines (224 loc) · 10.9 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
from Visitor import Visitor
from MineScriptParser import MineScriptParser
import tags
class MVisitor(Visitor):
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.ig_r_value = None # Stores the return variable of the current igfunc
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 visitIgAssign(self, ctx): # Expression of type $var = expression
name = ctx.ID().getText()
self.visitChildren(ctx)
self.add_var(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 name2.startswith("_"): self.mark_unused(name2)
self.add_var(name1)
return name1
def visitIgAssignUnit(self, ctx): # Expression of type $var++
name = ctx.ID().getText()
return name
def visitIgAssignOp(self, ctx): # Expression of type $var (*/+-%)= expression
name = ctx.ID().getText()
self.visitChildren(ctx)
return name
def visitIgAssignIgOp(self, ctx): # Expression of type $var (*/+-%)= $expression
name2 = self.visitChildren(ctx)
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.visit(ctx.igexpr(0))
name2 = self.visit(ctx.igexpr(1))
if name1.startswith("_"): self.mark_unused(name1)
if name2.startswith("_"): self.mark_unused(name2)
result = self.get_var()
return result
def visitIgOp(self, ctx): # Expression of type $expression (*/+-%^) expression
name = self.visit(ctx.igexpr())
self.visit(ctx.expr())
if name.startswith("_"): self.mark_unused(name)
result = self.get_var()
return result
def visitIgOpM(self, ctx): # Expression of type expression (*/+-%^) $expression
return self.visitIgOp(ctx)
def visitIgNot(self, ctx): # Expression of type !$expression
name = self.get_var_name(self.visit(ctx.igexpr()))
if name.startswith("_"): self.mark_unused(name)
result = self.get_var()
return result
def visitIgBoolOp(self, ctx): # Expression of type genexpr &&/|| genexpr
if len(ctx.igexpr()) == 1:
expr1 = self.visit(ctx.igexpr(0))
if expr1.startswith("_"): self.mark_unused(expr1)
expr2 = self.visit(ctx.expr())
else:
expr1 = self.visit(ctx.igexpr(0))
expr2 = self.visit(ctx.igexpr(1))
if expr1.startswith("_"): self.mark_unused(expr1)
if expr2.startswith("_"): self.mark_unused(expr2)
result = self.get_var()
return result
def visitIgComparison(self, ctx): # Expression of type $expression (> < <= >= != ==) expression
name = self.visit(ctx.igexpr())
self.visit(ctx.expr())
if name.startswith("_"): self.mark_unused(name)
result = self.get_var()
return result
def visitIgComparisonM(self, ctx): # Expression of type expression (> < <= >= != ==) $expression
return self.visitIgComparison(ctx)
def visitExecute(self, ctx): # Expression of type $execute(execute){ stat }
self.visit(ctx.expr())
stat = ctx.stat()
self.visit(stat)
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 name1.startswith("_"): self.mark_unused(name1)
if name2.startswith("_"): self.mark_unused(name2)
result = self.get_var()
return result
def visitIgIfElse(self, ctx): # Expression of type $if ($expression) { stat } ($else { stat })?
name = self.visit(ctx.igexpr())
self.visit(ctx.stat(0))
if ctx.stat(1) is not None:
self.visit(ctx.stat(1))
if name.startswith("_"): self.mark_unused(name)
def visitGetPos(self, ctx): # Expression of type $pos(selector, index)
self.visitChildren(ctx)
result = self.get_var()
return result
def visitGetData(self, ctx): # Expression of type $getdata(selector, path, scale?)
self.visitChildren(ctx)
result = self.get_var()
return result
def visitSetData(self, ctx): # Expression of type $setdata(selector, path, value)
self.visit(ctx.expr(0))
self.visit(ctx.expr(1))
if ctx.genexpr().igexpr() is not None:
name = self.get_var_name(self.visit(ctx.genexpr().igexpr()))
if name.startswith("_"): self.mark_unused(name)
def visitIsBlock(self, ctx): # Expression of type $isblock(selector, pos, block)
self.visitChildren(ctx)
result = self.get_var()
return result
def visitAddObj(self, ctx):
self.visitChildren(ctx)
def visitEnableTrigger(self, ctx):
self.visitChildren(ctx)
def visitRename(self, ctx):
self.visitChildren(ctx)
def visitGetScore(self, ctx): # Expression of type $getscore(selector, name)
self.visitChildren(ctx)
result = self.get_var()
return result
def visitSetScore(self, ctx): # Expression of type $setscore(selector, name, value)
self.visit(ctx.expr(0))
self.visit(ctx.expr(1))
ge = ctx.genexpr()
if ge.expr() is not None:
self.get_var()
elif ge.igexpr() is not None:
name2 = self.get_var_name(self.visit(ge.igexpr()))
if name2.startswith("_"): self.mark_unused(name2)
def visitAddTag(self, ctx): # Expression of type $addtag(selector, tag)
self.visitChildren(ctx)
def visitRemTag(self, ctx): # Expression of type $remtag(selector, tag)
self.visitChildren(ctx)
def visitHasTag(self, ctx): # Expression of type $hastag(selector, tag)
self.visitChildren(ctx)
result = self.get_var()
return result
def visitCount(self, ctx): # Expression of type $count(selector)
self.visitChildren(ctx)
result = self.get_var()
return result
def visitIgWhile(self, ctx): # Expression of type $while (genexpr) { stat }
stats = ctx.stat()
expr = ctx.genexpr()
if expr.igexpr() is not None:
expr_n = self.visit(expr.igexpr())
expr_n = self.visit(expr.igexpr())
else:
self.visit(expr.expr())
self.visit(stats)
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()
self.visit(init)
expr_n = self.visit(expr)
self.visit(stats)
self.visit(update)
expr_n = self.visit(expr)
if expr_n.startswith("_"): self.mark_unused(expr_n)
def visitIgForEntity(self, ctx): # Expression of type $forentity(selector; new_var) { stat }
name = ctx.ID().getText()
self.visit(ctx.expr())
self.memory[name] = ""
stats = ctx.stat()
self.visit(stats)
def visitIgPrint(self, ctx): # Expression of type $print(genexpression,...| COLOR)
for child in ctx.igPrintControl().igPrintArg():
e = child.genexpr()
if e.expr() is not None: self.visit(e.expr())
if e.igexpr() is not None:
self.visit(e.igexpr())
name = self.get_var_name(self.visit(e.igexpr()))
if name.startswith("_"): self.mark_unused(name)
def visitTeleport(self, ctx): # Expression of type $tp(selector, pos)
self.visitChildren(ctx)
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.igfunctions[name] = []
self.visit(stats)
def visitIgFuncCall(self, ctx): # Expression of type $func()
return ""
def visitIgReturn(self, ctx):
self.visitChildren(ctx)
def visitIgId(self, ctx): # Expression of type $var
name = self.get_var_name(ctx.ID().getText())
return name
def visitSetDisplay(self, ctx): # Expression of type $setdisplay(var, mode)
name = self.visit(ctx.igexpr())
if name.startswith("_"): self.mark_unused(name)
def visitCommand(self, ctx): # Expression of type $mc(expression)
self.visitChildren(ctx)
def visitPrint(self, ctx):
self.visitChildren(ctx)
del Visitor