Improve compilation output for builtins in special contexts
This commit is contained in:
parent
fbcbe0215a
commit
7b59e21183
9
ir.py
9
ir.py
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue