-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
59 lines (50 loc) · 871 Bytes
/
util.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
package main
import (
"errors"
)
func vec2list(vec []Value) *Pair {
if len(vec) == 0 {
return Empty.(*Pair)
}
res := &Pair{}
cur := res
for i := range vec {
v := vec[i]
cur.Car = &v
var next Value = &Pair{}
if i != len(vec)-1 {
cur.Cdr = &next
cur = (*cur.Cdr).(*Pair)
}
}
cdr := Empty
cur.Cdr = &cdr
return res
}
func list2vec(list *Pair) ([]Value, error) {
res := []Value{}
for list != Empty {
var ok bool
res = append(res, *list.Car)
list, ok = (*list.Cdr).(*Pair)
if !ok {
return nil, errors.New("Dotted list when regular list expected")
}
}
return res, nil
}
func Unscope(v Value) Value {
if v == Empty {
return v
}
switch v.(type) {
case Scoped:
return v.(Scoped).Symbol
case *Pair:
car := Unscope(*v.(*Pair).Car)
cdr := Unscope(*v.(*Pair).Cdr)
return &Pair{&car, &cdr}
default:
return v
}
}