-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreign.go
43 lines (32 loc) · 830 Bytes
/
foreign.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
package goscm
import "errors"
type Foreign struct {
function func (*Pair, *Environ) (SCMT, error)
}
func (fo *Foreign) Eval(*Environ) (SCMT, error) {
return fo, nil
}
func (*Foreign) String() string {
return "#<foreign function>"
}
func (fo *Foreign) Apply(args *Pair, env *Environ) (SCMT, error) {
var err error
var ret *Pair
ok := true
for ret = SCM_Nil; args != SCM_Nil; args, ok = args.Cdr.(*Pair) {
if !ok { // This is a dotted list
return SCM_Nil, errors.New("Got a dotted list. How to handle?")
}
val, err := args.Car.Eval(env)
if err != nil { return SCM_Nil, err }
ret = Cons(val, ret)
}
ret, err = Reverse(ret)
if err != nil { return SCM_Nil, err }
return fo.function(ret, env)
}
func NewForeign(f func (*Pair, *Environ) (SCMT, error)) *Foreign {
return &Foreign {
function: f,
}
}