From 034f27a8dd70a9936ecb0f6a4b41158995c26625 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 19 Jun 2024 23:57:43 +0200 Subject: [PATCH] added queens example, update kotest lib --- .../io_kotest_assertions_core_jvm.xml | 10 +-- .../libraries/io_kotest_runner_junit5_jvm.xml | 22 +++--- codeGenCpu6502/build.gradle | 2 +- codeGenIntermediate/build.gradle | 2 +- compiler/build.gradle | 2 +- compiler/test/TestCompilerOnExamples.kt | 1 + docs/source/todo.rst | 18 +++++ examples/queens.p8 | 70 +++++++++++++++++++ intermediate/build.gradle | 2 +- virtualmachine/build.gradle | 2 +- 10 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 examples/queens.p8 diff --git a/.idea/libraries/io_kotest_assertions_core_jvm.xml b/.idea/libraries/io_kotest_assertions_core_jvm.xml index 8f3f7ac6a..d15daa86a 100644 --- a/.idea/libraries/io_kotest_assertions_core_jvm.xml +++ b/.idea/libraries/io_kotest_assertions_core_jvm.xml @@ -1,17 +1,17 @@ - + - - + + - - + + diff --git a/.idea/libraries/io_kotest_runner_junit5_jvm.xml b/.idea/libraries/io_kotest_runner_junit5_jvm.xml index 09df403fd..9c87a4d43 100644 --- a/.idea/libraries/io_kotest_runner_junit5_jvm.xml +++ b/.idea/libraries/io_kotest_runner_junit5_jvm.xml @@ -1,14 +1,14 @@ - + - - - + + + - - + + @@ -18,12 +18,12 @@ - - + + - - - + + + diff --git a/codeGenCpu6502/build.gradle b/codeGenCpu6502/build.gradle index 8981ccaf4..f978df784 100644 --- a/codeGenCpu6502/build.gradle +++ b/codeGenCpu6502/build.gradle @@ -27,7 +27,7 @@ dependencies { // implementation "org.jetbrains.kotlin:kotlin-reflect" implementation "com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.0" - testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.0' + testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/codeGenIntermediate/build.gradle b/codeGenIntermediate/build.gradle index 1b3a2b562..4d3c3dca4 100644 --- a/codeGenIntermediate/build.gradle +++ b/codeGenIntermediate/build.gradle @@ -29,7 +29,7 @@ dependencies { // implementation "org.jetbrains.kotlin:kotlin-reflect" implementation "com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.0" - testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.0' + testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' diff --git a/compiler/build.gradle b/compiler/build.gradle index df0b50b0a..aee0be0af 100644 --- a/compiler/build.gradle +++ b/compiler/build.gradle @@ -39,7 +39,7 @@ dependencies { testImplementation project(':codeCore') testImplementation project(':intermediate') - testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.0' + testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' diff --git a/compiler/test/TestCompilerOnExamples.kt b/compiler/test/TestCompilerOnExamples.kt index 5af3df768..6b549285f 100644 --- a/compiler/test/TestCompilerOnExamples.kt +++ b/compiler/test/TestCompilerOnExamples.kt @@ -171,6 +171,7 @@ class TestCompilerOnExamplesBothC64andCx16: FunSpec({ "mandelbrot-gfx", "numbergame", "primes", + "queens", "screencodes", "sincos", "sorting", diff --git a/docs/source/todo.rst b/docs/source/todo.rst index ba8a9f4bc..b431240d5 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,8 +1,26 @@ TODO ==== +virtual: txt.cls() gives compile error undefined symbol clear_screen + https://github.com/irmen/prog8/issues/136 (string.find register order issue) +optimization: for 65c02 sometimes clc adc #1 is generated, this can be optimized into inc a (always? or not? mind the carry flag!) + +IR: sys.push() and sys.pop() etc should be translated into PUSH/POP instructions instead of subroutine calls + +if-optimization: + if row == NUMQUEENS { + print_solution() + return + } + compiles into this, where the bne+jmp could be a single beq instead: + cmp #8 + bne label_asm_21_afterif + jmp p8b_main.p8s_print_solution +label_asm_21_afterif: + + optimize signed byte/word division by powers of 2 (and shift right?), it's now using divmod routine. (also % ?) see inplacemodificationByteVariableWithLiteralval() and inplacemodificationSomeWordWithLiteralval() and for IR: see divideByConst() in IRCodeGen diff --git a/examples/queens.p8 b/examples/queens.p8 new file mode 100644 index 000000000..6edcb796a --- /dev/null +++ b/examples/queens.p8 @@ -0,0 +1,70 @@ +%import textio +%zeropage basicsafe + +; Recursive N-Queens solver. +; The problem is: find all possible ways to place 8 Queen chess pieces on a chess board, so that none of them attacks any other. +; (this program prints all solutions without taking mirroring and flipping the chess board into account) +; Note: this program can be compiled for multiple target systems. + +main { + const ubyte NUMQUEENS=8 + ubyte[NUMQUEENS] board + + sub could_place(byte row, byte col) -> bool { + byte i + for i in 0 to row-1 { + if board[i]==col or board[i]-i==col-row or board[i]+i==col+row + return false + } + return true + } + + ubyte solution_count + sub print_solution() { + solution_count++ + txt.home() + txt.print("found solution ") + txt.print_ub(solution_count) + txt.nl() + ubyte i + for i in 0 to NUMQUEENS-1 { + ubyte col = board[i] + txt.chrout(' ') + repeat col txt.chrout('.') + txt.chrout('q') + repeat NUMQUEENS-col-1 txt.chrout('.') + txt.nl() + } + } + + sub place_queen(ubyte row) { + if row == NUMQUEENS { + print_solution() + return + } + ubyte col + for col in 0 to NUMQUEENS-1 { + if could_place(row as byte, col as byte) { + board[row] = col + ; we need to save the local variables row and col. + sys.push(row) + sys.push(col) + place_queen(row + 1) + ; restore the local variables after the recursive call. + col = sys.pop() + row = sys.pop() + board[row] = 0 + } + } + } + + sub start() { + cbm.SETTIM(0,0,0) + txt.clear_screen() + place_queen(0) + txt.nl() + uword duration=100*cbm.RDTIM16()/6 + txt.print_uw(duration) + txt.print(" milliseconds\n") + } +} diff --git a/intermediate/build.gradle b/intermediate/build.gradle index a868a7118..24e42048f 100644 --- a/intermediate/build.gradle +++ b/intermediate/build.gradle @@ -25,7 +25,7 @@ compileTestKotlin { dependencies { implementation project(':codeCore') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" - testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.0' + testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/virtualmachine/build.gradle b/virtualmachine/build.gradle index fbe142e3c..41c1528fc 100644 --- a/virtualmachine/build.gradle +++ b/virtualmachine/build.gradle @@ -27,7 +27,7 @@ dependencies { implementation project(':intermediate') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" implementation "com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.0" - testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.0' + testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' }