diff --git a/compiler/source.py b/compiler/source.py index ce6b79a..e302a55 100644 --- a/compiler/source.py +++ b/compiler/source.py @@ -6,6 +6,10 @@ from dataclasses import dataclass from beartype import beartype from beartype.typing import Optional +from .logger import Logger + +logger = Logger(__name__) + @beartype @dataclass @@ -17,6 +21,38 @@ class Location: def __str__(self) -> str: return f"{self.file}:{self.line}:{self.character}" + @beartype + def __lt__(self, other: Location) -> bool: + if self.file != other.file: + logger.trace(f"{self} is not in the same file as {other}") + return False + + if self.line < other.line: + logger.trace(f"{self} lesser than {other}") + return True + + if self.line > other.line: + logger.trace(f"{self} greater than {other}") + return False + + if self.character < other.character: + logger.trace(f"{self} lesser than {other}") + return True + + return False + + @beartype + def __ge__(self, other: Location) -> bool: + return not self.__lt__(other) + + @beartype + def __eq__(self, other: Location) -> bool: + return all(( + self.file == other.file, + self.line == other.line, + self.character == other.character + )) + @beartype class SourceLocation: