summaryrefslogtreecommitdiff
path: root/projects/hackc/__main__.py
blob: c50461a4f74e630ac9e2f298bde4aee829035954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from pathlib import Path
from argparse import ArgumentParser
import os
from .parser import Parser


def compile_jack(input_path: Path, extensions: list, verbose: bool):
    try:
        filenames = os.listdir(input_path)
        files = [Path(input_path / f) for f in filenames]
        jack_files = filter(lambda f: f.suffix == ".jack", files)
    except NotADirectoryError:
        jack_files = [Path(input_path)]
    except:
        # TODO: error
        return

    for input_fn in jack_files:
        parser = Parser(input_fn, extensions=extensions)
        parser.tokenize()
        parser.parse()


if __name__ == "__main__":
    parser = ArgumentParser("hackc")
    parser.add_argument("-v", "--verbose", action="store_true", help="verbose mode")
    parser.add_argument(
        "-E",
        "--extension",
        metavar="extension",
        action="append",
        help="enable extension of standard Jack",
    )
    parser.add_argument("input_path", help="Jack file or directory")
    args = parser.parse_args()
    compile_jack(Path(args.input_path), args.extension or [], args.verbose)