From 37bfd831f43733449ed2955120ba1fa8d3a9b086 Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Thu, 11 Apr 2024 20:45:30 +0200 Subject: [PATCH] main+ir: fix errors with REPL input (and wrong output in error messages) --- compiler/__main__.py | 6 +++--- compiler/ir.py | 11 ++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/compiler/__main__.py b/compiler/__main__.py index 38c3186..dc02db9 100644 --- a/compiler/__main__.py +++ b/compiler/__main__.py @@ -65,8 +65,8 @@ def main(): context.check() - intermediate_representation = ir.IR(ast, source=data) - intermediate_representation.update_location() + intermediate_representation = ir.IR(ast) + intermediate_representation.update_location(source=tokens.data) print("\n---\n", repr(context)) @@ -84,7 +84,7 @@ def main(): e.pretty_print() finally: - CompilationWarning.show_warnings(data, file=sys.stdout) + CompilationWarning.show_warnings(tokens.data, file=sys.stdout) if __name__ == "__main__": diff --git a/compiler/ir.py b/compiler/ir.py index da83c43..3f64761 100644 --- a/compiler/ir.py +++ b/compiler/ir.py @@ -260,7 +260,7 @@ def _to_text(ir: list[IRItem]) -> list[str]: class IR: - def __init__(self, ast: nodes.Node, source: str): + def __init__(self, ast: nodes.Node): node_ir = ast.intermediate_representation() assert all((isinstance(ir, IRAction) for ir in node_ir)) @@ -268,17 +268,15 @@ class IR: # noinspection PyTypeChecker self.intermediate_representation: list[IRAction] = node_ir - self.source = source - def code(self) -> list[str]: return [x.codegen() for x in self.intermediate_representation] - def update_location(self): + def update_location(self, source: str): code = self.code() ir_item: IRAction for i, ir_item in enumerate(self.intermediate_representation): ir_item.ir_location = Location(line=i, ir=code) - ir_item.location.source = self.source + ir_item.location.source = source def pretty_print(self): messages = [] @@ -286,6 +284,9 @@ class IR: for i, ir_item in enumerate(self.intermediate_representation): prefix = f"{str(ir_item.location) + ':':<30}" source_info = ir_item.location.source_substring.splitlines(keepends=False) + logger.debug(f"source: {repr(ir_item.location.source)}") + if len(source_info) == 0: + source_info = [""] messages += [f"# {prefix} {source_info.pop(0)}"] while len(source_info) > 0: messages += [f"# {' ' * len(prefix)} {source_info.pop(0)}"]