summaryrefslogtreecommitdiff
path: root/chapter22/docs
diff options
context:
space:
mode:
Diffstat (limited to 'chapter22/docs')
-rw-r--r--chapter22/docs/examples/BubbleSort.smp31
1 files changed, 26 insertions, 5 deletions
diff --git a/chapter22/docs/examples/BubbleSort.smp b/chapter22/docs/examples/BubbleSort.smp
index af110159..025bb5a2 100644
--- a/chapter22/docs/examples/BubbleSort.smp
+++ b/chapter22/docs/examples/BubbleSort.smp
@@ -1,25 +1,46 @@
// -*- mode: java; -*-
-
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] <= 32 )
+ s.x++;
+};
+
+// Require specific syntax or usage
+val require = { Scan s, u8 c ->
+ skip(s);
+ if( s.buf[s.x++] != c )
+ usage();
+};
+
+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;
+};
+
// 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(); };
- var junk = new u8[1];
while( s.x < s.buf# ) {
- sys.io.p("scan ");
- junk[0] = s.buf[s.x++];
- sys.io.p(junk);
+ require(s,'[');
+ int n = number(s);
+ sys.libc._exit(n);
};
return 0;
};