Short version:
Is it possible to use a macro to automatically generate an
enum definition based off an array of string names.
Explanation:
I'm using compiler version 0.21.1 with the following flags:
-lang fb -maxerr inf -w all -w next -w param -v -exx
I have some data files that are parsed to load data into two
parrallel arrays. One holds a string name of a terrain type,
the other holds the various data for movability, etc.
I'm wanting to use an enum as an index into the arrays, yet
have the code remain flexible to handle new types of terrains
being added without having to manually add enum indexes in the
program to match each newly added type.
Example of part of my terrain data
Code: Select all
[GRASS]
COL: GREEN
MOV: ALLOW_MOVE
OBS: ALLOW_LIGHT
[DIRT]
COL: BROWN
MOV: ALLOW_MOVE
OBS: ALLOW_LIGHT
[FLOOR]
COL: WHITE
MOV: ALLOW_MOVE
OBS: ALLOW_LIGHT
[WATER]
COL: LIGHT_BLUE
MOV: ALLOW_MOVE
OBS: ALLOW_LIGHT
each time I added a new type of data.
Example of enum used
Code: Select all
Enum Terrain Types
GRASS = 1
DIRT
FLOOR
WATER
MAX_TERRAIN_TYPES = WATER
End Enum
I can think of a way that I could automatically generate a
seperate .bi file when the code is parsed, and then #include
that .bi (with the enum definition) in it.
This would require a seperate step, re-compiling the code each
time the file was updated.
I want to be able to easily add new data definition and have the
code (enum definitions) expand to handle them automatically.
So far I have not had much success with the macro (I'm not
sure if its possible, I have not used macros before other
than simple #defines)
Latest attempt at macro to create an enum from list of strings.
Code: Select all
dim as integer x = 2
dim as string * 5 s ( 1 to x ) = { "WORLD", "ROGUE" }
#macro hacked_enum( x, test_string())
"Enum Index_String_Values"
dim as integer d
for d = 1 to x
string_test(d) & "= " & d
next
"MAX_TERRAIN_TYPES = " & string_test(x)
"End Enum"
#endmacro
dim as integer test_enum_val
test_enum_val = WORLD
? WORLD
sleep