From 3e3567cd3276cd7b9ffdacf7ef2e417ca4da600b Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 4 Aug 2024 16:17:33 +0200 Subject: [PATCH] Added prime numbers example --- examples/prime/prime.q | 47 +++++++++++++++++++++++ src/build/core/ExecuteRegisterNumber.go | 5 +++ src/build/core/ExecuteRegisterRegister.go | 5 +++ tests/examples_test.go | 1 + 4 files changed, 58 insertions(+) create mode 100644 examples/prime/prime.q diff --git a/examples/prime/prime.q b/examples/prime/prime.q new file mode 100644 index 0000000..a117bdc --- /dev/null +++ b/examples/prime/prime.q @@ -0,0 +1,47 @@ +import log +import sys + +main() { + n := 100 + i := 2 + + loop { + if i > n { + return + } + + if isPrime(i) == 1 { + if i != 2 { + sys.write(1, " ", 1) + } + + log.number(i) + } + + i += 1 + } +} + +isPrime(x) { + if x == 2 { + return 1 + } + + if x % 2 == 0 { + return 0 + } + + i := 3 + + loop { + if i * i > x { + return 1 + } + + if x % i == 0 { + return 0 + } + + i += 2 + } +} \ No newline at end of file diff --git a/src/build/core/ExecuteRegisterNumber.go b/src/build/core/ExecuteRegisterNumber.go index 1884d51..63a45f6 100644 --- a/src/build/core/ExecuteRegisterNumber.go +++ b/src/build/core/ExecuteRegisterNumber.go @@ -1,6 +1,7 @@ package core import ( + "git.akyoto.dev/cli/q/src/build/arch/x64" "git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/cpu" "git.akyoto.dev/cli/q/src/build/errors" @@ -24,12 +25,16 @@ func (f *Function) ExecuteRegisterNumber(operation token.Token, register cpu.Reg f.RegisterNumber(asm.MUL, register, number) case token.Div, token.DivAssign: + f.SaveRegister(x64.RAX) + f.SaveRegister(x64.RDX) tmp := f.NewRegister() f.RegisterNumber(asm.MOVE, tmp, number) f.RegisterRegister(asm.DIV, register, tmp) f.FreeRegister(tmp) case token.Mod, token.ModAssign: + f.SaveRegister(x64.RAX) + f.SaveRegister(x64.RDX) tmp := f.NewRegister() f.RegisterNumber(asm.MOVE, tmp, number) f.RegisterRegister(asm.MODULO, register, tmp) diff --git a/src/build/core/ExecuteRegisterRegister.go b/src/build/core/ExecuteRegisterRegister.go index 129d452..42363ae 100644 --- a/src/build/core/ExecuteRegisterRegister.go +++ b/src/build/core/ExecuteRegisterRegister.go @@ -1,6 +1,7 @@ package core import ( + "git.akyoto.dev/cli/q/src/build/arch/x64" "git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/cpu" "git.akyoto.dev/cli/q/src/build/errors" @@ -24,9 +25,13 @@ func (f *Function) ExecuteRegisterRegister(operation token.Token, register cpu.R f.RegisterRegister(asm.MUL, register, operand) case token.Div, token.DivAssign: + f.SaveRegister(x64.RAX) + f.SaveRegister(x64.RDX) f.RegisterRegister(asm.DIV, register, operand) case token.Mod, token.ModAssign: + f.SaveRegister(x64.RAX) + f.SaveRegister(x64.RDX) f.RegisterRegister(asm.MODULO, register, operand) case token.And, token.AndAssign: diff --git a/tests/examples_test.go b/tests/examples_test.go index b378045..2279740 100644 --- a/tests/examples_test.go +++ b/tests/examples_test.go @@ -24,6 +24,7 @@ var examples = []struct { {"itoa", "", "9223372036854775807", 0}, {"collatz", "", "6 3 10 5 16 8 4 2 1", 0}, {"fizzbuzz", "", "1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz", 0}, + {"prime", "", "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97", 0}, } func TestExamples(t *testing.T) {