From 92e02c8973b3a8f00ea32cf83ed869339868be94 Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Thu, 11 Apr 2024 20:20:00 +0200 Subject: [PATCH] main+errors: move pretty printing errors into a method --- compiler/__main__.py | 8 ++------ compiler/errors.py | 8 ++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/compiler/__main__.py b/compiler/__main__.py index 0a28bb9..38c3186 100644 --- a/compiler/__main__.py +++ b/compiler/__main__.py @@ -7,8 +7,8 @@ import typing from . import semantic, ir, optimizations from .errors import CompilationError, CompilationWarning +from .interpreter import virtual_machine from .lexer import Lexer, Tokens -from .logger import rootLogger, LogLevel from .parser import Parser @@ -81,11 +81,7 @@ def main(): except CompilationError as e: e.location.source = tokens.data - print(f"{e}\n{e.location.show_in_source()}", flush=True) - if e.__cause__ is not None: - if rootLogger.level <= LogLevel.Debug: - raise e.__cause__ - print(f"Caused by:\n{e.__cause__.__class__.__name__}: {e.__cause__}", flush=True) + e.pretty_print() finally: CompilationWarning.show_warnings(data, file=sys.stdout) diff --git a/compiler/errors.py b/compiler/errors.py index f43ed21..c571a89 100644 --- a/compiler/errors.py +++ b/compiler/errors.py @@ -8,6 +8,7 @@ from typing import TextIO from termcolor import termcolor from . import source, lexer +from .logger import rootLogger, LogLevel class LevelType: @@ -40,6 +41,13 @@ class CompilationError(Exception): super().__init__(f"{str(location)}: {Levels.Error.value.show_with_suffix(':')} {message}") self.location = location + def pretty_print(self): + print(f"{self}\n{self.location.show_in_source()}", flush=True) + if self.__cause__ is not None: + if rootLogger.level <= LogLevel.Debug: + raise self.__cause__ + print(f"Caused by:\n{self.__cause__.__class__.__name__}: {self.__cause__}", flush=True) + class CompilationWarning(Warning): _pending_warnings: list[CompilationWarning] = []