[PicForth] Case implementation

easlab at absamail.co.za easlab at absamail.co.za
Sat Nov 20 10:33:12 CET 2004


was Subject: [PicForth] Re: Feature Requests

>>>>> "David" == David McNab <david at rebirthing.co.nz> writes:

> David> 1) case ... of .. endof .. endcase
> 
> David> As per standard Forth
> 
> David> Since PicForth already has begin/while/repeat/until/if/then,
> David> the mechanisms for branch resolution are already in place.
> David> While 'case' statements don't necessarily add more power to the
> David> language, they certainly make for far more readable code, and
> David> might even facilitate smaller code (eliminating dup/drop
> David> statements) etc.

Sam wrote:
> I've implemented it in my development version (you can get it using
> darcs as explained on http://www.rfc1149.net/devel/picforth). 
> However, as with other "if" cases that cannot be cleverly optimized, the
> generated code is ugly.

>From distant past memories of a p-code implementation, CASE is either:
1. mere syntax-sugar for repeated IFELSE
  or 
2. provided the 'density' between the lowest and highest cases is
   sufficient: efficiently implementated by indexed jumps.
  Which is probably nice with PICasm, but I don't know how it
  fits with picforth.

eg. CASE
    13: do13 ;
    15: do15 ;
    16: do16 ;
    20: twenty
ELSE doDefault;

Translates to:

fixOffsetToLowestCase13;
jump do13 ;
jump doDefault; //for 14
jump do15 ;
jump do16 ;
jump doDefault; //for 17
jump doDefault; //for 18
jump doDefault; //for 19
jump twenty;
doDefault;

== Chris Glur.


PS. I think that attempting to 'ape' C on a proper micro-processor 
with forth on a PIC can be taken too far -- well beyond 'productive
optimum'.  

OTOH since we've got some original thinkers here:
* every new Uproc has to invent it's own arbitrary mnemonics, because
   others are copyright.
* Intel might have started with: ldi 7, A
* and Motorola then said: movi 7, A
* what does PIC say for: "7 -> W"  ?
   Which is also only 6 chars [including white-chars], and UNDERSTOOD
  immediately what it means ?! Whereas "ldi 7, A" is 7 chars, and you
   have to 'look in the "explanation-table" ' to check the syntax.

Do you want to get married [do a training course] every time you
   work with a new micro-processor ?!

IMO this absurdity is perpetuated [and the greater absurdity of
selecting a "R" a "E" a ..."I" a "L" from the keyboard, instead of
a single-token: "REPEAT UNTIL"] because people want to pretend
that they are 'communicating' in near English with the little man in 
the box.

When you control a lift/elevator, and there are exactly nine
valid destinations, you select one "token" ; you don't type
"eight floor" !!

Why pretend that the the programmer has the freedom of a poet, when 
at many stages of the 'code' there are only 2 or 3 valid options, for well
structured languages ?

The earliest killer-aps: spread-sheets used this principle.
How did we get lost ?

Once I needed to code a minimal assembler for an 8-bit Uproc,
and I used the: 'A + B -> A'  format; instead of apeing the 
Motorola-book's eg.: adda  a,b [or whatever].

It was beautifully easy to read/understand the code.
The next best thing is to just select full-tokens from a menu,
instead of building with characters.   Correct syntax is FORCED
by the menu.




More information about the PicForth mailing list