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
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
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)
|
2018-10-25 03:30:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|