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 .eliminate_letrec import eliminate_letrec_program
11from .parse import parse_program
12from .uniqify import uniqify_program
13
14
15@click.command(
16    context_settings=dict(
17        help_option_names=["-h", "--help"],
18        max_content_width=120,
19    ),
20)
21@click.option(
22    "--check/--no-check",
23    default=True,
24    show_default=True,
25    help="Enable or disable semantic analysis",
26)
27@click.option(
28    "--optimize/--no-optimize",
29    default=True,
30    show_default=True,
31    help="Enable or disable optimization",
32)
33@click.option(
34    "-o",
35    "--output",
36    type=click.Path(writable=True, dir_okay=False, path_type=Path),
37    default=None,
38    help="Output file (defaults to <INPUT>.py)",
39)
40@click.argument(
41    "input",
42    type=click.Path(exists=True, readable=True, dir_okay=False, path_type=Path),
43)
44def main(
45    output: Path | None,
46    check: bool,
47    optimize: bool,
48    input: Path,
49) -> None:
50    l3 = parse_program(input.read_text())
51
52    if check:
53        check_program(l3)
54
55    fresh, l3 = uniqify_program(l3)  # type: ignore
56
57    l2 = eliminate_letrec_program(l3)
58
59    if optimize:
60        l2 = optimize_program(l2)
61
62    # l1 = cps_convert_program(l2, fresh)
63
64    module = to_ast_program(l2)
65
66    (output or input.with_suffix(".py")).write_text(module)