This repository was archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.js
More file actions
306 lines (306 loc) · 7.69 KB
/
Copy pathentry.js
File metadata and controls
306 lines (306 loc) · 7.69 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
const mathjs = require("mathjs");
const mc = require("!lang/mc");
const config = require("!config/mc-math");
const CompilerError = require("!errors/CompilerError");
class ScoreConstant {
constructor(value) {
this.value = Math.floor(value);
this.name = "#" + this.value;
this.objective = config.constScoreboard;
this.isConst = true;
}
build(arr, temp, write) {
if (write) {
arr.push(
`load{\nscoreboard players set ${this.name} ${this.objective} ${this.value}\n}`
);
}
return {
o: `${this.name} ${this.objective}`,
v: this.value,
clean: true,
useSet: true,
};
}
static eval(a, op, b) {
let res = null;
switch (op) {
case "+":
res = a.value + b.value;
break;
case "-":
res = a.value - b.value;
break;
case "*":
res = a.value * b.value;
break;
case "/":
res = a.value / b.value;
break;
case "<":
res = Math.min(a.value, b.value);
break;
case ">":
res = Math.max(a.value, b.value);
break;
case "%":
res = a.value % b.value;
break;
case "-=":
case "+=":
case "/=":
case "*=":
case "%=":
case "=":
throw new Error("invalid operation");
}
return new ScoreConstant(res | 0);
}
}
class ScoreHolder {
constructor(name, objective, macroopts = null) {
this.name = name;
this.objective = objective;
this.macro = macroopts;
this.clean = true;
}
build(arr, temp) {
if (this.macro) {
this.getMacro(arr, temp);
}
return {
o: `${this.name} ${this.objective}`,
clean: this.clean,
useSet: false,
};
}
getMacro(arr, temp) {
const objectives = this.macro.args.map((_) => _.build(arr, temp, true));
arr.push(
`macro ${this.macro.name} ${this.name} ${this.objective} ${objectives
.map((_) => _.o)
.join(" ")}`
);
}
}
class Op {
constructor(left, right, op) {
this.left = left;
this.right = right;
this.op = op;
this.isOp = true;
if (Op.reversableOps.includes(this.op) && this.left.isConst) {
[this.left, this.right] = [this.right, this.left];
}
if (this.left.isConst && this.right.isConst) {
this.isOp = false;
Object.assign(this, ScoreConstant.eval(this.left, this.op, this.right));
}
}
optimize() {}
static reversableOps = ["+", "*", "<", ">"];
static transforms = {
"%": "%=",
"*": "*=",
"+": "+=",
"-": "-=",
"/": "/=",
"<": "<",
">": ">",
"=": "=",
"%=": "%=",
"*=": "*=",
"-=": "-=",
"+=": "+=",
"/=": "/=",
};
static optimizableOps = ["-", "+", "-=", "+=", "=", "<", ">"];
static executionOrder = [
["=", "%", "%=", "<", ">", "*", "*="],
["/", "/="],
["+", "+=", "-", "-="],
];
static getPriority(op) {
return Op.executionOrder[
Op.executionOrder.find((list) => list.includes(op))
];
}
get priority() {
return Op.getPriority(this.op);
}
canBeOptimize() {
return this.right.isConst && Op.optimizableOps.includes(this.op);
}
build(arr = [], temp) {
if (this.isConst) {
return {
o: `${this.name} ${this.objective}`,
v: this.value,
clean: true,
useSet: true,
};
}
this.optimize();
let my = [];
let left = this.left.build(arr, temp);
if (left.clean && !this.op.includes("=")) {
let new_left = { o: `${temp()} ${config.tempScoreboard}`, clean: false };
if (left.useSet) {
my.push(`scoreboard players set ${new_left.o} ${left.v}`);
} else {
my.push(`scoreboard players operation ${new_left.o} = ${left.o}`);
}
left = new_left;
} else if (this.left instanceof ScoreConstant) {
this.left.build(arr, temp, true);
}
if (this.canBeOptimize()) {
let op = "add",
invert = "remove";
let value = this.right.value;
let use = null;
switch (this.op) {
case "-=":
case "-":
op = "remove";
invert = "add";
break;
case "=":
use = op = invert = "set";
break;
}
if (value < 0) {
use = invert;
value *= -1;
} else if (value > 0) {
use = op;
}
if (use) {
my.push(`scoreboard players ${use} ${left.o} ${value}`);
} else if (this.left instanceof ScoreHolder) {
return this.left.build();
}
} else {
let right = this.right.build(arr, temp, true).o;
if (this.op !== "=" || right !== left.o) {
my.push(
`scoreboard players operation ${left.o} ${
Op.transforms[this.op]
} ${right}`
);
}
}
arr.push(...my);
return left;
}
}
const operations = ["+", "-", "/", "*", "%", "="];
const punctuation = ["(", ")", ...operations, ","];
function isNumber(part) {
return /^-*[0-9]+(\.[0-9]+)?$/.test(part);
}
function fixup(parts) {
let res = [];
for (let i = 0; i < parts.length - 1; i++) {
if (
!punctuation.includes(parts[i]) &&
!punctuation.includes(parts[i + 1])
) {
res.push(new ScoreHolder(parts[i], parts[i + 1]));
i++;
} else {
res.push(parts[i]);
}
}
res.push(parts[parts.length - 1]);
return res;
}
function parse(parts, str) {
const res = [];
let id = 0;
let sid = 0;
const parts2 = fixup(parts);
const lookup = new Map();
let mid = 0;
let selfRef = false;
function itterate(node) {
switch (node.type) {
case "ParenthesisNode":
return itterate(node.content);
case "OperatorNode":
if (node.fn === "unaryMinus") {
return new Op(new ScoreConstant(0), itterate(node.args[0]), "-");
}
return new Op(itterate(node.args[0]), itterate(node.args[1]), node.op);
case "SymbolNode":
if (node.name === "$$0") {
selfRef = true;
}
return lookup.get(node.name);
case "FunctionNode":
const name = node.fn.name;
const args = node.args.map(itterate);
return new ScoreHolder("m_" + mid++, config.tempScoreboard, {
name,
args,
});
case "ConstantNode": {
return new ScoreConstant(node.value);
}
default:
console.log(node.type, node);
}
}
let eq = parts2
.map((part) => {
if (typeof part === "string") {
return part;
} else {
const _id = "$$" + id++;
lookup.set(_id, part);
return _id;
}
})
.join(" ");
/**
* @type {ScoreHolder | false}
*/
let equals = false;
if (eq.startsWith("$$0 =")) {
equals = lookup.get("$$0");
eq = eq.substr(6);
}
const mjstree = mathjs.simplify(mathjs.parse(eq));
let tree = itterate(mjstree);
if (equals !== false) {
equals.clean = false;
tree = new Op(equals, tree, "=");
}
tree.build(res, () => sid++);
return [
`load{\nscoreboard objectives add ${config.tempScoreboard} dummy\nscoreboard objectives add ${config.constScoreboard} dummy\n}`,
...res,
];
}
module.exports = (api) => {
const transpiler = mc.transpiler;
const GenericConsumer = transpiler.consumer.Generic;
GenericConsumer.addAction({
match: ({ token }) => token.startsWith("eq"),
exec(file, tokens, func) {
const expr = tokens.shift().token.substr(2).trim();
try {
const parts = expr
.replace(/([=()+\-*%/,])/g, " $1 ")
.split(" ")
.filter(Boolean);
const commands = parse(parts, expr);
const res = mc.transpiler.tokenize(commands.join("\n"));
tokens.unshift(...res);
} catch (e) {
throw new CompilerError(`invalid equation '${expr}'`);
}
},
});
return { exported: {} };
};