void createOutputFile(FILE* input, FILE* output, MacroTable* table ){ char* instruc = (char*)calloc(100,sizeof(char)); char* prev= (char*)calloc(100,sizeof(char)); char* parameter= (char*)calloc(100,sizeof(char)); char* oper = (char*)calloc(100,sizeof(char)); int index=-1; while(fscanf(input,"%s",instruc) > 0){ // If the operation with its operand is found, its mnemonic and operand is printed if(IsKeyword(instruc)==2){ fscanf(input,"%s",oper); strcat(instruc," "); strcat(instruc,oper); fprintf(output,"%s\n",instruc); } // If the operation without any operand is found, its mnemonic is printed if(IsKeyword(instruc)==1){ fprintf(output,"%s\n",instruc); } // finds a macro definition, and ignores its definition if(IsLabel(instruc)==1){ remove2P(instruc); if(isMacro(table,instruc)==1||isMacro(table,instruc)==2){//find a macro definition while(strcmp(instruc,"ENDMACRO")!=0){ fscanf(input,"%s",instruc);//ignores its definition } } else{ //if is label: strcat(instruc,":"); fprintf(output,"%s ",instruc); } } // prints macro whithout parameter in the output file if(isMacro(table,instruc)==1){ PrintMacroTableInFile(output,table, findLabelName(table,instruc)); } // prints macro with parameter if(isMacro(table,instruc)==2){ fscanf(input,"%s",parameter); PrintMacroTableInFileParameter(output,table, findLabelName(table,instruc),parameter); } } }
ReturnCode process(const pp::Token &token) { MSS_BEGIN(ReturnCode); if (state_ == MacroInclude) { if (isLineFeed(token) || isCarriageReturn(token)) { receiver_().include_detected(include_.str(), IncludeType::Macro); include_.str(""); state_ = Idle; } else include_ << token.range.content(); } else if (token.isWhitespace()) { } else { switch (state_) { case Idle: if (isMacro(token) && token.range.content() == "include") state_ = IncludeDetected; break; case IncludeDetected: if (isString(token)) { const auto content = token.range.content(); MSS(content.front() == '"' && content.back() == '"', IllegalInclude); MSS(content.size() > 2, IllegalIncludeSize); receiver_().include_detected(content.substr(1, content.size()-2), IncludeType::Local); state_ = Idle; } else if (isSymbol(token)) { MSS(token.range.content() == "<"); include_.str(""); state_ = BeginBracketInclude; } else if (isIdentifier(token)) { include_.str(""); include_ << token.range.content(); state_ = MacroInclude; } else { std::ostringstream oss; oss << "I expected either a Symbol or a String, but I got a " << toString(token.type); receiver_().include_error(oss.str()); MSS_L(UnexpectedToken); } break; case BeginBracketInclude: switch (token.type) { case Token::Identifier: include_ << token.range.content(); break; case Token::Symbol: { const auto symbol = token.range.content(); assert(symbol.size() == 1); switch (symbol[0]) { case '.': case '/': include_ << symbol; break; case '>': receiver_().include_detected(include_.str(), IncludeType::System); include_.str(""); state_ = Idle; break; default: MSS_L(UnexpectedSymbol); break; } } break; default: MSS_L(UnexpectedToken); break; } break; } } MSS_END(); }
ReturnCode process(const gubg::file::File &file) { MSS_BEGIN(ReturnCode); Range range; MSS(gubg::file::read(range, file)); pp::Lexer<std::vector<pp::Token>> lexer; MSS(lexer.tokenize(range)); enum State {Idle, IncludeDetected, BeginBracketInclude, MacroInclude}; State state = Idle; std::ostringstream incl; auto &tokens = lexer.tokens(); for (auto it = tokens.begin(); it != tokens.end(); ++it) { auto &token = *it; typedef pp::Token Token; if (state == MacroInclude) { if (isLineFeed(token) || isCarriageReturn(token)) { receiver_().includes_detected(incl.str(), IncludeType::Macro); incl.str(""); state = Idle; } else incl << token.range.content(); continue; } if (token.isWhitespace()) continue; switch (state) { case Idle: if (isMacro(token) && token.range.content() == "include") state = IncludeDetected; break; case IncludeDetected: if (isString(token)) { const auto content = token.range.content(); MSS(content.front() == '"' && content.back() == '"', IllegalInclude); MSS(content.size() > 2, IllegalIncludeSize); receiver_().includes_detected(content.substr(1, content.size()-2), IncludeType::Local); state = Idle; } else if (isSymbol(token)) { MSS(token.range.content() == "<"); incl.str(""); state = BeginBracketInclude; } else if (isIdentifier(token)) { incl.str(""); incl << token.range.content(); state = MacroInclude; } else { std::ostringstream oss; oss << "I expected either a Symbol or a String, but I got a " << toString(token.type); receiver_().includes_error(oss.str()); MSS_L(UnexpectedToken); } break; case BeginBracketInclude: switch (token.type) { case Token::Identifier: incl << token.range.content(); break; case Token::Symbol: { const auto symbol = token.range.content(); assert(symbol.size() == 1); switch (symbol[0]) { case '.': case '/': case '-': case '+': incl << symbol; break; case '>': receiver_().includes_detected(incl.str(), IncludeType::System); incl.str(""); state = Idle; break; default: MSS_L(UnexpectedSymbol); break; } } break; default: MSS_L(UnexpectedToken); break; } break; } } if (state == MacroInclude) { receiver_().includes_detected(incl.str(), IncludeType::Macro); incl.str(""); state = Idle; } MSS_END(); }