From 2eb2383387d16fc919c07e1a6b9211406576b893 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 29 Mar 2020 20:15:52 +0200 Subject: Fix compiler structure --- cpp.cpp | 15 +++++++++------ cpp.h | 8 ++++---- grammer.cpp | 2 +- mcc.cpp | 2 +- 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 CPP::tokens_from_pptokens(std::vector pp_tokens) +std::vector CPP::tokens_from_pptokens(const std::vector& pp_tokens) { std::vector result; @@ -213,7 +213,7 @@ std::vector CPP::tokens_from_pptokens(std::vector pp_tokens) } // Phase 7.b: Grammar Analysis -std::vector CPP::analysis(std::vector tokens) +std::vector CPP::analysis(const std::vector& tokens) { auto bnf = SubBNF(CPPBNF::GetCppBNFGram(), "translation-unit"); @@ -223,9 +223,12 @@ std::vector CPP::analysis(std::vector tokens) } // Phase 7.c: Translate -void CPP::translate() +void CPP::translate(const std::vector& 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 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 tokens_from_pptokens(std::vector pp_tokens); // phase 7.a -std::vector analysis(std::vector); // phase 7.b -void translate(); // phase 7.c +std::vector tokens_from_pptokens(const std::vector& pp_tokens); // phase 7.a +std::vector analysis(const std::vector&); // phase 7.b +void translate(const std::vector& 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 getCode(); std::vector 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 Compiler::compile(std::vector 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(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) { -- cgit v1.2.3