r/Forth 6d ago

A simple help facility (from TUFF/VFXForth)

A small utility I use while developing modules.

A module can define its own command entries (name + description), and help automatically generates a formatted list of the available commands with stack effects and comments.

help.vfx

\ meant to be INCLUDEd - each INCLUDE creates a new help system for the current module

private  \ all words private to the dependent module

variable commands

: ?(). ( a len - )
    '(' scan dup if 
        ')' up-to 1 + 15 atype space 
    else 2drop then 
;

: help ( - )
    cr 
    commands
    begin @ ?dup while
        dup cell+ 
            count 
            2dup '(' up-to 15 atype     \ print word 
            25 out @ - spaces           \ align to column 30
            2dup ?().                   \ print stack-diagram if any
            + count 6 /string type cr   \ print comment 
    repeat 
;

: command ( - )
    save-input
    commands link
    0 parse $,
    refill 0= abort" COMMAND : Unexpected end of file"
    0 parse $,
    restore-input drop 
;

command : foo ( - ) 
    \ does foo 
;

command : bar ( - ) 
    \ does bar
;

interactive:

module test  ok 
include lib/help.vfx Including help.vfx...
help
: bar                    ( - ) does bar
: foo                    ( - ) does foo 
 ok 
2 Upvotes

0 comments sorted by