mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 02:12:00 +03:00
Refactor parseLine logic in src/engine/parser.ts (#100)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
parent
62f55b10a9
commit
1e80c55011
1 changed files with 17 additions and 4 deletions
|
|
@ -62,6 +62,18 @@ export function parseLine(tokens: Token[], knownVars: Set<string>, line: string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const filtered = filterNoiseTokens(toks, knownVars);
|
||||||
|
|
||||||
|
const goalSeek = tryParseGoalSeek(filtered, knownVars);
|
||||||
|
if (goalSeek) {
|
||||||
|
return { expr: goalSeek };
|
||||||
|
}
|
||||||
|
|
||||||
|
const p = new Parser(filtered, knownVars);
|
||||||
|
return { assign, expr: p.parseSeq() };
|
||||||
|
}
|
||||||
|
|
||||||
|
function filterNoiseTokens(toks: Token[], knownVars: Set<string>): Token[] {
|
||||||
// Noise filtering: drop junk and unknown words, but keep words that follow
|
// Noise filtering: drop junk and unknown words, but keep words that follow
|
||||||
// a conversion operator (potential timezone names).
|
// a conversion operator (potential timezone names).
|
||||||
const filtered: Token[] = [];
|
const filtered: Token[] = [];
|
||||||
|
|
@ -81,7 +93,10 @@ export function parseLine(tokens: Token[], knownVars: Set<string>, line: string)
|
||||||
afterConv = tk.t === "conv";
|
afterConv = tk.t === "conv";
|
||||||
filtered.push(tk);
|
filtered.push(tk);
|
||||||
}
|
}
|
||||||
|
return filtered;
|
||||||
|
}
|
||||||
|
|
||||||
|
function tryParseGoalSeek(filtered: Token[], knownVars: Set<string>): Node | null {
|
||||||
// Goal seek: `? * 1.2 = 1000` → find x where lhs(x) = rhs(x)
|
// Goal seek: `? * 1.2 = 1000` → find x where lhs(x) = rhs(x)
|
||||||
const hasUnknown = filtered.some((tk) => tk.t === "unknown");
|
const hasUnknown = filtered.some((tk) => tk.t === "unknown");
|
||||||
if (hasUnknown) {
|
if (hasUnknown) {
|
||||||
|
|
@ -89,12 +104,10 @@ export function parseLine(tokens: Token[], knownVars: Set<string>, line: string)
|
||||||
if (assignIdx >= 0) {
|
if (assignIdx >= 0) {
|
||||||
const lhs = new Parser(filtered.slice(0, assignIdx), knownVars).parseSeq();
|
const lhs = new Parser(filtered.slice(0, assignIdx), knownVars).parseSeq();
|
||||||
const rhs = new Parser(filtered.slice(assignIdx + 1), knownVars).parseSeq();
|
const rhs = new Parser(filtered.slice(assignIdx + 1), knownVars).parseSeq();
|
||||||
if (lhs && rhs) return { expr: { k: "goalseek", lhs, rhs } };
|
if (lhs && rhs) return { k: "goalseek", lhs, rhs };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
const p = new Parser(filtered, knownVars);
|
|
||||||
return { assign, expr: p.parseSeq() };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue