This repository has been archived by the owner on May 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalue.go
165 lines (137 loc) · 3.94 KB
/
value.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
package sabre
import "reflect"
// Value represents data/forms in sabre. This includes those emitted by
// Reader, values obtained as result of an evaluation etc.
type Value interface {
// String should return the LISP representation of the value.
String() string
// Eval should evaluate this value against the scope and return
// the resultant value or an evaluation error.
Eval(scope Scope) (Value, error)
}
// Invokable represents any value that supports invocation. Vector, Fn
// etc support invocation.
type Invokable interface {
Value
Invoke(scope Scope, args ...Value) (Value, error)
}
// Seq implementations represent a sequence/list of values.
type Seq interface {
Value
// First should return first value of the sequence or nil if the
// sequence is empty.
First() Value
// Next should return the remaining sequence when the first value
// is excluded.
Next() Seq
// Cons should add the value to the beginning of the sequence and
// return the new sequence.
Cons(v Value) Seq
// Conj should join the given values to the sequence and return a
// new sequence.
Conj(vals ...Value) Seq
}
// Compare compares two values in an identity independent manner. If
// v1 has `Compare(Value) bool` method, the comparison is delegated to
// it as `v1.Compare(v2)`.
func Compare(v1, v2 Value) bool {
if (v1 == nil && v2 == nil) ||
(v1 == (Nil{}) && v2 == (Nil{})) {
return true
}
if cmp, ok := v1.(comparable); ok {
return cmp.Compare(v2)
}
return reflect.DeepEqual(v1, v2)
}
// comparable can be implemented by Value types to support comparison.
// See Compare().
type comparable interface {
Value
Compare(other Value) bool
}
// Values represents a list of values and implements the Seq interface.
type Values []Value
// Eval returns itself.
func (vals Values) Eval(_ Scope) (Value, error) { return vals, nil }
// First returns the first value in the list if the list is not empty.
// Returns Nil{} otherwise.
func (vals Values) First() Value {
if len(vals) == 0 {
return nil
}
return vals[0]
}
// Next returns a new sequence containing values after the first one. If
// there are no values to create a next sequence, returns nil.
func (vals Values) Next() Seq {
if len(vals) <= 1 {
return nil
}
return &List{Values: Values(vals[1:])}
}
// Cons returns a new sequence where 'v' is prepended to the values.
func (vals Values) Cons(v Value) Seq {
return &List{Values: append(Values{v}, vals...)}
}
// Conj returns a new sequence where 'v' is appended to the values.
func (vals Values) Conj(args ...Value) Seq {
return &List{Values: append(vals, args...)}
}
// Size returns the number of items in the list.
func (vals Values) Size() int { return len(vals) }
// Compare compares the values in this sequence to the other sequence.
// other sequence will be realized for comparison.
func (vals Values) Compare(v Value) bool {
other, ok := v.(Seq)
if !ok {
return false
}
if s, hasSize := other.(interface {
Size() int
}); hasSize {
if vals.Size() != s.Size() {
return false
}
}
var this Seq = vals
isEqual := true
for this != nil && other != nil {
v1, v2 := this.First(), other.First()
isEqual = isEqual && Compare(v1, v2)
if !isEqual {
break
}
this = this.Next()
other = other.Next()
}
return isEqual && (this == nil && other == nil)
}
// Uniq removes all the duplicates from the given value array.
// TODO: remove this naive implementation
func (vals Values) Uniq() []Value {
var result []Value
hashSet := map[string]struct{}{}
for _, v := range vals {
src := v.String()
if _, found := hashSet[src]; !found {
hashSet[src] = struct{}{}
result = append(result, v)
}
}
return result
}
func (vals Values) String() string {
return containerString(vals, "(", ")", " ")
}
func evalValueList(scope Scope, vals []Value) ([]Value, error) {
var result []Value
for _, arg := range vals {
v, err := arg.Eval(scope)
if err != nil {
return nil, newEvalErr(arg, err)
}
result = append(result, v)
}
return result, nil
}