diff options
| author | Cliff Click <cliffc@acm.org> | 2025-05-08 17:04:02 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2025-05-08 17:04:02 -0700 |
| commit | ef35099ea5e28dd232a5aa166aa7b2be5e4abee4 (patch) | |
| tree | 16e3d2900d88fb1f1647b618f1dc771ed1544bd1 /chapter22/docs/examples | |
| parent | 6851144b727ac7e98600e2a4d70d5476cf74484e (diff) | |
| download | Simple-ef35099ea5e28dd232a5aa166aa7b2be5e4abee4.tar.gz Simple-ef35099ea5e28dd232a5aa166aa7b2be5e4abee4.zip | |
Progress towards a 1st "real" program
Parser a 'char'
MemAddX86 kills flags, uses `inc` form for +1
GCM can skip anti-deps for missing aliases
Diffstat (limited to 'chapter22/docs/examples')
| -rw-r--r-- | chapter22/docs/examples/BubbleSort.smp | 31 |
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; }; |
