OK, so i'm writing my own compilier that will compile a script that controls a robot. An example of that script would be.
Code:
#Example Program
RESET
Forward 3
pause 1
headlights ON
Headlights on
reverse 3
pause 2
headlights off
TurnRight 4
TURNLEFT 4
pAuSe 3
# Do this three times in a row
LOOP 3
FORWARD 2
Headlights ON
Pause 2
RESET
End Loop
RESET
I kind of know how to go about doing this so far I have this much.
- reads the file(script) to a series of strings
- changes the entire script to lower case
- tokenize the string
- debug it by checking to make sure everything is in the right place
my current problem....well it's not really a problem, but it's annoying. my code is kind of repatitive and i can't seem to think of a good function to replace the duplicating parts. The repeating part is:
Code:
if (strcmp(tokenPtr, "reset") == 0)
printf ("RESET\n");
else if (strcmp(tokenPtr, "forward") == 0)
printf ("FORWARD\n");
else if (strcmp(tokenPtr, "reverse") == 0)
printf ("REVERSE\n");
else if (strcmp(tokenPtr, "turnleft") == 0)
printf ("TURN LEFT\n");
else if (strcmp(tokenPtr, "turnright") == 0)
printf ("TURN RIGHT\n");
else if (strcmp(tokenPtr, "headlights") == 0)
printf ("HEADLIGHTS\n");
else if (strcmp(tokenPtr, "pause") == 0)
printf ("PAUSE\n");
else if (strcmp(tokenPtr, "loop") == 0)
printf ("LOOP\n");
tokenPtr = strtok(NULL, " ");
I use it once when checking the script for errors, and i'll be using it again when i actually run the script once it is error free. Any suggestions?