Files
Aislo/common_util/common_util_line_stack_frac.ts
T
eomsangdonandClaude Opus 5.5 114c3100f1 feat(847-1): 기계 작업량 방식에 따라 안 고른 쪽 숨김 · 시간당 식이면 기계 굳음 · 기계 밖 겹분수 55 줄 나눔
- 숨김 규칙(formula_doc_render hiddenByWork) — 금액 줄에서 따라가 시간당 식 줄이 더 안 읽는 기호만 숨김(옵션 가로 · 공식 묶음 · 산출근거) · 보이기만(문서 값 · 식 그대로)
- 기계 굳음 — 시간당으로 바꿀 때 그 줄 기계를 덮음 `기계`에 담음 · 셈은 그 줄 단가를 굳은 기계로 다시 부름(계산기 단가 범위) · 작업량 식 · 변수도 그 기계 것 · 셈 답 `작업량.고정` · `기호`
- 덮음 열쇠 = 그 줄 단가 기호(옵션이 기계를 바꿔도 같은 줄) › 기계 열쇠(846-1 옛 꼴 그대로 읽음) · 서버 정리 기본 변수 = 굳은 기계 작업량 칸 · 사본 부름에 굳은 기계 식 문서
- 기계 창으로 바꿈 — 새 기계 작업량 식 · 규격 변수로 다시 차림(같은 식 = 현장 변수 남김 · 다른 식 = 버림 + 알림 · 맞는 식 없음 = 품셈 일당 + 알림)
- 수량 칸 = 「1 ÷ 시간당 작업량」 읽기 글 · 방식 하나뿐인 줄 = 고르기 없음
- 기계 밖 두 층 분수 = 짝 나란히 · 곱의 분수 항 펼침 · 중간 줄 이름(문서 키 사전)
- 시험: test_847_1_work_hide 새로 · test_846_2 「식 없는 기계 잠김 글」 → 고르기 없음(요구 884 차림 바뀜) · test_846_1 셈 답 `기호` 칸 더함 · test_802_4b2 소스 글자 보는 줄(옵션 호출 글 · 사본 단추 글) 지움(규칙 — 결과 시험 아님 · 읽기만은 같은 시험의 403 이 봄)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 11:56:32 +09:00

143 lines
5.9 KiB
TypeScript

