2021-07-26 05:31:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type vars map[symbol]expression
|
|
|
|
|
|
|
|
type env struct {
|
|
|
|
vars
|
|
|
|
outer *env
|
|
|
|
}
|
2021-07-31 20:25:20 +00:00
|
|
|
|
2022-05-01 02:48:37 +00:00
|
|
|
func (e *env) Find(s symbol) (*env, error) {
|
2021-07-26 05:31:22 +00:00
|
|
|
if _, ok := e.vars[s]; ok {
|
2022-05-01 02:48:37 +00:00
|
|
|
return e, nil
|
2021-07-26 05:31:22 +00:00
|
|
|
} else if e.outer == nil {
|
2022-05-01 02:48:37 +00:00
|
|
|
if panicOnException {
|
|
|
|
panic(fmt.Sprintf("Symbol %q is not defined", s))
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Symbol %q is not defined", s)
|
|
|
|
}
|
2021-07-26 05:31:22 +00:00
|
|
|
} else {
|
2022-07-03 21:45:02 +00:00
|
|
|
en, err := e.outer.Find(s)
|
|
|
|
return en, err
|
2021-07-26 05:31:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var globalenv env
|
2022-06-16 21:04:13 +00:00
|
|
|
var namespaces = make(map[string]env)
|
2022-06-16 21:48:53 +00:00
|
|
|
var altnamespaces = make(map[string]string)
|