summaryrefslogtreecommitdiffhomepage
path: root/cpp.h
blob: 2594a4c92f75388621238d5e818268a9950f7c34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once

#include "grammer.h"
#include "minicc.h"

#include <any>
#include <deque>
#include <memory>
#include <vector>

struct CPPContext {
 // global variable declarations
 // global variable definitions
 // functions declarations
 // functions definitions
};

class CPP {

public:

 CPP();
 ~CPP();

 // phases of translation, according to standard
 void source_charset_map(); // phase 1
 void backslash_escape(); // phase 2
 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(const std::vector<Token>& pp_tokens); // phase 7.a
 std::vector<Gram::TreeNode> analysis(const std::vector<Token>&); // 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& code);

 std::vector<uint8_t> getCode();
 std::vector<uint8_t> getData();

private:
 std::string m_code; // input from compile()
 std::vector<Token> m_tokens; // result of phase 7.a
 std::vector<Gram::TreeNode> 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, const std::vector<std::string>& pattern) const; ///< returns true iff specified type list matches; "" -> don't care

 std::deque<std::shared_ptr<std::any>> mValues; // values stack during phase 7.c

 std::unordered_map<std::string, std::function<std::shared_ptr<std::any>(index_t)>> getNodeEvalMap();
 std::unordered_map<std::string, std::function<std::shared_ptr<std::any>(index_t)>> node_eval_map;

 CPPContext mContext;
 void getValueOfToken(index_t index);
 void getValueOfNode(index_t index);
 void visitRecursive(index_t node_id);
};