Files
pplus-web/core/scripts/mojo/mojocorpus/godbolt.py
T
alexmandClaude 82e18f7966 Move the engine core into core/, a project of its own (pplus-core)
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
2026-10-05 05:43:58 +00:00

126 lines
5.9 KiB
Python

"""Compiler Explorer, the oracle every corpus item is checked against.
Requests follow Mojo Quest's (src/lib/compile.ts), which its CI runs against
the same compiler id, so a result here means what it means there:
run compile and execute in one request (an "executor request"); the
answer has buildResult.code, didExecute, code, stdout, stderr, timedOut
asm compile only and return the Intel-syntax listing with source lines
both compile with the listing AND execute; whether one request returns
both is itself a probe (probes/godbolt-asm-and-run.mojo)
Every answer is normalised to one shape, `Result`, and kept beside the raw
JSON (minus a listing duplicated in `asm`) so a later reading can change.
"""
from __future__ import annotations
import json
import time
import urllib.error
import urllib.request
from typing import Any
from .common import PipelineError
USER_AGENT = "programming-plus-mojo-corpus/0.1 (+https://pplus.scient.ing; corpus verification, low rate)"
ASM_FILTERS = {"binary": False, "execute": False, "intel": True, "demangle": True, "labels": True,
"libraryCode": False, "directives": True, "commentOnly": True, "trim": True}
def request_body(source: str, mode: str = "run", stdin: str = "", args: str = "") -> dict[str, Any]:
if mode == "run":
options = {"userArguments": args, "executeParameters": {"args": [], "stdin": stdin},
"compilerOptions": {"executorRequest": True, "skipAsm": True},
"filters": {"execute": True}, "tools": [], "libraries": []}
elif mode == "asm":
options = {"userArguments": args, "compilerOptions": {"executorRequest": False, "skipAsm": False},
"filters": dict(ASM_FILTERS), "tools": [], "libraries": []}
elif mode == "both":
options = {"userArguments": args, "executeParameters": {"args": [], "stdin": stdin},
"compilerOptions": {"executorRequest": False, "skipAsm": False},
"filters": {**ASM_FILTERS, "execute": True}, "tools": [], "libraries": []}
else:
raise PipelineError(f"unknown godbolt mode {mode!r}")
return {"source": source, "options": options, "lang": "mojo", "allowStoreCodeDebug": False}
def _lines(arr: Any) -> str:
return "\n".join((x.get("text", "") if isinstance(x, dict) else str(x)) for x in arr) if isinstance(arr, list) else ""
def normalise(mode: str, data: dict[str, Any]) -> dict[str, Any]:
"""One shape for every mode: compiled, buildStderr, executed, exitCode, stdout, stderr, timedOut, asm."""
if mode == "run":
build = data.get("buildResult") or {}
return {
"compiled": build.get("code") == 0,
"buildStderr": _lines(build.get("stderr")),
"executed": bool(data.get("didExecute")),
"exitCode": data.get("code") if isinstance(data.get("code"), int) else None,
"stdout": _lines(data.get("stdout")),
"stderr": _lines(data.get("stderr")),
"timedOut": bool(data.get("timedOut")),
"asm": None,
}
exe = data.get("execResult") or {}
asm = [asm_line(a) for a in data.get("asm") or [] if isinstance(a, dict)]
return {
"compiled": data.get("code") == 0,
"buildStderr": _lines(data.get("stderr")),
"executed": bool(exe.get("didExecute")) if exe else False,
"exitCode": exe.get("code") if isinstance(exe.get("code"), int) else None,
"stdout": _lines(exe.get("stdout")),
"stderr": _lines(exe.get("stderr")),
"timedOut": bool(exe.get("timedOut")),
"asm": asm,
"returnedAsm": bool(asm),
"returnedExec": bool(exe),
}
def asm_line(a: dict[str, Any]) -> dict[str, Any]:
"""One listing line, with where it came from. `file` and `main` keep godbolt's marking of the source
file as given (`source.file`, `source.mainsource`), so the instructions inlined from the standard
library, whose lines are lines of the library's files, can be told from the program's own."""
src = a.get("source") or {}
out: dict[str, Any] = {"text": a.get("text", ""), "line": src.get("line")}
if src:
out["file"] = src.get("file")
if "mainsource" in src:
out["main"] = src.get("mainsource")
return out
def slim_raw(data: dict[str, Any]) -> dict[str, Any]:
return {k: v for k, v in data.items() if k not in ("asm", "popularArguments")}
def post(base: str, compiler: str, body: dict[str, Any], timeout: int = 90, retries: int = 5,
attempts: list[int] | None = None) -> dict[str, Any]:
"""POST and return the answer. `attempts`, if given, receives how many tries it took, so a slow
answer can be told apart from a slow compile (godbolt reports its own compile time separately)."""
url = f"{base}/api/compiler/{urllib.request.quote(compiler, safe='')}/compile"
data = json.dumps(body).encode()
delay = 2.0
for attempt in range(retries + 1):
req = urllib.request.Request(url, data=data, method="POST", headers={
"Content-Type": "application/json", "Accept": "application/json", "User-Agent": USER_AGENT})
if attempts is not None:
attempts[:] = [attempt + 1]
try:
with urllib.request.urlopen(req, timeout=timeout) as r:
return json.loads(r.read().decode("utf-8"))
except urllib.error.HTTPError as e:
if e.code in (429, 500, 502, 503, 504) and attempt < retries:
wait = float(e.headers.get("Retry-After") or delay)
time.sleep(min(wait, 120)); delay *= 2
continue
raise PipelineError(f"godbolt answered HTTP {e.code} for {compiler}") from e
except (urllib.error.URLError, TimeoutError) as e:
if attempt < retries:
time.sleep(delay); delay *= 2
continue
raise PipelineError(f"godbolt unreachable: {e}") from e
raise AssertionError("unreachable")