63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
/*
|
|
Copyright (C) 2023 Brian Evans (aka sloum). All rights reserved.
|
|
|
|
This source code is available under the terms of the ffsl, or,
|
|
Floodgap Free Software License. A copy of the license has been
|
|
provided as the file 'LICENSE' in the same folder as this source
|
|
code file. If for some reason it is not present, you can find the
|
|
terms of version 1 of the FFSL at the following URL:
|
|
|
|
https://www.floodgap.com/software/ffsl/license.html
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type env struct {
|
|
vars map[string]token
|
|
imports map[string]bool
|
|
parent *env
|
|
}
|
|
|
|
func (e *env) Find(s string) (*env, error) {
|
|
if _, ok := e.vars[s]; ok {
|
|
return e, nil
|
|
} else if e.parent == nil {
|
|
return e.parent, fmt.Errorf("Symbol %q is not defined", s)
|
|
} else {
|
|
en, err := e.parent.Find(s)
|
|
return en, err
|
|
}
|
|
}
|
|
|
|
func (e *env) Imported(s string) bool {
|
|
if _, ok := e.imports[s]; ok {
|
|
return true
|
|
} else if e.parent == nil {
|
|
return false
|
|
} else {
|
|
return e.parent.Imported(s)
|
|
}
|
|
}
|
|
|
|
func (e *env) UpLevel(count int) *env {
|
|
var en *env = e
|
|
for ; count > 0; count-- {
|
|
if en.parent == nil {
|
|
break
|
|
}
|
|
}
|
|
return en
|
|
}
|
|
|
|
func (e *env) Add(name string, t token) {
|
|
e.vars[name] = t
|
|
}
|
|
|
|
func NewEnv(parent *env) *env {
|
|
return &env{make(map[string]token), make(map[string]bool), parent}
|
|
}
|