Search code examples
gforth

How save program / words in GForth


Is there a way to save my defined words to file, to continue experimenting later?

I so far found only way to copy+paste definitions from console, if they are still visible.


I am starting with forth and so I do a lot of mistakes and correct them later and I would like to save the words, I defined earlier and reuse them in next session.

Here is simple example:

Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc.
Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
:hello ."hello"; 
:1: Undefined word
>>>:hello<<< ."hello";
Backtrace:
$7F29BBAB3A00 throw 
$7F29BBAC9C98 no.extensions 
$7F29BBAB3CC0 interpreter-notfound1 
: hello ."hello"; 
:2: Undefined word
: hello >>>."hello";<<<
Backtrace:
$7F29BBAB3A00 throw 
$7F29BBAC9C20 no.extensions 
$7F29BBAB7338 compiler-notfound1 
: hello ." hello";  ok
: 2hello hello hello ;  ok
2hello  hellohello ok
: hello ." hello "; \ added space to end redefined hello   ok
2hello  hellohello ok                   
: 2hello hello hello ; redefined 2hello   ok
2hello  hello hello  ok
bye

now I have working hello (with space at the end) and working 2hello (using edited hello) - lets say, I had more troubles to fix 2hello to definete form and definition of hello is out of screen now.

  1. Is there a way, to save hello and 2hello to a file, which I would be able use next day to make more complicated words?
  2. could it be text file, so I would be able use some editor (say vim) to clean all bad definitions and comment those, which I want to keep?

I want to end with file welcome.fth:

: hello \ -- ; say hello and space at the end of word, to be able simply concatenate that 
    ." hello ";
: 2hello \ -- ; repeats hello two times
    hello hello ; 

to be able came next day and simply continue discovering

Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc.
Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
include welcome.fth  ok
2hello  hello hello  ok
\ now I can continue with learning

Solution

  • First, gforth saves its history in ~/.gforth-history file, which can be harvested by any text editor for my writing

    Second, having history accessible with up-arrow, and having editor to save files, I can climb back and save my inputs to the file.

    Does GNU FORTH have an editor?

    Just set position 0 t to first line, prepend i as insert to last line of my code to put it there, insert new line before with il as insert line and fill in all other lines in this manner back to begin.

    Then flush the buffer to disc and next time use this file use test.bl and load definitions with 0 load.

    (Or convert the block file to normal text in editor and include test.bl it)

    This I can do without leaving gforth so it works for me.

    (And eventually I can somehow improve it, because it is also written in forth :)