-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair.go
186 lines (151 loc) · 3.26 KB
/
pair.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package value
import "fmt"
var PairClass *Class // ::Std::Pair
// ::Std::Pair::Iterator
//
// Pair iterator class.
var PairIteratorClass *Class
type Pair struct {
Key Value
Value Value
}
// Creates a new Pair.
func PairConstructor(class *Class) Value {
return &Pair{}
}
func NewPair(key, val Value) *Pair {
return &Pair{
Key: key,
Value: val,
}
}
func (*Pair) Class() *Class {
return PairClass
}
func (*Pair) DirectClass() *Class {
return PairClass
}
func (*Pair) SingletonClass() *Class {
return nil
}
func (p *Pair) Copy() Value {
return p
}
func (p *Pair) Inspect() string {
return fmt.Sprintf("Std::Pair{key: %s, value: %s}", p.Key.Inspect(), p.Value.Inspect())
}
func (*Pair) InstanceVariables() SymbolMap {
return nil
}
const pairLength = 2
func (*Pair) Length() int {
return pairLength
}
// Get an element under the given index.
func (p *Pair) Subscript(key Value) (Value, *Error) {
var i int
i, ok := ToGoInt(key)
if !ok {
if i == -1 {
return nil, NewIndexOutOfRangeError(key.Inspect(), pairLength)
}
return nil, NewCoerceError(IntClass, key.Class())
}
return p.Get(i)
}
// Get an element under the given index.
func (p *Pair) Get(index int) (Value, *Error) {
switch index {
case 0, -2:
return p.Key, nil
case 1, -1:
return p.Value, nil
default:
return nil, NewIndexOutOfRangeError(fmt.Sprint(index), pairLength)
}
}
// Set an element under the given index.
func (p *Pair) SubscriptSet(key, val Value) *Error {
i, ok := ToGoInt(key)
if !ok {
if i == -1 {
return NewIndexOutOfRangeError(key.Inspect(), pairLength)
}
return NewCoerceError(IntClass, key.Class())
}
return p.Set(i, val)
}
// Set an element under the given index.
func (p *Pair) Set(index int, val Value) *Error {
switch index {
case 0, -2:
p.Key = val
return nil
case 1, -1:
p.Value = val
return nil
default:
return NewIndexOutOfRangeError(fmt.Sprint(index), pairLength)
}
}
type PairIterator struct {
Pair *Pair
Index int
}
func NewPairIterator(pair *Pair) *PairIterator {
return &PairIterator{
Pair: pair,
}
}
func NewPairIteratorWithIndex(pair *Pair, index int) *PairIterator {
return &PairIterator{
Pair: pair,
Index: index,
}
}
func (*PairIterator) Class() *Class {
return PairIteratorClass
}
func (*PairIterator) DirectClass() *Class {
return PairIteratorClass
}
func (*PairIterator) SingletonClass() *Class {
return nil
}
func (l *PairIterator) Copy() Value {
return &PairIterator{
Pair: l.Pair,
Index: l.Index,
}
}
func (l *PairIterator) Inspect() string {
return fmt.Sprintf("Std::Pair::Iterator{pair: %s, index: %d}", l.Pair.Inspect(), l.Index)
}
func (*PairIterator) InstanceVariables() SymbolMap {
return nil
}
func (l *PairIterator) Next() (Value, Value) {
if l.Index >= pairLength {
return nil, stopIterationSymbol
}
next, err := l.Pair.Get(l.Index)
if err != nil {
return nil, err
}
l.Index++
return next, nil
}
func initPair() {
PairClass = NewClassWithOptions(
ClassWithNoInstanceVariables(),
ClassWithSealed(),
ClassWithConstructor(PairConstructor),
)
PairClass.IncludeMixin(TupleMixin)
StdModule.AddConstantString("Pair", PairClass)
PairIteratorClass = NewClassWithOptions(
ClassWithSealed(),
ClassWithNoInstanceVariables(),
)
PairClass.AddConstantString("Iterator", PairIteratorClass)
}