Improved type system

This commit is contained in:
2024-08-08 12:55:25 +02:00
parent d624a5f895
commit 5d9be01a85
20 changed files with 111 additions and 67 deletions

16
src/types/Base.go Normal file
View File

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

6
src/types/Check.go Normal file
View File

@ -0,0 +1,6 @@
package types
// Check returns true if the first type can be converted into the second type.
func Check(a *Type, b *Type) bool {
return a == nil || a == b
}

11
src/types/Field.go Normal file
View File

@ -0,0 +1,11 @@
package types
import "git.akyoto.dev/cli/q/src/token"
// Field is a field in a data structure.
type Field struct {
Type *Type
Name string
Position token.Position
Offset uint8
}

View File

@ -1,6 +0,0 @@
package types
// New creates a new type from a list of tokens.
func New(name string) Type {
return Type(name)
}

View File

@ -1,16 +0,0 @@
package types
import "git.akyoto.dev/cli/q/src/token"
// NewList generates a list of types from comma separated tokens.
func NewList(tokens token.List, source []byte) []Type {
var list []Type
tokens.Split(func(parameter token.List) error {
typ := New(parameter.Text(source))
list = append(list, typ)
return nil
})
return list
}

27
src/types/Parse.go Normal file
View File

@ -0,0 +1,27 @@
package types
// Parse creates a new type from a list of tokens.
func Parse(name string) *Type {
switch name {
case "Int":
return Int
case "Int64":
return Int64
case "Int32":
return Int32
case "Int16":
return Int16
case "Int8":
return Int8
case "Float":
return Float
case "Float64":
return Float64
case "Float32":
return Float32
case "Pointer":
return Pointer
default:
panic("Unknown type " + name)
}
}

16
src/types/ParseList.go Normal file
View File

@ -0,0 +1,16 @@
package types
import "git.akyoto.dev/cli/q/src/token"
// ParseList generates a list of types from comma separated tokens.
func ParseList(tokens token.List, source []byte) []*Type {
var list []*Type
tokens.Split(func(parameter token.List) error {
typ := Parse(parameter.Text(source))
list = append(list, typ)
return nil
})
return list
}

View File

@ -1,10 +1,8 @@
package types
type Type string
const (
Invalid = ""
Any = "Any"
Int = "Int"
Pointer = "Pointer"
)
// Type represents a type in the type system.
type Type struct {
Name string
Fields []*Field
Size uint8
}