#include #include #include "../include/types.h" #include "../include/language.h" #include "../include/commands.h" #include #include #include using namespace std; #define DEBUG 1 #define MAX_LENGTH 250 ofstream out; char lowercase(char x) { return ( ((x >= 'A') && (x <= 'Z')) ? (char)(x | 0x20) : (char)(x) ); } void getStringBits(char ret[][MAX_LENGTH], const char* cmd) { int i = 0, j = 0, w = 0; if (DEBUG) { out << "String passed to getStringBits: " << cmd << endl; out << "Getting Bits!" << endl; } while (cmd[i] != '\n') { while (cmd[i] == ' ') i++; j = 0; while ( (cmd[i] != ' ') && (cmd[i] != '\n') ) { ret[w][j++] = lowercase(cmd[i++]); } ret[w++][j] = '\0'; if (w == 5) break; // don't wanna go past! } if (cmd[i] != '\n') { // piece of crap, isn't in brackets! ret[w - 1][j] = '('; // overwriting the null added before ret[w - 1][j + 1] = cmd[strlen(cmd) - 2]; ret[w - 1][j + 2] = ')'; ret[w - 1][j + 3] = '\0'; } } unsigned short mapConditionToInteger(char * condition) { // Special case the Sense Markers if (condition[strlen(condition) - 1] == ')') { unsigned short markerNumber = condition[strlen(condition) - 2] - '0'; return MARKER_MASK + markerNumber; // Liviu will deal with this in the default of switch } if (!strcmp(condition, FRIEND)) return FRIEND_MASK; if (!strcmp(condition, FOE)) return FOE_MASK; if (!strcmp(condition, FRIENDWITHFOOD)) return FRIENDWITHFOOD_MASK; if (!strcmp(condition, FOEWITHFOOD)) return FOEWITHFOOD_MASK; if (!strcmp(condition, FOOD)) return FOOD_MASK; if (!strcmp(condition, ROCK)) return ROCK_MASK; if (!strcmp(condition, FOEMARKER)) return FOEMARKER_MASK; if (!strcmp(condition, HOME)) return HOME_MASK; if (!strcmp(condition, FOEHOME)) return FOEHOME_MASK; return 50000; // NOUS SOMMES SCREWED! } unsigned short mapDirectionToInteger(char * direction) { return 50000; // NOUS SOMMES SCREWED! } int parseAlgo(char* filename, Command* & cmd_array, int & numC) { //return non-zero on error out.open("blah.txt"); int numCommands = 0; char inp[MAX_LENGTH + 1]; FILE * fin = fopen(filename, "r"); if(!fin) return 1; while(fgets(inp, MAX_LENGTH, fin)) numCommands++; fclose(fin); numC = numCommands; int curCommand; if (DEBUG) out << "Number of commands: " << numCommands << endl; struct Command * ret = (struct Command *)malloc(numCommands*sizeof(struct Command)); if (ret == NULL) { printf("NOUS SOMMES SCREWED!\n"); exit(1); } char stringBits[5][MAX_LENGTH]; // array of strings fin = fopen(filename, "r"); for (curCommand = 0; curCommand < numCommands; curCommand++) { if (DEBUG) out << "Current command: " << curCommand << endl; fgets(inp, MAX_LENGTH, fin); // input is in inp... STUPID INPUTTING OF STRINGS! getStringBits(stringBits, inp); // assuming that stringBits contains exactly FIVE thingies #if DEBUG out << "String bits for command # " << curCommand << " = "; for (int i = 0; i < 5; i++) { out << stringBits[i] << " : "; } out << endl; #endif if (!strcmp(stringBits[0], SENSE)) { // *** SENSE COMMAND if (!strcmp(stringBits[1], HERE)) { // COMMAND = "Sense Here x x x" ret[curCommand].opcode = SENSE_HERE_MASK; } else if (!strcmp(stringBits[1], AHEAD)) { // COMMAND = "Sense Ahead x x x" ret[curCommand].opcode = SENSE_AHEAD_MASK; } else if (!strcmp(stringBits[1], LEFTAHEAD)) { // COMMAND = "Sense LeftAhead x x x" ret[curCommand].opcode = SENSE_LEFTAHEAD_MASK; } else if (!strcmp(stringBits[1], RIGHTAHEAD)) { // COMMAND = "Sense RightAhead x x x" ret[curCommand].opcode = SENSE_RIGHTAHEAD_MASK; } ret[curCommand].st1 = (unsigned short)atoi(stringBits[2]); ret[curCommand].st2 = (unsigned short)atoi(stringBits[3]); ret[curCommand].arg = mapConditionToInteger(stringBits[4]); } else if (!strcmp(stringBits[0], MARK)) { // *** MARK COMMAND ret[curCommand].opcode = MARK_MASK; ret[curCommand].arg = (unsigned short)atoi(stringBits[1]); ret[curCommand].st1 = (unsigned short)atoi(stringBits[2]); } else if (!strcmp(stringBits[0], UNMARK)) { // *** UNMARK COMMAND ret[curCommand].opcode = UNMARK_MASK; ret[curCommand].arg = (unsigned short)atoi(stringBits[1]); ret[curCommand].st1 = (unsigned short)atoi(stringBits[2]); } else if (!strcmp(stringBits[0], PICKUP)) { // *** PICKUP COMMAND ret[curCommand].opcode = PICKUP_MASK; ret[curCommand].st1 = (unsigned short)atoi(stringBits[1]); ret[curCommand].st2 = (unsigned short)atoi(stringBits[2]); ret[curCommand].arg = 65432; } else if (!strcmp(stringBits[0], DROP)) { // *** DROP COMMAND ret[curCommand].opcode = DROP_MASK; ret[curCommand].st1 = (unsigned short)atoi(stringBits[1]); } else if (!strcmp(stringBits[0], TURN)) { // *** TURN COMMAND if (!strcmp(stringBits[1], LEFT)) ret[curCommand].opcode = TURNLEFT_MASK; else ret[curCommand].opcode = TURNRIGHT_MASK; ret[curCommand].st1 = (unsigned short)atoi(stringBits[2]); } else if (!strcmp(stringBits[0], MOVE)) { // *** MOVE COMMAND ret[curCommand].opcode = MOVE_MASK; ret[curCommand].st1 = (unsigned short)atoi(stringBits[1]); ret[curCommand].st2 = (unsigned short)atoi(stringBits[2]); } else { // *** FLIP COMMAND ret[curCommand].opcode = FLIP_MASK; ret[curCommand].arg = (unsigned short)atoi(stringBits[1]); ret[curCommand].st1 = (unsigned short)atoi(stringBits[2]); ret[curCommand].st2 = (unsigned short)atoi(stringBits[3]); } } fclose(fin); #if DEBUG for (int i = 0; i < numCommands; i++) { out << (unsigned short)ret[i].opcode << " " << (unsigned short)ret[i].arg << " " << ret[i].st1 << " " << ret[i].st2 << endl; } #endif out.close(); cmd_array = ret; // done return 0; }