@ 68160212362299a3894c7ae88d7b2c018f5cc3aa | history
package eval
import "government/sexpr"
// Env is a lexical environment: an immutable linked list of bindings,
// innermost first. The nil *Env is the empty environment.
//
// Extending returns a new head that points at the old list, so a closure
// can capture an environment just by keeping the pointer: later extensions
// by anyone else are invisible to it. Lookup walks the chain, so the first
// match is the innermost binding, which is what makes shadowing work.
type Env struct {
name sexpr.Symbol
value sexpr.Value
parent *Env
}
func (e *Env) Extend(name sexpr.Symbol, value sexpr.Value) *Env {
return &Env{name: name, value: value, parent: e}
}
func (e *Env) Lookup(name sexpr.Symbol) (sexpr.Value, bool) {
for b := e; b != nil; b = b.parent {
if b.name == name {
return b.value, true
}
}
return nil, false
}