about summary refs log tree commit diff
path: root/examples
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2025-04-08 14:58:46 +0300
committerNakidai <nakidai@disroot.org>2025-04-08 14:58:46 +0300
commit48aca48d1f2cbe0d4f7ed1036409b7c64dc31d75 (patch)
tree987928165a9c2c2ed34c3ec40f65ca32d628c330 /examples
parent9a246f38d326e98e69b9a6946d649d935bf056e9 (diff)
download3cl-48aca48d1f2cbe0d4f7ed1036409b7c64dc31d75.tar.gz
3cl-48aca48d1f2cbe0d4f7ed1036409b7c64dc31d75.zip
Add cnt.ccl example
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile1
-rw-r--r--examples/cnt.ccl30
-rw-r--r--examples/in/cnt0
-rw-r--r--examples/out/cnt13
4 files changed, 44 insertions, 0 deletions
diff --git a/examples/Makefile b/examples/Makefile
index 8d943eb..6bb825f 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -1,3 +1,4 @@
+TESTS += cnt
 TESTS += fac
 TESTS += fib
 TESTS += gcd
diff --git a/examples/cnt.ccl b/examples/cnt.ccl
new file mode 100644
index 0000000..f700aad
--- /dev/null
+++ b/examples/cnt.ccl
@@ -0,0 +1,30 @@
+^++=t/wo
+
+c/ount elements on the stack but more complicated / end [... elem] start -- end [... elem] count
+{
+    + / increment counter
+    %t / get last elem
+    E? / if it's end then stop counting
+        %t / reverse back
+        #
+    ;
+
+    &e=e / pop elem saving
+    @c   / end [...] start+1
+    $e%t / reverse back
+}
+
+C/ount elements on the stack / [elem ...] -- [elem ...] elem-count
+{
+    $E / push end on the stack
+    %_ / reverse the stack
+    ^- @c / count elements
+    &c=c / save count
+    %_ / reverse back
+    =_ / get rid of end
+    $c / push count back
+}
+
+^-=E / end of count
+^^^^^
+@C
diff --git a/examples/in/cnt b/examples/in/cnt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/examples/in/cnt
diff --git a/examples/out/cnt b/examples/out/cnt
new file mode 100644
index 0000000..365eac7
--- /dev/null
+++ b/examples/out/cnt
@@ -0,0 +1,13 @@
+Globals:
+  t=2
+  E=-1
+Functions:
+  c, 8 nodes
+  C, 10 nodes
+Stack:
+  0
+  0
+  0
+  0
+  0
+  5