Skip to content

Commit 104ba11

Browse files
authored
Merge pull request #325 from hjotha/refactor/builder-extract-anchor-helpers
refactor: extract microflow anchor selection helpers
2 parents 54fc4c7 + 58e660c commit 104ba11

3 files changed

Lines changed: 19 additions & 21 deletions

File tree

mdl/executor/cmd_microflows_builder_control.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ func (fb *flowBuilder) addIfStatement(s *ast.IfStmt) model.ID {
125125
// Destination: prefer the first statement's own @anchor(to: ...) if it
126126
// has one; otherwise fall back to trueBranchAnchor.To.
127127
flow := newHorizontalFlowWithCase(splitID, actID, "true")
128-
applyUserAnchors(flow, trueBranchAnchor, trueBranchAnchor)
129-
if thisAnchor != nil && thisAnchor.To != ast.AnchorSideUnset {
130-
flow.DestinationConnectionIndex = int(thisAnchor.To)
131-
}
128+
applyUserAnchors(flow, trueBranchAnchor, branchDestinationAnchor(trueBranchAnchor, thisAnchor))
132129
fb.flows = append(fb.flows, flow)
133130
} else {
134131
flow := newHorizontalFlow(lastThenID, actID)
@@ -180,10 +177,7 @@ func (fb *flowBuilder) addIfStatement(s *ast.IfStmt) model.ID {
180177
// First statement in ELSE - connect from split going down (false path).
181178
// Same compositional rule as the THEN branch.
182179
flow := newDownwardFlowWithCase(splitID, actID, "false")
183-
applyUserAnchors(flow, falseBranchAnchor, falseBranchAnchor)
184-
if thisAnchor != nil && thisAnchor.To != ast.AnchorSideUnset {
185-
flow.DestinationConnectionIndex = int(thisAnchor.To)
186-
}
180+
applyUserAnchors(flow, falseBranchAnchor, branchDestinationAnchor(falseBranchAnchor, thisAnchor))
187181
fb.flows = append(fb.flows, flow)
188182
} else {
189183
flow := newHorizontalFlow(lastElseID, actID)
@@ -241,10 +235,7 @@ func (fb *flowBuilder) addIfStatement(s *ast.IfStmt) model.ID {
241235
if lastThenID == "" {
242236
// First statement in THEN - connect from split going down with "true" case
243237
flow := newDownwardFlowWithCase(splitID, actID, "true")
244-
applyUserAnchors(flow, trueBranchAnchor, trueBranchAnchor)
245-
if thisAnchor != nil && thisAnchor.To != ast.AnchorSideUnset {
246-
flow.DestinationConnectionIndex = int(thisAnchor.To)
247-
}
238+
applyUserAnchors(flow, trueBranchAnchor, branchDestinationAnchor(trueBranchAnchor, thisAnchor))
248239
fb.flows = append(fb.flows, flow)
249240
} else {
250241
flow := newHorizontalFlow(lastThenID, actID)

mdl/executor/cmd_microflows_builder_flows.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,20 @@ func applyUserAnchors(flow *microflows.SequenceFlow, origin *ast.FlowAnchors, de
191191
}
192192
}
193193

194+
func branchDestinationAnchor(branchAnchor, stmtAnchor *ast.FlowAnchors) *ast.FlowAnchors {
195+
if stmtAnchor != nil && stmtAnchor.To != ast.AnchorSideUnset {
196+
return stmtAnchor
197+
}
198+
return branchAnchor
199+
}
200+
201+
func pendingFlowAnchors(previousAnchor, pendingAnchor, stmtAnchor *ast.FlowAnchors) (*ast.FlowAnchors, *ast.FlowAnchors) {
202+
if pendingAnchor == nil {
203+
return previousAnchor, stmtAnchor
204+
}
205+
return pendingAnchor, branchDestinationAnchor(pendingAnchor, stmtAnchor)
206+
}
207+
194208
// lastStmtIsReturn reports whether execution of a body is guaranteed to terminate
195209
// (via RETURN or RAISE ERROR) on every path — i.e. control can never fall off the
196210
// end of the body into the parent flow.

mdl/executor/cmd_microflows_builder_graph.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,8 @@ func (fb *flowBuilder) buildFlowGraph(stmts []ast.MicroflowStatement, returns *a
8888
// both From (origin side on the split) and To (the side of the
8989
// continuing activity), unless the incoming statement explicitly
9090
// overrides its own To.
91-
originAnchor := fb.previousStmtAnchor
92-
destAnchor := stmtAnchor
93-
if pendingFlowAnchor != nil {
94-
originAnchor = pendingFlowAnchor
95-
if destAnchor == nil || destAnchor.To == ast.AnchorSideUnset {
96-
destAnchor = pendingFlowAnchor
97-
}
98-
pendingFlowAnchor = nil
99-
}
91+
originAnchor, destAnchor := pendingFlowAnchors(fb.previousStmtAnchor, pendingFlowAnchor, stmtAnchor)
92+
pendingFlowAnchor = nil
10093
applyUserAnchors(flow, originAnchor, destAnchor)
10194
fb.flows = append(fb.flows, flow)
10295
fb.previousStmtAnchor = stmtAnchor

0 commit comments

Comments
 (0)