Implemented package specific structs

This commit is contained in:
2025-02-08 16:06:39 +01:00
parent 91bafc0867
commit 97cdcbd1cb
15 changed files with 121 additions and 78 deletions

View File

@ -30,15 +30,15 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
case "syscall":
return nil, f.CompileSyscall(root)
case "new":
typ, err := f.CompileNew(root)
return &Function{
Output: []*Output{
{
Type: &types.Pointer{
To: f.Types[root.Children[1].Token.Text(f.File.Bytes)],
},
Type: typ,
},
},
}, f.CompileNew(root)
}, err
case "delete":
return nil, f.CompileDelete(root)
case "store":

View File

@ -2,16 +2,49 @@ package core
import (
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/errors"
"git.akyoto.dev/cli/q/src/expression"
"git.akyoto.dev/cli/q/src/types"
)
// CompileNew compiles a `new` function call which allocates a struct.
func (f *Function) CompileNew(root *expression.Expression) error {
parameters := root.Children[1:]
structName := parameters[0].Token.Text(f.File.Bytes)
typ := f.Types[structName]
func (f *Function) CompileNew(root *expression.Expression) (types.Type, error) {
var (
parameters = root.Children[1:]
nameNode = parameters[0]
pkg = f.Package
name string
)
if nameNode.IsLeaf() {
name = nameNode.Token.Text(f.File.Bytes)
} else {
pkg = nameNode.Children[0].Token.Text(f.File.Bytes)
name = nameNode.Children[1].Token.Text(f.File.Bytes)
}
if pkg != f.File.Package {
if f.File.Imports == nil {
return nil, errors.New(&errors.UnknownPackage{Name: pkg}, f.File, nameNode.Token.Position)
}
imp, exists := f.File.Imports[pkg]
if !exists {
return nil, errors.New(&errors.UnknownPackage{Name: pkg}, f.File, nameNode.Token.Position)
}
imp.Used = true
}
typ, exists := f.Structs[pkg+"."+name]
if !exists {
return nil, errors.New(&errors.UnknownType{Name: name}, f.File, nameNode.Token.Position)
}
f.SaveRegister(f.CPU.Input[0])
f.RegisterNumber(asm.MOVE, f.CPU.Input[0], typ.Size())
f.CallSafe(f.Functions["mem.alloc"], f.CPU.Input[:1])
return nil
return &types.Pointer{To: typ}, nil
}

View File

@ -19,7 +19,7 @@ type Function struct {
Input []*Input
Output []*Output
Functions map[string]*Function
Types map[string]types.Type
Structs map[string]*types.Struct
DLLs dll.List
Err error
deferred []func()

View File

@ -4,6 +4,7 @@ import (
"git.akyoto.dev/cli/q/src/errors"
"git.akyoto.dev/cli/q/src/scope"
"git.akyoto.dev/cli/q/src/token"
"git.akyoto.dev/cli/q/src/types"
"git.akyoto.dev/cli/q/src/x86"
)
@ -12,7 +13,7 @@ func (f *Function) ResolveTypes() error {
for i, param := range f.Input {
param.Name = param.tokens[0].Text(f.File.Bytes)
typeName := param.tokens[1:].Text(f.File.Bytes)
param.Type = f.TypeByName(typeName)
param.Type = types.ByName(typeName, f.Package, f.Structs)
if param.Type == nil {
return errors.New(&errors.UnknownType{Name: typeName}, f.File, param.tokens[1].Position)
@ -34,7 +35,7 @@ func (f *Function) ResolveTypes() error {
for _, param := range f.Output {
typeName := param.tokens.Text(f.File.Bytes)
param.Type = f.TypeByName(typeName)
param.Type = types.ByName(typeName, f.Package, f.Structs)
if param.Type == nil {
return errors.New(&errors.UnknownType{Name: typeName}, f.File, param.tokens[1].Position)

View File

@ -1,17 +1 @@
package core
import (
"strings"
"git.akyoto.dev/cli/q/src/types"
)
// TypeByName returns the type with the given name or `nil` if it doesn't exist.
func (f *Function) TypeByName(name string) types.Type {
if strings.HasPrefix(name, "*") {
to := strings.TrimPrefix(name, "*")
return &types.Pointer{To: f.TypeByName(to)}
}
return f.Types[name]
}