summaryrefslogtreecommitdiffhomepage
path: root/cpp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp.cpp')
-rw-r--r--cpp.cpp65
1 files changed, 53 insertions, 12 deletions
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 <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