06ee9705a8
s:contains-char? to s:contains/char? s:contains-string? to s:contains/string? a:contains-string? to a:contains/string? old names are now deprecated and will be removed after 2021.7. FossilOrigin-Name: 5a19d7aac514c5ba87963c5f0645f3daa8a8e3dc04546c0627fa046479ecd8dd
49 lines
986 B
Forth
49 lines
986 B
Forth
# enumeration
|
|
|
|
This is a method of creating a set of constants from names
|
|
provided in an array.
|
|
|
|
The approach was inspired by 8th[1], in which a short modifier
|
|
allows setting the enum value at any time. In use this looks
|
|
like:
|
|
|
|
{ 'a 'b=10 'c 'd=998 'e 'f 'g=33 } a:enum
|
|
|
|
In this case:
|
|
|
|
a is 0
|
|
b is 10
|
|
c is 11
|
|
d is 998
|
|
e is 999
|
|
f is 1000
|
|
g is 33
|
|
|
|
Enumerations start at zero by default.
|
|
|
|
# Code
|
|
|
|
The approach used here is to iterate over strings in an array.
|
|
If the string contains a `=`, split the string, convert the
|
|
part after the `=` to a number, which will replace the enum
|
|
value. It then creates a constant using the string and
|
|
increments the counter.
|
|
|
|
~~~
|
|
:a:enum (a-)
|
|
#0 swap
|
|
[ dup $= s:contains/char?
|
|
[ nip $= s:split/char [ n:inc s:to-number ] dip ] if
|
|
over &const dip n:inc
|
|
] a:for-each drop ;
|
|
~~~
|
|
|
|
# Test
|
|
|
|
```
|
|
{ 'a=10 'b 'c 'd=998 'e 'f } a:enum
|
|
```
|
|
|
|
# References
|
|
|
|
[1] https://8th-dev.com/forum/index.php/topic,1979.msg11377.html
|