Implemented struct size calculation after a scan
This commit is contained in:
parent
c28eace966
commit
63a2752c56
@ -65,6 +65,17 @@ func Compile(files <-chan *fs.File, functions <-chan *core.Function, structs <-c
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate size of structs
|
||||||
|
for _, typ := range allTypes {
|
||||||
|
structure, isStruct := typ.(*types.Struct)
|
||||||
|
|
||||||
|
if !isStruct {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
structure.Update(allTypes)
|
||||||
|
}
|
||||||
|
|
||||||
// Resolve the types
|
// Resolve the types
|
||||||
for _, function := range allFunctions {
|
for _, function := range allFunctions {
|
||||||
err := function.ResolveTypes()
|
err := function.ResolveTypes()
|
||||||
|
@ -33,27 +33,12 @@ func (s *Scanner) scanStruct(file *fs.File, tokens token.List, i int) (int, erro
|
|||||||
fieldName := tokens[i].Text(file.Bytes)
|
fieldName := tokens[i].Text(file.Bytes)
|
||||||
i++
|
i++
|
||||||
fieldTypeName := tokens[i].Text(file.Bytes)
|
fieldTypeName := tokens[i].Text(file.Bytes)
|
||||||
fieldType := types.Int
|
|
||||||
|
|
||||||
switch fieldTypeName {
|
|
||||||
case "Int", "Int64":
|
|
||||||
case "Int32":
|
|
||||||
fieldType = types.Int32
|
|
||||||
case "Int16":
|
|
||||||
fieldType = types.Int16
|
|
||||||
case "Int8":
|
|
||||||
fieldType = types.Int8
|
|
||||||
default:
|
|
||||||
panic("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
i++
|
i++
|
||||||
|
|
||||||
structure.AddField(&types.Field{
|
structure.AddField(&types.Field{
|
||||||
Type: fieldType,
|
|
||||||
Name: fieldName,
|
Name: fieldName,
|
||||||
|
TypeName: fieldTypeName,
|
||||||
Position: token.Position(fieldPosition),
|
Position: token.Position(fieldPosition),
|
||||||
Offset: structure.Size(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
src/types/Base.go
Normal file
17
src/types/Base.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
// Base is used to describe basic types like integers and floats.
|
||||||
|
type Base struct {
|
||||||
|
name string
|
||||||
|
size int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name returns the name of the type.
|
||||||
|
func (s *Base) Name() string {
|
||||||
|
return s.name
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size returns the total size in bytes.
|
||||||
|
func (s *Base) Size() int {
|
||||||
|
return s.size
|
||||||
|
}
|
@ -4,8 +4,9 @@ import "git.akyoto.dev/cli/q/src/token"
|
|||||||
|
|
||||||
// Field is a memory region in a data structure.
|
// Field is a memory region in a data structure.
|
||||||
type Field struct {
|
type Field struct {
|
||||||
Type Type
|
|
||||||
Name string
|
Name string
|
||||||
|
Type Type
|
||||||
|
TypeName string
|
||||||
Position token.Position
|
Position token.Position
|
||||||
Offset int
|
Offset int
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Float64 = &Struct{name: "Float64", size: 8}
|
Float64 = &Base{name: "Float64", size: 8}
|
||||||
Float32 = &Struct{name: "Float32", size: 4}
|
Float32 = &Base{name: "Float32", size: 4}
|
||||||
Float = Float64
|
Float = Float64
|
||||||
)
|
)
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Int64 = &Struct{name: "Int64", size: 8}
|
Int64 = &Base{name: "Int64", size: 8}
|
||||||
Int32 = &Struct{name: "Int32", size: 4}
|
Int32 = &Base{name: "Int32", size: 4}
|
||||||
Int16 = &Struct{name: "Int16", size: 2}
|
Int16 = &Base{name: "Int16", size: 2}
|
||||||
Int8 = &Struct{name: "Int8", size: 1}
|
Int8 = &Base{name: "Int8", size: 1}
|
||||||
Int = Int64
|
Int = Int64
|
||||||
)
|
)
|
||||||
|
@ -15,7 +15,6 @@ func NewStruct(name string) *Struct {
|
|||||||
// AddField adds a new field to the end of the struct.
|
// AddField adds a new field to the end of the struct.
|
||||||
func (s *Struct) AddField(field *Field) {
|
func (s *Struct) AddField(field *Field) {
|
||||||
s.fields = append(s.fields, field)
|
s.fields = append(s.fields, field)
|
||||||
s.size += field.Type.Size()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FieldByName returns the field with the given name if it exists.
|
// FieldByName returns the field with the given name if it exists.
|
||||||
@ -38,3 +37,14 @@ func (s *Struct) Name() string {
|
|||||||
func (s *Struct) Size() int {
|
func (s *Struct) Size() int {
|
||||||
return s.size
|
return s.size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update updates the offsets and structure size.
|
||||||
|
func (s *Struct) Update(types map[string]Type) {
|
||||||
|
s.size = 0
|
||||||
|
|
||||||
|
for _, field := range s.fields {
|
||||||
|
field.Type = types[field.TypeName]
|
||||||
|
field.Offset = s.size
|
||||||
|
s.size += field.Type.Size()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user