-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathepp.cpp
More file actions
324 lines (320 loc) · 14.7 KB
/
epp.cpp
File metadata and controls
324 lines (320 loc) · 14.7 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* @file epp.cpp
* @author PowerAngelXd (692732163@qq.com)
* @brief main file
* @date 2022-03-15
*
* @copyright Copyright (c) PowerAngelXd
*
*/
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <stdio.h>
#include <unistd.h>
#include <cstring>
#include "lexer/eplex.h"
#include "parser/parser.h"
#include "eppack/error/epperr.h"
#include "eppack/tools/configloader.h"
#ifdef _WIN32
#include "eppack/global/elib.h"
#endif
//#define EPP_DEBUG
#ifdef _WIN32
void getAllFile(std::string path, std::vector<std::string>& files){
long hFile = 0;
struct _finddata_t fileinfo;
std::string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1){
do{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
#endif
#ifdef EPP_DEBUG
int main(){
#else
inline void epp_cli(){
#endif
parser::Parser p;
configloader::Loader loader("resource/config/common.ecfg");
int code = 10;
std::string cmd;
char work_path[256];
std::wcout<<L"Eytion++ [ Build Time:"<<__DATE__<<" "<<__TIME__<<"]\nCopyright (c) PowerAngelXd\nEytion PlusPlus version: dev-0.2.9\nyou can type 'help' to get 'Eytion++Cli & Eytion++Grammar' help document\n"<<std::endl;
while(true){
if(code == 0) break;
try{
getcwd(work_path, 256);
if(loader.getData().cli_nameshow == true){
std::cout<<"[Epp Cli]";
}
if(loader.getData().cli_workspace == true){
std::cout<<work_path;
}
std::cout<<"> ";
std::getline(std::cin, cmd);
if(cmd == "exit" || cmd == ":e") {
if(loader.getData().cli_exittip == true){
std::cout<<"Do you really want to quit this program? (you can turn off the 'cli-exittip' option in the configuration file and don't see this prompt again)"<<std::endl;
std::cout<<"yes[y/other keys] no[n/N]"<<std::endl;
std::cout<<"> ";
char k = getchar();
if(k == 'n' || k == 'N');
else code = 0;
}
else code = 0;
}
else if(cmd == "ls"){
#ifdef _WIN32
std::vector<std::string> files;
std::string crt_path;
std::cout<<"================================================="<<std::endl;
for(int i = 0; i < strlen(work_path); i++){
crt_path.push_back(work_path[i]);
}
getAllFile(crt_path, files);
for(auto file: files) std::cout<<file<<std::endl;
std::cout<<"================================================="<<std::endl;
#elif __linux__
system("ls");
#endif
}
else if(cmd == "envc"){
parser::Parser _p;
p = _p;
}
else if(cmd == "redcfg"){
loader = configloader::Loader("resource/config/common.ecfg");
std::cout<<"\033[42;37mThe configuration file was successfully reloaded!"<<"\033[0m"<<std::endl;
}
else if(cmd == "help"){
std::string help = "Eytion++ Cli Help Dcoument\n"
"Commands:\n"
"exit/:e Exit the Eytion++ Cli\n"
"envc Reset the Eytion++Runtime Environment\n"
"help View Eytion++ HelpDocument\n"
"run <file> Run a epp-file named 'file'\n"
"view <file> View a file named 'file'\n"
"-------------------------------------------------\n"
"Eytion++ Grammar:\n"
"addop ::= '+'|'-'\n"
"mulop ::= '/'|'*'|'%'\n"
"siadexpr ::= (identifier) '++' | '--' (identifier)\n"
"primexpr ::= number|identifier|string|char|boolconst|typeof|input|siadexpr\n"
"mulexpr ::= primexpr (mulop primexpr)*\n"
"addexpr ::= addexpr (addop addexpr)*\n"
"expr ::= addexpr | boolexpr | listexpr\n"
"typeof ::= 'typeof' '(' expr ')'\n"
"input ::= 'input' '(' string ')'\n"
"\n"
"stmt ::= outstmt|vorcstmt|assignstmt|deletestmt|blockstmt|ifstmt|whilestmt|repeatstmt|breakstmt\n"
"outstmt ::= 'out' expr ';'\n"
"vorcstmt ::= 'var'|'const' identifier (':' type) '=' expr ';'\n"
"assignstmt ::= identifier '=' expr ';'\n"
"deletestmt ::= 'delete' identifier ';'\n"
"blockstmt ::= '{' (stmt)* '}'\n"
"ifstmt ::= 'if' '(' boolexpr ')' blockstmt|stmt\n"
"elsestmt ::= 'else' blockstmt\n"
"whilestmt ::= 'while' '(' boolexpr ')' blockstmt|stmt\n"
"repeatstmt ::= 'repeat' '(' addexpr ')' blockstmt|stmt\n"
"breakstmt ::= 'break' ';'\n"
"for_each_stmt ::= 'for_each' '(' 'var' identifier ':' identifier ')' blockstmt";
std::cout<<help<<std::endl;
}
else if(cmd.find("run") == 0){
std::string cut;
cut.assign(cmd.begin() + 4, cmd.end());
std::ifstream file(cut);
std::size_t index = cut.find(".epp", cut.size() - ((std::string)".epp").size()); // file suffix check
if(index == std::string::npos) throw "The file must be in \".epp\" format";
if(file.fail()) throw "The specified file could not be found";
else{
epplex::Lexer lexer(file);
auto tokens = lexer.getTokenGroup();
east::astParser ast(tokens);
east::StatNode* node = ast.gen_statNode();
std::ofstream afile("debug/ast/ast.edebug");
afile<<node->to_string()<<std::endl;
afile.close();
p.stat = *node;
p.parse();
parser::Parser _p; // 避免污染环境,故清空
p = _p;
std::cout<<std::endl;
code = 1;
}
parser::Parser new_p;
p = new_p;
}
else if(cmd.find("view") == 0){
std::string cut;
cut.assign(cmd.begin() + 5, cmd.end());
std::ifstream file(cut);
std::istreambuf_iterator<char> begin(file);
std::istreambuf_iterator<char> end;
std::string str(begin, end);
std::cout<<std::endl<<"view file: "<<cut<<"\n"<<str<<std::endl;
}
else if(cmd.find("cd") == 0){
std::string cut;
cut.assign(cmd.begin() + 3, cmd.end());
chdir(cut.c_str());
}
else {
std::ofstream tfile("debug/tokens/tokens.edebug"); // 声明一个tfile,用于读写debug文件
std::stringstream ss(cmd); // 用于接收数据
epplex::Lexer lexer(ss); // 构造一个lexer
//std::cout<<ss.str()<<std::endl;
auto tokens = lexer.getTokenGroup(); // 获取tokens
std::vector<std::string> tg_msg;
for(auto token:tokens){tg_msg.push_back(token.format());}
for(auto msg:tg_msg){tfile<<msg<<std::endl;}
tfile.close();
if(loader.getData().debug_echotokeng == true) {
std::cout<<"DebugMode: TokenGroups\n";
for(auto token:tokens){std::cout<<token.format()<<",";}
std::cout<<std::endl;
}
east::astParser ast(tokens);
if(east::ValExprNode::is(ast)&&cmd[cmd.size() - 1] != ';'){
east::ValExprNode* repl_node = ast.gen_valExprNode();
if(loader.getData().debug_echoast == true){
std::cout<<"DebugMode: Ast\n";
std::cout<<repl_node->to_string()<<std::endl;
}
cenv::Calculation calc(p.sset);
cvisitor::visitor v;
//std::cout<<repl_node->to_string()<<std::endl;
if(repl_node->addexpr != nullptr)
v.visitAddExpr(repl_node->addexpr);
else if(repl_node->boolexpr != nullptr)
v.visitBoolExpr(repl_node->boolexpr);
else if(repl_node->listexpr != nullptr)
v.visitListExpr(repl_node->listexpr);
calc.ins = v.ins; calc.constpool = v.constpool;
calc.run();
p.sset = calc.sset;
if (calc.isArray()){
if (calc.result[0].first == "__STRING__"){
for (int i = 0; i < calc.result.size(); i++){
std::cout << calc.constpool[calc.result[i].second];
}
}
else if (calc.result[0].first == "__CHAR__"){
for (int i = 0; i < calc.result.size(); i++){
std::cout << calc.constpool[calc.result[i].second];
}
}
else{
for (int i = 0; i < calc.result.size(); i++){
std::cout << calc.result[i].second;
}
}
}
else{
if (calc.result[0].first == "__STRING__")
std::cout << calc.constpool[calc.result[0].second];
else if (calc.result[0].first == "__CHAR__")
std::cout << calc.constpool[calc.result[0].second];
else
std::cout << calc.result[0].second;
}
std::cout<<std::endl;
}
else{
east::StatNode* node = ast.gen_statNode();
if(loader.getData().debug_echoast == true){
std::cout<<"DebugMode: Ast\n";
std::cout<<node->to_string()<<std::endl;
}
std::ofstream afile("debug/ast/ast.edebug");
afile<<node->to_string()<<std::endl;
afile.close();
p.stat = *node;
p.parse();
}
std::cout<<std::endl;
code = 1;
}
}
catch(char const* e){
std::cout<<"Eytion++ Error: "<<e<<std::endl;
}
catch(std::string e){
std::cout<<"Eytion++ Error: "<<e<<std::endl;
}
catch(epperr::Epperr eppe){
if(loader.getData().cli_errorhighlight == true) std::cout<<"\033[41;37m";
std::cout<<eppe.what()<<"\033[0m"<<std::endl;
}
catch(epperr::EppClierr eppce){
if(loader.getData().cli_errorhighlight == true) std::cout<<"\033[41;37m";
std::cout<<eppce.what()<<"\033[0m"<<std::endl;
}
}
}
#ifndef EPP_DEBUG
int main(int argc, char *argv[]){
configloader::Loader loader("resource/config/common.ecfg");
std::string cmd;
if(argc >= 2){
if(strcmp(argv[1], "-v")==0 || strcmp(argv[1], "-version")==0) std::cout<<"EytionPlusPlus version => dev-0.2.9\nCli version => V1.0"<<std::endl;
else if(strcmp(argv[1], "-r")==0 || strcmp(argv[1], "-run")==0){
std::ifstream file(argv[2]);
std::size_t index = ((std::string)argv[2]).find(".epp", ((std::string)argv[2]).size() - ((std::string)".epp").size()); // file suffix check
if(index == std::string::npos) {std::cout<<"error: The file must be in \".epp\" format"<<std::endl; goto end;}
if(file.fail()) {std::cout<<"error: The specified file could not be found"<<std::endl; goto end;}
else{
try{
epplex::Lexer lexer(file);
//std::cout<<ss.str()<<std::endl;
auto tokens = lexer.getTokenGroup();
//for(auto token:tokens){std::cout<<token.format()<<std::endl;}
east::astParser ast(tokens);
east::StatNode* node = ast.gen_statNode();
std::ofstream afile("debug/ast/ast.edebug");
afile<<node->to_string()<<std::endl;
afile.close();
std::cout<<node->to_string()<<std::endl;
parser::Parser p;
p.stat = *node;
p.parse();
std::cout<<std::endl;
}
catch(char const* e){
std::cout<<"Eytion++ Error: "<<e<<std::endl;
}
catch(std::string e){
std::cout<<"Eytion++ Error: "<<e<<std::endl;
}
catch(epperr::Epperr eppe){
std::cout<<eppe.what()<<std::endl;
}
catch(epperr::EppClierr eppce){
std::cout<<eppce.what()<<std::endl;
}
}
}
else if(strcmp(argv[1], "-cli") == 0) epp_cli();
else if(strcmp(argv[1], "-?")==0 || strcmp(argv[1], "-h")==0 || strcmp(argv[1], "-help")==0){
std::string help = "Eytion++ Options Helper"
"Options:\n"
"-v, -version Output the version of Eytion++ on the screen\n"
"-?, -h, -help View Eytion++ options help\n"
"-r, -run <file> Run a file named 'file'\n"
"-cli Run Eytion++ cli";
std::cout<<help<<std::endl;
}
}
else{
epp_cli();
}
end:
return 0;
}
#endif