static int registerCommandInternal(const char *name, CommandType type, CommandArgumentType argType, CommandHook hook, void *cookie) { CommandEntry *entry; if (!gCommandState.commandStateInitialized) { return -1; } if (name == NULL || hook == NULL) { return -1; } if (type != CMD_TYPE_COMMAND && type != CMD_TYPE_FUNCTION) { return -1; } if (argType != CMD_ARGS_BOOLEAN && argType != CMD_ARGS_WORDS) { return -1; } entry = (CommandEntry *)malloc(sizeof(CommandEntry)); if (entry != NULL) { entry->name = strdup(name); if (entry->name != NULL) { int ret; entry->cookie = cookie; entry->type = type; entry->argType = argType; entry->hook = hook; ret = addToSymbolTable(gCommandState.symbolTable, entry->name, entry->type, entry); if (ret == 0) { return 0; } } free(entry); } return -1; }
int main(int argc, char *argv[]) { FILE *fp = NULL; char buffer[80], tok1[SIZE], tok2[SIZE], tok3[SIZE], tok4[SIZE], tok5[SIZE] = ""; int num, i, cnt = 0, s, templc, changeflag = 0, startflag=0, endflag=0; char c; char icls[5]; int opcode, length = 0; fp=fopen(argv[1], "r"); do { c = fgetc(fp); if(c=='\n') cnt++; }while(c!=EOF); fp=fopen(argv[1], "r"); do { strcpy(tok1, ""); strcpy(tok2, ""); strcpy(tok3, ""); strcpy(tok4, ""); strcpy(tok5, ""); fgets(buffer, 80, fp); changeflag = 0; num = sscanf(buffer, "%s%s%s%s%s%s", tok1, tok2, tok3, tok4, tok5, temp); if(tok2[strlen(tok2)-1]==',') tok2[strlen(tok2)-1]='\0'; if(tok3[strlen(tok3)-1]==',') tok3[strlen(tok3)-1]='\0'; sprintf(temp, "%d:\t\t", lc); strcat(opfile, temp); switch(num) { default: errorflag++; sprintf(temp, "Line %d:\tInvalid number of mnemonics found.\n", ln+1); strcat(errorLog, temp); break; case 1: fetchFromMot(tok1, icls, &opcode, &length); if(strcmp(tok1, "START")==0) { lc = 0; changeflag++; startflag++; } if(opcode==-1) { errorflag++; sprintf(temp, "Line %d:\tUndefined mnemonic '%s'.\n", ln+1, tok1); strcat(errorLog, temp); } generateIntermediate(temp, icls, opcode); strcat(opfile, temp); if(strcmp(tok1, "LTORG")==0 || strcmp(tok1, "END")==0) { for(i=0;i<litPtr;i++) { if(littable[i].address==-1) { strcat(opfile, "\n"); sprintf(temp, "%d:\t\t", lc); strcat(opfile, temp); addToLiteralTable(littable[i].lname, &opcode, 1); generateIntermediate(temp, "LIT", opcode); strcat(opfile, temp); lc++; } } changeflag++; if(strcmp(tok1, "END")==0) endflag++; addToPoolTable(); } length=0; break; case 2: fetchFromMot(tok1, icls, &opcode, &length); if(strcmp(tok1, "START")==0) { lc = atoi(tok2); changeflag++; startflag++; generateIntermediate(temp, icls, opcode); strcat(opfile, temp); sprintf(temp, ", "); strcat(opfile, temp); generateIntermediate(temp, "CONST", atoi(tok2)); strcat(opfile, temp); } else if(strcmp(tok1, "ORIGIN")==0) { for(i=0;i<symPtr;i++) { if(strcmp(symtable[i].sname, tok2)==0) { lc = symtable[i].address; break; } } changeflag++; generateIntermediate(temp, icls, opcode); strcat(opfile, temp); sprintf(temp, ", "); strcat(opfile, temp); generateIntermediate(temp, "SYM", i+1); strcat(opfile, temp); } length=0; break; case 4: if(strcmp(tok1, "ORIGIN")==0) { for(i=0;i<symPtr;i++) { if(strcmp(symtable[i].sname, tok2)==0) { if(strcmp(tok3, "+")==0) s = 1; else if(strcmp(tok3, "+")==0) s = -1; lc = symtable[i].address + (s * atoi(tok4)); break; } } fetchFromMot(tok1, icls, &opcode, &length); generateIntermediate(temp, icls, opcode); strcat(opfile, temp); strcat(opfile, ", "); generateIntermediate(temp, "SYM", i+1); strcat(opfile, temp); sprintf(temp, " %s (CONST, %s)", tok3, tok4); strcat(opfile, temp); length=0; changeflag++; break; } addToSymbolTable(tok1, &opcode, 1); strcpy(tok1, tok2); strcpy(tok2, tok3); strcpy(tok3, tok4); case 3: fetchFromMot(tok1, icls, &opcode, &length); if(strcmp(icls,"IS")==0) { generateIntermediate(temp, icls, opcode); strcat(opfile, temp); strcat(opfile, ", "); if(strcmp(tok1, "BC")!=0) fetchFromReg(tok2, &opcode); else if(strcmp(tok1, "BC")==0) fetchCondition(tok2, &opcode); sprintf(temp, "0%d", opcode); strcat(opfile, temp); strcat(opfile, ", "); if(tok3[0] == '=') { addToLiteralTable(tok3, &opcode, 0); generateIntermediate(temp, "LIT", opcode); strcat(opfile, temp); } else { addToSymbolTable(tok3, &opcode, 0); generateIntermediate(temp, "SYM", opcode); strcat(opfile, temp); } changeflag++; } fetchFromMot(tok2, icls, &opcode, &length); if(strcmp(tok2, "DS")==0) { generateIntermediate(temp, icls, opcode); strcat(opfile, temp); strcat(opfile, ", "); addToSymbolTable(tok1, &opcode, 1); length = atoi(tok3); generateIntermediate(temp, "SYM", opcode); strcat(opfile, temp); changeflag++; } else if(strcmp(tok2, "DC")==0) { generateIntermediate(temp, icls, opcode); strcat(opfile, temp); strcat(opfile, ", "); addToSymbolTable(tok1, &opcode, 1); length = 1; generateIntermediate(temp, "SYM", opcode); strcat(opfile, temp); changeflag++; } else if(strcmp(tok2, "EQU")==0) { for(i=0;i<symPtr;i++) { if(strcmp(symtable[i].sname, tok3)==0) { templc = lc; lc = symtable[i].address; addToSymbolTable(tok1, &opcode, 1); lc = templc; } break; } fetchFromMot(tok2, icls, &opcode, &length); generateIntermediate(temp, icls, opcode); strcat(opfile, temp); strcat(opfile, ", "); generateIntermediate(temp, "SYM", i+1); strcat(opfile, temp); length=0; changeflag++; } break; case 5: if(strcmp(tok2, "EQU")==0) { for(i=0;i<symPtr;i++) { if(strcmp(symtable[i].sname, tok3)==0) { templc = lc; if(strcmp(tok4, "+")==0) s = 1; else if(strcmp(tok4, "-")==0) s = -1; lc = symtable[i].address + (s * atoi(tok5)); addToSymbolTable(tok1, &opcode, 1); lc = templc; break; } } if(i==symPtr || symtable[i].address == -1) { errorflag++; sprintf(temp, "Line %d:\tEQU needs to be backward reference.\n", ln+1); strcat(errorLog, temp); } fetchFromMot(tok2, icls, &opcode, &length); generateIntermediate(temp, icls, opcode); strcat(opfile, temp); strcat(opfile, ", "); generateIntermediate(temp, "SYM", i+1); strcat(opfile, temp); sprintf(temp, " %s (CONST, %s)", tok4, tok5); strcat(opfile, temp); changeflag++; length=0; } break; } if(changeflag==0) { errorflag++; sprintf(temp, "Line %d:\tCould not parse statement\n", ln+1); strcat(errorLog, temp); } strcat(opfile, "\n"); lc = lc + length; ln++; }while(cnt!=ln); for(i=0;i<symPtr;i++) { if(symtable[i].address==-1) { errorflag++; sprintf(temp, "%s:\tUndefined symbol '%s'\n", argv[1], symtable[i].sname); strcat(errorLog, temp); } } if(!(startflag==1 && endflag==1)) { errorflag++; sprintf(temp, "%s:\tThere should be exactly one START and one END symbol\n", argv[1]); strcat(errorLog, temp); } if(errorflag==0) { printf("%s", opfile); saveOutput(argv[1]); } else { printf("%d error(s) found\n", errorflag); printf("%s", errorLog); } fclose(fp); return 0; }
string LexicalAnalyser::getNextToken () { static bool lineEOF; if (NULL == inputFile) { throw NoInputFileException; } // Check if the 'line' variable holds any content. if (!line || lineEOF) { if (inputFileEOF) { std::cerr << "No Token found\n"; throw NoMoreTokenException; } string tmpLine; do { getline (*inputFile, tmpLine); // std::cerr << tmpLine.size (); } while (tmpLine.size () <= 0 && !inputFile->eof()); if ( line ) { delete line; } line = new istringstream (tmpLine); lineEOF = false; curLine++; if (inputFile->eof()) { inputFileEOF = true; } } // If it does, read from it. string lexeme; *line >> lexeme; if (line->eof()) lineEOF = true; if (lexeme.size () <= 0) { if (lineEOF) { // std::cerr << "[LEX] recursive call\n"; return getNextToken(); } else { assert (0); } } string token = tokenize (lexeme); col = line->tellg(); // Line break because of compiler warning col -= lexeme.length() - 1; addToSymbolTable (lexeme, token, curLine, col); // std::cerr << "[LexicalAnalyser] Found token : " << token << "\n"; return token; }