
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1197 · main · 10 min 35 s
1Étoiles
0Forks
0Issues
| 1 | #!/usr/bin/env python3 |
| 2 | """Write the prelude's impls for `()` and tuples (ADR 0018 Rev. 1). |
| 3 | |
| 4 | Eq, Ord, Show, Debug, Hash and Default, for `()` and tuples of 2 to |
| 5 | MAX_ARITY elements, each element constrained by the same trait. They are |
| 6 | generated because they differ only in arity; edit this script, not the |
| 7 | block in std/prelude.lse. |
| 8 | |
| 9 | scripts/gen-tuple-impls.py rewrite the block |
| 10 | scripts/gen-tuple-impls.py --check fail if the block is out of date |
| 11 | """ |
| 12 | |
| 13 | import pathlib |
| 14 | import subprocess |
| 15 | import sys |
| 16 | import tempfile |
| 17 | |
| 18 | MAX_ARITY = 12 |
| 19 | ROOT = pathlib.Path(__file__).resolve().parent.parent |
| 20 | PRELUDE = ROOT / "std" / "prelude.lse" |
| 21 | BEGIN = "// --- `()` and tuples: generated by scripts/gen-tuple-impls.py, do not edit ---" |
| 22 | END = "// --- end of the generated tuple impls ---" |
| 23 | |
| 24 | |
| 25 | def format_louise(text): |
| 26 | """The canonical form: the file through `louise fmt`.""" |
| 27 | louise = ROOT / "target" / "debug" / "louise" |
| 28 | with tempfile.TemporaryDirectory() as tmp: |
| 29 | f = pathlib.Path(tmp) / "prelude.lse" |
| 30 | f.write_text(text) |
| 31 | if louise.exists(): |
| 32 | subprocess.run([louise, "fmt", str(f)], check=True) |
| 33 | else: |
| 34 | subprocess.run( |
| 35 | ["cargo", "run", "-q", "-p", "louise", "--", "fmt", str(f)], |
| 36 | cwd=ROOT, |
| 37 | check=True, |
| 38 | ) |
| 39 | return f.read_text() |
| 40 | |
| 41 | |
| 42 | def impls(arity): |
| 43 | vars_ = [chr(ord("a") + i) for i in range(arity)] |
| 44 | ty = "(" + ", ".join(vars_) + ")" |
| 45 | fields = range(arity) |
| 46 | |
| 47 | def head(trait): |
| 48 | where = ", ".join(f"{trait}<{v}>" for v in vars_) |
| 49 | return f"impl {trait}<{ty}> where {where} {{" |
| 50 | |
| 51 | def joined(method): |
| 52 | parts = ' <> ", " <> '.join(f"{method} x.{i}" for i in fields) |
| 53 | return f'"(" <> {parts} <> ")"' |
| 54 | |
| 55 | hash_ = "hash_combine 0 (hash x.0)" |
| 56 | for i in range(1, arity): |
| 57 | hash_ = f"hash_combine ({hash_}) (hash x.{i})" |
| 58 | eq = " && ".join(f"x.{i} == y.{i}" for i in fields) |
| 59 | compare = " <> ".join(f"compare x.{i} y.{i}" for i in fields) |
| 60 | default = "(" + ", ".join("default ()" for _ in fields) + ")" |
| 61 | # `louise fmt` canonical form: function bodies always break. |
| 62 | def method(name, body): |
| 63 | return f" fn {name} {{\n {body}\n }}" |
| 64 | |
| 65 | return [ |
| 66 | f"{head('Eq')}\n{method('eq :: x y', eq)}\n}}", |
| 67 | f"{head('Ord')}\n{method('compare :: x y', compare)}\n}}", |
| 68 | f"{head('Show')}\n{method('show :: x', joined('show'))}\n}}", |
| 69 | f"{head('Debug')}\n{method('debug :: x', joined('debug'))}\n}}", |
| 70 | f"{head('Hash')}\n{method('hash :: x', hash_)}\n}}", |
| 71 | f"{head('Default')}\n{method('default ::', default)}\n}}", |
| 72 | ] |
| 73 | |
| 74 | |
| 75 | def block(): |
| 76 | unit = [ |
| 77 | "impl Eq<()> {\n fn eq :: _ _ {\n True\n }\n}", |
| 78 | "impl Ord<()> {\n fn compare :: _ _ {\n Eq\n }\n}", |
| 79 | 'impl Show<()> {\n fn show :: _ {\n "()"\n }\n}', |
| 80 | 'impl Debug<()> {\n fn debug :: _ {\n "()"\n }\n}', |
| 81 | "impl Hash<()> {\n fn hash :: _ {\n 0\n }\n}", |
| 82 | "impl Default<()> {\n fn default :: {\n ()\n }\n}", |
| 83 | ] |
| 84 | doc = ( |
| 85 | "// Tuples compare element by element, in order; they show as `(1, a)`\n" |
| 86 | "// and debug as `(1, \"a\")`. A tuple's hash folds its elements' with\n" |
| 87 | "// `hash_combine`, as a derived impl does.\n" |
| 88 | ) |
| 89 | items = unit + [i for n in range(2, MAX_ARITY + 1) for i in impls(n)] |
| 90 | return BEGIN + "\n" + doc + "\n" + "\n\n".join(items) + "\n" + END |
| 91 | |
| 92 | |
| 93 | def main(): |
| 94 | text = PRELUDE.read_text() |
| 95 | new = block() |
| 96 | if BEGIN in text: |
| 97 | start = text.index(BEGIN) |
| 98 | end = text.index(END) + len(END) |
| 99 | updated = text[:start] + new + text[end:] |
| 100 | else: |
| 101 | updated = text.rstrip("\n") + "\n\n" + new + "\n" |
| 102 | updated = format_louise(updated) |
| 103 | if "--check" in sys.argv[1:]: |
| 104 | if updated != text: |
| 105 | sys.exit("std/prelude.lse: the tuple impls are out of date; run scripts/gen-tuple-impls.py") |
| 106 | return |
| 107 | PRELUDE.write_text(updated) |
| 108 | |
| 109 | |
| 110 | if __name__ == "__main__": |
| 111 | main() |