Fix recursion for each function name

This commit is contained in:
Jan-Niclas Loosen
2026-01-23 12:55:49 +01:00
parent 188cba7fa6
commit add07249dc
7 changed files with 21 additions and 158 deletions

View File

@@ -13,6 +13,9 @@ import syntax
# Global registry for function start/end nodes
FUNCTIONS = {}
# Global variable to track the current function being processed
CURRENT_FUNCTION = None
class CONST(compiler.CONST):
def cfa(self, pred, end = None):
node = CFG_Node(self)
@@ -220,16 +223,9 @@ class CALL(compiler.CALL):
call_node.add_child(f_start)
call_node.add_child(return_node)
# TODO: Why only g? Also f can be recursive.
# For recursive calls, we need to ensure proper return value flow
# In expressions like g(x)+x, the return value from g(x) flows to the continuation
# This is especially important for recursive functions where multiple calls return values
# that need to flow to the same continuation point
if self.f_name == 'g':
# For recursive calls in g, ensure the return node connects to continuation
# This handles cases like g(y) where the return value flows to the same place as g(x)
# Return value flow for recursive calls
if CURRENT_FUNCTION is not None and self.f_name == CURRENT_FUNCTION:
return_node.add_child(cont)
return cont
class DECL(compiler.DECL):
@@ -245,12 +241,22 @@ class DECL(compiler.DECL):
f_end.label = f"END {self.f_name}({', '.join(self.params)})"
FUNCTIONS[self.f_name] = (f_start, f_end)
# Unwrap the method body
body_end = self.body.cfa(f_start, f_end)
# Set the current function context for recursion detection
global CURRENT_FUNCTION
previous_function = CURRENT_FUNCTION
CURRENT_FUNCTION = self.f_name
try:
# Unwrap the method body
body_end = self.body.cfa(f_start, f_end)
# Attach the end node if it is provided
if body_end is not None:
body_end.add_child(f_end)
finally:
# Restore the previous function context
CURRENT_FUNCTION = previous_function
# Attach the end node if it is provided
if body_end is not None:
body_end.add_child(f_end)
return pred
class LET(compiler.LET):