1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-18 23:47:48 +02:00
ferret/pkg/compiler/scope.go

89 lines
1.3 KiB
Go
Raw Normal View History

2018-09-18 22:42:38 +02:00
package compiler
import (
"github.com/MontFerret/ferret/pkg/runtime/core"
)
type (
2019-10-11 23:31:23 +02:00
globalScope struct {
params map[string]struct{}
}
2018-09-18 22:42:38 +02:00
scope struct {
2019-10-11 23:31:23 +02:00
global *globalScope
2018-09-18 22:42:38 +02:00
parent *scope
2019-10-11 23:31:23 +02:00
vars map[string]struct{}
2018-09-18 22:42:38 +02:00
}
)
2019-10-11 23:31:23 +02:00
func newGlobalScope() *globalScope {
return &globalScope{
params: map[string]struct{}{},
}
}
func newRootScope(global *globalScope) *scope {
2018-09-18 22:42:38 +02:00
return &scope{
2019-10-11 23:31:23 +02:00
global: global,
vars: make(map[string]struct{}),
2018-09-18 22:42:38 +02:00
}
}
func newScope(parent *scope) *scope {
2019-10-11 23:31:23 +02:00
s := newRootScope(parent.global)
2018-09-18 22:42:38 +02:00
s.parent = parent
return s
}
2019-10-11 23:31:23 +02:00
func (s *scope) AddParam(name string) {
s.global.params[name] = struct{}{}
}
func (s *scope) HasVariable(name string) bool {
_, exists := s.vars[name]
2018-09-18 22:42:38 +02:00
if exists {
2019-10-11 23:31:23 +02:00
return true
2018-09-18 22:42:38 +02:00
}
if s.parent != nil {
2019-10-11 23:31:23 +02:00
return s.parent.HasVariable(name)
2018-09-18 22:42:38 +02:00
}
2019-10-11 23:31:23 +02:00
return false
2018-09-18 22:42:38 +02:00
}
func (s *scope) SetVariable(name string) error {
_, exists := s.vars[name]
if exists {
2018-10-28 07:45:26 +02:00
return core.Error(ErrVariableNotUnique, name)
2018-09-18 22:42:38 +02:00
}
// TODO: add type detection
2019-10-11 23:31:23 +02:00
s.vars[name] = struct{}{}
2018-09-18 22:42:38 +02:00
return nil
}
func (s *scope) RemoveVariable(name string) error {
_, exists := s.vars[name]
if !exists {
2018-10-28 07:45:26 +02:00
return core.Error(ErrVariableNotFound, name)
}
delete(s.vars, name)
return nil
}
2018-10-28 07:45:26 +02:00
func (s *scope) ClearVariables() {
2019-10-11 23:31:23 +02:00
s.vars = make(map[string]struct{})
2018-10-28 07:45:26 +02:00
}
2018-09-18 22:42:38 +02:00
func (s *scope) Fork() *scope {
return newScope(s)
}