This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.cpp
509 lines (374 loc) · 11.7 KB
/
core.cpp
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "core.hpp"
// will be the error code given when a function return successfully
// this content is set so that if they use it out of context they are still told so
const char* lambda_finish = "\aERROR: use of `return` or `break` out of context\n";
// this gets returned from runMacro() on error
const char* macro_error = "\aERROR: in macro execution\n";
FILE* program = stdin;
char* progName = NULL;
void runFile(char* programFile, bool& errorReporting){
progName = programFile;
program = fopen(programFile, "r");
// file error
if (program == NULL) {
setTermEffect(TERM_EFF_BOLD);
std::cerr <<metaName <<": ";
color_fputs(stderr, "error: ", 255, 0, 0);
std::cerr <<": could not open file \'" <<programFile <<"\'\n";
setTermEffect();
exit(EXIT_FAILURE);
}
// the most important component of the program :)
std::stack<CalcValue> mainStack;
UserVar first_node(NULL, " ", 0.0);
first_node.first = &first_node;
std::vector<UserVar> var_nodes;
var_nodes.reserve(2000); // overkill but it will prevent addresses from changing
var_nodes.push_back(first_node);
bool elseStatement = false;
std::vector<void*> freeable;
char* rpnln = NULL;
char* rpnln_head = rpnln;
size_t lineLen = 0;
// for each line in the programFile...
for (;;) {
// used for line numbers in errors
line++;
rpnln = rpnln_head;
// here there be bugs
if (getline(&rpnln, &lineLen, program) == -1) { // EOF
for (void* x : freeable)
free(x);
return;
}
// I need a copy of it to call free() on later.
rpnln_head = rpnln;
char *errorToken = NULL;
// process the line
if ((errorToken =
processLine(mainStack, var_nodes, errorReporting, rpnln, elseStatement, program, freeable))
&& errorReporting
) {
// file name and
setTermEffect(TERM_EFF_BOLD);
std::cerr <<"^^ in " <<programFile <<":" <<line <<':' <<errorToken - rpnln_head <<"\n";
setTermEffect();
// print the problem statement
color_fprintf(stderr, 255, 0, 0, "\t%s", fileutils::getLineFromFile(programFile, line));
// point to the problem area
//for (uint16_t i = 0; i < errorToken - rpnln_head; i++)
// std::cerr <<' ';
//color_fputs(stderr, "^\n", 255, 0, 0);
// windows sucks :P
#ifdef _WIN32
std::cin.ignore();
#endif
//for (void* p : freeable) free(p);
//freeable.clear();
// you're dead :P
exit(EXIT_FAILURE);
}
}
}
/* from string_stack.hpp
namespace macro {
typedef enum {
MACRO, LAMBDA, OTHER
} exec_t;
typedef enum {
SUCCESS, RETURN, ERROR, BREAK
} ret_t;
}
*/
macro::ret_t runFile(FILE* prog_file, std::vector<UserVar>& var_nodes, bool& errorReporting,
std::stack<CalcValue>& mainStack, bool& elseStatement
) {
if (!prog_file)
return macro::ERROR;
std::vector<void*> freeable;
size_t local_line = 0;
char* rpnln = NULL, *rpnln_head = rpnln;
size_t lineLen = 0;
// for each line in the programFile...
for (;;) {
// used for line numbers in errors
local_line++;
rpnln = rpnln_head;
if (getline(&rpnln, &lineLen, prog_file) == -1) {
for (void* x : freeable)
free(x);
free(rpnln);
return macro::SUCCESS; // EOF
}
rpnln_head = rpnln;
// I need a copy of it to call free() on later.
char* errorToken = NULL;
// process the line
if ((errorToken =
processLine(mainStack, var_nodes, errorReporting, rpnln, elseStatement, prog_file, freeable))
&& errorReporting
) {
if (errorToken == lambda_finish) {
// prevent memory leaks...
for (auto x : freeable)
free(x);
free(rpnln_head);
if (rpnln == lambda_finish)
return macro::RETURN;
else
return macro::BREAK;
}
// info on location
setTermEffect(TERM_EFF_BOLD);
std::cerr <<"^^ in " <<progName <<": local_line=" <<local_line
<<":col=" <<errorToken - rpnln_head <<"?\n";
setTermEffect();
// print the problem statement
rewind(prog_file);
color_fprintf(stderr, 255, 0, 0, "\t%s\t",
fileutils::getLineFromFile(prog_file, local_line));
/*
// point to the problem area
for (uint16_t i = 0; i < errorToken - rpnln_head; i++)
std::cerr <<' ';
color_fputs(stderr, "^\n", 255, 0, 0);
*/
// prevent memory leaks...
for (void* x : freeable)
free(x);
free(rpnln_head);
return macro::ERROR;
}
free(rpnln_head);
rpnln = rpnln_head = NULL;
}
// prevent memory leaks...
free(rpnln_head);
return macro::SUCCESS;
}
void runShell(std::vector<UserVar>& var_nodes, bool& errorReporting,
std::stack<CalcValue>& mainStack, bool& elseStatement, std::vector<void*> freeable
){
std::cout <<line++ <<">>> ";
char* rpnln = (char*) malloc(256);
size_t lineLen = 256;
if (getline(&rpnln, &lineLen, stdin) == -1) {
//std::cerr <<"\aERROR: Input failed... email toast27@gmail.com if this persists\n\n";
std::cout <<"\nGood-bye!\n";
exit(EXIT_SUCCESS);
return;
}
// I need a copy of it to call free() on later.
char* rpnln_head = rpnln;
// process the line
bool errors = processLine(mainStack, var_nodes, errorReporting, rpnln, elseStatement, stdin, freeable);
if (errors)
while (!mainStack.empty())
mainStack.pop();
// prevent memory leaks...
free(rpnln_head);
// this fails...
if (!mainStack.empty()) {
if (!printCalcValue(mainStack.top(), var_nodes))
std::cout <<'\n';
}
std::cout <<std::endl;
}
// Returns the start of the token/string-constant
// manages escape-sequences within string-constants
char* qtok(char* str, char** next){
if (str == NULL)
return NULL;
char *current = str, *start = str;
// skip beginning whitespace.
while (*current != '\0' && isspace(*current))
current++;
start = current;
if (*current == '"') {
// Quoted token
start = current;
current++; // Skip the beginning quote.
for (;;) {
// Go until we find a quote or the end of string.
while (*current != '\0' && *current != '"') {
if (*current == '\\') { // escape sequences
if (*(current + 1) == 'n') {
*current = '\n';
strutils::deleteChar(current + 1);
/*
*current = '\n';
*(current + 1) = '\r';
// this is much more efficienct than deleteChar()
// but doesn't do the same thing as '\n'..
// so I probably should find a better solution...
// TODO: find a better solution for "\n"
// line feed
*/
} else if (*(current + 1) == 'r') { // "\\r" => "\r\r"
*current = *(current + 1) = '\r'; // nice hack ;)
current++;
// horizontal tab
} else if (*(current + 1) == 't') { // "\\t" => "\t"
*current = '\t';
strutils::deleteChar(current + 1);
// bel
} else if (*(current + 1) == 'a') { // "\\a" => "\a"
*current = '\a';
strutils::deleteChar(current + 1);
// backspace
} else if (*(current + 1) == 'b') { // "\\b" => "\b"
*current = '\b';
strutils::deleteChar(current + 1);
// formfeed
} else if (*(current + 1) == 'f') { // "\\f" => "\f"
*current = '\f';
strutils::deleteChar(current + 1);
// vertical tab
} else if (*(current + 1) == 'v') { // "\\v" => "\v"
*current = '\v';
strutils::deleteChar(current + 1);
// escaped backslash
} else if (*(current + 1) == '\\') // "\\\\" => "\\"
strutils::deleteChar(current);
// escape sequence "\nnn"
else if (isdigit(*(current + 1))) { // "\\nnn" => "char(nnn)"
// escape sequence can be a maximum of 3 chars long
char str[3] = {
*(current + 1),
*(current + 2),
*(current + 3)
};
char* ptr;
// convert the octal literal to single char (8bit int)
*current = (char) strtol(str, &ptr, 8);
strutils::deleteChars(current + 1, ptr - str);
// escape sequence "\xhh"
} else if (*(current + 1) == 'x' && isdigit(*(current + 2))) { // "\\hh" => "char(hh)"
// escape sequence can be a maximum of 2 chars long
char str[2] = { *(current + 2), *(current + 3) };
char* ptr;
// convert the hex literal to single char (8bit int)
*current = (char) strtol(str, &ptr, 16);
strutils::deleteChars(current + 1, ptr - str);
// indicates that the next character should be ignored by the tokenizer
// this works for \", \', etc.
} else
strutils::deleteChar(current);
//current++;
}
current++;
}
//current++;
// Reached the ending quote.
goto finalize;
}
}
// Not quoted so run till we see a space.
while (*current && !isspace(*current))
current++;
finalize:
if (*current) {
// Close token if not closed already.
*current = '\0';
current++;
// Eat trailing whitespace.
while (*current && isspace(*current))
current++;
}
*next = current;
//return unescapeToken(start);
return start;
}
char* runMacro(StrStack* macro, std::stack<CalcValue>& mainStack, std::vector<UserVar> var_nodes, std::vector<void*>& freeable, bool showErrors, bool elseStatement) {
size_t buff_size = 500;
char* buff = (char*) malloc(buff_size);
macro->toString(&buff, &buff_size);
// put the string in a temp file
FILE* statement = fileutils::mktmpfile();
//FILE* statement = tmpfile();
if (!statement) {
if (showErrors) {
printf("\aERROR: tmpfile() returned null. This isn't your fault. :/\n");
return (char*) macro_error;
}
}
fputs(buff, statement);
rewind(statement);
freeable.push_back(buff);
// add layer to scope
UserVar first_node(NULL, " ", 0.0);
var_nodes.push_back(first_node);
// run the temp file
macro::ret_t ret = runFile(statement, var_nodes, showErrors, (mainStack), elseStatement);\
if (ret == macro::ERROR) {
vars::clearScope(var_nodes);
fclose(statement);
return (char*) macro_error;
} else if (ret == macro::RETURN || ret == macro::BREAK) {
// variables go out of scope
vars::clearScope(var_nodes);
fclose(statement);
return (char*) lambda_finish;
}
return NULL;
}
char* runMacroKeepScope(StrStack* macro, std::stack<CalcValue>& mainStack, std::vector<UserVar> var_nodes, std::vector<void*>& freeable, bool showErrors, bool elseStatement) {
size_t buff_size = 500;
char* buff = (char*) malloc(buff_size);
macro->toString(&buff, &buff_size);
// put the string in a temp file
FILE* statement = fileutils::mktmpfile();
//FILE* statement = tmpfile();
if (!statement) {
if (showErrors) {
printf("\aERROR: tmpfile() returned null. This isn't your fault. :/\n");
return (char*) macro_error;
}
}
fputs(buff, statement);
rewind(statement);
freeable.push_back(buff);
// run the temp file
macro::ret_t ret = runFile(statement, var_nodes, showErrors, (mainStack), elseStatement);
if (ret == macro::ERROR) {
fclose(statement);
return (char*) macro_error;
} else if (ret == macro::RETURN || ret == macro::BREAK) {
fclose(statement);
return (char*) lambda_finish;
}
return NULL;
}
char* spawnMacro(StrStack macro, std::stack<CalcValue> mainStack, std::vector<UserVar> var_nodes, bool showErrors, bool elseStatement) {
size_t buff_size = 500;
char* buff = (char*) malloc(buff_size);
macro.toString(&buff, &buff_size);
// put the string in a temp file
FILE* statement = fileutils::mktmpfile();
//FILE* statement = tmpfile();
if (!statement) {
if (showErrors) {
printf("\aERROR: tmpfile() returned null. This isn't your fault. :/\n");
return (char*) macro_error;
}
}
fputs(buff, statement);
rewind(statement);
free(buff);
// add layer to scope
UserVar first_node(NULL, " ", 0.0);
var_nodes.push_back(first_node);
// run the temp file
macro::ret_t ret = runFile(statement, var_nodes, showErrors, (mainStack), elseStatement);\
if (ret == macro::ERROR) {
vars::clearScope(var_nodes);
fclose(statement);
return (char*) macro_error;
} else if (ret == macro::RETURN || ret == macro::BREAK) {
// variables go out of scope
vars::clearScope(var_nodes);
fclose(statement);
return (char*) lambda_finish;
}
return NULL;
}