Added eval package

This commit is contained in:
2025-02-27 19:45:18 +01:00
parent efb3089211
commit e7afb2dab5
16 changed files with 274 additions and 148 deletions

View File

@ -1,8 +1,11 @@
package core
import (
"fmt"
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/errors"
"git.urbach.dev/cli/q/src/eval"
"git.urbach.dev/cli/q/src/expression"
"git.urbach.dev/cli/q/src/scope"
"git.urbach.dev/cli/q/src/token"
@ -13,21 +16,21 @@ import (
// CompileAssignDivision compiles an assign statement that has quotient and remainder on the left side and division on the right.
func (f *Function) CompileAssignDivision(expr *expression.Expression) error {
var (
left = expr.Children[0]
right = expr.Children[1]
variables = expr.Children[0]
division = expr.Children[1]
quotientVariable *scope.Variable
remainderVariable *scope.Variable
err error
)
if expr.Token.Kind == token.Define {
quotientVariable, err = f.Define(left.Children[0])
quotientVariable, err = f.Define(variables.Children[0])
if err != nil {
return err
}
remainderVariable, err = f.Define(left.Children[1])
remainderVariable, err = f.Define(variables.Children[1])
if err != nil {
return err
@ -38,7 +41,7 @@ func (f *Function) CompileAssignDivision(expr *expression.Expression) error {
f.AddVariable(quotientVariable)
f.AddVariable(remainderVariable)
} else {
quotient := left.Children[0]
quotient := variables.Children[0]
name := quotient.Token.Text(f.File.Bytes)
quotientVariable = f.VariableByName(name)
@ -46,7 +49,7 @@ func (f *Function) CompileAssignDivision(expr *expression.Expression) error {
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, quotient.Token.Position)
}
remainder := left.Children[1]
remainder := variables.Children[1]
name = remainder.Token.Text(f.File.Bytes)
remainderVariable = f.VariableByName(name)
@ -58,7 +61,7 @@ func (f *Function) CompileAssignDivision(expr *expression.Expression) error {
defer f.UseVariable(remainderVariable)
}
dividendExpr := right.Children[0]
dividendExpr := division.Children[0]
dividend, err := f.Evaluate(dividendExpr)
if err != nil {
@ -69,10 +72,21 @@ func (f *Function) CompileAssignDivision(expr *expression.Expression) error {
return errors.New(&errors.TypeMismatch{Encountered: dividend.Type.Name(), Expected: types.AnyInt.Name()}, f.File, dividendExpr.Token.Position)
}
divisor := right.Children[1]
err = f.Execute(right.Token, dividend.Register, divisor)
divisor := division.Children[1]
switch dividend.Kind {
case eval.Number:
f.SaveRegister(x86.RAX)
f.RegisterNumber(asm.MOVE, x86.RAX, dividend.Number)
err = f.Execute(division.Token, x86.RAX, divisor)
case eval.Register:
err = f.Execute(division.Token, dividend.Register, divisor)
defer f.FreeRegister(dividend.Register)
default:
panic(fmt.Errorf("%s: not implemented: %d", f.UniqueName, dividend.Kind))
}
f.RegisterRegister(asm.MOVE, quotientVariable.Register, x86.RAX)
f.RegisterRegister(asm.MOVE, remainderVariable.Register, x86.RDX)
f.FreeRegister(dividend.Register)
return err
}