L3.main

 1from pathlib import Path
 2
 3import click
 4
 5# from L2.cps_convert import cps_convert_program
 6from L2.optimize import optimize_program
 7from L2.to_python import to_ast_program
 8
 9from .check import check_program
10from .diagnostics import analyze_source
11from .eliminate_letrec import eliminate_letrec_program
12from .parse import parse_program
13from .uniqify import uniqify_program
14
15
16@click.command(
17    context_settings=dict(
18        help_option_names=["-h", "--help"],
19        max_content_width=120,
20    ),
21)
22@click.option(
23    "--diagnostics-json",
24    is_flag=True,
25    default=False,
26    help="Emit parser/checker diagnostics as JSON for editor integrations",
27)
28@click.option(
29    "--check/--no-check",
30    default=True,
31    show_default=True,
32    help="Enable or disable semantic analysis",
33)
34@click.option(
35    "--optimize/--no-optimize",
36    default=True,
37    show_default=True,
38    help="Enable or disable optimization",
39)
40@click.option(
41    "-o",
42    "--output",
43    type=click.Path(writable=True, dir_okay=False, path_type=Path),
44    default=None,
45    help="Output file (defaults to <INPUT>.py)",
46)
47@click.argument(
48    "input",
49    type=click.Path(exists=True, readable=True, dir_okay=False, path_type=Path),
50)
51def main(
52    output: Path | None,
53    diagnostics_json: bool,
54    check: bool,
55    optimize: bool,
56    input: Path,
57) -> None:
58    source = input.read_text()
59
60    if diagnostics_json:
61        report = analyze_source(source)
62        click.echo(report.model_dump_json(indent=2))
63        raise SystemExit(0 if report.ok else 1)
64
65    l3 = parse_program(source)
66
67    if check:
68        check_program(l3)
69
70    fresh, l3 = uniqify_program(l3)  # type: ignore
71
72    l2 = eliminate_letrec_program(l3)
73
74    if optimize:
75        l2 = optimize_program(l2)
76
77    # l1 = cps_convert_program(l2, fresh)
78
79    module = to_ast_program(l2)
80
81    (output or input.with_suffix(".py")).write_text(module)