2024-10-05 23:27:00 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
type Environment struct {
|
|
|
|
values map[string]any
|
|
|
|
parent *Environment
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEnvironment(parent *Environment) *Environment {
|
|
|
|
return &Environment{values: map[string]any{}, parent: parent}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *Environment) get(key string) any {
|
|
|
|
if found, ok := env.values[key]; ok {
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
if env.parent != nil {
|
|
|
|
return env.parent.get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *Environment) exists(key string) bool {
|
|
|
|
_, ok := env.values[key]
|
|
|
|
|
|
|
|
if !ok && env.parent != nil {
|
|
|
|
return env.parent.exists(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *Environment) set(key string, val any) {
|
2024-10-07 21:06:23 +03:00
|
|
|
|
|
|
|
if env.parent != nil && env.parent.exists(key) {
|
|
|
|
env.parent.set(key, val)
|
|
|
|
}
|
|
|
|
|
2024-10-05 23:27:00 +03:00
|
|
|
env.values[key] = val
|
|
|
|
}
|