Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-kernel

Source of release 0.1.0

0.1.0 / tools / sources.py

#!/usr/bin/env python3
"""Write src/lower/sources.lucb: the texts `write_root` puts in a snippet root, so a host
needs no luce-kernel or luce-std checkout at run time. They are the lib module's fragments
(src/lib) and luce-std's math and math32, read from ../luce-std. Run it after changing
src/lib or when luce-std's math changes:

    python3 tools/sources.py [--std ../luce-std]
"""
import argparse
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def literal(text):
    out = []
    for ch in text:
        if ch == "\\":
            out.append("\\\\")
        elif ch == '"':
            out.append('\\"')
        elif ch == "\n":
            out.append("\\n")
        elif ch == "\t":
            out.append("\\t")
        elif ch == "\r":
            out.append("\\r")
        else:
            out.append(ch)
    return '"' + "".join(out) + '"'


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--std", default=str(ROOT.parent / "luce-std"))
    args = parser.parse_args()
    files = []
    lib = ROOT / "src" / "lib"
    order = (lib / "ORDER").read_text()
    files.append(("luce_kernel/lib/ORDER", order))
    for name in order.split():
        files.append((f"luce_kernel/lib/{name}", (lib / name).read_text()))
    std = Path(args.std) / "src"
    files.append(("luce_std/math.lucb", (std / "math.lucb").read_text()))
    files.append(("luce_std/math32.lucb", (std / "math32.lucb").read_text()))
    text = ("# Generated by tools/sources.py from src/lib and luce-std; edit those, not this file.\n"
            "# The files `write_root` writes into a snippet root, by path under the root.\n\n"
            f"let source_paths: str[{len(files)}] = [\n")
    text += "".join(f'    "{path}",\n' for path, _ in files)
    text += f"]\n\nlet source_texts: str[{len(files)}] = [\n"
    text += "".join(f"    {literal(body)},\n" for _, body in files)
    text += "]\n"
    (ROOT / "src" / "lower" / "sources.lucb").write_text(text)


if __name__ == "__main__":
    main()