refactor: add runner class
This commit is contained in:
32
main.py
32
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__':
|
||||
|
||||
@@ -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}")
|
||||
|
||||
33
src/runner.py
Normal file
33
src/runner.py
Normal file
@@ -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))
|
||||
Reference in New Issue
Block a user