source: add IRLocation type specifically for generated IR
This commit is contained in:
parent
6bbe2b195e
commit
39c12a360c
1 changed files with 43 additions and 0 deletions
|
|
@ -138,3 +138,46 @@ class SourceLocation:
|
|||
result[i] = "\n".join(lines)
|
||||
|
||||
return "\n".join(result)
|
||||
|
||||
|
||||
@typecheck
|
||||
class IRLocation:
|
||||
def __init__(self, line: int = -1, ir: list[str] = None):
|
||||
if ir is None:
|
||||
ir = []
|
||||
|
||||
self.line = line
|
||||
self.ir = ir
|
||||
|
||||
@typecheck
|
||||
def __lt__(self, other: IRLocation):
|
||||
assert self.line >= 0
|
||||
|
||||
return self.line < other.line
|
||||
|
||||
@typecheck
|
||||
def __ge__(self, other: IRLocation):
|
||||
assert self.line >= 0
|
||||
|
||||
return self.line >= other.line
|
||||
|
||||
@typecheck
|
||||
def __eq__(self, other: IRLocation):
|
||||
assert self.line >= 0
|
||||
|
||||
return self.line == other.line
|
||||
|
||||
def __str__(self) -> str:
|
||||
assert self.line >= 0
|
||||
assert len(self.ir) > self.line
|
||||
|
||||
repr_len = int(math.log10(len(self.ir)) + 1)
|
||||
|
||||
messages = []
|
||||
messages += self.ir[self.line - 2:self.line + 1]
|
||||
messages += ["^" * len(self.ir[self.line])]
|
||||
messages += self.ir[self.line:self.line + 3]
|
||||
|
||||
messages = [f"{str(self.line + i - 2).zfill(repr_len)}: {m}" for i, m in enumerate(messages)]
|
||||
|
||||
return "\n".join(messages)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue