161 lines
3.0 KiB
Go
161 lines
3.0 KiB
Go
package entity
|
|
|
|
import "fmt"
|
|
import "unicode"
|
|
import "github.com/google/uuid"
|
|
import "git.tebibyte.media/fspl/fspl/errors"
|
|
|
|
// Signature is a function or method signature that is used in functions,
|
|
// methods, and specifying interface behaviors. It defines the type of a
|
|
// function.
|
|
type Signature struct {
|
|
// Syntax
|
|
Position errors.Position
|
|
Name string
|
|
Arguments []*Declaration
|
|
Return Type
|
|
|
|
// Semantics
|
|
Acc Access
|
|
Unt uuid.UUID
|
|
ArgumentOrder []string
|
|
ArgumentMap map[string] *Declaration
|
|
}
|
|
func (*Signature) ty(){}
|
|
func (this *Signature) Access () Access { return this.Acc }
|
|
func (this *Signature) Unit () uuid.UUID { return this.Unt }
|
|
func (this *Signature) String () string {
|
|
out := "[" + this.Name
|
|
for _, argument := range this.Arguments {
|
|
out += fmt.Sprint(" ", argument)
|
|
}
|
|
out += "]"
|
|
if this.Return != nil {
|
|
out += fmt.Sprint(":", this.Return)
|
|
}
|
|
return out
|
|
}
|
|
func (this *Signature) Equals (ty Type) bool {
|
|
real, ok := ty.(*Signature)
|
|
if !ok ||
|
|
len(real.Arguments) != len(this.Arguments) ||
|
|
!TypesEqual(real.Return, this.Return) ||
|
|
real.Name != this.Name { return false }
|
|
|
|
for index, argument := range this.Arguments {
|
|
if !argument.Type().Equals(real.Arguments[index].Type()) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Member is a syntactical construct that is used to list members in struct
|
|
// literals.
|
|
type Member struct {
|
|
Position errors.Position
|
|
Name string
|
|
Value Expression
|
|
}
|
|
func (this *Member) String () string {
|
|
return fmt.Sprint(this.Name, ":", this.Value)
|
|
}
|
|
|
|
// Operator determines which operation to apply to a value in an operation
|
|
// expression.
|
|
type Operator int; const (
|
|
// Math
|
|
OperatorAdd Operator = iota
|
|
OperatorIncrement
|
|
OperatorSubtract
|
|
OperatorDecrement
|
|
OperatorMultiply
|
|
OperatorDivide
|
|
OperatorModulo
|
|
|
|
// Logic
|
|
OperatorLogicalNot
|
|
OperatorLogicalOr
|
|
OperatorLogicalAnd
|
|
OperatorLogicalXor
|
|
|
|
// Bit manipulation
|
|
OperatorNot
|
|
OperatorOr
|
|
OperatorAnd
|
|
OperatorXor
|
|
OperatorLeftShift
|
|
OperatorRightShift
|
|
|
|
// Comparison
|
|
OperatorLess
|
|
OperatorGreater
|
|
OperatorLessEqual
|
|
OperatorGreaterEqual
|
|
OperatorEqual
|
|
)
|
|
|
|
var operatorToString = []string {
|
|
// Math
|
|
"+",
|
|
"++",
|
|
"-",
|
|
"--",
|
|
"*",
|
|
"/",
|
|
"%",
|
|
|
|
// Logic
|
|
"!",
|
|
"|",
|
|
"&",
|
|
"^",
|
|
|
|
// Bit manipulation
|
|
"!!",
|
|
"||",
|
|
"&&",
|
|
"^^",
|
|
"<<",
|
|
">>",
|
|
|
|
// Comparison
|
|
"<",
|
|
">",
|
|
"<=",
|
|
">=",
|
|
"=",
|
|
}
|
|
|
|
var stringToOperator map[string] Operator
|
|
|
|
func init () {
|
|
stringToOperator = make(map[string] Operator)
|
|
for index, str := range operatorToString {
|
|
stringToOperator[str] = Operator(index)
|
|
}
|
|
}
|
|
|
|
func OperatorFromString (str string) Operator {
|
|
return stringToOperator[str]
|
|
}
|
|
|
|
func (this Operator) String () string {
|
|
return operatorToString[this]
|
|
}
|
|
|
|
// Quote puts quotes around a string to turn it into a string literal.
|
|
func Quote (in string) string {
|
|
out := "'"
|
|
for _, char := range in {
|
|
if char == '\'' {
|
|
out += "\\'"
|
|
} else if unicode.IsPrint(char) {
|
|
out += string(char)
|
|
} else {
|
|
out += fmt.Sprintf("\\%03o", char)
|
|
}
|
|
}
|
|
return out + "'"
|
|
}
|