First working solution of new task

This commit is contained in:
Jan-Niclas Loosen
2026-03-05 18:03:55 +01:00
parent 691d6eba8c
commit de46c67129
11 changed files with 1077 additions and 9 deletions

View File

@@ -148,11 +148,15 @@ class IF(compiler.IF):
class WHILE(compiler.WHILE):
def cfa(self, pred, end = None):
# Dedicated loop-head so every iteration re-evaluates the full condition.
cond_entry = CFG_Node()
pred.add_child(cond_entry)
if hasattr(self.cond, 'arg1') and hasattr(self.cond, 'arg2'):
# This is a comparison operation (e.g., a > b)
# Create the condition evaluation nodes
left_node = self.cond.arg1.cfa(pred)
left_node = self.cond.arg1.cfa(cond_entry)
right_node = self.cond.arg2.cfa(left_node)
# Create the comparison node and attach
@@ -161,7 +165,7 @@ class WHILE(compiler.WHILE):
right_node.add_child(comp_node)
else:
# This is a simple condition (e.g., constant true/false)
cond_node = self.cond.cfa(pred)
cond_node = self.cond.cfa(cond_entry)
comp_node = cond_node
# Attach junction node
@@ -175,11 +179,8 @@ class WHILE(compiler.WHILE):
# The body should connect back to the start of condition evaluation
body_end = self.body.cfa(body_entry)
if body_end is not None:
# Connect the body end back to the condition evaluation
if hasattr(self.cond, 'arg1') and hasattr(self.cond, 'arg2'):
body_end.add_child(left_node)
else:
body_end.add_child(pred)
# Connect loop body back to full condition evaluation.
body_end.add_child(cond_entry)
# Attach joining node
after = CFG_Node()