49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package core
|
|
|
|
import (
|
|
"git.akyoto.dev/cli/q/src/asm"
|
|
"git.akyoto.dev/cli/q/src/ast"
|
|
"git.akyoto.dev/cli/q/src/errors"
|
|
"git.akyoto.dev/cli/q/src/x64"
|
|
)
|
|
|
|
// CompileAssignDivision compiles an assign statement that has quotient and remainder on the left side and division on the right.
|
|
func (f *Function) CompileAssignDivision(node *ast.Assign) error {
|
|
left := node.Expression.Children[0]
|
|
right := node.Expression.Children[1]
|
|
|
|
quotient := left.Children[0]
|
|
name := quotient.Token.Text(f.File.Bytes)
|
|
quotientVariable := f.VariableByName(name)
|
|
|
|
if quotientVariable == nil {
|
|
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, quotient.Token.Position)
|
|
}
|
|
|
|
remainder := left.Children[1]
|
|
name = remainder.Token.Text(f.File.Bytes)
|
|
remainderVariable := f.VariableByName(name)
|
|
|
|
if remainderVariable == nil {
|
|
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, remainder.Token.Position)
|
|
}
|
|
|
|
dividend := right.Children[0]
|
|
_, dividendRegister, isTemporary, err := f.Evaluate(dividend)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
divisor := right.Children[1]
|
|
err = f.Execute(right.Token, dividendRegister, divisor)
|
|
f.RegisterRegister(asm.MOVE, quotientVariable.Register, x64.RAX)
|
|
f.RegisterRegister(asm.MOVE, remainderVariable.Register, x64.RDX)
|
|
|
|
if isTemporary {
|
|
f.FreeRegister(dividendRegister)
|
|
}
|
|
|
|
return err
|
|
}
|