30 lines
573 B
Go
30 lines
573 B
Go
package object
|
|
|
|
func NewEnvironment() *Environment {
|
|
return &Environment{store: map[string]Object{}}
|
|
}
|
|
|
|
func NewEnclosedEnvironment(outer *Environment) *Environment {
|
|
return &Environment{
|
|
store: map[string]Object{},
|
|
outer: outer,
|
|
}
|
|
}
|
|
|
|
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, obj Object) Object {
|
|
e.store[name] = obj
|
|
return obj
|
|
}
|