From 26e5f891d537b5d4fc2890c9cdbeb73d5748d568 Mon Sep 17 00:00:00 2001 From: thetedgt <44469837+thetedgt@users.noreply.github.com> Date: Sat, 3 Nov 2018 13:34:18 +0200 Subject: homework 1 (#27) --- homework/81625/cpp2html/src/main.cpp | 107 +++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 homework/81625/cpp2html/src/main.cpp (limited to 'homework/81625/cpp2html/src/main.cpp') diff --git a/homework/81625/cpp2html/src/main.cpp b/homework/81625/cpp2html/src/main.cpp new file mode 100644 index 0000000..92f4d30 --- /dev/null +++ b/homework/81625/cpp2html/src/main.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include "Lexer.h" + +void printTokens(std::vector &tokens) +{ + for(int i = 0;i &tokens, const char* code) +{ + std::ofstream out("../cpp2.html"); + std::string header = "\n" + "\n" + "\n" + " \n" + "\n" + "\n"; + out << header; + unsigned codeInd = 0; + for(int i = 0;i < tokens.size();i++) + { + while(code[codeInd] == ' ' || code[codeInd] == '\t' || code[codeInd] == '\n') + { + if(code[codeInd] == '\n') out << "
"; + else out << " "; + //else out << + ++codeInd; + } + if(tokens[i].type == TokenType::NUMBER) + { + out << "" << tokens[i].value <<""; + } + else if(tokens[i].type == TokenType::STRING) + { + out << "" << tokens[i].lexeme <<""; + } + else if(tokens[i].type == TokenType::COMMENT) + { + out << "" << tokens[i].lexeme <<""; + } + else if(tokens[i].type <= TokenType::RIGHT_SHIFT_EQUAL) + { + out << "" << tokens[i].lexeme <<""; + } + else if(tokens[i].type <= TokenType::XOR_EQ) + { + out << "" << tokens[i].lexeme <<""; + } + else if(tokens[i].type <= TokenType::PRE_PRAGMA) + { + out << "" << tokens[i].lexeme <<""; + } + else + { + out << tokens[i].lexeme; + } + codeInd += tokens[i].lexeme.length(); + } + out << "\n\n"; + out.close(); +} + +int main() +{ + char code[] = "#include\nbool isCapitalCase(char c)\n{\n return c >= 'A' && c <= 'Z';\n}\nm_KeyWordsTable[\"break\"] = TokenType::Break;\n// this is a comment test\n" + "/* another one */"; + Tokenizer tokenizer(code); + std::vector tokens; + + if(tokenizer.tokenize(tokens)) + { + cpp2html(tokens, code); + } + //printTokens(tokens); + return 0; +} + -- cgit v1.3.1-23-gd82d