diff options
| author | Nakidai <nakidai@disroot.org> | 2025-03-26 20:22:14 +0300 |
|---|---|---|
| committer | Nakidai <nakidai@disroot.org> | 2025-03-26 20:22:14 +0300 |
| commit | 6fa64679e24b2bef4aee49657df1b2f411b18392 (patch) | |
| tree | 9a0a34ec741e24dc8ee80231c71fa5baaed02f78 /examples | |
| parent | ec7a100bd2b528ebc467993fc289bdbbe97dc57e (diff) | |
| download | 3cl-6fa64679e24b2bef4aee49657df1b2f411b18392.tar.gz 3cl-6fa64679e24b2bef4aee49657df1b2f411b18392.zip | |
Add examples as tests
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/Makefile | 10 | ||||
| -rw-r--r-- | examples/fib.ccl | 20 | ||||
| -rw-r--r-- | examples/fib.ccl.test | 0 | ||||
| -rw-r--r-- | examples/gcd.ccl | 25 | ||||
| -rw-r--r-- | examples/gcd.ccl.test | 0 |
5 files changed, 55 insertions, 0 deletions
diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..35e7d74 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,10 @@ +TESTS += fib +TESTS += gcd + +all: ${TESTS} + +.PHONY: ${TESTS} +${TESTS}: + @echo "Test: $@" + @test "`../3cl -d $@.ccl`" = "`cat $@.ccl.test`" + @echo diff --git a/examples/fib.ccl b/examples/fib.ccl new file mode 100644 index 0000000..1a2ae68 --- /dev/null +++ b/examples/fib.ccl @@ -0,0 +1,20 @@ +/ Takes an index in the fibonacci sequence on the stack +/ Returns a number of the fibonacci sequence with a given index +F { + / Let us define some local variables + &a &b &c + + / If index is zero or one then we just return the number + ?c #; $c+ = c + ?c #; + + / Otherwise we get previous two numbers of the sequence + / and return their sum + = c $c $c + - @F = a + -- @F = b + $a $b* +} + +^+++++++++++++++++++++++=t$t +$t@F diff --git a/examples/fib.ccl.test b/examples/fib.ccl.test new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/examples/fib.ccl.test diff --git a/examples/gcd.ccl b/examples/gcd.ccl new file mode 100644 index 0000000..2f0a0d5 --- /dev/null +++ b/examples/gcd.ccl @@ -0,0 +1,25 @@ +/ Euclid gcd +/ Last 2 numbers on the stack are arguments +/ Result is passed through the stack too +F { + / Parse arguments into variables + &a=a + &b=b + + / If a == b then exit with it + $ab?#; + + b[-] &c=c / a - b + &f^+=f c ( / if a - b > 0 (or a > b) + $c $b + ^=f#) f ( / else + $b a[-] $a + #) + + / Call ourselves but + / with simpler case + @F +} + +/ gcd(4, 5) +^++++ ^+++++ @F diff --git a/examples/gcd.ccl.test b/examples/gcd.ccl.test new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/examples/gcd.ccl.test |