summaryrefslogtreecommitdiff
path: root/docs/chapter24/examples
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2025-06-13 15:42:08 -0700
committerCliff Click <cliffc@acm.org>2026-08-10 11:11:18 -0700
commite57bdd315a7918a0d130e1c95648ca4a1b4f08ed (patch)
treeea659c39bce731b506347a80f424e3c325ff5326 /docs/chapter24/examples
parent08d33f808db30936b273582bcfaddcbc5a57c112 (diff)
downloadSimple-e57bdd315a7918a0d130e1c95648ca4a1b4f08ed.tar.gz
Simple-e57bdd315a7918a0d130e1c95648ca4a1b4f08ed.zip
Chapter 24: Chaining Relationals and Sparse Conditional Constant Propagation
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: Hadházy Tamás <helsgithub15@gmail.com>
Diffstat (limited to 'docs/chapter24/examples')
-rw-r--r--docs/chapter24/examples/A_helloWorld.smp3
-rw-r--r--docs/chapter24/examples/BubbleSort.smp37
2 files changed, 40 insertions, 0 deletions
diff --git a/docs/chapter24/examples/A_helloWorld.smp b/docs/chapter24/examples/A_helloWorld.smp
new file mode 100644
index 00000000..07ff935f
--- /dev/null
+++ b/docs/chapter24/examples/A_helloWorld.smp
@@ -0,0 +1,3 @@
+// -*- mode: java; -*-
+sys.io.p("Hello, World!");
+return 0;
diff --git a/docs/chapter24/examples/BubbleSort.smp b/docs/chapter24/examples/BubbleSort.smp
new file mode 100644
index 00000000..4352c47a
--- /dev/null
+++ b/docs/chapter24/examples/BubbleSort.smp
@@ -0,0 +1,37 @@
+// -*- mode: java; -*-
+
+// An implementation of
+// https://sampleprograms.io/projects/bubble-sort/
+//
+// Bubble sort, with reading (parsing) and writing (formatting) the data.
+
+val sort = { sys.aryi64 nums ->
+ var done = false;
+ while( !done ) {
+ done = true;
+ for( var i = 0; i < nums.len-1; i++ )
+ if( nums.buf[i] > nums.buf[i+1] ) {
+ int tmp = nums.buf[i];
+ nums.buf[i] = nums.buf[i+1];
+ nums.buf[i+1] = tmp; // Swap
+ done = false;
+ }
+ }
+};
+
+// Read from stdin a line of the form "[ 4, 3, 2, 1]" and print it sorted.
+val main = { ->
+ // Parse array of numbers from stdin
+ var nums = sys.parseAryI64( sys.io.stdin() );
+ if( !nums ) {
+ sys.io.p("stdin must look like [4, 3, 2, 1]");
+ return 1;
+ }
+
+ // Bubble sort array of integers
+ sort(nums);
+
+ // Print a list of numbers to stdout
+ nums.str().write(1);
+ return 0;
+};