NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
CodeGenerator.cxx
Go to the documentation of this file.
1//***************************************************************************************
2//
6//
7//***************************************************************************************
8#include "NAGASH/Global.h"
9
10std::vector<std::vector<std::string>> Token;
11std::vector<std::vector<std::vector<std::string>>> TokenReplacement;
12std::string line;
13std::string Code{""};
14
15void trim_back(std::string &s)
16{
17 s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch)
18 { return !std::isspace(ch); })
19 .base(),
20 s.end());
21}
22
23std::size_t replace_all(std::string &inout, std::string_view what, std::string_view with)
24{
25 std::size_t count{};
26 for (std::string::size_type pos{};
27 inout.npos != (pos = inout.find(what.data(), pos, what.length()));
28 pos += with.length(), ++count)
29 {
30 inout.replace(pos, what.length(), with.data(), with.length());
31 }
32 return count;
33}
34
35void nestedforloop(size_t layer, std::string newcode, std::ofstream &out)
36{
37 if (layer == 0)
38 newcode = Code;
39 ++layer;
40
41 size_t replacementsize = TokenReplacement[layer - 1].size();
42 for (size_t i = 0; i < replacementsize; ++i)
43 {
44 std::string nnewcode = newcode;
45 for (size_t j = 0; j < Token[layer - 1].size(); j++)
46 {
47 // std::cout << TokenReplacement[layer - 1][i][j] << " ";
48 replace_all(nnewcode, Token[layer - 1][j], TokenReplacement[layer - 1][i][j]);
49 }
50
51 if (layer != Token.size())
52 nestedforloop(layer, nnewcode, out);
53 else
54 out << nnewcode;
55 }
56}
57
58int main(int argc, char **argv)
59{
60 if (argc != 3)
61 {
62 std::cout << "Usage: " << argv[0] << " (input file) (output file)" << std::endl;
63 return 0;
64 }
65
66 std::ifstream infile;
67 infile.open(argv[1]);
68 if (!infile.is_open())
69 {
70 std::cout << "Error: the input file is corrupted" << std::endl;
71 return 0;
72 }
73
74 bool ReadCode = false;
75 bool ReadReplacement = false;
76 size_t linecount = 0;
77 while (std::getline(infile, line))
78 {
79 ++linecount;
80
81 // remove all the trailing spaces, tabs, and carriage returns
83
84 if (line.size() == 0)
85 continue;
86
87 if (line.back() == ':')
88 {
89 std::string tokens = line;
90 tokens.pop_back();
91
92 // Parse the tokens
93 std::regex word_regex("([^\\s]+)");
94 auto words_begin = std::sregex_iterator(tokens.begin(), tokens.end(), word_regex);
95 auto words_end = std::sregex_iterator();
96 std::vector<std::string> matched_words;
97 for (auto i = words_begin; i != words_end; ++i)
98 {
99 matched_words.push_back((*i).str());
100 }
101
102 if (matched_words.size() == 0)
103 {
104 std::cout << "Warning: no tokens found in line " << linecount << std::endl;
105 continue;
106 }
107
108 if (matched_words.size() == 1 && matched_words[0] == "Code")
109 {
110 ReadCode = true;
111 ReadReplacement = false;
112 }
113 else
114 {
115 Token.emplace_back(matched_words);
116 TokenReplacement.emplace_back(std::vector<std::vector<std::string>>());
117
118 ReadReplacement = true;
119 ReadCode = false;
120 }
121
122 continue;
123 }
124
125 if (ReadCode)
126 Code.append(line + "\n");
127
128 if (ReadReplacement)
129 {
130 // Parse the replacements
131 std::regex word_regex("([^\\s]+)");
132 auto words_begin = std::sregex_iterator(line.begin(), line.end(), word_regex);
133 auto words_end = std::sregex_iterator();
134 std::vector<std::string> vreplacement;
135 for (auto i = words_begin; i != words_end; ++i)
136 {
137 vreplacement.push_back((*i).str());
138 }
139
140 if (vreplacement.size() > Token.back().size())
141 {
142 std::cout << "Warning: more replacements than needed in line " << linecount << std::endl;
143 for (size_t i = 0; i < vreplacement.size() - Token.back().size(); i++)
144 vreplacement.pop_back();
145 }
146
147 if (vreplacement.size() < Token.back().size())
148 {
149 std::cout << "Warning: less replacements than needed in line " << linecount << std::endl;
150 for (size_t i = 0; i < Token.back().size() - vreplacement.size(); i++)
151 vreplacement.emplace_back("");
152 }
153
154 TokenReplacement.back().emplace_back(vreplacement);
155 }
156 }
157
158 infile.close();
159
160 /*
161 std::cout << Code << std::endl;
162
163 for (size_t i = 0; i < Token.size(); i++)
164 {
165 for (size_t j = 0; j < Token[i].size(); j++)
166 std::cout << Token[i][j] << " ";
167 std::cout << std::endl;
168 }
169
170 for (size_t i = 0; i < TokenReplacement.size(); i++)
171 {
172 for (size_t j = 0; j < TokenReplacement[i].size(); j++)
173 {
174 for (size_t k = 0; k < TokenReplacement[i][j].size(); k++)
175 std::cout << TokenReplacement[i][j][k] << " ";
176 std::cout << std::endl;
177 }
178 }
179 */
180
181 std::ofstream outfile;
182 outfile.open(argv[2]);
183 nestedforloop(0, "", outfile);
184 outfile.close();
185
186 return 0;
187}
std::size_t replace_all(std::string &inout, std::string_view what, std::string_view with)
int main(int argc, char **argv)
std::vector< std::vector< std::vector< std::string > > > TokenReplacement
std::string line
std::vector< std::vector< std::string > > Token
void nestedforloop(size_t layer, std::string newcode, std::ofstream &out)
std::string Code
void trim_back(std::string &s)
Some global definitions.