@poe-platform/safe-bash 0.1.50 → 0.1.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4822,47 +4822,62 @@ function evaluateArithmetic(program, variables) {
|
|
|
4822
4822
|
throw new Error(`Unsupported arithmetic operator ${operator}`);
|
|
4823
4823
|
}
|
|
4824
4824
|
};
|
|
4825
|
-
const evaluate = (node) => {
|
|
4826
|
-
if (++steps > 1e4) throw new Error("Arithmetic operation limit exceeded");
|
|
4827
|
-
let value2;
|
|
4828
|
-
if (node.kind === "literal") return node.value;
|
|
4829
|
-
if (node.kind === "name") {
|
|
4830
|
-
if (visiting.has(node.name) || visiting.size >= 64) throw new Error("Arithmetic variable recursion");
|
|
4831
|
-
visiting.add(node.name);
|
|
4832
|
-
try {
|
|
4833
|
-
return evaluate(parseArithmetic(variables[node.name] ?? "0"));
|
|
4834
|
-
} finally {
|
|
4835
|
-
visiting.delete(node.name);
|
|
4836
|
-
}
|
|
4837
|
-
}
|
|
4838
|
-
if (node.kind === "conditional") return evaluate(evaluate(node.condition) ? node.yes : node.no);
|
|
4839
|
-
if (node.kind === "unary") {
|
|
4840
|
-
const operand = evaluate(node.operand);
|
|
4841
|
-
if (node.operator === "+") value2 = operand;
|
|
4842
|
-
else if (node.operator === "-") value2 = -operand;
|
|
4843
|
-
else if (node.operator === "!") value2 = BigInt(!operand);
|
|
4844
|
-
else if (node.operator === "~") value2 = ~operand;
|
|
4845
|
-
else {
|
|
4846
|
-
value2 = BigInt.asIntN(64, operand + (node.operator === "++" ? 1n : -1n));
|
|
4847
|
-
variables[node.operand.name] = String(value2);
|
|
4848
|
-
if (node.postfix) value2 = operand;
|
|
4849
|
-
}
|
|
4850
|
-
} else {
|
|
4851
|
-
if (node.operator === "=") value2 = evaluate(node.right);
|
|
4852
|
-
else {
|
|
4853
|
-
const left = evaluate(node.left);
|
|
4854
|
-
if (node.operator === "&&") return left ? BigInt(evaluate(node.right) !== 0n) : 0n;
|
|
4855
|
-
if (node.operator === "||") return left ? 1n : BigInt(evaluate(node.right) !== 0n);
|
|
4856
|
-
if (node.operator === ",") return evaluate(node.right);
|
|
4857
|
-
value2 = binary(precedence[node.operator] === 2 ? node.operator.slice(0, -1) : node.operator, left, evaluate(node.right), node.right.start ?? 0);
|
|
4858
|
-
}
|
|
4859
|
-
if (precedence[node.operator] === 2) variables[node.left.name] = String(BigInt.asIntN(64, value2));
|
|
4860
|
-
}
|
|
4861
|
-
return BigInt.asIntN(64, value2);
|
|
4862
|
-
};
|
|
4863
4825
|
try {
|
|
4864
4826
|
if (program.error) throw program.error;
|
|
4865
|
-
|
|
4827
|
+
const pending = [{ kind: "evaluate", node: program.tree }];
|
|
4828
|
+
let value2 = 0n;
|
|
4829
|
+
while (pending.length) {
|
|
4830
|
+
const frame = pending.pop();
|
|
4831
|
+
if (frame.kind === "evaluate") {
|
|
4832
|
+
if (++steps > 1e4) throw new Error("Arithmetic operation limit exceeded");
|
|
4833
|
+
const node = frame.node;
|
|
4834
|
+
if (node.kind === "literal") value2 = node.value;
|
|
4835
|
+
else if (node.kind === "name") {
|
|
4836
|
+
if (visiting.has(node.name) || visiting.size >= 64) throw new Error("Arithmetic variable recursion");
|
|
4837
|
+
visiting.add(node.name);
|
|
4838
|
+
pending.push({ kind: "variable", name: node.name }, { kind: "evaluate", node: parseArithmetic(variables[node.name] ?? "0") });
|
|
4839
|
+
} else if (node.kind === "conditional") {
|
|
4840
|
+
pending.push({ kind: "conditional", node }, { kind: "evaluate", node: node.condition });
|
|
4841
|
+
} else if (node.kind === "unary") {
|
|
4842
|
+
pending.push({ kind: "unary", node }, { kind: "evaluate", node: node.operand });
|
|
4843
|
+
} else if (node.operator === "=") {
|
|
4844
|
+
pending.push({ kind: "right", node }, { kind: "evaluate", node: node.right });
|
|
4845
|
+
} else {
|
|
4846
|
+
pending.push({ kind: "left", node }, { kind: "evaluate", node: node.left });
|
|
4847
|
+
}
|
|
4848
|
+
} else if (frame.kind === "variable") visiting.delete(frame.name);
|
|
4849
|
+
else if (frame.kind === "conditional") {
|
|
4850
|
+
pending.push({ kind: "evaluate", node: value2 ? frame.node.yes : frame.node.no });
|
|
4851
|
+
} else if (frame.kind === "unary") {
|
|
4852
|
+
const { node } = frame;
|
|
4853
|
+
const operand = value2;
|
|
4854
|
+
if (node.operator === "+") value2 = operand;
|
|
4855
|
+
else if (node.operator === "-") value2 = -operand;
|
|
4856
|
+
else if (node.operator === "!") value2 = BigInt(!operand);
|
|
4857
|
+
else if (node.operator === "~") value2 = ~operand;
|
|
4858
|
+
else {
|
|
4859
|
+
value2 = BigInt.asIntN(64, operand + (node.operator === "++" ? 1n : -1n));
|
|
4860
|
+
variables[node.operand.name] = String(value2);
|
|
4861
|
+
if (node.postfix) value2 = operand;
|
|
4862
|
+
}
|
|
4863
|
+
value2 = BigInt.asIntN(64, value2);
|
|
4864
|
+
} else if (frame.kind === "left") {
|
|
4865
|
+
const { node } = frame;
|
|
4866
|
+
if (node.operator === "&&" || node.operator === "||") {
|
|
4867
|
+
if (node.operator === "&&" ? value2 === 0n : value2 !== 0n) value2 = BigInt(value2 !== 0n);
|
|
4868
|
+
else pending.push({ kind: "logical" }, { kind: "evaluate", node: node.right });
|
|
4869
|
+
} else {
|
|
4870
|
+
if (node.operator !== ",") pending.push({ kind: "right", node, left: value2 });
|
|
4871
|
+
pending.push({ kind: "evaluate", node: node.right });
|
|
4872
|
+
}
|
|
4873
|
+
} else if (frame.kind === "right") {
|
|
4874
|
+
const { node } = frame;
|
|
4875
|
+
if (node.operator !== "=") value2 = binary(precedence[node.operator] === 2 ? node.operator.slice(0, -1) : node.operator, frame.left, value2, node.right.start ?? 0);
|
|
4876
|
+
if (precedence[node.operator] === 2) variables[node.left.name] = String(BigInt.asIntN(64, value2));
|
|
4877
|
+
value2 = BigInt.asIntN(64, value2);
|
|
4878
|
+
} else value2 = BigInt(value2 !== 0n);
|
|
4879
|
+
}
|
|
4880
|
+
return value2;
|
|
4866
4881
|
} catch (error) {
|
|
4867
4882
|
if (error instanceof ArithmeticFailure) throw new Error(`${program.source.trimStart()}: ${error.message} (error token is "${program.source.slice(error.offset)}")`);
|
|
4868
4883
|
if (error instanceof ShellSyntaxError) {
|