-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathael.h
executable file
·835 lines (773 loc) · 18 KB
/
ael.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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/*
Author: Willie Lawrence - cptx032@gmail.com
*/
#ifndef AEL_H
#define AEL_H
#include <iostream>
#include <sstream> // cast double to string
#include <vector>
#include <unordered_map> // hash_map
#include <fstream> // reading file
#include <thread>
#include <random>
#define AEL_VERSION "0.0.4"
// when a function is not found the function saved
// in this var is runned
#define AEL_DEFAULT_FUNCTION "__ael_default_function"
// tok = token, in this case "token" is meant to be a "word"
typedef std::string tok;
typedef std::vector<tok> phrase;
typedef std::unordered_map<tok, tok> aeldict;
typedef aeldict(*aelnamefunction)(void);
typedef std::vector<tok> string_stack;
// used to print a phrase in terminal
void _ael_log(phrase &ph)
{
for (int i=0; i < ph.size(); i++)
{
std::cout << "{" << ph[i] << "} ";
}
std::cout << std::endl;
}
// function to show formated "syntax/semantic" errors
void _ael_error(phrase &ph, std::string error)
{
std::cerr << "Error: " << ph[0] << " " << error << "\nArgs:\n\t";
_ael_log(ph);
}
bool _ael_error_invalid_number_arguments_exact(phrase &ph, int expected)
{
if (ph.size() - 1 != expected)
{
// fixme: use _ael_error
std::cerr << "Error: " << ph[0] << " takes exactly " << expected << " arguments (" << ph.size()-1 << " given)" << std::endl;
std::cout << "Args:\n\t";
_ael_log(ph);
return true;
}
return false;
}
bool _ael_error_invalid_number_arguments_minimum(phrase &ph, int min_expected)
{
if ((ph.size() - 1) < min_expected)
{
// fixme: use _ael_error
std::cerr << "Error: " << ph[0] << " takes at least " << min_expected << " arguments (" << ph.size()-1 << " given)" << std::endl;
std::cout << "Args:\n\t";
_ael_log(ph);
return true;
}
return false;
}
bool is_a_blank_char(char _c)
{
if(_c==' '||_c=='\t'||_c=='\n'||_c=='\r')
return true;
return false;
}
// transforms float to string
tok to_string(double _v)
{
std::ostringstream str_concatenated;
str_concatenated << _v;
return str_concatenated.str();
}
// returns the content of a text file
std::string get_file_content(const char* file_path)
{
std::string contents;
std::string line;
std::ifstream script_file(file_path);
if(script_file.is_open())
{
while (getline(script_file, line))
{
if (line[0] != '#')
{
contents += line + '\n';
}
}
script_file.close();
}
else
{
std::cerr << "'" << file_path << "'" << " file not found" << std::endl;
}
return contents;
}
class aelinterpreter
{
public:
// stores the variables
aeldict dictionary;
// defining "aelfunction": a function-type to ael-functions
// each ael-function must return void and receive the interpreter
// followed by a phrase
typedef void(*aelfunction)(aelinterpreter&, phrase&);
// stores the ael functions
std::unordered_map<tok, aelfunction> functions;
// stores the ael lists, called here by "stack"
std::unordered_map<tok, string_stack> stack;
// stores the hashes
std::unordered_map<tok, std::unordered_map<tok, tok> > maps;
aelinterpreter(){}
// gets a string and parses it. the result of this process is a phrase
// the phrase must be passed to to_tokens function
void to_tokens(const char* _s, phrase &ph)
{
std::string script = _s;
ph.push_back("");
int block_counter = 0;
char actual_char;
for(int i=0; i < script.size(); i++)
{
actual_char = script[i];
// se estiver dentro de um bloco
if (block_counter > 0)
{
// e abrir outro bloco
if (actual_char == '{')
{
// aumenta o contador de blocos
// para saber quando realmente
// o caractere '}' fecha o bloco
block_counter++;
ph[ph.size()-1] += actual_char;
}
// se ele fechar um bloco
else if (actual_char == '}')
{
// decrementa o contador de blocos
block_counter--;
// se for maior que zero é porque
// se fechou um bloco dentro de outro
if(block_counter != 0)
{
// inclui o caractere '}'
// porque faz parte da string
ph[ph.size()-1] += actual_char;
}
}
// se ele digitar qualquer outro caractere
// dentro de um bloco
// simplesmente adiciona o caractere
else
{
ph[ph.size()-1] += actual_char;
}
}
// se não houver blocos abertos
else
{
// e ele abrir um bloco
if (actual_char == '{')
{
// incrementa o contador de blocos
// e não salva o caractere '{'
// no próximo loop já não entra nesse
// 'else'
block_counter++;
}
// se é um caractere em branco e
// não estiver dentro de um bloco
else if (is_a_blank_char(actual_char)) // [fixme] > and last is not blank
{
// adiciona outro token
if(ph[ph.size()-1] != "")
ph.push_back("");
}
// se o caractere é um ';' adiciona
// outro token
else if(actual_char == '.')
{
ph.push_back(".");
ph.push_back("");
}
else
{
ph[ph.size()-1] += actual_char;
}
}
}
// se sobrar algum token vazio
if(ph[ph.size()-1] == "")
{
ph.erase(ph.end());
}
}
// return the value in variables dictionary
// if value is not found the own key is returned
tok get_value(tok key)
{
// if key starts with '"'
if(key[0] == '{')
{
return key.substr(1, key.size()-2);
}
if(this->has_var(key))
{
// fixme: verificar se ainda há necessidade disso
if(this->dictionary[key][0] == '{')
{
return this->get_value(this->dictionary[key]);
}
else
{
return this->dictionary[key];
}
}
return key;
}
// returns true if a variable exists in variables dictionary
bool has_var(tok var_identifier)
{
return this->dictionary.find(var_identifier) != this->dictionary.end();
}
// returns true if a variable exists in stack dictionary
bool has_stack(tok var_identifier)
{
return this->stack.find(var_identifier) != this->stack.end();
}
// returns true if a variable exists in function dictionary
bool has_function(tok function_identifier)
{
return this->functions.find(function_identifier) != this->functions.end();
}
// runs a ael-function identified by the first token passing
// all phrase as argument
void run_cmd(phrase &ph)
{
if(has_function(ph[0]))
{
this->functions[ph[0]](*this, ph);
}
else
{
std::string function_name = this->get_value(AEL_DEFAULT_FUNCTION);
if (this->has_function(function_name))
{
// try: avoid create a new list
std::vector<tok> _new_phrase;
_new_phrase.push_back(this->get_value(AEL_DEFAULT_FUNCTION));
for (int i=0; i < ph.size(); i++)
{
_new_phrase.push_back(ph[i]);
}
// here i'm trying not call "run_cmd" recursively to avoid
// any type of infinity loop
this->functions[_new_phrase[0]](*this, _new_phrase);
}
else
{
_ael_error(ph, "can't find that function");
}
}
}
// receives a complete script and runs each command splitted by "." char
void interprets(phrase &ph)
{
phrase p;
for(int i=0; i < ph.size(); i++)
{
if(ph[i] == ".")
{
this->run_cmd(p);
p.clear();
}
else
{
p.push_back(ph[i]);
}
}
}
};
typedef void(*aelfunction)(aelinterpreter&, phrase&);
// trace arg1 arg2 argN.
// no new lines
void _ael_trace(aelinterpreter &ael, phrase &ph)
{
for(int i=1; i < ph.size(); i++)
{
std::cout << ael.get_value(ph[i]) << " ";
}
std::cout << std::endl;
}
//[doc] creates a var in interpreter dictionary
void _ael_set(aelinterpreter &ael, phrase &ph)
{
if(ph.size() == 3)
{
ael.dictionary[ph[1]] = ael.get_value(ph[2]);
}
else if(ph.size() == 2)
{
ael.dictionary[ph[1]] = "";
}
else
{
// fixme: better error formatting
std::cerr << "Error: set takes 1 or 2 arguments (" << ph.size()-1 << " given)" << std::endl;
return;
}
}
// interprets a string like a script (reflexion)
void _ael_run(aelinterpreter &ael, phrase &ph)
{
if (_ael_error_invalid_number_arguments_exact(ph, 1))
{
return;
}
std::string content = ael.get_value(ph[1]).c_str();
phrase p;
ael.to_tokens(content.c_str(), p);
ael.interprets(p);
}
// print each argument one after other (without white spaces)
void _ael_print(aelinterpreter &ael, phrase &ph)
{
for(int i=1; i < ph.size(); i++)
{
std::cout << ael.get_value(ph[i]);
}
}
// deletes one or more variables of interpreter dictionary
void _ael_del(aelinterpreter &ael, phrase &ph)
{
if(_ael_error_invalid_number_arguments_minimum(ph, 1))
{
return;
}
for(int i=1;i < ph.size(); i++)
{
ael.dictionary.erase(ph[i]);
}
}
// trace in terminal all functions and variables in scope
void _ael_dump(aelinterpreter &ael, phrase &ph)
{
std::cout << "Functions:\n";
std::unordered_map<tok, void(*)(aelinterpreter&, phrase&)>::iterator it = ael.functions.begin();
while(it != ael.functions.end())
{
std::cout << "\t" << it->first << std::endl;
it++;
}
std::cout << "\nVariables:\n";
aeldict::iterator itv = ael.dictionary.begin();
while(itv != ael.dictionary.end())
{
std::cout << "\t" << itv->first << std::endl;
itv++;
}
}
void _ael_add_number(aelinterpreter &ael, phrase &ph)
{
if (ph.size() != 3 && ph.size() != 4)
{
// fixme: format better this error string
std::cerr << "Error: wrong numbers of arguments" << std::endl;
return;
}
double value1 = atof(ael.get_value(ph[1]).c_str());
double value2 = atof(ael.get_value(ph[2]).c_str());
if (ph.size() == 4)
{
ael.dictionary[ph[3]] = to_string(value1 + value2);
}
else
{
ael.dictionary[ph[1]] = to_string(value1 + value2);
}
}
// * a b <dest> (if dest is not provided the result is stored in a)
void _ael_mult_number(aelinterpreter &ael, phrase &ph)
{
if (ph.size() != 3 && ph.size() != 4)
{
// fixme: format better this error string
std::cerr << "Error: wrong numbers of arguments" << std::endl;
return;
}
double value1 = atof(ael.get_value(ph[1]).c_str());
double value2 = atof(ael.get_value(ph[2]).c_str());
if (ph.size() == 4)
{
ael.dictionary[ph[3]] = to_string(value1 * value2);
}
else
{
ael.dictionary[ph[1]] = to_string(value1 * value2);
}
}
// / a b <dest> (if dest is not provided the result is stored in a)
void _ael_div_number(aelinterpreter &ael, phrase &ph)
{
if (ph.size() != 3 && ph.size() != 4)
{
// fixme: format better this error string
std::cerr << "Error: wrong numbers of arguments" << std::endl;
return;
}
double value1 = atof(ael.get_value(ph[1]).c_str());
double value2 = atof(ael.get_value(ph[2]).c_str());
if (ph.size() == 4)
{
ael.dictionary[ph[3]] = to_string(value1 / value2);
}
else
{
ael.dictionary[ph[1]] = to_string(value1 / value2);
}
}
// read and then interprets a Ael script file
void _ael_import(aelinterpreter &ael, phrase &ph)
{
if (_ael_error_invalid_number_arguments_exact(ph, 1))
{
return;
}
std::string content = get_file_content(ael.get_value(ph[1]).c_str());
phrase p;
ael.to_tokens(content.c_str(), p);
ael.interprets(p);
}
// does nothing
void _ael_nop(aelinterpreter &ael, phrase &ph)
{
}
// loop NUMBER CODE.
void _ael_loop(aelinterpreter &ael, phrase &ph)
{
if (_ael_error_invalid_number_arguments_exact(ph, 2))
{
return;
}
int loop = atoi(ael.get_value(ph[1]).c_str());
phrase p;
ael.to_tokens(ael.get_value(ph[2]).c_str(), p);
while (loop > 0)
{
loop--;
ael.interprets(p);
}
}
void _ael_if(aelinterpreter &ael, phrase &ph)
{
// formats:
// 1.
// if <var1> <op> <var2> <code>.
// 2.
// if <var1> <op> <var2> <code> <else> <code>.
// TODO: make elifs
if (ph.size() != 5 && ph.size() != 7)
{
std::cerr << "ERROR" << std::endl;
return;
}
std::string var1 = ael.get_value(ph[1]);
std::string op = ph[2];
std::string var2 = ael.get_value(ph[3]);
bool pass_in_if = false;
if (op == "==")
{
pass_in_if = var1 == var2;
}
else if (op == "!=")
{
pass_in_if = var1 != var2;
}
else if (op == ">")
{
double v1 = atof(var1.c_str());
double v2 = atof(var2.c_str());
pass_in_if = v1 > v2;
}
else if (op == ">=")
{
double v1 = atof(var1.c_str());
double v2 = atof(var2.c_str());
pass_in_if = v1 >= v2;
}
else if (op == "<")
{
double v1 = atof(var1.c_str());
double v2 = atof(var2.c_str());
pass_in_if = v1 < v2;
}
else if (op == "<=")
{
double v1 = atof(var1.c_str());
double v2 = atof(var2.c_str());
pass_in_if = v1 <= v2;
}
else
{
_ael_error(ph, "wrong operator");
return;
}
if (pass_in_if)
{
phrase p;
ael.to_tokens(ael.get_value(ph[4]).c_str(), p);
ael.interprets(p);
}
else
{
// has else
if (ph.size() == 7)
{
phrase p;
ael.to_tokens(ael.get_value(ph[6]).c_str(), p);
ael.interprets(p);
}
}
}
const bool AEL_VERBOSE = true;
void ael_trace(std::string m)
{
if (AEL_VERBOSE)
{
std::cout << m << std::endl;
}
}
// this function is called by 'instance' of stack
void _ael_stack_variable(aelinterpreter &i, phrase &ph)
{
// stack loop <code>. (acessible by `@item`)
// stack clear.
// stack push <value:get_value>.
// stack pop.
// stack length <destvar>.
// stack set <index> <value>.
// stack get <index> <destvar>.
// stack erase <index>.
if(_ael_error_invalid_number_arguments_minimum(ph, 1))
{
return;
}
tok stack_name = ph[0];
tok command = ph[1];
if (command == "push")
{
if (_ael_error_invalid_number_arguments_exact(ph, 2))
{
return;
}
i.stack[stack_name].push_back(i.get_value(ph[2]));
}
else if (command == "pop")
{
if (_ael_error_invalid_number_arguments_exact(ph, 1))
{
return;
}
if (i.stack[stack_name].size() > 0)
{
i.stack[stack_name].pop_back();
}
}
else if (command == "clear")
{
if (_ael_error_invalid_number_arguments_exact(ph, 1))
{
return;
}
i.stack.erase(stack_name);
}
else if (command == "loop")
{
if (_ael_error_invalid_number_arguments_exact(ph, 2))
{
return;
}
phrase p;
i.to_tokens(i.get_value(ph[2]).c_str(), p);
for (int stack_i=0; stack_i < i.stack[stack_name].size(); stack_i++)
{
i.dictionary["@item"] = i.stack[stack_name][stack_i];
i.interprets(p);
}
}
else if (command == "length")
{
if (_ael_error_invalid_number_arguments_exact(ph, 2))
{
return;
}
i.dictionary[ph[2]] = to_string(i.stack[stack_name].size());
}
else if (command == "set")
{
if (_ael_error_invalid_number_arguments_exact(ph, 3))
{
return;
}
int index = atoi(i.get_value(ph[2]).c_str());
if (index < i.stack[stack_name].size())
{
i.stack[stack_name][index] = i.get_value(ph[3]);
}
else
{
_ael_error(ph, "index out of stack bounds");
}
}
else if (command == "get")
{
if (_ael_error_invalid_number_arguments_exact(ph, 3))
{
return;
}
int index = atoi(i.get_value(ph[2]).c_str());
if (index < i.stack[stack_name].size())
{
i.dictionary[ph[3]] = i.stack[stack_name][index];
}
else
{
_ael_error(ph, "index out of stack bounds");
}
}
else if (command == "erase")
{
if (_ael_error_invalid_number_arguments_exact(ph, 2))
{
return;
}
int index = atoi(i.get_value(ph[2]).c_str());
if (index < i.stack[stack_name].size())
{
i.stack[stack_name].erase(i.stack[stack_name].begin() + index);
}
else
{
_ael_error(ph, "index out of stack bounds");
}
}
else
{
_ael_error(ph, "invalid hash command");
}
}
void _ael_stack(aelinterpreter &ael, phrase &ph)
{
if (_ael_error_invalid_number_arguments_exact(ph, 1))
{
return;
}
ael.functions[ael.get_value(ph[1])] = _ael_stack_variable;
}
void _ael_map_variable(aelinterpreter &ael, phrase &ph)
{
// map map_name.
// map_name update key1 value1 key2 value2 (...).
// map_name get key dest_var.
if(_ael_error_invalid_number_arguments_minimum(ph, 3))
{
return;
}
tok command = ph[1];
if (command == "get")
{
if(_ael_error_invalid_number_arguments_exact(ph, 3))
{
return;
}
tok key = ael.get_value(ph[2]);
ael.dictionary[ph[3]] = ael.maps[ph[0]][key];
}
else if (command == "update")
{
if (((ph.size() - 2) % 2) != 0)
{
_ael_error(ph, "map update must have keys and values");
return;
}
for (int i=2; i < ph.size(); i+=2)
{
ael.maps[ph[0]][ael.get_value(ph[i])] = ael.get_value(ph[i + 1]);
}
}
else
{
_ael_error(ph, "invalid map command");
}
}
void _ael_map(aelinterpreter &ael, phrase &ph)
{
if (_ael_error_invalid_number_arguments_exact(ph, 1))
{
return;
}
ael.functions[ael.get_value(ph[1])] = _ael_map_variable;
}
// load FILE_NAME DEST_VAR FILE_NAME2 DEST_VAR2 ...
// load a file and put the content inside 'DEST_VAR'
void _ael_load(aelinterpreter &ael, phrase &ph)
{
if (ph.size() < 3)
{
_ael_error(ph, "_ael_load: Invalid number of arguments less than 2");
return;
}
if (((ph.size() - 1) % 2) != 0)
{
_ael_error(ph, "_ael_load: Invalid number of arguments. not divisible by 2");
return;
}
for (int i=1; i < ph.size() - 1; i+=2)
{
ael.dictionary[ph[i + 1]] = get_file_content(ph[i].c_str());
}
}
// function that receives a phrase and run it
void async_interprets(aelinterpreter *ael, phrase ph) {
ael->interprets(ph);
}
// async <CODE>
void _ael_async(aelinterpreter &ael, phrase &ph) {
if (_ael_error_invalid_number_arguments_exact(ph, 1)) {
return;
}
phrase code;
ael.to_tokens(ael.get_value(ph[1]).c_str(), code);
std::thread(async_interprets, &ael, code).detach();
}
// randint min max DESTVAR
void _ael_randint(aelinterpreter &ael, phrase &ph) {
if (_ael_error_invalid_number_arguments_exact(ph, 3)) {
return;
}
int min = atoi(ael.get_value(ph[1]).c_str());
int max = atoi(ael.get_value(ph[2]).c_str());
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int> uni(min, max);
int random_integer = uni(rng);
ael.dictionary[ph[3]] = to_string(random_integer);
}
//[doc] load main functions
void load_main_ael_functions(aelinterpreter &i)
{
i.functions["trace"] = _ael_trace;
i.functions["set"] = _ael_set;
i.functions["run"] = _ael_run;
i.functions["del"] = _ael_del;
i.functions["dump"] = _ael_dump;
i.functions["print"] = _ael_print;
i.functions["import"] = _ael_import;
i.functions["#"] = _ael_nop;
i.functions["+"] = _ael_add_number;
i.functions["*"] = _ael_mult_number;
i.functions["/"] = _ael_div_number;
i.functions["loop"] = _ael_loop;
i.functions["if"] = _ael_if;
i.functions["stack"] = _ael_stack;
i.functions["load"] = _ael_load;
i.functions["async"] = _ael_async;
i.functions["randint"] = _ael_randint;
i.functions["map"] = _ael_map;
i.dictionary["__ael_version"] = AEL_VERSION;
i.dictionary[AEL_DEFAULT_FUNCTION] = "run";
}
#endif