From fafc9664801412e900b35ba7091a9e218f909788 Mon Sep 17 00:00:00 2001 From: Luxferre Date: Sun, 5 Jul 2026 18:39:20 +0300 Subject: [PATCH] added more examples --- README.md | 8 ++++++++ examples/fizzbuzz.clx | 10 ++++++++++ examples/hello.clx | 2 +- examples/tcpecho.clx | 16 ++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) mode change 100644 => 100755 examples/fizzbuzz.clx create mode 100755 examples/tcpecho.clx diff --git a/README.md b/README.md index 1fa8c85..2d8917a 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,14 @@ When the packed executable runs: - If a `_main` word is defined in the environment, it is executed automatically. - If `_main` is not present, the interpreter falls back to running the standard REPL. +## Examples + +The [examples](examples/) directory contains several simple examples of Clyx programming, namely: + +- a [Hello World](examples/hello.clx) program +- a [FizzBuzz](examples/fizzbuzz.clx) program +- a [TCP echo server](examples/tcpecho.clx) + ## FAQ ### Where does the name Clyx originate from? diff --git a/examples/fizzbuzz.clx b/examples/fizzbuzz.clx old mode 100644 new mode 100755 index e69de29..5f344a7 --- a/examples/fizzbuzz.clx +++ b/examples/fizzbuzz.clx @@ -0,0 +1,10 @@ +#!/usr/bin/env clyx +:: _main [ + for [ 1 101 range ] [ + 1 # tracking flag + swap # the index is on top + dup 3 mod 0= if [ "Fizz" puts swap drop 0 swap ] [ ] + dup 5 mod 0= if [ "Buzz" puts swap drop 0 swap ] [ ] + swap if [ . ] [ drop cr ] + ] +] diff --git a/examples/hello.clx b/examples/hello.clx index a258a39..bc38b37 100755 --- a/examples/hello.clx +++ b/examples/hello.clx @@ -1,4 +1,4 @@ #!/usr/bin/env clyx :: _main [ - "Hello, world!" puts cr + "Hello from Clyx!" puts cr ] diff --git a/examples/tcpecho.clx b/examples/tcpecho.clx new file mode 100755 index 0000000..73c228d --- /dev/null +++ b/examples/tcpecho.clx @@ -0,0 +1,16 @@ +#!/usr/bin/env clyx +:: _main [ + "Listening on port 1337..." puts cr + 1337 nlstn # listen on port 1337 + 1 while [ # outer connection accept loop + dup read # accept a new connection -> conn_fd + 1 # loop condition for client session loop + while [ + dup read # read data + dup vlen 0 != # check if data is non-empty + if [ over swap write drop 1 ] [ drop 0 ] + ] + close # close the connection socket + 1 # loop condition for outer server loop + ] +]