-
Notifications
You must be signed in to change notification settings - Fork 19
/
f2md.fth
85 lines (76 loc) · 2.15 KB
/
f2md.fth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
\ This is a highly specialized Forth program that creates a turn-key
\ application, which when run, acts as a filter reading from standard input
\ and writing to standard output. It expects as its input a specially formatted
\ document, which is actually a well documented Forth program, which it then
\ turns into a markdown document.
\
\ Compilation:
\
\ ./embed -o f2md.blk f2md.fth
\
\ Usage:
\ ./embed -i f2md.blk < embed.fth > embed.md
\
.( Compiling f2md.fth ) cr
only forth definitions
system +order
decimal
variable line
variable start-line
variable code-block
variable verbatim
10 constant nl
9 constant tab
create queue 4 c, 0 c, 0 c, 0 c, 0 c,
create nbye 4 c, nl c, char b c, char y c, char e , ( "\nbye" )
: .pipe [char] | emit ;
: .tab tab emit ;
: .line .tab line @ 5 u.r .pipe space ;
: start 1 line ! 1 start-line ! 0 code-block ! ;
: =line 1 start-line ! ;
: <>line 0 start-line ! ; \ 0 code-block ! ;
: encode 1 code-block ! ;
: code? code-block @ 0= 0= ;
: +line line 1+! ;
: nl? dup nl = ;
: tab? dup tab = ;
: .nl nl emit ;
: .nl? if .nl then ;
: slash? dup [char] \ = ;
: start? start-line @ 0= 0= ; ( -- start-line )
: aswap 2dup c@ >r c@ swap c! r> swap c! ; ( b b -- )
: enqueue queue count 1- for aft dup dup 1+ aswap 1+ then next c! ;
\ : .queue queue count .tab .pipe type .pipe cr ;
: end? queue count nbye count compare 0= ;
: verbatim! dup verbatim @ <> if verbatim ! .nl else drop then ;
: escape ( c -- )
code? if emit exit then
dup [char] _ = if [char] \ emit emit exit then
\ dup [char] < = if drop ." <" exit then
\ dup [char] > = if drop ." >" exit then
emit ;
: filter ( c -- )
nl? if emit =line exit then
start? if
slash? if
drop key
tab? if 1 verbatim! encode exit else 0 verbatim! then
nl? if emit =line exit else drop then
<>line exit
else
1 verbatim!
tab? if encode else <>line then
.line emit +line exit
then
then
emit ;
: converter begin key dup enqueue end? if emit exit then filter again ;
: end .nl begin key emit again ;
: f2md start converter end bye ;
' f2md <boot> !
.( Compilation done ) cr
only forth definitions
.( Saving... ) cr
save
.( Application Saved ) cr
bye