-
Notifications
You must be signed in to change notification settings - Fork 15
/
Cifa.cpp
1379 lines (1333 loc) · 40.9 KB
/
Cifa.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
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 "Cifa.h"
#include <algorithm>
#include <iostream>
#include <sstream>
#define BREAK_POINT(content) \
if (content) { int i = 0; }
namespace cifa
{
Cifa::Cifa()
{
register_function("print", [](ObjectVector& d)
{
for (auto& d1 : d)
{
if (d1.isType<std::string>())
{
std::cout << d1.toString();
}
else
{
std::cout << d1.toDouble();
}
}
std::cout << "\n";
return Object(double(d.size()));
});
register_function("to_string", [](ObjectVector& d)
{
if (d.empty())
{
return Object("");
}
std::ostringstream stream;
stream << d[0].toDouble();
return Object(stream.str());
});
register_function("to_number", [](ObjectVector& d)
{
if (d.empty())
{
return Object();
}
return Object(atof(d[0].toString().c_str()));
});
//parameters["true"] = Object(1, "__");
//parameters["false"] = Object(0, "__");
//parameters["break"] = Object("break", "__");
//parameters["continue"] = Object("continue", "__");
auto ifv = [](ObjectVector& x) -> Object
{
if (x.size() != 3) { return cifa::Object(); }
int x0 = x[0];
double x1 = x[1];
double x2 = x[2];
return (x0) ? x1 : x2;
};
register_function("ifv", ifv);
register_function("ifvalue", ifv);
register_function("pow", [](ObjectVector& x) -> Object
{
if (x.size() <= 1) { return cifa::Object(); }
double x0 = x[0];
double x1 = x[1];
return pow(x0, x1);
});
register_function("max", [](ObjectVector& x) -> Object
{
if (x.size() == 0) { return cifa::Object(); }
if (x.size() == 1)
{
return x[0];
}
double max_val = x[0];
for (int i = 1; i < x.size(); i++)
{
double v = x[i];
if (max_val < v)
{
max_val = v;
}
}
return max_val;
});
register_function("min", [](ObjectVector& x) -> Object
{
if (x.size() == 0) { return cifa::Object(); }
if (x.size() == 1)
{
return x[0];
}
double min_val = x[0];
for (int i = 1; i < x.size(); i++)
{
double v = x[i];
if (min_val > v)
{
min_val = v;
}
}
return min_val;
});
#define REGISTER_FUNCTION(func) \
register_function(#func, [](ObjectVector& x) -> Object \
{ \
if (x.size() == 0) { return cifa::Object(); } \
double x0 = x[0]; \
return func(x0); \
});
REGISTER_FUNCTION(abs);
REGISTER_FUNCTION(sqrt);
REGISTER_FUNCTION(round);
REGISTER_FUNCTION(sin);
REGISTER_FUNCTION(cos);
REGISTER_FUNCTION(tan);
REGISTER_FUNCTION(asin);
REGISTER_FUNCTION(acos);
REGISTER_FUNCTION(atan);
REGISTER_FUNCTION(sinh);
REGISTER_FUNCTION(cosh);
REGISTER_FUNCTION(tanh);
REGISTER_FUNCTION(exp);
REGISTER_FUNCTION(log);
REGISTER_FUNCTION(log10);
}
Object Cifa::eval(CalUnit& c, std::unordered_map<std::string, Object>& p)
{
if (p.count("return"))
{
return p["return"];
}
else if (c.type == CalUnitType::Operator)
{
if (c.v.size() == 1)
{
if (c.str == "+") { return eval(c.v[0], p); }
if (c.str == "-") { return sub(Object(0.0), eval(c.v[0], p)); }
if (c.str == "!") { return !eval(c.v[0], p); }
if (c.str == "++") { return get_parameter(c.v[0], p) = add(get_parameter(c.v[0], p), Object(1)); }
if (c.str == "--") { return get_parameter(c.v[0], p) = add(get_parameter(c.v[0], p), Object(-1)); }
if (c.str == "()++")
{
auto v = get_parameter(c, p);
get_parameter(c.v[0], p) = add(get_parameter(c.v[0], p), Object(1));
return v;
}
if (c.str == "()--")
{
auto v = get_parameter(c, p);
get_parameter(c.v[0], p) = add(get_parameter(c.v[0], p), Object(-1));
return v;
}
}
if (c.v.size() == 2)
{
if (c.str == "." && c.v[0].can_cal())
{
if (c.v[1].type == CalUnitType::Function)
{
if (c.v[1].v[0].type != CalUnitType::None)
{
std::vector<CalUnit> v = { c.v[0] };
expand_comma(c.v[1].v[0], v);
return run_function(c.v[1].str, v, p);
}
else
{
std::vector<CalUnit> v = { c.v[0] };
return run_function(c.v[1].str, v, p);
}
}
if (c.v[1].type == CalUnitType::Parameter)
{
return get_parameter(c.v[0].str + "::" + c.v[1].str, p);
}
}
//.和::作为取成员运算符时,目前只保证一层
if (c.str == "::") { return get_parameter(c.v[0].str + "::" + c.v[1].str, p); }
if (c.str == "*") { return mul(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "/") { return div(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "%") { return int(eval(c.v[0], p)) % int(eval(c.v[1], p)); }
if (c.str == "+") { return add(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "-") { return sub(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == ">") { return more(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "<") { return less(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == ">=") { return more_equal(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "<=") { return less_equal(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "==") { return equal(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "!=") { return not_equal(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "&") { return bit_and(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "|") { return bit_or(eval(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "&&") { return bool(eval(c.v[0], p)) && bool(eval(c.v[1], p)); }
if (c.str == "||") { return bool(eval(c.v[0], p)) || bool(eval(c.v[1], p)); }
if (c.str == "=") { return get_parameter(c.v[0], p) = eval(c.v[1], p); }
if (c.str == "+=") { return get_parameter(c.v[0], p) = add(get_parameter(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "-=") { return get_parameter(c.v[0], p) = sub(get_parameter(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "*=") { return get_parameter(c.v[0], p) = mul(get_parameter(c.v[0], p), eval(c.v[1], p)); }
if (c.str == "/=") { return get_parameter(c.v[0], p) = div(get_parameter(c.v[0], p), eval(c.v[1], p)); }
if (c.str == ",")
{
Object o;
o.v.emplace_back(eval(c.v[0], p));
o.v.emplace_back(eval(c.v[1], p));
return o;
}
if (c.str == "?") //条件1 ? 语句1 : 语句2;
{
if (eval(c.v[0], p)) //比较?运算符左侧的 [条件1]
{
return eval(c.v[1].v[0], p); //取:运算符左侧 [语句1] 的结果
}
else
{
return eval(c.v[1].v[1], p); //取:运算符右侧 [语句2] 的结果
}
}
}
return Object();
}
else if (c.type == CalUnitType::Constant)
{
return Object(atof(c.str.c_str()));
}
else if (c.type == CalUnitType::String)
{
return Object(c.str);
}
else if (c.type == CalUnitType::Parameter)
{
return get_parameter(c, p);
}
else if (c.type == CalUnitType::Function)
{
std::vector<CalUnit> v;
if (!c.v.empty())
{
expand_comma(c.v[0], v);
}
return run_function(c.str, v, p);
}
else if (c.type == CalUnitType::Key)
{
if (c.str == "if") //if(条件1){语句1}else{语句2}
{
if (eval(c.v[0], p)) //判断 [条件1]
{
return eval(c.v[1], p); //取: [语句1] 执行结果
}
else if (c.v.size() >= 3)
{
return eval(c.v[2], p); //取: [语句2] 执行结果
}
return Object(0);
}
if (c.str == "for") //for(语句1;条件1;语句2){语句3}
{
Object o;
for (
eval(c.v[0].v[0], p); //执行 [语句1]
eval(c.v[0].v[1], p); //判断 [条件1]
eval(c.v[0].v[2], p) //执行 [语句2]
)
{
o = eval(c.v[1], p); //执行 [语句3] 并 取执行结果
if (o.type1 == "__" && o.toString() == "break") { break; }
if (o.type1 == "__" && o.toString() == "continue") { continue; }
if (p.count("return")) { return p["return"]; }
}
//o.type = "";
return o;
}
if (c.str == "while") //while (条件1) {语句1}
{
Object o;
while (eval(c.v[0], p)) //判断 [条件1]
{
o = eval(c.v[1], p); //执行 [语句1] 并 取执行结果
if (o.type1 == "__" && o.toString() == "break") { break; }
if (o.type1 == "__" && o.toString() == "continue") { continue; }
if (p.count("return")) { return p["return"]; }
}
//o.type = "";
return o;
}
if (c.str == "do") //do {语句1} while (条件1);
{
Object o;
do
{
o = eval(c.v[0], p); //执行 [语句1] 并 取执行结果
if (o.type1 == "__" && o.toString() == "break") { break; }
if (o.type1 == "__" && o.toString() == "continue") { continue; }
if (p.count("return")) { return p["return"]; }
} while (eval(c.v[1].v[0], p)); //判断 [条件1]
return o;
}
if (c.str == "return")
{
p["return"] = eval(c.v[0], p);
return p["return"];
}
if (c.str == "break")
{
return Object("break", "__");
}
if (c.str == "continue")
{
return Object("continue", "__");
}
if (c.str == "true")
{
return Object(1, "__");
}
if (c.str == "false")
{
return Object(0, "__");
}
}
else if (c.type == CalUnitType::Union)
{
Object o;
for (auto& c1 : c.v)
{
o = eval(c1, p);
if (o.type1 == "__" && o.toString() == "break") { break; }
if (o.type1 == "__" && o.toString() == "continue") { break; }
if (p.count("return")) { return p["return"]; }
}
return o;
}
return Object();
}
void Cifa::expand_comma(CalUnit& c1, std::vector<CalUnit>& v)
{
if (c1.str == ",")
{
for (auto& c2 : c1.v)
{
expand_comma(c2, v);
}
}
else
{
if (c1.type != CalUnitType::None)
{
v.push_back(c1);
}
}
}
CalUnit& Cifa::find_right_side(CalUnit& c1)
{
CalUnit* p = &c1;
while (p->v.size() > 0)
{
p = &(p->v.back());
}
return *p;
}
CalUnitType Cifa::guess_char(char c)
{
if (std::string("0123456789").find(c) != std::string::npos)
{
return CalUnitType::Constant;
}
if (std::string("+-*/%=.!<>&|,?:").find(c) != std::string::npos)
{
return CalUnitType::Operator;
}
if (std::string("_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").find(c) != std::string::npos)
{
return CalUnitType::Parameter;
}
if (std::string("()[]{};").find(c) != std::string::npos)
{
return CalUnitType::Split;
}
if (std::string("\"\'").find(c) != std::string::npos)
{
return CalUnitType::String;
}
return CalUnitType::None;
}
//分割语法
std::list<CalUnit> Cifa::split(std::string& str)
{
std::string r;
std::list<CalUnit> rv;
//删除注释
size_t pos = 0;
while (pos != std::string::npos)
{
if ((pos = str.find("/*", pos)) != std::string::npos)
{
auto pos1 = str.find("*/", pos + 2);
if (pos1 == std::string::npos)
{
pos1 = str.size();
}
else
{
pos1 += 2;
}
for (size_t i = pos; i < pos1; i++)
{
if (str[i] != '\n') { str[i] = ' '; }
}
}
}
pos = 0;
while (pos != std::string::npos)
{
if ((pos = str.find("//", pos)) != std::string::npos)
{
auto pos1 = str.find("\n", pos + 2);
if (pos1 == std::string::npos)
{
pos1 = str.size();
}
std::fill(str.begin() + pos, str.begin() + pos1, ' ');
}
}
CalUnitType stat = CalUnitType::None;
char in_string = 0;
char c0 = 0;
size_t line = 1, col = 0;
for (size_t i = 0; i < str.size(); i++)
{
if (i > 1) { c0 = str[i - 1]; }
auto c = str[i];
col++;
auto pre_stat = stat;
auto g = guess_char(c);
if (in_string)
{
if (stat == CalUnitType::String && in_string == c)
{
in_string = 0;
stat = CalUnitType::None;
}
else
{
stat = CalUnitType::String;
}
}
else if (g == CalUnitType::String)
{
if (in_string == 0)
{
in_string = c;
stat = CalUnitType::String;
}
}
else if (g == CalUnitType::Constant)
{
if (stat == CalUnitType::Constant || stat == CalUnitType::Operator
|| stat == CalUnitType::Split || stat == CalUnitType::None)
{
stat = CalUnitType::Constant;
}
else if (stat == CalUnitType::Parameter)
{
stat = CalUnitType::Parameter;
}
}
else if (g == CalUnitType::Operator)
{
if (c == '.' && stat == CalUnitType::Constant)
{
}
else if ((c == '+' || c == '-') && (c0 == 'E' || c0 == 'e') && stat == CalUnitType::Constant)
{
}
else
{
stat = CalUnitType::Operator;
}
}
else if (g == CalUnitType::Split)
{
stat = CalUnitType::Split;
}
else if (g == CalUnitType::Parameter)
{
if ((c == 'E' || c == 'e') && stat == CalUnitType::Constant)
{
}
else
{
stat = CalUnitType::Parameter;
}
}
else if (g == CalUnitType::None)
{
stat = CalUnitType::None;
}
if (pre_stat != stat || stat == CalUnitType::Operator || stat == CalUnitType::Split)
{
if (pre_stat != CalUnitType::None)
{
CalUnit c(pre_stat, r);
c.line = line;
c.col = col - r.size();
rv.emplace_back(std::move(c));
}
r.clear();
if (g != CalUnitType::String)
{
r = c;
}
}
else
{
r += c;
}
if (c == '\n')
{
col = 0;
line++;
}
}
if (stat != CalUnitType::None)
{
CalUnit c(stat, r);
c.line = line;
c.col = col - r.size();
rv.emplace_back(std::move(c));
}
for (auto it = rv.begin(); it != rv.end(); ++it)
{
//括号前的变量视为函数
if (it->str == "(")
{
if (it != rv.begin() && std::prev(it)->type == CalUnitType::Parameter)
{
std::prev(it)->type = CalUnitType::Function;
if (functions.count(std::prev(it)->str))
{
}
else
{
//脚本中的自定义函数
functions2[std::prev(it)->str] = {};
}
}
}
for (auto& keys1 : keys)
{
if (vector_have(keys1, it->str))
{
it->type = CalUnitType::Key;
}
}
if (vector_have(types, it->str))
{
it->type = CalUnitType::Type;
}
}
//合并多字节运算符
for (auto& ops1 : ops)
{
for (auto& op : ops1)
{
if (op.size() == 2)
{
for (auto it = rv.begin(); it != rv.end();)
{
auto itr = std::next(it);
if (itr != rv.end()
&& it->str == std::string(1, op[0]) && itr->str == std::string(1, op[1])
&& it->line == itr->line && it->col == itr->col - 1)
{
it->str = op;
it = rv.erase(std::next(it));
}
else
{
++it;
}
}
}
}
}
//不处理类型符号
for (auto it = rv.begin(); it != rv.end();)
{
if (it->type == CalUnitType::Type)
{
//记录下类型符号曾经存在的位置
auto itr = std::next(it);
if (itr != rv.end()
&& (itr->type == CalUnitType::Parameter || itr->type == CalUnitType::Function))
{
itr->with_type = true;
it = rv.erase(it);
}
else
{
++it;
}
}
else
{
++it;
}
}
return rv;
}
//表达式语法树
//参数含义:是否合并{},是否合并[],是否合并()
CalUnit Cifa::combine_all_cal(std::list<CalUnit>& ppp, bool curly, bool square, bool round)
{
//合并{}
if (curly) { combine_curly_bracket(ppp); }
//合并[]
if (square) { combine_square_bracket(ppp); }
//合并()
if (round) { combine_round_bracket(ppp); }
//合并算符
combine_ops(ppp);
//检查分号正确性并去除
combine_semi(ppp);
//合并关键字
combine_keys(ppp);
combine_functions2(ppp);
//到此处应仅剩余单独分号(空语句)、语句、语句组,只需简单合并即可
//即使只有一条语句也必须返回Union!
CalUnit c;
c.type = CalUnitType::Union;
if (ppp.size() == 0)
{
return c;
}
else
{
c.line = ppp.front().line;
c.col = ppp.front().col;
for (auto it = ppp.begin(); it != ppp.end(); ++it)
{
if (it->type != CalUnitType::Split)
{
c.v.emplace_back(std::move(*it));
}
}
return c;
}
}
//查找现有最内层括号,并返回位置
std::list<CalUnit>::iterator Cifa::inside_bracket(std::list<CalUnit>& ppp, std::list<CalUnit>& ppp2, const std::string& bl, const std::string& br)
{
bool have = false;
auto it = ppp.begin();
for (; it != ppp.end(); ++it)
{
if (it->str == bl || it->str == br)
{
have = true;
break;
}
}
if (!have)
{
return ppp.end();
}
if (it->str == br)
{
add_error(*it, "unpaired right bracket %s", it->str.c_str());
return ppp.end();
}
auto itl0 = it, itr0 = ppp.end();
for (auto itr = it; itr != ppp.end(); ++itr)
{
if (itr->str == br)
{
itr0 = itr;
for (auto itl = std::prev(itr); itl != ppp.begin(); --itl)
{
if (itl->str == bl)
{
itl0 = itl;
break;
}
}
break;
}
}
if (itr0 == ppp.end())
{
add_error(*it, "unpaired left bracket %s", it->str.c_str());
return ppp.end();
}
ppp2.splice(ppp2.begin(), ppp, std::next(itl0), itr0);
return itl0;
}
void Cifa::combine_curly_bracket(std::list<CalUnit>& ppp)
{
while (true)
{
std::list<CalUnit> ppp2;
auto it = inside_bracket(ppp, ppp2, "{", "}");
if (it == ppp.end())
{
break;
}
auto c1 = combine_all_cal(ppp2, false, true, true); //此处合并多行
c1.str = "{}";
c1.line = it->line;
c1.col = it->col;
it = ppp.erase(it);
*it = std::move(c1);
}
}
void Cifa::combine_square_bracket(std::list<CalUnit>& ppp)
{
while (true)
{
std::list<CalUnit> ppp2;
auto it = inside_bracket(ppp, ppp2, "[", "]");
if (it == ppp.end())
{
break;
}
auto c1 = combine_all_cal(ppp2, true, false, true);
c1.str = "[]";
c1.line = it->line;
c1.col = it->col;
it = ppp.erase(it);
*it = std::move(c1);
if (it != ppp.begin())
{
if (std::prev(it)->type == CalUnitType::Parameter)
{
std::prev(it)->v = { *it };
ppp.erase(it);
}
}
}
}
void Cifa::combine_round_bracket(std::list<CalUnit>& ppp)
{
while (true)
{
std::list<CalUnit> ppp2;
//auto size = ppp.size();
auto it = inside_bracket(ppp, ppp2, "(", ")");
if (it == ppp.end())
{
break;
}
it = ppp.erase(it);
auto c1 = combine_all_cal(ppp2, true, true, false);
c1.str = "()";
c1.line = it->line;
c1.col = it->col;
if (c1.v.size() == 0)
{
it->type = CalUnitType::None;
}
else if (c1.v.size() == 1)
{
*it = std::move(c1.v[0]);
}
else
{
*it = std::move(c1);
}
//括号前
if (it != ppp.begin())
{
auto itl = std::prev(it);
if (itl->type == CalUnitType::Function || itl->type == CalUnitType::Parameter || itl->type == CalUnitType::Constant)
{
itl->v = { std::move(*it) };
ppp.erase(it);
}
else if (itl->type == CalUnitType::Key && vector_have(keys[2], itl->str))
{
itl->v = { std::move(*it) };
ppp.erase(it);
}
}
}
}
void Cifa::combine_ops(std::list<CalUnit>& ppp)
{
for (const auto& ops1 : ops)
{
for (auto& op : ops1)
{
bool is_right = false;
if (vector_have(ops_single, op) || vector_have(ops_right, op) || op == "+" || op == "-") //右结合
{
auto it = ppp.end();
for (; it != ppp.begin();)
{
--it;
if (it->type == CalUnitType::Operator && it->str == op && it->v.size() == 0)
{
if (it == ppp.begin() || vector_have(ops_single, it->str)
|| !std::prev(it)->can_cal() && (op == "+" || op == "-")) //+-退化为单目运算的情况
{
is_right = true;
auto itr = std::next(it);
bool is_single = false;
if (itr != ppp.end())
{
if ((it->str == "+" || it->str == "-")
&& (itr->type == CalUnitType::Constant || itr->type == CalUnitType::Function || itr->type == CalUnitType::Parameter)
|| (it->str == "++" || it->str == "--")
&& itr->type == CalUnitType::Parameter)
{
it->v = { std::move(*itr) };
it = ppp.erase(itr);
is_single = true;
}
}
if (!is_single && it != ppp.begin() && (it->str == "++" || it->str == "--") && std::prev(it)->type == CalUnitType::Parameter)
{
it->v = { std::move(*std::prev(it)) };
it->str = "()" + it->str;
it = ppp.erase(std::prev(it));
}
}
else
{
if (it->str != "+" && it->str != "-")
{
is_right = true;
auto itr = std::next(it);
if (itr != ppp.end())
{
it->v = { std::move(*std::prev(it)), std::move(*itr) };
ppp.erase(itr);
it = ppp.erase(std::prev(it));
}
}
}
}
}
}
if (!is_right) //未能成功右结合则判断为左结合
{
for (auto it = ppp.begin(); it != ppp.end();)
{
if (it->type == CalUnitType::Operator && it->str == op && it->v.size() == 0 && it != ppp.begin())
{
auto itr = std::next(it);
if (itr != ppp.end())
{
it->v = { std::move(*std::prev(it)), std::move(*itr) };
ppp.erase(itr);
it = ppp.erase(std::prev(it));
}
}
++it;
}
}
}
}
}
void Cifa::combine_semi(std::list<CalUnit>& ppp)
{
for (auto it = ppp.begin(); it != ppp.end();)
{
if (it->can_cal())
{
auto itr = std::next(it);
if (itr != ppp.end() && itr->str == ";")
{
it->suffix = true;
it = ppp.erase(itr);
}
else
{
++it;
}
}
else
{
++it;
}
}
}
void Cifa::combine_keys(std::list<CalUnit>& ppp)
{
//合并关键字,从右向左
auto it = ppp.end();
while (it != ppp.begin())
{
--it;
for (size_t para_count = 1; para_count < keys.size(); para_count++)
{
auto& keys1 = keys[para_count];
if (it->type == CalUnitType::Key && it->v.size() < para_count && vector_have(keys1, it->str))
{
auto itr = std::next(it);
if (itr != ppp.end())
{
it->v.emplace_back(std::move(*itr));
itr = ppp.erase(itr);
}
}
if (it->str == "if" && it->v.size() == 2 && std::next(it) != ppp.end())
{
auto itr = std::next(it);
if (itr->str == "else")
{
it->v.emplace_back(std::move(*itr));
ppp.erase(itr);
if (!it->v[2].v.empty())
{
auto it_else = std::move(it->v[2].v[0]);
it->v[2] = std::move(it_else); //cannot assign directly when debug
//it->v[2] = std::move(it->v[2].v[0]);
}
}
}
if (it->str == "do" && it->v.empty() && std::next(it) != ppp.end())
{
auto itr1 = std::next(it);
auto itr2 = std::next(itr1);
if (itr1->str == "{}" && itr2->str == "while") //必须后面接 {} 和 while
{
it->v.emplace_back(std::move(*itr1));
it->v.emplace_back(std::move(*itr2));
ppp.erase(itr1);
ppp.erase(itr2);
}
}
}
}
}
void Cifa::combine_types(std::list<CalUnit>& ppp) {}
void Cifa::combine_functions2(std::list<CalUnit>& ppp)
{
//合并关键字,从右向左
auto it = ppp.end();
while (it != ppp.begin())
{
--it;
if (it->type == CalUnitType::Function && !it->suffix)
{
auto itr = std::next(it);
if (itr != ppp.end() && itr->type == CalUnitType::Union && itr->str == "{}")
{
Function2 f;
f.body = std::move(*itr);
for (auto& c : it->v)
{
f.arguments.emplace_back(std::move(c.str));
}
functions2[it->str] = std::move(f);
ppp.erase(itr);
it = ppp.erase(it);
}
}
}
}
void Cifa::register_function(const std::string& name, func_type func)
{
functions[name] = func;
}
void Cifa::register_user_data(const std::string& name, void* p)
{
user_data[name] = p;
}
void Cifa::register_parameter(const std::string& name, Object o)