fspl/entity/toplevel.go

123 lines
3.3 KiB
Go

package entity
import "fmt"
import "github.com/google/uuid"
import "git.tebibyte.media/fspl/fspl/errors"
// TopLevel is any construct that is placed at the root of a file.
type TopLevel interface {
fmt.Stringer
// Position returns the position of the entity within its source file.
Position () errors.Position
// Access returns the access control mode of the entity.
Access () Access
// Unit returns the unit that the entity was defined in.
Unit () uuid.UUID
topLevel ()
}
var _ TopLevel = &Typedef { }
// Typedef binds a type to a global identifier.
type Typedef struct {
// Syntax
Pos errors.Position
Acc Access
Name string
Type Type
// Semantics
Unt uuid.UUID
Methods map[string] *Method
}
func (*Typedef) topLevel(){}
func (this *Typedef) Position () errors.Position { return this.Pos }
func (this *Typedef) Access () Access { return this.Acc }
func (this *Typedef) Unit () uuid.UUID { return this.Unt }
func (this *Typedef) String () string {
output := ""
output += fmt.Sprint(this.Acc, " ")
output += fmt.Sprint(this.Name, ": ", this.Type)
if this.Methods != nil {
for _, method := range this.Methods {
output += fmt.Sprint("\n", method)
}
}
return output
}
var _ TopLevel = &Function { }
// Function binds a global identifier and argument list to an expression which
// is evaluated each time the function is called. If no expression is specified,
// the function is marked as external. Functions have an argument list, where
// each argument is passed as a separate variable. They return one value. All of
// these are typed.
type Function struct {
// Syntax
Pos errors.Position
Acc Access
LinkName string
Signature *Signature
Body Expression
// Semantics
Unt uuid.UUID
Scope
}
func (*Function) topLevel(){}
func (this *Function) Position () errors.Position { return this.Pos }
func (this *Function) Access () Access { return this.Acc }
func (this *Function) Unit () uuid.UUID { return this.Unt }
func (this *Function) String () string {
output := ""
output += fmt.Sprint(this.Acc, " ")
output += this.Signature.String()
if this.LinkName != "" {
output += fmt.Sprint(" '", this.LinkName, "'")
}
if this.Body != nil {
output += fmt.Sprint(" = ", this.Body)
}
return output
}
var _ TopLevel = &Method { }
// Method is like a function, except localized to a defined type. Methods are
// called on an instance of that type, and receive a pointer to that instance
// via the "this" variable when they are run. Method names are not globally
// unique, but are unique within the type they are defined on.
type Method struct {
// Syntax
Pos errors.Position
Acc Access
TypeName string
LinkName string
Signature *Signature
Body Expression
// Semantics
Unt uuid.UUID
Type Type
This *Declaration
Scope
}
func (*Method) topLevel(){}
func (this *Method) Position () errors.Position { return this.Pos }
func (this *Method) Access () Access { return this.Acc }
func (this *Method) Unit () uuid.UUID { return this.Unt }
func (this *Method) String () string {
output := ""
output += fmt.Sprint(this.Acc, " ")
output += fmt.Sprint(this.TypeName, ".", this.Signature)
if this.LinkName != "" {
output += fmt.Sprint(" '", this.LinkName, "'")
}
if this.Body != nil {
output += fmt.Sprint(" = ", this.Body)
}
return output
}