summaryrefslogtreecommitdiffhomepage
path: root/cpp.cpp
blob: 3d8c20da605d18b7e6a365b6c781c916f4cc5469 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "cpp.h"

#include "bnf.h"
#include "cppbnf.h"
#include "grammer.h"
#include "minicc.h"
#include "debug.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <unordered_set>

using namespace Gram;

CPP::CPP(){}

CPP::~CPP(){}

// Phase 1: Map physical character set to basic source character set
void CPP::source_charset_map()
{
 // TODO
}

// Phase 2: Escape backslashed line endings
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
std::pair<index_t, std::vector<TreeNode>> CPP::preprocessing_tokenize(const std::string& s)
{
 m_code = s;

 m_charTokens = sourceToCharTokens(s);

 auto bnf{SubBNF(GetCppBNFLex(), "preprocessing-token")};

 // add to bnf to match whole file
 bnf["file"] = {
  {"preprocessing-token-list"},
  {"whitespace-list", "preprocessing-token-list"}
 };
 bnf["preprocessing-token-list"] = {
  {"preprocessing-token-padded"},
  {"preprocessing-token-list", "preprocessing-token-padded"}
 };
 bnf["preprocessing-token-padded"] = {
  {"preprocessing-token"},
  {"preprocessing-token", "whitespace-list"}
 };
 bnf["whitespace-list"] = {
  {"whitespace-char"},
  {"whitespace-list", "whitespace-char" }
 };
 bnf["whitespace-char"] = {
  {" "}, {"\t"}, {"\n"}, {"\r"}
 };
 Gram::Compiler compiler(bnf, "file");
 debug = true;
 std::pair<index_t, std::vector<TreeNode>> Tree = compiler.compile(m_charTokens);

 debug = true;
 compiler.DumpTree();

 return Tree;
}

// Phase 4: Preprocessing
void CPP::preprocess()
{
 // TODO
}

// Phase 5: Map chars and strings to execution charset
void CPP::execution_charset_map()
{
 // TODO
}

// Phase 6: Concatenate adjacent string literals
void CPP::concatenate_strings()
{
 // TODO
}

std::string CPP::valueOfNode(index_t node_index, const std::vector<TreeNode>& Tree)
{
 std::string result;

 std::vector<int32_t> todo(1, int32_t(node_index));

 while (!todo.empty()) {
  int32_t current_index = todo.back();
  todo.pop_back();

  // visit node if token
  if (ChildIdIsToken(current_index)) {
   result += m_code[TokenIdFromChildId(current_index)];
  } else {

   const TreeNode &node{Tree[current_index]};

   // iterate backwards in childs, to get depth-first search in tree, from the beginning
   std::for_each(node.child_ids.rbegin(), node.child_ids.rend(), [&](int32_t child){
    todo.push_back(child);
   });
  }
 }

 return result;
};

namespace {
 std::unordered_set<std::string> pp_types{
  "identifier",
  "pp-number",
  "character-literal",
  "user-defined-character-literal",
  "string-literal",
  "user-defined-string-literal",
  "preprocessing-op-or-punc"
 };
}

// Phase 7.a: Create tokens from preprocessing tokens
std::vector<Token> CPP::tokens_from_pptokens(std::pair<index_t, std::vector<TreeNode>> Tree)
{
 std::vector<Token> result;

 // "identifier" + value -> "identifier" + value, except identifiers from table 5.11, p.14 -> keyword as value, value
 // "pp-number" + value -> "literal" + value
 // "character-literal" -> "literal" + value
 // "user-defined-character-literal" -> "literal" + value
 // "string-literal" -> "literal" + value
 // "user-defined-string-literal" -> "literal" + value
 // "preprocessing-op-or-punc" -> value+value (operator,punctuator)
 
 // TODO: traverse Tree, creating Token list
 std::vector<index_t> todo(1, index_t(Tree.first));

 while (!todo.empty()) {
  index_t current_index = todo.back();
  todo.pop_back();

  TreeNode &node{Tree.second[current_index]};

  // visit node
  if (pp_types.find(node.type) != pp_types.end()) { // TODO
   std::cout << node.type << ": " << valueOfNode(current_index, Tree.second) << std::endl;
  } else { // only traverse further if not handled

   // iterate backwards in childs, to get depth-first search in tree, from the beginning
   std::for_each(node.child_ids.rbegin(), node.child_ids.rend(), [&](int32_t child){
    if (!ChildIdIsToken(child))
     todo.push_back(child);
   });
  }
 }
 
 return result;
}

// Phase 7.b: Grammar Analysis
std::pair<index_t, std::vector<Gram::TreeNode>> analysis(std::vector<Token>)
{
 return {0 , {}};
}

// Phase 7.c: Translate
void CPP::translate()
{
 // TODO
}

// 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(const std::string& code)
{
#if 0 // fix signatures!
 source_charset_map();
 backslash_escape();
 preprocessing_tokenize(code);
 preprocess();
 execution_charset_map();
 concatenate_strings();
 tokens_from_pptokens();
  analysis();
  translate();
 instantiate();
 link();
#endif
}

class CppTest: public ::testing::Test
{
protected:
 CppTest() {
  //debug = true;
 }
 ~CppTest() {
 }
};

#if 0
TEST_F(CppTest, preprocessing_tokenize) {
 CPP cpp;
 auto ppTree = cpp.preprocessing_tokenize("int main() { return 1; }");

 cpp.tokens_from_pptokens(ppTree);
}
#endif

#if 0
TEST_F(CppTest, preprocessing_tokenize2) {
 CPP cpp;
 auto ppTree = cpp.preprocessing_tokenize("in ma");

 cpp.tokens_from_pptokens(ppTree);
}
#endif

#if 0
TEST(Cpp, translate) {
 CPP::translate();
}
#endif