From a69788ca52b685b9c6d0022dffb796aa718f35fa Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Thu, 11 Jan 2024 00:54:35 +0100 Subject: [PATCH] source: avoid printing source file twice if it is the same for beginning and end (which is very likely). --- compiler/source.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/compiler/source.py b/compiler/source.py index 90eea04..4b85a5f 100644 --- a/compiler/source.py +++ b/compiler/source.py @@ -17,10 +17,16 @@ class Location: file: str = "" internal: bool = False + def to_string(self, show_file: bool = True) -> str: + filename = "" if self.internal else self.file + message = "" + if show_file: + message = f"{filename}:" + message += f"{self.line}:{self.character}" + return message + def __str__(self) -> str: - if self.internal: - return "" - return f"{self.file}:{self.line}:{self.character}" + return self.to_string() @typecheck def __lt__(self, other: Location) -> bool: @@ -76,7 +82,7 @@ class SourceLocation: def __str__(self): if self.begin == self.end: return str(self.begin) - return f"{str(self.begin)} - {str(self.end)}" + return f"{str(self.begin)} - {self.end.to_string(self.begin.file != self.end.file)}" @typecheck def __lt__(self, other: SourceLocation) -> bool: