feat: improve global error management
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
@@ -7,6 +8,9 @@ from src.token import Token, TokenType
|
|||||||
|
|
||||||
|
|
||||||
class Pebble:
|
class Pebble:
|
||||||
|
had_error: bool = False
|
||||||
|
had_runtime_error: bool = False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def run_file(path: str | Path):
|
def run_file(path: str | Path):
|
||||||
source: str = ""
|
source: str = ""
|
||||||
@@ -19,10 +23,15 @@ class Pebble:
|
|||||||
from src.runner import Runner
|
from src.runner import Runner
|
||||||
runner: Runner = Runner(source, path)
|
runner: Runner = Runner(source, path)
|
||||||
runner.run()
|
runner.run()
|
||||||
|
if Pebble.had_error:
|
||||||
|
exit(65)
|
||||||
|
if Pebble.had_runtime_error:
|
||||||
|
exit(70)
|
||||||
|
|
||||||
@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}", file=sys.stderr)
|
||||||
|
Pebble.had_error = True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def error(cls, position: Position, msg: str):
|
def error(cls, position: Position, msg: str):
|
||||||
@@ -37,4 +46,5 @@ class Pebble:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def runtime_error(cls, error: PebbleRuntimeError):
|
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
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from src.interpreter.interpreter import Interpreter
|
|||||||
from src.interpreter.resolver import Resolver
|
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.token import Token
|
from src.token import Token
|
||||||
|
|
||||||
|
|
||||||
@@ -19,15 +20,26 @@ class Runner:
|
|||||||
tokens: list[Token] = lexer.process()
|
tokens: list[Token] = lexer.process()
|
||||||
print(list(filter(lambda t: t.type not in Parser.IGNORE, tokens)))
|
print(list(filter(lambda t: t.type not in Parser.IGNORE, tokens)))
|
||||||
|
|
||||||
|
if Pebble.had_error:
|
||||||
|
return
|
||||||
|
|
||||||
parser: Parser = Parser(tokens)
|
parser: Parser = Parser(tokens)
|
||||||
program: list[Stmt] = parser.parse()
|
program: list[Stmt] = parser.parse()
|
||||||
|
|
||||||
|
if Pebble.had_error:
|
||||||
|
return
|
||||||
|
|
||||||
interpreter: Interpreter = Interpreter()
|
interpreter: Interpreter = Interpreter()
|
||||||
resolver: Resolver = Resolver(interpreter)
|
resolver: Resolver = Resolver(interpreter)
|
||||||
|
|
||||||
resolver.resolve(*program)
|
resolver.resolve(*program)
|
||||||
|
if Pebble.had_error:
|
||||||
|
return
|
||||||
interpreter.interpret(program)
|
interpreter.interpret(program)
|
||||||
|
|
||||||
|
if Pebble.had_runtime_error:
|
||||||
|
return
|
||||||
|
|
||||||
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))
|
||||||
|
|||||||
Reference in New Issue
Block a user