Improve compilation output for builtins in special contexts

This commit is contained in:
Emi Simpson 2024-03-15 18:52:17 -04:00
parent fbcbe0215a
commit 7b59e21183
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
1 changed files with 8 additions and 1 deletions

9
ir.py
View File

@ -201,6 +201,13 @@ class Application:
if isinstance(self.first, Function | Builtin) and self.arg.is_value():
return unwrap_opt(self.first.try_apply(self.arg)).codegen()
else:
match self.first:
case Application(Builtin('+', _, _), addend1):
return f'({addend1.codegen()} + {self.arg.codegen()})'
case Builtin('S', _, _):
return f'(1+{self.arg.codegen()})'
case Builtin('pred', _, _):
return f'({self.arg.codegen()}-1)'
return f'({self.first.codegen()})({self.arg.codegen()})'
@dataclass
@ -319,7 +326,7 @@ def compile_tree(tree: 'MatchTree[Expression]', match_against: Expression) -> Re
def location_to_ir(location: StructurePath) -> Callable[[Expression], Expression]:
def access_location(part: int) -> Callable[[Expression], Expression]:
def remove(expr: Expression) -> Expression:
return Application(Builtin(f'{part}', Builtin._PLUS_CONST(-1), f'$=>$-1'), expr)
return Application(Builtin(f'pred', Builtin._PLUS_CONST(-1), f'$=>$-1'), expr)
def access_location_prime(expr: Expression) -> Expression:
if part < 1:
return remove(expr)