about summary refs log tree commit diff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile5
-rw-r--r--examples/fac.ccl33
-rw-r--r--examples/in/fac1
-rw-r--r--examples/in/num1
-rw-r--r--examples/num.ccl39
-rw-r--r--examples/out/fac12
-rw-r--r--examples/out/num12
7 files changed, 102 insertions, 1 deletions
diff --git a/examples/Makefile b/examples/Makefile
index faf2f75..db76c98 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -1,9 +1,12 @@
+TESTS += fac
 TESTS += fib
 TESTS += gcd
+TESTS += num
 
 all: ${TESTS}
 
 .PHONY: ${TESTS}
 ${TESTS}:
-	@echo "Test: $@"
+	@printf "Testing %s..." $@
+	@echo " OK"
 	@test "`../3cl -d $@.ccl < in/$@`" = "`cat out/$@`"
diff --git a/examples/fac.ccl b/examples/fac.ccl
new file mode 100644
index 0000000..2d53d79
--- /dev/null
+++ b/examples/fac.ccl
@@ -0,0 +1,33 @@
+^=z / z = 0
+F/actorial / n -- nth-factorial
+{
+    z?+#; / n == 0 ? return 1
+
+    =a$a$a / dup n
+    - @F=a / a = F(n - 1)
+
+    =b / b = n
+
+    / push a * b
+    b[$a]
+    $b-=b
+    b[*]
+}
+
+I/terative factorial / n -- nth-fctorial
+{
+    z?+#;   / n == 0 ? return 1
+
+    =c$c    / c = n - 1
+    $c-=c   / --c
+
+    c (                     / while c > 0
+        $c                  /   push c
+        =a=ba[$b]$a-=aa[*]  /   multiply 2 toppest nums
+        $c-=c               /   --c
+    )
+}
+
+^++++++
+^++++++ @F
+^++++++ @I
diff --git a/examples/in/fac b/examples/in/fac
new file mode 100644
index 0000000..d698f74
--- /dev/null
+++ b/examples/in/fac
@@ -0,0 +1 @@
+lol this file doesnt have any read calls so i can write here anythin i want
diff --git a/examples/in/num b/examples/in/num
new file mode 100644
index 0000000..0aac502
--- /dev/null
+++ b/examples/in/num
@@ -0,0 +1 @@
+22869
diff --git a/examples/num.ccl b/examples/num.ccl
new file mode 100644
index 0000000..c35d62f
--- /dev/null
+++ b/examples/num.ccl
@@ -0,0 +1,39 @@
+^+++++ +++++ = t/en
+$t- = n/ine
+^- = E/OF
+^ = e/rror state
+^+++++++++++++++++++++++++++++++++++++++++++++++ = z/ero character - 1
+$z+++++ +++++ + = T/ '0' - 1 + 11
+^ = Z / actual zero
+
+P/arse a number from stdin / -- input
+{
+    &r / our result :D
+
+    _ (
+        &c>c / read into local
+        $cE?=_^+=e#; / if EOF then exit setting error
+
+        &s$z~=s/ubtract / put char - ('0' - 1) into s
+
+        &f^=f s( / if s >= 0 then our char is '0' or above
+                  / meaning it's possibly a digit
+
+            ^$c$T~~=s/ubtract / put -(char - ('0' - 1 + 11)) into s
+            &f^=f s( / if s >= 0 then our char is '9' or below
+                      / meaning it's a digit
+
+                / c is a digit here
+                $c$z~- / convert character into number
+                t[$r]n[*] / shift result one decimal digit left
+                *=r / finally append character
+
+            ^+=f#)
+            $fZ?=_#;=_
+        ^+=f#)
+        $fZ?=_^+=e#;=_ / otherwise exit setting error
+    )
+    $r
+}
+
+@P
diff --git a/examples/out/fac b/examples/out/fac
new file mode 100644
index 0000000..3de0ab5
--- /dev/null
+++ b/examples/out/fac
@@ -0,0 +1,12 @@
+Globals:
+  a=0
+  b=720
+  c=0
+  z=0
+Functions:
+  F, 13 nodes
+  I, 7 nodes
+Stack:
+  6
+  720
+  720
diff --git a/examples/out/num b/examples/out/num
new file mode 100644
index 0000000..f268f3b
--- /dev/null
+++ b/examples/out/num
@@ -0,0 +1,12 @@
+Globals:
+  e=1
+  n=9
+  t=10
+  z=47
+  E=-1
+  T=58
+  Z=0
+Functions:
+  P, 3 nodes
+Stack:
+  22869