Added scope information to some entities
This commit is contained in:
parent
57e41e48ff
commit
c9e7390f06
|
@ -192,8 +192,12 @@ func (this *Operation) String () string {
|
||||||
// equivalent to those of its last expression. A block is never a valid location
|
// equivalent to those of its last expression. A block is never a valid location
|
||||||
// expression.
|
// expression.
|
||||||
type Block struct {
|
type Block struct {
|
||||||
|
// Syntax
|
||||||
Pos lexer.Position
|
Pos lexer.Position
|
||||||
Steps []Statement `parser:" '{' @@* '}' "`
|
Steps []Statement `parser:" '{' @@* '}' "`
|
||||||
|
|
||||||
|
// Semantics
|
||||||
|
Scope
|
||||||
}
|
}
|
||||||
func (*Block) expression(){}
|
func (*Block) expression(){}
|
||||||
func (*Block) statement(){}
|
func (*Block) statement(){}
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
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
|
||||||
|
}
|
|
@ -41,6 +41,9 @@ type Function struct {
|
||||||
Public bool `parser:" @'+'? "`
|
Public bool `parser:" @'+'? "`
|
||||||
Signature *Signature `parser:" @@ "`
|
Signature *Signature `parser:" @@ "`
|
||||||
Body Expression `parser:" ( '=' @@ )? "`
|
Body Expression `parser:" ( '=' @@ )? "`
|
||||||
|
|
||||||
|
// Semantics
|
||||||
|
Scope
|
||||||
}
|
}
|
||||||
func (*Function) topLevel(){}
|
func (*Function) topLevel(){}
|
||||||
func (this *Function) String () string {
|
func (this *Function) String () string {
|
||||||
|
@ -62,6 +65,9 @@ type Method struct {
|
||||||
TypeName string `parser:" @Ident "`
|
TypeName string `parser:" @Ident "`
|
||||||
Signature *Signature `parser:" '.' @@ "`
|
Signature *Signature `parser:" '.' @@ "`
|
||||||
Body Expression `parser:" ( '=' @@ )? "`
|
Body Expression `parser:" ( '=' @@ )? "`
|
||||||
|
|
||||||
|
// Semantics
|
||||||
|
Scope
|
||||||
}
|
}
|
||||||
func (*Method) topLevel(){}
|
func (*Method) topLevel(){}
|
||||||
func (this *Method) String () string {
|
func (this *Method) String () string {
|
||||||
|
|
Loading…
Reference in New Issue