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:
Horizontal42 2026-08-19 16:36:05 +03:00 • committed by GitHub
parent 62f55b10a9
commit 1e80c55011
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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() };
} }