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 pathlib import Path
|
||||||
from src.formatter import Formatter
|
|
||||||
from src.interpreter.interpreter import Interpreter
|
from src.pebble import Pebble
|
||||||
from src.interpreter.resolver import Resolver
|
|
||||||
from src.lexer import Lexer
|
|
||||||
from src.parser.parser import Parser
|
|
||||||
from src.token import Token
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
path: str = "examples/15_resolution.peb"
|
path: Path = Path("examples/15_resolution.peb")
|
||||||
source: str = ""
|
Pebble.run_file(path)
|
||||||
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))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -1,9 +1,25 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from src.interpreter.error import PebbleRuntimeError
|
from src.interpreter.error import PebbleRuntimeError
|
||||||
from src.position import Position
|
from src.position import Position
|
||||||
from src.token import Token, TokenType
|
from src.token import Token, TokenType
|
||||||
|
|
||||||
|
|
||||||
class Pebble:
|
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
|
@staticmethod
|
||||||
def report(position: Position, where: str, msg: str):
|
def report(position: Position, where: str, msg: str):
|
||||||
print(f"({position}) Error{where}: {msg}")
|
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