Fixed incorrect division results
This commit is contained in:
parent
7598411c8f
commit
c088697446
@ -22,7 +22,7 @@ main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isPrime(x int) -> int {
|
isPrime(x int) -> bool {
|
||||||
if x == 2 {
|
if x == 2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ func (f *Function) CompileAssign(node *ast.Assign) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if left.Token.Kind == token.Separator && right.Token.Kind == token.Div {
|
if left.Token.Kind == token.Separator && right.Token.Kind == token.Div {
|
||||||
return f.CompileAssignDivision(node)
|
return f.CompileAssignDivision(node.Expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ast.IsFunctionCall(right) {
|
if !ast.IsFunctionCall(right) {
|
||||||
|
@ -2,30 +2,60 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"git.akyoto.dev/cli/q/src/asm"
|
"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/errors"
|
||||||
|
"git.akyoto.dev/cli/q/src/expression"
|
||||||
|
"git.akyoto.dev/cli/q/src/scope"
|
||||||
|
"git.akyoto.dev/cli/q/src/token"
|
||||||
|
"git.akyoto.dev/cli/q/src/types"
|
||||||
"git.akyoto.dev/cli/q/src/x86"
|
"git.akyoto.dev/cli/q/src/x86"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CompileAssignDivision compiles an assign statement that has quotient and remainder on the left side and division on the right.
|
// 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 {
|
func (f *Function) CompileAssignDivision(expr *expression.Expression) error {
|
||||||
left := node.Expression.Children[0]
|
var (
|
||||||
right := node.Expression.Children[1]
|
left = expr.Children[0]
|
||||||
|
right = expr.Children[1]
|
||||||
|
quotientVariable *scope.Variable
|
||||||
|
remainderVariable *scope.Variable
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
quotient := left.Children[0]
|
if expr.Token.Kind == token.Define {
|
||||||
name := quotient.Token.Text(f.File.Bytes)
|
quotientVariable, err = f.Define(left.Children[0])
|
||||||
quotientVariable := f.VariableByName(name)
|
|
||||||
|
|
||||||
if quotientVariable == nil {
|
if err != nil {
|
||||||
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, quotient.Token.Position)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
remainder := left.Children[1]
|
remainderVariable, err = f.Define(left.Children[1])
|
||||||
name = remainder.Token.Text(f.File.Bytes)
|
|
||||||
remainderVariable := f.VariableByName(name)
|
|
||||||
|
|
||||||
if remainderVariable == nil {
|
if err != nil {
|
||||||
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, remainder.Token.Position)
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
quotientVariable.Type = types.Int
|
||||||
|
remainderVariable.Type = types.Int
|
||||||
|
f.AddVariable(quotientVariable)
|
||||||
|
f.AddVariable(remainderVariable)
|
||||||
|
} else {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.UseVariable(quotientVariable)
|
||||||
|
defer f.UseVariable(remainderVariable)
|
||||||
}
|
}
|
||||||
|
|
||||||
dividend := right.Children[0]
|
dividend := right.Children[0]
|
||||||
@ -38,9 +68,7 @@ func (f *Function) CompileAssignDivision(node *ast.Assign) error {
|
|||||||
divisor := right.Children[1]
|
divisor := right.Children[1]
|
||||||
err = f.Execute(right.Token, dividendRegister, divisor)
|
err = f.Execute(right.Token, dividendRegister, divisor)
|
||||||
f.RegisterRegister(asm.MOVE, quotientVariable.Register, x86.RAX)
|
f.RegisterRegister(asm.MOVE, quotientVariable.Register, x86.RAX)
|
||||||
f.UseVariable(quotientVariable)
|
|
||||||
f.RegisterRegister(asm.MOVE, remainderVariable.Register, x86.RDX)
|
f.RegisterRegister(asm.MOVE, remainderVariable.Register, x86.RDX)
|
||||||
f.UseVariable(remainderVariable)
|
|
||||||
|
|
||||||
if isTemporary {
|
if isTemporary {
|
||||||
f.FreeRegister(dividendRegister)
|
f.FreeRegister(dividendRegister)
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"git.akyoto.dev/cli/q/src/ast"
|
"git.akyoto.dev/cli/q/src/ast"
|
||||||
"git.akyoto.dev/cli/q/src/errors"
|
"git.akyoto.dev/cli/q/src/errors"
|
||||||
"git.akyoto.dev/cli/q/src/expression"
|
"git.akyoto.dev/cli/q/src/expression"
|
||||||
|
"git.akyoto.dev/cli/q/src/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CompileDefinition compiles a variable definition.
|
// CompileDefinition compiles a variable definition.
|
||||||
@ -34,6 +35,10 @@ func (f *Function) CompileDefinition(node *ast.Define) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if left.Token.Kind == token.Separator && right.Token.Kind == token.Div {
|
||||||
|
return f.CompileAssignDivision(node.Expression)
|
||||||
|
}
|
||||||
|
|
||||||
if !ast.IsFunctionCall(right) {
|
if !ast.IsFunctionCall(right) {
|
||||||
return errors.New(errors.NotImplemented, f.File, node.Expression.Token.Position)
|
return errors.New(errors.NotImplemented, f.File, node.Expression.Token.Position)
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,10 @@ func (f *Function) ExecuteRegisterNumber(operation token.Token, register cpu.Reg
|
|||||||
f.RegisterNumber(asm.MUL, register, number)
|
f.RegisterNumber(asm.MUL, register, number)
|
||||||
|
|
||||||
case token.Div, token.DivAssign:
|
case token.Div, token.DivAssign:
|
||||||
f.SaveRegister(x86.RAX)
|
if register != x86.RAX {
|
||||||
|
f.SaveRegister(x86.RAX)
|
||||||
|
}
|
||||||
|
|
||||||
f.SaveRegister(x86.RDX)
|
f.SaveRegister(x86.RDX)
|
||||||
tmp := f.NewRegister()
|
tmp := f.NewRegister()
|
||||||
f.RegisterNumber(asm.MOVE, tmp, number)
|
f.RegisterNumber(asm.MOVE, tmp, number)
|
||||||
|
@ -25,7 +25,10 @@ func (f *Function) ExecuteRegisterRegister(operation token.Token, register cpu.R
|
|||||||
f.RegisterRegister(asm.MUL, register, operand)
|
f.RegisterRegister(asm.MUL, register, operand)
|
||||||
|
|
||||||
case token.Div, token.DivAssign:
|
case token.Div, token.DivAssign:
|
||||||
f.SaveRegister(x86.RAX)
|
if register != x86.RAX {
|
||||||
|
f.SaveRegister(x86.RAX)
|
||||||
|
}
|
||||||
|
|
||||||
f.SaveRegister(x86.RDX)
|
f.SaveRegister(x86.RDX)
|
||||||
f.RegisterRegister(asm.DIV, register, operand)
|
f.RegisterRegister(asm.DIV, register, operand)
|
||||||
|
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
main() {
|
main() {
|
||||||
quotient := 0
|
|
||||||
remainder := 0
|
|
||||||
dividend := 256
|
dividend := 256
|
||||||
divisor := 100
|
divisor := 100
|
||||||
|
|
||||||
quotient, remainder = 256 / 100
|
quotient, remainder := 256 / 100
|
||||||
assert quotient == 2
|
assert quotient == 2
|
||||||
assert remainder == 56
|
assert remainder == 56
|
||||||
|
|
||||||
quotient, remainder = dividend / 100
|
quotient, remainder = dividend / 100
|
||||||
assert quotient == 2
|
assert quotient == 2
|
||||||
assert remainder == 56
|
assert remainder == 56
|
||||||
@ -15,7 +13,7 @@ main() {
|
|||||||
quotient, remainder = 256 / divisor
|
quotient, remainder = 256 / divisor
|
||||||
assert quotient == 2
|
assert quotient == 2
|
||||||
assert remainder == 56
|
assert remainder == 56
|
||||||
|
|
||||||
quotient, remainder = dividend / divisor
|
quotient, remainder = dividend / divisor
|
||||||
assert quotient == 2
|
assert quotient == 2
|
||||||
assert remainder == 56
|
assert remainder == 56
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
main() {
|
|
||||||
f(10)
|
|
||||||
}
|
|
||||||
|
|
||||||
f(new int) {
|
|
||||||
old := new
|
|
||||||
new -= 1
|
|
||||||
assert new != old
|
|
||||||
}
|
|
30
tests/programs/operator-assign.q
Normal file
30
tests/programs/operator-assign.q
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
main() {
|
||||||
|
number(10)
|
||||||
|
register(10)
|
||||||
|
}
|
||||||
|
|
||||||
|
number(x int) {
|
||||||
|
y := x
|
||||||
|
x -= 1
|
||||||
|
assert x < y
|
||||||
|
x += 1
|
||||||
|
assert x == y
|
||||||
|
x *= 2
|
||||||
|
assert x > y
|
||||||
|
x /= 2
|
||||||
|
assert x == y
|
||||||
|
}
|
||||||
|
|
||||||
|
register(x int) {
|
||||||
|
y := x
|
||||||
|
num := 1
|
||||||
|
x -= num
|
||||||
|
assert x < y
|
||||||
|
x += num
|
||||||
|
assert x == y
|
||||||
|
num = 2
|
||||||
|
x *= num
|
||||||
|
assert x > y
|
||||||
|
x /= num
|
||||||
|
assert x == y
|
||||||
|
}
|
@ -7,11 +7,11 @@ main() {
|
|||||||
f := 6
|
f := 6
|
||||||
g := 7
|
g := 7
|
||||||
|
|
||||||
assert a != 0
|
assert a == 1
|
||||||
assert b != 0
|
assert b == 2
|
||||||
assert c != 0
|
assert c == 3
|
||||||
assert d != 0
|
assert d == 4
|
||||||
assert e != 0
|
assert e == 5
|
||||||
assert f != 0
|
assert f == 6
|
||||||
assert g != 0
|
assert g == 7
|
||||||
}
|
}
|
@ -14,58 +14,56 @@ import (
|
|||||||
|
|
||||||
var programs = []struct {
|
var programs = []struct {
|
||||||
Name string
|
Name string
|
||||||
Input string
|
|
||||||
Output string
|
|
||||||
ExitCode int
|
ExitCode int
|
||||||
}{
|
}{
|
||||||
{"empty", "", "", 0},
|
{"empty", 0},
|
||||||
{"assert", "", "", 1},
|
{"assert", 1},
|
||||||
{"variables", "", "", 0},
|
{"binary", 0},
|
||||||
{"reassign", "", "", 0},
|
{"octal", 0},
|
||||||
{"reuse", "", "", 0},
|
{"hexadecimal", 0},
|
||||||
{"return", "", "", 0},
|
{"variables", 0},
|
||||||
{"return-multi", "", "", 0},
|
{"reassign", 0},
|
||||||
{"math", "", "", 0},
|
{"reuse", 0},
|
||||||
{"precedence", "", "", 0},
|
{"return", 0},
|
||||||
{"op-assign", "", "", 0},
|
{"return-multi", 0},
|
||||||
{"binary", "", "", 0},
|
{"math", 0},
|
||||||
{"octal", "", "", 0},
|
{"precedence", 0},
|
||||||
{"hexadecimal", "", "", 0},
|
{"operator-assign", 0},
|
||||||
{"const", "", "", 0},
|
{"const", 0},
|
||||||
{"escape-rune", "", "", 0},
|
{"escape-rune", 0},
|
||||||
{"escape-string", "", "", 0},
|
{"escape-string", 0},
|
||||||
{"bitwise-and", "", "", 0},
|
{"bitwise-and", 0},
|
||||||
{"bitwise-or", "", "", 0},
|
{"bitwise-or", 0},
|
||||||
{"bitwise-xor", "", "", 0},
|
{"bitwise-xor", 0},
|
||||||
{"shift", "", "", 0},
|
{"shift", 0},
|
||||||
{"modulo", "", "", 0},
|
{"modulo", 0},
|
||||||
{"modulo-assign", "", "", 0},
|
{"modulo-assign", 0},
|
||||||
{"div-split", "", "", 0},
|
{"div-split", 0},
|
||||||
{"int64", "", "", 0},
|
{"int64", 0},
|
||||||
{"negative", "", "", 0},
|
{"negative", 0},
|
||||||
{"negation", "", "", 0},
|
{"negation", 0},
|
||||||
{"square-sum", "", "", 0},
|
{"square-sum", 0},
|
||||||
{"chained-calls", "", "", 0},
|
{"chained-calls", 0},
|
||||||
{"nested-calls", "", "", 0},
|
{"nested-calls", 0},
|
||||||
{"param", "", "", 0},
|
{"param", 0},
|
||||||
{"param-multi", "", "", 0},
|
{"param-multi", 0},
|
||||||
{"param-order", "", "", 0},
|
{"param-order", 0},
|
||||||
{"branch", "", "", 0},
|
{"branch", 0},
|
||||||
{"branch-and", "", "", 0},
|
{"branch-and", 0},
|
||||||
{"branch-or", "", "", 0},
|
{"branch-or", 0},
|
||||||
{"branch-both", "", "", 0},
|
{"branch-both", 0},
|
||||||
{"branch-save", "", "", 0},
|
{"branch-save", 0},
|
||||||
{"jump-near", "", "", 0},
|
{"jump-near", 0},
|
||||||
{"switch", "", "", 0},
|
{"switch", 0},
|
||||||
{"loop-infinite", "", "", 0},
|
{"loop-infinite", 0},
|
||||||
{"loop-lifetime", "", "", 0},
|
{"loop-lifetime", 0},
|
||||||
{"loop-for", "", "", 0},
|
{"loop-for", 0},
|
||||||
{"memory-free", "", "", 0},
|
{"memory-free", 0},
|
||||||
{"out-of-memory", "", "", 0},
|
{"out-of-memory", 0},
|
||||||
{"index-static", "", "", 0},
|
{"index-static", 0},
|
||||||
{"index-dynamic", "", "", 0},
|
{"index-dynamic", 0},
|
||||||
{"struct", "", "", 0},
|
{"struct", 0},
|
||||||
{"len", "", "", 0},
|
{"len", 0},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrograms(t *testing.T) {
|
func TestPrograms(t *testing.T) {
|
||||||
@ -74,12 +72,12 @@ func TestPrograms(t *testing.T) {
|
|||||||
|
|
||||||
t.Run(test.Name+"/debug", func(t *testing.T) {
|
t.Run(test.Name+"/debug", func(t *testing.T) {
|
||||||
config.Optimize(false)
|
config.Optimize(false)
|
||||||
run(t, file, "debug", test.Input, test.Output, test.ExitCode)
|
run(t, file, "debug", "", "", test.ExitCode)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run(test.Name+"/release", func(t *testing.T) {
|
t.Run(test.Name+"/release", func(t *testing.T) {
|
||||||
config.Optimize(true)
|
config.Optimize(true)
|
||||||
run(t, file, "release", test.Input, test.Output, test.ExitCode)
|
run(t, file, "release", "", "", test.ExitCode)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user