Implemented structs
This commit is contained in:
@ -1,13 +1,13 @@
|
||||
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}
|
||||
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 (
|
||||
|
@ -1,6 +1,21 @@
|
||||
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
|
||||
// Check returns true if the encountered type `a` can be converted into the expected type `b`.
|
||||
func Check(a Type, b Type) bool {
|
||||
if a == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if a == b {
|
||||
return true
|
||||
}
|
||||
|
||||
aPointer, aIsPointer := a.(*Pointer)
|
||||
bPointer, bIsPointer := b.(*Pointer)
|
||||
|
||||
if aIsPointer && bIsPointer && (bPointer.To == nil || aPointer.To == bPointer.To) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import "git.akyoto.dev/cli/q/src/token"
|
||||
|
||||
// Field is a field in a data structure.
|
||||
type Field struct {
|
||||
Type *Type
|
||||
Type Type
|
||||
Name string
|
||||
Position token.Position
|
||||
Offset uint8
|
||||
|
@ -1,7 +1,7 @@
|
||||
package types
|
||||
|
||||
// Parse creates a new type from a list of tokens.
|
||||
func Parse(name string) *Type {
|
||||
func Parse(name string) Type {
|
||||
switch name {
|
||||
case "Int":
|
||||
return Int
|
||||
@ -20,7 +20,7 @@ func Parse(name string) *Type {
|
||||
case "Float32":
|
||||
return Float32
|
||||
case "Pointer":
|
||||
return Pointer
|
||||
return PointerAny
|
||||
default:
|
||||
panic("Unknown type " + name)
|
||||
}
|
||||
|
@ -3,8 +3,8 @@ 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
|
||||
func ParseList(tokens token.List, source []byte) []Type {
|
||||
var list []Type
|
||||
|
||||
tokens.Split(func(parameter token.List) error {
|
||||
typ := Parse(parameter.Text(source))
|
||||
|
17
src/types/Pointer.go
Normal file
17
src/types/Pointer.go
Normal file
@ -0,0 +1,17 @@
|
||||
package types
|
||||
|
||||
type Pointer struct {
|
||||
To Type
|
||||
}
|
||||
|
||||
func (p *Pointer) UniqueName() string {
|
||||
if p.To == nil {
|
||||
return "Pointer"
|
||||
}
|
||||
|
||||
return "Pointer:" + p.To.UniqueName()
|
||||
}
|
||||
|
||||
func (p *Pointer) TotalSize() uint8 {
|
||||
return 8
|
||||
}
|
16
src/types/Struct.go
Normal file
16
src/types/Struct.go
Normal file
@ -0,0 +1,16 @@
|
||||
package types
|
||||
|
||||
// Struct is a structure in memory whose regions are addressable with fields.
|
||||
type Struct struct {
|
||||
Name string
|
||||
Fields []*Field
|
||||
Size uint8
|
||||
}
|
||||
|
||||
func (s *Struct) UniqueName() string {
|
||||
return s.Name
|
||||
}
|
||||
|
||||
func (s *Struct) TotalSize() uint8 {
|
||||
return s.Size
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
package types
|
||||
|
||||
// Type represents a type in the type system.
|
||||
type Type struct {
|
||||
Name string
|
||||
Fields []*Field
|
||||
Size uint8
|
||||
type Type interface {
|
||||
UniqueName() string
|
||||
TotalSize() uint8
|
||||
}
|
||||
|
Reference in New Issue
Block a user