summaryrefslogtreecommitdiff
path: root/chapter22/docs
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2025-05-10 10:10:24 -0700
committerCliff Click <cliffc@acm.org>2025-05-10 10:10:24 -0700
commitb4eb623979cc9566d2a22c718b4ba8ded3bca5a4 (patch)
tree7216d86a803d74b2d0a61f69d198a03bc0172b88 /chapter22/docs
parent9112455cf661a2173edf11ef57e29935a1f4e9a8 (diff)
downloadSimple-b4eb623979cc9566d2a22c718b4ba8ded3bca5a4.tar.gz
Simple-b4eb623979cc9566d2a22c718b4ba8ded3bca5a4.zip
clean up tests
BubbleSort is complete but ugly risc5 encoding <= test EvalR5/Arm handles stdout/stderr.
Diffstat (limited to 'chapter22/docs')
-rw-r--r--chapter22/docs/examples/BubbleSort.smp30
1 files changed, 25 insertions, 5 deletions
diff --git a/chapter22/docs/examples/BubbleSort.smp b/chapter22/docs/examples/BubbleSort.smp
index f6b30752..a80d6bd8 100644
--- a/chapter22/docs/examples/BubbleSort.smp
+++ b/chapter22/docs/examples/BubbleSort.smp
@@ -1,5 +1,10 @@
// -*- mode: java; -*-
+// An implementation of
+// https://sampleprograms.io/projects/bubble-sort/
+//
+// Bubble sort, with reading (parsing) and writing (formatting) the data.
+
val _exit = { u8[~] msg ->
sys.io.p(msg);
sys.libc._exit(1);
@@ -40,6 +45,19 @@ val number = { Scan s ->
return n;
};
+val sort = { int[] ns, int len ->
+ var done = false;
+ while( !done ) {
+ done = true;
+ for( var i = 0; i < len-1; i++ )
+ if( ns[i] > ns[i+1] ) {
+ int tmp = ns[i];
+ ns[i] = ns[i+1];
+ ns[i+1] = tmp; // Swap
+ done = false;
+ }
+ }
+};
// Read from stdin a line of the form "[ 4, 3, 2, 1]" and print it sorted.
val main = { ->
@@ -47,23 +65,26 @@ val main = { ->
require(s,'[');
// Array of parsed results
int[] !ns = new int[16];
- i32 num = 0;
+ i32 len = 0;
// Parse a list of numbers
if( !peek(s,']') ) { // Handle the empty list
// Parse "123, " until a something else
while( s.x < s.buf# ) {
int n = number(s);
- ns[num++] = n;
+ ns[len++] = n;
if( !peek(s,',') )
break;
};
require(s,']'); // Close the list
}
+ // Bubble sort array of integers
+ sort(ns,len);
+
// Print a list of numbers
sys.io.p("[ ");
u8[] !junk = new u8[1];
- for( int i=0; i<num; i++ ) {
+ for( int i=0; i<len; i++ ) {
int x = ns[i];
if( !x ) { sys.io.p("0, "); continue; }
@@ -81,6 +102,5 @@ val main = { ->
sys.io.p(", ");
}
sys.io.p("]");
-
- return num;
+ return 0;
};