@ de62e8f8076f6010f59527991dbc61d7625a603e | history
package eval
import (
"sort"
"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) (outcome, error) {
if len(operands) == 0 {
return outcome{}, errorf(ErrSyntax, "lambda needs a parameter list and a body")
}
params, err := parseParams(operands[0])
if err != nil {
return outcome{}, err
}
body := operands[1:]
if len(body) == 0 {
return outcome{}, errorf(ErrSyntax, "lambda needs a body")
}
if err := checkBodyShape(body); err != nil {
return outcome{}, err
}
return done(&Closure{params: params, body: body, env: env})
}
// 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
}
// enterClosure binds each parameter to its argument in the closure's own
// environment, runs the body's defines and every expression but the last,
// and returns the last expression with the environment to evaluate it in.
// The caller evaluates that final expression as a tail call.
//
// As in Scheme, the body's defines form their own scope nested inside the
// parameters', so a define may shadow a parameter.
func enterClosure(c *Closure, args []sexpr.Value) (sexpr.Value, *Env, error) {
if len(args) != len(c.params) {
return nil, 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)
last := len(c.body) - 1
for _, form := range c.body[:last] {
var err error
if items, ok := defineForm(form); ok {
_, err = s.define(items)
err = at(err, form)
} else {
_, err = Eval(form, s.env)
}
if err != nil {
return nil, nil, err
}
}
return c.body[last], s.env, 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]
}
// ReservedNames lists the names isReserved accepts. The ast package checks
// its builtin code table against this list, so the evaluator and the OID
// encoding cannot disagree about which names are builtins.
func ReservedNames() []string {
names := make([]string, 0, len(specialForms)+len(builtins))
for name := range specialForms {
names = append(names, string(name))
}
for _, b := range builtins {
names = append(names, b.name)
}
sort.Strings(names)
return names
}