/* =============================================================================
* common_util_line_stack_frac.ts
* 산출근거 분수 층(PLAN 845-2 · 요구 880 「분모 · 분자가 쌓여 층층이 → 행으로 나눔」)
* 층 = 조판(`line_stack_tex`)과 같은 셈 — ÷ 하나 = 분수 한 층 · 감싸개(반올림 · 올림 · 버림)는 안 셈
* 기계경비식(`splitFrac`) = 한 줄에 분수 한 층까지:
* ① 곱 · 나눗셈 사슬에 ÷ 가 둘 넘으면 「a ÷ b」 를 짝으로 나란히(겹 대신 · 값 같음)
* ② 「…분자 ÷ …분모」 짝(그림 이름 머리가 줄 이름과 다름) = 중간 줄 「상여 = 16 ÷ 12」
* ③ 분수 안 덧셈 묶음이 또 분수를 품으면 = 중간 줄(그림 사전 `partName` — 「손료계수」)
* ④ 그래도 남는 안쪽 분수 = 조판이 ÷ 한 줄로(`TexCtx.frac`)
* 그리기만 — 문서 식 · 값 · 금액 불변 · 중간 줄 값 = 같은 식 조각을 같은 셈(`evalNode`) · 화면 없음(시험이 Node 로 돌림)
* 기계 밖 문서(847-1) = 두 층 넘는 줄만 같은 규칙 + 곱의 분수 항(「× (1 ÷ L)」)을 사슬로 펼침(`lift`)
* ========================================================================== */
import type { Node } from "./common_util_formula_doc";
import { evalNode, type Values } from "./common_util_formula_doc_view";
const HIDE = new Set(["반올림", "올림", "버림"]);
const hidden = (n: Node): n is ["call", string, Node[]] =>
n[0] === "call" && HIDE.has(n[1]) && n[2].length >= 1 && n[2].length <= 2;
/** 조판 분수 층 수 */
export function fracDepth(n: Node): number {
switch (n[0]) {
case "neg":
case "pct":
return fracDepth(n[1]);
case "call": {
const items = n[2];
return hidden(n) ? fracDepth(items[0]) : Math.max(0, ...items.map(fracDepth));
}
case "bin": {
const d = Math.max(fracDepth(n[2]), fracDepth(n[3]));
return n[1] === "/" ? d + 1 : d;
}
default:
return 0;
}
}
/** 왼쪽으로 쌓인 곱 · 나눗셈 사슬 → [연산, 항] · `lift` = 곱한 분수 항 「× (a ÷ b)」 → 「× a ÷ b」 */
function chain(n: Node, lift = false): ["*" | "/", Node][] {
if (n[0] === "bin" && (n[1] === "*" || n[1] === "/")) {
const r = n[3];
if (lift && n[1] === "*" && isFrac(r)) return [...chain(n[2], lift), ["*", r[2]], ["/", r[3]]];
return [...chain(n[2], lift), [n[1] as "*" | "/", r]];
}
return [["*", n]];
}
const product = (xs: Node[]): Node => xs.reduce((a, b) => ["bin", "*", a, b]);
function isFrac(n: Node): n is ["bin", string, Node, Node] {
return n[0] === "bin" && n[1] === "/";
}
/** ① ÷ 둘 넘는 사슬 = 「a ÷ b」 짝을 나란히 — 감싸개 · 괄호 안까지 */
export function regroup(n: Node, lift = false): Node {
const re = (x: Node): Node => regroup(x, lift);
switch (n[0]) {
case "neg":
return ["neg", re(n[1])];
case "pct":
return ["pct", re(n[1])];
case "call":
return ["call", n[1], n[2].map(re)];
case "bin": {
const items = chain(n, lift);
if (items.length < 2 || items.filter(([o]) => o === "/").length < 2)
return ["bin", n[1], re(n[2]), re(n[3])];
const out: Node[] = [];
for (const [o, raw] of items) {
const x = re(raw);
const last = out[out.length - 1];
if (o === "*" || !last) out.push(x);
else if (isFrac(last))
out[out.length - 1] = ["bin", "/", last[2], ["bin", "*", last[3], x]];
else out[out.length - 1] = ["bin", "/", last, x];
}
return product(out);
}
default:
return n;
}
}
export interface FracRow {
/** 중간 줄 이름(그림 전용 — 문서 기호 아님) */
id: string;
node: Node;
}
const numOk = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v);
/** 기계경비식 한 줄 → 중간 줄 + 줄인 식 · 중간 값(`vals` 에 얹을 것) */
export function splitFrac(
id: string,
n: Node,
alias: Record<string, string>,
taken: Set<string>,
vals: Values,
part?: string,
lift = false,
): { rows: FracRow[]; node: Node; vals: Values } {
const rows: FracRow[] = [];
const got: Values = {};
const name = (x: string): string => alias[x] ?? x;
const own = name(id);
const take = (nm: string, sub: Node): Node | null => {
if (!nm || taken.has(nm) || nm in got) return null;
const v = evalNode(sub, { ...vals, ...got });
if (!numOk(v)) return null;
got[nm] = v;
rows.push({ id: nm, node: sub });
return ["var", nm];
};
// 감싸개(버림 등)는 그대로 두고 안쪽만
const inner = (x: Node, fn: (y: Node) => Node): Node =>
hidden(x) ? ["call", x[1], [inner(x[2][0], fn), ...x[2].slice(1)]] : fn(x);
let node = inner(regroup(n, lift), (x) => {
// ② 분자 · 분모 짝(곱 사슬의 분수 항)
const factors = (y: Node): Node[] =>
y[0] === "bin" && y[1] === "*" ? [...factors(y[2]), y[3]] : [y];
const parts = factors(x).map((f) => {
if (!isFrac(f) || f[2][0] !== "var" || f[3][0] !== "var") return f;
const a = /^(.+)분자$/.exec(name(f[2][1]));
const b = /^(.+)분모$/.exec(name(f[3][1]));
return a && b && a[1] === b[1] && a[1] !== own ? (take(a[1], f) ?? f) : f;
});
return product(parts);
});
// ③ 분수 안 덧셈 묶음이 분수를 품음 = 중간 줄(사전 이름 있을 때만)
if (part && fracDepth(node) > 1) {
const dig = (x: Node, inFrac: boolean): Node => {
if (x[0] === "bin" && (x[1] === "+" || x[1] === "-") && inFrac && fracDepth(x) >= 1)
return take(part, x) ?? x;
if (x[0] === "bin") return ["bin", x[1], dig(x[2], inFrac || x[1] === "/"), dig(x[3], inFrac || x[1] === "/")]; // prettier-ignore
if (x[0] === "call") return ["call", x[1], x[2].map((y) => dig(y, inFrac))];
if (x[0] === "neg" || x[0] === "pct") return [x[0], dig(x[1], inFrac)] as Node;
return x;
};
node = dig(node, false);
}
return { rows, node, vals: got };
}