fspl/entity/toplevel.go

112 lines
2.7 KiB
Go

package entity
import "fmt"
import "git.tebibyte.media/sashakoshka/fspl/errors"
// TopLevel is any construct that is placed at the root of a file.
type TopLevel interface {
topLevel ()
}
// Access determines the external access rule for a top-level entity.
type Access int; const (
AccessPrivate Access = iota
AccessRestricted
AccessPublic
)
func (this Access) String () string {
switch this {
case AccessPrivate: return "-"
case AccessRestricted: return "~"
case AccessPublic: return "+"
default: return fmt.Sprintf("entity.Access(%d)", this)
}
}
// Typedef binds a type to a global identifier.
type Typedef struct {
// Syntax
Position errors.Position
Acc Access
Name string
Type Type
// Semantics
Methods map[string] *Method
}
func (*Typedef) topLevel(){}
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
}
// 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
Position errors.Position
Acc Access
Signature *Signature
LinkName string
Body Expression
// Semantics
Scope
}
func (*Function) topLevel(){}
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
}
// 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
Position errors.Position
Acc Access
TypeName string
Signature *Signature
LinkName string
Body Expression
// Semantics
Scope
Type Type
This *Declaration
}
func (*Method) topLevel(){}
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
}