about summary refs log tree commit diff
path: root/examples/num.ccl
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2025-03-31 17:22:19 +0300
committerNakidai <nakidai@disroot.org>2025-03-31 17:22:19 +0300
commit445784a629c99336d47b31ea9540bd95bbb97dc1 (patch)
tree119e91b0fb0813244a7d3968a7cc405e722a2979 /examples/num.ccl
parentd0ea12857007f9d6d48df7e87b8dcdff4dd8a75e (diff)
download3cl-445784a629c99336d47b31ea9540bd95bbb97dc1.tar.gz
3cl-445784a629c99336d47b31ea9540bd95bbb97dc1.zip
Add 2 more examples
- Number parser
- Factorial (2 ways to implement)
Diffstat (limited to 'examples/num.ccl')
-rw-r--r--examples/num.ccl39
1 files changed, 39 insertions, 0 deletions
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