#include "cpp.h" #include "bnf.h" #include "cppbnf.h" #include "debug.h" #include "lexer.h" #include "grammer.h" #include "minicc.h" #include #include #include using namespace Gram; CPP::CPP(){} CPP::~CPP(){} // Phase 1: Map physical character set to basic source character set void CPP::source_charset_map() { // TODO } // Phase 2: Escape backslashed line endings void CPP::backslash_escape() { // TODO } // Phase 3: Parse preprocessing tokens std::vector CPP::preprocessing_tokenize(const std::string& s) { auto bnf{SubBNF(GetCppBNFLex(), "preprocessing-token")}; Lex::Lexer lexer(bnf, "preprocessing-token"); return lexer.Lex(s); } // Phase 4: Preprocessing void CPP::preprocess() { // TODO } // Phase 5: Map chars and strings to execution charset void CPP::execution_charset_map() { // TODO } // Phase 6: Concatenate adjacent string literals void CPP::concatenate_strings() { // TODO } std::string CPP::valueOfNode(index_t node_index, const std::vector& Tree) { std::string result; std::vector todo(1, int32_t(node_index)); while (!todo.empty()) { int32_t current_index = todo.back(); todo.pop_back(); // visit node if token if (ChildIdIsToken(current_index)) { result += m_code[TokenIdFromChildId(current_index)]; } else { const TreeNode &node{Tree[current_index]}; // iterate backwards in childs, to get depth-first search in tree, from the beginning std::for_each(node.child_ids.rbegin(), node.child_ids.rend(), [&](int32_t child){ todo.push_back(child); }); } } return result; }; namespace { std::unordered_set pp_types { "identifier", "pp-number", "character-literal", "user-defined-character-literal", "string-literal", "user-defined-string-literal", "preprocessing-op-or-punc" }; std::unordered_set keywords { "alignas", "alignof", // ... Keywords table, p.15 }; } // Phase 7.a: Create tokens from preprocessing tokens std::vector CPP::tokens_from_pptokens(std::vector pp_tokens) { std::vector result; // "identifier" + value -> "identifier" + value, except identifiers from table 5.11, p.14 -> keyword as value, value // "pp-number" + value -> "literal" + value // "character-literal" -> "literal" + value // "user-defined-character-literal" -> "literal" + value // "string-literal" -> "literal" + value // "user-defined-string-literal" -> "literal" + value // "preprocessing-op-or-punc" -> value+value (operator,punctuator) for (auto& token: pp_tokens) { if (pp_types.find(token.type) != pp_types.end()) { if (token.type == "identifier") { #if 0 if (keywords.find(token.value) != keywords.end()) result.emplace_back("keyword", token.value); else #endif result.emplace_back(Token{"identifier"s, token.value}); } else if (token.type == "preprocessing-op-or-punc") result.emplace_back(Token{token.value, token.value}); else result.emplace_back(Token{"literal", token.value}); } else throw std::runtime_error("Unhandled preprocessing token: "s + token.value + " ("s + token.type + ")"s); } return result; } // Phase 7.b: Grammar Analysis std::pair> analysis(std::vector) { return {0 , {}}; } // Phase 7.c: Translate void CPP::translate() { // TODO } // Phase 8: Instantiate objects void CPP::instantiate() { // TODO } // Phase 9: Link libraries void CPP::link() { // TODO } // phases of translation, according to standard void CPP::translate(const std::string& code) { #if 0 // fix signatures! source_charset_map(); backslash_escape(); preprocessing_tokenize(code); preprocess(); execution_charset_map(); concatenate_strings(); tokens_from_pptokens(); analysis(); translate(); instantiate(); link(); #endif } class CppTest: public ::testing::Test { protected: CppTest() { //debug = true; } ~CppTest() { } }; #if 1 TEST_F(CppTest, preprocessing_tokenize) { CPP cpp; auto pp_tokens = cpp.preprocessing_tokenize("int main() { return 1; }"); ASSERT_EQ(pp_tokens.size(), 9); auto tokens = cpp.tokens_from_pptokens(pp_tokens); ASSERT_EQ(tokens.size(), 9); } #endif #if 0 TEST_F(CppTest, preprocessing_tokenize2) { CPP cpp; auto ppTree = cpp.preprocessing_tokenize("in ma"); cpp.tokens_from_pptokens(ppTree); } #endif #if 0 TEST(Cpp, translate) { CPP::translate(); } #endif