-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptHandler.cpp
More file actions
480 lines (429 loc) · 15.2 KB
/
Copy pathScriptHandler.cpp
File metadata and controls
480 lines (429 loc) · 15.2 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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include "ScriptHandler.h"
//bool checkconditions(std::queue<std::string> separatedcodeline);
template <class T>
bool dooperation(T firstvariable, T secondvariable, string theoperator);
ScriptHandler::ScriptHandler()
{
std::cout << "WARNING: Scripts cannot run functions without a passed handler";
std::cout << "See documentation(doesn't exist) for how it should work\n";
hasfunctionhandler = false;
std::vector<std::string> inputstrings;
std::ifstream scriptfile;
scriptfile.open("Script.txt");
if(scriptfile.fail())
std::cout << "file failed to open";
else
{
int i = 0;
std::string tempstring;
while(getline(scriptfile, tempstring))
{
//getline(scriptfile, tempstring);
if(SCRIPTHANDLERDEBUG == true)
std::cout << "got string " << tempstring << std::endl;
inputstrings.push_back(tempstring);
tempstring = "";
}
RunScript(inputstrings);
}
}
ScriptHandler::ScriptHandler(std::function<void(string)> passedhandler)
{
hasfunctionhandler = true;
functionhandler = passedhandler;
std::vector<std::string> inputstrings;
std::ifstream scriptfile;
scriptfile.open("Script.txt");
if(scriptfile.fail())
std::cout << "file failed to open";
else
{
int i = 0;
std::string tempstring;
while(getline(scriptfile, tempstring))
{
//getline(scriptfile, tempstring);
if(SCRIPTHANDLERDEBUG == true)
std::cout << "got string " << tempstring << std::endl;
inputstrings.push_back(tempstring);
tempstring = "";
}
RunScript(inputstrings);
}
}
void ScriptHandler::OpenFileAtLine(std::string file, std::string line)
{
}
void ScriptHandler::RunScript(std::vector<std::string> scriptdata)
{
//TODO: make a better, more intuitive process for math
//std::cout << "got dem strings! See? " << scriptdata[3] << std::endl;
//create stringstream
//split the stringstream into parts
//use those parts to run commands
/*std::vector<integermodule> intvector;
std::vector<floatmodule> floatvector;
std::vector<stringmodule> stringvector;*/
std::stringstream codeline;
std::queue<std::string> separatedcodeline;
int currentline = 0;
int positioncursora = 0;
int positioncursorb = 0;
bool ignoreline = false;
std::string tempstring = "";
std::stack<int> locationoflastwhile;
std::stack<int> locationoflastif;
while(currentline < scriptdata.size()) //so long as there is a new line of code in the script
{
codeline.str("");
codeline.clear();
codeline.str(scriptdata[currentline]);
//std::cout << "codeline contains " << codeline.str() << std::endl;
while(codeline >> tempstring) //separate the line of code
{
//codeline >> tempstring;
separatedcodeline.push(tempstring);
//std::cout << "separated " << separatedcodeline.back() << std::endl;
}
if(ignoreline == false && !separatedcodeline.empty()) //was ignoreline == false
{
//std::cout << "running code." << std::endl;
//BUG IS DUE TO ENDIF ALWAYS SETTING IGNORELINETOFALSE
//I HAVE TO KNOW IF AN IF STATEMENT WAS TRUE IN ORDER TO DO THAT
if(separatedcodeline.front() == "if")
{
//if an if was run, add a location
locationoflastif.push(currentline);
//std::cout << "doing if code" << std::endl;
separatedcodeline.pop();
ignoreline = true;
if(checkconditions(separatedcodeline))
{
ignoreline = false;
}
}
else if(separatedcodeline.front() == "while")
{
//std::cout << "running while code" << std::endl;
separatedcodeline.pop();
if(checkconditions(separatedcodeline))
{
//cout << "PUSHING LINE" << currentline << endl;
locationoflastwhile.push(currentline);
//std::cout << "running while code" << std::endl;
}
else
{
//cout << "flagging ignore line as true" << endl;
ignoreline = true;
}
}
else if(separatedcodeline.front() == "int")
{
//int name data
//integermodule tempmodule;
//cout << "BEGININTEGER" << endl;
string integername;
int integerdata;
separatedcodeline.pop(); // pop the int
integername = separatedcodeline.front();
//cout << "NAME: " << separatedcodeline.front() << endl;
separatedcodeline.pop();// pop the name
integerdata = atoi(separatedcodeline.front().c_str());
//cout << "VALUE: " << integerdata << endl;
//cout << "ENDINTEGER" << endl;
//if I attempt to pop to clear the data, it crashes. I don't know why
//intvector.push_back(tempmodule);
intmap[integername] = integerdata;
//tempmodule = intvector[0];
//std::cout << "name: " << tempmodule.integername << " value: " << tempmodule.integerdata << std::endl;
}
else if(separatedcodeline.front() == "float")
{
//std::cout << "begin float";
//floatmodule tempmodule;
string floatname;
float floatdata;
separatedcodeline.pop(); //pop the float
floatname = separatedcodeline.front();
separatedcodeline.pop();
floatdata = atof(separatedcodeline.front().c_str());
//floatvector.push_back(tempmodule);
floatmap[floatname] = floatdata;
//tempmodule = floatvector[0];
//std::cout << "name: " << tempmodule.floatname << " value: " << tempmodule.floatdata << std::endl;
}
else if(separatedcodeline.front() == "string")
{
//stringmodule tempmodule;
string stringname;
string stringdata;
separatedcodeline.pop(); //pop the float
stringname = separatedcodeline.front();
separatedcodeline.pop();
stringdata = separatedcodeline.front();
//stringvector.push_back(tempmodule);
stringmap[stringname] = stringdata;
cout << stringmap[stringname] << "TESTING" << endl;
//std::cout << tempmodule.stringname << tempmodule.stringdata;
}
else if(separatedcodeline.front() == "math") //math varname operation
{
separatedcodeline.pop();
//std::cout << "doing math code" << std::endl;
std::string endvariable = separatedcodeline.front();
separatedcodeline.pop();
std::string operationstring;
while(!separatedcodeline.empty())
{
operationstring = operationstring + separatedcodeline.front();
separatedcodeline.pop();
}
intmap[endvariable] = domath(operationstring, intmap, stringmap, floatmap);
}
else if(separatedcodeline.front() == "function")
{
separatedcodeline.pop();
if(hasfunctionhandler == true)
{
std::string functionstring;
functionstring = separatedcodeline.front();
separatedcodeline.pop();
while(!separatedcodeline.empty())
{
functionstring = functionstring + " " + separatedcodeline.front();
separatedcodeline.pop();
}
functionhandler(functionstring);
}
else
{
std::cout << "ERR: FUNCT LINE:" << codeline << " Function handler does not exist\n";
}
}
else if(separatedcodeline.front() == "print")
{
std::string varstring;
//std::cout << "running print code" << std::endl;
separatedcodeline.pop();
while(!separatedcodeline.empty())
{
varstring = isvariabletype(separatedcodeline.front());
if(varstring == "false")
{
std::cout << separatedcodeline.front() << " ";
}
else if(varstring == "int")
{
std::cout << intmap[separatedcodeline.front()] << " ";
}
else if(varstring == "float")
{
std::cout << floatmap[separatedcodeline.front()] << " ";
}
else if(varstring == "string")
{
std::cout << stringmap[separatedcodeline.front()] << " ";
}
separatedcodeline.pop();
}
std::cout << std::endl;
}
else
{
//see if the number is a variable or something
}
}
if(!separatedcodeline.empty())
{
if(separatedcodeline.front() == "endif")
{
//need to make this happen only if there was an if statement
//std::cout << "found end of if loop";
if(!locationoflastif.empty())
{
ignoreline = false;
locationoflastif.pop();
}
}
if(separatedcodeline.front() == "endwhile")
{
//std::cout << "found end of while loop";
if(!locationoflastwhile.empty())
{
ignoreline = false;
currentline = locationoflastwhile.top() - 1;
//cout << "GOTO LINE" << locationoflastwhile.top() - 1 << endl;
locationoflastwhile.pop();
}
else if(ignoreline == false)
std::cout<<"no while to co-respond to endwhile at line " << currentline + 1 << std::endl;
else if(ignoreline == true)
ignoreline = false;
}
//if the front value is equal to any variable look for more things
//lets make the variable lists into maps first
}
//after all the stuff is read and done with, delete the current queue.
while(separatedcodeline.empty() == false)
{
separatedcodeline.pop();
}
//while(locationoflastwhile.empty() == false)
//{
// locationoflastwhile.pop();
//}
//if I want to move the cursor, change currentline + or -;
currentline++;
}
}
//pre: needs a separatedcodeline with at least 3 variables. 1st number, second operator, third number
//post: performs operation and returns true or false
//this is where I need to get variables, for now.
//if(intmap.find(intigername) == m.end())
bool ScriptHandler::checkconditions(std::queue<std::string> separatedcodeline)
{
//start with void pointers
//get a funcion that returns a pair: string and pointer
//use string to turn all the below pointers into the appropriate
string theoperator;
string firstvariable;
string secondvariable;
bool isfirstavariable;
bool issecondavariable;
string comparetypes = "int";
int firsti, secondi;
string firsts, seconds;
float firstf, secondf;
//get the inputs
firstvariable = separatedcodeline.front();
separatedcodeline.pop();
theoperator = separatedcodeline.front();
separatedcodeline.pop();
secondvariable = separatedcodeline.front();
//figure out what exactly the inputs are
//A is a variable type X, B isn't
//A is a variable type X, B is a variable type X
//A isn't, B is a variable type X
if(intmap.find(firstvariable) != intmap.end())
{
firsti = intmap[firstvariable];
isfirstavariable = true;
comparetypes = "int";
}
if(floatmap.find(firstvariable) != floatmap.end())
{
firstf = floatmap[firstvariable];
isfirstavariable = true;
comparetypes = "float";
}
if(stringmap.find(firstvariable) != stringmap.end())
{
firsts = stringmap[firstvariable];
isfirstavariable = true;
comparetypes = "string";
}
//begin comparison of second variable
if(intmap.find(secondvariable) != intmap.end())
{
secondi = intmap[secondvariable];
issecondavariable = true;
comparetypes = "int";
}
if(floatmap.find(secondvariable) != floatmap.end())
{
secondf = floatmap[secondvariable];
issecondavariable = true;
comparetypes = "float";
}
if(stringmap.find(secondvariable) != stringmap.end())
{
seconds = stringmap[secondvariable];
issecondavariable = true;
comparetypes = "string";
}
//if first wasn't a variable, assume it's of type the one that was a variable was
if(isfirstavariable == false)
{
//check type, convert first to said type
if(comparetypes == "int")
firsti = atoi(firstvariable.c_str());
if(comparetypes == "float")
firstf = atof(firstvariable.c_str());
if(comparetypes == "string")
firsts = firstvariable;
}
//same as first
if(issecondavariable == false)
{
//check type, convert second to said type
if(comparetypes == "int")
secondi = atoi(secondvariable.c_str());
if(comparetypes == "float")
secondf = atof(secondvariable.c_str());
if(comparetypes == "string")
seconds = secondvariable;
}
//run the comparisons default value will assume ints
if(comparetypes == "int")
return dooperation(firsti, secondi, theoperator);
if(comparetypes == "float")
return dooperation(firstf, secondf, theoperator);
if(comparetypes == "string")
return dooperation(firsts, seconds, theoperator);
}
template <class T>
bool dooperation(T firstvariable, T secondvariable, string theoperator)
{
//cout << "First:" << firstvariable << " second: " << secondvariable << " OP: " << theoperator << endl;
if (theoperator == "=")
{
if(firstvariable == secondvariable)
return true;
}
if (theoperator == "!=")
{
if(firstvariable != secondvariable)
return true;
}
if (theoperator == ">")
{
if(firstvariable > secondvariable)
return true;
}
if (theoperator == "<")
{
if(firstvariable < secondvariable)
return true;
}
if (theoperator == ">=")
{
if(firstvariable >= secondvariable)
return true;
}
if (theoperator == "<=")
{
if(firstvariable <= secondvariable)
return true;
}
//std::cin.get();
return false;
}
//returns if the string exists as a variable
//checks int, float, and bool variables though, be careful
std::string ScriptHandler::isvariabletype(std::string teststring)
{
if(intmap.find(teststring) != intmap.end())
{
return "int";
}
else if(floatmap.find(teststring) != floatmap.end())
{
return "float";
}
else if(stringmap.find(teststring) != stringmap.end())
{
return "string";
}
return "false";
}