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
|
||||
for _, function := range allFunctions {
|
||||
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)
|
||||
i++
|
||||
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++
|
||||
|
||||
structure.AddField(&types.Field{
|
||||
Type: fieldType,
|
||||
Name: fieldName,
|
||||
TypeName: fieldTypeName,
|
||||
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.
|
||||
type Field struct {
|
||||
Type Type
|
||||
Name string
|
||||
Type Type
|
||||
TypeName string
|
||||
Position token.Position
|
||||
Offset int
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package types
|
||||
|
||||
var (
|
||||
Float64 = &Struct{name: "Float64", size: 8}
|
||||
Float32 = &Struct{name: "Float32", size: 4}
|
||||
Float64 = &Base{name: "Float64", size: 8}
|
||||
Float32 = &Base{name: "Float32", size: 4}
|
||||
Float = Float64
|
||||
)
|
||||
|
@ -1,9 +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}
|
||||
Int64 = &Base{name: "Int64", size: 8}
|
||||
Int32 = &Base{name: "Int32", size: 4}
|
||||
Int16 = &Base{name: "Int16", size: 2}
|
||||
Int8 = &Base{name: "Int8", size: 1}
|
||||
Int = Int64
|
||||
)
|
||||
|
@ -15,7 +15,6 @@ func NewStruct(name string) *Struct {
|
||||
// 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.
|
||||
@ -38,3 +37,14 @@ func (s *Struct) Name() string {
|
||||
func (s *Struct) Size() int {
|
||||
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