retroforth/example/retro-muri.forth
crc 42432e48c1 add muri in retro to examples
FossilOrigin-Name: 01c9b1102bf783fbf264c84cbd56793342bfa38238d7a6cb1d6cf9bf5c5bbb2c
2018-11-19 19:23:33 +00:00

81 lines
1.5 KiB
Forth
Executable file

#!/usr/bin/env retro
~~~
{{
'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 ;
}}
~~~
~~~
'Image d:create #4096 allot
'AP var
:I, (n-) &Image @AP + store &AP v:inc ;
~~~
## Pass 1
~~~
'Pass_1:_ s:put
#0 !AP
#0 sys:argv
[ dup s:length n:zero? [ drop #0 ] if 0;
fetch-next &n:inc dip
$i [ i here n:dec fetch I, ] case
$d [ s:to-number I, ] case
$r [ drop #-1 I, ] case
$: [ @AP swap 'muri! s:prepend const ] case
$s [ &I, s:for-each #0 I, ] case
'ERROR s:put nl
] unu
@AP n:put '_cells s:put nl
~~~
## Pass 2
~~~
'Pass_2:_ s:put
#0 !AP
#0 sys:argv
[ dup s:length n:zero? [ drop #0 ] if 0;
fetch-next &n:inc dip
$i [ drop &AP v:inc ] case
$d [ drop &AP v:inc ] case
$r [ 'muri! s:prepend d:lookup d:xt fetch I, ] case
$: [ drop ] case
$s [ s:length n:inc &AP v:inc-by ] case
'ERROR s:put nl
] unu
@AP n:put '_cells s:put nl
~~~
## Save Image
~~~
'FID var
:write-byte (n-) @FID file:write ;
:mask (n-) #255 and ;
:write-cell (n-)
dup mask write-byte
#8 shift dup mask write-byte
#8 shift dup mask write-byte
#8 shift mask write-byte ;
:save-image (s-)
file:W file:open !FID
&Image @AP [ fetch-next write-cell ] times drop
@FID file:close ;
'ngaImage save-image
~~~