github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/compiler/analysis/bool.go (about)

     1  package analysis
     2  
     3  import (
     4  	"go/ast"
     5  	"go/constant"
     6  	"go/token"
     7  	"go/types"
     8  )
     9  
    10  func BoolValue(expr ast.Expr, info *types.Info) (bool, bool) {
    11  	v := info.Types[expr].Value
    12  	if v != nil && v.Kind() == constant.Bool {
    13  		return constant.BoolVal(v), true
    14  	}
    15  	switch e := expr.(type) {
    16  	case *ast.BinaryExpr:
    17  		switch e.Op {
    18  		case token.LAND:
    19  			if b, ok := BoolValue(e.X, info); ok {
    20  				if !b {
    21  					return false, true
    22  				}
    23  				return BoolValue(e.Y, info)
    24  			}
    25  		case token.LOR:
    26  			if b, ok := BoolValue(e.X, info); ok {
    27  				if b {
    28  					return true, true
    29  				}
    30  				return BoolValue(e.Y, info)
    31  			}
    32  		}
    33  	case *ast.UnaryExpr:
    34  		if e.Op == token.NOT {
    35  			if b, ok := BoolValue(e.X, info); ok {
    36  				return !b, true
    37  			}
    38  		}
    39  	case *ast.ParenExpr:
    40  		return BoolValue(e.X, info)
    41  	}
    42  	return false, false
    43  }