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 "sort"
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 sort.Slice(vs, func(i, j int) bool {
102
103 return constant.Compare(vs[i], token.LEQ, vs[j])
104 })
105
106 for _, v := range vs {
107 fmt.Println(constant.StringVal(v))
108 }
109
110
111
112
113
114
115
116
117
118 }
119
120 func ExampleSign() {
121 zero := constant.MakeInt64(0)
122 one := constant.MakeInt64(1)
123 negOne := constant.MakeInt64(-1)
124
125 mkComplex := func(a, b constant.Value) constant.Value {
126 b = constant.MakeImag(b)
127 return constant.BinaryOp(a, token.ADD, b)
128 }
129
130 vs := []constant.Value{
131 negOne,
132 mkComplex(zero, negOne),
133 mkComplex(one, negOne),
134 mkComplex(negOne, one),
135 mkComplex(negOne, negOne),
136 zero,
137 mkComplex(zero, zero),
138 one,
139 mkComplex(zero, one),
140 mkComplex(one, one),
141 }
142
143 for _, v := range vs {
144 fmt.Printf("% d %s\n", constant.Sign(v), v)
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159 }
160
161 func ExampleVal() {
162 maxint := constant.MakeInt64(math.MaxInt64)
163 fmt.Printf("%v\n", constant.Val(maxint))
164
165 e := constant.MakeFloat64(math.E)
166 fmt.Printf("%v\n", constant.Val(e))
167
168 b := constant.MakeBool(true)
169 fmt.Printf("%v\n", constant.Val(b))
170
171 b = constant.Make(false)
172 fmt.Printf("%v\n", constant.Val(b))
173
174
175
176
177
178
179
180 }
181
View as plain text