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.
31 lines
582 B
Go
31 lines
582 B
Go
package object
|
|
|
|
func NewEnclosedEnvironment(outer *Environment) *Environment {
|
|
env := NewEnvironment()
|
|
env.outer = outer
|
|
return env
|
|
}
|
|
|
|
func NewEnvironment() *Environment {
|
|
s := make(map[string]Object)
|
|
return &Environment{store: s, outer: nil}
|
|
}
|
|
|
|
type Environment struct {
|
|
store map[string]Object
|
|
outer *Environment
|
|
}
|
|
|
|
func (e *Environment) Get(name string) (Object, bool) {
|
|
obj, ok := e.store[name]
|
|
if !ok && e.outer != nil {
|
|
obj, ok = e.outer.Get(name)
|
|
}
|
|
return obj, ok
|
|
}
|
|
|
|
func (e *Environment) Set(name string, val Object) Object {
|
|
e.store[name] = val
|
|
return val
|
|
}
|