feat(runner): implement simple REPL

This commit is contained in:
2026-02-07 01:37:22 +01:00
parent d392dba30d
commit 2f19fbe254
2 changed files with 71 additions and 6 deletions

View File

@@ -1,11 +1,8 @@
from pathlib import Path from src.runner import Runner
from src.pebble import Pebble
def main(): def main():
path: Path = Path("examples/advanced/02_polymorphism.peb") Runner.repl()
Pebble.run_file(path)
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -7,7 +7,7 @@ from src.interpreter.resolver import Resolver
from src.lexer import Lexer from src.lexer import Lexer
from src.parser.parser import Parser from src.parser.parser import Parser
from src.pebble import Pebble from src.pebble import Pebble
from src.token import Token from src.token import Token, TokenType
class Runner: class Runner:
@@ -43,3 +43,71 @@ class Runner:
formatter: Formatter = Formatter() formatter: Formatter = Formatter()
with open("formatted.peb", "w") as f: with open("formatted.peb", "w") as f:
f.write(formatter.print(program)) f.write(formatter.print(program))
@staticmethod
def repl():
line: str
lexer: Lexer
tokens: list[Token]
parser: Parser
program: list[Stmt]
interpreter: Interpreter = Interpreter()
resolver: Resolver
brace_depth: int
paren_depth: int
buf: str = ""
while True:
try:
line = input(">>> " if len(buf) == 0 else "... ")
except EOFError:
print()
break
except KeyboardInterrupt:
buf = ""
print()
continue
buf += line + "\n"
lexer = Lexer(buf)
tokens = lexer.process()
if Pebble.had_error:
Pebble.had_error = False
continue
brace_depth = 0
paren_depth = 0
for token in tokens:
match token.type:
case TokenType.LEFT_BRACE:
brace_depth += 1
case TokenType.RIGHT_BRACE:
brace_depth = max(0, brace_depth - 1)
case TokenType.LEFT_PAREN:
paren_depth += 1
case TokenType.RIGHT_PAREN:
paren_depth = max(0, paren_depth - 1)
if brace_depth > 0 or paren_depth > 0:
continue
parser = Parser(tokens)
program = parser.parse()
buf = ""
if Pebble.had_error:
Pebble.had_error = False
continue
resolver: Resolver = Resolver(interpreter)
resolver.resolve(*program)
if Pebble.had_error:
Pebble.had_error = False
continue
interpreter.interpret(program)
if Pebble.had_runtime_error:
Pebble.had_runtime_error = False
continue