Implemented dynamic array indices

This commit is contained in:
2025-01-24 22:47:28 +01:00
parent c0b30e9c22
commit 6dad6fb6a6
10 changed files with 393 additions and 21 deletions

View File

@ -1,9 +1,13 @@
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"
"git.akyoto.dev/cli/q/src/errors"
"git.akyoto.dev/cli/q/src/token"
)
// CompileAssignArray compiles an assign statement for array elements.
@ -20,19 +24,33 @@ func (f *Function) CompileAssignArray(node *ast.Assign) error {
defer f.UseVariable(variable)
index := left.Children[1]
offset, err := f.Number(index.Token)
if err != nil {
return err
}
memory := asm.Memory{
Base: variable.Register,
Offset: byte(offset),
Length: byte(1),
Base: variable.Register,
Offset: 0,
OffsetRegister: cpu.Register(math.MaxUint8),
Length: byte(1),
}
_, err = f.ExpressionToMemory(right, memory)
index := left.Children[1]
if index.Token.Kind == token.Number {
offset, err := f.Number(index.Token)
if err != nil {
return err
}
memory.Offset = byte(offset)
} else {
_, indexRegister, err := f.Evaluate(index)
if err != nil {
return err
}
memory.OffsetRegister = indexRegister
}
_, err := f.ExpressionToMemory(right, memory)
return err
}