/** * Pseudo to Mojo 1.1.0: the one translator in the sync model that only goes one way. * * This is an app-owned port of the Mojo half of src/engines/pseudo/translate.js * (the engine file is left untouched; this fork is where the app's Mojo is * decided). What changed from the engine's version: * * - it writes `def main()` itself, from the program's own lines or from the * call the student is running (COUNT_BASE('A', "GATTACA") becomes * print(count_base("A", "GATTACA"))); * - `raises` is inferred, transitively, so the result passes the checker; * - imports come from the harvested standard-library index; * - it never writes a construct the interpreter declines when a verified * spelling exists (a fixed-length ARRAY is an append loop, not List(length=)); * - it never invents Mojo. What has no faithful spelling (a statistic the * standard library lacks, a blank to fill in) becomes * `raise Error("not translated: ...")` with a diagnostic, never a value * that looks right and is not; * - FORALL, EXISTS, SUM, set-builder, KEYS, SORTED, SPLIT, FILL and the * other built-ins with no one-line spelling become a short loop written * just before the statement that uses them. * * The translation stays literal on purpose (`var n: Int` then `n = ...`, * `total = total + 1`, an index loop over codepoints). Idiomatic rewrites are * left to the optional Mojo helper under src/mojo/, so a student can see the * difference. Nothing here may import that helper. */ import { studyPseudo as study } from '../services/pseudo-study.js'; import { sourceOf, parseExpression } from '../engines/pseudo/parse.js'; import { walkStatements, walkExpr, statementExprs, rootName } from '../engines/pseudo/analyze.js'; import { toDiagnostic } from '../services/pseudo.js'; import { MOJO_STD_EXPORTS } from '../mojo/generated/stdlib.js'; import type { Diagnostic } from '../common/types.js'; import type { LineMap } from './types.js'; import { GENERATED_HEADER } from './header.js'; export { GENERATED_HEADER }; export interface MojoOptions { /** The call to run, such as COUNT_BASE('A', "GATTACA"); becomes the body of `main` when the pseudo has no lines of its own. */ call?: string | null; /** Internal: write the definitions only, no imports and no main (a library routine). */ defsOnly?: boolean; /** Internal: the library routines' Mojo, written before the program's own definitions. */ libraryBlocks?: Array<{ id: string; lines: string[]; imports: string[] }>; } export interface MojoResult { source: string; map: LineMap; notes: Diagnostic[] } /** Every standard-library name the translator can write, with the module it comes from. A test holds each to the harvested index. */ export const USED_STDLIB: Readonly> = { Set: 'std.collections', sort: 'std.builtin.sort', sqrt: 'std.math', exp: 'std.math', log: 'std.math', log2: 'std.math', log10: 'std.math', sin: 'std.math', cos: 'std.math', tan: 'std.math', atan2: 'std.math', pi: 'std.math', floor: 'std.math', ceil: 'std.math', comb: 'std.math', asin: 'std.math', acos: 'std.math', atan: 'std.math', sinh: 'std.math', cosh: 'std.math', tanh: 'std.math', hypot: 'std.math', cbrt: 'std.math', expm1: 'std.math', log1p: 'std.math', trunc: 'std.math', copysign: 'std.math', fma: 'std.math', isnan: 'std.math', isinf: 'std.math', isfinite: 'std.math', inf: 'std.math', nan: 'std.math', ulp: 'std.math', nextafter: 'std.math', gcd: 'std.math', lcm: 'std.math', random_float64: 'std.random', random_si64: 'std.random', }; /** Names the translator writes that Mojo provides without an import. */ export const USED_PRELUDE: readonly string[] = ['Int', 'Float64', 'String', 'Bool', 'List', 'Dict', 'print', 'range', 'len', 'min', 'max', 'abs', 'round', 'input', 'atol', 'atof', 'chr', 'ord', 'Error', 'Int8', 'Int16', 'Int32', 'Int64', 'UInt8', 'UInt16', 'UInt32', 'UInt64', 'Float32', 'DType']; /** The Mojo type for a pseudo number type: INT8 is Int8, FLOAT32 is Float32, INTEGER is Int, FLOAT and FLOAT64 are Float64. */ export function mojoNumber(t: any): string | null { if (!t) return null; if (t.k === 'INTEGER') return t.bits ? `${t.signed ? 'Int' : 'UInt'}${t.bits}` : 'Int'; if (t.k === 'FLOAT') return t.bits === 32 ? 'Float32' : 'Float64'; return null; } const isSized = (t: any): boolean => !!t && ((t.k === 'INTEGER' && !!t.bits) || (t.k === 'FLOAT' && t.bits === 32)); const up = (s: unknown): string => String(s).toUpperCase(); /** GCCount -> gc_count, MaxOf -> max_of, I -> i, COUNT_BASE -> count_base. */ export function snake(name: string): string { if (/^[A-Z0-9_]+$/.test(name)) return name.toLowerCase(); return name .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2') .replace(/([a-z0-9])([A-Z])/g, '$1_$2') .toLowerCase(); } /** Words a pseudo name must not become in Mojo: keywords, builtins the translation itself calls, and imported names. */ const RESERVED = new Set([ 'def', 'fn', 'struct', 'trait', 'comptime', 'alias', 'var', 'ref', 'mut', 'out', 'read', 'owned', 'inout', 'deinit', 'raises', 'raise', 'try', 'except', 'finally', 'with', 'as', 'import', 'from', 'and', 'or', 'not', 'in', 'is', 'if', 'elif', 'else', 'for', 'while', 'break', 'continue', 'return', 'pass', 'del', 'assert', 'class', 'lambda', 'global', 'yield', 'match', 'case', 'self', 'len', 'min', 'max', 'abs', 'round', 'print', 'range', 'input', 'sum', 'type', 'list', 'str', 'int', 'float', 'set', 'dict', 'chr', 'ord', 'pow', 'sort', 'sorted', 'copy', 'main', 'atol', 'atof', 'floor', 'ceil', 'sqrt', 'exp', 'log', 'log2', 'log10', 'sin', 'cos', 'tan', 'atan2', 'pi', 'comb', 'true', 'false', 'none', 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'float32', 'float64', 'dtype', ...Object.keys(USED_STDLIB), ]); const COLLECTIONS = new Set(['LIST', 'ARRAY', 'HASHMAP', 'SET', 'RECORD']); const isCollection = (t: any): boolean => !!t && COLLECTIONS.has(t.k); /** A statement or expression with no faithful Mojo spelling. Caught at the statement, which becomes a visible `raise`. */ class Gap extends Error { } interface Line { text: string; from: number | null } interface Pending { d: number; text: string } interface Ctx { sub: any | null; owner: string } /** An expression made only of numeric literals: Mojo folds these at compile time. */ function litOnly(e: any): boolean { if (!e) return false; if (e.kind === 'Num') return true; if (e.kind === 'Unary') return e.op !== 'NOT' && litOnly(e.arg); if (e.kind === 'Binary') return ['+', '-', '*'].includes(e.op) && litOnly(e.left) && litOnly(e.right); return false; } function hasFloatLit(e: any): boolean { if (!e) return false; if (e.kind === 'Num') return !!e.float || /[.e]/.test(String(e.value)); return hasFloatLit(e.arg) || hasFloatLit(e.left) || hasFloatLit(e.right); } function key(name: string): string { return up(name); } /* ------------------------------------------------------------------ */ /* Which routines raise */ /* ------------------------------------------------------------------ */ /** Does a statement list contain something that raises in Mojo? `raising` is the set of subroutines already known to. */ function bodyRaises(body: any[], raising: Set): boolean { let hit = false; const exprHit = (e: any) => { walkExpr(e, (x: any) => { if (hit) return false; if (x.kind === 'Index' && x.target?.type?.k === 'HASHMAP') hit = true; if (x.kind === 'CallExpr') { if (!x.builtin && raising.has(key(x.name))) hit = true; if (x.builtin === 'TO_INTEGER' || x.builtin === 'TO_FLOAT') hit = true; } return undefined; }); }; walkStatements(body, (s: any) => { if (hit) return false; if (s.kind === 'Input' || s.kind === 'Assert' || s.kind === 'BlankStmt') hit = true; if (s.kind === 'Call' && raising.has(key(s.name))) hit = true; if (s.kind === 'BuiltinCall' && s.name === 'DELETE') hit = true; for (const e of statementExprs(s)) exprHit(e); return undefined; }); return hit; } function inferRaising(program: any, seed: Iterable): Set { const raising = new Set(seed); let changed = true; while (changed) { changed = false; for (const sub of program.subs.values()) { const k = key(sub.name); if (!raising.has(k) && bodyRaises(sub.node.body, raising)) { raising.add(k); changed = true; } } } return raising; } /* ------------------------------------------------------------------ */ /* The translator */ /* ------------------------------------------------------------------ */ class Gen { out: Line[] = []; pending: Pending[] = []; notes = new Map(); gaps: Diagnostic[] = []; gapOwners = new Set(); imports = new Set(); names = new Map(); commentsByLine = new Map(); usedComments = new Set(); hoistOK = true; temp = 0; curLine = 1; srcLines: string[]; varParams = new Map(); /** The type an empty list literal takes from where it is going (an assignment's target, a parameter, a return type). */ hint: any = null; temps = new Set(); /** Set while translating a call's own text (the argument literals), which has no type annotations. */ constructor(private program: any, private source: string, private raising: Set, private opts: MojoOptions) { for (const c of program.comments || []) this.commentsByLine.set(c.line, c); this.srcLines = source.split('\n'); for (const sub of [...program.subs.values(), ...(program.libs?.values() ?? [])]) { const changed = this.changedIn(sub.node.body); const assigned = this.assignedIn(sub.node.body); this.varParams.set(key(sub.name), sub.params.map((p: any) => p.mode !== 'BYREF' && ((changed.has(up(p.name)) && isCollection(p.type)) || assigned.has(up(p.name))))); } } /* ---------- bookkeeping ---------- */ changedIn(body: any[]): Set { const out = new Set(); walkStatements(body, (x: any) => { if (x.kind === 'BuiltinCall' && x.args[0]) { const r = rootName(x.args[0]); if (r) out.add(up(r)); } if ((x.kind === 'Assign' || x.kind === 'Input') && x.target.kind !== 'Name') { const r = rootName(x.target); if (r) out.add(up(r)); } if (x.kind === 'Call') x.args.forEach((a: any, i: number) => { if (x.sub?.params?.[i]?.mode === 'BYREF' && a.kind !== 'Name') { const r = rootName(a); if (r) out.add(up(r)); } }); return undefined; }); return out; } /** Names a body gives a new value to directly (X <- ..., INPUT X, a FOR counter). */ assignedIn(body: any[]): Set { const out = new Set(); walkStatements(body, (x: any) => { if ((x.kind === 'Assign' || x.kind === 'Input') && x.target.kind === 'Name') out.add(up(x.target.name)); if (x.kind === 'For' || x.kind === 'ForEach') out.add(up(x.var)); return undefined; }); return out; } withHint(t: any, fn: () => T): T { const saved = this.hint; this.hint = t; try { return fn(); } finally { this.hint = saved; } } note(id: string, text: string): void { if (!this.notes.has(id)) this.notes.set(id, { lang: 'mojo', severity: 'note', id: `translate-${id}`, line: this.curLine, message: text }); } gap(why: string): never { throw new Gap(why); } need(name: string): void { this.imports.add(name); } name(n: string): string { const k = up(n); const have = this.names.get(k); if (have) return have; let s = snake(n); if (RESERVED.has(s)) s = `${s}_`; this.names.set(k, s); return s; } constName(n: string): string { return snake(n).toUpperCase(); } fresh(stem: string): string { const t = `_${stem}${++this.temp}`; this.temps.add(t); return t; } flush(indent: number, from: number | null): void { for (const h of this.pending) this.out.push({ text: `${' '.repeat(indent + h.d)}${h.text}`, from }); this.pending = []; } emit(indent: number, text: string, from: number | null): void { this.flush(indent, from); this.out.push({ text: text ? `${' '.repeat(indent)}${text}` : '', from }); } hoist(lines: Array<[number, string]>): void { if (!this.hoistOK) this.gap('a computed value inside a condition that is checked again each time'); for (const [d, text] of lines) this.pending.push({ d, text }); } leadingComments(line: number, indent: number): void { for (const [l, c] of this.commentsByLine) { if (l >= line || this.usedComments.has(l) || !c.standalone) continue; this.usedComments.add(l); this.emit(indent, `# ${c.text}`, l); } } trailing(line: number): string { const c = this.commentsByLine.get(line); if (!c || c.standalone || this.usedComments.has(line)) return ''; this.usedComments.add(line); return ` # ${c.text}`; } /* ---------- types and defaults ---------- */ type(t: any): string { if (!t) this.gap('a value whose type is not known'); switch (t.k) { case 'INTEGER': case 'FLOAT': return mojoNumber(t)!; case 'CHAR': case 'STRING': return 'String'; case 'BOOL': return 'Bool'; case 'ARRAY': if (t.dims === 2) return `List[List[${this.type(t.of)}]]`; return `List[${this.type(t.of)}]`; case 'LIST': return `List[${this.type(t.of)}]`; case 'HASHMAP': return `Dict[${this.type(t.key)}, ${this.type(t.val)}]`; case 'SET': this.need('Set'); return `Set[${this.type(t.of)}]`; case 'RECORD': return t.name; default: return this.gap(`a value of kind ${t.k}`); } } /** What a declared-but-unset value holds: the notation's collections start empty, a record starts with every field at its default. */ defaultOf(t: any): string { switch (t?.k) { case 'INTEGER': return '0'; case 'FLOAT': return '0.0'; case 'BOOL': return 'False'; case 'CHAR': case 'STRING': return 'String("")'; case 'LIST': case 'HASHMAP': case 'SET': return `${this.type(t)}()`; case 'RECORD': return `${t.name}(${t.fieldNames.map((f: string) => this.defaultOf(t.fields.get(up(f)))).join(', ')})`; default: return this.gap('a value with no default'); } } /** A value that would share storage with its source, copied: M <- L gives M its own list. */ copied(e: any, text: string): string { if (!isCollection(e.type) || !(['Name', 'Field', 'Index'].includes(e.kind) || this.temps.has(text))) return text; this.note('copy', 'In the notation, assigning or returning a collection gives a separate copy. Mojo spells that .copy(), because Mojo tracks which piece of code owns a value.'); return `${text}.copy()`; } /** * A value going where another number type belongs, converted as the notation converts it: an INTEGER into a * FLOAT becomes Float64(...) (Float32(...) for a FLOAT32), an INTEGER into a sized type becomes Int32(...), a * FLOAT32 into a FLOAT becomes Float64(...). A literal needs nothing: Mojo gives it the type it lands in. */ widened(target: any, e: any, text: string): string { if (!target || !e?.type || e.kind === 'Num') return text; const want = mojoNumber(target); const have = mojoNumber(e.type); if (!want || !have || want === have) return text; if (isSized(target) && target.k === 'INTEGER') this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.'); return `${want}(${text})`; } /* ---------- program ---------- */ run(): MojoResult { const p = this.program; for (const c of p.constants.values()) { this.curLine = c.line; this.leadingComments(c.line, 0); this.names.set(up(c.name), this.constName(c.name)); this.guarded(null, c.line, () => this.emit(0, `comptime ${this.constName(c.name)} = ${this.expr(c.node.value)}${this.trailing(c.line)}`, c.line)); } for (const d of p.defines.values()) { this.curLine = d.line; this.leadingComments(d.line, 0); this.guarded(null, d.line, () => { const vals = d.node.values.map((v: any) => this.expr(v)).join(', '); this.emit(0, `def ${this.name(d.name)}() -> ${this.type(d.type)}:${this.trailing(d.line)}`, d.line); this.emit(1, `return ${this.type(d.type)}(${vals})`, d.line); }); this.note('define', 'A DEFINE set becomes a small function that builds the Set, because Mojo has no global variables; the code calls it where the pseudo names the set.'); this.emit(0, '', null); this.emit(0, '', null); } for (const t of p.types.values()) { this.curLine = t.node.line; this.leadingComments(t.node.line, 0); this.emit(0, '@fieldwise_init', t.node.line); this.emit(0, `struct ${t.name}(Copyable, Movable):`, t.node.line); this.note('struct', 'A record becomes a struct with fields. The traits a struct declares (Copyable, Movable) change between Mojo versions.'); for (const f of t.fieldNames) this.guarded(null, t.node.line, () => this.emit(1, `var ${this.name(f)}: ${this.type(t.fields.get(up(f)))}`, t.node.line)); this.emit(0, '', null); this.emit(0, '', null); } for (const b of this.opts.libraryBlocks || []) { this.emit(0, `# From the Programming+ library: ${b.id} (verified in pplus-library; generated from its pseudo)`, null); for (const l of b.lines) this.emit(0, l, null); for (const n of b.imports) this.imports.add(n); this.emit(0, '', null); this.emit(0, '', null); } for (const sub of p.subs.values()) { this.subroutine(sub); this.emit(0, '', null); this.emit(0, '', null); } if (this.opts.defsOnly) return this.assemble(); this.mainFunction(); this.leadingComments(Infinity, 0); return this.assemble(); } assemble(): MojoResult { const imports = new Map(); for (const n of this.imports) { const mod = USED_STDLIB[n]; if (!mod) continue; if (!imports.has(mod)) imports.set(mod, []); imports.get(mod)!.push(n); } const head: Line[] = [{ text: GENERATED_HEADER, from: null }]; for (const [mod, names] of [...imports].sort((a, b) => a[0].localeCompare(b[0]))) head.push({ text: `from ${mod} import ${names.sort().join(', ')}`, from: null }); head.push({ text: '', from: null }); const lines = [...head, ...this.out]; while (lines.length && !lines[lines.length - 1].text.trim()) lines.pop(); return { source: lines.map((l) => l.text).join('\n') + '\n', map: lines.map((l) => l.from), notes: [...this.notes.values(), ...this.gaps] }; } /** Run `fn`; when it finds something with no faithful Mojo, write a visible `raise` in its place. */ guarded(owner: string | null, line: number, fn: () => void, indent = 0): void { const saved = this.pending.length; try { fn(); } catch (err) { if (!(err instanceof Gap)) throw err; this.pending.length = saved; const text = (this.srcLines[line - 1] ?? '').trim(); this.gaps.push({ lang: 'mojo', severity: 'warning', id: 'translate-gap', line, message: `Programming+ could not translate this line into Mojo (${err.message}). The generated code stops here with an error instead of guessing.` }); this.emit(indent, `# pseudo line ${line}: ${text}`, line); this.emit(indent, `raise Error("not translated: pseudo line ${line}")`, line); this.gapOwners.add(owner ?? 'main'); } } subroutine(sub: any): void { const s = sub.node; this.curLine = s.line; this.leadingComments(s.line, 0); const k = key(sub.name); const flags = this.varParams.get(k) || []; let header = ''; try { const params = sub.params.map((p: any, i: number) => { const n = this.name(p.name); if (p.mode === 'BYREF') { this.note('mut', 'BYREF becomes the mut argument convention, which lets the function change the caller\'s variable.'); return `mut ${n}: ${this.type(p.type)}`; } if (flags[i]) { this.note('var-param', 'A parameter the procedure gives a new value to or changes is declared var, so the function owns a copy it may change; for a collection the caller passes .copy().'); return `var ${n}: ${this.type(p.type)}`; } return `${n}: ${this.type(p.type)}`; }).join(', '); const ret = s.kind === 'Function' ? ` -> ${this.type(sub.returns)}` : ''; header = `def ${this.name(sub.name)}(${params})${this.raising.has(k) ? ' raises' : ''}${ret}:`; } catch (err) { if (!(err instanceof Gap)) throw err; header = `def ${this.name(sub.name)}() raises:`; this.gaps.push({ lang: 'mojo', severity: 'warning', id: 'translate-gap', line: s.line, message: `Programming+ could not write this routine's signature in Mojo (${err.message}).` }); this.gapOwners.add(k); this.emit(0, `${header}${this.trailing(s.line)}`, s.line); this.emit(1, `raise Error("not translated: pseudo line ${s.line}")`, s.line); return; } this.emit(0, `${header}${this.trailing(s.line)}`, s.line); for (const r of sub.requires) this.emit(1, `# REQUIRE ${sourceOf(r.expr)}`, r.line); for (const e of sub.ensures) this.emit(1, `# ENSURE ${sourceOf(e.expr)}`, e.line); const before = this.out.length; this.block(s.body, 1, { sub, owner: k }); if (this.out.length === before) this.emit(1, 'pass', s.line); } /** `def main()`: the program's own lines, then the call the student is running. */ mainFunction(): void { const p = this.program; const body = p.main.body; const callText = (this.opts.call || '').trim(); const mainRaises = this.raising.has('MAIN'); if (!body.length && !callText) return; this.emit(0, `def main()${mainRaises ? ' raises' : ''}:`, body[0]?.line ?? null); const before = this.out.length; if (body.length) this.block(body, 1, { sub: null, owner: 'main' }); if (callText) this.callLine(callText, 1); if (this.out.length === before) this.emit(1, 'pass', null); } /** print(f(args)): the arguments are literals in the call's own text, written for the parameter types. */ callLine(text: string, d: number): void { this.guarded('main', 1, () => { const parsed: any = parseExpression(text); if (parsed.diags.some((x: any) => x.severity === 'error') || parsed.expr.kind !== 'CallExpr') this.gap('the call is not a call'); const call = parsed.expr; const sub = this.program.subs.get(up(call.name)); if (!sub || sub.params.length !== call.args.length) this.gap('the call does not match a routine'); const flags = this.varParams.get(key(sub.name)) || []; const args = call.args.map((a: any, i: number) => this.literal(a, sub.params[i].type, flags[i])); const expr = `${this.name(sub.name)}(${args.join(', ')})`; if (sub.kind === 'Function') this.emit(d, `print(${expr})`, null); else this.emit(d, expr, null); }, d); } /** A literal argument, in the Mojo that fits its parameter. */ literal(e: any, t: any, owned = false): string { switch (e.kind) { case 'Num': { const s = String(e.value); return t?.k === 'FLOAT' && !/[.e]/.test(s) ? `${s}.0` : s; } case 'Str': case 'Char': return JSON.stringify(e.value); case 'Bool': return e.value ? 'True' : 'False'; case 'Unary': if (e.op === '-') return `-${this.literal(e.arg, t)}`; break; case 'ListLit': return `${this.type(t)}(${e.items.map((x: any) => this.literal(x, t?.of)).join(', ')})`; case 'SetLit': this.need('Set'); return `${this.type(t)}(${e.items.map((x: any) => this.literal(x, t?.of)).join(', ')})`; default: break; } void owned; return this.gap(`the argument ${sourceOf(e)}`); } block(body: any[], d: number, ctx: Ctx): void { const before = this.out.length; for (const s of body) this.stmt(s, d, ctx); if (this.out.length === before) this.emit(d, 'pass', null); } stmt(s: any, d: number, ctx: Ctx): void { this.curLine = s.line ?? this.curLine; this.leadingComments(s.line, d); this.guarded(ctx.owner, s.line, () => this.stmtBody(s, d, ctx), d); } /* ---------- statements ---------- */ stmtBody(s: any, d: number, ctx: Ctx): void { const tr = () => this.trailing(s.line); switch (s.kind) { case 'Declare': { const t = s.resolved; // DECLARE x : T <- value (one line in the pseudo) is one line in Mojo too: var x: T = value if (s.init && s.names.length === 1 && t.k !== 'ARRAY' && t.k !== 'RECORD') { const a = s.init; const value = this.withHint(a.target.type, () => this.expr(a.value)); const typed = ['LIST', 'HASHMAP', 'SET'].includes(t.k) ? '' : `: ${this.type(t)}`; this.emit(d, `var ${this.name(s.names[0].name)}${typed} = ${this.widened(a.target.type, a.value, this.copied(a.value, value))}${tr()}`, s.line); a.mergedIntoDeclare = true; return; } for (const n of s.names) { const v = this.name(n.name); if (t.k === 'ARRAY') { this.arrayDecl(v, t, d, s.line); continue; } if (['LIST', 'HASHMAP', 'SET'].includes(t.k) || t.k === 'RECORD') this.emit(d, `var ${v} = ${this.defaultOf(t)}${tr()}`, s.line); else this.emit(d, `var ${v}: ${this.type(t)}${tr()}`, s.line); if (t.k === 'RECORD') this.note('record-init', 'A struct in Mojo is created with a value for every field at once; the translation starts every field at its default, and the pseudo\'s field assignments change them afterwards.'); } return; } case 'Assign': { if (s.mergedIntoDeclare) return; const value = this.withHint(s.target.type, () => this.expr(s.value)); this.emit(d, `${this.lhs(s.target)} = ${this.widened(s.target.type, s.value, this.copied(s.value, value))}${tr()}`, s.line); return; } case 'Input': { const t = s.target.type; const read = 'input()'; const conv = t?.k === 'INTEGER' ? `atol(${read})` : t?.k === 'FLOAT' ? `atof(${read})` : t?.k === 'BOOL' ? `${read} == "TRUE"` : read; this.emit(d, `${this.lhs(s.target)} = ${conv}${tr()}`, s.line); return; } case 'Output': { const vals = s.values.map((v: any) => this.outputValue(v)); this.emit(d, `print(${vals.join(', ')}${vals.length > 1 ? ', sep=""' : ''})${tr()}`, s.line); return; } case 'If': { const conds = s.branches.map((b: any, i: number) => { this.hoistOK = i === 0; try { return this.expr(b.cond); } finally { this.hoistOK = true; } }); s.branches.forEach((b: any, i: number) => { this.emit(d, `${i ? 'elif' : 'if'} ${conds[i]}:${i ? this.trailing(b.line) : tr()}`, b.line); this.block(b.body, d + 1, ctx); }); if (s.elseBody) { this.emit(d, 'else:', s.elseLine); this.block(s.elseBody, d + 1, ctx); } return; } case 'Case': { const subj = this.expr(s.subject); const arms = s.arms.map((a: any) => a.labels.map((l: any) => (l.value ? `${subj} == ${this.expr(l.value)}` : `${this.expr(l.low)} <= ${subj} < ${this.expr(l.high)}`)).join(' or ')); s.arms.forEach((a: any, i: number) => { this.emit(d, `${i ? 'elif' : 'if'} ${arms[i]}:`, a.line); this.block(a.body, d + 1, ctx); }); if (s.otherwise) { this.emit(d, s.arms.length ? 'else:' : 'if True:', s.otherwiseLine); this.block(s.otherwise, d + 1, ctx); } this.note('case', 'Mojo has no CASE statement: it becomes a chain of if and elif.'); return; } case 'For': { const i = this.name(s.var); const a = this.expr(s.start); const b = this.expr(s.stop); const r = s.step ? `range(${a}, ${b}, ${this.expr(s.step)})` : a === '0' ? `range(${b})` : `range(${a}, ${b})`; for (const inv of s.invariants || []) this.emit(d, `# INVARIANT ${sourceOf(inv.expr)}`, inv.line); if (s.parallel) this.emit(d, '# PARALLEL FOR: the passes are independent; this translation runs them one after another', s.line); this.emit(d, `for ${i} in ${r}:${tr()}`, s.line); this.block(s.body, d + 1, ctx); return; } case 'ForEach': { const x = this.name(s.var); const it = s.iterable; const coll = this.expr(it); if (it.type?.k === 'STRING') { const k = this.fresh(`${x}_pos`); this.emit(d, `for ${k} in range(${coll}.count_codepoints()):${tr()}`, s.line); this.emit(d + 1, `var ${x} = String(${coll}[codepoint=${k}])`, s.line); } else if (it.type?.k === 'HASHMAP' || it.type?.k === 'SET') { this.emit(d, `for ${x} in ${coll}${it.type.k === 'HASHMAP' ? '.keys()' : ''}:${tr()}`, s.line); this.note('each-order', 'The lab visits SET members and HASHMAP keys smallest first. Mojo visits them in its own order, which the notation never promised; a program whose answer depends on that order is worth rewriting.'); } else if (it.kind === 'Range') { this.emit(d, `for ${x} in range(${this.expr(it.lo)}, ${this.expr(it.hi)}):${tr()}`, s.line); } else { // FOR EACH walks a snapshot, so a body that changes the list does not change the walk const r = rootName(it); const snap = r && this.changedIn(s.body).has(up(r)); if (snap) this.note('each-snapshot', 'FOR EACH walks the collection as it was when the loop began. The body changes it, so the translation walks a copy.'); this.emit(d, `for ${x} in ${snap ? `${coll}.copy()` : coll}:${tr()}`, s.line); } this.block(s.body, d + 1, ctx); return; } case 'While': { this.hoistOK = false; let cond: string; try { cond = this.expr(s.cond); } finally { this.hoistOK = true; } for (const inv of s.invariants || []) this.emit(d, `# INVARIANT ${sourceOf(inv.expr)}`, inv.line); if (s.decreases) this.emit(d, `# DECREASES ${sourceOf(s.decreases.expr)}`, s.decreases.line); this.emit(d, `while ${cond}:${tr()}`, s.line); this.block(s.body, d + 1, ctx); return; } case 'Repeat': { this.hoistOK = false; let cond: string; try { cond = this.expr(s.cond); } finally { this.hoistOK = true; } this.emit(d, `while True:${tr()}`, s.line); this.block(s.body, d + 1, ctx); this.emit(d + 1, `if ${cond}:`, s.endLine); this.emit(d + 2, 'break', s.endLine); this.note('repeat', 'REPEAT ... UNTIL has no direct counterpart: it becomes while True, with a break at the end once the UNTIL condition holds.'); return; } case 'Break': this.emit(d, `break${tr()}`, s.line); return; case 'Continue': this.emit(d, `continue${tr()}`, s.line); return; case 'Return': { if (!s.value) { this.emit(d, `return${tr()}`, s.line); return; } const v = this.copied(s.value, this.withHint(ctx.sub?.returns, () => this.expr(s.value))); this.emit(d, `return ${this.widened(ctx.sub?.returns, s.value, v)}${tr()}`, s.line); return; } case 'Call': { const flags = this.varParams.get(key(s.name)) || []; const args = s.args.map((a: any, i: number) => this.callArg(a, s.sub?.params?.[i], flags[i])); this.emit(d, `${this.name(s.name)}(${args.join(', ')})${tr()}`, s.line); return; } case 'BuiltinCall': this.emit(d, `${this.builtinStmt(s)}${tr()}`, s.line); return; case 'ExprStmt': this.emit(d, `${this.expr(s.expr)}${tr()}`, s.line); return; case 'Assert': { const c = this.expr(s.expr); this.emit(d, `if not (${c}):`, s.line); this.emit(d + 1, `raise Error("ASSERT failed on pseudo line ${s.line}: ${sourceOf(s.expr).replace(/["\\]/g, '')}")`, s.line); return; } case 'BlankStmt': this.gap(`a blank to fill in: <${s.text}>`); return; default: return; } } /** DECLARE A : ARRAY[n] OF T: a list filled to its length, the way the verified programs do it (an append loop, since List(length=) is not run by the interpreter). */ arrayDecl(v: string, t: any, d: number, line: number): void { const dims = (t.dimExprs || []).map((x: any) => this.expr(x)); const fill = this.defaultOf(t.of); if (dims.length === 2) { const row = this.fresh('row'); this.emit(d, `var ${v} = ${this.type(t)}()`, line); this.emit(d, `for _ in range(${dims[0]}):`, line); this.emit(d + 1, `var ${row} = List[${this.type(t.of)}]()`, line); this.emit(d + 1, `for _ in range(${dims[1]}):`, line); this.emit(d + 2, `${row}.append(${fill})`, line); this.emit(d + 1, `${v}.append(${row}^)`, line); } else { this.emit(d, `var ${v} = ${this.type(t)}()`, line); this.emit(d, `for _ in range(${dims[0]}):`, line); this.emit(d + 1, `${v}.append(${fill})`, line); } this.note('array', 'A fixed-length ARRAY becomes a list filled to its full length with an append loop, so every cell holds a value.'); } callArg(a: any, param: any, owned: boolean | undefined): string { const text = this.widened(param?.type, a, this.withHint(param?.type, () => this.expr(a))); return owned ? this.copied(a, text) : text; } builtinStmt(s: any): string { const [a, b, c] = s.args.map((x: any) => this.expr(x)); switch (s.name) { case 'APPEND': return `${a}.append(${b})`; case 'INSERT': return `${a}.insert(${b}, ${c})`; case 'REMOVE': return `_ = ${a}.pop(${b})`; case 'ADD': return `${a}.add(${b})`; case 'DELETE': return `_ = ${a}.pop(${b})`; case 'SORT': this.need('sort'); return `sort(${a})`; default: return this.gap(`the built-in ${s.name}`); } } lhs(t: any): string { return this.expr(t, true); } outputValue(v: any): string { if (v.type?.k === 'BOOL') return `("TRUE" if ${this.expr(v)} else "FALSE")`; return this.expr(v); } /* ---------- expressions ---------- */ /** An iteration source for a quantifier or set builder: a range, or a collection's items. */ domain(dom: any): string { return dom.kind === 'Range' ? `range(${this.expr(dom.lo)}, ${this.expr(dom.hi)})` : this.expr(dom); } expr(e: any, isTarget = false): string { const P = (x: any) => (x && ['Binary', 'Chain', 'Quant', 'Unary'].includes(x.kind) ? `(${this.expr(x)})` : this.expr(x)); switch (e.kind) { case 'Num': { const t = String(e.value); return e.float && !/[.e]/.test(t) ? `${t}.0` : t; } case 'Str': case 'Char': return JSON.stringify(e.value); case 'Bool': return e.value ? 'True' : 'False'; case 'Blank': return this.gap(`a blank to fill in: <${e.text}>`); case 'TypeRef': return e.name === 'INTEGER' ? 'Int' : e.name === 'FLOAT' || e.name === 'FLOAT64' ? 'Float64' : e.name === 'FLOAT32' ? 'Float32' : e.name.startsWith('UINT') ? `UInt${e.name.slice(4)}` : `Int${e.name.slice(3)}`; case 'Name': if (e.sym?.kind === 'const') return this.constName(e.name); if (e.sym?.kind === 'define') return `${this.name(e.name)}()`; if (up(e.name) === 'RESULT' && e.sym?.kind === 'result') return this.gap('RESULT'); return this.name(e.name); case 'Unary': return e.op === 'NOT' ? `not ${P(e.arg)}` : `-${P(e.arg)}`; case 'Binary': { const map: Record = { AND: 'and', OR: 'or', DIV: '//', MOD: '%', '^': '**', '&': '+', IN: 'in' }; if (e.op === '/') this.note('divide', 'In the notation, dividing by zero stops the program. In Mojo a Float64 division by zero gives inf or nan and the program goes on.'); if (e.op === '/' && mojoNumber(e.type) === 'Float32') { const side = (x: any) => (x.kind === 'Num' || mojoNumber(x.type) === 'Float32' ? this.sideExpr(x, e, x === e.left ? 'l' : 'r') : `Float32(${this.expr(x)})`); return `${side(e.left)} / ${side(e.right)}`; } if (e.op === '/') return `Float64(${this.expr(e.left)}) / Float64(${this.expr(e.right)})`; if (isSized(e.type) && e.type.k === 'INTEGER' && ['+', '-', '*', '^', 'DIV', 'MOD'].includes(e.op)) this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.'); if (e.op === 'IN' && e.right.kind === 'Range') return `${this.expr(e.right.lo)} <= ${P(e.left)} < ${this.expr(e.right.hi)}`; if (e.op === '&') { const side = (x: any) => (x.type?.k === 'CHAR' || x.type?.k === 'STRING' ? P(x) : `String(${this.expr(x)})`); return `${side(e.left)} + ${side(e.right)}`; } let left = this.sideExpr(e.left, e, 'l'); // Mojo folds arithmetic on float literals exactly (0.1 + 0.2 == 0.3); the notation computes with Float64, so the left side is made one if (litOnly(e.left) && litOnly(e.right) && (hasFloatLit(e.left) || hasFloatLit(e.right)) && !left.startsWith('Float64(') && ['+', '-', '*', '==', '!=', '<', '<=', '>', '>='].includes(e.op)) left = `Float64(${left})`; return `${left} ${map[e.op] || e.op} ${this.sideExpr(e.right, e, 'r')}`; } case 'Chain': return e.operands.map((o: any, i: number) => (i ? `${e.ops[i - 1] === 'IN' ? 'in' : e.ops[i - 1]} ${P(o)}` : P(o))).join(' '); case 'Index': { const t = this.expr(e.target); if (e.target.type?.k === 'STRING' && !isTarget) return `String(${t}[codepoint=${this.expr(e.indices[0])}])`; if (e.indices.length === 2) return `${t}[${this.expr(e.indices[0])}][${this.expr(e.indices[1])}]`; return `${t}[${this.expr(e.indices[0])}]`; } case 'Field': return `${this.expr(e.target)}.${this.name(e.field)}`; case 'CallExpr': return this.call(e); case 'ListLit': { const t = e.type?.k === 'LIST' && e.type.of?.k !== 'ANY' ? e.type : this.hint?.k === 'LIST' || this.hint?.k === 'ARRAY' ? this.hint : e.type; return `${this.type(t)}(${e.items.map((x: any) => this.withHint(t?.of, () => this.expr(x))).join(', ')})`; } case 'SetLit': this.need('Set'); return `${this.type(e.type?.of?.k !== 'ANY' ? e.type : this.hint)}(${e.items.map((x: any) => this.expr(x)).join(', ')})`; case 'MapLit': return `{${e.entries.map((x: any) => `${this.expr(x.key)}: ${this.expr(x.value)}`).join(', ')}}`; case 'Range': return `range(${this.expr(e.lo)}, ${this.expr(e.hi)})`; case 'SetBuilder': { const v = this.name(e.var); const dom = this.domain(e.domain); const t = this.fresh('set'); this.hoistOK = false; let body: string; try { body = this.expr(e.body); } finally { this.hoistOK = true; } this.hoist([[0, `var ${t} = ${this.type(e.type)}()`], [0, `for ${v} in ${dom}:`], [1, `if ${body}:`], [2, `${t}.add(${v})`]]); this.note('comprehension', 'Mojo has no set comprehension, so a set written by rule becomes a loop, written just before the line that uses it, that fills a Set.'); return t; } case 'Quant': { const v = this.name(e.var); const dom = this.domain(e.domain); const t = this.fresh(e.q === 'FORALL' ? 'all' : e.q === 'EXISTS' ? 'any' : 'sum'); const saved = this.hoistOK; this.hoistOK = false; let body: string; try { body = this.expr(e.body); } finally { this.hoistOK = saved; } if (e.q === 'FORALL') this.hoist([[0, `var ${t} = True`], [0, `for ${v} in ${dom}:`], [1, `if not (${body}):`], [2, `${t} = False`], [2, 'break']]); else if (e.q === 'EXISTS') this.hoist([[0, `var ${t} = False`], [0, `for ${v} in ${dom}:`], [1, `if ${body}:`], [2, `${t} = True`], [2, 'break']]); else this.hoist([[0, `var ${t} = ${e.type?.k === 'FLOAT' ? '0.0' : '0'}`], [0, `for ${v} in ${dom}:`], [1, `${t} += ${body}`]]); this.note('quantifier', 'FORALL, EXISTS, and SUM have no one-line Mojo spelling; they become a loop, written just before the line that uses them, that sets a flag or adds to a running total.'); return t; } default: return this.gap(`a ${e.kind} expression`); } } /** An operand of a binary operator: parenthesized when precedence needs it, and an Int meeting a Float64 is converted. */ sideExpr(x: any, parent: any, side: 'l' | 'r'): string { const BIND: Record = { OR: 1, AND: 2, '==': 4, '!=': 4, '<': 4, '<=': 4, '>': 4, '>=': 4, IN: 4, '&': 5, '+': 6, '-': 6, '*': 7, '/': 7, DIV: 7, MOD: 7, '^': 9 }; let s = this.expr(x); const other = side === 'l' ? parent.right : parent.left; if (['+', '-', '*', '==', '!=', '<', '<=', '>', '>=', 'DIV', 'MOD', '^'].includes(parent.op) && x.kind !== 'Num' && other?.type) { // two sides of different number types meet as the notation has them meet const mine = mojoNumber(x.type); const theirs = mojoNumber(other.type); const compare = ['==', '!=', '<', '<=', '>', '>='].includes(parent.op); if (mine && theirs && mine !== theirs) { if (x.type.k === 'INTEGER' && other.type.k === 'FLOAT') s = `${theirs}(${s})`; else if (x.type.k === 'INTEGER' && other.type.k === 'INTEGER' && !x.type.bits) s = `${theirs}(${s})`; else if (compare && x.type.k === 'FLOAT' && other.type.k === 'FLOAT' && mine === 'Float32') s = `Float64(${s})`; else if (compare && x.type.k === 'INTEGER' && other.type.k === 'INTEGER') s = `Int(${s})`; } } if (x.kind === 'Chain' || x.kind === 'Quant') return `(${s})`; if (x.kind === 'Unary' && x.op === 'NOT' && BIND[parent.op] > 3) return `(${s})`; if (x.kind !== 'Binary') return s; const a = BIND[x.op]; const b = BIND[parent.op]; if (a < b || (a === b && side === 'r' && !(parent.op === x.op && ['+', '*', 'AND', 'OR', '&'].includes(parent.op))) || (a === 4 && b === 4)) return `(${s})`; return s; } /** A value as the Mojo number type T: unchanged when it already is one, or a literal; converted otherwise. */ as(T: string, node: any, text: string): string { if (mojoNumber(node?.type) === T) return text; return `${T}(${text})`; } /** IS_CLOSE's four values, as Float64 unless they are already FLOATs of one precision. */ args4(e: any): string[] { const f32 = e.args.slice(0, 2).every((x: any) => mojoNumber(x.type) === 'Float32'); return e.args.map((x: any) => (x.kind === 'Num' || mojoNumber(x.type) === (f32 ? 'Float32' : 'Float64') ? this.expr(x) : `${f32 ? 'Float32' : 'Float64'}(${this.expr(x)})`)); } /** A built-in that is a short loop: write it before the statement and name its result. */ loopResult(stem: string, type: string, init: string, lines: Array<[number, string]>): string { const t = this.fresh(stem); this.hoist([[0, `var ${t}${type ? `: ${type}` : ''} = ${init}`], ...lines.map(([d, x]): [number, string] => [d, x.split('$T').join(t)])]); return t; } call(e: any): string { if (e.isOld) return this.gap('OLD(...) outside a contract'); if (e.convert) { const to = mojoNumber(e.type)!; if (e.type.k === 'INTEGER') this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.'); const arg = e.args[0]; // a FLOAT literal becomes a Float64 first, so the conversion is from a runtime value, as in the notation return `${to}(${arg.kind === 'Num' && arg.float ? `Float64(${this.expr(arg)})` : this.expr(arg)})`; } if (!e.builtin) { const flags = this.varParams.get(key(e.name)) || []; const args = e.args.map((a: any, i: number) => this.callArg(a, e.sub?.params?.[i], flags[i])); return `${this.name(e.name)}(${args.join(', ')})`; } const args: string[] = e.args.map((a: any) => this.expr(a)); const [a, b, c] = args; const t0 = e.args[0]?.type?.k; const math = (f: string): string => { this.need(f); return `${f}(Float64(${a}))`; }; switch (e.builtin) { case 'LENGTH': return t0 === 'STRING' ? `${a}.count_codepoints()` : `len(${a})`; case 'ROWS': return `len(${a})`; case 'COLUMNS': return `len(${a}[0])`; case 'INT': return `Int(${a})`; case 'ROUND': return `Int(round(${a}))`; case 'ABS': return `abs(${a})`; case 'MIN': case 'MAX': { const mixed = e.type?.k === 'FLOAT' && e.args.length > 1 && e.args.some((x: any) => x.type?.k === 'INTEGER'); return `${e.builtin.toLowerCase()}(${args.map((x, i) => (mixed ? this.widened(e.type, e.args[i], x) : x)).join(', ')})`; } case 'SQRT': return math('sqrt'); case 'EXP': return math('exp'); case 'LOG': return math('log'); case 'LOG2': return math('log2'); case 'LOG10': return math('log10'); case 'SIN': return math('sin'); case 'COS': return math('cos'); case 'TAN': return math('tan'); case 'ATAN2': this.need('atan2'); return `atan2(Float64(${a}), Float64(${b}))`; case 'PI': this.need('pi'); return 'pi'; case 'CHOOSE': this.need('comb'); return `comb(${a}, ${b})`; case 'FLOOR': this.need('floor'); return `Int(floor(${a}))`; case 'CEIL': this.need('ceil'); return `Int(ceil(${a}))`; case 'RAND': this.need('random_float64'); return `random_float64() * ${a}`; case 'RANDINT': this.need('random_si64'); return `Int(random_si64(${a}, ${b} - 1))`; case 'SLICE': if (t0 === 'STRING') { // the verified programs build a piece of a String letter by letter; s[byte=a:b] counts bytes and is not run by the interpreter const k = this.fresh('at'); this.note('slice', 'A piece of a String is built letter by letter, which counts characters the way the notation does.'); return this.loopResult('piece', '', 'String("")', [[0, `for ${k} in range(${b}, ${c}):`], [1, `$T += String(${a}[codepoint=${k}])`]]); } return `${a}[${b}:${c}]`; case 'UPPER': return `${a}.upper()`; case 'LOWER': return `${a}.lower()`; case 'ORD': return `ord(${a})`; case 'CHR': return `chr(${a})`; case 'TO_STRING': return `String(${a})`; case 'TO_INTEGER': return `atol(${a})`; case 'TO_FLOAT': return t0 === 'STRING' ? `atof(${a})` : `Float64(${a})`; case 'REPLACE': return `${a}.replace(${b}, ${c})`; case 'JOIN': return `${b}.join(${a})`; case 'INDEX_OF': if (t0 === 'STRING') return `${a}.find(${b})`; { const i = this.fresh('i'); return this.loopResult('at', '', '-1', [[0, `for ${i} in range(len(${a})):`], [1, `if ${a}[${i}] == ${b}:`], [2, `$T = ${i}`], [2, 'break']]); } case 'SPLIT': { const p = this.fresh('piece'); const parts = this.fresh('parts'); this.hoist([[0, `var ${parts} = List[String]()`], [0, `for ${p} in ${a}.split(${b}):`], [1, `${parts}.append(String(${p}))`]]); return parts; } case 'FILL': { const ty = this.type(e.type?.of ?? e.args[1]?.type); return this.loopResult('fill', '', `List[${ty}]()`, [[0, 'for _ in range(' + a + '):'], [1, `$T.append(${b})`]]); } case 'SUM_OF': { const x = this.fresh('x'); if (isSized(e.type)) return this.loopResult('total', this.type(e.type), '0', [[0, `for ${x} in ${a}:`], [1, `$T += ${x}`]]); const zero = e.type?.k === 'FLOAT' ? '0.0' : '0'; return this.loopResult('total', '', zero, [[0, `for ${x} in ${a}:`], [1, `$T += ${x}`]]); } case 'MEAN_OF': { const x = this.fresh('x'); const sum = this.loopResult('total', '', '0.0', [[0, `for ${x} in ${a}:`], [1, `$T += Float64(${x})`]]); return `(${sum} / Float64(len(${a})))`; } case 'DOT': { const i = this.fresh('i'); if (isSized(e.type)) return this.loopResult('dot', this.type(e.type), '0', [[0, `for ${i} in range(len(${a})):`], [1, `$T += ${a}[${i}] * ${b}[${i}]`]]); return this.loopResult('dot', '', e.type?.k === 'FLOAT' ? '0.0' : '0', [[0, `for ${i} in range(len(${a})):`], [1, `$T += ${a}[${i}] * ${b}[${i}]`]]); } case 'VARIANCE_OF': case 'STDEV_OF': { const x = this.fresh('x'); const m = this.loopResult('mean', '', '0.0', [[0, `for ${x} in ${a}:`], [1, `$T += Float64(${x})`]]); this.hoist([[0, `${m} = ${m} / Float64(len(${a}))`]]); const v = this.loopResult('var', '', '0.0', [[0, `for ${x} in ${a}:`], [1, `$T += (Float64(${x}) - ${m}) * (Float64(${x}) - ${m})`]]); this.hoist([[0, `${v} = ${v} / Float64(len(${a}) - 1)`]]); this.note('statistics', 'The standard library has no variance or standard deviation, so they are written as loops: the sample variance divides by n - 1, as the notation does.'); if (e.builtin === 'VARIANCE_OF') return v; this.need('sqrt'); return `sqrt(${v})`; } case 'NORM_OF': { const x = this.fresh('x'); this.need('sqrt'); const q = this.loopResult('squares', '', '0.0', [[0, `for ${x} in ${a}:`], [1, `$T += Float64(${x}) * Float64(${x})`]]); return `sqrt(${q})`; } case 'MEDIAN_OF': { const x = this.fresh('x'); this.need('sort'); const of = e.args[0].type?.of; const sorted = this.loopResult('sorted', '', `${this.type({ k: 'LIST', of })}()`, [[0, `for ${x} in ${a}:`], [1, `$T.append(${x})`]]); this.hoist([[0, `sort(${sorted})`]]); const mid = this.loopResult('median', '', '0.0', [ [0, `if len(${sorted}) % 2 == 1:`], [1, `$T = Float64(${sorted}[len(${sorted}) // 2])`], [0, 'else:'], [1, `$T = (Float64(${sorted}[len(${sorted}) // 2 - 1]) + Float64(${sorted}[len(${sorted}) // 2])) / 2.0`], ]); return mid; } case 'KEYS': { const k = this.fresh('k'); this.need('sort'); const keys = this.loopResult('keys', '', `${this.type({ k: 'LIST', of: e.args[0].type.key })}()`, [[0, `for ${k} in ${a}.keys():`], [1, `$T.append(${k})`]]); this.hoist([[0, `sort(${keys})`]]); return keys; } case 'SORTED': { const k = this.fresh('x'); this.need('sort'); const of = e.args[0].type?.k === 'SET' ? e.args[0].type.of : e.args[0].type?.of; const items = this.loopResult('sorted', '', `${this.type({ k: 'LIST', of })}()`, [[0, `for ${k} in ${a}:`], [1, `$T.append(${k})`]]); this.hoist([[0, `sort(${items})`]]); return items; } // ---- numerical work case 'ASIN': return math('asin'); case 'ACOS': return math('acos'); case 'ATAN': return math('atan'); case 'SINH': return math('sinh'); case 'COSH': return math('cosh'); case 'TANH': return math('tanh'); case 'CBRT': return math('cbrt'); case 'EXPM1': return math('expm1'); case 'LOG1P': return math('log1p'); case 'HYPOT': this.need('hypot'); return `hypot(Float64(${a}), Float64(${b}))`; case 'POW': return `Float64(${a}) ** Float64(${b})`; case 'COPYSIGN': this.need('copysign'); return `copysign(Float64(${a}), Float64(${b}))`; case 'E': return '2.718281828459045'; case 'TRUNC': this.need('trunc'); return `trunc(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`; case 'SIGN': { const T = this.type(e.type); return `(${T}(1) if ${a} > 0 else (${T}(-1) if ${a} < 0 else ${T}(0)))`; } case 'CLAMP': return `min(max(${a}, ${b}), ${c})`; case 'FMA': { const T = mojoNumber(e.type)!; this.need('fma'); return `fma(${this.as(T, e.args[0], a)}, ${this.as(T, e.args[1], b)}, ${this.as(T, e.args[2], c)})`; } case 'IS_NAN': this.need('isnan'); return `isnan(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`; case 'IS_INF': this.need('isinf'); return `isinf(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`; case 'IS_FINITE': this.need('isfinite'); return `isfinite(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`; case 'INF': this.need('inf'); return 'inf[DType.float64]()'; case 'NAN': this.need('nan'); return 'nan[DType.float64]()'; case 'ULP': { const T = mojoNumber(e.type)!; this.need('ulp'); return `ulp(${this.as(T, e.args[0], a)})`; } case 'NEXT_AFTER': { const T = mojoNumber(e.type)!; this.need('nextafter'); return `nextafter(${this.as(T, e.args[0], a)}, ${this.as(T, e.args[1], b)})`; } case 'IS_CLOSE': { const d = this.args4(e); return `(${d[0]} == ${d[1]} or abs(${d[0]} - ${d[1]}) <= max(${d[2]} * max(abs(${d[0]}), abs(${d[1]})), ${d[3]}))`; } case 'MAX_VALUE': case 'MIN_VALUE': case 'EPSILON': { const T = mojoNumber(e.type)!; const ty = e.type; if (e.builtin === 'EPSILON') return `${T}(${ty.bits === 32 ? '1.1920928955078125e-07' : '2.220446049250313e-16'})`; if (ty.k === 'INTEGER') { const hi = ty.signed ? 2 ** (ty.bits - 1) - 1 : 2 ** ty.bits - 1; const lo = ty.signed ? -(2 ** (ty.bits - 1)) : 0; return `${T}(${e.builtin === 'MAX_VALUE' ? hi : lo})`; } const m = ty.bits === 32 ? '3.4028234663852886e38' : '1.7976931348623157e308'; return `${T}(${e.builtin === 'MAX_VALUE' ? m : `-${m}`})`; } case 'GCD': this.need('gcd'); return `gcd(${a}, ${b})`; case 'LCM': this.need('lcm'); return `lcm(${a}, ${b})`; case 'BIT_AND': return `(${a} & ${b})`; case 'BIT_OR': return `(${a} | ${b})`; case 'BIT_XOR': return `(${a} ^ ${b})`; case 'BIT_NOT': return `~${a}`; case 'SHIFT_LEFT': case 'SHIFT_RIGHT': { const T = this.type(e.type); const n = isSized(e.type) ? `${T}(${b})` : b; if (e.builtin === 'SHIFT_LEFT' && isSized(e.type)) this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.'); return `(${a} ${e.builtin === 'SHIFT_LEFT' ? '<<' : '>>'} ${n})`; } case 'WRAP': return `${this.type(e.type)}(${a})`; default: return this.gap(`the built-in ${e.builtin}, which Mojo's standard library has no function for`); } } } /* ------------------------------------------------------------------ */ /* Entry point */ /* ------------------------------------------------------------------ */ /** * Translate pseudo to Mojo. Returns `unsupported` only when the pseudo does * not parse or check; everything else yields Mojo, with a diagnostic for each * line that could not be translated. */ export function pseudoToMojo(pseudo: string, opts: MojoOptions = {}): MojoResult | { unsupported: Diagnostic[] } { const st: any = study(pseudo, { coach: false }); if (!st.ok || !st.program) { const errors = (st.diags as any[]).filter((d) => d.severity === 'error').map((d) => toDiagnostic(d)); return { unsupported: errors.length ? errors : [{ lang: 'pseudo', severity: 'error', id: 'translate-no-program', line: 1, message: 'The pseudo does not read yet, so there is nothing to translate.' }] }; } const program = st.program; // the library routines the program uses, each once, the ones they use first const libs = libraryOrder(program); const libRaising = new Set(); const libraryBlocks: Array<{ id: string; lines: string[]; imports: string[] }> = []; for (const lib of libs) { const raising = inferRaising(lib.program, libRaising); const gen = new Gen(lib.program, lib.source, raising, { defsOnly: true }); gen.run(); for (const sub of lib.program.subs.values()) if (raising.has(up(sub.name))) libRaising.add(up(sub.name)); const lines = gen.out.map((l) => l.text); while (lines.length && !lines[lines.length - 1].trim()) lines.pop(); libraryBlocks.push({ id: lib.id, lines, imports: [...gen.imports] }); } opts = { ...opts, libraryBlocks }; const callText = (opts.call || '').trim(); const callName = callText ? /^\s*([A-Za-z_][A-Za-z0-9_]*)/.exec(callText)?.[1] : null; // Inference, then translation; a line that could not be translated becomes a raise, so its routine raises too, and the pass is run again. const extra = new Set(libRaising); for (let pass = 0; ; pass++) { const raising = inferRaising(program, extra); if (bodyRaises(program.main.body, raising)) raising.add('MAIN'); if (callName && raising.has(up(callName))) raising.add('MAIN'); for (const k of extra) if (k === 'MAIN') raising.add('MAIN'); const gen = new Gen(program, pseudo, raising, opts); const result = gen.run(); const fresh = [...gen.gapOwners].map(up).filter((k) => !raising.has(k === 'MAIN' ? 'MAIN' : k)); if (!fresh.length || pass >= 3) return result; for (const k of fresh) extra.add(k); } } /** The names of every routine a program calls, anywhere in it. */ function calledNames(program: any): Set { const out = new Set(); const seen = new Set(); const visit = (x: any): void => { if (!x || typeof x !== 'object' || seen.has(x)) return; seen.add(x); if (Array.isArray(x)) { for (const y of x) visit(y); return; } if ((x.kind === 'CallExpr' || x.kind === 'Call') && x.name) out.add(up(x.name)); for (const [k, v] of Object.entries(x)) if (k !== 'sub' && k !== 'sym' && k !== 'type' && k !== 'resolved' && v && typeof v === 'object') visit(v); }; visit(program.main?.body); for (const s of program.subs.values()) { visit(s.node.body); visit(s.requires); visit(s.ensures); } return out; } /** Every library routine a program calls, directly or through another routine, dependencies first, each once. */ function libraryOrder(program: any, out: Map = new Map()): any[] { const called = calledNames(program); for (const sub of program.libs?.values() ?? []) { if (!called.has(up(sub.name))) continue; if (out.has(sub.lib.id)) continue; libraryOrder(sub.lib.program, out); out.set(sub.lib.id, sub.lib); } return [...out.values()]; } /** The set of names a generated source may use from the standard library, for the grounding test. */ export function stdlibNamesUsed(): string[] { return Object.keys(USED_STDLIB); } export const STD_INDEX = MOJO_STD_EXPORTS;