fspl/entity/literal.go

94 lines
3.1 KiB
Go

package entity
import "fmt"
import "github.com/alecthomas/participle/v2/lexer"
// LiteralInt specifies an integer value. It can be assigned to any type that is
// derived from an integer, or a float. It cannot be directly assigned to an
// interface because it contains no inherent type information. A value cast may
// be used for this purpose.
type LiteralInt struct {
Pos lexer.Position
Value int `parser:" @Int "`
}
func (*LiteralInt) expression(){}
func (*LiteralInt) statement(){}
func (this *LiteralInt) String () string {
return fmt.Sprint(this.Value)
}
// LiteralFloat specifies a floating point value. It can be assigned to any type
// that is derived from a float. It cannot be directly assigned to an interface
// because it contains no inherent type information. A value cast may be used
// for this purpose.
type LiteralFloat struct {
Pos lexer.Position
Value float64 `parser:" @Float "`
}
func (*LiteralFloat) expression(){}
func (*LiteralFloat) statement(){}
func (this *LiteralFloat) String () string {
return fmt.Sprint(this.Value)
}
// Array is a composite array literal. It can contain any number of values. It
// can be assigned to any array type that:
// 1. has an identical length, and
// 2. who's element type can be assigned to by all the element values in the
// literal.
// It cannot be directly assigned to an interface because it contains no
// inherent type information. A value cast may be used for this purpose.
type LiteralArray struct {
Pos lexer.Position
Elements []Expression `parser:" '(' '*' @@* ')' "`
}
func (*LiteralArray) expression(){}
func (*LiteralArray) statement(){}
func (this *LiteralArray) String () string {
out := "(*"
for _, element := range this.Elements {
out += fmt.Sprint(" ", element)
}
return out + ")"
}
// Struct is a composite structure literal. It can contain any number of
// name:value pairs. It can be assigned to any struct type that:
// 1. has at least the members specified in the literal
// 2. who's member types can be assigned to by the corresponding member
// values in the literal.
// It cannot be directly assigned to an interface because it contains no
// inherent type information. A value cast may be used for this purpose.
type LiteralStruct struct {
Pos lexer.Position
Members []Member `parser:" '(' @@* ')' "`
}
func (*LiteralStruct) expression(){}
func (*LiteralStruct) statement(){}
func (this *LiteralStruct) String () string {
out := "("
for index, member := range this.Members {
if index > 1 { out += " " }
out += fmt.Sprint(member)
}
return out + ")"
}
// LiteralBoolean is a boolean literal. It may be either true or false. It can
// be assigned to any integer or boolean type. It cannot be directly assigned to
// an interface because it contains no inherent type information. A value cast
// may be used for this purpose.
type LiteralBoolean struct {
Pos lexer.Position
Value bool `parser:" @('true' | 'false') "`
}
func (*LiteralBoolean) expression(){}
func (*LiteralBoolean) statement(){}
func (this *LiteralBoolean) String () string {
if this.Value {
return "true"
} else {
return "false"
}
}