diff --git a/main.py b/main.py old mode 100644 new mode 100755 index ba40241..f119544 --- a/main.py +++ b/main.py @@ -1,8 +1,31 @@ +#!/usr/bin/python + +import argparse +import os +from pathlib import Path + from src.runner import Runner +def is_valid_file(parser, arg): + if not os.path.isfile(arg): + parser.error("The file %s does not exist!" % arg) + return None + return Path(arg) + + def main(): - Runner.repl() + parser = argparse.ArgumentParser( + prog="pebble" + ) + parser.add_argument("script", type=lambda x: is_valid_file(parser, x), help="File to run", nargs="?") + + args = parser.parse_args() + + if args.script is None: + Runner.repl() + else: + Runner.run_file(args.script) if __name__ == '__main__': diff --git a/src/runner.py b/src/runner.py index 3685767..c14002e 100644 --- a/src/runner.py +++ b/src/runner.py @@ -1,3 +1,4 @@ +from pathlib import Path from typing import Optional from src.ast.stmt import Stmt @@ -11,12 +12,16 @@ from src.token import Token, TokenType class Runner: - def __init__(self, source: str, path: Optional[str] = None): - self.source: str = source - self.path: Optional[str] = path + @staticmethod + def run_file(path: Path): + with open(path, "r") as f: + source: str = f.read() - def run(self): - lexer: Lexer = Lexer(self.source, self.path) + Runner.run(source, path) + + @staticmethod + def run(source: str, path: Optional[Path] = None): + lexer: Lexer = Lexer(source, path) tokens: list[Token] = lexer.process() print(list(filter(lambda t: t.type not in Parser.IGNORE, tokens))) @@ -75,7 +80,6 @@ class Runner: Pebble.had_error = False continue - brace_depth = 0 paren_depth = 0 for token in tokens: