@ 0d159351c7131f98bbe83fa0f45b743e0d28b3f9 | history
package eval
import "government/sexpr"
// Closure is a lambda's code paired with the environment it was created
// in. Calling it extends that environment, never the caller's, which is
// what makes scope lexical.
type Closure struct {
sexpr.FunctionBase
params []sexpr.Symbol
body []sexpr.Value // defines first, then at least one expression
env *Env
name string // set by the first define that binds it; "" if anonymous
}
func (*Closure) Builtin() bool { return false }
func (c *Closure) Name() string { return c.name }
func (c *Closure) displayName() string {
if c.name == "" {
return "lambda"
}
return c.name
}
// (lambda (p1 ... pk) body...)
//
// The parameter list and the shape of the body are checked once, here,
// rather than on every call.
func evalLambda(operands []sexpr.Value, env *Env) (sexpr.Value, error) {
if len(operands) == 0 {
return nil, errorf(ErrSyntax, "lambda needs a parameter list and a body")
}
params, err := parseParams(operands[0])
if err != nil {
return nil, err
}
body := operands[1:]
if len(body) == 0 {
return nil, errorf(ErrSyntax, "lambda needs a body")
}
if err := checkBodyShape(body); err != nil {
return nil, err
}
return &Closure{params: params, body: body, env: env}, nil
}
// checkBodyShape enforces: zero or more defines, then one or more
// expressions.
func checkBodyShape(body []sexpr.Value) error {
defines := 0
for i, form := range body {
if !isFormNamed(form, "define") {
continue
}
if i != defines {
return errorf(ErrSyntax, "define must come before any expression in a lambda body")
}
defines++
}
if defines == len(body) {
return errorf(ErrSyntax, "lambda body needs an expression after its definitions")
}
return nil
}
func parseParams(v sexpr.Value) ([]sexpr.Symbol, error) {
items, ok := properList(v)
if !ok {
return nil, errorf(ErrSyntax, "lambda parameters must be a list of symbols, got %s", sexpr.Print(v))
}
params := make([]sexpr.Symbol, len(items))
seen := make(map[sexpr.Symbol]bool, len(items))
for i, item := range items {
name, ok := item.(sexpr.Symbol)
if !ok {
return nil, errorf(ErrSyntax, "lambda parameter %s is not a symbol", sexpr.Print(item))
}
if isReserved(name) {
return nil, errorf(ErrSyntax, "lambda parameter %s is a reserved name", name)
}
if seen[name] {
return nil, errorf(ErrSyntax, "duplicate lambda parameter %s", name)
}
seen[name] = true
params[i] = name
}
return params, nil
}
// applyClosure binds each parameter to its argument in the closure's own
// environment, runs the body's defines, then evaluates its expressions in
// order. The value of the last one is the result.
//
// As in Scheme, the body's defines form their own scope nested inside the
// parameters', so a define may shadow a parameter.
func applyClosure(c *Closure, args []sexpr.Value) (sexpr.Value, error) {
if len(args) != len(c.params) {
return nil, errorf(ErrArity, "%s takes %s, got %d", c.displayName(), plural(len(c.params), "argument"), len(args))
}
env := c.env
for i, p := range c.params {
env = env.Extend(p, args[i])
}
s := newScope(env)
var result sexpr.Value
for _, form := range c.body {
var err error
if items, ok := defineForm(form); ok {
_, err = s.define(items)
} else {
result, err = Eval(form, s.env)
}
if err != nil {
return nil, err
}
}
return result, nil
}
// Reserved names are the special forms and builtins. In a .gov program
// they are a separate namespace from user symbols, so they can never be
// rebound; rejecting them here keeps source-mode programs consistent with
// that.
func isReserved(name sexpr.Symbol) bool {
if _, ok := specialForms[name]; ok {
return true
}
return builtinNames[name]
}