CamelForth

Failure to Extend
I'm maintaining some code that was written by several other Forth programmers of varying expertise. Some of it is quite elegant. Other parts, not so much. You can usually tell an new-to-Forth programmer by several signs:

  • excessive use of variables for temporary results
  • overly long routines
  • failure to factor common subexpressions into new words
  • failure to extend the language

Here's an example of the latter: I've found many occurrences of the form

variable @ 1- 0 MAX variable !

a seven-word sequence clearly intended to decrement the named variable to, but not beyond, zero.

After working with Forth for a while, most programmers would say, "This is a common operation. Perhaps it should be factored out into a distinct word."

But a more experienced Forth programmer would not think in terms of the langauge; he would think in terms of the application. Clearly "decrement-to-zero" is an operation that this application frequently requires. Why not add that operation to the language?

: DECREMENT ( adr -- ) DUP @ DUP IF 1- THEN SWAP ! ;
variable DECREMENT


Now, any self-respecting C programmer would be aghast at the idea of defining a new function to decrement a variable. That's a language function! But the point of an extensible language, like Forth, is that you can extend it to provide the functions you need, not just the functions the language designer gave you.

And that's one way you can tell a Forth programmer from a C programmer writing Forth code.


Notes:

  1. DECREMENT is an improvement over the unfactored code, in that it allows the variable to have any unsigned value, rather than requiring a positive signed value.
  2. As shown, there's a superfluous store in DECREMENT if the counter is zero. Alternative definitions are:
    : DECREMENT ( adr -- ) DUP @ ?DUP IF 1- SWAP ! ELSE DROP THEN ;
    : DECREMENT ( adr -- ) DUP @ ?DUP IF 1- OVER ! THEN DROP ;
  3. As it happens, DECREMENT was such a useful function that I made it a CODE word and added it to the kernel.

Comments
But how? Zbiggy | 13 Jan : 13:57
Comments: 11

Registered: 21 Jun : 16:46

Reply to this
As I asked "quite a while" ago on the forum: how CamelForth can be extended non-painful way? I mean: of course, a little size of RAM 8051 has is suggesting keeping it for data rather than for code (besides: to avoid loading new words each time after reset). But how can I "generate" my own binary of CamelForth, extended with my own words?
There's a need for some kind of tutorial, I think.

Failure to Extend Zbiggy | 13 Jan : 14:00
Comments: 11

Registered: 21 Jun : 16:46

Reply to this
I should have added: "...less painful way, you demonstrated". Dealing with Forth alone rather than with assmebler 8051.

Failure to Extend Brad R | 15 Jan : 02:20
Comments: 10


Reply to this
I think what you really are asking for is a cross-compiler, also known in Forth circles as a metacompiler. This is a program which runs on your desktop PC, accepts Forth source code, and produces a binary output for your desired CPU (8051 or whatever).

On the MSP430, with the new version 0.41 it's now possible to add your Forth words to the system, and then using the FET-Pro430 tool download a copy of the updated Forth binary image, which can then be programmed into more chips. But that depends on the ability to add Forth definitions directly to the MCU's flash ROM. Of the 8051 family, the only chip for which I currently support compiling to flash is the C8051F.

Failure to Extend Zbiggy | 11 Feb : 21:19
Comments: 11

Registered: 21 Jun : 16:46

Reply to this
But wouldn't it be possible to extend CamelForth to make it behave similarly to AmForth (I mean this - "The Forth system, and any words I compile, are saved in low flash memory, which means my applications are non-volatile").

Actually, the two - freely selectable - modes of compilation would be handy:
- compilation to microcontroller's RAM - for evaluation/testing purposes
- compilation to flash memory - for extending the wordset

Not tried AmForth still, however - just found the quoted description lately.

Failure to Extend Brad R | 12 Feb : 10:29
Comments: 10


Reply to this
Actually, the new release (0.41) of MSP430 CamelForth does exactly that. New words are saved to flash, and can be made a permanent part of the dictionary. If you have enough RAM, new words can be compiled there, too.


You must be logged in to make comments on this site - please log in, or if you are not registered click here to signup