mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 23:51:59 +03:00
split evalBin into percent/quantity arithmetic helpers
This commit is contained in:
parent
d66cad2dcf
commit
42f3611dca
1 changed files with 38 additions and 32 deletions
|
|
@ -199,12 +199,7 @@ function numericAdd(a: Quantity, b: Quantity, sign: 1 | -1): Quantity {
|
||||||
return qty(sign === 1 ? a.value.add(b.value) : a.value.sub(b.value), unit);
|
return qty(sign === 1 ? a.value.add(b.value) : a.value.sub(b.value), unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
function evalBin(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", l: Value, r: Value, ctx: EvalCtx): Value {
|
function evalPercentArith(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", l: Value, r: Value): Value {
|
||||||
// ---- dates
|
|
||||||
if (l.kind === "date" || r.kind === "date") return evalDateArith(op, l, r);
|
|
||||||
|
|
||||||
// ---- percent combinations
|
|
||||||
if (l.kind === "percent" || r.kind === "percent") {
|
|
||||||
if (l.kind === "percent" && r.kind === "percent") {
|
if (l.kind === "percent" && r.kind === "percent") {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case "plus": return pct(l.value.add(r.value));
|
case "plus": return pct(l.value.add(r.value));
|
||||||
|
|
@ -234,8 +229,7 @@ function evalBin(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", l: Value,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const a = l as Quantity;
|
function evalQuantityArith(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", a: Quantity, b: Quantity, ctx: EvalCtx): Value {
|
||||||
const b = r as Quantity;
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case "plus": return numericAdd(a, b, 1);
|
case "plus": return numericAdd(a, b, 1);
|
||||||
case "minus": return numericAdd(a, b, -1);
|
case "minus": return numericAdd(a, b, -1);
|
||||||
|
|
@ -281,6 +275,18 @@ function evalBin(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", l: Value,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function evalBin(op: "plus" | "minus" | "mul" | "div" | "mod" | "pow", l: Value, r: Value, ctx: EvalCtx): Value {
|
||||||
|
if (l.kind === "date" || r.kind === "date") {
|
||||||
|
return evalDateArith(op, l, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (l.kind === "percent" || r.kind === "percent") {
|
||||||
|
return evalPercentArith(op, l, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
return evalQuantityArith(op, l as Quantity, r as Quantity, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
function evalBit(op: string, l: Value, r: Value): Value {
|
function evalBit(op: string, l: Value, r: Value): Value {
|
||||||
if (l.kind !== "quantity" || r.kind !== "quantity") throw new EvalError("bitwise needs integers");
|
if (l.kind !== "quantity" || r.kind !== "quantity") throw new EvalError("bitwise needs integers");
|
||||||
if (!l.value.isFinite() || !r.value.isFinite()) throw new EvalError("bitwise needs finite integers");
|
if (!l.value.isFinite() || !r.value.isFinite()) throw new EvalError("bitwise needs finite integers");
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue