summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-03-29 20:15:52 +0200
committerRoland Reichwein <mail@reichwein.it>2020-03-29 20:15:52 +0200
commit2eb2383387d16fc919c07e1a6b9211406576b893 (patch)
tree83e5c5feb3b337552590d1ce7a4730d91081327f
parent12c12ebbdd3f843c87ba12c29727003f1f78a7ff (diff)
Fix compiler structure
-rw-r--r--cpp.cpp15
-rw-r--r--cpp.h8
-rw-r--r--grammer.cpp2
-rw-r--r--mcc.cpp2
4 files changed, 15 insertions, 12 deletions
diff --git a/cpp.cpp b/cpp.cpp
index 73f11e1..ea40661 100644
--- a/cpp.cpp
+++ b/cpp.cpp
@@ -182,7 +182,7 @@ namespace {
}
// Phase 7.a: Create tokens from preprocessing tokens
-std::vector<Token> CPP::tokens_from_pptokens(std::vector<Token> pp_tokens)
+std::vector<Token> CPP::tokens_from_pptokens(const std::vector<Token>& pp_tokens)
{
std::vector<Token> result;
@@ -213,7 +213,7 @@ std::vector<Token> CPP::tokens_from_pptokens(std::vector<Token> pp_tokens)
}
// Phase 7.b: Grammar Analysis
-std::vector<Gram::TreeNode> CPP::analysis(std::vector<Token> tokens)
+std::vector<Gram::TreeNode> CPP::analysis(const std::vector<Token>& tokens)
{
auto bnf = SubBNF(CPPBNF::GetCppBNFGram(), "translation-unit");
@@ -223,9 +223,12 @@ std::vector<Gram::TreeNode> CPP::analysis(std::vector<Token> tokens)
}
// Phase 7.c: Translate
-void CPP::translate()
+void CPP::translate(const std::vector<Gram::TreeNode>& tree)
{
- // TODO
+ if (tree.size() == 0)
+ throw std::runtime_error("ICE: Tree is empty");
+
+ //traverse(i, );
}
// Phase 8: Instantiate objects
@@ -241,7 +244,7 @@ void CPP::link()
}
// phases of translation, according to standard
-void CPP::translate(const std::string& code)
+void CPP::compile(const std::string& code)
{
source_charset_map();
@@ -257,7 +260,7 @@ void CPP::translate(const std::string& code)
auto tokens = tokens_from_pptokens(pp_tokens);
auto nodes = analysis(tokens);
- translate();
+ translate(nodes);
instantiate();
diff --git a/cpp.h b/cpp.h
index 467b268..571182a 100644
--- a/cpp.h
+++ b/cpp.h
@@ -21,14 +21,14 @@ std::vector<Token> preprocessing_tokenize(const std::string& s); // phase 3
void preprocess(); // phase 4
void execution_charset_map(); // phase 5
void concatenate_strings(); // phase 6
-std::vector<Token> tokens_from_pptokens(std::vector<Token> pp_tokens); // phase 7.a
-std::vector<Gram::TreeNode> analysis(std::vector<Token>); // phase 7.b
-void translate(); // phase 7.c
+std::vector<Token> tokens_from_pptokens(const std::vector<Token>& pp_tokens); // phase 7.a
+std::vector<Gram::TreeNode> analysis(const std::vector<Token>&); // phase 7.b
+void translate(const std::vector<Gram::TreeNode>& tree); // phase 7.c
void instantiate(); // phase 8
void link(); // phase 9
// all phases of translation
-void translate(const std::string& code);
+void compile(const std::string& code);
std::vector<uint8_t> getCode();
std::vector<uint8_t> getData();
diff --git a/grammer.cpp b/grammer.cpp
index 40775ef..8b6f01b 100644
--- a/grammer.cpp
+++ b/grammer.cpp
@@ -313,7 +313,7 @@ std::vector<TreeNode> Compiler::compile(std::vector<Token> p_tokens)
if (!match(m_top, 0, tokens.size()))
throw std::runtime_error("Compile error");
- //DumpTree();
+ DumpTree();
return nodes;
}
diff --git a/mcc.cpp b/mcc.cpp
index fa9b0b8..7d94cf8 100644
--- a/mcc.cpp
+++ b/mcc.cpp
@@ -38,7 +38,7 @@ int main(int argc, char* argv[])
std::string unit_string(reinterpret_cast<char*>(unit.data()), unit.size());
- cpp.translate(unit_string);
+ cpp.compile(unit_string);
Elf::Write(out_filename, cpp.getCode(), cpp.getData());
} catch (const std::exception& ex) {