Replaced builtin store function with a new syntax

This commit is contained in:
2025-04-11 14:21:51 +02:00
parent a9d783c675
commit 43a006e4af
12 changed files with 54 additions and 46 deletions

View File

@ -56,6 +56,13 @@ func (f *Function) CompileAssign(node *ast.Assign) error {
return err
}
leftSize := leftValue.Memory.Length
rightSize := uint8(rightValue.Type().Size())
if rightSize != 0 && leftSize != rightSize {
panic("memory store length mismatch")
}
f.ValueToMemory(rightValue, leftValue.Memory)
return nil
}

View File

@ -41,9 +41,6 @@ func (f *Function) CompileCall(root *expression.Expression) ([]*Parameter, error
case "delete":
return nil, f.CompileDelete(root)
case "store":
return nil, f.CompileMemoryStore(root)
}
}

View File

@ -46,7 +46,7 @@ func (f *Function) CompileFor(loop *ast.For) error {
panic("could not recognize loop header")
}
_, err := f.ExpressionToRegister(from, counter)
typ, err := f.ExpressionToRegister(from, counter)
if err != nil {
return err
@ -98,6 +98,7 @@ func (f *Function) CompileFor(loop *ast.For) error {
variable = &scope.Variable{
Name: name,
Value: eval.Register{
Typ: typ,
Register: counter,
Alive: ast.Count(loop.Body, f.File.Bytes, token.Identifier, name),
},

View File

@ -1,36 +0,0 @@
package core
import (
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/errors"
"git.urbach.dev/cli/q/src/expression"
)
// CompileMemoryStore compiles a variable-width store to memory.
func (f *Function) CompileMemoryStore(root *expression.Expression) error {
parameters := root.Children[1:]
name := parameters[0].Token.Text(f.File.Bytes)
numBytes, err := f.ToNumber(parameters[1].Token)
if err != nil {
return err
}
value := parameters[2]
variable := f.VariableByName(name)
if variable == nil {
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, parameters[0].Token.Position)
}
defer f.UseVariable(variable)
memory := asm.Memory{
Base: variable.Value.Register,
OffsetRegister: -1,
Length: byte(numBytes),
}
_, err = f.ExpressionToMemory(value, memory)
return err
}

View File

@ -28,6 +28,24 @@ func (f *Function) EvaluateArray(expr *expression.Expression) (*eval.Memory, err
Length: byte(1),
}
if len(expr.Children) == 1 {
pointer, isPointer := base.Value.Typ.(*types.Pointer)
if !isPointer {
return nil, errors.New(&errors.TypeMismatch{Encountered: base.Value.Typ.Name(), Expected: types.AnyPointer.Name()}, f.File, expr.Token.Position)
}
// TODO: This is a hack that needs to be removed
memory.Length = 8
value := &eval.Memory{
Typ: pointer.To,
Memory: memory,
}
return value, nil
}
indexExpr := expr.Children[1]
index, err := f.Evaluate(indexExpr)

View File

@ -112,6 +112,11 @@ func TestParse(t *testing.T) {
{"Array access 6", "a.b()[c]", "(@ (λ (. a b)) c)"},
{"Array access 7", "a.b(c)[d]", "(@ (λ (. a b) c) d)"},
{"Array access 8", "a.b(c)[d][e]", "(@ (@ (λ (. a b) c) d) e)"},
{"Dereferencing", "[a]", "(@ a)"},
{"Dereferencing 2", "[a+b]", "(@ (+ a b))"},
{"Dereferencing 3", "[a+b]=c", "(= (@ (+ a b)) c)"},
{"Dereferencing 3", "[a+b]=c+d", "(= (@ (+ a b)) (+ c d))"},
}
for _, test := range tests {

View File

@ -75,8 +75,17 @@ func Parse(tokens []token.Token) *Expression {
group.Precedence = math.MaxInt8
if cursor == nil {
cursor = group
root = group
if t.Kind == token.ArrayEnd {
cursor = New()
cursor.Token.Position = tokens[groupPosition].Position
cursor.Token.Kind = token.Array
cursor.Precedence = precedence(token.Array)
cursor.AddChild(group)
root = cursor
} else {
cursor = group
root = group
}
} else {
cursor.AddChild(group)
}