Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How save program / words in GForth

Tags:

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
like image 331
gilhad Avatar asked Dec 15 '25 17:12

gilhad


2 Answers

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 :)

like image 67
gilhad Avatar answered Dec 19 '25 07:12

gilhad


Here is a VERY easy way to access any EXTERNAL editor in GForth. I will explain here how it works under Linux (any version), but I guess the trick works with other Operating Systems too, it will work with Apple OSX systems.

1) Just make sure that the editor that you prefer to use with GForth is installed on your system. VIM is already installed on Linux and OSX, and under Windows 10 you can run the BASH shell natively (Google for: 'how to install BASH on Windows 10') I know that VIM is a very powerful editor, but I don't like it because it's NOT a screen-editor. A great editor is MICRO, that even allowed mouse control!! ( check: https://github.com/zyedidia/micro/wiki/Installing-Micro )

2) Here is the trick: In GForth there is a way to send a command to the Operating System: - start GForth and type in the following command: S" pwd" system and press the ENTER key... (pwd = Print Working Directory)

With S" pwd" system , you are sending the command 'pwd' from within GForth to the operating system, and that command will print the 'working directory' of your Linux operating system.

Instead of 'pwd' you could of course also use another command....

Well, you guessed it: Instead of 'pwd' you type the name of your editor ' ./micro' and presto: the MICRO editor starts up, and now you can start typing in your GForth code into the editor.

When you are ready to execute the code, just save the code into a file, like: mycode.f The file will be saved in the directory that was printed out on the screen in step 2 (above)

To run the code in GForth, type the following command: include mycode.f
(you don't have to type a PATH, your file was saved in the default diretory of GForth!)

It will be added to your WORDS vocabulary, and now you can test the code.

Hope you will find this trick useful. Enjoy writing code in Gforth with the editor of your choice !

like image 30
Robbert van Herksen Avatar answered Dec 19 '25 06:12

Robbert van Herksen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!