2023-09-23 04:48:50 +00:00
|
|
|
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.
|
2023-10-17 07:11:11 +00:00
|
|
|
AddVariable (declaration *Declaration)
|
2023-09-23 04:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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]
|
|
|
|
}
|
|
|
|
|
2023-10-17 07:11:11 +00:00
|
|
|
func (this *Scope) AddVariable (declaration *Declaration) {
|
2023-09-23 04:48:50 +00:00
|
|
|
if this.variables == nil {
|
|
|
|
this.variables = make(map[string] *Declaration)
|
|
|
|
}
|
2023-10-17 07:11:11 +00:00
|
|
|
this.variables[declaration.Name] = declaration
|
2023-09-23 04:48:50 +00:00
|
|
|
}
|