gforth
[Top][All Lists]

## [gforth] If then else in interpreted mode

 From: John Allsup Subject: [gforth] If then else in interpreted mode Date: Wed, 01 May 2013 23:15:01 +0100 User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:17.0) Gecko/20130328 Thunderbird/17.0.5

Hi, I'm new to this list and was wondering if there were any thoughts on the following,
```
```
I read in the gforth manual that there is no satisfactory way to do interpret mode if else then. I played around with a quick idea (you can see a little more at the links on http://thewikiman.allsup.co/?ForthLang where I link to transcripts of experiments with gforth. (I a bit of a forth and gforth noob, but anyway)
```
Let's just define
: 2' ' ' ; ( and ) : 3' ' ' ' ; ( etc, for convenience )
and for the sake of a simple example, define
: a 1 . ; : b 2 . ; : c 3 . ;

Suppose we do
3' a b c
```
so we get a b c on the stack in that order (c at the top). Consider the effect of
```    _n_ pick execute ( where _n_ is 0, 1 or 2 )
```
then with e.g. _n_ == 2 we get a executed, with the stack left unchanged, and _n_ == 0 picks out c (pun intended)
```
Now consider what happens if we define
: jif 0 = if 0 else 1 then ;
```
Then whatever is at TOS, if it is nonzero, gets turned into a 1, and a zero value gets left as is.
```If we then define
: fy pick execute ;
then
2' a b 0 jif fy ( executes b, or the else clause, and )
```
2' a b 1 jif fy ( executes a, or the iftrue clause, and, more importantly ) 2' a b 42 jif fy ( executes a as well, as nonzero values collapse to 1 )
```
You can then define
: jiffy jif fy ;
to be able to do
2' left right cond jiffy

I tend to like to define
: jemmy fy ;
```
so as to have two nicely named words: jiffy and jemmy that do the work of interpret time conditional execution.
```
```
This is obviously not the highest performance approach, but since in interpret mode at the outer level (essentially the only place you'd be using such constructs), the limiting factor is the user's typing speed anyway, and for proper compiled words you have other constructs like if else then anyway.
```
```
As to taking this much further, we hit a place where it is really nice to have more stacks (since you ideally want an operator stack to do the picking from rather than the data stack so that you can push xt's onto an operator stack, then place data on the data stack, do a pick-from-opstack execute ( thing ) a few times, drop the xt's off the operator stack without the mess that results when you are forced to pick through the data added to the data stack after the ops but before you know which op you want. At a non-portable hardware level, I note that x64 adds 8 GP registers which would be useful in implementing extra stacks efficiently on a custom forth.
```
John

--
: jda ." John D. Allsup" ." woz 'ere" ." yeah!" ; immediate jda
: website ." allsup.co" and ." chalisque.com" ; 1 1 website if
: blog ." deardiary.chalisque.org" ; immediate then
( no, I haven't checked whether this actually runs... )

```