refactor objects
This commit is contained in:
@@ -4,24 +4,31 @@ import "unicode"
|
||||
|
||||
func NewEnclosedEnvironment(outer *Environment) *Environment {
|
||||
env := NewEnvironment()
|
||||
env.outer = outer
|
||||
env.parent = outer
|
||||
return env
|
||||
}
|
||||
|
||||
func NewEnvironment() *Environment {
|
||||
s := make(map[string]Object)
|
||||
return &Environment{store: s, outer: nil}
|
||||
return &Environment{store: s, parent: nil}
|
||||
}
|
||||
|
||||
type Environment struct {
|
||||
store map[string]Object
|
||||
outer *Environment
|
||||
store map[string]Object
|
||||
parent *Environment
|
||||
}
|
||||
|
||||
// New creates a new copy of the environment with the current environment as parent
|
||||
func (e *Environment) New() *Environment {
|
||||
env := NewEnvironment()
|
||||
env.parent = e
|
||||
return env
|
||||
}
|
||||
|
||||
func (e *Environment) Get(name string) (Object, bool) {
|
||||
obj, ok := e.store[name]
|
||||
if !ok && e.outer != nil {
|
||||
obj, ok = e.outer.Get(name)
|
||||
if !ok && e.parent != nil {
|
||||
obj, ok = e.parent.Get(name)
|
||||
}
|
||||
return obj, ok
|
||||
}
|
||||
@@ -40,7 +47,7 @@ func (e *Environment) ExportedHash() *Hash {
|
||||
for k, v := range e.store {
|
||||
if unicode.IsUpper(rune(k[0])) {
|
||||
s := String{Value: k}
|
||||
pairs[s.HashKey()] = HashPair{Key: s, Value: v}
|
||||
pairs[s.Hash()] = HashPair{Key: s, Value: v}
|
||||
}
|
||||
}
|
||||
return &Hash{Pairs: pairs}
|
||||
|
||||
Reference in New Issue
Block a user