Simplify code generation for a top-level recursive function
This commit is contained in:
parent
23ef1f1228
commit
1d8fdb1a81
12
ir.py
12
ir.py
|
@ -38,8 +38,18 @@ class ReplHole:
|
|||
return '[]'
|
||||
|
||||
def render(self) -> str:
|
||||
def render_var(var_name: str, var_expr: Expression) -> str:
|
||||
match var_expr:
|
||||
case LetBinding(prev_func_name, MonoFunc(formal, func_body), Variable(mb_prev_func_name)):
|
||||
if mb_prev_func_name == prev_func_name:
|
||||
func_body_with_new_name = func_body.subst(Variable(var_name), prev_func_name)
|
||||
return f'function {var_name}({formal}) {{return {func_body_with_new_name.codegen()}}}'
|
||||
else:
|
||||
raise Exception('This useless LetBinding should have been removed by the optimization stage')
|
||||
case _:
|
||||
return f'const {var_name} = ({var_expr.codegen()});'
|
||||
return '\n'.join(
|
||||
f'const {var_name} = ({var_expr.codegen()});'
|
||||
render_var(var_name, var_expr)
|
||||
for (var_name, var_expr) in self.val_bindings[::-1]
|
||||
if var_name not in types_.BUILTINS_CONTEXT
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue