diff options
| author | Roland Reichwein <mail@reichwein.it> | 2020-02-16 17:33:12 +0100 | 
|---|---|---|
| committer | Roland Reichwein <mail@reichwein.it> | 2020-02-16 17:33:12 +0100 | 
| commit | 142194b90d444d988890f9578a24b5d6094ddab0 (patch) | |
| tree | df70ccce1f79432c5ec4b78842a6133a71605aee | |
| parent | 6340b97a4fc435d838262ed25cee9566fea7da4c (diff) | |
Translation phase 3: preprocessing tokens (WIP)
| -rw-r--r-- | cpp.cpp | 65 | ||||
| -rw-r--r-- | cpp.h | 4 | ||||
| -rw-r--r-- | cppbnf.h | 6 | 
3 files changed, 61 insertions, 14 deletions
| @@ -1,16 +1,13 @@  #include "cpp.h" +#include "bnf.h" +#include "cppbnf.h" +#include "grammer.h" +#include "minicc.h" +  #include <gtest/gtest.h>  #include <gmock/gmock.h> -void CPP::PreprocessorTokensToTokens(std::vector<Token>& 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<Token> sourceToCharTokens(const std::string& code) +{ + std::vector<Token> 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<Token>& 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 @@ -11,7 +11,7 @@ void PreprocessorTokensToTokens(std::vector<Token>& 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(); | 
