summaryrefslogtreecommitdiff
path: root/docs/chapter22/examples
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2025-04-21 14:18:23 -0700
committerCliff Click <cliffc@acm.org>2026-10-08 16:52:27 -0700
commit2475516bee0ca64ffb1bff184e671db9b69bd617 (patch)
treea1afa6182faed5cf5c9a2dd22f00fa5e18181357 /docs/chapter22/examples
parent53308013c3f2bad224208805df0f54416067b8be (diff)
downloadSimple-2475516bee0ca64ffb1bff184e671db9b69bd617.tar.gz
Simple-2475516bee0ca64ffb1bff184e671db9b69bd617.zip
Chapter 22: A Hello, World!
Co-authored-by: godalming123 <68993177+godalming123@users.noreply.github.com> Co-authored-by: Tamás Hadházy <85063808+Hels15@users.noreply.github.com> Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: Symious <14933944+symious@users.noreply.github.com> Co-authored-by: Jun Kawasaki <jun784@gmail.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'docs/chapter22/examples')
-rw-r--r--docs/chapter22/examples/A_helloWorld.smp3
-rw-r--r--docs/chapter22/examples/BubbleSort.smp109
2 files changed, 112 insertions, 0 deletions
diff --git a/docs/chapter22/examples/A_helloWorld.smp b/docs/chapter22/examples/A_helloWorld.smp
new file mode 100644
index 00000000..07ff935f
--- /dev/null
+++ b/docs/chapter22/examples/A_helloWorld.smp
@@ -0,0 +1,3 @@
+// -*- mode: java; -*-
+sys.io.p("Hello, World!");
+return 0;
diff --git a/docs/chapter22/examples/BubbleSort.smp b/docs/chapter22/examples/BubbleSort.smp
new file mode 100644
index 00000000..a491bd82
--- /dev/null
+++ b/docs/chapter22/examples/BubbleSort.smp
@@ -0,0 +1,109 @@
+// -*- 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);
+};
+
+val usage = { -> _exit("stdin must look like [4, 3, 2, 1]"); };
+
+// Scanner
+struct Scan {
+ int !x;
+ u8[~] buf;
+};
+
+// Skip whitespace
+val skip = { !Scan s ->
+ while( s.buf[s.x] <= ' ' )
+ s.x++;
+};
+
+
+// Require specific syntax or usage
+val require = { !Scan !s, u8 c ->
+ skip(s);
+ if( s.buf[s.x++] != 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;
+ while( '0' <= s.buf[s.x] & s.buf[s.x] <= '9' )
+ n = n*10 + (s.buf[s.x++] - '0');
+ 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 = { ->
+ !Scan !s = new Scan{ buf = sys.io.stdin(); };
+ require(s,'[');
+ // Array of parsed results
+ int[] !ns = new int[16];
+ 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[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<len; i++ ) {
+ int x = ns[i];
+ if( !x ) { sys.io.p("0, "); continue; }
+
+ int ten = 1, log = 0;
+ while( ten <= x ) { ten *= 10; log++; }
+ ten /= 10;
+ for( int i=0; i<log; i++ ) {
+ int q = x/ten;
+ int r = x - q*ten;
+ junk[0] = q+'0';
+ sys.io.p(junk);
+ x = r;
+ ten /= 10;
+ }
+ sys.io.p(", ");
+ }
+ sys.io.p("]");
+ return 0;
+};