r/Forth • • 8d ago

Using <EOF> to break interpretation/compilation after INCLUDE a file

Hi,

Here the definition of <EOF> for eForth Windows:

\ stop compilation before end of included file

: <EOF> ( -- )

#tib @ >in ! ;

I use this word in my FORTH files—which are loaded via `INCLUDE`—for two distinct purposes:

  1. when I include text-based documentation after `<EOF>`
  2. when I am debugging a program; I use it to store experimental code or test versions of certain definitions.

When I execute `INCLUDE <file>`, the FORTH compiler loads `<file>` and stops the loading process upon encountering `<EOF>`.

5 Upvotes

11 comments sorted by

1

u/tabemann 8d ago

The equivalent in zeptoforth:

: <EOF> ( -- ) source nip >parse ! ;

wouldn't work because in zeptoforth files are parsed line by line to avoid requiring a buffer in RAM for containing the largest possible source file and also because utils/codeload3.sh uploads source files line by line.

As mentioned by u/mykesx, I recommend the use of:

false [if]
Text to ignore goes here.
[then]

to ignore blocks of text.

2

u/PETREMANN 8d ago

I am familiar with this solution. <EOF> was a FORTH word that we implemented in TURBO-Forth for MS-DOS back in 1987. At the time, it was EOF.

1

u/mykesx moderator 8d ago

I find it helpful to be able to move the if down some number of lines and then up, to isolate what word(s) are causing failure.

2

u/tabemann 8d ago

One thing I do in zeptoforth is have included by default echo the code line-by-line as it evaluates it, so if there is an error on a particular line it will show which line it stopped on.

1

u/mykesx moderator 7d ago

Inspiration prints the line, column, and the source text along with the error. In Zed IDE, I can cmd-click on the line # and it opens the file and jumps to the line..

1

u/tabemann 7d ago

zeptoforth does not store line/column information or source code for compiled code, as after all SRAM and flash are at a premium (even if the RP2040 and RP2350 have more of them than smaller STM32 MCU's).

Also, its only protection is that accidental writes to flash do not modify it, so there is the potential for a failure that, say, overwrites key parts of the kernel in SRAM would cause a crash without even a register dump and stack trace.

As a result, it is useful to have an echo of evaluated code to give the user an idea of where a failure is in their code, so if it just stops the user can at least see where it stopped.

1

u/alberthemagician 5d ago

If you are going to read the whole file anyway, it is better to use the portable

 BEGIN REFILL 0= UNTIL  

It even works with a Forth that slurps the whole file and define

: REFILL 0 ;

1

u/tabemann 5d ago

That'll hang if a file is being uploaded via the console as the console will never reach EOF under normal conditions (which is why false [if] ... [then] is better).

1

u/alberthemagician 4d ago

Valid point. That make the `` 0 [IF] '' solution preferable. In reality these tricks are used during development, so everybody chooses what is the fastest on their system.

2

u/mykesx moderator 8d ago

There’s also [defined] [undefined] and [if] [then] [else] in some forths. It allows you to stop compilation of any sections of code.

false [if] … [then]

2

u/alberthemagician 8d ago

In my simple Forth I have

 : INCLUDED GET-FILE EVALUATE ;

If you put EXIT outside of a definition, it terminates:

 " 1 2 EXIT 3 " EVALUATE .S
 1 2 OK