Simplified type system

This commit is contained in:
2025-02-04 23:58:01 +01:00
parent bde68d4d64
commit 8421a21c9a
16 changed files with 70 additions and 55 deletions

View File

@ -1,16 +0,0 @@
package types
var (
Float64 = &Struct{Name: "Float64", Size: 8}
Float32 = &Struct{Name: "Float32", Size: 4}
Int64 = &Struct{Name: "Int64", Size: 8}
Int32 = &Struct{Name: "Int32", Size: 4}
Int16 = &Struct{Name: "Int16", Size: 2}
Int8 = &Struct{Name: "Int8", Size: 1}
PointerAny = &Pointer{To: nil}
)
var (
Float = Float64
Int = Int64
)

View File

@ -2,10 +2,10 @@ package types
import "git.akyoto.dev/cli/q/src/token"
// Field is a field in a data structure.
// Field is a memory region in a data structure.
type Field struct {
Type Type
Name string
Position token.Position
Offset uint8
Offset int
}

7
src/types/Float.go Normal file
View File

@ -0,0 +1,7 @@
package types
var (
Float64 = &Struct{name: "Float64", size: 8}
Float32 = &Struct{name: "Float32", size: 4}
Float = Float64
)

9
src/types/Int.go Normal file
View File

@ -0,0 +1,9 @@
package types
var (
Int64 = &Struct{name: "Int64", size: 8}
Int32 = &Struct{name: "Int32", size: 4}
Int16 = &Struct{name: "Int16", size: 2}
Int8 = &Struct{name: "Int8", size: 1}
Int = Int64
)

View File

@ -1,7 +1,7 @@
package types
// Check returns true if the encountered type `a` can be converted into the expected type `b`.
func Check(a Type, b Type) bool {
// Is returns true if the encountered type `a` can be converted into the expected type `b`.
func Is(a Type, b Type) bool {
if a == nil {
return true
}

View File

@ -1,17 +1,22 @@
package types
var PointerAny = &Pointer{To: nil}
// Pointer is the address of an object.
type Pointer struct {
To Type
}
func (p *Pointer) UniqueName() string {
// Name returns the type name.
func (p *Pointer) Name() string {
if p.To == nil {
return "Pointer"
}
return "Pointer:" + p.To.UniqueName()
return "Pointer:" + p.To.Name()
}
func (p *Pointer) TotalSize() uint8 {
// Size returns the total size in bytes.
func (p *Pointer) Size() int {
return 8
}

View File

@ -1,22 +1,26 @@
package types
// Struct is a structure in memory whose regions are addressable with fields.
// Struct is a structure in memory whose regions are addressable with named fields.
type Struct struct {
Name string
Fields []*Field
Size uint8
name string
fields []*Field
size int
}
func (s *Struct) UniqueName() string {
return s.Name
// NewStruct creates a new struct.
func NewStruct(name string) *Struct {
return &Struct{name: name}
}
func (s *Struct) TotalSize() uint8 {
return s.Size
// AddField adds a new field to the end of the struct.
func (s *Struct) AddField(field *Field) {
s.fields = append(s.fields, field)
s.size += field.Type.Size()
}
// FieldByName returns the field with the given name if it exists.
func (s *Struct) FieldByName(name string) *Field {
for _, field := range s.Fields {
for _, field := range s.fields {
if field.Name == name {
return field
}
@ -24,3 +28,13 @@ func (s *Struct) FieldByName(name string) *Field {
return nil
}
// Name returns the name of the struct.
func (s *Struct) Name() string {
return s.name
}
// Size returns the total size in bytes.
func (s *Struct) Size() int {
return s.size
}

View File

@ -1,6 +1,7 @@
package types
// Type is the generic interface for different data types.
type Type interface {
UniqueName() string
TotalSize() uint8
Name() string
Size() int
}