diff --git a/main.py b/main.py index 769968c..ba40241 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,8 @@ -from pathlib import Path - -from src.pebble import Pebble +from src.runner import Runner def main(): - path: Path = Path("examples/advanced/02_polymorphism.peb") - Pebble.run_file(path) + Runner.repl() if __name__ == '__main__': diff --git a/src/runner.py b/src/runner.py index 82dffe1..3685767 100644 --- a/src/runner.py +++ b/src/runner.py @@ -7,7 +7,7 @@ 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 +from src.token import Token, TokenType class Runner: @@ -43,3 +43,71 @@ class Runner: formatter: Formatter = Formatter() with open("formatted.peb", "w") as f: 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