-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
81 lines (70 loc) · 1.53 KB
/
expr.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
)
func main() {
expr, err := parser.ParseExpr("shape.color==color.red")
if err != nil {
panic(err)
}
// Checking if the expression was binary.
bExpr, ok := expr.(*ast.BinaryExpr)
if !ok {
panic("expr is not a binary expr.")
}
// If the operation is not “==”, die.
if bExpr.Op != token.EQL {
panic("the op should have been ==.")
}
// Left must be a selector expr, meaning follow with a selector which is “dot” in this case.
left, ok := bExpr.X.(*ast.SelectorExpr)
if !ok {
panic("left should have been a selector expr.")
}
// Same as above.
right, ok := bExpr.Y.(*ast.SelectorExpr)
if !ok {
panic("right should have been a selector expr.")
}
// Checking for attributes.
if left.Sel.Name != "color" {
panic("left should have had a color attr.")
}
// Same as above.
if right.Sel.Name != "red" {
panic("right should have had a red attr.")
}
// Then we finally gofmt the code and print it to stdout.
if err := format.Node(os.Stdout, token.NewFileSet(), expr); err != nil {
panic(err)
}
fmt.Println()
// 手动构建语法树,生成相同代码
newExpr := &ast.BinaryExpr{
X: &ast.SelectorExpr{
X: &ast.Ident{
Name: "shape",
},
Sel: &ast.Ident{
Name: "color",
},
},
Op: token.EQL,
Y: &ast.SelectorExpr{
X: &ast.Ident{
Name: "color",
},
Sel: &ast.Ident{
Name: "red",
},
},
}
if err := format.Node(os.Stdout, token.NewFileSet(), newExpr); err != nil {
panic(err)
}
}