Improved division split

This commit is contained in:
Eduard Urbach 2024-07-28 18:12:42 +02:00
parent bb74c0cf50
commit c2c147f1b4
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
11 changed files with 84 additions and 64 deletions

View File

@ -15,7 +15,7 @@ func Count(body AST, buffer []byte, kind token.Kind, name string) uint8 {
count += node.Expression.Count(buffer, kind, name)
case *Define:
count += node.Value.Count(buffer, kind, name)
count += node.Expression.Count(buffer, kind, name)
case *Return:
if node.Value != nil {

View File

@ -2,11 +2,9 @@ package ast
import (
"git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/token"
)
// Define represents a variable definition.
type Define struct {
Value *expression.Expression
Name token.Token
Expression *expression.Expression
}

View File

@ -83,9 +83,7 @@ func toASTNode(tokens token.List, buffer []byte) (Node, error) {
return nil, errors.New(errors.MissingOperand, nil, expr.Token.End())
}
name := expr.Children[0].Token
value := expr.Children[1]
return &Define{Name: name, Value: value}, nil
return &Define{Expression: expr}, nil
case IsAssignment(expr):
if len(expr.Children) < 2 {

View File

@ -29,11 +29,14 @@ func (f *Function) CompileAssignDivision(node *ast.Assign) error {
}
dividend := right.Children[0]
name = dividend.Token.Text(f.File.Bytes)
dividendVariable := f.VariableByName(name)
dividendRegister, err := f.Evaluate(dividend)
if err != nil {
return err
}
divisor := right.Children[1]
err := f.Execute(right.Token, dividendVariable.Register, divisor)
err = f.Execute(right.Token, dividendRegister, divisor)
f.RegisterRegister(asm.MOVE, quotientVariable.Register, x64.RAX)
f.RegisterRegister(asm.MOVE, remainderVariable.Register, x64.RDX)
return err

View File

@ -9,20 +9,22 @@ import (
// CompileDefinition compiles a variable definition.
func (f *Function) CompileDefinition(node *ast.Define) error {
name := node.Name.Text(f.File.Bytes)
left := node.Expression.Children[0]
right := node.Expression.Children[1]
name := left.Token.Text(f.File.Bytes)
if f.IdentifierExists(name) {
return errors.New(&errors.VariableAlreadyExists{Name: name}, f.File, node.Name.Position)
return errors.New(&errors.VariableAlreadyExists{Name: name}, f.File, left.Token.Position)
}
uses := token.Count(f.Body, f.File.Bytes, token.Identifier, name) - 1
if uses == 0 {
return errors.New(&errors.UnusedVariable{Name: name}, f.File, node.Name.Position)
return errors.New(&errors.UnusedVariable{Name: name}, f.File, left.Token.Position)
}
register := f.NewRegister()
err := f.ExpressionToRegister(node.Value, register)
err := f.ExpressionToRegister(right, register)
f.AddVariable(&scope.Variable{
Name: name,

View File

@ -0,0 +1,24 @@
package core
import (
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/token"
)
// Evaluate evaluates an expression and returns a register that contains the value of the expression.
func (f *Function) Evaluate(expr *expression.Expression) (cpu.Register, error) {
if expr.Token.Kind == token.Identifier {
name := expr.Token.Text(f.File.Bytes)
variable := f.VariableByName(name)
if variable.Alive == 1 {
f.UseVariable(variable)
return variable.Register, nil
}
}
tmp := f.NewRegister()
err := f.ExpressionToRegister(expr, tmp)
return tmp, err
}

View File

@ -0,0 +1,22 @@
main() {
quotient := 0
remainder := 0
dividend := 256
divisor := 100
quotient, remainder = 256 / 100
assert quotient == 2
assert remainder == 56
quotient, remainder = dividend / 100
assert quotient == 2
assert remainder == 56
quotient, remainder = 256 / divisor
assert quotient == 2
assert remainder == 56
quotient, remainder = dividend / divisor
assert quotient == 2
assert remainder == 56
}

View File

@ -0,0 +1,8 @@
main() {
x := 256
x %= 100
assert x == 56
x %= 10
assert x == 6
}

9
tests/programs/modulo.q Normal file
View File

@ -0,0 +1,9 @@
main() {
assert 0 % 1 == 0
assert 1 % 1 == 0
assert 2 % 1 == 0
assert 0 % 2 == 0
assert 1 % 2 == 1
assert 2 % 2 == 0
assert 3 % 2 == 1
}

View File

@ -1,46 +0,0 @@
import sys
main() {
if 0 % 1 != 0 {
sys.exit(1)
}
if 1 % 1 != 0 {
sys.exit(1)
}
if 2 % 1 != 0 {
sys.exit(1)
}
if 0 % 2 != 0 {
sys.exit(1)
}
if 1 % 2 != 1 {
sys.exit(1)
}
if 2 % 2 != 0 {
sys.exit(1)
}
if 3 % 2 != 1 {
sys.exit(1)
}
x := 256
x %= 100
if x != 56 {
sys.exit(1)
}
x %= 10
if x != 6 {
sys.exit(1)
}
sys.exit(0)
}

View File

@ -33,14 +33,16 @@ var programs = []struct {
{"branch-and", "", "", 0},
{"branch-or", "", "", 0},
{"branch-both", "", "", 0},
{"jump-near", "", "", 0},
{"loop", "", "", 0},
{"loop-lifetime", "", "", 0},
{"bitwise-and", "", "", 0},
{"bitwise-or", "", "", 0},
{"bitwise-xor", "", "", 0},
{"shift", "", "", 0},
{"remainder", "", "", 0},
{"jump-near", "", "", 0},
{"loop", "", "", 0},
{"loop-lifetime", "", "", 0},
{"modulo", "", "", 0},
{"modulo-assign", "", "", 0},
{"division-split", "", "", 0},
{"assert", "", "", 1},
{"negative", "", "", 32},
{"negation", "", "", 32},