-
Notifications
You must be signed in to change notification settings - Fork 0
/
Meow.h
427 lines (410 loc) · 9.62 KB
/
Meow.h
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <iostream>
#include <regex>
#include <string>
#include <fstream>
using namespace std;
class Meow
{
public:
map<string,string> data = {{"VERSION","2.1.0"},{"PS1",">"}};
string meow = "meow";
string gtext = "";
void worker(string text,bool t){
smatch m;
/*if (regex_search(text, m, regex(R"((\$[[:alpha:]][[:alnum:]]*))")))
text = text.replace(m[1].first,m[1].second,existV(std::string(m[1].first, m[1].second),data));*/
regex rg_filter("@"+meow+"(:on|:off|)");
regex rg_code(meow+".gets\\(([0-9,]+)\\)");
regex rg_value("var ([a-zA-Z0-9]+) =[ ]{0,1}(.*)");
regex rg_ucode(meow+".puts\\((.*)\\)");
regex rg_system(meow+".system\\((.*)\\)");
regex rg_print(meow+".print\\((.*)\\)");
regex rg_range(meow+".1to\\(([0-9,]+)\\)");
regex rg_setting("@crt ([a-zA-Z0-9]+)");
regex rg_text("@crt.text\\((.*)\\)");
regex rg_if("if (.*) : (.*)");
/*
@Meow:(on|off)
meow.gets()
meow.puts()
meow.system()
meow.1to()
@crt *
@crt.text
@crt.show
exit
*/
if(regex_match(text,rg_code)){
smatch value;
regex_search(text, value,rg_code);
vector<string> v = splitString(value[1],',');
int i ;
for(auto x : v){
i = stoi(x);
cout<<char(i);
}
}
else if(regex_match(text,rg_range)){
smatch value;
regex_search(text, value,rg_range);
Mood = true;
Meowput(to_i(value[1]));
Mood = false;
}
else if(regex_match(text,rg_text)){
smatch value;
regex_search(text, value,rg_text);
gtext = value[1];
}else if(regex_match(text,rg_print)){
smatch value;
regex_search(text, value,rg_print);
cout<<syntax(value[1]);
}
else if(regex_match(text,rg_if)){
regex rg_equal("(.*) eq (.*)");
smatch value;smatch all;
regex_search(text, value,rg_if);
string v = value[1];
if(regex_match(v,all,rg_equal)){
if(syntax(all[1]) == syntax(all[2])){
worker(value[2],t);
}else {
for(auto x : all)
cout<<":"<<syntax(x)<<":"<<endl;
}
}
}
else if(regex_match(text,rg_system)){
smatch value;
regex_search(text, value,rg_system);
string s = syntax(value[1]);
system(s.c_str());
}
else if(regex_match(text,rg_setting)){
smatch value;
regex_search(text, value,rg_setting);
meow = value[1];
}
else if(text == "exit"){
exit(0);
}
else if(text == "@crt.show"){
cout<<meow;
}else if(text == "help"){
help(1);
}
else if(regex_match(text,rg_ucode)){
smatch value;
regex_search(text, value,rg_ucode);
string v = syntax(value[1]);
for(int i=0;i < v.length();i++){
//cout<<v[i];
//char c = v[i]; // Access the string representation and then the character
Meowput(static_cast<int>(v[i]));
if(i+1 < v.length()){
cout<<",";}
}
}else if(regex_match(text,rg_filter)){
smatch value;
regex_search(text,value,rg_filter);
if(value[1] == "")
put(Mood);
else{
if(value[1] == ":on")
Mood = true;
else
Mood = false;
//put(Mood);
}
}else if(text.at(0) == '#'){
}else if(regex_match(text,rg_value)){
smatch value;string input;
regex_search(text, value,rg_value);
if(value[2] == meow+".input"){
getline(cin,input);
}else
input = syntax(value[2]);
if(!exist(value[1],data))
data.insert({value[1],input});
else{
auto itr = data.find(value[1]);
if(itr != data.end()){
(*itr).second = input;
}
}
}else if(syntax(text)!= "Error"){
cout<<syntax(text);
}
else{
cout<<"\e[31m[!]\e[0m Error Command in this line \n(\e[1mMeow\e[22m):in `<main>': \e[4m"<<text<<"\e[24m"<<endl;
if(t)
exit(0);
}
}
string decode(string text){
string rtn = "";int no;
for(auto a : split(text,",")){
no = split(a,meow).size()-1;
cout<<no;
cout<<char(no);
}
return rtn;
}
vector<string> readfile(string text){
ifstream file(text.c_str());
if(!file){
cout<<text<<": No such file or directory "<<endl;
exit(0);
return {text+": No such file or directory "};
}else{
string line;
vector<string> lines;
while (getline(file, line)){
if(line != "")
lines.push_back(line);
}
file.close();
return lines;
}
}
void passive(){
string text = GetEnv("HOME");
text = text+"/.Meowrc";
ifstream file(text.c_str());
if(!file){
ofstream { text.c_str() };
}else{
string line;
while (getline(file, line)){
if(line != "")
worker(line,false);
}
}
}
void history(string text,bool read){
string path = GetEnv("HOME");
path = path+"/.Meow_history";
if(!read){
fstream file(path.c_str(),ios::app);
if(!file)
ofstream { path.c_str() };
else
file << text + "\n";
file.close();
}else{
ifstream file(path.c_str());
if(!file){
ofstream { path.c_str() };
}else{
string line;int i=1;
while (getline(file, line)){
if(line != ""){
cout<<i<<"| "<<line<<endl;
i++;
}
}
}
}
}
void help(int v){
if(v == 0){
cout<<"Usage: Meow [--] [programfile] [arguments]\n"
" -h show this message \n"
" -v print the version number\n"
" -e 'command' one line of script\n"
" -p findout pointer name\n"
<<endl;
}else if(v==1){
cout<<"help\t show this message\n"
<<endl;
}
}
inline bool exists_ (const std::string& name) {
ifstream f(name.c_str());
return f.good();
}
private:
map<string,int> line ;
bool Mood = false;
//int line = 0;
string GetEnv( const string & var ) {
const char * val = getenv( var.c_str() );
if ( val == nullptr ) { // invalid to assign nullptr to string
return "";
}
else {
return val;
}
}
void puts(string text){
cout<<text<<endl;
}void put(bool id){
if(id == true)
cout<<"on"<<endl;
else
cout<<"off"<<endl;
}void Meowput(int no){
if(Mood == true){
int j=0;
while(j < no ){
if(gtext != ""){
cout<<gtext;j++;
}else{
cout<<meow;j++;
}
//if(j+1 < no){
cout<<" ";//}
}
}else
cout<<no;
}vector<string> splitString(const string& s, char delimiter) {
vector<string> tokens;
stringstream ss(s);
string token;
while (getline(ss, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
bool exist(string text,map<string,string> data){
for (auto itr = data.begin(); itr != data.end(); ++itr)
{
if (text == itr->first)
{
return true;
}
}
return false;
}
/*string existV(string text,map<string,string> data){
text.erase(0,1);
for (auto itr = data.begin(); itr != data.end(); ++itr)
{
if (text == itr->first)
{
return itr->second;
}
}
return "$"+text;
}*/
string existV(string text,map<string,string> data){
for (auto itr = data.begin(); itr != data.end(); ++itr)
{
if (text == itr->first)
{
return itr->second;
}
}
return "";
}
int existL(string text,map<string,int> data){
//text.erase(0,1);
for (auto itr = data.begin(); itr != data.end(); ++itr)
{
if (text == itr->first)
{
return itr->second;
}
}
return -1;
}
string value_system(string text){
regex str("\"(.*)\"");
regex num("([0-9]+)");
if(text == "<>"){
cin >> text;
return text;
}else if(regex_match(text,str)){
smatch value;
regex_search(text,value,str);
return value[1];
}else if(regex_match(text,num)){
smatch value;
regex_search(text,value,num);
return value[1];
}
}
int to_i(string str){
int i ;
istringstream ( str ) >> i;
return i;
}
void strip(string str)
{
if (str.length() != 0)
{
auto w = string(" ");
auto n = string("\n");
auto r = string("\t");
auto t = string("\r");
auto v = string(1, str.front());
while ((v == w) || (v == t) || (v == r) || (v == n))
{
str.erase(str.begin());
v = string(1, str.front());
}
v = string(1, str.back());
while ((v == w) || (v == t) || (v == r) || (v == n))
{
str.erase(str.end() - 1);
v = string(1, str.back());
}
}
}
string syntax(string text){
smatch value;
//cout<<"true01->"<<text;
regex sys("`(.*)`");
regex str("\'(.*)\'");
regex str1("\"(.*)\"");
regex nom("([0-9]+)");
if(regex_search(text,value,str)){
return value[1];
}else if(regex_search(text,value,str1)){
return value[1];
}else if((text == "false")||(text == "true")){
return text;
}else if(regex_search(text,value,nom)){
return value[1];
}else if(exist(text,data)){
return existV(text,data);
}else if(regex_search(text,value,sys)){
string v = value[1];
v = sys_str(v.c_str());
v.pop_back();
return v;
}
else
return "Error";
}
vector<string> split(string s, string delimiter)
{
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
string token;
vector<string> res;
while ((pos_end = s.find(delimiter, pos_start)) != string::npos)
{
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}
res.push_back(s.substr(pos_start));
return res;
}
string sys_str(const char* cmd) {
char buffer[128];
string result = "";
FILE* pipe = popen(cmd, "r");
if (!pipe) throw runtime_error("popen() failed!");
try {
while (fgets(buffer, sizeof buffer, pipe) != NULL) {
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
};