Improved type system

This commit is contained in:
2025-02-09 23:52:07 +01:00
parent 7634244c56
commit f19d9063a5
30 changed files with 132 additions and 85 deletions

View File

@ -17,9 +17,10 @@ import (
func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
var (
pkg = f.Package
pkgNode *expression.Expression
name string
nameNode = root.Children[0]
fn *Function
name string
exists bool
)
@ -53,8 +54,11 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
return nil, f.CompileMemoryStore(root)
}
} else {
pkg = nameNode.Children[0].Token.Text(f.File.Bytes)
name = nameNode.Children[1].Token.Text(f.File.Bytes)
pkgNode = nameNode.Children[0]
nameNode = nameNode.Children[1]
pkg = pkgNode.Token.Text(f.File.Bytes)
name = nameNode.Token.Text(f.File.Bytes)
}
if pkg == "kernel32" || pkg == "user32" || pkg == "gdi32" || pkg == "comctl32" {
@ -74,13 +78,13 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
return nil, nil
} else if pkg != f.File.Package {
if f.File.Imports == nil {
return nil, errors.New(&errors.UnknownPackage{Name: pkg}, f.File, nameNode.Token.Position)
return nil, errors.New(&errors.UnknownPackage{Name: pkg}, f.File, pkgNode.Token.Position)
}
imp, exists := f.File.Imports[pkg]
if !exists {
return nil, errors.New(&errors.UnknownPackage{Name: pkg}, f.File, nameNode.Token.Position)
return nil, errors.New(&errors.UnknownPackage{Name: pkg}, f.File, pkgNode.Token.Position)
}
imp.Used = true
@ -93,6 +97,11 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
}
parameters := root.Children[1:]
if len(parameters) != len(fn.Input) {
return nil, errors.New(&errors.ParameterCountMismatch{Function: fn.Name, Count: len(parameters), ExpectedCount: len(fn.Input)}, f.File, nameNode.Token.End())
}
registers := f.CPU.Input[:len(parameters)]
for i := len(parameters) - 1; i >= 0; i-- {
@ -107,9 +116,20 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
continue
}
encountered := ""
expected := ""
if typ != nil {
encountered = typ.Name()
}
if fn.Input[i].Type != nil {
expected = fn.Input[i].Type.Name()
}
return nil, errors.New(&errors.TypeMismatch{
Encountered: typ.Name(),
Expected: fn.Input[i].Type.Name(),
Encountered: encountered,
Expected: expected,
ParameterName: fn.Input[i].Name,
}, f.File, parameters[i].Token.Position)
}