forked from zhemao/glisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparisons.go
218 lines (199 loc) · 4.73 KB
/
comparisons.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
package glisp
import (
"bytes"
"fmt"
"strconv"
)
type Comparable interface {
Sexp
// Cmp compares x and y and returns:
//
// -1 if x < y
// 0 if x == y
// +1 if x > y
//
Cmp(Comparable) (int, error)
}
func Compare(a Sexp, b Sexp) (int, error) {
if a != SexpNull && b == SexpNull {
return 1, nil
}
switch at := a.(type) {
case SexpInt:
return compareInt(at, b)
case SexpChar:
return compareChar(at, b)
case SexpFloat:
return compareFloat(at, b)
case SexpBool:
return compareBool(at, b)
case SexpStr:
return compareString(at, b)
case SexpSymbol:
return compareSymbol(at, b)
case *SexpPair:
return comparePair(at, b)
case SexpArray:
return compareArray(at, b)
case SexpSentinel:
if at == SexpNull && b == SexpNull {
return 0, nil
} else {
return -1, nil
}
case SexpBytes:
return compareBytes(at, b)
}
if isComparable(a) && isComparable(b) {
return a.(Comparable).Cmp(b.(Comparable))
}
return 0, fmt.Errorf("cannot compare %s to %s", InspectType(a), InspectType(b))
}
func compareFloat(f SexpFloat, expr Sexp) (int, error) {
switch e := expr.(type) {
case SexpInt:
return compareFloatAndInt(f, e), nil
case SexpFloat:
return f.Cmp(e), nil
case SexpChar:
return f.Cmp(NewSexpFloat(float64(e))), nil
}
return 0, fmt.Errorf("cannot compare %s to %s", InspectType(f), InspectType(expr))
}
func compareIntAndFloat(e SexpInt, f SexpFloat) int {
return -compareFloatAndInt(f, e)
}
func compareFloatAndInt(f SexpFloat, e SexpInt) int {
return f.Cmp(NewSexpFloatInt(e))
}
func compareBetweenInt(f, e SexpInt) int {
return f.v.Cmp(e.v)
}
func compareInt(i SexpInt, expr Sexp) (int, error) {
switch e := expr.(type) {
case SexpInt:
return compareBetweenInt(i, e), nil
case SexpFloat:
return compareIntAndFloat(i, e), nil
case SexpChar:
si, _ := NewSexpIntStr(strconv.FormatInt(int64(byte(e)), 10))
return compareBetweenInt(i, si), nil
}
return 0, fmt.Errorf("cannot compare %s to %s", InspectType(i), InspectType(expr))
}
func compareChar(c SexpChar, expr Sexp) (int, error) {
switch e := expr.(type) {
case SexpInt:
return compareBetweenInt(NewSexpInt(int(c)), e), nil
case SexpFloat:
return NewSexpFloat(float64(c)).Cmp(e), nil
case SexpChar:
ci := NewSexpInt64(int64(byte(c)))
ei := NewSexpInt64(int64(byte(e)))
return compareBetweenInt(ci, ei), nil
}
return 0, fmt.Errorf("cannot compare %s to %s", InspectType(c), InspectType(expr))
}
func compareString(s SexpStr, expr Sexp) (int, error) {
switch e := expr.(type) {
case SexpStr:
return bytes.Compare([]byte(s), []byte(e)), nil
case SexpBytes:
return bytes.Compare([]byte(s), e.bytes), nil
}
return 0, fmt.Errorf("cannot compare %s to %s", InspectType(s), InspectType(expr))
}
func compareBytes(s SexpBytes, expr Sexp) (int, error) {
switch e := expr.(type) {
case SexpBytes:
return bytes.Compare(s.bytes, e.bytes), nil
case SexpStr:
return bytes.Compare(s.bytes, []byte(e)), nil
}
return 0, fmt.Errorf("cannot compare %s to %s", InspectType(s), InspectType(expr))
}
func compareSymbol(sym SexpSymbol, expr Sexp) (int, error) {
switch e := expr.(type) {
case SexpSymbol:
return compareBetweenInt(NewSexpInt(sym.Number()), NewSexpInt(e.Number())), nil
}
return 0, fmt.Errorf("cannot compare %s to %s", InspectType(sym), InspectType(expr))
}
func comparePair(a *SexpPair, b Sexp) (int, error) {
var bp *SexpPair
switch t := b.(type) {
case *SexpPair:
bp = t
default:
return 0, fmt.Errorf("cannot compare %s to %s", InspectType(a), InspectType(b))
}
res, err := Compare(a.head, bp.head)
if err != nil {
return 0, err
}
if res != 0 {
return res, nil
}
return Compare(a.tail, bp.tail)
}
func compareArray(a SexpArray, b Sexp) (int, error) {
var ba SexpArray
switch t := b.(type) {
case SexpArray:
ba = t
default:
return 0, fmt.Errorf("cannot compare %s to %s", InspectType(a), InspectType(b))
}
var length int
if len(a) < len(ba) {
length = len(a)
} else {
length = len(ba)
}
for i := 0; i < length; i++ {
res, err := Compare(a[i], ba[i])
if err != nil {
return 0, err
}
if res != 0 {
return res, nil
}
}
return compareBetweenInt(NewSexpInt(len(a)), NewSexpInt(len(ba))), nil
}
func compareBool(a SexpBool, b Sexp) (int, error) {
var bb SexpBool
switch bt := b.(type) {
case SexpBool:
bb = bt
default:
return 0, fmt.Errorf("cannot compare %s to %s", InspectType(a), InspectType(b))
}
// true > false
if a && bb {
return 0, nil
}
if a {
return 1, nil
}
if bb {
return -1, nil
}
return 0, nil
}
func existInList(a Sexp, element Sexp) (bool, error) {
for {
if a == SexpNull {
return false, nil
}
expr := a.(*SexpPair)
eq, err := Compare(expr.head, element)
if err != nil {
return false, err
}
if eq == 0 {
return true, nil
}
a = expr.tail
}
}