#include "cpp.h" #include "bnf.h" #include "cppbnf.h" #include "grammer.h" #include "minicc.h" #include "debug.h" #include #include // 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 } namespace { std::vector sourceToCharTokens(const std::string& code) { std::vector result; Location location{1, 1}; for (char c: code) { if (c == '\n') { location.column = 1; location.line++; } else if (std::isprint(c)) { location.column++; } result.emplace_back(Token{std::string(1, c), std::string(1, c), location}); } return result; } } // Phase 3: Parse preprocessing tokens void CPP::preprocessing_tokenize(const std::string& s) { auto charTokens {sourceToCharTokens(s)}; auto bnf{SubBNF(GetCppBNFLex(), "preprocessing-token")}; // add to bnf to match whole file bnf["file"] = { {"preprocessing-token-list"}, {"whitespace-list", "preprocessing-token-list"} }; bnf["preprocessing-token-list"] = { {"preprocessing-token-padded"}, {"preprocessing-token-list", "preprocessing-token-padded"} }; bnf["preprocessing-token-padded"] = { {"preprocessing-token"}, {"preprocessing-token", "whitespace-list"} }; bnf["whitespace-list"] = { {"whitespace-char"}, {"whitespace-list", "whitespace-char" } }; bnf["whitespace-char"] = { {" "}, {"\t"}, {"\n"}, {"\r"} }; Gram::Compiler compiler(bnf, "file"); debug = true; auto Tree = compiler.compile(charTokens); } // 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 } // Phase 7: Create tokens from preprocessing tokens void CPP::tokens_from_pptokens() { // TODO } void CPP::PreprocessorTokensToTokens(std::vector& tokens) { for (auto& i : tokens) { if (i.type == "preprocessing-op-or-punc") i.type = i.value; } } // 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) { source_charset_map(); backslash_escape(); preprocessing_tokenize(code); preprocess(); execution_charset_map(); concatenate_strings(); tokens_from_pptokens(); instantiate(); link(); } class CppTest: public ::testing::Test { protected: CppTest() { debug = false; } ~CppTest() { } }; TEST_F(CppTest, preprocessing_tokenize) { CPP::preprocessing_tokenize("int main() { return 1; }"); } #if 0 TEST(Cpp, translate) { CPP::translate(); } #endif