From 239d52984594c2011ea3093e90a3aa68faad6fbd Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Thu, 8 May 2025 21:45:44 -0700 Subject: More bubble sort test Really a ascii scanner r/w lib. Parse a string into an array of ints, and print them back out. Couple X86 encoding bugs. Inlined Fun idom same as inlined CallEnd --- chapter22/docs/examples/BubbleSort.smp | 54 ++++++++++++++++++++++++++++++---- chapter22/docs/examples/bubble.dat | 2 ++ 2 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 chapter22/docs/examples/bubble.dat (limited to 'chapter22/docs/examples') diff --git a/chapter22/docs/examples/BubbleSort.smp b/chapter22/docs/examples/BubbleSort.smp index 025bb5a2..70f45717 100644 --- a/chapter22/docs/examples/BubbleSort.smp +++ b/chapter22/docs/examples/BubbleSort.smp @@ -25,6 +25,13 @@ val require = { Scan s, u8 c -> usage(); }; +val peek = { Scan s, u8 c -> + skip(s); + if( s.buf[s.x] != c ) return false; + s.x++; + return true; +}; + val number = { Scan s -> skip(s); i32 n = 0; @@ -37,10 +44,45 @@ val number = { Scan s -> // Read from stdin a line of the form "[ 4, 3, 2, 1]" and print it sorted. val main = { -> Scan !s = new Scan{ buf = sys.io.stdin(); }; - while( s.x < s.buf# ) { - require(s,'['); - int n = number(s); - sys.libc._exit(n); - }; - return 0; + require(s,'['); + // Array of parsed results + int[] !ns = new int[16]; + i32 num = 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); + i32 q=n; + ns[num++] = n; + if( !peek(s,',') ) + break; + }; + require(s,']'); // Close the list + } + + // Print a list of numbers + sys.io.p("[ "); + u8[] !buf = new u8[20]; + for( int i=0; i0 ) + { q/=10; log++; } + i32 raw=log; + sys.libc._exit(raw); + // for( int j=0, a=x; j