fspl/entity/scope.go

42 lines
1.1 KiB
Go

package entity
// Scoped represents any entity that has its own scope.
type Scoped interface {
// Variable returns the declaration of the variable with the given name.
// If no variable is found in this scope, it returns nil. Only the scope
// in this entity is searched.
Variable (name string) *Declaration
// AddVariable adds a variable to this entity's scope.
AddVariable (declaration *Declaration)
// OverVariables calls callback for each defined variable, stopping if
// returns false.
OverVariables (callback func (*Declaration) bool)
}
// Scope implements a scope.
type Scope struct {
variables map[string] *Declaration
}
func (this *Scope) Variable (name string) *Declaration {
if this.variables == nil {
return nil
}
return this.variables[name]
}
func (this *Scope) AddVariable (declaration *Declaration) {
if this.variables == nil {
this.variables = make(map[string] *Declaration)
}
this.variables[declaration.Name] = declaration
}
func (this *Scope) OverVariables (callback func (*Declaration) bool) {
for _, declaration := range this.variables {
if !callback(declaration) { break }
}
}