I am porting to Java a bytecode compiler written in C. In the C implementation, the set of operations is represented in an enum:
typedef enum {
SOME_OP,
ANOTHER_OP,
//Etc.
} Operations;
Operations are then mixed with numeric arguments in an array defined as:
uint8_t* opsAndArgs;
For example, opsAndArgs might hold:
{SOME_OP, 42, ANOTHER_OP, 99, 03, DIFFERENT_OP, RANDOM_OP, 14, etc.}
Finally, a function in the C program iterates through opsAndArgs and switches on operators, consuming arguments as appropriate:
int i = 0;
while (i < opsAndArgs.length) {
switch (opsAndArgs[i] {
case SOME_OP:
handleSomeOp(opsAndArgs[i+1]);
i = i + 2; break;
case ANOTHER_OP:
handleAnotherOp(opsAndArgs[i+1], opsAndArgs[i+2]);
i = i + 3; break;
case DIFFERENT_OP:
handleDifferentOp();
i = i + 1; break;
Etc.
}
}
Is there a way I can do the same thing in Java? I.e., create an Enum that I can mix into an array with numeric values, and still have the ability to switch on Enum-defined operators while treating the remaining values as arguments?
It seems like the original program is taking advantage of some feature of C enums such that they are mix-able with uint8_t values.