#pragma once #include "asm/segment.h" #include "flowgraph/graph.h" #include "grammer.h" #include "minicc.h" #include #include #include #include #include struct CPPContext { // global variable declarations // global variable definitions // functions declarations // functions definitions FlowGraph::Graph graph; }; class CPP { public: CPP(); ~CPP(); // phases of translation, according to standard void source_charset_map(); // phase 1 void backslash_escape(); // phase 2 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(const std::vector& pp_tokens); // phase 7.a std::vector analysis(const std::vector&); // phase 7.b void translate(); // phase 7.c void instantiate(); // phase 8 void link(); // phase 9 // all phases of translation void compile(const std::string& source); std::vector getCode(); std::vector getData(); private: std::string m_source; // input from compile() std::vector m_tokens; // result of phase 7.a std::vector m_nodes; // result of phase 7.b std::string valueOfNode(index_t node_index) const; std::string typeOfNode(index_t node_index) const; std::string locationOfNode(index_t node_index) const; ///< Empty if no location available void compileError(index_t node_id, const std::string& msg) const; std::string typeOfChild(int32_t child_id) const; bool childTypesOfNodeMatch(index_t index, const std::vector& pattern) const; ///< returns true iff specified type list matches; "" -> don't care bool childTypesOfChildMatch(index_t index, index_t child_index, const std::vector& pattern) const; ///< returns true iff specified type list matches in specified child; "" -> don't care std::deque mValues; // values stack during phase 7.c std::any getValue(index_t node_id, index_t child_id); std::string getType(index_t node_id, index_t child_index); std::string ruleString(index_t node_id); std::unordered_map> getNodeEvalMap(); std::unordered_map> node_eval_map; CPPContext mCPPContext; // intermediate data of phase 7.c void getValueOfToken(index_t index); void getValueOfNode(index_t index); void visitRecursive(index_t node_id); FlowGraph::Graph BinaryOperation(index_t index); // index: node // phase 8: instantiate: instantiate templates; flowgraph->asm Segment mSegment; // phase 9: link std::vector mCode; };