|
|
Up |
|
|
  |
Author: Krishna MyneniKrishna Myneni Date: Aug 1, 2007 18:15
I've seen this discussed in c.l.f. before, but since I can't readily find the
thread, I will bring it up again: What are some implementations of the C
language "enum" in Forth? I've coded the following simple implementation:
\ ---------------------
( simple enumeration utility
Use: val enum name1 name2 name3 ...
where val is the unsigned integer starting value, and name1, name2,
name3, ... are defined by enum to be constants taking the values
val, val+1, val+2, ...
|
| Show full article (1.09Kb) |
|
| | 20 Comments |
|
  |
Author: Jeff M.Jeff M. Date: Aug 1, 2007 19:15
I've never particularly liked making Forth /look/ like another
language, but some language features are nice. Enums are nice, but
C...? Yuck. I prefer the following implementation:
: ENUM ( n -- )
CREATE , DOES> DUP @ CONSTANT ++ ;
Where ++ is defined as "increment [addr]". Then I use it like so:
0 ENUM COLOR
COLOR RED
COLOR BLUE
COLOR PURPLE
Depending on how I feel that day, sometimes I make a version that
defaults to 0 as the base or make a version of the created words that
allow for different increments than 1.
HTH,
Jeff M.
|
| |
|
| | no comments |
|
  |
Date: Aug 1, 2007 19:22
On 2007-08-02, Krishna Myneni bellsouth.net> wrote:
> I've seen this discussed in c.l.f. before, but since I can't readily find the
> thread, I will bring it up again: What are some implementations of the C
> language "enum" in Forth? I've coded the following simple implementation:
>
Not quite C's enum, but I had occation to match a set of bitfield enums
of the form:
enum {
VAL1 = (1 << 0),
VAL2 = (1 << 1),
VAL3 = (1 << 2),
...
};
In doing so I came up with this:
: BIT-ENUM: ( u -- ) 0 DO 1 I LSHIFT CONSTANT LOOP ;
used thus:
3 BIT-ENUM: VAL1 VAL2 VAL3
|
| Show full article (0.78Kb) |
| no comments |
|
  |
Author: Krishna MyneniKrishna Myneni Date: Aug 1, 2007 21:46
Jeff M. wrote:
> I've never particularly liked making Forth /look/ like another
> language, but some language features are nice. Enums are nice, but
> C...? Yuck. I prefer the following implementation:
>
> : ENUM ( n -- )
> CREATE , DOES> DUP @ CONSTANT ++ ;
>
> Where ++ is defined as "increment [addr]". Then I use it like so:
>
> 0 ENUM COLOR
>
> COLOR RED
> COLOR BLUE
> COLOR PURPLE
>
> Depending on how I feel that day, sometimes I make a version that
> defaults to 0 as the base or make a version of the created words that
> allow for different increments than 1.
> ...
|
| Show full article (1.30Kb) |
| no comments |
|
  |
Author: Roelf ToxopeusRoelf Toxopeus Date: Aug 1, 2007 23:48
In article bignews3.bellsouth.net>,
Krishna Myneni bellsouth.net> wrote:
> I've seen this discussed in c.l.f. before, but since I can't readily find the
> thread, I will bring it up again: What are some implementations of the C
> language "enum" in Forth? I've coded the following simple implementation:
>
[...]
> The above version is also simple to implement, but I'm interested to know
> what
> others use.
>
We have this in MacForth, for the convenience to import large numbers
of ENUM's from Apple 'C' Header filess. Not seen their usage in another
context.
Roelf
|
| |
| no comments |
|
  |
Author: David N. WilliamsDavid N. Williams Date: Aug 2, 2007 05:11
Krishna Myneni wrote:
> I've seen this discussed in c.l.f. before, but since I can't readily
> find the thread, I will bring it up again: What are some implementations
> of the C language "enum" in Forth? I've coded the following simple
> implementation:
>
> \ ---------------------
> ( simple enumeration utility
>
> Use: val enum name1 name2 name3 ...
>
> where val is the unsigned integer starting value, and name1, name2,
> name3, ... are defined by enum to be constants taking the values
>
> val, val+1, val+2, ...
>
> e.g. "0 enum orange apple banana"
> )
> : enum ( u -- )
> BEGIN ...
|
| Show full article (2.38Kb) |
| no comments |
|
  |
Author: David N. WilliamsDavid N. Williams Date: Aug 2, 2007 05:19
Oops!!
David N. Williams wrote:
> [...]
> PREPARSE, and does PARSE-NAME SDROP. Both are included in my
^
^
PARSE-NAME-AWAY
-- David
|
| |
| no comments |
|
  |
|
|
  |
Author: Marcel HendrixMarcel Hendrix Date: Aug 2, 2007 10:04
Coos Haak wrote Re: enum in Forth
[..]
> I have implemented a version, but I must think hard to remember when I used it.
I have used ENUM four times:
* TinyKiss.frt (TINYKISS language compiler vsn 1.1 by Jack W. Crenshaw),
* meteor.frt (The Computer Language Shootout, http://shootout.alioth.debian.org/,
contributed by Ian Osgood),
* floyd.frt (Floyd's algorithm F2 for a deck of cards),
* TinyC.frt (Marc Feeley's tiny C compiler).
-marcel
|
| |
| no comments |
|
  |
|
|
  |
Author: Ian OsgoodIan Osgood Date: Aug 2, 2007 11:40
On Aug 2, 10:04 am, m...@iae.nl (Marcel Hendrix) wrote:
> Coos Haak wrote Re: enum in Forth
> [..]
>
>> I have implemented a version, but I must think hard to remember when I used it.
This minimal enum came in handy for making a nicely-formatted little
table:
: enum ( n -- ) 0 do I constant loop ;
: table create does> ( i -- t[i] ) swap cells + @ ;
7 enum E SE SW W NW NE STOP
table offset 1 , 7 , 6 , -1 , -7 , -6 , 0 ,
table rotate SE , SW , W , NW , NE , E , STOP ,
table reflect E , NE , NW , W , SW , SE , STOP ,
Lifting start out as a param would make it a bit more general purpose
(7 0 enum ...).
Ian
|
| |
| no comments |
|
|
|
|