46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
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.pebble import Pebble
|
|
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)))
|
|
|
|
if Pebble.had_error:
|
|
return
|
|
|
|
parser: Parser = Parser(tokens)
|
|
program: list[Stmt] = parser.parse()
|
|
|
|
if Pebble.had_error:
|
|
return
|
|
|
|
interpreter: Interpreter = Interpreter()
|
|
resolver: Resolver = Resolver(interpreter)
|
|
|
|
resolver.resolve(*program)
|
|
if Pebble.had_error:
|
|
return
|
|
interpreter.interpret(program)
|
|
|
|
if Pebble.had_runtime_error:
|
|
return
|
|
|
|
formatter: Formatter = Formatter()
|
|
with open("formatted.peb", "w") as f:
|
|
f.write(formatter.print(program))
|