Refactored code structure

This commit is contained in:
2024-07-03 11:39:24 +02:00
parent ed03f6a802
commit feebfe65bb
54 changed files with 583 additions and 450 deletions

69
src/build/core/Execute.go Normal file
View File

@ -0,0 +1,69 @@
package core
import (
"strconv"
"git.akyoto.dev/cli/q/src/build/ast"
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/cli/q/src/build/errors"
"git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/token"
)
// Execute executes an operation on a register with a value operand.
func (f *Function) Execute(operation token.Token, register cpu.Register, value *expression.Expression) error {
if value.IsLeaf() {
return f.ExecuteLeaf(operation, register, value.Token)
}
var temporary cpu.Register
if ast.IsFunctionCall(value) {
temporary = f.cpu.Return[0]
} else {
found := false
temporary, found = f.cpu.FindFree(f.cpu.General)
if !found {
panic("no free registers")
}
}
f.cpu.Use(temporary)
defer f.cpu.Free(temporary)
err := f.EvaluateTo(value, temporary)
if err != nil {
return err
}
return f.opRegisterRegister(operation, register, temporary)
}
// ExecuteLeaf performs an operation on a register with the given leaf operand.
func (f *Function) ExecuteLeaf(operation token.Token, register cpu.Register, operand token.Token) error {
switch operand.Kind {
case token.Identifier:
name := operand.Text()
variable, exists := f.variables[name]
if !exists {
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, operand.Position)
}
defer f.useVariable(variable)
return f.opRegisterRegister(operation, register, variable.Register)
case token.Number:
value := operand.Text()
number, err := strconv.Atoi(value)
if err != nil {
return err
}
return f.opRegisterNumber(operation, register, number)
}
return errors.New(errors.NotImplemented, f.File, operation.Position)
}