-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair.go
116 lines (93 loc) · 1.83 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
package goscm
import (
"errors"
"reflect"
)
type Pair struct {
Car SCMT
Cdr SCMT
}
var SCM_Nil = &Pair {}
func (pair *Pair) Eval(env *Environ) (SCMT, error) {
if pair == SCM_Nil {
return SCM_Nil, errors.New("Cannot eval empty list")
}
proc, err := pair.Car.Eval(env)
if err != nil { return SCM_Nil, err }
args, ok := pair.Cdr.(*Pair)
if !ok {
return SCM_Nil, errors.New("Cannot eval tupple")
}
f, ok := proc.(Func)
if !ok {
return SCM_Nil, errors.New("Attempting to apply non-function")
}
return f.Apply(args, env)
}
func (pair *Pair) String() string {
ret := "("
for pair != SCM_Nil {
ret += pair.Car.String()
if pair.Cdr == SCM_Nil {
break
}
if reflect.TypeOf(pair.Cdr) != reflect.TypeOf(&Pair{}) {
ret += " . "
ret += pair.Cdr.String()
break
}
pair = pair.Cdr.(*Pair)
ret += " "
}
return ret + ")"
}
func NewList(args ...SCMT) *Pair {
list := SCM_Nil
for i := len(args) - 1; i >= 0; i-- {
list = Cons(args[i], list)
}
return list
}
// Helpers
func (p *Pair) ToSlice() ([]SCMT, error) {
var s []SCMT
var ok bool
for p != SCM_Nil {
s = append(s, p.Car)
p, ok = p.Cdr.(*Pair)
if !ok {
s = append(s, p.Cdr)
break
}
}
return s, nil
}
func (list *Pair) MapEval(env *Environ) (*Pair, error) {
ret := SCM_Nil
listt, err := list.ToSlice()
if err != nil { return SCM_Nil, err }
for i := len(listt) - 1; i >= 0; i-- {
newval, err := listt[i].Eval(env)
if err != nil { return SCM_Nil, err }
ret = Cons(newval, ret)
}
return ret, nil
}
func Cons(car SCMT, cdr SCMT) *Pair {
return &Pair {
Car: car,
Cdr: cdr,
}
}
func Reverse(pair *Pair) (*Pair, error) {
var ok bool
ret := SCM_Nil
for pair != SCM_Nil {
ret = Cons(pair.Car, ret)
pair, ok = pair.Cdr.(*Pair)
if !ok {
return SCM_Nil, errors.New("Attempt to reverse dotted list")
}
}
return ret, nil
}