retroforth/example/retro-unu.forth
crc 3a81020c6c add commentary to unu, muri
FossilOrigin-Name: 46b16bf4094758e6982fa8462182462ef47287bb9ae47104db31d1a26aaa3cea
2018-11-19 21:08:11 +00:00

50 lines
1.3 KiB
Forth
Executable file

#!/usr/bin/env retro
# Unu
unu
(verb) (-hia) pull out, withdraw, draw out, extract.
Unu is a tool for extracting fenced code blocks from Markdown
documents.
I always found documenting my projects annoying. Eventually I
decided to start mixing the code and commentary using Markdown.
Unu is the tool I use to extract the sources from the original
files. I've found that this makes it easier for me to keep the
commentary up to date, and has lead to better commented code.
## The Code
~~~
{{
'Fenced var
:toggle-fence @Fenced not !Fenced ;
:fenced? (-f) @Fenced ;
:handle-line (s-)
fenced? [ over call ] [ drop ] choose ;
---reveal---
:unu (sq-)
swap [ dup '~~~ s:eq?
[ drop toggle-fence ]
[ handle-line ] choose
] file:for-each-line drop ;
}}
#0 sys:argv [ s:put nl ] unu
~~~
## Commentary
The basic process for this is simple:
- Read a line from a file
- If the line is a fence (~~~) boundary, toggle the fence state
- If not a fence boundary and the fence state is true process the
line
- Repeat until done
The C implementation displays the lines to *stdout*. For this I
decided that the `unu` word should be a combinator. This makes
it easy to use as the basis for other tools. (See
*example/retro-muri.forth* as a demonstration of this)