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: