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