Improved type system

This commit is contained in:
2024-08-08 12:55:25 +02:00
parent d624a5f895
commit 5d9be01a85
20 changed files with 111 additions and 67 deletions

View File

@ -1,11 +1,10 @@
package core
import (
"strings"
"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"
)
// CompileCall executes a function call.
@ -18,7 +17,6 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
nameNode = root.Children[0]
fn *Function
name string
fullName string
exists bool
)
@ -47,12 +45,7 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
imp.Used = true
}
tmp := strings.Builder{}
tmp.WriteString(pkg)
tmp.WriteString(".")
tmp.WriteString(name)
fullName = tmp.String()
fn, exists = f.Functions[fullName]
fn, exists = f.Functions[pkg+"."+name]
if !exists {
return nil, errors.New(&errors.UnknownFunction{Name: name}, f.File, nameNode.Token.Position)
@ -68,10 +61,10 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
return nil, err
}
if typ != fn.Parameters[i].Type {
if !types.Check(typ, fn.Parameters[i].Type) {
return nil, errors.New(&errors.TypeMismatch{
Encountered: string(typ),
Expected: string(fn.Parameters[i].Type),
Encountered: typ.Name,
Expected: fn.Parameters[i].Type.Name,
ParameterName: fn.Parameters[i].Name,
}, f.File, parameters[i].Token.Position)
}
@ -87,7 +80,7 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
}
}
f.Call(fullName)
f.Call(fn.UniqueName)
for _, register := range registers {
if register == f.CPU.Output[0] && root.Parent != nil {

View File

@ -5,7 +5,6 @@ import (
"git.akyoto.dev/cli/q/src/ast"
"git.akyoto.dev/cli/q/src/errors"
"git.akyoto.dev/cli/q/src/expression"
"git.akyoto.dev/cli/q/src/types"
)
// CompileDefinition compiles a variable definition.
@ -28,7 +27,7 @@ func (f *Function) CompileDefinition(node *ast.Define) error {
variable.Type = typ
if variable.Type == types.Invalid {
if variable.Type == nil {
return errors.New(errors.UnknownType, f.File, node.Expression.Token.End())
}

View File

@ -21,10 +21,10 @@ func (f *Function) CompileReturn(node *ast.Return) error {
return err
}
if typ != types.Any && typ != f.ReturnTypes[i] {
if !types.Check(typ, f.ReturnTypes[i]) {
return errors.New(&errors.TypeMismatch{
Encountered: string(typ),
Expected: string(f.ReturnTypes[i]),
Encountered: typ.Name,
Expected: f.ReturnTypes[i].Name,
ParameterName: "",
IsReturn: true,
}, f.File, node.Values[i].Token.Position)

View File

@ -8,7 +8,7 @@ import (
)
// Evaluate evaluates an expression and returns a register that contains the value of the expression.
func (f *Function) Evaluate(expr *expression.Expression) (types.Type, cpu.Register, error) {
func (f *Function) Evaluate(expr *expression.Expression) (*types.Type, cpu.Register, error) {
if expr.Token.Kind == token.Identifier {
name := expr.Token.Text(f.File.Bytes)
variable := f.VariableByName(name)

View File

@ -9,18 +9,18 @@ import (
)
// ExpressionToMemory puts the result of an expression into the specified memory address.
func (f *Function) ExpressionToMemory(node *expression.Expression, memory asm.Memory) (types.Type, error) {
func (f *Function) ExpressionToMemory(node *expression.Expression, memory asm.Memory) (*types.Type, error) {
if node.IsLeaf() && node.Token.IsNumeric() {
number, err := f.Number(node.Token)
if err != nil {
return types.Invalid, err
return nil, err
}
size := byte(sizeof.Signed(int64(number)))
if size != memory.Length {
return types.Invalid, errors.New(&errors.NumberExceedsBounds{Number: number, ExpectedSize: memory.Length, Size: size}, f.File, node.Token.Position)
return nil, errors.New(&errors.NumberExceedsBounds{Number: number, ExpectedSize: memory.Length, Size: size}, f.File, node.Token.Position)
}
f.MemoryNumber(asm.STORE, memory, number)

View File

@ -11,7 +11,7 @@ import (
)
// ExpressionToRegister puts the result of an expression into the specified register.
func (f *Function) ExpressionToRegister(node *expression.Expression, register cpu.Register) (types.Type, error) {
func (f *Function) ExpressionToRegister(node *expression.Expression, register cpu.Register) (*types.Type, error) {
f.SaveRegister(register)
if node.IsFolded {
@ -31,7 +31,7 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
}
if fn == nil || len(fn.ReturnTypes) == 0 {
return types.Any, err
return nil, err
}
return fn.ReturnTypes[0], err
@ -46,7 +46,7 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
if len(node.Children) == 1 {
if !node.Token.IsUnaryOperator() {
return types.Invalid, errors.New(errors.MissingOperand, f.File, node.Token.End())
return nil, errors.New(errors.MissingOperand, f.File, node.Token.End())
}
typ, err := f.ExpressionToRegister(node.Children[0], register)
@ -69,10 +69,10 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
typ, err := f.ExpressionToRegister(left, register)
if err != nil {
return types.Invalid, err
return nil, err
}
if typ == types.Pointer && (node.Token.Kind == token.Add || node.Token.Kind == token.Sub) && right.Token.Kind == token.Identifier && f.VariableByName(right.Token.Text(f.File.Bytes)).Type == types.Pointer {
if typ == types.Pointer && right.Token.Kind == token.Identifier && f.VariableByName(right.Token.Text(f.File.Bytes)).Type == types.Pointer {
typ = types.Int
}

View File

@ -17,7 +17,7 @@ type Function struct {
File *fs.File
Body token.List
Parameters []*scope.Variable
ReturnTypes []types.Type
ReturnTypes []*types.Type
Functions map[string]*Function
Err error
deferred []func()

View File

@ -10,14 +10,14 @@ import (
// TokenToRegister moves a token into a register.
// It only works with identifiers, numbers and strings.
func (f *Function) TokenToRegister(t token.Token, register cpu.Register) (types.Type, error) {
func (f *Function) TokenToRegister(t token.Token, register cpu.Register) (*types.Type, error) {
switch t.Kind {
case token.Identifier:
name := t.Text(f.File.Bytes)
variable := f.VariableByName(name)
if variable == nil {
return types.Invalid, errors.New(&errors.UnknownIdentifier{Name: name}, f.File, t.Position)
return nil, errors.New(&errors.UnknownIdentifier{Name: name}, f.File, t.Position)
}
f.UseVariable(variable)
@ -29,7 +29,7 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) (types.
number, err := f.Number(t)
if err != nil {
return types.Invalid, err
return nil, err
}
f.SaveRegister(register)
@ -45,6 +45,6 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) (types.
return types.Pointer, nil
default:
return types.Invalid, errors.New(errors.InvalidExpression, f.File, t.Position)
return nil, errors.New(errors.InvalidExpression, f.File, t.Position)
}
}