From d8109841288ab8f5d061f1fd0a5f2381e616e507 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 6 Feb 2026 15:24:39 +0100 Subject: [PATCH] refactor: add runner class --- main.py | 32 +++++--------------------------- src/pebble.py | 16 ++++++++++++++++ src/runner.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 src/runner.py diff --git a/main.py b/main.py index 4c5e0e4..5e0fa6c 100644 --- a/main.py +++ b/main.py @@ -1,33 +1,11 @@ -from src.ast.stmt import Stmt -from src.formatter import Formatter -from src.interpreter.interpreter import Interpreter -from src.interpreter.resolver import Resolver -from src.lexer import Lexer -from src.parser.parser import Parser -from src.token import Token +from pathlib import Path + +from src.pebble import Pebble def main(): - path: str = "examples/15_resolution.peb" - source: str = "" - with open(path, "r") as f: - source = f.read() - lexer: Lexer = Lexer(source, path) - tokens: list[Token] = lexer.process() - print(list(filter(lambda t: t.type not in Parser.IGNORE, tokens))) - - parser: Parser = Parser(tokens) - program: list[Stmt] = parser.parse() - - interpreter: Interpreter = Interpreter() - resolver: Resolver = Resolver(interpreter) - - resolver.resolve(*program) - interpreter.interpret(program) - - formatter: Formatter = Formatter() - with open("formatted.peb", "w") as f: - f.write(formatter.print(program)) + path: Path = Path("examples/15_resolution.peb") + Pebble.run_file(path) if __name__ == '__main__': diff --git a/src/pebble.py b/src/pebble.py index 628092f..d35bb54 100644 --- a/src/pebble.py +++ b/src/pebble.py @@ -1,9 +1,25 @@ +from pathlib import Path +from typing import Optional + from src.interpreter.error import PebbleRuntimeError from src.position import Position from src.token import Token, TokenType class Pebble: + @staticmethod + def run_file(path: str | Path): + source: str = "" + with open(path, "r") as f: + source = f.read() + Pebble.run(source, str(path)) + + @staticmethod + def run(source: str, path: Optional[str] = None): + from src.runner import Runner + runner: Runner = Runner(source, path) + runner.run() + @staticmethod def report(position: Position, where: str, msg: str): print(f"({position}) Error{where}: {msg}") diff --git a/src/runner.py b/src/runner.py new file mode 100644 index 0000000..0948d94 --- /dev/null +++ b/src/runner.py @@ -0,0 +1,33 @@ +from typing import Optional + +from src.ast.stmt import Stmt +from src.formatter import Formatter +from src.interpreter.interpreter import Interpreter +from src.interpreter.resolver import Resolver +from src.lexer import Lexer +from src.parser.parser import Parser +from src.token import Token + + +class Runner: + def __init__(self, source: str, path: Optional[str] = None): + self.source: str = source + self.path: Optional[str] = path + + def run(self): + lexer: Lexer = Lexer(self.source, self.path) + tokens: list[Token] = lexer.process() + print(list(filter(lambda t: t.type not in Parser.IGNORE, tokens))) + + parser: Parser = Parser(tokens) + program: list[Stmt] = parser.parse() + + interpreter: Interpreter = Interpreter() + resolver: Resolver = Resolver(interpreter) + + resolver.resolve(*program) + interpreter.interpret(program) + + formatter: Formatter = Formatter() + with open("formatted.peb", "w") as f: + f.write(formatter.print(program))