@@ -415,7 +415,7 @@ func (c *Compiler) hasCondExprInTree(expr ast.Expression) bool {
415415 return false
416416}
417417
418- func (c * Compiler ) logicalOrCondExpr (expr ast.Expression ) (* ast.InfixExpression , bool ) {
418+ func (c * Compiler ) fallbackOrExpr (expr ast.Expression ) (* ast.InfixExpression , bool ) {
419419 infix , ok := ast .IsLogicalOr (expr )
420420 if ! ok {
421421 return nil , false
@@ -425,12 +425,12 @@ func (c *Compiler) logicalOrCondExpr(expr ast.Expression) (*ast.InfixExpression,
425425 return infix , info .HasFallbackOr ()
426426}
427427
428- func (c * Compiler ) hasLogicalOrCondExprInTree (expr ast.Expression ) bool {
429- if _ , ok := c .logicalOrCondExpr (expr ); ok {
428+ func (c * Compiler ) hasFallbackOrInTree (expr ast.Expression ) bool {
429+ if _ , ok := c .fallbackOrExpr (expr ); ok {
430430 return true
431431 }
432432 for _ , child := range ast .ExprChildren (expr ) {
433- if c .hasLogicalOrCondExprInTree (child ) {
433+ if c .hasFallbackOrInTree (child ) {
434434 return true
435435 }
436436 }
@@ -462,31 +462,26 @@ func (c *Compiler) handleComparisons(op string, left, right []*Symbol, info *Exp
462462 return lhsSyms , cond
463463}
464464
465- // extractCondExprs walks the expression tree, evaluates each CondScalar
466- // comparison, ANDs results into cond, and stores LHS values in the
467- // statement-local condLHS map for substitution during later value compilation.
468- // LHS temporaries are appended to temps so the caller can free them on the
469- // false path.
465+ // extractCondExprs evaluates value-position comparisons and stores their LHS
466+ // values for substitution during later value compilation.
470467func (c * Compiler ) extractCondExprs (expr ast.Expression , cond llvm.Value , temps []condTemp ) (llvm.Value , []condTemp ) {
471468 info := c .ExprCache [key (c .FuncNameMangled , expr )]
472469
473- // Handle conditional expression (comparison in value position).
474470 // Comparisons with ranges can be extracted only when all required iterators
475471 // are already bound by an outer loop (no pending ranges).
476472 if infix , ok := expr .(* ast.InfixExpression ); ok && info .HasCondScalar () && len (c .pendingLoopRanges (info .Ranges )) == 0 {
477473 cond , temps = c .extractCondExprs (infix .Left , cond , temps )
478474 cond , temps = c .extractCondExprs (infix .Right , cond , temps )
479- return c .extractInfixCondExpr (infix , info , cond , temps )
475+ return c .extractCondComparison (infix , info , cond , temps )
480476 }
481477
482- // Not a conditional expression — recurse into children
483478 for _ , child := range ast .ExprChildren (expr ) {
484479 cond , temps = c .extractCondExprs (child , cond , temps )
485480 }
486481 return cond , temps
487482}
488483
489- func (c * Compiler ) extractInfixCondExpr (infix * ast.InfixExpression , info * ExprInfo , cond llvm.Value , temps []condTemp ) (llvm.Value , []condTemp ) {
484+ func (c * Compiler ) extractCondComparison (infix * ast.InfixExpression , info * ExprInfo , cond llvm.Value , temps []condTemp ) (llvm.Value , []condTemp ) {
490485 left := c .compileExpression (infix .Left , nil )
491486 right := c .compileExpression (infix .Right , nil )
492487
@@ -495,8 +490,8 @@ func (c *Compiler) extractInfixCondExpr(infix *ast.InfixExpression, info *ExprIn
495490
496491 c .requireCondLHSFrame ()[key (c .FuncNameMangled , infix )] = lhsSyms
497492 temps = append (temps , condTemp {infix .Left , left })
498- // Free right-side temporaries (only used for comparison).
499- // Left-side values are retained in condLHS for later substitution.
493+
494+ // Right is comparison-only; left is retained for condLHS substitution.
500495 c .freeTemporary (infix .Right , right )
501496 return cond , temps
502497}
@@ -517,36 +512,28 @@ func (c *Compiler) cleanupCondExprElse(temps []condTemp) {
517512 }
518513}
519514
520- // compileCondExprValue extracts cond-expr predicates for expr, branches through
521- // value-position conditions, and compiles onTrue on admitted paths only. Plain
522- // comparisons compose as AND; value-position logical OR tries the left operand
523- // first and falls back to the right operand only on the left false path.
515+ // compileCondExprValue gates value-position cond expressions. Comparisons
516+ // compose as AND; fallback OR tries the right side only after the left fails.
524517func (c * Compiler ) compileCondExprValue (expr ast.Expression , baseCond llvm.Value , onTrue func ()) {
525518 c .pushCondLHSFrame ()
526519 defer c .popCondLHSFrame ()
527520
528- c .compileCondExprValueInFrame (expr , baseCond , onTrue )
529- }
530-
531- func (c * Compiler ) compileCondExprValueInFrame (expr ast.Expression , baseCond llvm.Value , onTrue func ()) {
532- if c .hasLogicalOrCondExprInTree (expr ) {
533- c .compileCondExprWithFailure (expr , baseCond , onTrue , func () {})
521+ if c .hasFallbackOrInTree (expr ) {
522+ c .compileYield (expr , baseCond , onTrue , func () {})
534523 return
535524 }
536525
537526 cond , temps := c .extractCondExprs (expr , baseCond , nil )
538- c .compileCondExprBranch (cond , temps , onTrue , func () {})
527+ c .branchCond (cond , temps , onTrue , func () {})
539528}
540529
541- // compileOperandCondExprValue extracts conditional expressions from expr's
542- // operands while leaving expr itself to the caller. Use this when the caller is
543- // compiling the root operation into an already-seeded output slot.
544- func (c * Compiler ) compileOperandCondExprValue (expr ast.Expression , baseCond llvm.Value , onTrue func ()) {
530+ // compileCondOperands leaves expr itself to the caller.
531+ func (c * Compiler ) compileCondOperands (expr ast.Expression , baseCond llvm.Value , onTrue func ()) {
545532 c .pushCondLHSFrame ()
546533 defer c .popCondLHSFrame ()
547534
548- if c .hasLogicalOrCondExprInTree (expr ) {
549- c .compileCondExprChildrenInFrame (ast .ExprChildren (expr ), baseCond , onTrue , func () {})
535+ if c .hasFallbackOrInTree (expr ) {
536+ c .compileChildYields (ast .ExprChildren (expr ), baseCond , onTrue , func () {})
550537 return
551538 }
552539
@@ -555,10 +542,10 @@ func (c *Compiler) compileOperandCondExprValue(expr ast.Expression, baseCond llv
555542 for _ , child := range ast .ExprChildren (expr ) {
556543 cond , temps = c .extractCondExprs (child , cond , temps )
557544 }
558- c .compileCondExprBranch (cond , temps , onTrue , func () {})
545+ c .branchCond (cond , temps , onTrue , func () {})
559546}
560547
561- func (c * Compiler ) compileCondExprBranch (cond llvm.Value , temps []condTemp , onTrue func (), onFalse func ()) {
548+ func (c * Compiler ) branchCond (cond llvm.Value , temps []condTemp , onTrue func (), onFalse func ()) {
562549 if cond .IsNil () {
563550 onTrue ()
564551 return
@@ -578,42 +565,42 @@ func (c *Compiler) compileCondExprBranch(cond llvm.Value, temps []condTemp, onTr
578565 c .builder .SetInsertPointAtEnd (contBlock )
579566}
580567
581- func (c * Compiler ) compileCondExprChildrenInFrame (children []ast.Expression , baseCond llvm.Value , onTrue func (), onFalse func ()) {
568+ func (c * Compiler ) compileChildYields (children []ast.Expression , baseCond llvm.Value , onTrue func (), onFalse func ()) {
582569 if len (children ) == 0 {
583- c .compileCondExprBranch (baseCond , nil , onTrue , onFalse )
570+ c .branchCond (baseCond , nil , onTrue , onFalse )
584571 return
585572 }
586573
587574 child := children [0 ]
588575 rest := children [1 :]
589576 if c .hasCondExprInTree (child ) {
590- c .compileCondExprWithFailure (child , baseCond , func () {
591- c .compileCondExprChildrenInFrame (rest , llvm.Value {}, onTrue , onFalse )
577+ c .compileYield (child , baseCond , func () {
578+ c .compileChildYields (rest , llvm.Value {}, onTrue , onFalse )
592579 }, onFalse )
593580 return
594581 }
595582
596- c .compileCondExprChildrenInFrame (rest , baseCond , onTrue , onFalse )
583+ c .compileChildYields (rest , baseCond , onTrue , onFalse )
597584}
598585
599- func (c * Compiler ) compileCondExprWithFailure (expr ast.Expression , baseCond llvm.Value , onTrue func (), onFalse func ()) {
600- if logicalOr , ok := c .logicalOrCondExpr (expr ); ok {
601- c .compileLogicalOrCondExprWithFailure (logicalOr , baseCond , onTrue , onFalse )
586+ func (c * Compiler ) compileYield (expr ast.Expression , baseCond llvm.Value , onTrue func (), onFalse func ()) {
587+ if logicalOr , ok := c .fallbackOrExpr (expr ); ok {
588+ c .compileFallbackOr (logicalOr , baseCond , onTrue , onFalse )
602589 return
603590 }
604591
605- if ! c .hasLogicalOrCondExprInTree (expr ) {
592+ if ! c .hasFallbackOrInTree (expr ) {
606593 cond , temps := c .extractCondExprs (expr , baseCond , nil )
607- c .compileCondExprBranch (cond , temps , onTrue , onFalse )
594+ c .branchCond (cond , temps , onTrue , onFalse )
608595 return
609596 }
610597
611- c .compileCondExprChildrenInFrame (ast .ExprChildren (expr ), baseCond , func () {
598+ c .compileChildYields (ast .ExprChildren (expr ), baseCond , func () {
612599 if infix , ok := expr .(* ast.InfixExpression ); ok {
613600 info := c .ExprCache [key (c .FuncNameMangled , infix )]
614601 if info != nil && info .HasCondScalar () && len (c .pendingLoopRanges (info .Ranges )) == 0 {
615- cond , temps := c .extractInfixCondExpr (infix , info , llvm.Value {}, nil )
616- c .compileCondExprBranch (cond , temps , onTrue , onFalse )
602+ cond , temps := c .extractCondComparison (infix , info , llvm.Value {}, nil )
603+ c .branchCond (cond , temps , onTrue , onFalse )
617604 return
618605 }
619606 }
@@ -630,10 +617,10 @@ func (c *Compiler) withCondLHS(expr ast.Expression, syms []*Symbol, body func())
630617 body ()
631618}
632619
633- func (c * Compiler ) compileLogicalOrCondExprWithFailure (expr * ast.InfixExpression , baseCond llvm.Value , onTrue func (), onFalse func ()) {
620+ func (c * Compiler ) compileFallbackOr (expr * ast.InfixExpression , baseCond llvm.Value , onTrue func (), onFalse func ()) {
634621 if ! baseCond .IsNil () {
635- c .compileCondExprBranch (baseCond , nil , func () {
636- c .compileLogicalOrCondExprWithFailure (expr , llvm.Value {}, onTrue , onFalse )
622+ c .branchCond (baseCond , nil , func () {
623+ c .compileFallbackOr (expr , llvm.Value {}, onTrue , onFalse )
637624 }, onFalse )
638625 return
639626 }
@@ -647,8 +634,8 @@ func (c *Compiler) compileLogicalOrCondExprWithFailure(expr *ast.InfixExpression
647634 c .withCondLHS (expr , right , onTrue )
648635 }
649636
650- c .compileCondExprWithFailure (expr .Left , llvm.Value {}, leftTrue , func () {
651- c .compileCondExprWithFailure (expr .Right , llvm.Value {}, rightTrue , onFalse )
637+ c .compileYield (expr .Left , llvm.Value {}, leftTrue , func () {
638+ c .compileYield (expr .Right , llvm.Value {}, rightTrue , onFalse )
652639 })
653640}
654641
0 commit comments