From 142194b90d444d988890f9578a24b5d6094ddab0 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 16 Feb 2020 17:33:12 +0100 Subject: Translation phase 3: preprocessing tokens (WIP) --- cpp.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 12 deletions(-) (limited to 'cpp.cpp') diff --git a/cpp.cpp b/cpp.cpp index 54dd0d2..2f65a19 100644 --- a/cpp.cpp +++ b/cpp.cpp @@ -1,16 +1,13 @@ #include "cpp.h" +#include "bnf.h" +#include "cppbnf.h" +#include "grammer.h" +#include "minicc.h" + #include #include -void CPP::PreprocessorTokensToTokens(std::vector& tokens) -{ - for (auto& i : tokens) { - if (i.type == "preprocessing-op-or-punc") - i.type = i.value; - } -} - // Phase 1: Map physical character set to basic source character set void CPP::source_charset_map() { @@ -23,10 +20,37 @@ 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() +void CPP::preprocessing_tokenize(const std::string& s) { - // TODO + auto charTokens {sourceToCharTokens(s)}; + + auto bnf{SubBNF(GetCppBNFLex(), "preprocessing-token")}; + Gram::Compiler compiler(bnf, "preprocessing-token"); + auto Tree = compiler.compile(charTokens); } // Phase 4: Preprocessing @@ -50,24 +74,35 @@ void CPP::concatenate_strings() // 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() +void CPP::translate(const std::string& code) { source_charset_map(); backslash_escape(); - preprocessing_tokenize(); + preprocessing_tokenize(code); preprocess(); execution_charset_map(); concatenate_strings(); @@ -76,6 +111,12 @@ void CPP::translate() link(); } +TEST(Cpp, preprocessing_tokenize) { + CPP::preprocessing_tokenize("int main() { return 1; }"); +} + +#if 0 TEST(Cpp, translate) { CPP::translate(); } +#endif -- cgit v1.2.3