From c9e7390f06ff3b03b7d2a79acc14204ff13acd1a Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 23 Sep 2023 00:48:50 -0400 Subject: [PATCH] Added scope information to some entities --- entity/expression.go | 4 ++++ entity/scope.go | 31 +++++++++++++++++++++++++++++++ entity/toplevel.go | 6 ++++++ 3 files changed, 41 insertions(+) create mode 100644 entity/scope.go diff --git a/entity/expression.go b/entity/expression.go index c69b81d..7fc3461 100644 --- a/entity/expression.go +++ b/entity/expression.go @@ -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(){} diff --git a/entity/scope.go b/entity/scope.go new file mode 100644 index 0000000..ee13d10 --- /dev/null +++ b/entity/scope.go @@ -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 +} diff --git a/entity/toplevel.go b/entity/toplevel.go index 5e5f41e..e8df198 100644 --- a/entity/toplevel.go +++ b/entity/toplevel.go @@ -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 {