summaryrefslogtreecommitdiffhomepage
path: root/cpp.cpp
blob: 2f65a191dbb890bd03a806a8a326827fdb5f4a64 (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
#include "cpp.h"

#include "bnf.h"
#include "cppbnf.h"
#include "grammer.h"
#include "minicc.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")};
 Gram::Compiler compiler(bnf, "preprocessing-token");
 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();
}

TEST(Cpp, preprocessing_tokenize) {
 CPP::preprocessing_tokenize("int main() { return 1; }");
}

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