diff options
author | Einhard Leichtfuß <alguien@respiranto.de> | 2025-05-21 13:35:39 +0200 |
---|---|---|
committer | Einhard Leichtfuß <alguien@respiranto.de> | 2025-05-21 13:35:39 +0200 |
commit | bd81d7ec6c8373c1cbab0eedeb23860a4b474a79 (patch) | |
tree | e3a2abee84ec5a15f8c5e70be97256b96737a644 | |
parent | bc620adb5a07d791c67c42abd29d30e5c1a5c4d2 (diff) |
Add example code
-rw-r--r-- | test/example000.sish | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/test/example000.sish b/test/example000.sish new file mode 100644 index 0000000..f898cde --- /dev/null +++ b/test/example000.sish @@ -0,0 +1,94 @@ +// Example code. +// - NOTE: The syntax is very likely to change, and this may be not +// up-to-date. + + +function square(int n) = ($n * $n) + + +function f(int x, int y, str s, bool b) = + ( $x + 4 * ($y + 5) == $y - $x + 4 + || $b + && (int $s == $x || $s == str $x) + ) + + +// Recursion. +function f(int n) = (n + f n); + + +procedure int p(str x, int n, bool b) +{ + int ret; + + // Pass $x into `sed`, store result in $s. + s <= @sed "-E" "s/x/y/g" <= $x; + + // Pass $x into grep, get return code. + ret <- @grep "-qE" "x" <= $s; + + if ($ret == 0 && b) + { + return 0 /* return/exit code */, int $x /* return value */; + } + else + { + return 1, 0; + } +} + + +procedure int q() +{ + str s; + read_line $s; + print $s; + print_err $s; + return 0, 42; +} + + +procedure int r() +{ + int ret; + str stdout, stderr; + int value; + + // Evaluate an expression. + value = square 7 - 5; + + // Get all data from a procedure call. + (ret, value, stdout, stderr) <- p "x" 42 (3 == 4) <= "stdin"; + + // Minimal procedure call. + // - stdout, stderr, stdin are passed through. + // - Non-zero return code causes this procedure to return with that return + // code. + q; + + // Get some data from a procedure call. + // - `:` means pass through, `_` means ignore. + (ret, value) <- q; + (ret, _, :, stderr) <- q; + (value, stdout, stderr) <= q; + value <= q; + (value, stdout) <= q; + (_, stdout) <= q; + + return 0, $value; +} + + +procedure main() +{ + while true + { + @date "-u"; + r; + } + + return 127; // no return value, only return code +} + + +// vi: ts=2 sw=0 et |