forked from gopherjs/gopherjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bool.go
44 lines (41 loc) · 826 Bytes
/
bool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package analysis
import (
"go/ast"
"go/token"
"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/types"
)
func BoolValue(expr ast.Expr, info *types.Info) (bool, bool) {
v := info.Types[expr].Value
if v != nil && v.Kind() == exact.Bool {
return exact.BoolVal(v), true
}
switch e := expr.(type) {
case *ast.BinaryExpr:
switch e.Op {
case token.LAND:
if b, ok := BoolValue(e.X, info); ok {
if !b {
return false, true
}
return BoolValue(e.Y, info)
}
case token.LOR:
if b, ok := BoolValue(e.X, info); ok {
if b {
return true, true
}
return BoolValue(e.Y, info)
}
}
case *ast.UnaryExpr:
if e.Op == token.NOT {
if b, ok := BoolValue(e.X, info); ok {
return !b, true
}
}
case *ast.ParenExpr:
return BoolValue(e.X, info)
}
return false, false
}