-
Notifications
You must be signed in to change notification settings - Fork 2
/
lambda.go
106 lines (100 loc) · 2.24 KB
/
lambda.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
package main
import (
"fmt"
"strings"
)
type lambdaFn struct {
args []string
restArg string
body *ConsCell
doc *ConsCell
isMacro bool
env *env
}
var noRestArg string = ""
func mkLambda(cdr *ConsCell, isMacro bool, e *env) (*lambdaFn, error) {
args := []string{}
restArg := noRestArg
// look for fn name
if cdr == Nil {
return nil, baseError("missing arguments")
}
fnNameAtom, ok := cdr.car.(Atom)
var fnName string
if ok {
fnName = fnNameAtom.s
cdr = cdr.cdr.(*ConsCell)
}
if cdr == Nil {
return nil, baseError("missing arguments")
}
argList, ok := cdr.car.(*ConsCell)
if !ok {
return nil, baseError("lambda requires an argument list")
}
emptyArgList := false
top:
for argList != Nil && !emptyArgList {
if argList.car == Nil {
emptyArgList = true
} else {
arg, ok := argList.car.(Atom)
if !ok {
return nil, baseError("argument list item is not an atom")
}
args = append(args, arg.s)
}
switch t := argList.cdr.(type) {
case Atom:
restArg = t.s
break top
case *ConsCell:
argList = t
default:
return nil, baseError("unknown type in lambda arg list")
}
}
if emptyArgList && restArg == noRestArg {
return nil, baseError("lambda with () argument requires a rest argument")
}
body := cdr.cdr.(*ConsCell)
// Find `doc` form and save it if found:
doc := Nil
if body != Nil && body.car != Nil {
doc2, ok := body.car.(*ConsCell)
if ok && doc2 != Nil && doc2.car.Equal(Atom{"doc"}) {
cdrCons, ok := doc2.cdr.(*ConsCell)
if !ok {
return nil, baseError("doc form is not a list")
}
// Omit explicitly undocumented functions:
if cdrCons != Nil && cdrCons.car != Nil {
doc = cdrCons
}
body = body.cdr.(*ConsCell) // Skip `doc` part.
}
}
f := lambdaFn{args,
restArg,
body,
doc,
isMacro,
e}
if fnName != "" {
// Monkey-patch the environment the lambda is created in, so the
// lambda can invoke itself if the name is available:
e.Set(fnName, &f)
}
return &f, nil
}
func (f *lambdaFn) String() string {
restArgsRepr := ""
if f.restArg != noRestArg {
restArgsRepr = fmt.Sprintf(" . %s", f.restArg)
}
return fmt.Sprintf("<lambda(%s%s)>",
strings.Join(f.args, " "), restArgsRepr)
}
func (f *lambdaFn) Equal(o Sexpr) bool {
return false
}