Added scope information to some entities

This commit is contained in:
Sasha Koshka 2023-09-23 00:48:50 -04:00
parent 57e41e48ff
commit c9e7390f06
3 changed files with 41 additions and 0 deletions

View File

@ -192,8 +192,12 @@ func (this *Operation) String () string {
// equivalent to those of its last expression. A block is never a valid location
// expression.
type Block struct {
// Syntax
Pos lexer.Position
Steps []Statement `parser:" '{' @@* '}' "`
// Semantics
Scope
}
func (*Block) expression(){}
func (*Block) statement(){}

31
entity/scope.go Normal file
View File

@ -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
}

View File

@ -41,6 +41,9 @@ type Function struct {
Public bool `parser:" @'+'? "`
Signature *Signature `parser:" @@ "`
Body Expression `parser:" ( '=' @@ )? "`
// Semantics
Scope
}
func (*Function) topLevel(){}
func (this *Function) String () string {
@ -62,6 +65,9 @@ type Method struct {
TypeName string `parser:" @Ident "`
Signature *Signature `parser:" '.' @@ "`
Body Expression `parser:" ( '=' @@ )? "`
// Semantics
Scope
}
func (*Method) topLevel(){}
func (this *Method) String () string {