summaryrefslogtreecommitdiffhomepage
path: root/cpp.cpp
blob: 8526cffbdf60c3eb6aa9181e41b0c01af0c8e46b (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
#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>

// 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
void CPP::preprocessing_tokenize(const std::string& s)
{
 auto 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;
 auto Tree = compiler.compile(charTokens);
}

// 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
}

// Phase 7: Create tokens from preprocessing tokens
void CPP::tokens_from_pptokens()
{
 // TODO
}

void CPP::PreprocessorTokensToTokens(std::vector<Token>& tokens)
{
 for (auto& i : tokens) {
  if (i.type == "preprocessing-op-or-punc")
   i.type = i.value;
 }
}

// 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)
{
 source_charset_map();
 backslash_escape();
 preprocessing_tokenize(code);
 preprocess();
 execution_charset_map();
 concatenate_strings();
 tokens_from_pptokens();
 instantiate();
 link();
}

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

TEST_F(CppTest, preprocessing_tokenize) {
 CPP::preprocessing_tokenize("int main() { return 1; }");
}

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