diff --git a/src/pebble.py b/src/pebble.py index d35bb54..b56d3cc 100644 --- a/src/pebble.py +++ b/src/pebble.py @@ -1,3 +1,4 @@ +import sys from pathlib import Path from typing import Optional @@ -7,6 +8,9 @@ from src.token import Token, TokenType class Pebble: + had_error: bool = False + had_runtime_error: bool = False + @staticmethod def run_file(path: str | Path): source: str = "" @@ -19,10 +23,15 @@ class Pebble: from src.runner import Runner runner: Runner = Runner(source, path) runner.run() + if Pebble.had_error: + exit(65) + if Pebble.had_runtime_error: + exit(70) @staticmethod def report(position: Position, where: str, msg: str): - print(f"({position}) Error{where}: {msg}") + print(f"({position}) Error{where}: {msg}", file=sys.stderr) + Pebble.had_error = True @classmethod def error(cls, position: Position, msg: str): @@ -37,4 +46,5 @@ class Pebble: @classmethod def runtime_error(cls, error: PebbleRuntimeError): - print(f"{error}\n({error.token.position})") + print(f"{error}\n({error.token.position})", file=sys.stderr) + Pebble.had_runtime_error = True diff --git a/src/runner.py b/src/runner.py index 0948d94..82dffe1 100644 --- a/src/runner.py +++ b/src/runner.py @@ -6,6 +6,7 @@ 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 @@ -19,15 +20,26 @@ class Runner: 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))