-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.go
242 lines (228 loc) · 4.96 KB
/
vector.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// Copyright (c) 2022-2023 Markku Rossi
//
// All rights reserved.
//
package scheme
import (
"fmt"
"strings"
"github.com/markkurossi/scheme/types"
)
// Vector implements vector values.
type Vector []Value
// Scheme returns the value as a Scheme string.
func (v Vector) Scheme() string {
return v.String()
}
// Eq tests if the argument value is eq? to this value.
func (v Vector) Eq(o Value) bool {
ov, ok := o.(Vector)
if !ok {
return false
}
if len(v) == 0 && len(ov) == 0 {
return true
}
return false
}
// Equal tests if the argument value is equal to this value.
func (v Vector) Equal(o Value) bool {
ov, ok := o.(Vector)
if !ok || len(v) != len(ov) {
return false
}
for idx, vv := range v {
if !vv.Equal(ov[idx]) {
return false
}
}
return true
}
// Type implements the Value.Type().
func (v Vector) Type() *types.Type {
t := &types.Type{
Enum: types.EnumVector,
}
for _, el := range v {
if el != nil {
t.Element = types.Unify(t.Element, el.Type())
}
}
if t.Element == nil {
t.Element = types.Unspecified
}
return t
}
func (v Vector) String() string {
var str strings.Builder
str.WriteString("#(")
for idx, el := range v {
if idx > 0 {
str.WriteRune(' ')
}
str.WriteString(fmt.Sprintf("%v", el))
}
str.WriteRune(')')
return str.String()
}
var vectorBuiltins = []Builtin{
{
Name: "vector?",
Args: []string{"obj"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
_, ok := args[0].(Vector)
return Boolean(ok), nil
},
},
{
Name: "make-vector",
Args: []string{"k", "[fill<any>]"},
Return: &types.Type{
Enum: types.EnumVector,
Element: types.Any,
},
Native: func(scm *Scheme, args []Value) (Value, error) {
length, err := Int64(args[0])
if err != nil || length < 0 {
return nil, fmt.Errorf("invalid length: %v", args[0])
}
var fill Value
if len(args) == 2 {
fill = args[1]
}
elements := make([]Value, length, length)
for i := 0; i < int(length); i++ {
elements[i] = fill
}
return Vector(elements), nil
},
},
{
Name: "vector",
Args: []string{"obj..."},
Return: &types.Type{
Enum: types.EnumVector,
Element: types.Any,
},
Native: func(scm *Scheme, args []Value) (Value, error) {
v := make([]Value, len(args))
copy(v, args)
return Vector(v), nil
},
},
{
Name: "vector-length",
Args: []string{"vector"},
Return: types.InexactInteger,
Native: func(scm *Scheme, args []Value) (Value, error) {
v, ok := args[0].(Vector)
if !ok {
return nil, fmt.Errorf("not a vector: %v", args[0])
}
return NewNumber(len(v)), nil
},
},
{
Name: "vector-ref",
Args: []string{"vector", "k"},
Return: types.Any,
Native: func(scm *Scheme, args []Value) (Value, error) {
vector, ok := args[0].(Vector)
if !ok {
return nil, fmt.Errorf("invalid vector: %v", args[0])
}
k, err := Int64(args[1])
if err != nil {
return nil, fmt.Errorf("invalid index: %v", args[1])
}
if k < 0 || k >= int64(len(vector)) {
return nil, fmt.Errorf("index %v out of range for vector %v",
k, args[0])
}
return vector[k], nil
},
},
{
Name: "vector-set!",
Args: []string{"vector", "k", "obj"},
Return: &types.Type{
Enum: types.EnumVector,
Element: types.Any,
},
Native: func(scm *Scheme, args []Value) (Value, error) {
vector, ok := args[0].(Vector)
if !ok {
return nil, fmt.Errorf("invalid vector: %v", args[0])
}
k, error := Int64(args[1])
if error != nil {
return nil, fmt.Errorf("invalid index: %v", args[1])
}
if k < 0 || k >= int64(len(vector)) {
return nil, fmt.Errorf("index %v out of range for vector %v",
k, args[0])
}
vector[k] = args[2]
return args[0], nil
},
},
{
Name: "vector->list",
Args: []string{"vector"},
Return: &types.Type{
Enum: types.EnumPair,
Car: types.Any,
Cdr: types.Any,
},
Native: func(scm *Scheme, args []Value) (Value, error) {
vector, ok := args[0].(Vector)
if !ok {
return nil, fmt.Errorf("invalid vector: %v", args[0])
}
var pair Pair
for i := len(vector) - 1; i >= 0; i-- {
pair = NewPair(vector[i], pair)
}
return pair, nil
},
},
{
Name: "list->vector",
Args: []string{"list"},
Return: &types.Type{
Enum: types.EnumVector,
Element: types.Any,
},
Native: func(scm *Scheme, args []Value) (Value, error) {
var elements []Value
err := Map(func(idx int, v Value) error {
elements = append(elements, v)
return nil
}, args[0])
if err != nil {
return nil, err
}
return Vector(elements), nil
},
},
{
Name: "vector-fill!",
Args: []string{"vector", "fill<any>"},
Return: &types.Type{
Enum: types.EnumVector,
Element: types.Any,
},
Native: func(scm *Scheme, args []Value) (Value, error) {
vector, ok := args[0].(Vector)
if !ok {
return nil, fmt.Errorf("invalid vector: %v", args[0])
}
for i := 0; i < len(vector); i++ {
vector[i] = args[1]
}
return args[0], nil
},
},
}