commit
e092cb029c
252 files changed
+35037
No files matched your search
@@ -0,0 +1,76 @@
|
||||
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
/**
|
||||
* Mojo 1.1.0 and 1.2.0 give every `input()` call a buffered reader of its own. When the input arrives all at once
|
||||
* (a pipe, as on godbolt, where Compile & run sends the input box as the program's stdin), the first call reads
|
||||
* every line into its buffer and keeps the rest, so the second call finds nothing left and raises EOF. Programming+'s
|
||||
* Run view hands out one line per call, as a terminal does (core/docs/trust.md). The Compiler view says so when it
|
||||
* matters: when a program that may call `input()` more than once was sent with input.
|
||||
*
|
||||
* "May call more than once" is decided from the text alone, erring toward saying it: more than one call site, a
|
||||
* call inside a loop (or a comprehension), or a call inside any function other than `main`, which can be called
|
||||
* any number of times. Strings and comments are not calls (core's Mojo lexer reads them). DOM-free.
|
||||
*/
|
||||
import { lex, type Tok } from '#core/mojo/lex.js';
|
||||
|
||||
export interface InputReads {
|
||||
/** The lines with an `input(` call, in order. */
|
||||
lines: number[];
|
||||
/** Why the program may read input more than once, or null when it reads it at most once. */
|
||||
why: 'several' | 'loop' | 'function' | null;
|
||||
}
|
||||
|
||||
const LOOPS = new Set(['for', 'while']);
|
||||
|
||||
export function inputReads(source: string): InputReads {
|
||||
let toks: Tok[];
|
||||
try { toks = lex(source).tokens; } catch { return { lines: [], why: null }; }
|
||||
const lines: number[] = [];
|
||||
let inLoop = false, inHelper = false;
|
||||
// one entry per open block: whether it is a loop body, and whether it is a function other than main
|
||||
const blocks: Array<{ loop: boolean; helper: boolean }> = [];
|
||||
let lineLoop = false, lineHelper = false; // what the logical line now being read opens
|
||||
let atStart = true;
|
||||
const depthLoop = () => blocks.some((b) => b.loop);
|
||||
const depthHelper = () => blocks.some((b) => b.helper);
|
||||
let pending = 0; // calls on the logical line being read, settled at its end
|
||||
const endLine = () => {
|
||||
if (pending && (lineLoop || depthLoop())) inLoop = true;
|
||||
if (pending && (lineHelper || depthHelper())) inHelper = true;
|
||||
pending = 0;
|
||||
};
|
||||
for (let i = 0; i < toks.length; i++) {
|
||||
const t = toks[i];
|
||||
if (t.kind === 'newline' || t.kind === 'eof') { endLine(); atStart = true; continue; }
|
||||
if (t.kind === 'indent') { endLine(); blocks.push({ loop: lineLoop, helper: lineHelper }); lineLoop = false; lineHelper = false; atStart = true; continue; }
|
||||
if (t.kind === 'dedent') { endLine(); blocks.pop(); atStart = true; continue; }
|
||||
if (atStart) { lineLoop = false; lineHelper = false; atStart = false; }
|
||||
if (t.kind !== 'name') continue;
|
||||
if (LOOPS.has(t.text)) lineLoop = true; // a loop statement, or a comprehension on this line
|
||||
if (t.text === 'def' && toks[i + 1]?.kind === 'name' && toks[i + 1].text !== 'main') lineHelper = true;
|
||||
if (t.text === 'input' && toks[i + 1]?.text === '(' && toks[i - 1]?.text !== '.' && toks[i - 1]?.text !== 'def') { lines.push(t.line); pending++; }
|
||||
}
|
||||
endLine();
|
||||
const why = lines.length > 1 ? 'several' : inLoop ? 'loop' : inHelper ? 'function' : null;
|
||||
return { lines, why };
|
||||
}
|
||||
|
||||
/**
|
||||
* The note the Compiler view shows for a program sent with `stdin`, or null when none is needed: no input was
|
||||
* sent, or the program reads it at most once.
|
||||
*/
|
||||
export function inputNote(source: string, stdin: string): string | null {
|
||||
if (!stdin.trim()) return null;
|
||||
const r = inputReads(source);
|
||||
if (!r.why) return null;
|
||||
const where = r.why === 'several' ? `calls input() on lines ${listLines(r.lines)}` : r.why === 'loop' ? `calls input() in a loop (line ${r.lines[0]})` : `calls input() in a function that can run more than once (line ${r.lines[0]})`;
|
||||
return `This program ${where}. In Mojo 1.2.0 each input() call reads through a buffer of its own, and here the input arrives all at once: the first call takes every line, and the next one stops with EOF. That is the compiler's run, not a mistake in your input. The Run view hands out one line per call, as typing at a terminal does.`;
|
||||
}
|
||||
|
||||
function listLines(ns: number[]): string {
|
||||
const u = [...new Set(ns)];
|
||||
if (u.length <= 1) return String(u[0] ?? '');
|
||||
const shown = u.slice(0, 4).map(String);
|
||||
if (u.length > 4) return `${shown.join(', ')} and ${u.length - 4} more`;
|
||||
return `${shown.slice(0, -1).join(', ')} and ${shown[shown.length - 1]}`;
|
||||
}
|
||||
Reference in new issue
Block a user