-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhjscript.cpp
More file actions
3751 lines (3421 loc) · 137 KB
/
Copy pathhjscript.cpp
File metadata and controls
3751 lines (3421 loc) · 137 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
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "hjscript.hpp"
// Used by hjbase::HUAJISCRIPTBASE::Take_One_Token(std::vector<std::string>::const_iterator)
#define REQUIRE_MORE_TOKENS_RETURN_CODE -1
// Used by hjbase::HUAJISCRIPTBASE::Collect_Tokens(const std::string&)
#define REQUIRE_MORE_TOKENS_FOR_COMPLETE_COMMAND_RETURN_CODE 0
#define COMMAND_COMPLETED_RETURN_CODE 1
// Used by hjbase::HUAJISCRIPTBASE::Huaji_Command_Interpreter(const const_itVecStr&)
#define EXIT_RETURN_CODE 2
#define VALID_COMMAND_RETURN_CODE 1
#define INVALID_COMMAND_RETURN_CODE 0
#define UNKNOWN_COMMAND_RETURN_CODE -1
// Used by hjbase::LISTFORMATTER::Take_One_Char(char)
#define FORMATTING_COMPLETE 1
#define REQUIRE_MORE_CHAR_FOR_COMPLETE_LST 0
#define TYPE_DOUBLE 0
#define TYPE_LONG 1
#define INCREASE_AST_DEPTH 1
#define DECREASE_AST_DEPTH -1
using namespace hjbase::cnt::keywords;
using namespace hjbase::cnt::operators;
using namespace hjbase::cnt::common_msgs;
using namespace hjbase::cnt::miscellaneous;
using namespace hjbase::cnt::commands_names;
using namespace hjbase::cnt::type_tag;
using namespace hjbase::ufunc;
using namespace hjbase::except;
void hjbase::ufunc::Print_IVSTR(const const_itVecStr& iVstr) {
for(std::vector<std::string>::const_iterator it=iVstr.begin();it<iVstr.end();++it) {
std::cout<<*it<<" ";
}
std::cout<<std::endl;
}
void hjbase::ufunc::Highlight_Error_Part(const const_itVecStr& iVstr) {
for(std::vector<std::string>::const_iterator it=iVstr.begin();it<iVstr.end();++it) {
std::cout<<std::string((*it).size(), '~')<<" ";
}
std::cout<<std::endl;
}
void hjbase::ufunc::Signal_Error(const std::string& error_msg, const const_itVecStr& error_part) {
std::cerr<<error_msg<<std::endl;
std::cout<<" ";
Print_IVSTR(error_part);
std::cout<<" ";
Highlight_Error_Part(error_part);
}
void hjbase::ufunc::Signal_Error(const std::string& error_msg, const std::string& error_part) {
std::cerr<<error_msg<<std::endl;
std::cout<<" "<<error_part<<std::endl;
}
bool hjbase::ufunc::Check_If_Float_Point(const const_itVecStr& vals) {
// iterate over vals vector
for(std::vector<std::string>::const_iterator it=vals.begin();it<vals.end();++it) {
/*
just try to find a dot to determine if it is a float number or not,
note this is not enough to make sure it is a valid numerical value.
But we will not check it here.
*/
std::size_t found = (*it).find_first_of('.');
if(found!=std::string::npos) {
// std::string::npos is returned if not found, here we know it founds '.'
return true;
}
}
// '.' not found
return false;
}
std::string hjbase::ufunc::Convert_Ptr_To_Str(void* ptr) {
std::stringstream ss;
ss<<ptr;
return ss.str();
}
std::pair<int, int> hjbase::ufunc::Get_First_Element_Pos(const std::string& this_list) {
if(this_list.front()!=cEXPR_START||this_list.back()!=cEXPR_END) {
Signal_Error(TE_NOT_LIST, this_list);
throw type_except;
}
// the previous implicitly checked this_list.size() must be at least 2
if(this_list.size()==2) {
Signal_Error(LE_EMPTY, this_list);
throw eval_except;
}
bool is_quotation = false;
bool is_sublist = false;
// Get the first position which is not a space after cEXPR_START
int start_pos = 1;
for(;start_pos<this_list.size()-1;++start_pos) {
if(!std::isspace(this_list[start_pos])) {
if(this_list[start_pos]==cEXPR_START) {
is_sublist = true;
}
if(this_list[start_pos]==QUOTATION_MARK) {
is_quotation = true;
}
break;
}
}
for(std::string::const_iterator it=this_list.begin()+start_pos+1;it<this_list.end();++it) {
if(is_quotation) {
if(*it==QUOTATION_MARK) {
// exclude QUOTATION mark
return std::pair<int, int>(start_pos+1, it-this_list.begin()-start_pos-1);
}
}
else if(is_sublist) {
if(*it==cEXPR_END) {
// include cEXPR_START and cEXPR_END
return std::pair<int, int>(start_pos, it-this_list.begin()-start_pos+1);
}
}
else {
if(*it==' ') {
// exclude space
return std::pair<int, int>(start_pos, it-this_list.begin()-start_pos);
}
}
}
if(is_quotation||is_sublist) {
Signal_Error(LE_INCOMPLETE, this_list);
throw eval_except;
}
// From previous condition, start_pos must be less or equal to this_list.size()-1
if(start_pos==this_list.size()-1) {
Signal_Error(LE_EMPTY, this_list);
}
return std::pair<int, int>(start_pos, this_list.size()-start_pos-1);
}
std::string hjbase::ufunc::Format_List_String(const std::string& original_lst) {
// construct LISTFORMATTER object
LISTFORMATTER lst_formatter;
for(std::string::const_iterator it=original_lst.begin();it<original_lst.end()-1;++it) {
if(lst_formatter.Take_One_Char(*it)==FORMATTING_COMPLETE) {
Signal_Error(TE_NOT_LIST, original_lst);
throw type_except;
}
}
if(lst_formatter.Take_One_Char(original_lst.back())!=FORMATTING_COMPLETE) {
Signal_Error(TE_NOT_LIST, original_lst);
throw type_except;
}
return lst_formatter.formatted_lst;
}
inline bool hjbase::ufunc::Starts_With(const std::string& this_str, const std::string& start_str) {
if(this_str.substr(0,start_str.size())==start_str) {
return true;
}
return false;
}
inline bool hjbase::ufunc::Is_Numerical(const std::string& this_str) {
for(std::string::const_iterator it=this_str.begin();it<this_str.end();++it) {
if(!isdigit(*it)&&(*it)!='.'&&(*it)!='-') {
return false;
}
}
return true;
}
inline bool hjbase::ufunc::Quotation_Rquired_For_List_Elem(const std::string& this_str) {
if(this_str.front()==cEXPR_START&&this_str.back()==cEXPR_END) {
return false;
}
if(this_str.front()==QUOTATION_MARK&&this_str.back()==QUOTATION_MARK) {
return false;
}
bool ret_flag = false;
for(std::string::const_iterator it=this_str.begin();it<this_str.end();++it) {
if(*it==' ') {
ret_flag = true;
break;
}
}
return ret_flag;
}
bool hjbase::ufunc::Starts_With_Less_Predicate::operator() (const std::string& x, const std::string& y) const {
/*
As the comment said in header file, to make it a valid strict less relation
here we let (Starts_With(x, y)||Starts_With(y, x)) return false, so that (x<y)==(y<x)==false,
which is equal. equivalent key (duplicate) is not allowed to be inserted into the map.
This makes S still satisfy the requirement.
*/
if(Starts_With(x, y)||Starts_With(y, x)) {
return false;
}
return x < y;
}
void hjbase::ufunc::Iterate_Over_Names_Map(const std::map<std::string, std::string>* names,
const void (*f)(const std::string&, const std::string&)) {
for(std::map<std::string, std::string>::const_iterator name_it=names->begin();
name_it!=names->end();++name_it) {
(*f)(name_it->first, name_it->second);
}
}
const_itVecStr::const_itVecStr(std::vector<std::string>::const_iterator from_begin,
std::vector<std::string>::const_iterator from_end)
: this_begin (from_begin)
, this_end (from_end) {
#ifdef itVecStr_RANGE_SAFETY_CHECK
if(this_begin>this_end) {
Signal_Error(IE_CONST_ITVECSTR_OOR, std::string());
throw huaji_except;
}
#endif
}
const_itVecStr::~const_itVecStr() {}
inline std::vector<std::string>::const_iterator const_itVecStr::begin() const {
return this_begin;
}
inline std::vector<std::string>::const_iterator const_itVecStr::end() const {
return this_end;
}
inline std::string const_itVecStr::front() const {
return *(this_begin);
}
inline std::string const_itVecStr::back() const {
return *(this_end-1);
}
inline std::string const_itVecStr::at(int pos) const {
return *(this_begin+pos);
}
inline int const_itVecStr::size() const {
return this_end-this_begin;
}
hjbase::LISTFORMATTER::LISTFORMATTER()
// Not allowed to have space after EXPR_START
: prev_space (true)
, list_depth (0)
, is_in_list_quotation (false) {
formatted_lst = EXPR_START;
}
hjbase::LISTFORMATTER::~LISTFORMATTER() {}
int hjbase::LISTFORMATTER::Take_One_Char(char cur_char) {
if(is_in_list_quotation) {
formatted_lst.push_back(cur_char);
if(cur_char==QUOTATION_MARK) {
is_in_list_quotation = false;
formatted_lst.push_back(' ');
prev_space = true;
}
}
else {
switch (cur_char) {
case cEXPR_END: {
// Replace last char with cEXPR_END if last char is a space
if(formatted_lst.back()==' ') {
formatted_lst[formatted_lst.size()-1] = cEXPR_END;
}
else {
formatted_lst.push_back(cEXPR_END);
}
// not in any other sublist
if(!list_depth) {
return FORMATTING_COMPLETE;
}
// in other sublist, just decrease list_depth
else {
--list_depth;
}
break;
}
case cEXPR_START: {
formatted_lst.push_back(cEXPR_START);
// Not allowed to have space after EXPR_START
prev_space = true;
++list_depth;
break;
}
case QUOTATION_MARK: {
// add a space if no prev_space
if(!prev_space) {
formatted_lst.push_back(' ');
}
formatted_lst.push_back(cur_char);
is_in_list_quotation = true;
break;
}
default: {
if(cur_char==' ') {
if(!prev_space) {
prev_space = true;
formatted_lst.push_back(cur_char);
}
}
else {
formatted_lst.push_back(cur_char);
}
}
}
// Reset prev_space if cur_char is not a space
if(cur_char!=' '&&cur_char!=cEXPR_START) {
prev_space = false;
}
}
return REQUIRE_MORE_CHAR_FOR_COMPLETE_LST;
}
hjbase::HUAJITOKENIZER::HUAJITOKENIZER()
: is_in_quotation (false)
, is_in_block_comment (false)
, is_in_line_comment (false)
, is_in_square_bracket (false)
, is_in_nosubst (false)
, is_in_list (false)
, enable_raw_string (false)
, lst_formatter (nullptr)
, source (nullptr) {
token_queue = new std::queue<std::string>;
}
hjbase::HUAJITOKENIZER::~HUAJITOKENIZER() {
if(lst_formatter) {
delete lst_formatter;
}
delete token_queue;
}
std::string hjbase::HUAJITOKENIZER::Get_One_Token() {
std::string token;
if(token_queue->size()) {
token = token_queue->front();
token_queue->pop();
return token;
}
try {
while(1) {
char cur_char = source->get();
if(cur_char==std::char_traits<char>::eof()) {
// is_in_list store token in lst_formatter object, so token.size() won't work here.
if(token.size()||is_in_list) {
Signal_Error(SE_REQUIRE_MORE_TOKENS, token);
}
throw token_except;
}
// already in quotation mark
else if(is_in_quotation) {
if(cur_char==QUOTATION_MARK) {
is_in_quotation = false;
if(enable_raw_string) {
return token;
}
else {
return token.insert(0, STRING_TAG);
}
}
// characters substitution
else if(cur_char=='\\') {
char next_char = source->get();
switch (next_char) {
case 'n': {
token.push_back('\n');
break;
}
case 'r': {
token.push_back('\r');
break;
}
case 't': {
token.push_back('\t');
break;
}
default:
token.push_back(next_char);
}
}
else {
token.push_back(cur_char);
}
}
else if(is_in_list) {
if(!lst_formatter) {
lst_formatter = new LISTFORMATTER();
}
if(lst_formatter->Take_One_Char(cur_char)==FORMATTING_COMPLETE) {
std::string ret_lst = lst_formatter->formatted_lst;
delete lst_formatter;
// add a LIST_TAG
ret_lst.insert(0, LIST_TAG);
lst_formatter = nullptr;
is_in_list = false;
return ret_lst;
}
}
// in no substitution block
else if(is_in_nosubst) {
if(cur_char==NOSUBST) {
is_in_nosubst = false;
return token;
}
}
// already in block comment
else if(is_in_block_comment) {
if(cur_char=='*') {
if(source->get()=='/') {
is_in_block_comment = false;
}
}
}
// inline comment, exit comment mode if a endline char found
else if(is_in_line_comment) {
if(cur_char=='\n') {
is_in_line_comment = false;
}
}
else {
char next_char = source->peek();
switch (cur_char) {
case '/': {
if(next_char=='*') {
source->get();
is_in_block_comment = true;
if(token.size()) {
return token;
}
}
else if(next_char=='/') {
source->get();
is_in_line_comment = true;
if(token.size()) {
return token;
}
}
else {
token.push_back(cur_char);
}
break;
}
case '<': {
if(next_char=='=') {
token.push_back(cur_char);
token.push_back(source->get());
}
else {
if(token.size()) {
token_queue->push(ENV_START);
return token;
}
return ENV_START;
}
break;
}
case '>': {
if(next_char=='=') {
token.push_back(cur_char);
token.push_back(source->get());
}
else {
if(token.size()) {
token_queue->push(ENV_END);
return token;
}
return ENV_END;
}
break;
}
case '!': {
if(next_char=='=') {
token.push_back(cur_char);
token.push_back(source->get());
}
else {
token.push_back(cur_char);
}
break;
}
case '=': {
if(token.size()) {
token_queue->push(FUNC_EQ);
return token;
}
return FUNC_EQ;
}
case cLIST_TO_EVALUATE: {
if(next_char==cEXPR_START) {
source->get();
if(token.size()) {
token_queue->push(EXPR_START);
token_queue->push(FUNC_LIST);
return token;
}
else {
token_queue->push(FUNC_LIST);
return EXPR_START;
}
}
else {
token.push_back(cLIST_TO_EVALUATE);
}
break;
}
case cLIST_DIRECT: {
if(next_char==cEXPR_START) {
source->get();
is_in_list = true;
if(token.size()) {
return token;
}
}
else {
return LIST_DIRECT;
}
break;
}
case cSLICER: {
if(is_in_square_bracket&&token.size()) {
return token;
}
else {
if(token.size()) {
token_queue->push(SLICER);
return token;
}
else {
return SLICER;
}
}
}
case QUOTATION_MARK: {
is_in_quotation = true;
if(token.size()) {
return token;
}
break;
}
case NOSUBST: {
is_in_nosubst = true;
if(token.size()) {
return token;
}
break;
}
case cSQUARE_BRACKET_START: {
if(token.size()) {
is_in_square_bracket = true;
token_queue->push(FUNC_SLICE);
token_queue->push(token);
return EXPR_START;
}
else {
return SQUARE_BRACKET_START;
}
}
case cSQUARE_BRACKET_END: {
if(token.size()) {
if(is_in_square_bracket) {
token_queue->push(EXPR_END);
is_in_square_bracket = false;
}
else {
token_queue->push(SQUARE_BRACKET_END);
}
return token;
}
else {
if(is_in_square_bracket) {
is_in_square_bracket = false;
return EXPR_END;
}
else {
return SQUARE_BRACKET_END;
}
}
}
case cBLOCK_START: {
if(token.size()) {
token_queue->push(BLOCK_START);
return token;
}
return BLOCK_START;
}
case cBLOCK_END: {
if(token.size()) {
token_queue->push(BLOCK_END);
return token;
}
return BLOCK_END;
}
case cEXPR_START: {
if(token.size()) {
token_queue->push(EXPR_START);
return token;
}
return EXPR_START;
}
case cEXPR_END: {
if(token.size()) {
token_queue->push(EXPR_END);
return token;
}
return EXPR_END;
}
case cSEPARATOR: {
if(token.size()) {
token_queue->push(SEPARATOR);
return token;
}
return SEPARATOR;
}
case cDELIMITER: {
if(token.size()) {
token_queue->push(DELIMITER);
return token;
}
return DELIMITER;
}
default: {
if(std::isspace(cur_char)) {
if(token.size()) {
return token;
}
// ignore if token is empty
}
else {
token.push_back(cur_char);
}
}
}
}
}
}
catch (const std::ios_base::failure& e) {
std::cout<<e.what()<<std::endl;
throw token_except;
}
}
hjbase::FUNC::FUNC(bool with_side_effects, const const_itVecStr& body_iVstr,
const std::map<std::string, std::string>* stack_map, const std::list<std::string>* arg_names, bool lazy)
// use std::vector::vector range constructor
: fbody_container (new std::vector<std::string>(body_iVstr.begin(), body_iVstr.end()))
/*
now fbody should built on fbody_container, as the container for body_iVstr is
allocated on stack by hjbase::HUAJISCRIPTBASE::Entry_Point and will be cleared
once the root command execution completed
*/
, stack_frame_template(stack_map)
, has_side_effects (with_side_effects)
, var_names (arg_names)
, is_lazy (lazy) {
fbody = new const_itVecStr(fbody_container->begin(), fbody_container->end());
}
hjbase::FUNC::~FUNC() {
delete fbody_container;
delete fbody;
delete stack_frame_template;
delete var_names;
}
inline std::map<std::string, std::string>*
hjbase::FUNC::Get_Stack_Frame_Template_Copy_On_Heap() const {
return new std::map<std::string, std::string>(*stack_frame_template);
}
inline std::list<std::string> hjbase::FUNC::Get_Var_Names() const {
return *var_names;
}
inline const_itVecStr hjbase::FUNC::Get_Fbody() {
return *fbody;
}
inline const std::map<std::string, std::string>* hjbase::FUNC::Get_Stack_Frame_Template() const {
return stack_frame_template;
}
hjbase::HUAJISCRIPTBASE::HUAJISCRIPTBASE()
: enable_gc (true)
, collect_status (0)
, collect_length (0)
, enable_float_point (true)
, enable_debug_mode (false)
, current_ast_depth (0)
, enable_raw_string (false)
, allow_side_effects (true)
, last_if_cond (true) {
tokenizer = new HUAJITOKENIZER();
is_console_stack = new std::vector<bool>;
input_stream_stack = new std::vector<std::istream*>;
sourced_history = new std::set<std::string>;
names_stack = new std::vector<std::map<std::string, std::string>*>;
gc_records = new std::map<std::string, std::list<std::string>>;
temp_return_val = UNDEFINED_NAME;
namespace_pool = new std::map<std::string, std::map<std::string, std::string>*>;
Push_Stack_Frame(new std::map<std::string, std::string>);
func_pool = new std::map<std::string, FUNC*>;
object_pool = new std::map<std::string,std::map<std::string,std::string>*>;
object_stack = new std::vector<std::map<std::string, std::string>*>;
lazy_expr_pool = new std::map<std::string, const_itVecStr*>;
istream_pool = new std::map<std::string, std::istream*>;
ostream_pool = new std::map<std::string, std::ostream*>;
array_pool = new std::map<std::string, std::vector<std::string>*>;
}
hjbase::HUAJISCRIPTBASE::~HUAJISCRIPTBASE() {
for(std::vector<std::map<std::string, std::string>*>::iterator it=names_stack->begin();it<names_stack->end();++it) {
delete *it;
}
delete names_stack;
for(std::map<std::string, FUNC*>::iterator it=func_pool->begin();it!=func_pool->end();++it) {
delete it->second;
}
delete func_pool;
for(std::vector<std::map<std::string, std::string>*>::iterator it=object_stack->begin();it<object_stack->end();++it) {
delete *it;
}
delete object_stack;
for(std::map<std::string, std::map<std::string, std::string>*>::iterator it=object_pool->begin();
it!=object_pool->end();++it) {
delete it->second;
}
delete object_pool;
delete gc_records;
for(std::map<std::string, std::map<std::string, std::string>*>::iterator it=namespace_pool->begin();
it!=namespace_pool->end();++it) {
delete it->second;
}
delete namespace_pool;
for(std::map<std::string, const_itVecStr*>::iterator it=lazy_expr_pool->begin();it!=lazy_expr_pool->end();++it) {
delete it->second;
}
delete lazy_expr_pool;
delete tokenizer;
// input_stream_stack should be empty when deleted, so no need to delete istream object.
delete input_stream_stack;
delete is_console_stack;
delete sourced_history;
for(std::map<std::string, std::istream*>::iterator it=istream_pool->begin();it!=istream_pool->end();++it) {
if(it->first!=CIN_PTRSTR) {
delete it->second;
}
}
for(std::map<std::string, std::ostream*>::iterator it=ostream_pool->begin();it!=ostream_pool->end();++it) {
if(it->first!=COUT_PTRSTR) {
delete it->second;
}
}
delete istream_pool;
delete ostream_pool;
for(std::map<std::string, std::vector<std::string>*>::iterator it=array_pool->begin();it!=array_pool->end();++it) {
delete it->second;
}
delete array_pool;
}
std::pair<bool, int> hjbase::HUAJISCRIPTBASE::More_On_Check_If_GC_Required_Level_1(const std::string& ref_val) {
return std::pair<bool, int>(false, -1);
}
std::pair<bool, int> hjbase::HUAJISCRIPTBASE::Check_If_GC_Required(const std::string& ref_val) {
// if ref_val is empty, this violates our set definition
if(ref_val==EMPTY_STRING_OBJECT) {
return std::pair<bool, int>(false, -1);
}
std::map<std::string, int, hjbase::ufunc::Starts_With_Less_Predicate>::const_iterator
res_it = EXTRA_DATA_STRUCTURE_REQUIRED_TYPE_TREE.find(ref_val);
if(res_it==EXTRA_DATA_STRUCTURE_REQUIRED_TYPE_TREE.end()) {
return More_On_Check_If_GC_Required_Level_1(ref_val);
}
// ref_val can't be shorter
if(ref_val.size()<(res_it->first).size()) {
return std::pair<bool, int>(false, -1);
}
return std::pair<bool, int>(true, res_it->second);
}
void hjbase::HUAJISCRIPTBASE::GC_New_Record(const std::string& name, const std::string& val) {
if(!enable_gc) {
return;
}
// Check if val is some type that needs to be recorded for garbage collection
if(Check_If_GC_Required(val).first) {
std::map<std::string, std::list<std::string>>::iterator
record_it = gc_records->find(val);
if(record_it==gc_records->end()) {
// Build record list
std::list<std::string> record_list;
record_list.push_front(name);
gc_records->insert(std::pair<std::string, std::list<std::string>>(val, record_list));
}
else {
// Modify record list
(record_it->second).push_front(name);
}
}
return;
}
void hjbase::HUAJISCRIPTBASE::GC_Delete_Record(const std::string& name, const std::string& val) {
if(!enable_gc) {
return;
}
std::pair<bool, int> res_pair = Check_If_GC_Required(val);
if(res_pair.first) {
std::map<std::string, std::list<std::string>>::iterator
record_it = gc_records->find(val);
if(record_it==gc_records->end()) {
Signal_Error(IE_UNKNOWN, val);
throw huaji_except;
return;
}
for(std::list<std::string>::const_iterator record_list_it=(record_it->second).begin();
record_list_it!=(record_it->second).end();++record_list_it) {
// Check if this record matches name
if(*record_list_it==name) {
// If this is the last name entry in the record
if((record_it->second).size()==1) {
Delete_Internal_Object(val, res_pair.second);
gc_records->erase(val);
}
// just remove this record
else {
(record_it->second).erase(record_list_it);
}
return;
}
}
// Not found
Signal_Error(IE_UNKNOWN, name);
throw huaji_except;
}
}
void hjbase::HUAJISCRIPTBASE::GC_New_MultiRecords(const std::map<std::string, std::string>* names) {
if(enable_gc) {
for(std::map<std::string, std::string>::const_iterator name_it=names->begin();
name_it!=names->end();++name_it) {
GC_New_Record(name_it->first, name_it->second);
}
}
}
void hjbase::HUAJISCRIPTBASE::GC_Delete_MultiRecords(const std::map<std::string, std::string>* names) {
if(enable_gc) {
for(std::map<std::string, std::string>::const_iterator name_it=names->begin();
name_it!=names->end();++name_it) {
GC_Delete_Record(name_it->first, name_it->second);
}
}
}
void hjbase::HUAJISCRIPTBASE::GC_Delete_MultiRecords(const std::vector<std::pair<std::string, std::string>>& names) {
for(std::vector<std::pair<std::string, std::string>>::const_iterator name_it=names.begin();
name_it!=names.end();++name_it) {
GC_Delete_Record(name_it->first, name_it->second);
}
}
void hjbase::HUAJISCRIPTBASE::Delete_Func_Object(const std::string& func_ptrStr) {
std::map<std::string, FUNC*>::const_iterator func_it = func_pool->find(func_ptrStr);
if(func_it==func_pool->end()) {
#ifdef SIGNAL_UNABLE_TO_DELETE
Signal_Error(IE_NOT_DELETABLE, func_ptrStr);
#endif
return;
}
// Delete stack_frame_template
const std::map<std::string, std::string>* stack_map = (func_it->second)->Get_Stack_Frame_Template();
GC_Delete_MultiRecords(stack_map);
// Delete func object and erase node in RBt
delete func_it->second;
func_pool->erase(func_it);
return;
}
void hjbase::HUAJISCRIPTBASE::Delete_Object_Object(const std::string& object_ptrStr) {
std::map<std::string, std::map<std::string, std::string>*>::const_iterator object_it
= object_pool->find(object_ptrStr);
if(object_it==object_pool->end()) {
#ifdef SIGNAL_UNABLE_TO_DELETE
Signal_Error(IE_NOT_DELETABLE, object_ptrStr);
#endif
return;
}
// Delete object content
GC_Delete_MultiRecords(object_it->second);
delete object_it->second;
object_pool->erase(object_it);
return;
}
void hjbase::HUAJISCRIPTBASE::Delete_Lazy_Object(const std::string& lazy_ptrStr) {
std::map<std::string, const_itVecStr*>::const_iterator lazy_it
= lazy_expr_pool->find(lazy_ptrStr);
if(lazy_it==lazy_expr_pool->end()) {
#ifdef SIGNAL_UNABLE_TO_DELETE
Signal_Error(IE_NOT_DELETABLE, lazy_ptrStr);
#endif
return;
}
delete lazy_it->second;
lazy_expr_pool->erase(lazy_it);
return;
}
void hjbase::HUAJISCRIPTBASE::Delete_IS_Object(const std::string& is_ptrStr) {
std::map<std::string, std::istream*>::const_iterator is_it
= istream_pool->find(is_ptrStr);
if(is_it==istream_pool->end()) {
#ifdef SIGNAL_UNABLE_TO_DELETE
Signal_Error(IE_NOT_DELETABLE, is_ptrStr);
#endif
return;
}
delete is_it->second;
istream_pool->erase(is_it);
return;
}
void hjbase::HUAJISCRIPTBASE::Delete_OS_Object(const std::string& os_ptrStr) {
std::map<std::string, std::ostream*>::const_iterator os_it
= ostream_pool->find(os_ptrStr);
if(os_it==ostream_pool->end()) {
#ifdef SIGNAL_UNABLE_TO_DELETE
Signal_Error(IE_NOT_DELETABLE, os_ptrStr);
#endif
return;
}
delete os_it->second;
ostream_pool->erase(os_it);
return;
}
std::vector<std::string>* hjbase::HUAJISCRIPTBASE::Get_Array_Object(const std::string& array_ptrStr) {
std::map<std::string, std::vector<std::string>*>::const_iterator array_it
= array_pool->find(array_ptrStr);
if(array_it==array_pool->end()) {
#ifdef SIGNAL_UNABLE_TO_DELETE
Signal_Error(IE_NOT_DELETABLE, array_ptrStr);
#endif
return nullptr;
}
return array_it->second;
}
void hjbase::HUAJISCRIPTBASE::Delete_Array_Object(const std::string& array_ptrStr) {
std::vector<std::string>* array_ptr = Get_Array_Object(array_ptrStr);
if(!array_ptr) {
return;
}
for(std::vector<std::string>::const_iterator val_it=array_ptr->begin();
val_it<array_ptr->end();++val_it) {
GC_Delete_Record(array_ptrStr, *val_it);
}
}
void hjbase::HUAJISCRIPTBASE::More_On_Delete_Object_Level_1(const std::string& ref_val, int type_code) {
Signal_Error(IE_NOT_DELETABLE, ref_val);