2020-11-14 21:09:54 +00:00
|
|
|
/*
|
|
|
|
Serene --- Yet an other Lisp
|
|
|
|
|
|
|
|
Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-11-15 22:16:48 +00:00
|
|
|
package core
|
2020-11-14 21:09:54 +00:00
|
|
|
|
2020-12-15 19:08:51 +00:00
|
|
|
import "fmt"
|
|
|
|
|
2020-11-14 21:09:54 +00:00
|
|
|
type IScope interface {
|
2020-12-15 19:08:51 +00:00
|
|
|
Lookup(rt *Runtime, k string) *Binding
|
2020-11-15 22:16:48 +00:00
|
|
|
Insert(k string, v IExpr, public bool)
|
2021-01-07 19:45:07 +00:00
|
|
|
GetNS(rt *Runtime) *Namespace
|
|
|
|
SetNS(ns *string)
|
2020-11-14 21:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Binding struct {
|
2020-11-15 22:16:48 +00:00
|
|
|
Value IExpr
|
2020-11-15 19:30:07 +00:00
|
|
|
Public bool
|
2020-11-14 21:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Scope struct {
|
2020-11-15 19:30:07 +00:00
|
|
|
bindings map[string]Binding
|
|
|
|
parent *Scope
|
2021-01-07 19:45:07 +00:00
|
|
|
ns *string
|
2020-11-14 21:09:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 19:08:51 +00:00
|
|
|
func (s *Scope) Lookup(rt *Runtime, k string) *Binding {
|
|
|
|
if rt.IsDebugMode() {
|
|
|
|
fmt.Printf("[DEBUG] Looking up '%s'\n", k)
|
|
|
|
}
|
|
|
|
|
2020-11-14 21:09:54 +00:00
|
|
|
v, ok := s.bindings[k]
|
|
|
|
if ok {
|
2021-01-07 19:45:07 +00:00
|
|
|
if rt.IsDebugMode() {
|
|
|
|
fmt.Printf("[DEBUG] Found '%s': '%s'\n", k, v.Value.String())
|
|
|
|
}
|
2020-11-15 19:30:07 +00:00
|
|
|
return &v
|
2020-11-14 21:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.parent != nil {
|
2020-12-15 19:08:51 +00:00
|
|
|
return s.parent.Lookup(rt, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
builtin := rt.LookupBuiltin(k)
|
|
|
|
|
|
|
|
if builtin != nil {
|
2021-01-07 19:45:07 +00:00
|
|
|
if rt.IsDebugMode() {
|
|
|
|
fmt.Printf("[DEBUG] Found builtin '%s': '%s'\n", k, builtin)
|
|
|
|
}
|
|
|
|
|
2020-12-15 19:08:51 +00:00
|
|
|
return &Binding{builtin, true}
|
2020-11-14 21:09:54 +00:00
|
|
|
}
|
2020-11-15 19:30:07 +00:00
|
|
|
|
|
|
|
return nil
|
2020-11-14 21:09:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 22:16:48 +00:00
|
|
|
func (s *Scope) Insert(k string, v IExpr, public bool) {
|
2020-11-15 19:30:07 +00:00
|
|
|
s.bindings[k] = Binding{Value: v, Public: public}
|
2020-11-14 21:09:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 19:45:07 +00:00
|
|
|
func (s *Scope) GetNS(rt *Runtime) *Namespace {
|
|
|
|
if s.ns == nil {
|
|
|
|
panic("A scope with no namespace !!!!")
|
|
|
|
}
|
|
|
|
ns, ok := rt.GetNS(*s.ns)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("A scope with the wrong namespace! '%s'", s.ns))
|
|
|
|
}
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scope) SetNS(ns *string) {
|
|
|
|
s.ns = ns
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeScope(rt *Runtime, parent *Scope, namespace *string) *Scope {
|
|
|
|
var belongsTo *string
|
|
|
|
|
|
|
|
if parent != nil {
|
|
|
|
nsName := parent.GetNS(rt).GetName()
|
|
|
|
belongsTo = &nsName
|
|
|
|
} else if namespace == nil {
|
|
|
|
panic("When the 'parent' is nil, you have to provide the 'namespace' name.")
|
|
|
|
} else {
|
|
|
|
belongsTo = namespace
|
|
|
|
}
|
|
|
|
|
2020-11-19 19:14:06 +00:00
|
|
|
return &Scope{
|
2020-11-14 21:09:54 +00:00
|
|
|
parent: parent,
|
2020-11-15 19:30:07 +00:00
|
|
|
bindings: map[string]Binding{},
|
2021-01-07 19:45:07 +00:00
|
|
|
ns: belongsTo,
|
2020-11-14 21:09:54 +00:00
|
|
|
}
|
|
|
|
}
|