Source file
src/go/constant/example_test.go
1
2
3
4
5 package constant_test
6
7 import (
8 "fmt"
9 "go/constant"
10 "go/token"
11 "math"
12 "slices"
13 )
14
15 func Example_complexNumbers() {
16
17 ar := constant.MakeFloat64(2.3)
18 ai := constant.MakeImag(constant.MakeInt64(5))
19 a := constant.BinaryOp(ar, token.ADD, ai)
20
21
22 b := constant.MakeUint64(11)
23 c := constant.BinaryOp(a, token.MUL, b)
24
25
26 Ar, exact := constant.Float64Val(constant.Real(c))
27 if !exact {
28 fmt.Printf("Could not represent real part %s exactly as float64\n", constant.Real(c))
29 }
30 Ai, exact := constant.Float64Val(constant.Imag(c))
31 if !exact {
32 fmt.Printf("Could not represent imaginary part %s as exactly as float64\n", constant.Imag(c))
33 }
34 C := complex(Ar, Ai)
35
36 fmt.Println("literal", 25.3+55i)
37 fmt.Println("go/constant", c)
38 fmt.Println("complex128", C)
39
40
41
42
43
44
45
46 }
47
48 func ExampleBinaryOp() {
49
50 a := constant.MakeUint64(11)
51 b := constant.MakeFloat64(0.5)
52 c := constant.BinaryOp(a, token.QUO, b)
53 fmt.Println(c)
54
55
56 }
57
58 func ExampleUnaryOp() {
59 vs := []constant.Value{
60 constant.MakeBool(true),
61 constant.MakeFloat64(2.7),
62 constant.MakeUint64(42),
63 }
64
65 for i, v := range vs {
66 switch v.Kind() {
67 case constant.Bool:
68 vs[i] = constant.UnaryOp(token.NOT, v, 0)
69
70 case constant.Float:
71 vs[i] = constant.UnaryOp(token.SUB, v, 0)
72
73 case constant.Int:
74
75
76 vs[i] = constant.UnaryOp(token.XOR, v, 16)
77 }
78 }
79
80 for _, v := range vs {
81 fmt.Println(v)
82 }
83
84
85
86
87
88
89 }
90
91 func ExampleCompare() {
92 vs := []constant.Value{
93 constant.MakeString("Z"),
94 constant.MakeString("bacon"),
95 constant.MakeString("go"),
96 constant.MakeString("Frame"),
97 constant.MakeString("defer"),
98 constant.MakeFromLiteral(`"a"`, token.STRING, 0),
99 }
100
101 slices.SortFunc(vs, func(a, b constant.Value) int {
102 if constant.Compare(a, token.LSS, b) {
103 return -1
104 }
105 if constant.Compare(a, token.GTR, b) {
106 return +1
107 }
108 return 0
109 })
110
111 for _, v := range vs {
112 fmt.Println(constant.StringVal(v))
113 }
114
115
116
117
118
119
120
121
122
123 }
124
125 func ExampleSign() {
126 zero := constant.MakeInt64(0)
127 one := constant.MakeInt64(1)
128 negOne := constant.MakeInt64(-1)
129
130 mkComplex := func(a, b constant.Value) constant.Value {
131 b = constant.MakeImag(b)
132 return constant.BinaryOp(a, token.ADD, b)
133 }
134
135 vs := []constant.Value{
136 negOne,
137 mkComplex(zero, negOne),
138 mkComplex(one, negOne),
139 mkComplex(negOne, one),
140 mkComplex(negOne, negOne),
141 zero,
142 mkComplex(zero, zero),
143 one,
144 mkComplex(zero, one),
145 mkComplex(one, one),
146 }
147
148 for _, v := range vs {
149 fmt.Printf("% d %s\n", constant.Sign(v), v)
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164 }
165
166 func ExampleVal() {
167 maxint := constant.MakeInt64(math.MaxInt64)
168 fmt.Printf("%v\n", constant.Val(maxint))
169
170 e := constant.MakeFloat64(math.E)
171 fmt.Printf("%v\n", constant.Val(e))
172
173 b := constant.MakeBool(true)
174 fmt.Printf("%v\n", constant.Val(b))
175
176 b = constant.Make(false)
177 fmt.Printf("%v\n", constant.Val(b))
178
179
180
181
182
183
184
185 }
186
View as plain text