retroforth/example/retro-unu.retro
crc 660e52bdcb rename words in sys: to script: (old names still work in this release, but are deprecated)
FossilOrigin-Name: 1a43743f43076eb087ee4dd7fbfa96b8dfda2aa4ddcff41e6a5a9634ba6e239c
2020-09-14 19:55:02 +00:00

50 lines
1.3 KiB
Text
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 script:get-argument [ 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)