You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
822 B
Go

package main
import (
"fmt"
)
type UnknownSymbolError struct{ symbol }
func (u UnknownSymbolError) Error() string {
return fmt.Sprintf(`unknown symbol "%v"`, u.symbol)
}
type environment struct {
items map[symbol]interface{}
outer *environment
}
func newEnvironment(outer *environment) *environment {
return &environment{
items: make(map[symbol]interface{}),
outer: outer,
}
}
func (e environment) get(key symbol) (interface{}, error) {
v, ok := e.items[key]
if ok {
debugPrint(fmt.Sprintf(`found key "%v": %v`, key, v))
return v, nil
}
if e.outer != nil {
return e.outer.get(key)
}
return nil, UnknownSymbolError{key}
}
func (e environment) set(key symbol, val interface{}) {
e.items[key] = val
}
12 years ago
func (e environment) defined(key symbol) bool {
_, err := e.get(key)
return err == nil
12 years ago
}