• executing Forth programs (was: VALUE ...)

    From Anton Ertl@21:1/5 to albert@spenarnc.xs4all.nl on Thu Mar 21 07:52:09 2024
    albert@spenarnc.xs4all.nl writes:
    I typically restart the application by restarting the Forth system and >>reloading the application source.

    With ample memory it is not worthwhile to restart the Forth system.

    As if that was costly.

    With ciforth making executables is easy.

    Easy is still harder than doing nothing.

    With a Forth system and an OS (like Unix) that have good support for
    executing scripts, the Forth source code can just serve as script; if
    you want to call the script without mentioning the Forth system, you
    can put the Forth system in the hash-bang line in Unix, e.g.

    #! /usr/bin/gforth

    or

    #! /usr/bin/env gforth

    as first line (the latter is beneficial if you don't know whether
    gforth is in /usr/bin or /usr/local/bin on the systems where the
    script is invoked). Ok, you still have to do that and make the file executable, but you don't have to repeat that every time you change
    the program source, while you have to rebuild the binary executable
    every time, however easy that may be.

    This alternative allows to
    pass parameters to the executable.

    In Gforth you can pass parameters in two ways:

    My preferred way is to pass them before loading the program, e.g., with

    gforth -e "100 constant somesize" program.fs

    The advantage of this method is that there is no need for special argument-accessing code in program.fs; however, you may want to
    provide a default for SOMESIZE with

    [undefined] somesize [if] 50 constant somesize [then]

    The disadvantage of this method is that the invocation of the program
    does not look like that of a classic Unix executable. So if you want
    to invoke program without explicitly mentioning gforth, you need to
    use NEXT-ARG (or some of the other non-standard words described in <https://gforth.org/manual/OS-command-line-arguments.html#index-next_002darg>).

    Here's an example:

    [/tmp:147807] cat <<EOF >example
    #! /usr/bin/env gforth

    : echo ( -- )
    begin
    next-arg 2dup 0 0 d<> while
    type space
    repeat
    2drop ;

    echo cr bye
    EOF
    [/tmp:147808] chmod +x example
    [/tmp:147809] ./example a b c d
    a b c d

    If I edit example, I don't have to repeat the chmod.

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2023: https://euro.theforth.net/2023

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From albert@spenarnc.xs4all.nl@21:1/5 to Anton Ertl on Thu Mar 21 11:30:46 2024
    In article <2024Mar21.085209@mips.complang.tuwien.ac.at>,
    Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
    albert@spenarnc.xs4all.nl writes:
    I typically restart the application by restarting the Forth system and >>>reloading the application source.

    With ample memory it is not worthwhile to restart the Forth system.

    As if that was costly.
    Merely inconvenient.


    With ciforth making executables is easy.

    Easy is still harder than doing nothing.
    It is so easy that it pays off not loading the source several times,
    even with command repeaters like rlwrap.
    The situation is that you want to run programs with different parameters,
    e.g. projecteuler.net with increasing sizes, the last one several hours. Reloading the source is just a waste of time, but more importantly
    a distraction.
    $EDITOR aap.frt
    ..
    make aap

    With a line in the Makefile
    %: %.frt ; $(FORTH) -c $<


    With a Forth system and an OS (like Unix) that have good support for >executing scripts, the Forth source code can just serve as script; if
    you want to call the script without mentioning the Forth system, you
    can put the Forth system in the hash-bang line in Unix, e.g.

    #! /usr/bin/gforth

    or

    #! /usr/bin/env gforth

    as first line (the latter is beneficial if you don't know whether
    gforth is in /usr/bin or /usr/local/bin on the systems where the
    script is invoked). Ok, you still have to do that and make the file >executable, but you don't have to repeat that every time you change
    the program source, while you have to rebuild the binary executable
    every time, however easy that may be.

    <SNIP>
    Here's an example:

    [/tmp:147807] cat <<EOF >example
    #! /usr/bin/env gforth

    : echo ( -- )
    begin
    next-arg 2dup 0 0 d<> while
    type space
    repeat
    2drop ;

    echo cr bye
    EOF
    [/tmp:147808] chmod +x example
    [/tmp:147809] ./example a b c d
    a b c d

    If I edit example, I don't have to repeat the chmod.

    Here is the take on this with ciforth
    ----------------------------
    #!/usr/bin/lina -s

    BEGIN ARGC 1 > WHILE 1 ARG[] TYPE SPACE SHIFT-ARGS REPEAT
    CR
    ----------------------------
    ~/PROJECT/ciforths/ciforth: script a b c d
    a b c d

    - You specify with -s that you run a script
    - No space after #! , that is a pitfall.
    - in scripting you want to run interpretive control structures
    more than normal
    - Of course you must chmod.

    Options are convenient, because Forth has many faces,
    interactive trying, scripting, compilation, debugging, developing,
    ease of use at the cost of occasional failure.
    none : bald interpreter
    -a : library attached
    -c : compile
    -s : script
    -n : newbie, autoload from library
    -e : electives, development tools
    Read once through the applicable chapter and you at least
    remember how to do things. Option -s is not hard to
    associate with scripts.

    - anton

    Groetjes Albert
    --
    Don't praise the day before the evening. One swallow doesn't make spring.
    You must not say "hey" before you have crossed the bridge. Don't sell the
    hide of the bear until you shot it. Better one bird in the hand than ten in
    the air. First gain is a cat purring. - the Wise from Antrim -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)