Reputation: 7319
I feel like there has to be a better way to do this than the extremely long way I tried. I have two tokens (token3 and token4) which can be strings R1 through R6. I also have int variables of the same names, R1 through R6. I basically want the case statement to be
(variable of name token3) += (variable of name token4);
The way I implemented this seems ridiculous and way too repetitive, but I don't know another way. There has to be some way to simplify this right?
// ADD COMMAND
else if (!strcmp(token2_instr, "ADD")) {
printf("%s\n", token2_instr);
token3_arg1 = strtok_r(NULL, ", ", &line_save);
token4_arg2 = strtok_r(NULL, " \n", &line_save);
int num = atoi(token4_arg2);
// ADD Reg, Reg
if (num == 0) {
if (!strcmp(token3_arg1, "R1")) {
if (!strcmp(token4_arg2, "R1"))
R1 += R1;
else if(!strcmp(token4_arg2, "R2"))
R1 += R2;
else if(!strcmp(token4_arg2, "R3"))
R1 += R3;
else if(!strcmp(token4_arg2, "R4"))
R1 += R4;
else if(!strcmp(token4_arg2, "R5"))
R1 += R5;
else if(!strcmp(token4_arg2, "R6"))
R1 += R6;
}
else if(!strcmp(token3_arg1, "R2")) {
if (!strcmp(token4_arg2, "R1"))
R2 += R1;
else if(!strcmp(token4_arg2, "R2"))
R2 += R2;
else if(!strcmp(token4_arg2, "R3"))
R2 += R3;
else if(!strcmp(token4_arg2, "R4"))
R2 += R4;
else if(!strcmp(token4_arg2, "R5"))
R2 += R5;
else if(!strcmp(token4_arg2, "R6"))
R2 += R6;
}
else if(!strcmp(token3_arg1, "R3")) {
if (!strcmp(token4_arg2, "R1"))
R3 += R1;
else if(!strcmp(token4_arg2, "R2"))
R3 += R2;
else if(!strcmp(token4_arg2, "R3"))
R3 += R3;
else if(!strcmp(token4_arg2, "R4"))
R3 += R4;
else if(!strcmp(token4_arg2, "R5"))
R3 += R5;
else if(!strcmp(token4_arg2, "R6"))
R3 += R6;
}
else if(!strcmp(token3_arg1, "R4")) {
if (!strcmp(token4_arg2, "R1"))
R4 += R1;
else if(!strcmp(token4_arg2, "R2"))
R4 += R2;
else if(!strcmp(token4_arg2, "R3"))
R4 += R3;
else if(!strcmp(token4_arg2, "R4"))
R4 += R4;
else if(!strcmp(token4_arg2, "R5"))
R4 += R5;
else if(!strcmp(token4_arg2, "R6"))
R4 += R6;
}
else if(!strcmp(token3_arg1, "R5")) {
if (!strcmp(token4_arg2, "R1"))
R5 += R1;
else if(!strcmp(token4_arg2, "R2"))
R5 += R2;
else if(!strcmp(token4_arg2, "R3"))
R5 += R3;
else if(!strcmp(token4_arg2, "R4"))
R5 += R4;
else if(!strcmp(token4_arg2, "R5"))
R5 += R5;
else if(!strcmp(token4_arg2, "R6"))
R5 += R6;
}
else if(!strcmp(token3_arg1, "R6")) {
if (!strcmp(token4_arg2, "R1"))
R6 += R1;
else if(!strcmp(token4_arg2, "R2"))
R6 += R2;
else if(!strcmp(token4_arg2, "R3"))
R6 += R3;
else if(!strcmp(token4_arg2, "R4"))
R6 += R4;
else if(!strcmp(token4_arg2, "R5"))
R6 += R5;
else if(!strcmp(token4_arg2, "R6"))
R6 += R6;
}
}
// ADD Reg, Immediate
else {
if (!strcmp(token3_arg1, "R1"))
R1 += num;
else if(!strcmp(token3_arg1, "R2"))
R2 += num;
else if(!strcmp(token3_arg1, "R3"))
R3 += num;
else if(!strcmp(token3_arg1, "R4"))
R4 += num;
else if(!strcmp(token3_arg1, "R5"))
R5 += num;
else if(!strcmp(token3_arg1, "R6"))
R6 += num;
}
}
Upvotes: 0
Views: 80
Reputation: 3710
This problem can be solved in an elegant way by introducing the right data structures. Paul Griffiths mentions this in his comment.
First, let's set up a data structure:
const int numberOfVariables = 6;
int R[numberOfVariables];
for (int i=0; i<numberOfVariables; i++) {
R[i] = 0;
}
char* tokenNames[] = {"R1", "R2", "R3", "R4", "R5", "R6"};
numberOfVariables
and tokenNames
are the points where you can alter the behavior, if you have more or lesser names or other variable names. So basically, instead of having six variables, we have one array for their names and one for their values.
Then:
// Assumptions for this snippet.
char token3_arg1[] = "R3";
char token4_arg2[] = "R4";
// Find the index of the name of token 3.
int indexToken3 = 0;
for (; indexToken3<numberOfVariables; indexToken3++) {
// (debug) printf("check: %s", tokenNames[indexToken3]);
if (!strcmp(token3_arg1, tokenNames[indexToken3])) {
break;
}
}
// Find the index of the name of token 4.
int indexToken4 = 0;
for (; indexToken4<numberOfVariables; indexToken4++) {
// (debug) printf("check: %s", tokenNames[indexToken4]);
if (!strcmp(token4_arg2, tokenNames[indexToken4])) {
break;
}
}
Paul Griffiths is finding the index by converting the digit in the variable name to a number. Here, I prefer the way with more flexibility - imagine you have R33
.
Having both indices, finally:
R[indexToken3] += R[indexToken4];
The trick behind is that the counters for each Rn
are at the same index as their names are.
If the project gets bigger, I highly recommend to write a function for finding the index, or even using a finished library function.
Upvotes: 2
Reputation: 44220
This is an ugly hack, but at least it avoids the repitition and the strcmp() chains:
static inline int *regname2ptr( char *regname, int *p1, int *p2, int *p3, int *p4, int *p5, int *p6)
{
if (*regname, || *regname != 'R') return NULL; // let it go boom
switch(reg[1] - '0' ) {
case 1: return p1;
case 2: return p2;
case 3: return p3;
case 4: return p4;
case 5: return p5;
case 6: return p6;
default: return NULL; // let it go boom encore
}
the_func()
{
int r1,r2,r3,r4,r5,r6;
int *src, *dst;
dst = regname2ptr(token4_arg1, &r1,&r2,&r3,&r4,&r5,&r6);
src = regname2ptr(token4_arg2, &r1,&r2,&r3,&r4,&r5,&r6);
*dst += *src;
}
Upvotes: 1