Added more tests

This commit is contained in:
2025-02-14 00:26:48 +01:00
parent e4cbb91f61
commit c8b5c4dbfe
8 changed files with 73 additions and 13 deletions

View File

@ -7,7 +7,7 @@ import (
"git.akyoto.dev/cli/q/src/ast"
"git.akyoto.dev/cli/q/src/cpu"
"git.akyoto.dev/cli/q/src/errors"
"git.akyoto.dev/cli/q/src/token"
"git.akyoto.dev/cli/q/src/types"
)
// CompileAssignArray compiles an assign statement for array elements.
@ -33,7 +33,7 @@ func (f *Function) CompileAssignArray(node *ast.Assign) error {
index := left.Children[1]
if index.Token.Kind == token.Number {
if index.Token.IsNumeric() {
offset, err := f.Number(index.Token)
if err != nil {
@ -42,12 +42,16 @@ func (f *Function) CompileAssignArray(node *ast.Assign) error {
memory.Offset = int8(offset)
} else {
_, indexRegister, isTemporary, err := f.Evaluate(index)
typ, indexRegister, isTemporary, err := f.Evaluate(index)
if err != nil {
return err
}
if !types.Is(typ, types.Int) {
return errors.New(&errors.TypeMismatch{Encountered: typ.Name(), Expected: types.Int.Name()}, f.File, index.Token.Position)
}
memory.OffsetRegister = indexRegister
if isTemporary {

View File

@ -25,12 +25,11 @@ func (f *Function) CompileDefinition(node *ast.Define) error {
return err
}
variable.Type = typ
if variable.Type == nil {
return errors.New(errors.CouldNotInferType, f.File, node.Expression.Token.End())
if typ == nil {
return errors.New(errors.UntypedExpression, f.File, node.Expression.Token.End())
}
variable.Type = typ
f.AddVariable(variable)
return nil
}

View File

@ -1,6 +1,8 @@
package core
import (
"math"
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/ast"
"git.akyoto.dev/cli/q/src/cpu"
@ -40,10 +42,46 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
}
if node.Token.Kind == token.Array {
array := f.VariableByName(node.Children[0].Token.Text(f.File.Bytes))
offset, err := f.Number(node.Children[1].Token)
f.MemoryRegister(asm.LOAD, asm.Memory{Base: array.Register, Offset: int8(offset), Length: 1}, register)
return types.Int, err
name := node.Children[0].Token.Text(f.File.Bytes)
array := f.VariableByName(name)
if array == nil {
return nil, errors.New(&errors.UnknownIdentifier{Name: name}, f.File, node.Children[0].Token.Position)
}
index := node.Children[1]
memory := asm.Memory{
Base: array.Register,
Offset: 0,
OffsetRegister: cpu.Register(math.MaxUint8),
Length: byte(1),
}
if index.Token.IsNumeric() {
offset, err := f.Number(index.Token)
if err != nil {
return nil, err
}
memory.Offset = int8(offset)
} else {
typ, err := f.ExpressionToRegister(index, register)
if err != nil {
return nil, err
}
if !types.Is(typ, types.Int) {
return nil, errors.New(&errors.TypeMismatch{Encountered: typ.Name(), Expected: types.Int.Name()}, f.File, index.Token.Position)
}
memory.OffsetRegister = register
}
f.MemoryRegister(asm.LOAD, memory, register)
return types.Int, nil
}
if node.Token.Kind == token.Period {