From 51e1667e716ea8c6b20f37cdec1f99eef55eccd6 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Mon, 29 Aug 2022 20:20:08 +0800 Subject: hackc: tokenizer --- projects/hackc/__main__.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 projects/hackc/__main__.py (limited to 'projects/hackc/__main__.py') diff --git a/projects/hackc/__main__.py b/projects/hackc/__main__.py new file mode 100644 index 0000000..a7bc06f --- /dev/null +++ b/projects/hackc/__main__.py @@ -0,0 +1,29 @@ +from pathlib import Path +from argparse import ArgumentParser +import os +from .parser import Parser + + +def compile_jack(input_path: Path, 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) + parser.tokenize() + parser.print_tokens() + + +if __name__ == "__main__": + parser = ArgumentParser("hackc") + parser.add_argument("-v", "--verbose", action="store_true", help="verbose mode") + parser.add_argument("input_path", help="Jack file or directory") + args = parser.parse_args() + compile_jack(Path(args.input_path), args.verbose) -- cgit v1.2.3