32 lines
839 B
Go
32 lines
839 B
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 (name string, declaration *Declaration)
|
||
|
}
|
||
|
|
||
|
// 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 (name string, declaration *Declaration) {
|
||
|
if this.variables == nil {
|
||
|
this.variables = make(map[string] *Declaration)
|
||
|
}
|
||
|
this.variables[name] = declaration
|
||
|
}
|