generateArmsChain generates an if/else chain for a group of arms. This handles multiple patterns for the same constructor with guards.
(arms []*ast.MatchArm)
| 306 | // generateArmsChain generates an if/else chain for a group of arms. |
| 307 | // This handles multiple patterns for the same constructor with guards. |
| 308 | func (g *MatchCodeGen) generateArmsChain(arms []*ast.MatchArm) { |
| 309 | for i, arm := range arms { |
| 310 | hasGuard := arm.Guard != nil |
| 311 | isFirst := i == 0 |
| 312 | isLast := i == len(arms)-1 |
| 313 | |
| 314 | if hasGuard { |
| 315 | guardResult := GenerateExpr(arm.Guard) |
| 316 | |
| 317 | if isFirst { |
| 318 | g.Write("if " + string(guardResult.Output) + " {\n") |
| 319 | } else { |
| 320 | g.Write("} else if " + string(guardResult.Output) + " {\n") |
| 321 | } |
| 322 | } else { |
| 323 | // Unguarded arm |
| 324 | if !isFirst { |
| 325 | // This is the else fallback after guarded arms |
| 326 | g.Write("} else {\n") |
| 327 | } |
| 328 | // If first and unguarded, no wrapper needed (direct code in case) |
| 329 | } |
| 330 | |
| 331 | // Generate body |
| 332 | bodyResult := GenerateExpr(arm.Body) |
| 333 | // Check if body is already a return statement (ReturnExpr) |
| 334 | _, isReturnExpr := arm.Body.(*ast.ReturnExpr) |
| 335 | if g.Match.IsExpr && !isReturnExpr { |
| 336 | // Only prepend "return" if body is not already a return statement |
| 337 | g.Write("return " + string(bodyResult.Output) + "\n") |
| 338 | } else { |
| 339 | g.Buf.Write(bodyResult.Output) |
| 340 | g.WriteByte('\n') |
| 341 | } |
| 342 | |
| 343 | // Close if/else if last arm has guard or previous arms had guards |
| 344 | if isLast && (hasGuard || i > 0 && arms[0].Guard != nil) { |
| 345 | g.Write("}\n") |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // generateGroupedCasesWithAssignment is like generateGroupedCases but for assignment context. |
| 351 | func (g *MatchCodeGen) generateGroupedCasesWithAssignment(varName string, typeSwitch bool) { |
no test coverage detected