What programs mean (the languages, translators, Mojo checker and
interpreter, prover, fuzzer, machine model, catalog problems and the Mojo
corpus with godbolt's stored answers) was mixed into the site's tree, so a
change to the prover and a change to a button went through the same gate
and the same docs. This moves all of it under core/, laid out as its own
repository, ready to be extracted with its history and pinned here as a
git submodule.
- core/: src/{engines,services,sync,mojo,exercises,curriculum,common},
mojo/, syntax/mojo.xml, its tests (with Workspace and workForProblem in
place of the site's Session), scripts (corpus pipeline, play, the
exercise and probe starters, engines export/diff), and its docs
(engines, trust, mojo-corpus, exercises, coverage). Its own pixi.toml,
package.json, README, AGENTS.md and CI workflow.
- core/tests/boundary.test.mjs: library modules use no DOM, import no
page widget, and import nothing outside src/.
- core/scripts/cli.mjs: pixi run cli check|run|prove|fuzz|translate.
- core/scripts/dafny.py: Dafny 4.11.0 pinned by version and SHA-256 per
platform, fetched into .tools/ (pixi run dafny-fetch, pixi run dafny).
- The site imports core only as #core/<path>.js: an import map in
index.html, "imports" in package.json for node, core's declarations for
tsc. The site's TypeScript builds core first (npm run build).
- assemble.sh, the Quarto engine copy, the render cache key, check-pages,
quarto-export and quarto-css read core/dist and core/mojo; quarto-css
copies core's syntax/mojo.xml into the extension.
- Site tests that covered engines moved to core; the rest keep Session.
- README, AGENTS.md, docs/app.md, docs/deploy.md and the CI workflow
describe the split (checkout with submodules; core's tests and corpus
status run against the pinned commit).
Gate: site 110 pass; core 307 pass; corpus status --strict passes.
Manual check: the assembled site opens Bridge (count-base-proved),
Developer and Hardware (count-base) with the editor, the stepper and the
board, and no module errors.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
1200 lines
63 KiB
TypeScript
1200 lines
63 KiB
TypeScript
/**
|
||
* matho: equations, written in LaTeX.
|
||
*
|
||
* A student pastes the formula from the paper or the textbook, written the
|
||
* way they would write it in a LaTeX document, and the pane becomes pseudo:
|
||
* one FUNCTION per equation, sums and products as accumulator loops, cases
|
||
* as IF/ELSE, Greek letters as names, subscripts as list positions.
|
||
*
|
||
* \bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i
|
||
*
|
||
* FUNCTION X_BAR(N : INTEGER, X : LIST OF FLOAT) RETURNS FLOAT
|
||
* DECLARE Total : FLOAT
|
||
* Total <- 0.0
|
||
* FOR I <- 0 TO N
|
||
* Total <- Total + X[I]
|
||
* NEXT I
|
||
* RETURN 1 / N * Total
|
||
* ENDFUNCTION
|
||
*
|
||
* The reader knows a deliberate subset of mathematics: arithmetic, fractions,
|
||
* roots, powers, e^x, |x|, floors and ceilings, sums and products with
|
||
* bounds or over a set, binomial coefficients, factorials, the usual
|
||
* functions (sin, cos, tan, exp, ln, log), max and min, dot products and
|
||
* norms of vectors, piecewise definitions with \begin{cases}, and
|
||
* references to equations defined higher up. Anything else (integrals,
|
||
* limits, infinity, derivatives) is refused with a sentence that says what
|
||
* finite procedure to write instead, because a procedure is what a program
|
||
* is, and that is the lesson.
|
||
*
|
||
* The decisions a translation has to make (which letters are whole numbers,
|
||
* which are lists, where a 1-based sum meets a 0-based list) are reported
|
||
* as notes on the equation's line, so nothing is decided silently.
|
||
*/
|
||
import type { Diagnostic, LanguageService, Lowered, Parsed, RunOptions, Trace } from '../common/types.js';
|
||
import { studyOf, runPseudo, defaultCall as pseudoDefaultCall } from './pseudo.js';
|
||
import { reservedNames } from '../common/reference.js';
|
||
|
||
/* ------------------------------------------------------------------ */
|
||
/* Tokens */
|
||
/* ------------------------------------------------------------------ */
|
||
|
||
type Tok = { t: 'cmd' | 'num' | 'id' | 'sym' | 'text' | 'end'; v: string; pos: number };
|
||
|
||
const TEXT_CMDS = new Set(['text', 'mathrm', 'operatorname', 'textrm', 'mathit', 'mbox']);
|
||
const SKIP_CMDS = new Set([',', ';', ':', '!', ' ', 'quad', 'qquad', 'displaystyle', 'nonumber', 'left', 'right', 'big', 'Big', 'bigg', 'Bigg', 'limits', 'nolimits', 'cdots', 'dots', 'ldots']);
|
||
|
||
function tokenize(src: string): { toks: Tok[]; error: string | null } {
|
||
const toks: Tok[] = [];
|
||
let i = 0;
|
||
let error: string | null = null;
|
||
const n = src.length;
|
||
while (i < n) {
|
||
const ch = src[i];
|
||
if (ch === '%') { while (i < n && src[i] !== '\n') i++; continue; }
|
||
if (/\s/.test(ch)) { i++; continue; }
|
||
if (ch === '\\') {
|
||
const m = /^\\([A-Za-z]+|.)/.exec(src.slice(i));
|
||
if (!m) { i++; continue; }
|
||
const name = m[1];
|
||
i += m[0].length;
|
||
if (SKIP_CMDS.has(name)) continue;
|
||
if (TEXT_CMDS.has(name)) {
|
||
// \text{...}: the braces hold words, not maths
|
||
let j = i; while (j < n && /\s/.test(src[j])) j++;
|
||
if (src[j] === '{') {
|
||
let depth = 0; let k = j;
|
||
for (; k < n; k++) { if (src[k] === '{') depth++; else if (src[k] === '}') { depth--; if (depth === 0) break; } }
|
||
toks.push({ t: 'text', v: src.slice(j + 1, k).trim(), pos: i - m[0].length });
|
||
i = k + 1;
|
||
continue;
|
||
}
|
||
}
|
||
if (name === 'label') { // \label{...} is for the document, not the maths
|
||
const close = src.indexOf('}', i); i = close < 0 ? n : close + 1; continue;
|
||
}
|
||
toks.push({ t: 'cmd', v: name, pos: i - m[0].length });
|
||
continue;
|
||
}
|
||
if (/[0-9]/.test(ch) || (ch === '.' && /[0-9]/.test(src[i + 1] || ''))) {
|
||
const m = /^[0-9]*\.?[0-9]+(?:[eE][+-]?[0-9]+)?|^[0-9]+\./.exec(src.slice(i))!;
|
||
toks.push({ t: 'num', v: m[0], pos: i });
|
||
i += m[0].length;
|
||
continue;
|
||
}
|
||
if (/[A-Za-z]/.test(ch)) { toks.push({ t: 'id', v: ch, pos: i }); i++; continue; }
|
||
if ('{}()[]^_+-*/=<>,|!&:;\'.'.includes(ch)) { toks.push({ t: 'sym', v: ch, pos: i }); i++; continue; }
|
||
if ('·×÷'.includes(ch)) { toks.push({ t: 'cmd', v: ch === '·' || ch === '×' ? 'cdot' : 'div', pos: i }); i++; continue; }
|
||
if ('≤≥≠'.includes(ch)) { toks.push({ t: 'cmd', v: ch === '≤' ? 'le' : ch === '≥' ? 'ge' : 'ne', pos: i }); i++; continue; }
|
||
if (ch === 'π') { toks.push({ t: 'cmd', v: 'pi', pos: i }); i++; continue; }
|
||
if (ch === '∑') { toks.push({ t: 'cmd', v: 'sum', pos: i }); i++; continue; }
|
||
if (ch === '∏') { toks.push({ t: 'cmd', v: 'prod', pos: i }); i++; continue; }
|
||
if (ch === '√') { toks.push({ t: 'cmd', v: 'sqrt', pos: i }); i++; continue; }
|
||
if (ch === '∈') { toks.push({ t: 'cmd', v: 'in', pos: i }); i++; continue; }
|
||
if (ch === '$') { i++; continue; }
|
||
if (!error) error = `The character ${JSON.stringify(ch)} is not part of the mathematics this reader knows.`;
|
||
i++;
|
||
}
|
||
toks.push({ t: 'end', v: '', pos: n });
|
||
return { toks, error };
|
||
}
|
||
|
||
/* ------------------------------------------------------------------ */
|
||
/* Syntax tree */
|
||
/* ------------------------------------------------------------------ */
|
||
|
||
type Decor = 'bar' | 'hat' | 'vec' | 'tilde' | 'bold' | null;
|
||
|
||
export type Expr =
|
||
| { k: 'num'; v: string }
|
||
| { k: 'var'; name: string; decor: Decor; sub: Expr | null; subText: string | null; pos: number }
|
||
| { k: 'bin'; op: '+' | '-' | '*' | '/' | 'mod'; l: Expr; r: Expr }
|
||
| { k: 'neg'; e: Expr }
|
||
| { k: 'pow'; b: Expr; e: Expr }
|
||
| { k: 'call'; fn: string; args: Expr[]; pos: number }
|
||
| { k: 'sqrt'; e: Expr; n: Expr | null }
|
||
| { k: 'abs'; e: Expr }
|
||
| { k: 'norm'; e: Expr }
|
||
| { k: 'floor'; e: Expr }
|
||
| { k: 'ceil'; e: Expr }
|
||
| { k: 'big'; op: 'sum' | 'prod'; idx: string; lo: Expr | null; hi: Expr | null; over: Expr | null; body: Expr; pos: number }
|
||
| { k: 'binom'; n: Expr; m: Expr }
|
||
| { k: 'fact'; e: Expr }
|
||
| { k: 'pi' }
|
||
| { k: 'cases'; arms: Array<{ value: Expr; cond: Cond | null }> }
|
||
| { k: 'dot'; l: Expr; r: Expr };
|
||
|
||
export type Cond =
|
||
| { k: 'cmp'; op: '<' | '<=' | '>' | '>=' | '==' | '!='; l: Expr; r: Expr }
|
||
| { k: 'and'; l: Cond; r: Cond }
|
||
| { k: 'or'; l: Cond; r: Cond }
|
||
| { k: 'not'; c: Cond };
|
||
|
||
export interface Equation {
|
||
/** 1-based line of the source where the equation starts. */
|
||
line: number;
|
||
source: string;
|
||
lhs: Expr | null;
|
||
rhs: Expr;
|
||
}
|
||
|
||
export interface MathoProgram {
|
||
equations: Equation[];
|
||
pseudo: string;
|
||
/** pseudo line (1-based) → equation index */
|
||
lineMap: number[];
|
||
/** Name and parameters of every function the pseudo defines, in order. */
|
||
defs: Array<{ name: string; params: Array<{ name: string; type: string }>; line: number }>;
|
||
}
|
||
|
||
class ParseError extends Error { constructor(msg: string, public pos: number, public id = 'matho-syntax') { super(msg); } }
|
||
|
||
const GREEK: Record<string, string> = {
|
||
alpha: 'Alpha', beta: 'Beta', gamma: 'Gamma', delta: 'Delta', epsilon: 'Epsilon', varepsilon: 'Epsilon', zeta: 'Zeta', eta: 'Eta', theta: 'Theta', vartheta: 'Theta',
|
||
iota: 'Iota', kappa: 'Kappa', lambda: 'Lambda', mu: 'Mu', nu: 'Nu', xi: 'Xi', rho: 'Rho', varrho: 'Rho', sigma: 'Sigma', tau: 'Tau', upsilon: 'Upsilon', phi: 'Phi', varphi: 'Phi',
|
||
chi: 'Chi', psi: 'Psi', omega: 'Omega', Gamma: 'GammaU', Delta: 'DeltaU', Theta: 'ThetaU', Lambda: 'LambdaU', Xi: 'XiU', Pi: 'PiU', Sigma: 'SigmaU', Phi: 'PhiU', Psi: 'PsiU', Omega: 'OmegaU', ell: 'Ell', hbar: 'HBar',
|
||
};
|
||
const FUNCS: Record<string, string> = { sin: 'SIN', cos: 'COS', tan: 'TAN', exp: 'EXP', ln: 'LOG', log: 'LOG10', sqrt: 'SQRT', max: 'MAX', min: 'MIN', arctan: 'ATAN', atan: 'ATAN', abs: 'ABS' };
|
||
const REFUSED: Record<string, string> = {
|
||
int: 'An integral has no finite procedure behind it. Write the sum it approximates instead: \\sum_{i=0}^{n-1} f(a + i \\, h) \\, h with a step h, which becomes a loop.',
|
||
iint: 'A double integral has no finite procedure behind it. Write it as nested sums over a grid of steps.',
|
||
oint: 'A contour integral has no finite procedure behind it. Write it as a sum over the points of the path.',
|
||
infty: 'A program cannot run to infinity. Give the sum or product an upper bound, such as n, and make n a parameter.',
|
||
lim: 'A limit is not a procedure. Pick a small step or a large n and write what the expression is there.',
|
||
partial: 'A derivative is not a procedure. Write the difference quotient it approximates: (f(x + h) - f(x)) / h for a step h.',
|
||
nabla: 'A gradient is not a procedure. Write one difference quotient per coordinate.',
|
||
prime: 'A derivative is not a procedure. Write the difference quotient it approximates.',
|
||
forall: 'A statement about all values is a specification, not a procedure. Put it in the speco pane as an ENSURE.',
|
||
exists: 'A statement about some value is a specification, not a procedure. Put it in the speco pane, or write the loop that searches for the value.',
|
||
cup: 'Set union is not a single step in pseudo. Write the loop that ADDs each member of one SET to the other.',
|
||
cap: 'Set intersection is not a single step in pseudo. Write the loop that keeps the members found in both.',
|
||
to: 'An arrow is not an operation. Write the value the expression becomes.',
|
||
rightarrow: 'An arrow is not an operation. Write the value the expression becomes.',
|
||
approx: 'Write = and the expression you actually want computed; a program cannot be approximately equal.',
|
||
propto: 'Proportionality leaves the constant out. Name the constant and write = with it in.',
|
||
sim: 'A distribution symbol is not a procedure. Write RANDN() or RAND(x) for the sample you want drawn.',
|
||
};
|
||
|
||
/* ------------------------------------------------------------------ */
|
||
/* Parser */
|
||
/* ------------------------------------------------------------------ */
|
||
|
||
class Parser {
|
||
toks: Tok[];
|
||
i = 0;
|
||
/** While the left side is being read, `f(x)` defines f even if f is new. */
|
||
private lhsMode = false;
|
||
/** How many \| ... \| norms are open, so that the closing \| is not read as a new factor. */
|
||
private normOpen = 0;
|
||
/** Functions defined by earlier equations, by their LaTeX name key. */
|
||
constructor(toks: Tok[], private knownFns: Set<string>) { this.toks = toks; }
|
||
|
||
get tok(): Tok { return this.toks[this.i]; }
|
||
peek(k = 1): Tok { return this.toks[Math.min(this.i + k, this.toks.length - 1)]; }
|
||
next(): Tok { return this.toks[this.i++]; }
|
||
isSym(v: string): boolean { return this.tok.t === 'sym' && this.tok.v === v; }
|
||
isCmd(v: string): boolean { return this.tok.t === 'cmd' && this.tok.v === v; }
|
||
expectSym(v: string, what: string): void {
|
||
if (!this.isSym(v)) throw new ParseError(`Expected ${JSON.stringify(v)} ${what}, but found ${this.describe(this.tok)}.`, this.tok.pos);
|
||
this.next();
|
||
}
|
||
describe(t: Tok): string { return t.t === 'end' ? 'the end of the equation' : t.t === 'cmd' ? `\\${t.v}` : t.t === 'text' ? `the text "${t.v}"` : JSON.stringify(t.v); }
|
||
|
||
/** The whole equation: `lhs = rhs`, `rhs`, or cases. */
|
||
equation(): { lhs: Expr | null; rhs: Expr } {
|
||
if (this.tok.t === 'end') throw new ParseError('The equation is empty.', 0);
|
||
this.lhsMode = true;
|
||
const first = this.expr();
|
||
this.lhsMode = false;
|
||
if (this.isSym('=') || this.isCmd('equiv') || (this.isSym(':') && this.peek().t === 'sym' && this.peek().v === '=')) {
|
||
if (this.isSym(':')) this.next();
|
||
this.next();
|
||
const rhs = this.exprOrCases();
|
||
if (this.isSym('=')) throw new ParseError('A chain of equalities (a = b = c) is a statement, not a definition. Keep the two sides you want computed, and put the rest in a comment.', this.tok.pos, 'matho-chain');
|
||
this.endOrError();
|
||
return { lhs: first, rhs };
|
||
}
|
||
this.endOrError();
|
||
return { lhs: null, rhs: first };
|
||
}
|
||
|
||
private endOrError(): void {
|
||
if (this.tok.t !== 'end') {
|
||
const t = this.tok;
|
||
if (t.t === 'sym' && (t.v === '<' || t.v === '>')) throw new ParseError('A comparison on its own is a condition, not a value. Use it inside \\begin{cases} ... \\end{cases}, or in the speco pane.', t.pos, 'matho-condition');
|
||
throw new ParseError(`Did not expect ${this.describe(t)} here.`, t.pos);
|
||
}
|
||
}
|
||
|
||
private exprOrCases(): Expr {
|
||
if (this.isCmd('begin')) return this.cases();
|
||
return this.expr();
|
||
}
|
||
|
||
private cases(): Expr {
|
||
this.next(); // \begin
|
||
const env = this.braceText();
|
||
if (env !== 'cases') throw new ParseError(`\\begin{${env}} is not something this reader knows; only \\begin{cases} is.`, this.tok.pos, 'matho-command');
|
||
const arms: Array<{ value: Expr; cond: Cond | null }> = [];
|
||
for (; ;) {
|
||
const value = this.expr();
|
||
let cond: Cond | null = null;
|
||
if (this.isSym('&')) {
|
||
this.next();
|
||
cond = this.condition();
|
||
}
|
||
arms.push({ value, cond });
|
||
if (this.isCmd('\\')) { this.next(); if (this.isCmd('end')) break; continue; }
|
||
if (this.isCmd('end')) break;
|
||
throw new ParseError(`Expected \\\\ between the cases or \\end{cases}, but found ${this.describe(this.tok)}.`, this.tok.pos);
|
||
}
|
||
this.next(); // \end
|
||
const close = this.braceText();
|
||
if (close !== 'cases') throw new ParseError(`\\begin{cases} was closed by \\end{${close}}.`, this.tok.pos);
|
||
if (!arms.some((a) => a.cond === null)) throw new ParseError('The cases need a last line with no condition (write "& \\text{otherwise}"), so that every input has a value.', this.tok.pos, 'matho-cases-otherwise');
|
||
if (arms.findIndex((a) => a.cond === null) !== arms.length - 1) throw new ParseError('The case with no condition ("otherwise") has to be the last one.', this.tok.pos, 'matho-cases-otherwise');
|
||
return { k: 'cases', arms };
|
||
}
|
||
|
||
private braceText(): string {
|
||
this.expectSym('{', 'after \\begin or \\end');
|
||
let s = '';
|
||
while (!this.isSym('}') && this.tok.t !== 'end') s += this.next().v;
|
||
this.expectSym('}', 'to close the environment name');
|
||
return s;
|
||
}
|
||
|
||
/** A condition in a cases arm: `\text{if } x < 0`, `x \ge 0 \text{ and } y > 0`, `0 \le x < 1`, `\text{otherwise}`. */
|
||
private condition(): Cond | null {
|
||
if (this.tok.t === 'text') {
|
||
const w = this.tok.v.toLowerCase().replace(/[^a-z]/g, '');
|
||
if (w === 'otherwise' || w === 'else') { this.next(); return null; }
|
||
if (w === 'if' || w === 'when' || w === 'for' || w === 'where') this.next();
|
||
}
|
||
return this.orCond();
|
||
}
|
||
private orCond(): Cond {
|
||
let l = this.andCond();
|
||
while (this.isCmd('lor') || this.isCmd('vee') || (this.tok.t === 'text' && /^or$/i.test(this.tok.v.trim()))) { this.next(); l = { k: 'or', l, r: this.andCond() }; }
|
||
return l;
|
||
}
|
||
private andCond(): Cond {
|
||
let l = this.cmp();
|
||
while (this.isCmd('land') || this.isCmd('wedge') || this.isSym(',') || (this.tok.t === 'text' && /^and$/i.test(this.tok.v.trim()))) { this.next(); l = { k: 'and', l, r: this.cmp() }; }
|
||
return l;
|
||
}
|
||
private cmpOp(): '<' | '<=' | '>' | '>=' | '==' | '!=' | null {
|
||
const t = this.tok;
|
||
if (t.t === 'sym') {
|
||
if (t.v === '<') { this.next(); if (this.isSym('=')) { this.next(); return '<='; } return '<'; }
|
||
if (t.v === '>') { this.next(); if (this.isSym('=')) { this.next(); return '>='; } return '>'; }
|
||
if (t.v === '=') { this.next(); if (this.isSym('=')) this.next(); return '=='; }
|
||
if (t.v === '!' && this.peek().t === 'sym' && this.peek().v === '=') { this.next(); this.next(); return '!='; }
|
||
}
|
||
if (t.t === 'cmd') {
|
||
const m: Record<string, any> = { le: '<=', leq: '<=', leqslant: '<=', ge: '>=', geq: '>=', geqslant: '>=', ne: '!=', neq: '!=', lt: '<', gt: '>' };
|
||
if (m[t.v]) { this.next(); return m[t.v]; }
|
||
}
|
||
return null;
|
||
}
|
||
private cmp(): Cond {
|
||
const l = this.expr();
|
||
const op = this.cmpOp();
|
||
if (!op) throw new ParseError(`A condition needs a comparison (<, \\le, =, \\ne, ...) after ${this.describe(this.toks[this.i - 1])}.`, this.tok.pos, 'matho-condition');
|
||
const r = this.expr();
|
||
let c: Cond = { k: 'cmp', op, l, r };
|
||
// a chain such as 0 \le x < 1
|
||
const op2 = this.cmpOp();
|
||
if (op2) { const r2 = this.expr(); c = { k: 'and', l: c, r: { k: 'cmp', op: op2, l: r, r: r2 } }; }
|
||
return c;
|
||
}
|
||
|
||
/* ---- expressions ---- */
|
||
|
||
expr(): Expr {
|
||
let l = this.term();
|
||
for (; ;) {
|
||
if (this.isSym('+')) { this.next(); l = { k: 'bin', op: '+', l, r: this.term() }; }
|
||
else if (this.isSym('-')) { this.next(); l = { k: 'bin', op: '-', l, r: this.term() }; }
|
||
else if (this.isCmd('pm') || this.isCmd('mp')) throw new ParseError('\\pm gives two values; a function returns one. Write the + version and the - version as two equations.', this.tok.pos, 'matho-pm');
|
||
else break;
|
||
}
|
||
return l;
|
||
}
|
||
|
||
private startsFactor(t: Tok): boolean {
|
||
if (t.t === 'num' || t.t === 'id' || t.t === 'text') return true;
|
||
if (t.t === 'sym') return t.v === '(' || t.v === '{' || t.v === '[';
|
||
if (t.t === 'cmd' && t.v === '|') return this.normOpen === 0;
|
||
if (t.t === 'cmd') return !['cdot', 'times', 'div', 'bmod', 'mod', 'le', 'leq', 'ge', 'geq', 'ne', 'neq', 'lt', 'gt', 'land', 'lor', 'end', '\\', 'equiv', 'in', 'pm', 'mp', 'right', 'rfloor', 'rceil', 'rvert', 'rVert', 'rangle'].includes(t.v);
|
||
return false;
|
||
}
|
||
|
||
private term(): Expr {
|
||
let l = this.unary();
|
||
for (; ;) {
|
||
if (this.isCmd('cdot') || this.isCmd('times') || this.isSym('*')) {
|
||
const dotted = this.isCmd('cdot');
|
||
this.next();
|
||
const r = this.unary();
|
||
l = dotted && isVector(l) && isVector(r) ? { k: 'dot', l, r } : { k: 'bin', op: '*', l, r };
|
||
} else if (this.isSym('/') || this.isCmd('div')) { this.next(); l = { k: 'bin', op: '/', l, r: this.unary() }; }
|
||
else if (this.isCmd('bmod') || this.isCmd('mod')) { this.next(); l = { k: 'bin', op: 'mod', l, r: this.unary() }; }
|
||
else if (this.startsFactor(this.tok) && !(this.tok.t === 'sym' && this.tok.v === '[' && false)) {
|
||
// juxtaposition is multiplication: 2 \pi r, m v^2, \frac{1}{n} \sum ...
|
||
l = { k: 'bin', op: '*', l, r: this.unary() };
|
||
} else break;
|
||
}
|
||
return l;
|
||
}
|
||
|
||
private unary(): Expr {
|
||
if (this.isSym('-')) { this.next(); return { k: 'neg', e: this.unary() }; }
|
||
if (this.isSym('+')) { this.next(); return this.unary(); }
|
||
return this.postfix();
|
||
}
|
||
|
||
/** An atom with its subscripts, superscripts, and factorials. */
|
||
private postfix(): Expr {
|
||
let e = this.atom();
|
||
for (; ;) {
|
||
if (this.isSym('^')) {
|
||
this.next();
|
||
const ex = this.scriptArg();
|
||
if (e.k === 'var' && e.name === 'e' && !e.sub && !e.decor) e = { k: 'call', fn: 'EXP', args: [ex], pos: e.pos };
|
||
else e = { k: 'pow', b: e, e: ex };
|
||
} else if (this.isSym('_')) {
|
||
this.next();
|
||
if (e.k !== 'var' || e.sub) throw new ParseError('A subscript belongs on a name: x_i, v_{\\max}, a_{i+1}.', this.tok.pos, 'matho-subscript');
|
||
const save = this.i;
|
||
const argToks = this.scriptTokens();
|
||
const text = argToks.every((t) => t.t === 'id' || t.t === 'text' || t.t === 'num') ? argToks.map((t) => t.v).join('') : null;
|
||
this.i = save;
|
||
e = { ...e, sub: this.scriptArg(), subText: text };
|
||
} else if (this.isSym('!')) { this.next(); e = { k: 'fact', e }; }
|
||
else if (this.isSym('\'')) throw new ParseError(REFUSED.prime, this.tok.pos, 'matho-refused');
|
||
else break;
|
||
}
|
||
return e;
|
||
}
|
||
|
||
/** The tokens of the next `{...}` group or single token, without consuming them. */
|
||
private scriptTokens(): Tok[] {
|
||
const out: Tok[] = [];
|
||
if (this.isSym('{')) {
|
||
let depth = 0;
|
||
for (let j = this.i; j < this.toks.length; j++) {
|
||
const t = this.toks[j];
|
||
if (t.t === 'sym' && t.v === '{') { depth++; if (depth === 1) continue; }
|
||
if (t.t === 'sym' && t.v === '}') { depth--; if (depth === 0) break; }
|
||
out.push(t);
|
||
}
|
||
} else out.push(this.tok);
|
||
return out;
|
||
}
|
||
|
||
private scriptArg(): Expr {
|
||
if (this.isSym('{')) { this.next(); const e = this.expr(); this.expectSym('}', 'to close the group'); return e; }
|
||
// a single token: x^2, x_i, e^x, \sum_i
|
||
const t = this.tok;
|
||
if (t.t === 'num' || t.t === 'id' || t.t === 'cmd' || t.t === 'text') return this.atom();
|
||
throw new ParseError(`Expected a group in braces or a single symbol after ${this.describe(this.toks[this.i - 1])}.`, t.pos);
|
||
}
|
||
|
||
private atom(): Expr {
|
||
const t = this.tok;
|
||
if (t.t === 'num') { this.next(); return { k: 'num', v: t.v }; }
|
||
if (t.t === 'id') {
|
||
this.next();
|
||
// f(x) where f names an earlier equation: a call
|
||
if (this.isSym('(') && (this.knownFns.has(t.v) || this.lhsMode)) return this.callArgs(t.v, t.pos);
|
||
return { k: 'var', name: t.v, decor: null, sub: null, subText: null, pos: t.pos };
|
||
}
|
||
if (t.t === 'text') {
|
||
this.next();
|
||
const name = t.v.replace(/\s+/g, '_');
|
||
if (this.isSym('(') && (this.knownFns.has(name) || this.lhsMode)) return this.callArgs(name, t.pos);
|
||
return { k: 'var', name, decor: null, sub: null, subText: null, pos: t.pos };
|
||
}
|
||
if (t.t === 'sym') {
|
||
if (t.v === '(' || t.v === '[') {
|
||
this.next();
|
||
const e = this.expr();
|
||
this.expectSym(t.v === '(' ? ')' : ']', 'to close the bracket');
|
||
return e;
|
||
}
|
||
if (t.v === '{') { this.next(); const e = this.expr(); this.expectSym('}', 'to close the group'); return e; }
|
||
if (t.v === '|') { this.next(); const e = this.expr(); this.expectSym('|', 'to close the absolute value'); return { k: 'abs', e }; }
|
||
throw new ParseError(`Did not expect ${this.describe(t)} here.`, t.pos);
|
||
}
|
||
if (t.t === 'cmd') return this.command();
|
||
throw new ParseError('The equation ends too early.', t.pos);
|
||
}
|
||
|
||
private callArgs(fn: string, pos: number): Expr {
|
||
this.expectSym('(', 'after the function name');
|
||
const args: Expr[] = [];
|
||
if (!this.isSym(')')) { args.push(this.expr()); while (this.isSym(',')) { this.next(); args.push(this.expr()); } }
|
||
this.expectSym(')', 'to close the arguments');
|
||
return { k: 'call', fn, args, pos };
|
||
}
|
||
|
||
private group(what: string): Expr {
|
||
this.expectSym('{', what);
|
||
const e = this.expr();
|
||
this.expectSym('}', 'to close the group');
|
||
return e;
|
||
}
|
||
|
||
private command(): Expr {
|
||
const t = this.next();
|
||
const c = t.v;
|
||
if (REFUSED[c]) throw new ParseError(REFUSED[c], t.pos, 'matho-refused');
|
||
if (GREEK[c]) return { k: 'var', name: '\\' + c, decor: null, sub: null, subText: null, pos: t.pos };
|
||
if (c === 'pi') return { k: 'pi' };
|
||
if (c === 'frac' || c === 'dfrac' || c === 'tfrac') { const n = this.group('for the top of the fraction'); const d = this.group('for the bottom of the fraction'); return { k: 'bin', op: '/', l: n, r: d }; }
|
||
if (c === 'sqrt') {
|
||
let n: Expr | null = null;
|
||
if (this.isSym('[')) { this.next(); n = this.expr(); this.expectSym(']', 'to close the root index'); }
|
||
return { k: 'sqrt', e: this.group('for what the root is taken of'), n };
|
||
}
|
||
if (c === 'binom' || c === 'dbinom' || c === 'tbinom') return { k: 'binom', n: this.group('for the top of the binomial'), m: this.group('for the bottom of the binomial') };
|
||
if (c === 'bar' || c === 'overline' || c === 'hat' || c === 'vec' || c === 'tilde' || c === 'mathbf' || c === 'boldsymbol') {
|
||
const decor: Decor = c === 'overline' ? 'bar' : c === 'mathbf' || c === 'boldsymbol' ? 'bold' : (c as Decor);
|
||
const inner = this.group(`for what \\${c} applies to`);
|
||
if (inner.k !== 'var' || inner.sub) throw new ParseError(`\\${c} goes over a single name: \\${c}{x}.`, t.pos, 'matho-decor');
|
||
return { ...inner, decor };
|
||
}
|
||
if (c === 'sum' || c === 'prod') return this.bigOp(c, t.pos);
|
||
if (c === 'lfloor') { const e = this.expr(); if (!this.isCmd('rfloor')) throw new ParseError('\\lfloor needs a matching \\rfloor.', this.tok.pos); this.next(); return { k: 'floor', e }; }
|
||
if (c === 'lceil') { const e = this.expr(); if (!this.isCmd('rceil')) throw new ParseError('\\lceil needs a matching \\rceil.', this.tok.pos); this.next(); return { k: 'ceil', e }; }
|
||
if (c === 'lvert') { const e = this.expr(); if (!this.isCmd('rvert')) throw new ParseError('\\lvert needs a matching \\rvert.', this.tok.pos); this.next(); return { k: 'abs', e }; }
|
||
if (c === 'lVert' || c === '|') {
|
||
this.normOpen++;
|
||
const e = this.expr();
|
||
this.normOpen--;
|
||
if (!(this.isCmd('rVert') || this.isCmd('|'))) throw new ParseError('\\lVert (or \\|) needs a matching \\rVert (or \\|).', this.tok.pos);
|
||
this.next();
|
||
return { k: 'norm', e };
|
||
}
|
||
if (c === 'langle') { const l = this.expr(); this.expectSym(',', 'between the two vectors of the inner product'); const r = this.expr(); if (!this.isCmd('rangle')) throw new ParseError('\\langle needs a matching \\rangle.', this.tok.pos); this.next(); return { k: 'dot', l, r }; }
|
||
if (c === 'log' && this.isSym('_')) {
|
||
this.next();
|
||
const base = this.scriptArg();
|
||
const arg = this.funcArg();
|
||
if (base.k === 'num' && base.v === '10') return { k: 'call', fn: 'LOG10', args: [arg], pos: t.pos };
|
||
if (base.k === 'num' && base.v === '2') return { k: 'call', fn: 'LOG2', args: [arg], pos: t.pos };
|
||
if (base.k === 'var' && base.name === 'e' && !base.sub) return { k: 'call', fn: 'LOG', args: [arg], pos: t.pos };
|
||
return { k: 'bin', op: '/', l: { k: 'call', fn: 'LOG', args: [arg], pos: t.pos }, r: { k: 'call', fn: 'LOG', args: [base], pos: t.pos } };
|
||
}
|
||
if (FUNCS[c]) {
|
||
if (this.isSym('^')) {
|
||
// \sin^2 x
|
||
this.next();
|
||
const p = this.scriptArg();
|
||
const arg = this.funcArg();
|
||
return { k: 'pow', b: { k: 'call', fn: FUNCS[c], args: [arg], pos: t.pos }, e: p };
|
||
}
|
||
if (c === 'max' || c === 'min') {
|
||
this.expectSym('(', `after \\${c}`);
|
||
const args = [this.expr()];
|
||
while (this.isSym(',')) { this.next(); args.push(this.expr()); }
|
||
this.expectSym(')', `to close \\${c}`);
|
||
return { k: 'call', fn: FUNCS[c], args, pos: t.pos };
|
||
}
|
||
return { k: 'call', fn: FUNCS[c], args: [this.funcArg()], pos: t.pos };
|
||
}
|
||
if (c === 'begin') throw new ParseError('\\begin{cases} can only be the whole right-hand side of an equation.', t.pos, 'matho-cases-place');
|
||
if (c === 'mathbb' || c === 'mathcal') { const inner = this.group(`for \\${c}`); return inner; }
|
||
throw new ParseError(`\\${c} is not a command this reader knows. It knows arithmetic, \\frac, \\sqrt, powers, e^x, |x|, \\lfloor \\rfloor, \\sum, \\prod, \\binom, n!, \\sin \\cos \\tan \\exp \\ln \\log, \\max \\min, \\vec and \\|v\\|, \\begin{cases}, and Greek letters.`, t.pos, 'matho-command');
|
||
}
|
||
|
||
/** What a function such as \sin applies to: a bracketed argument, or the next power. */
|
||
private funcArg(): Expr {
|
||
if (this.isSym('(')) { this.next(); const e = this.expr(); this.expectSym(')', 'to close the argument'); return e; }
|
||
if (this.isSym('{')) return this.group('');
|
||
return this.postfix();
|
||
}
|
||
|
||
private bigOp(op: 'sum' | 'prod', pos: number): Expr {
|
||
let idx: string | null = null;
|
||
let lo: Expr | null = null;
|
||
let hi: Expr | null = null;
|
||
let over: Expr | null = null;
|
||
for (let k = 0; k < 2; k++) {
|
||
if (this.isSym('_')) {
|
||
this.next();
|
||
const braced = this.isSym('{');
|
||
if (braced) this.next();
|
||
const v = this.tok;
|
||
if (v.t !== 'id' && !(v.t === 'cmd' && GREEK[v.v])) throw new ParseError(`\\${op} needs an index letter under it: \\${op}_{i=0}^{n-1}.`, v.pos, 'matho-sum-index');
|
||
this.next();
|
||
idx = v.t === 'id' ? v.v : '\\' + v.v;
|
||
if (this.isSym('=')) { this.next(); lo = this.expr(); }
|
||
else if (this.isCmd('in')) { this.next(); over = this.expr(); }
|
||
if (braced) this.expectSym('}', 'to close the index');
|
||
} else if (this.isSym('^')) {
|
||
this.next();
|
||
hi = this.scriptArg();
|
||
}
|
||
}
|
||
if (!idx) throw new ParseError(`\\${op} needs an index letter under it: \\${op}_{i=0}^{n-1}.`, pos, 'matho-sum-index');
|
||
if (hi && !lo && !over) throw new ParseError(`\\${op}_{${idx}} has an upper bound but no lower one: write \\${op}_{${idx}=0}^{...}.`, pos, 'matho-sum-bounds');
|
||
const body = this.term();
|
||
return { k: 'big', op, idx, lo, hi, over, body, pos };
|
||
}
|
||
}
|
||
|
||
function isVector(e: Expr): boolean { return e.k === 'var' && (e.decor === 'vec' || e.decor === 'bold') && !e.sub; }
|
||
|
||
/* ------------------------------------------------------------------ */
|
||
/* Lowering */
|
||
/* ------------------------------------------------------------------ */
|
||
|
||
type PType = 'INTEGER' | 'FLOAT' | 'LIST OF FLOAT' | 'LIST OF LIST OF FLOAT';
|
||
|
||
interface Param { key: string; name: string; type: PType; roles: Set<'index' | 'bound' | 'list' | 'matrix' | 'vector' | 'plain'> }
|
||
|
||
interface Def { key: string; fnName: string; params: Param[]; }
|
||
|
||
const RESERVED = new Set(reservedNames().map((r) => r.term.toUpperCase()));
|
||
|
||
/** A variable's key: its base name plus decoration plus a non-index subscript. */
|
||
function varKey(v: Extract<Expr, { k: 'var' }>, indexSub: boolean): string {
|
||
return `${v.decor ?? ''}:${v.name}:${indexSub ? '' : (v.subText ?? '')}`;
|
||
}
|
||
|
||
function baseWord(name: string): string {
|
||
if (name.startsWith('\\')) return GREEK[name.slice(1)] ?? name.slice(1);
|
||
if (name.length === 1) return name.toUpperCase();
|
||
return name.split(/[_\s]+/).map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join('');
|
||
}
|
||
|
||
/** `x` → X, `\sigma` → Sigma, `\bar{x}` → XBar, `v_{max}` → VMax, `x_0` → X0. */
|
||
function pseudoName(v: Extract<Expr, { k: 'var' }>, indexSub: boolean): string {
|
||
let s = baseWord(v.name);
|
||
if (v.decor === 'bar') s += 'Bar';
|
||
else if (v.decor === 'hat') s += 'Hat';
|
||
else if (v.decor === 'tilde') s += 'Tilde';
|
||
if (!indexSub && v.subText) {
|
||
const sub = v.subText.replace(/^\\/, '');
|
||
s += GREEK[sub] ?? (sub.charAt(0).toUpperCase() + sub.slice(1));
|
||
}
|
||
if (!/^[A-Za-z_]\w*$/.test(s)) s = s.replace(/\W/g, '_');
|
||
if (RESERVED.has(s.toUpperCase())) s += 'Val';
|
||
return s;
|
||
}
|
||
|
||
/** The function name an equation's left side gives: `f` → F, `\bar{x}` → X_BAR, `E_k` → E_K, `\sigma^2` → SIGMA_SQUARED. */
|
||
function functionName(lhs: Expr): string {
|
||
let squared = false;
|
||
let e = lhs;
|
||
if (e.k === 'pow' && e.e.k === 'num' && e.e.v === '2') { squared = true; e = e.b; }
|
||
if (e.k === 'fact' && e.e.k === 'var') return 'FACTORIAL';
|
||
if (e.k === 'call' && Object.values(FUNCS).includes(e.fn)) return [e.fn, ...e.args.map((a) => (a.k === 'var' ? baseWord(a.name).toUpperCase() : 'OF'))].join('_');
|
||
if (e.k === 'call') e = { k: 'var', name: e.fn, decor: null, sub: null, subText: null, pos: e.pos };
|
||
if (e.k !== 'var') return 'VALUE';
|
||
const parts = [baseWord(e.name).toUpperCase()];
|
||
if (e.decor === 'bar') parts.push('BAR'); else if (e.decor === 'hat') parts.push('HAT'); else if (e.decor === 'tilde') parts.push('TILDE');
|
||
if (e.subText) parts.push((GREEK[e.subText.replace(/^\\/, '')] ?? e.subText).toUpperCase());
|
||
if (squared) parts.push('SQUARED');
|
||
let s = parts.join('_').replace(/\W/g, '_');
|
||
if (RESERVED.has(s)) s += '_FN';
|
||
return s;
|
||
}
|
||
|
||
class Lowerer {
|
||
diagnostics: Diagnostic[] = [];
|
||
defs = new Map<string, Def>();
|
||
lines: string[] = [];
|
||
lineMap: number[] = [];
|
||
outDefs: MathoProgram['defs'] = [];
|
||
|
||
private note(line: number, id: string, message: string): void { this.diagnostics.push({ lang: 'matho', severity: 'note', id, line, message }); }
|
||
private warn(line: number, id: string, message: string): void { this.diagnostics.push({ lang: 'matho', severity: 'warning', id, line, message }); }
|
||
|
||
lower(eqs: Equation[]): void {
|
||
eqs.forEach((eq, k) => {
|
||
const start = this.lines.length;
|
||
this.one(eq, k);
|
||
for (let i = start; i < this.lines.length; i++) this.lineMap[i] = k;
|
||
});
|
||
}
|
||
|
||
private one(eq: Equation, k: number): void {
|
||
const { lhs, rhs } = eq;
|
||
// --- the function's own name and explicit arguments
|
||
let fnName = lhs ? functionName(lhs) : (k === 0 ? 'VALUE' : `VALUE_${k + 1}`);
|
||
const explicit: Array<Extract<Expr, { k: 'var' }>> = [];
|
||
let lhsIndex: Extract<Expr, { k: 'var' }> | null = null;
|
||
const lhsIsBuiltin = lhs?.k === 'call' && Object.values(FUNCS).includes(lhs.fn);
|
||
if (lhs?.k === 'call' && !lhsIsBuiltin) for (const a of lhs.args) { if (a.k !== 'var' || a.sub) { this.warn(eq.line, 'matho-lhs-args', `The arguments on the left side should be plain names, as in f(x, y); ${describeExpr(a)} was ignored.`); continue; } explicit.push(a); }
|
||
if (lhs?.k === 'var' && lhs.sub && lhs.sub.k === 'var' && mentionsSub(rhs, lhs.sub.name)) { lhsIndex = lhs.sub; fnName = functionName({ ...lhs, sub: null, subText: null }); }
|
||
if (lhs && lhs.k !== 'var' && lhs.k !== 'call' && !(lhs.k === 'pow' && lhs.b.k === 'var') && !(lhs.k === 'fact' && lhs.e.k === 'var')) this.warn(eq.line, 'matho-lhs', `The left side ${describeExpr(lhs)} is an expression, not a name, so the function is called ${fnName}.`);
|
||
if (this.defs.has(fnName)) { let n = 2; while (this.defs.has(`${fnName}_${n}`)) n++; fnName = `${fnName}_${n}`; }
|
||
|
||
// --- parameters: explicit first, then the free names of the right side
|
||
const params: Param[] = [];
|
||
const byKey = new Map<string, Param>();
|
||
const bound = new Set<string>(); // index names bound by sums in scope while walking
|
||
const indexNames = new Set<string>(); // every index letter in this equation (sums + lhs index)
|
||
collectIndices(rhs, indexNames);
|
||
if (lhsIndex) indexNames.add(lhsIndex.name);
|
||
const add = (v: Extract<Expr, { k: 'var' }>, role: 'index' | 'bound' | 'plain'): Param => {
|
||
const indexSub = !!v.sub && isIndexExpr(v.sub, indexNames);
|
||
const key = varKey(v, indexSub);
|
||
let p = byKey.get(key);
|
||
if (!p) {
|
||
p = { key, name: pseudoName(v, indexSub), type: 'FLOAT', roles: new Set() };
|
||
let n = 2; const base = p.name; while ([...byKey.values()].some((q) => q.name === p!.name) || p.name.toUpperCase() === fnName) p.name = `${base}${n++}`;
|
||
if (p.name !== base) this.note(eq.line, 'matho-case', `${describeExpr(v)} and ${p.name.toUpperCase() === fnName || base.toUpperCase() === fnName ? `the function ${fnName}` : base} are different symbols in mathematics but would be the same name in pseudo, so ${describeExpr(v)} became ${p.name}.`);
|
||
byKey.set(key, p); params.push(p);
|
||
}
|
||
p.roles.add(role);
|
||
if (indexSub && v.sub) p.roles.add(subDepth(v.sub) === 2 ? 'matrix' : 'list');
|
||
if (isVector(v)) p.roles.add('vector');
|
||
return p;
|
||
};
|
||
if (lhsIndex) { const p = add(lhsIndex, 'index'); p.type = 'INTEGER'; }
|
||
for (const a of explicit) add(a, 'plain');
|
||
const walk = (e: Expr, inBound: boolean, inIndex: boolean): void => {
|
||
switch (e.k) {
|
||
case 'var':
|
||
if (bound.has(e.name) && !e.sub && !e.decor) return;
|
||
if (e.name === 'e' && !e.sub && !e.decor) { /* Euler's e on its own is a name; a note is given at emit time */ }
|
||
if (this.defs.has(functionName(e)) && !e.sub) { // a reference to an earlier equation: its parameters join ours
|
||
const d = this.defs.get(functionName(e))!;
|
||
for (const q of d.params) { const p = byKey.get(q.key) ?? (byKey.set(q.key, { ...q, roles: new Set(q.roles) }).get(q.key)!); if (!params.includes(p)) params.push(p); }
|
||
return;
|
||
}
|
||
add(e, inBound ? 'bound' : inIndex ? 'index' : 'plain');
|
||
if (e.sub) walk(e.sub, false, true);
|
||
return;
|
||
case 'num': case 'pi': return;
|
||
case 'bin': if (e.op === 'mod') { walk(e.l, true, false); walk(e.r, true, false); return; } walk(e.l, inBound, inIndex); walk(e.r, inBound, inIndex); return;
|
||
case 'dot': walk(e.l, false, false); walk(e.r, false, false); return;
|
||
case 'fact': walk(e.e, true, false); return;
|
||
case 'neg': case 'abs': case 'norm': case 'floor': case 'ceil': walk(e.e, inBound, inIndex); return;
|
||
case 'sqrt': walk(e.e, false, false); if (e.n) walk(e.n, true, false); return;
|
||
case 'pow': walk(e.b, inBound, inIndex); walk(e.e, inBound, inIndex); return;
|
||
case 'call':
|
||
if (this.defs.has(functionName({ k: 'var', name: e.fn, decor: null, sub: null, subText: null, pos: e.pos }))) {
|
||
const d = this.defs.get(functionName({ k: 'var', name: e.fn, decor: null, sub: null, subText: null, pos: e.pos }))!;
|
||
// explicit arguments cover the first parameters; the rest join ours
|
||
for (const q of d.params.slice(e.args.length)) { const p = byKey.get(q.key) ?? (byKey.set(q.key, { ...q, roles: new Set(q.roles) }).get(q.key)!); if (!params.includes(p)) params.push(p); }
|
||
}
|
||
for (const a of e.args) walk(a, false, false);
|
||
return;
|
||
case 'binom': walk(e.n, true, false); walk(e.m, true, false); return;
|
||
case 'big':
|
||
if (e.lo) walk(e.lo, true, false);
|
||
if (e.hi) walk(e.hi, true, false);
|
||
if (e.over) { if (e.over.k === 'var' && !e.over.sub) add(e.over, 'plain').roles.add('vector'); else walk(e.over, false, false); }
|
||
{ const was = bound.has(e.idx); bound.add(e.idx); walk(e.body, false, false); if (!was) bound.delete(e.idx); }
|
||
return;
|
||
case 'cases':
|
||
for (const a of e.arms) { walk(a.value, false, false); if (a.cond) walkCond(a.cond); }
|
||
return;
|
||
}
|
||
};
|
||
const walkCond = (c: Cond): void => { if (c.k === 'cmp') { walk(c.l, false, false); walk(c.r, false, false); } else if (c.k === 'not') walkCond(c.c); else { walkCond(c.l); walkCond(c.r); } };
|
||
walk(rhs, false, false);
|
||
|
||
// --- types from roles
|
||
for (const p of params) {
|
||
if (p.roles.has('matrix')) p.type = 'LIST OF LIST OF FLOAT';
|
||
else if (p.roles.has('list') || p.roles.has('vector')) p.type = 'LIST OF FLOAT';
|
||
else if (p.roles.has('index') || p.roles.has('bound')) p.type = 'INTEGER';
|
||
else if (p.type !== 'INTEGER') p.type = 'FLOAT';
|
||
}
|
||
// the explicit arguments keep their order; anything else the right side needs is appended, with a note
|
||
const explicitKeys = new Set(explicit.map((a) => varKey(a, false)));
|
||
if (lhs?.k === 'call' && !lhsIsBuiltin) {
|
||
const extra = params.filter((p) => !explicitKeys.has(p.key) && !lhsIndex);
|
||
if (extra.length) this.note(eq.line, 'matho-extra-params', `${extra.map((p) => p.name).join(', ')} ${extra.length === 1 ? 'is' : 'are'} used on the right side but ${extra.length === 1 ? 'is not an argument' : 'are not arguments'} of ${describeExpr(lhs)}, so ${extra.length === 1 ? 'it became an extra parameter' : 'they became extra parameters'} of ${fnName}.`);
|
||
}
|
||
for (const p of params) {
|
||
if (p.type === 'LIST OF FLOAT' && p.roles.has('list')) this.note(eq.line, 'matho-list', `${p.name} is indexed, so it is a LIST OF FLOAT, and the subscript is a position in it.`);
|
||
if (p.type === 'LIST OF LIST OF FLOAT') this.note(eq.line, 'matho-matrix', `${p.name} has two indices, so it is a LIST OF LIST OF FLOAT: a list of rows.`);
|
||
if (p.type === 'INTEGER' && p.roles.has('bound') && !p.roles.has('index')) this.note(eq.line, 'matho-bound', `${p.name} counts something (it bounds a sum or product, or sits in a binomial coefficient), so it is an INTEGER.`);
|
||
}
|
||
|
||
// --- emit
|
||
const ctx: EmitCtx = { params: byKey, indexNames, defs: this.defs, decls: [], used: new Set(params.map((p) => p.name)), shifts: new Map(), shifted: new Set(), notes: [], line: eq.line, bound: new Map(), loopDepth: 0, hoisted: new Map(), prelude: [] };
|
||
if (lhsIndex) ctx.bound.set(lhsIndex.name, byKey.get(varKey(lhsIndex, false))!.name);
|
||
const body: string[] = [];
|
||
let ret: string[] = [];
|
||
if (rhs.k === 'cases') {
|
||
ret = this.emitCases(rhs, ctx, body, 1);
|
||
} else {
|
||
const expr = emitExpr(rhs, ctx, body, 1);
|
||
ret = [` RETURN ${expr}`];
|
||
}
|
||
const head = `FUNCTION ${fnName}(${params.map((p) => `${p.name} : ${p.type}`).join(', ')}) RETURNS FLOAT`;
|
||
this.lines.push(`// ${eq.source.replace(/\s+/g, ' ').trim()}`);
|
||
this.lines.push(head);
|
||
for (const d of ctx.decls) this.lines.push(` DECLARE ${d.name} : ${d.type}`);
|
||
this.lines.push(...ctx.prelude, ...body, ...ret, 'ENDFUNCTION', '');
|
||
for (const n of ctx.notes) this.note(eq.line, n.id, n.message);
|
||
this.defs.set(fnName, { key: fnName, fnName, params });
|
||
this.outDefs.push({ name: fnName, params: params.map((p) => ({ name: p.name, type: p.type })), line: eq.line });
|
||
}
|
||
|
||
private emitCases(e: Extract<Expr, { k: 'cases' }>, ctx: EmitCtx, out: string[], indent: number): string[] {
|
||
const pad = ' '.repeat(indent);
|
||
const lines: string[] = [];
|
||
e.arms.forEach((arm, i) => {
|
||
const last = i === e.arms.length - 1;
|
||
if (!last) {
|
||
const cond = emitCond(arm.cond!, ctx, out, indent);
|
||
lines.push(`${pad}${i === 0 ? 'IF' : 'ELSEIF'} ${cond} THEN`);
|
||
} else lines.push(`${pad}ELSE`);
|
||
const inner: string[] = [];
|
||
const v = emitExpr(arm.value, ctx, inner, indent + 1);
|
||
lines.push(...inner, `${pad} RETURN ${v}`);
|
||
});
|
||
lines.push(`${pad}ENDIF`);
|
||
return lines;
|
||
}
|
||
}
|
||
|
||
interface EmitCtx {
|
||
params: Map<string, Param>;
|
||
indexNames: Set<string>;
|
||
defs: Map<string, Def>;
|
||
decls: Array<{ name: string; type: string }>;
|
||
used: Set<string>;
|
||
/** index letter → how many to subtract when it indexes a list (a sum from 1) */
|
||
shifts: Map<string, number>;
|
||
/** index letters whose shift was actually used on a subscript */
|
||
shifted: Set<string>;
|
||
notes: Array<{ id: string; message: string }>;
|
||
line: number;
|
||
/** index letter → its pseudo name, while in scope */
|
||
bound: Map<string, string>;
|
||
/** how many sum/product loops the emitter is inside */
|
||
loopDepth: number;
|
||
/** calls of earlier equations that do not change inside a loop, computed once before it: call text → local name */
|
||
hoisted: Map<string, string>;
|
||
/** the lines that compute the hoisted values, placed before the body */
|
||
prelude: string[];
|
||
}
|
||
|
||
function fresh(ctx: EmitCtx, base: string): string {
|
||
const taken = (x: string) => ctx.used.has(x) || RESERVED.has(x.toUpperCase()) || ctx.defs.has(x.toUpperCase());
|
||
let name = base;
|
||
if (taken(name) && !taken(`${base}Val`)) name = `${base}Val`;
|
||
let n = 2;
|
||
while (taken(name)) name = `${base}${n++}`;
|
||
ctx.used.add(name);
|
||
return name;
|
||
}
|
||
|
||
function mentionsSub(e: Expr, idx: string): boolean {
|
||
let found = false;
|
||
const w = (x: Expr): void => {
|
||
if (found) return;
|
||
switch (x.k) {
|
||
case 'var': if (x.sub && subMentions(x.sub, idx)) found = true; return;
|
||
case 'bin': case 'dot': w(x.l); w(x.r); return;
|
||
case 'neg': case 'abs': case 'norm': case 'floor': case 'ceil': case 'fact': w(x.e); return;
|
||
case 'sqrt': w(x.e); return;
|
||
case 'pow': w(x.b); w(x.e); return;
|
||
case 'call': x.args.forEach(w); return;
|
||
case 'binom': w(x.n); w(x.m); return;
|
||
case 'big': w(x.body); return;
|
||
case 'cases': x.arms.forEach((a) => w(a.value)); return;
|
||
default: return;
|
||
}
|
||
};
|
||
w(e);
|
||
return found;
|
||
}
|
||
|
||
function subMentions(sub: Expr, idx: string): boolean {
|
||
switch (sub.k) {
|
||
case 'var': return !sub.sub && sub.name === idx;
|
||
case 'bin': return subMentions(sub.l, idx) || subMentions(sub.r, idx);
|
||
case 'neg': return subMentions(sub.e, idx);
|
||
default: return false;
|
||
}
|
||
}
|
||
|
||
function collectIndices(e: Expr, out: Set<string>): void {
|
||
switch (e.k) {
|
||
case 'big': out.add(e.idx); collectIndices(e.body, out); if (e.lo) collectIndices(e.lo, out); if (e.hi) collectIndices(e.hi, out); return;
|
||
case 'bin': case 'dot': collectIndices(e.l, out); collectIndices(e.r, out); return;
|
||
case 'neg': case 'abs': case 'norm': case 'floor': case 'ceil': case 'fact': collectIndices(e.e, out); return;
|
||
case 'sqrt': collectIndices(e.e, out); return;
|
||
case 'pow': collectIndices(e.b, out); collectIndices(e.e, out); return;
|
||
case 'call': e.args.forEach((a) => collectIndices(a, out)); return;
|
||
case 'binom': collectIndices(e.n, out); collectIndices(e.m, out); return;
|
||
case 'cases': e.arms.forEach((a) => collectIndices(a.value, out)); return;
|
||
case 'var': if (e.sub) collectIndices(e.sub, out); return;
|
||
default: return;
|
||
}
|
||
}
|
||
|
||
/** Is a subscript a position (built from index letters and numbers) rather than a label such as max or 0? */
|
||
function isIndexExpr(e: Expr, indexNames: Set<string>): boolean {
|
||
switch (e.k) {
|
||
case 'var': return !e.sub && !e.decor && indexNames.has(e.name);
|
||
case 'num': return false;
|
||
case 'bin': return (e.op === '+' || e.op === '-' || e.op === '*') && (isIndexExpr(e.l, indexNames) || isIndexExpr(e.r, indexNames)) && (isIndexExpr(e.l, indexNames) || e.l.k === 'num') && (isIndexExpr(e.r, indexNames) || e.r.k === 'num');
|
||
case 'neg': return isIndexExpr(e.e, indexNames);
|
||
default: return false;
|
||
}
|
||
}
|
||
|
||
/** `a_{ij}` is two indices when the subscript is two index letters juxtaposed. */
|
||
function subDepth(sub: Expr): number {
|
||
if (sub.k === 'bin' && sub.op === '*' && sub.l.k === 'var' && sub.r.k === 'var' && !sub.l.sub && !sub.r.sub) return 2;
|
||
return 1;
|
||
}
|
||
|
||
function describeExpr(e: Expr): string {
|
||
switch (e.k) {
|
||
case 'num': return e.v;
|
||
case 'var': return (e.decor ? `\\${e.decor}{${e.name}}` : e.name) + (e.subText ? `_{${e.subText}}` : e.sub ? '_{...}' : '');
|
||
case 'call': return `${e.fn}(${e.args.map(describeExpr).join(', ')})`;
|
||
case 'pow': return `${describeExpr(e.b)}^{${describeExpr(e.e)}}`;
|
||
case 'bin': return `${describeExpr(e.l)} ${e.op} ${describeExpr(e.r)}`;
|
||
default: return `a ${e.k}`;
|
||
}
|
||
}
|
||
|
||
const PREC: Record<string, number> = { '+': 6, '-': 6, '*': 7, '/': 7, mod: 7, '^': 9 };
|
||
|
||
function emitExpr(e: Expr, ctx: EmitCtx, out: string[], indent: number): string {
|
||
const pad = ' '.repeat(indent);
|
||
const wrapIf = (s: string, child: Expr, parentPrec: number, right = false): string => {
|
||
if (child.k === 'bin') { const p = PREC[child.op]; if (p < parentPrec || (p === parentPrec && right)) return `(${s})`; }
|
||
if (child.k === 'neg' && parentPrec >= 7) return `(${s})`;
|
||
return s;
|
||
};
|
||
switch (e.k) {
|
||
case 'num': return e.v;
|
||
case 'pi': return 'PI()';
|
||
case 'var': {
|
||
if (!e.sub && !e.decor && ctx.bound.has(e.name)) return ctx.bound.get(e.name)!;
|
||
const fnName = functionName(e);
|
||
if (!e.sub && ctx.defs.has(fnName)) { const d = ctx.defs.get(fnName)!; return hoist(ctx, `${d.fnName}(${d.params.map((p) => ctx.params.get(p.key)?.name ?? p.name).join(', ')})`, pseudoName(e, false)); }
|
||
const indexSub = !!e.sub && isIndexExpr(e.sub, ctx.indexNames);
|
||
const p = ctx.params.get(varKey(e, indexSub));
|
||
const name = p ? p.name : pseudoName(e, indexSub);
|
||
if (e.name === 'e' && !e.sub && !e.decor && !ctx.notes.some((n) => n.id === 'matho-euler')) ctx.notes.push({ id: 'matho-euler', message: 'A plain e is a name here (E), not Euler\'s number; write e^{x} for EXP(x), or EXP(1) for the constant.' });
|
||
if (!indexSub || !e.sub) return name;
|
||
if (subDepth(e.sub) === 2 && e.sub.k === 'bin') return `${name}[${emitIndex(e.sub.l, ctx, out, indent)}][${emitIndex(e.sub.r, ctx, out, indent)}]`;
|
||
return `${name}[${emitIndex(e.sub, ctx, out, indent)}]`;
|
||
}
|
||
case 'bin': {
|
||
const p = PREC[e.op];
|
||
const l = wrapIf(emitExpr(e.l, ctx, out, indent), e.l, p);
|
||
const r = wrapIf(emitExpr(e.r, ctx, out, indent), e.r, p, true);
|
||
return `${l} ${e.op === 'mod' ? 'MOD' : e.op} ${r}`;
|
||
}
|
||
case 'dot': return `DOT(${emitExpr(e.l, ctx, out, indent)}, ${emitExpr(e.r, ctx, out, indent)})`;
|
||
case 'neg': { const s = emitExpr(e.e, ctx, out, indent); return `-${e.e.k === 'bin' ? `(${s})` : s}`; }
|
||
case 'pow': {
|
||
const b = emitExpr(e.b, ctx, out, indent);
|
||
const x = emitExpr(e.e, ctx, out, indent);
|
||
const bs = e.b.k === 'bin' || e.b.k === 'neg' || e.b.k === 'pow' ? `(${b})` : b;
|
||
if (e.e.k === 'bin' && e.e.op === '/' && e.e.l.k === 'num' && e.e.l.v === '1' && e.e.r.k === 'num' && e.e.r.v === '2') return `SQRT(${b})`;
|
||
const xs = e.e.k === 'bin' || e.e.k === 'neg' ? `(${x})` : x;
|
||
return `${bs} ^ ${xs}`;
|
||
}
|
||
case 'call': {
|
||
const fnName = functionName({ k: 'var', name: e.fn, decor: null, sub: null, subText: null, pos: e.pos });
|
||
if (ctx.defs.has(fnName)) {
|
||
const d = ctx.defs.get(fnName)!;
|
||
const args = e.args.map((a) => emitExpr(a, ctx, out, indent));
|
||
const rest = d.params.slice(e.args.length).map((p) => ctx.params.get(p.key)?.name ?? p.name);
|
||
const text = `${d.fnName}(${[...args, ...rest].join(', ')})`;
|
||
const usesIndex = [...ctx.bound.values()].some((n) => new RegExp(`\\b${n}\\b`).test(args.join(',')));
|
||
return usesIndex ? text : hoist(ctx, text, baseWord(e.fn));
|
||
}
|
||
if (e.fn === 'ATAN') return `ATAN2(${emitExpr(e.args[0], ctx, out, indent)}, 1)`;
|
||
return `${e.fn}(${e.args.map((a) => emitExpr(a, ctx, out, indent)).join(', ')})`;
|
||
}
|
||
case 'sqrt': {
|
||
const x = emitExpr(e.e, ctx, out, indent);
|
||
if (!e.n) return `SQRT(${x})`;
|
||
return `${e.e.k === 'bin' || e.e.k === 'neg' ? `(${x})` : x} ^ (1 / ${emitExpr(e.n, ctx, out, indent)})`;
|
||
}
|
||
case 'abs': return `ABS(${emitExpr(e.e, ctx, out, indent)})`;
|
||
case 'norm': return `NORM_OF(${emitExpr(e.e, ctx, out, indent)})`;
|
||
case 'floor': return `FLOOR(${emitExpr(e.e, ctx, out, indent)})`;
|
||
case 'ceil': return `CEIL(${emitExpr(e.e, ctx, out, indent)})`;
|
||
case 'binom': return `CHOOSE(${emitExpr(e.n, ctx, out, indent)}, ${emitExpr(e.m, ctx, out, indent)})`;
|
||
case 'fact': {
|
||
// n! as a product loop
|
||
const n = emitExpr(e.e, ctx, out, indent);
|
||
const acc = fresh(ctx, 'Factorial');
|
||
const k = fresh(ctx, 'K');
|
||
ctx.decls.push({ name: acc, type: 'FLOAT' });
|
||
out.push(`${pad}${acc} <- 1.0`, `${pad}FOR ${k} <- 2 TO ${e.e.k === 'num' || e.e.k === 'var' ? n : `(${n})`} + 1`, `${pad} ${acc} <- ${acc} * ${k}`, `${pad}NEXT ${k}`);
|
||
if (!ctx.notes.some((x) => x.id === 'matho-factorial')) ctx.notes.push({ id: 'matho-factorial', message: `${describeExpr(e.e)}! became a product loop, ${acc}, run before the RETURN.` });
|
||
return acc;
|
||
}
|
||
case 'big': {
|
||
const isSum = e.op === 'sum';
|
||
const acc = fresh(ctx, isSum ? 'Total' : 'Product');
|
||
ctx.decls.push({ name: acc, type: 'FLOAT' });
|
||
const idxName = fresh(ctx, baseWord(e.idx));
|
||
out.push(`${pad}${acc} <- ${isSum ? '0.0' : '1.0'}`);
|
||
const inner: string[] = [];
|
||
let shift = 0;
|
||
if (e.over) {
|
||
out.push(`${pad}FOR EACH ${idxName} IN ${emitExpr(e.over, ctx, out, indent)}`);
|
||
} else {
|
||
const lo = e.lo ? emitExpr(e.lo, ctx, out, indent) : '0';
|
||
let hi: string;
|
||
if (!e.hi) {
|
||
// no upper bound: the length of the list the index walks
|
||
const list = listIndexedBy(e.body, e.idx, ctx);
|
||
if (!list) throw new ParseError(`\\${e.op}_{${e.idx}} has no upper bound and ${e.idx} indexes no list, so the reader cannot tell how many passes to make. Write \\${e.op}_{${e.idx}=0}^{n-1}.`, e.pos, 'matho-sum-bounds');
|
||
hi = `LENGTH(${list})`;
|
||
ctx.notes.push({ id: 'matho-sum-length', message: `\\${e.op}_{${e.idx}} has no bounds, so the loop runs over every position of ${list}.` });
|
||
} else hi = inclusiveToExclusive(e.hi, ctx, out, indent);
|
||
if (e.lo && e.lo.k === 'num' && e.lo.v === '1' && e.hi && (e.hi.k === 'var' || (e.hi.k === 'bin' && e.hi.op !== '-'))) shift = 1;
|
||
out.push(`${pad}FOR ${idxName} <- ${lo} TO ${hi}`);
|
||
}
|
||
const wasBound = ctx.bound.get(e.idx);
|
||
const wasShift = ctx.shifts.get(e.idx);
|
||
ctx.bound.set(e.idx, idxName);
|
||
if (shift) ctx.shifts.set(e.idx, shift); else ctx.shifts.delete(e.idx);
|
||
ctx.loopDepth++;
|
||
const body = emitExpr(e.body, ctx, inner, indent + 1);
|
||
ctx.loopDepth--;
|
||
if (shift && ctx.shifted.has(e.idx)) ctx.notes.push({ id: 'matho-one-based', message: `The ${isSum ? 'sum' : 'product'} runs from ${e.idx} = 1, and lists in pseudo start at position 0, so a subscript ${e.idx} became a position ${idxName} - 1.` });
|
||
ctx.shifted.delete(e.idx);
|
||
if (wasBound === undefined) ctx.bound.delete(e.idx); else ctx.bound.set(e.idx, wasBound);
|
||
if (wasShift === undefined) ctx.shifts.delete(e.idx); else ctx.shifts.set(e.idx, wasShift);
|
||
const needsParens = e.body.k === 'bin' && (isSum ? e.body.op === '+' || e.body.op === '-' : e.body.op !== '*');
|
||
out.push(...inner, `${pad} ${acc} <- ${acc} ${isSum ? '+' : '*'} ${needsParens ? `(${body})` : body}`, `${pad}NEXT ${idxName}`);
|
||
return acc;
|
||
}
|
||
case 'cases': throw new ParseError('\\begin{cases} can only be the whole right-hand side of an equation.', 0, 'matho-cases-place');
|
||
}
|
||
}
|
||
|
||
/** Inside a loop, a value that does not change from pass to pass is computed once, before the loop. */
|
||
function hoist(ctx: EmitCtx, callText: string, nameHint: string): string {
|
||
if (ctx.loopDepth === 0) return callText;
|
||
const have = ctx.hoisted.get(callText);
|
||
if (have) return have;
|
||
const local = fresh(ctx, nameHint);
|
||
ctx.decls.push({ name: local, type: 'FLOAT' });
|
||
ctx.prelude.push(` ${local} <- ${callText}`);
|
||
ctx.hoisted.set(callText, local);
|
||
ctx.notes.push({ id: 'matho-hoist', message: `${callText} does not change from one pass of the loop to the next, so it is computed once, as ${local}, before the loop.` });
|
||
return local;
|
||
}
|
||
|
||
/** A subscript as a list position, with the 1-based shift applied to bare index letters. */
|
||
function emitIndex(e: Expr, ctx: EmitCtx, out: string[], indent: number): string {
|
||
const s = emitExpr(e, ctx, out, indent);
|
||
if (e.k === 'var' && !e.sub && ctx.shifts.get(e.name)) { ctx.shifted.add(e.name); return `${s} - ${ctx.shifts.get(e.name)}`; }
|
||
if (e.k === 'bin') {
|
||
// i+1 with a shift of 1 is just I
|
||
const shiftOf = (x: Expr): number => x.k === 'var' && !x.sub ? ctx.shifts.get(x.name) ?? 0 : 0;
|
||
const sh = shiftOf(e.l) || shiftOf(e.r);
|
||
if (sh) {
|
||
ctx.shifted.add(e.l.k === 'var' && shiftOf(e.l) ? e.l.name : (e.r as any).name);
|
||
if (e.op === '+' && e.r.k === 'num' && Number(e.r.v) === sh) return emitExpr(e.l, ctx, out, indent);
|
||
return `${s} - ${sh}`;
|
||
}
|
||
}
|
||
return s;
|
||
}
|
||
|
||
/** The upper bound of a sum is inclusive; FOR ... TO is exclusive. `n-1` → N, `n` → N + 1, `5` → 6. */
|
||
function inclusiveToExclusive(hi: Expr, ctx: EmitCtx, out: string[], indent: number): string {
|
||
if (hi.k === 'bin' && hi.op === '-' && hi.r.k === 'num' && hi.r.v === '1') return emitExpr(hi.l, ctx, out, indent);
|
||
if (hi.k === 'num' && /^\d+$/.test(hi.v)) return String(Number(hi.v) + 1);
|
||
const s = emitExpr(hi, ctx, out, indent);
|
||
return hi.k === 'bin' ? `${s} + 1` : `${s} + 1`;
|
||
}
|
||
|
||
function listIndexedBy(body: Expr, idx: string, ctx: EmitCtx): string | null {
|
||
let found: string | null = null;
|
||
const w = (e: Expr): void => {
|
||
if (found) return;
|
||
switch (e.k) {
|
||
case 'var': if (e.sub && e.sub.k === 'var' && e.sub.name === idx) { const p = ctx.params.get(varKey(e, true)); found = p ? p.name : pseudoName(e, true); } return;
|
||
case 'bin': case 'dot': w(e.l); w(e.r); return;
|
||
case 'neg': case 'abs': case 'norm': case 'floor': case 'ceil': case 'fact': case 'sqrt': w(e.e); return;
|
||
case 'pow': w(e.b); w(e.e); return;
|
||
case 'call': e.args.forEach(w); return;
|
||
case 'big': w(e.body); return;
|
||
default: return;
|
||
}
|
||
};
|
||
w(body);
|
||
return found;
|
||
}
|
||
|
||
function emitCond(c: Cond, ctx: EmitCtx, out: string[], indent: number): string {
|
||
switch (c.k) {
|
||
case 'cmp': return `${emitExpr(c.l, ctx, out, indent)} ${c.op} ${emitExpr(c.r, ctx, out, indent)}`;
|
||
case 'and': return `${emitCond(c.l, ctx, out, indent)} AND ${emitCond(c.r, ctx, out, indent)}`;
|
||
case 'or': return `(${emitCond(c.l, ctx, out, indent)}) OR (${emitCond(c.r, ctx, out, indent)})`;
|
||
case 'not': return `NOT (${emitCond(c.c, ctx, out, indent)})`;
|
||
}
|
||
}
|
||
|
||
/* ------------------------------------------------------------------ */
|
||
/* Splitting a document into equations */
|
||
/* ------------------------------------------------------------------ */
|
||
|
||
const WRAPPERS = /\\\[|\\\]|\\begin\{(?:equation|align|gather|eqnarray|math|displaymath)\*?\}|\\end\{(?:equation|align|gather|eqnarray|math|displaymath)\*?\}|\$\$|\\nonumber|\\notag/g;
|
||
|
||
/** Lines that belong together: an equation continues while braces or a cases block are open, or the next line starts with an operator or `=`. */
|
||
export function splitEquations(src: string): Array<{ line: number; text: string }> {
|
||
const lines = src.split('\n');
|
||
const out: Array<{ line: number; text: string }> = [];
|
||
let cur: { line: number; text: string } | null = null;
|
||
let depth = 0;
|
||
let inCases = 0;
|
||
const flush = () => { if (cur && cur.text.trim()) out.push(cur); cur = null; };
|
||
lines.forEach((raw, i) => {
|
||
let line = raw.replace(/%.*$/, '').replace(WRAPPERS, ' ');
|
||
if (!line.trim()) { if (depth <= 0 && inCases <= 0) flush(); return; }
|
||
// an unclosed brace keeps the equation open, unless the next line plainly starts a new one (a fresh `=` outside any cases)
|
||
const freshEquation = depth > 0 && inCases <= 0 && /^[^=&]*[A-Za-z}]\s*=/.test(line) && !/^\s*(?:&\s*)?=/.test(line);
|
||
const continues = !freshEquation && (depth > 0 || inCases > 0 || /^\s*(?:&\s*)?(?:=|\+|-|\\cdot|\\times)/.test(line) || (cur && /(?:=|\+|-|\\cdot|\\times|\\frac|\\left\(|\{)\s*$/.test(cur.text)));
|
||
if (freshEquation) depth = 0;
|
||
if (!continues) flush();
|
||
if (inCases <= 0 && !/\\begin\{cases\}/.test(line)) line = line.replace(/&/g, ' '); // alignment marks outside cases
|
||
if (!cur) cur = { line: i + 1, text: line };
|
||
else cur.text += '\n' + line;
|
||
for (const ch of line) { if (ch === '{') depth++; else if (ch === '}') depth--; }
|
||
inCases += (line.match(/\\begin\{cases\}/g) || []).length - (line.match(/\\end\{cases\}/g) || []).length;
|
||
});
|
||
flush();
|
||
// a trailing \\ ends an equation in align; drop it
|
||
for (const e of out) e.text = e.text.replace(/\\\\\s*$/, '').replace(/^\s*&/, '');
|
||
return out;
|
||
}
|
||
|
||
/* ------------------------------------------------------------------ */
|
||
/* The service */
|
||
/* ------------------------------------------------------------------ */
|
||
|
||
export function parseMatho(source: string): Parsed<MathoProgram> {
|
||
const diagnostics: Diagnostic[] = [];
|
||
const equations: Equation[] = [];
|
||
const known = new Set<string>();
|
||
for (const chunk of splitEquations(source)) {
|
||
const { toks, error } = tokenize(chunk.text);
|
||
if (error) diagnostics.push({ lang: 'matho', severity: 'error', id: 'matho-character', line: chunk.line, message: error });
|
||
const p = new Parser(toks, known);
|
||
try {
|
||
const eq = p.equation();
|
||
equations.push({ line: chunk.line, source: chunk.text, lhs: eq.lhs, rhs: eq.rhs });
|
||
if (eq.lhs?.k === 'call') known.add(eq.lhs.fn);
|
||
if (eq.lhs?.k === 'var' && !eq.lhs.sub && !eq.lhs.decor && eq.lhs.name.length === 1) known.add(eq.lhs.name);
|
||
} catch (e: any) {
|
||
if (e instanceof ParseError) {
|
||
const before = chunk.text.slice(0, e.pos);
|
||
const line = chunk.line + (before.match(/\n/g) || []).length;
|
||
const col = before.length - before.lastIndexOf('\n');
|
||
diagnostics.push({ lang: 'matho', severity: 'error', id: e.id, line, col, message: e.message, guide: e.id === 'matho-refused' ? 'matho-what-a-program-can-be' : undefined });
|
||
} else throw e;
|
||
}
|
||
}
|
||
const low = new Lowerer();
|
||
try {
|
||
low.lower(equations);
|
||
} catch (e: any) {
|
||
if (e instanceof ParseError) {
|
||
const eq = equations[Math.min(low.outDefs.length, equations.length - 1)];
|
||
diagnostics.push({ lang: 'matho', severity: 'error', id: e.id, line: eq ? eq.line : 1, message: e.message });
|
||
return { program: { equations, pseudo: '', lineMap: [], defs: [] }, diagnostics, ok: false };
|
||
}
|
||
throw e;
|
||
}
|
||
diagnostics.push(...low.diagnostics);
|
||
const pseudo = low.lines.join('\n').replace(/\n+$/, '\n');
|
||
// a translation that does not read as pseudo is a bug in the reader; surface it rather than hide it
|
||
const st = studyOf(pseudo);
|
||
for (const d of st.diagnostics.filter((x) => x.severity === 'error')) {
|
||
const k = low.lineMap[d.line - 1] ?? 0;
|
||
diagnostics.push({ lang: 'matho', severity: 'error', id: 'matho-pseudo', line: equations[k]?.line ?? 1, message: `The pseudo this became does not read (line ${d.line}: ${d.message}). The translation is too literal here; open the pseudo pane and finish it by hand.` });
|
||
}
|
||
return { program: { equations, pseudo, lineMap: low.lineMap, defs: low.outDefs }, diagnostics, ok: !diagnostics.some((d) => d.severity === 'error') && equations.length > 0 };
|
||
}
|
||
|
||
export function lowerMatho(parsed: Parsed<MathoProgram>, target: string): Lowered | null {
|
||
const p = parsed.program;
|
||
if (!p || !p.pseudo || target !== 'pseudo') return null;
|
||
return {
|
||
target: 'pseudo',
|
||
source: p.pseudo,
|
||
map: p.lineMap.map((k, i) => ({ from: p.equations[k]?.line ?? 0, to: i + 1 })).filter((m) => m.from > 0),
|
||
notes: [],
|
||
};
|
||
}
|
||
|
||
/** A call that fits the last function: lists of four values, n = 4, indices 1, other numbers 2.0. */
|
||
export function defaultCallMatho(parsed: Parsed<MathoProgram>): string | null {
|
||
const p = parsed.program;
|
||
if (!p || !p.defs.length) return null;
|
||
const d = p.defs[p.defs.length - 1];
|
||
const sample = (q: { name: string; type: string }): string => {
|
||
if (q.type === 'LIST OF FLOAT') return '[1.0, 2.0, 3.0, 4.0]';
|
||
if (q.type === 'LIST OF LIST OF FLOAT') return '[[1.0, 2.0], [3.0, 4.0]]';
|
||
if (q.type === 'INTEGER') return /^(N|M|Len|Count)/.test(q.name) ? '4' : '1';
|
||
return /^(X|T|P|Q|Prob|Theta|Alpha)\d*$/.test(q.name) ? '0.5' : '2.0';
|
||
};
|
||
// when a matrix is passed, its bounds are 2
|
||
const hasMatrix = d.params.some((q) => q.type === 'LIST OF LIST OF FLOAT');
|
||
return `${d.name}(${d.params.map((q) => (hasMatrix && q.type === 'INTEGER' ? '2' : sample(q))).join(', ')})`;
|
||
}
|
||
|
||
export function runMatho(parsed: Parsed<MathoProgram>, options: RunOptions): Trace {
|
||
const low = lowerMatho(parsed, 'pseudo');
|
||
if (!low) throw new Error('The equations do not translate yet; see the problems view.');
|
||
const st = studyOf(low.source);
|
||
if (!st.ok) throw new Error('The pseudo these equations became has an error: ' + (st.diagnostics.find((d) => d.severity === 'error')?.message || ''));
|
||
const tr = runPseudo(st, { ...options, call: options.call ?? defaultCallMatho(parsed) ?? pseudoDefaultCall(st.program) });
|
||
const toMatho = new Map(low.map.map((m) => [m.to, m.from]));
|
||
const hits = new Map<number, number>();
|
||
for (const [line, n] of tr.hits) { const e = toMatho.get(line); if (e) hits.set(e, (hits.get(e) || 0) + n); }
|
||
return {
|
||
...tr,
|
||
lang: 'matho',
|
||
hits,
|
||
steps: tr.steps.map((s: any) => ({ ...s, pseudoLine: s.line, line: toMatho.get(s.line) ?? s.line })),
|
||
error: tr.error ? { ...tr.error, line: tr.error.line != null ? toMatho.get(tr.error.line) ?? tr.error.line : null } : null,
|
||
};
|
||
}
|
||
|
||
export function tokenizeMatho(line: string): Array<{ text: string; cls: string }> {
|
||
const out: Array<{ text: string; cls: string }> = [];
|
||
const re = /(%.*$)|(\\[A-Za-z]+|\\.)|(\d+(?:\.\d+)?)|([A-Za-z])|([{}])|(\s+)|(.)/g;
|
||
let m: RegExpExecArray | null;
|
||
while ((m = re.exec(line))) {
|
||
const [text, c, cmd, n, w, br] = m;
|
||
let cls = '';
|
||
if (c) cls = 'tok-comment';
|
||
else if (cmd) cls = REFUSED[cmd.slice(1)] ? 'tok-error' : GREEK[cmd.slice(1)] ? 'tok-name' : 'tok-keyword';
|
||
else if (n) cls = 'tok-number';
|
||
else if (w) cls = 'tok-name';
|
||
else if (br) cls = 'tok-punct';
|
||
out.push({ text, cls });
|
||
if (c) break;
|
||
}
|
||
return out;
|
||
}
|
||
|
||
export const mathoService: LanguageService<MathoProgram> = {
|
||
id: 'matho',
|
||
name: 'matho',
|
||
parse: parseMatho,
|
||
run: runMatho,
|
||
lower: lowerMatho,
|
||
defaultCall: defaultCallMatho,
|
||
tokenize: tokenizeMatho,
|
||
};
|