generate: Add String functions, TypeHash function for types
This commit is contained in:
parent
c86f9b03f2
commit
fae702edfd
@ -1,5 +1,10 @@
|
|||||||
package generate
|
package generate
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
import "maps"
|
||||||
|
import "slices"
|
||||||
|
import "crypto/md5"
|
||||||
|
|
||||||
type Protocol struct {
|
type Protocol struct {
|
||||||
Messages map[uint16] Message
|
Messages map[uint16] Message
|
||||||
Types map[string] Type
|
Types map[string] Type
|
||||||
@ -11,7 +16,7 @@ type Message struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Type interface {
|
type Type interface {
|
||||||
|
fmt.Stringer
|
||||||
}
|
}
|
||||||
|
|
||||||
type TypeInt struct {
|
type TypeInt struct {
|
||||||
@ -19,29 +24,84 @@ type TypeInt struct {
|
|||||||
Signed bool
|
Signed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (typ TypeInt) String() string {
|
||||||
|
output := ""
|
||||||
|
if typ.Signed {
|
||||||
|
output += "I"
|
||||||
|
} else {
|
||||||
|
output += "U"
|
||||||
|
}
|
||||||
|
output += fmt.Sprint(typ.Bits)
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
type TypeFloat struct {
|
type TypeFloat struct {
|
||||||
Bits int
|
Bits int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (typ TypeFloat) String() string {
|
||||||
|
return fmt.Sprintf("F%d", typ.Bits)
|
||||||
|
}
|
||||||
|
|
||||||
type TypeString struct { }
|
type TypeString struct { }
|
||||||
|
|
||||||
|
func (TypeString) String() string {
|
||||||
|
return "String"
|
||||||
|
}
|
||||||
|
|
||||||
type TypeBuffer struct { }
|
type TypeBuffer struct { }
|
||||||
|
|
||||||
|
func (TypeBuffer) String() string {
|
||||||
|
return "Buffer"
|
||||||
|
}
|
||||||
|
|
||||||
type TypeArray struct {
|
type TypeArray struct {
|
||||||
Element Type
|
Element Type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (typ TypeArray) String() string {
|
||||||
|
return fmt.Sprintf("[]%v", typ.Element)
|
||||||
|
}
|
||||||
|
|
||||||
type TypeTable struct { }
|
type TypeTable struct { }
|
||||||
|
|
||||||
|
func (TypeTable) String() string {
|
||||||
|
return "Table"
|
||||||
|
}
|
||||||
|
|
||||||
type TypeTableDefined struct {
|
type TypeTableDefined struct {
|
||||||
Fields map[uint16] Field
|
Fields map[uint16] Field
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (typ TypeTableDefined) String() string {
|
||||||
|
output := "{"
|
||||||
|
for _, key := range slices.Sorted(maps.Keys(typ.Fields)) {
|
||||||
|
output += fmt.Sprintf("%04X %v", key, typ.Fields[key])
|
||||||
|
}
|
||||||
|
output += "}"
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
type Field struct {
|
type Field struct {
|
||||||
Name string
|
Name string
|
||||||
Type Type
|
Type Type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (field Field) String() string {
|
||||||
|
return fmt.Sprintf("%s %v", field.Name, field.Type)
|
||||||
|
}
|
||||||
|
|
||||||
type TypeNamed struct {
|
type TypeNamed struct {
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (typ TypeNamed) String() string {
|
||||||
|
return typ.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func HashType(typ Type) [16]byte {
|
||||||
|
// TODO: if we ever want to make the compiler more efficient, this would
|
||||||
|
// be a good place to start, complex string concatenation in a hot path
|
||||||
|
// (sorta)
|
||||||
|
return md5.Sum([]byte(typ.String()))
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user