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 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------ cpp.h | 4 ++-- cppbnf.h | 6 ++++++ 3 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 cppbnf.h 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 diff --git a/cpp.h b/cpp.h index 7bb62c2..7388e94 100644 --- a/cpp.h +++ b/cpp.h @@ -11,7 +11,7 @@ void PreprocessorTokensToTokens(std::vector& tokens); // phases of translation, according to standard void source_charset_map(); // phase 1 void backslash_escape(); // phase 2 -void preprocessing_tokenize(); // phase 3 +void preprocessing_tokenize(const std::string& s); // phase 3 void preprocess(); // phase 4 void execution_charset_map(); // phase 5 void concatenate_strings(); // phase 6 @@ -20,6 +20,6 @@ void instantiate(); // phase 8 void link(); // phase 9 // all phases of translation -void translate(); +void translate(const std::string& code); } diff --git a/cppbnf.h b/cppbnf.h new file mode 100644 index 0000000..384e6cf --- /dev/null +++ b/cppbnf.h @@ -0,0 +1,6 @@ +#pragma once + +#include "bnf.h" + +BNF GetCppBNFLex(); +BNF GetCppBNFGram(); -- cgit v1.2.3