refactor objects
Some checks failed
Publish Image / publish (push) Waiting to run
Test / build (push) Waiting to run
Build / build (push) Has been cancelled

This commit is contained in:
2024-04-01 17:34:10 -04:00
parent 803f330e82
commit 99f7553d67
15 changed files with 101 additions and 94 deletions

View File

@@ -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}