main+ir: fix errors with REPL input (and wrong output in error messages)

This commit is contained in:
Antoine Viallon 2024-04-11 20:45:30 +02:00
parent 103339d666
commit 37bfd831f4
Signed by: aviallon
GPG key ID: 186FC35EDEB25716
2 changed files with 9 additions and 8 deletions

View file

@ -65,8 +65,8 @@ def main():
context.check() context.check()
intermediate_representation = ir.IR(ast, source=data) intermediate_representation = ir.IR(ast)
intermediate_representation.update_location() intermediate_representation.update_location(source=tokens.data)
print("\n---\n", repr(context)) print("\n---\n", repr(context))
@ -84,7 +84,7 @@ def main():
e.pretty_print() e.pretty_print()
finally: finally:
CompilationWarning.show_warnings(data, file=sys.stdout) CompilationWarning.show_warnings(tokens.data, file=sys.stdout)
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -260,7 +260,7 @@ def _to_text(ir: list[IRItem]) -> list[str]:
class IR: class IR:
def __init__(self, ast: nodes.Node, source: str): def __init__(self, ast: nodes.Node):
node_ir = ast.intermediate_representation() node_ir = ast.intermediate_representation()
assert all((isinstance(ir, IRAction) for ir in node_ir)) assert all((isinstance(ir, IRAction) for ir in node_ir))
@ -268,17 +268,15 @@ class IR:
# noinspection PyTypeChecker # noinspection PyTypeChecker
self.intermediate_representation: list[IRAction] = node_ir self.intermediate_representation: list[IRAction] = node_ir
self.source = source
def code(self) -> list[str]: def code(self) -> list[str]:
return [x.codegen() for x in self.intermediate_representation] return [x.codegen() for x in self.intermediate_representation]
def update_location(self): def update_location(self, source: str):
code = self.code() code = self.code()
ir_item: IRAction ir_item: IRAction
for i, ir_item in enumerate(self.intermediate_representation): for i, ir_item in enumerate(self.intermediate_representation):
ir_item.ir_location = Location(line=i, ir=code) ir_item.ir_location = Location(line=i, ir=code)
ir_item.location.source = self.source ir_item.location.source = source
def pretty_print(self): def pretty_print(self):
messages = [] messages = []
@ -286,6 +284,9 @@ class IR:
for i, ir_item in enumerate(self.intermediate_representation): for i, ir_item in enumerate(self.intermediate_representation):
prefix = f"{str(ir_item.location) + ':':<30}" prefix = f"{str(ir_item.location) + ':':<30}"
source_info = ir_item.location.source_substring.splitlines(keepends=False) 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 = ["<NO SOURCE>"]
messages += [f"# {prefix} {source_info.pop(0)}"] messages += [f"# {prefix} {source_info.pop(0)}"]
while len(source_info) > 0: while len(source_info) > 0:
messages += [f"# {' ' * len(prefix)} {source_info.pop(0)}"] messages += [f"# {' ' * len(prefix)} {source_info.pop(0)}"]