forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_unary.go
116 lines (107 loc) · 2.44 KB
/
op_unary.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package gno
import (
"fmt"
"math/big"
)
func (m *Machine) doOpUpos() {
ux := m.PopExpr().(*UnaryExpr)
if debug {
debug.Printf("doOpUpos(%v)\n", ux)
}
// nothing to do, +x is just x?
}
func (m *Machine) doOpUneg() {
ux := m.PopExpr().(*UnaryExpr)
if debug {
debug.Printf("doOpUneg(%v)\n", ux)
}
xv := m.PeekValue(1)
// Switch on the base type.
// NOTE: this is faster than computing the kind of kv.T.
switch baseOf(xv.T) {
case IntType:
xv.SetInt(-xv.GetInt())
case Int8Type:
xv.SetInt8(-xv.GetInt8())
case Int16Type:
xv.SetInt16(-xv.GetInt16())
case Int32Type:
xv.SetInt32(-xv.GetInt32())
case Int64Type:
xv.SetInt64(-xv.GetInt64())
case UintType:
xv.SetUint(-xv.GetUint())
case Uint8Type:
xv.SetUint8(-xv.GetUint8())
case Uint16Type:
xv.SetUint16(-xv.GetUint16())
case Uint32Type:
xv.SetUint32(-xv.GetUint32())
case Uint64Type:
xv.SetUint64(-xv.GetUint64())
case UntypedBigintType, BigintType:
bv := xv.V.(BigintValue)
xv.V = BigintValue{V: new(big.Int).Neg(bv.V)}
case nil:
// NOTE: for now only BigintValue is possible.
bv := xv.V.(BigintValue)
xv.V = BigintValue{V: new(big.Int).Neg(bv.V)}
default:
panic(fmt.Sprintf("unexpected type %s in operation",
baseOf(xv.T)))
}
}
func (m *Machine) doOpUnot() {
ux := m.PopExpr().(*UnaryExpr)
if debug {
debug.Printf("doOpUnot(%v)\n", ux)
}
xv := m.PeekValue(1)
// Switch on the base type.
switch baseOf(xv.T) {
case BoolType, UntypedBoolType:
xv.SetBool(!xv.GetBool())
default:
panic(fmt.Sprintf("unexpected type %s in operation",
baseOf(xv.T)))
}
}
func (m *Machine) doOpUxor() {
ux := m.PopExpr().(*UnaryExpr)
if debug {
debug.Printf("doOpUxor(%v)\n", ux)
}
xv := m.PeekValue(1)
// Switch on the base type.
switch baseOf(xv.T) {
case IntType:
xv.SetInt(^xv.GetInt())
case Int8Type:
xv.SetInt8(^xv.GetInt8())
case Int16Type:
xv.SetInt16(^xv.GetInt16())
case Int32Type:
xv.SetInt32(^xv.GetInt32())
case Int64Type:
xv.SetInt64(^xv.GetInt64())
case UintType:
xv.SetUint(^xv.GetUint())
case Uint8Type:
xv.SetUint8(^xv.GetUint8())
case Uint16Type:
xv.SetUint16(^xv.GetUint16())
case Uint32Type:
xv.SetUint32(^xv.GetUint32())
case Uint64Type:
xv.SetUint64(^xv.GetUint64())
case UntypedBigintType, BigintType:
// XXX can it even be implemented?
panic("not yet implemented")
default:
panic(fmt.Sprintf("unexpected type %s in operation",
baseOf(xv.T)))
}
}
func (m *Machine) doOpUrecv() {
panic("not yet implemented")
}