Improved ssa compiler
All checks were successful
/ test (push) Successful in 15s

This commit is contained in:
Eduard Urbach 2025-07-02 16:55:24 +02:00
parent c9c6b94c18
commit 3301cf5542
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
49 changed files with 690 additions and 262 deletions

View file

@ -12,7 +12,6 @@ var (
Int8 = &Base{name: "int8", size: 1}
Float64 = &Base{name: "float64", size: 8}
Float32 = &Base{name: "float32", size: 4}
String = &Array{Of: Byte}
UInt64 = &Base{name: "uint64", size: 8}
UInt32 = &Base{name: "uint32", size: 4}
UInt16 = &Base{name: "uint16", size: 2}
@ -20,6 +19,19 @@ var (
Void = &Base{name: "void", size: 0}
)
var (
CString = &Pointer{To: Byte}
String = &Struct{
Package: "",
UniqueName: "string",
name: "string",
Fields: []*Field{
{Name: "ptr", Type: CString, Index: 0, Offset: 0},
{Name: "len", Type: Int, Index: 1, Offset: 8},
},
}
)
var (
Byte = UInt8
Float = Float64

17
src/types/Field.go Normal file
View file

@ -0,0 +1,17 @@
package types
import "git.urbach.dev/cli/q/src/token"
// Field is a memory region in a data structure.
type Field struct {
Name string
Type Type
Position token.Position
Index uint8
Offset uint8
}
// String returns the name of the struct.
func (f *Field) String() string {
return f.Name
}

View file

@ -45,6 +45,8 @@ func Parse[T ~[]token.Token](tokens T, source []byte) Type {
}
switch tokens[0].String(source) {
case "string":
return String
case "int":
return Int
case "int64":

50
src/types/Struct.go Normal file
View file

@ -0,0 +1,50 @@
package types
// Struct is a structure in memory whose regions are addressable with named fields.
type Struct struct {
Package string
UniqueName string
Fields []*Field
name string
}
// NewStruct creates a new struct.
func NewStruct(pkg string, name string) *Struct {
return &Struct{
Package: pkg,
UniqueName: pkg + "." + name,
name: name,
}
}
// AddField adds a new field to the end of the struct.
func (s *Struct) AddField(field *Field) {
s.Fields = append(s.Fields, field)
}
// FieldByName returns the field with the given name if it exists.
func (s *Struct) FieldByName(name string) *Field {
for _, field := range s.Fields {
if field.Name == name {
return 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 {
sum := 0
for _, field := range s.Fields {
sum += field.Type.Size()
}
return sum
}

View file

@ -13,7 +13,7 @@ func TestName(t *testing.T) {
assert.Equal(t, types.AnyPointer.Name(), "*any")
assert.Equal(t, (&types.Pointer{To: types.Int}).Name(), "*int64")
assert.Equal(t, (&types.Array{Of: types.Int}).Name(), "[]int64")
assert.Equal(t, types.String.Name(), "[]uint8")
assert.Equal(t, types.String.Name(), "string")
}
func TestSize(t *testing.T) {
@ -24,7 +24,7 @@ func TestSize(t *testing.T) {
assert.Equal(t, types.Int64.Size(), 8)
assert.Equal(t, types.AnyArray.Size(), 8)
assert.Equal(t, types.AnyPointer.Size(), 8)
assert.Equal(t, types.String.Size(), 8)
assert.Equal(t, types.String.Size(), 16)
assert.Equal(t, (&types.Pointer{To: types.Int}).Size(), 8)
}