mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 01:02:01 +03:00
refactor: use switch statement in parser loop for readability
Replaces the deeply nested if-chain inside the Pratt parser loop in `src/engine/parser.ts` with a `switch` statement over token types. Introduces a labeled `loop: for(;;)` to maintain accurate `continue`/`break` semantics while greatly enhancing the visual parsing and extensibility of the logic without hurting evaluation speed. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
868a34497b
commit
7adbbb6754
1 changed files with 120 additions and 103 deletions
|
|
@ -204,114 +204,131 @@ class Parser {
|
||||||
|
|
||||||
if (!lhs) return null;
|
if (!lhs) return null;
|
||||||
|
|
||||||
for (;;) {
|
loop: for (;;) {
|
||||||
tk = this.peek();
|
tk = this.peek();
|
||||||
if (!tk) break;
|
if (!tk) break;
|
||||||
|
|
||||||
if (tk.t === "percent") {
|
switch (tk.t) {
|
||||||
if (PREC.POSTFIX < minPrec) break;
|
case "percent": {
|
||||||
|
if (PREC.POSTFIX < minPrec) break loop;
|
||||||
this.i++;
|
this.i++;
|
||||||
lhs = { k: "pct", x: lhs };
|
lhs = { k: "pct", x: lhs };
|
||||||
continue;
|
continue loop;
|
||||||
}
|
}
|
||||||
if (tk.t === "bang") {
|
case "bang": {
|
||||||
if (PREC.POSTFIX < minPrec) break;
|
if (PREC.POSTFIX < minPrec) break loop;
|
||||||
this.i++;
|
this.i++;
|
||||||
lhs = { k: "fact", x: lhs };
|
lhs = { k: "fact", x: lhs };
|
||||||
continue;
|
continue loop;
|
||||||
}
|
}
|
||||||
if (tk.t === "unit") {
|
case "unit": {
|
||||||
if (PREC.POSTFIX < minPrec) break;
|
if (PREC.POSTFIX < minPrec) break loop;
|
||||||
this.i++;
|
this.i++;
|
||||||
lhs = { k: "unit", x: lhs, unit: tk.unit };
|
lhs = { k: "unit", x: lhs, unit: tk.unit };
|
||||||
continue;
|
continue loop;
|
||||||
}
|
}
|
||||||
if (tk.t === "currency") {
|
case "currency": {
|
||||||
if (PREC.POSTFIX < minPrec) break;
|
if (PREC.POSTFIX < minPrec) break loop;
|
||||||
this.i++;
|
this.i++;
|
||||||
lhs = { k: "curr", x: lhs, code: tk.code };
|
lhs = { k: "curr", x: lhs, code: tk.code };
|
||||||
continue;
|
continue loop;
|
||||||
}
|
}
|
||||||
if (tk.t === "scale") {
|
case "scale": {
|
||||||
if (PREC.POSTFIX < minPrec) break;
|
if (PREC.POSTFIX < minPrec) break loop;
|
||||||
this.i++;
|
this.i++;
|
||||||
lhs = { k: "scale", x: lhs, mult: tk.mult, label: "" };
|
lhs = { k: "scale", x: lhs, mult: tk.mult, label: "" };
|
||||||
continue;
|
continue loop;
|
||||||
}
|
}
|
||||||
if (tk.t === "op" && tk.op === "pow") {
|
case "op": {
|
||||||
if (PREC.POW < minPrec) break;
|
switch (tk.op) {
|
||||||
|
case "pow": {
|
||||||
|
if (PREC.POW < minPrec) break loop;
|
||||||
const save = this.i;
|
const save = this.i;
|
||||||
this.i++;
|
this.i++;
|
||||||
const r = this.parseExpr(PREC.POW);
|
const r = this.parseExpr(PREC.POW);
|
||||||
if (!r) { this.i = save; break; }
|
if (!r) { this.i = save; break loop; }
|
||||||
lhs = { k: "bin", op: "pow", l: lhs, r };
|
lhs = { k: "bin", op: "pow", l: lhs, r };
|
||||||
continue;
|
continue loop;
|
||||||
}
|
}
|
||||||
if (tk.t === "op" && (tk.op === "mul" || tk.op === "div" || tk.op === "mod")) {
|
case "mul":
|
||||||
if (PREC.MUL < minPrec) break;
|
case "div":
|
||||||
|
case "mod": {
|
||||||
|
if (PREC.MUL < minPrec) break loop;
|
||||||
const save = this.i;
|
const save = this.i;
|
||||||
this.i++;
|
this.i++;
|
||||||
const r = this.parseExpr(PREC.MUL + 1);
|
const r = this.parseExpr(PREC.MUL + 1);
|
||||||
if (!r) { this.i = save; break; }
|
if (!r) { this.i = save; break loop; }
|
||||||
lhs = { k: "bin", op: tk.op, l: lhs, r };
|
lhs = { k: "bin", op: tk.op, l: lhs, r };
|
||||||
continue;
|
continue loop;
|
||||||
}
|
}
|
||||||
if (tk.t === "pctop" && (tk.op === "of" || tk.op === "off" || tk.op === "on")) {
|
case "plus":
|
||||||
if (!(lhs.k === "pct" || lhs.k === "var" || lhs.k === "pctop")) {
|
case "minus": {
|
||||||
this.i++;
|
if (PREC.ADD < minPrec) break loop;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (PREC.MUL < minPrec) break;
|
|
||||||
const save = this.i;
|
|
||||||
this.i++;
|
|
||||||
const r = this.parseExpr(PREC.MUL + 1);
|
|
||||||
if (!r) { this.i = save; break; }
|
|
||||||
lhs = { k: "pctop", op: tk.op, l: lhs, r };
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (tk.t === "lparen" || tk.t === "const" || tk.t === "func" || (tk.t === "word" && this.vars.has(tk.raw))) {
|
|
||||||
if (PREC.MUL < minPrec) break;
|
|
||||||
const save = this.i;
|
|
||||||
const r = this.parseExpr(PREC.MUL + 1);
|
|
||||||
if (!r) { this.i = save; break; }
|
|
||||||
lhs = { k: "bin", op: "mul", l: lhs, r };
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (tk.t === "op" && (tk.op === "plus" || tk.op === "minus")) {
|
|
||||||
if (PREC.ADD < minPrec) break;
|
|
||||||
const save = this.i;
|
const save = this.i;
|
||||||
this.i++;
|
this.i++;
|
||||||
const r = this.parseExpr(PREC.ADD + 1);
|
const r = this.parseExpr(PREC.ADD + 1);
|
||||||
if (!r) { this.i = save; break; }
|
if (!r) { this.i = save; break loop; }
|
||||||
lhs = { k: "bin", op: tk.op, l: lhs, r };
|
lhs = { k: "bin", op: tk.op, l: lhs, r };
|
||||||
continue;
|
continue loop;
|
||||||
}
|
}
|
||||||
if (tk.t === "bitop") {
|
}
|
||||||
if (PREC.BIT < minPrec) break;
|
break loop;
|
||||||
|
}
|
||||||
|
case "pctop": {
|
||||||
|
if (tk.op === "of" || tk.op === "off" || tk.op === "on") {
|
||||||
|
if (!(lhs.k === "pct" || lhs.k === "var" || lhs.k === "pctop")) {
|
||||||
|
this.i++;
|
||||||
|
continue loop;
|
||||||
|
}
|
||||||
|
if (PREC.MUL < minPrec) break loop;
|
||||||
const save = this.i;
|
const save = this.i;
|
||||||
this.i++;
|
this.i++;
|
||||||
const r = this.parseExpr(PREC.BIT + 1);
|
const r = this.parseExpr(PREC.MUL + 1);
|
||||||
if (!r) { this.i = save; break; }
|
if (!r) { this.i = save; break loop; }
|
||||||
lhs = { k: "bit", op: tk.op, l: lhs, r };
|
lhs = { k: "pctop", op: tk.op, l: lhs, r };
|
||||||
continue;
|
continue loop;
|
||||||
}
|
}
|
||||||
if (tk.t === "conv") {
|
if (tk.op.startsWith("as_pct") || tk.op.endsWith("what_is")) {
|
||||||
if (PREC.CONV < minPrec) break;
|
if (PREC.CONV < minPrec) break loop;
|
||||||
const save = this.i;
|
|
||||||
this.i++;
|
|
||||||
const target = this.parseTarget();
|
|
||||||
if (!target) { this.i = save; break; }
|
|
||||||
lhs = { k: "conv", x: lhs, target: this.tryHistoricalDate(target) };
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (tk.t === "pctop" && (tk.op.startsWith("as_pct") || tk.op.endsWith("what_is"))) {
|
|
||||||
if (PREC.CONV < minPrec) break;
|
|
||||||
const save = this.i;
|
const save = this.i;
|
||||||
this.i++;
|
this.i++;
|
||||||
const r = this.parseExpr(PREC.ADD);
|
const r = this.parseExpr(PREC.ADD);
|
||||||
if (!r) { this.i = save; break; }
|
if (!r) { this.i = save; break loop; }
|
||||||
lhs = { k: "pctop", op: tk.op, l: lhs, r };
|
lhs = { k: "pctop", op: tk.op, l: lhs, r };
|
||||||
continue;
|
continue loop;
|
||||||
|
}
|
||||||
|
break loop;
|
||||||
|
}
|
||||||
|
case "lparen":
|
||||||
|
case "const":
|
||||||
|
case "func":
|
||||||
|
case "word": {
|
||||||
|
if (tk.t === "word" && !this.vars.has(tk.raw)) break loop;
|
||||||
|
if (PREC.MUL < minPrec) break loop;
|
||||||
|
const save = this.i;
|
||||||
|
const r = this.parseExpr(PREC.MUL + 1);
|
||||||
|
if (!r) { this.i = save; break loop; }
|
||||||
|
lhs = { k: "bin", op: "mul", l: lhs, r };
|
||||||
|
continue loop;
|
||||||
|
}
|
||||||
|
case "bitop": {
|
||||||
|
if (PREC.BIT < minPrec) break loop;
|
||||||
|
const save = this.i;
|
||||||
|
this.i++;
|
||||||
|
const r = this.parseExpr(PREC.BIT + 1);
|
||||||
|
if (!r) { this.i = save; break loop; }
|
||||||
|
lhs = { k: "bit", op: tk.op, l: lhs, r };
|
||||||
|
continue loop;
|
||||||
|
}
|
||||||
|
case "conv": {
|
||||||
|
if (PREC.CONV < minPrec) break loop;
|
||||||
|
const save = this.i;
|
||||||
|
this.i++;
|
||||||
|
const target = this.parseTarget();
|
||||||
|
if (!target) { this.i = save; break loop; }
|
||||||
|
lhs = { k: "conv", x: lhs, target: this.tryHistoricalDate(target) };
|
||||||
|
continue loop;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue