83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package entity
|
|
|
|
import "fmt"
|
|
import "github.com/alecthomas/participle/v2/lexer"
|
|
|
|
// TopLevel is any construct that is placed at the root of a file.
|
|
type TopLevel interface {
|
|
topLevel ()
|
|
}
|
|
|
|
// Typedef binds a type to a global identifier.
|
|
type Typedef struct {
|
|
// Syntax
|
|
Pos lexer.Position
|
|
Public bool `parser:" @'+'? "`
|
|
Name string `parser:" @Ident "`
|
|
Type Type `parser:" ':' @@ "`
|
|
|
|
// Semantics
|
|
Methods map[string] Method
|
|
}
|
|
func (*Typedef) topLevel(){}
|
|
func (this *Typedef) String () string {
|
|
out := fmt.Sprint(this.Name, ": ", this.Type)
|
|
if this.Methods != nil {
|
|
for _, method := range this.Methods {
|
|
out += fmt.Sprint("\n", method)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// 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 lexer.Position
|
|
Public bool `parser:" @'+'? "`
|
|
Signature *Signature `parser:" @@ "`
|
|
Body Expression `parser:" ( '=' @@ )? "`
|
|
|
|
// Semantics
|
|
Scope
|
|
}
|
|
func (*Function) topLevel(){}
|
|
func (this *Function) String () string {
|
|
if this.Body == nil {
|
|
return fmt.Sprint(this.Signature)
|
|
} else {
|
|
return fmt.Sprint(this.Signature, " = ", this.Body)
|
|
}
|
|
}
|
|
|
|
// 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. Method names are not globally unique, bur are unique
|
|
// within the type they are defined on.
|
|
type Method struct {
|
|
// Syntax
|
|
Pos lexer.Position
|
|
Public bool `parser:" @'+'? "`
|
|
TypeName string `parser:" @Ident "`
|
|
Signature *Signature `parser:" '.' @@ "`
|
|
Body Expression `parser:" ( '=' @@ )? "`
|
|
|
|
// Semantics
|
|
Scope
|
|
}
|
|
func (*Method) topLevel(){}
|
|
func (this *Method) String () string {
|
|
if this.Body == nil {
|
|
return fmt.Sprint(this.TypeName, ".", this.Signature)
|
|
} else {
|
|
return fmt.Sprint (
|
|
this.TypeName, ".",
|
|
this.Signature, " = ",
|
|
this.Body)
|
|
}
|
|
}
|