-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfunction.cpp
1498 lines (1344 loc) · 44.6 KB
/
function.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
//
// AYA version 5
//
// 関数を扱うクラス CFunction
// - 主処理部
// written by umeici. 2004
//
#if defined(WIN32) || defined(_WIN32_WCE)
# include "stdafx.h"
#endif
#include <assert.h>
#include <vector>
#include "function.h"
#include "ayavm.h"
#include "log.h"
#include "messages.h"
#include "misc.h"
#include "parser0.h"
#include "globaldef.h"
#include "sysfunc.h"
//////////DEBUG/////////////////////////
#ifdef _WINDOWS
#ifdef _DEBUG
#include <crtdbg.h>
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)
#endif
#endif
#include "basis.h"
////////////////////////////////////////
CFunction::CFunction(CAyaVM& vmr, const yaya::string_t& n, const yaya::string_t& df, int lc) : pvm(&vmr), name(n), dicfilename(df), linecount(lc), dicfilename_fullpath(vmr.basis().ToFullPath(df))
{
statelenm1 = 0;
namelen = name.size();
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::deep_copy_statement
* 機能概要: CStatement配列をディープコピーします
* -----------------------------------------------------------------------
*/
void CFunction::deep_copy_statement(CFunction &from)
{
for ( size_t i = 0 ; i < from.statement.size() ; ++i ) {
statement[i].deep_copy(from.statement[i]);
}
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::CompleteSetting
* 機能概要: 関数の構築が完了した(≒辞書の読み込みが完了した)際に呼ばれます
* 実行の際に必要な最後の初期化処理を行ないます
* -----------------------------------------------------------------------
*/
void CFunction::CompleteSetting(void)
{
statelenm1 = statement.size() - 1;
}
CFunction::ExecutionResult CFunction::Execute() {
CValue arg(F_TAG_ARRAY, 0/*dmy*/);
return Execute(arg);
}
CFunction::ExecutionResult CFunction::Execute(const CValue& arg) {
CLocalVariable lvar(*pvm);
return Execute(arg, lvar);
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::Execute
* 機能概要: 関数を実行します
*
* 引数CValue argは必ず配列型です。arrayが空であれば引数の無いコールとなります
* -----------------------------------------------------------------------
*/
CFunction::ExecutionResult CFunction::Execute(const CValue &arg, CLocalVariable &lvar)
{
int exitcode = ST_NOP;
// _argvを作成
lvar.SetValue(L"_argv", arg);
// _argcを作成
CValue t_argc((yaya::int_t)arg.array_size());
lvar.SetValue(L"_argc", t_argc);
//_FUNC_NAME_を作成
lvar.SetValue(L"_FUNC_NAME_", this->name);
// 実行
if (!pvm->call_limit().AddCall(name)) {
CBasisFuncPos shiori_OnCallLimit;
ptrdiff_t funcpos = shiori_OnCallLimit.Find(*pvm, L"shiori.OnCallLimit");
size_t lock = pvm->call_limit().temp_unlock();
if(funcpos >= 0) {
//_argv[0] = dicname _argv[1] = linenum
CValue arg(F_TAG_ARRAY, 0/*dmy*/);
arg.array().emplace_back(CValueSub(dicfilename));
arg.array().emplace_back(CValueSub((yaya::int_t)linecount));
pvm->function_exec().func[funcpos].Execute(arg);
}
else {
pvm->logger().Error(E_E, 97, dicfilename, linecount);
}
pvm->call_limit().reset_lock(lock);
pvm->call_limit().DeleteCall();
return ExecutionResult(pvm);
}
ExecutionResult result(NULL);
Execute_SEHbody(result,lvar, exitcode);
pvm->call_limit().DeleteCall();
for ( size_t i = 0 ; i < statement.size() ; ++i ) {
statement[i].cell_cleanup();
}
return result;
}
void CFunction::Execute_SEHhelper(CFunction::ExecutionResult& aret, CLocalVariable& lvar, int& exitcode)
{
aret = ExecuteInBrace(1, lvar, BRACE_DEFAULT, exitcode, NULL, 0);
}
void CFunction::Execute_SEHbody(ExecutionResult& retas, CLocalVariable& lvar, int& exitcode)
{
#ifdef _WINDOWS
__try
#else
try
#endif
{
Execute_SEHhelper(retas, lvar, exitcode);
}
#ifdef _WINDOWS
__except (EXCEPTION_EXECUTE_HANDLER)
#else
catch(...)
#endif
{
throw yaya::memory_error();
}
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::ExecuteInBrace
* 機能概要: {}を実行し、結果をひとつ返します
* 引数 type この{}の種別。ただし0~の場合はswitch構文の際の候補抽出位置
* exitcode 終了コード。ST_NOP/ST_BREAK/ST_RETURN/ST_CONTINUE=通常/break/return/continue
*
* "{}"内の各ステートメントを実行します。引数lineで指定される位置から実行を開始し、"}"に突き当たるまで
* 順次実行していきます。
* 返値は実行を終了した"}"の位置です。
* -----------------------------------------------------------------------
*/
CFunction::ExecutionInBraceResult CFunction::ExecuteInBrace(size_t line, CLocalVariable &lvar, yaya::int_t type, int &exitcode, std::vector<CVecValue>* UpperLvCandidatePool,bool inpool)
{
// 開始時の処理
lvar.AddDepth();
CDuplEvInfo* pdupl = statement[line-1].dupl_block.get();
const bool inmutiarea = statement[line-1].ismutiarea; // pool用
// 実行
CSelecter output(pvm, pdupl, (ptrdiff_t)type);
bool exec_end = 0; // この{}の実行を終了するためのフラグ 1で終了
bool ifflg = 0; // if-elseif-else制御用。1でそのブロックを処理したことを示す
bool notpoolblock = 0; // pool用
const bool ispoolbegin = !inpool; // pool用
bool meltblock = 0;
if(pdupl){
if ( pdupl->GetType() & CHOICETYPE_POOL_FLAG ) {
if(!inpool) {
UpperLvCandidatePool = &output.values;
inpool = 1;
}
}
else {
notpoolblock = true;
}
if ( pdupl->GetType() & CHOICETYPE_MELT_FLAG ) {
meltblock = 1;
}
}
if(!UpperLvCandidatePool){
UpperLvCandidatePool = &output.values;
meltblock = 0;
}
const bool inpool_to_next = (!inmutiarea ? !notpoolblock : false);
size_t t_statelenm1 = statelenm1;
size_t i = 0;
for(i = line; i < t_statelenm1; i++) {
CStatement& st = statement[i];
switch(st.type) {
case ST_OPEN: { // "{"
ExecutionInBraceResult info = ExecuteInBrace(i + 1, lvar, BRACE_DEFAULT, exitcode, UpperLvCandidatePool, inpool_to_next);
i = info.linenum;
output.Append(info.Output());
break;
}
case ST_CLOSE: // "}" 注 関数終端の"}"はここを通らない
exec_end = 1;
break;
case ST_COMBINE: // "--"
output.AddArea();
break;
case ST_FORMULA_OUT_FORMULA: // 出力(数式。配列、引数つき関数も含まれる)
output.Append(GetFormulaAnswer(lvar, st));
break;
case ST_FORMULA_SUBST: // 代入
GetFormulaAnswer(lvar, st);
break;
case ST_IF: // if
ifflg = 0;
if (GetFormulaAnswer(lvar, st).GetTruth()) {
ExecutionInBraceResult info = ExecuteInBrace(i + 2, lvar, BRACE_DEFAULT, exitcode, UpperLvCandidatePool, inpool_to_next);
i = info.linenum;
output.Append(info.Output());
ifflg = 1;
}
else
i = st.jumpto;
break;
case ST_ELSEIF: // elseif
if (ifflg)
i = st.jumpto;
else if (GetFormulaAnswer(lvar, st).GetTruth()) {
ExecutionInBraceResult info = ExecuteInBrace(i + 2, lvar, BRACE_DEFAULT, exitcode, UpperLvCandidatePool, inpool_to_next);
i = info.linenum;
output.Append(info.Output());
ifflg = 1;
}
else
i = st.jumpto;
break;
case ST_ELSE: // else
if (ifflg)
i = st.jumpto;
else {
ExecutionInBraceResult info = ExecuteInBrace(i + 2, lvar, BRACE_DEFAULT, exitcode, UpperLvCandidatePool, inpool_to_next);
i = info.linenum;
output.Append(info.Output());
}
break;
case ST_PARALLEL: // parallel
{
const CValue& val = GetFormulaAnswer(lvar, st);
if (val.GetType() == F_TAG_ARRAY) {
for(size_t j = 0; j < val.array_size(); ++j)
output.Append(CValue(val.array()[j]));
}
else
output.Append(val);
}
break;
case ST_VOID: // void
{
//実行だけして捨てる
GetFormulaAnswer(lvar, st);
}
break;
case ST_WHILE: // while
{
size_t loop_max = pvm->call_limit().GetMaxLoop();
size_t loop_cur = 0;
while ( (loop_max == 0) || (loop_max > loop_cur++) ) {
if (!GetFormulaAnswer(lvar, st).GetTruth())
break;
CValue t_value = ExecuteInBrace(i + 2, lvar, BRACE_LOOP, exitcode, UpperLvCandidatePool, inpool_to_next);
output.Append(t_value);
if (exitcode == ST_BREAK) {
exitcode = ST_NOP;
break;
}
else if (exitcode == ST_RETURN)
break;
else if (exitcode == ST_CONTINUE)
exitcode = ST_NOP;
}
if (loop_max && loop_max <= loop_cur ) {
CBasisFuncPos shiori_OnLoopLimit;
ptrdiff_t funcpos = shiori_OnLoopLimit.Find(*pvm, L"shiori.OnLoopLimit");
if (funcpos >= 0) {
//_argv[0] = dicname _argv[1] = linenum
CValue arg(F_TAG_ARRAY, 0/*dmy*/);
arg.array().emplace_back(CValueSub(dicfilename));
arg.array().emplace_back(CValueSub(st.linecount));
pvm->function_exec().func[funcpos].Execute(arg);
}
else {
pvm->logger().Error(E_E, 98, dicfilename, st.linecount);
}
}
i = st.jumpto;
break;
}
case ST_FOR: // for
{
GetFormulaAnswer(lvar, st); //for第一パラメータ
size_t loop_max = pvm->call_limit().GetMaxLoop();
size_t loop_cur = 0;
while ( (loop_max == 0) || (loop_max > loop_cur++) ) {
if (!GetFormulaAnswer(lvar, statement[i + 1]).GetTruth()) //for第二パラメータ
break;
CValue t_value = ExecuteInBrace(i + 4, lvar, BRACE_LOOP, exitcode, UpperLvCandidatePool, inpool_to_next);
output.Append(t_value);
if (exitcode == ST_BREAK) {
exitcode = ST_NOP;
break;
}
else if (exitcode == ST_RETURN)
break;
else if (exitcode == ST_CONTINUE)
exitcode = ST_NOP;
GetFormulaAnswer(lvar, statement[i + 2]); //for第三パラメータ
}
if (loop_max && loop_max <= loop_cur ) {
CBasisFuncPos shiori_OnLoopLimit;
ptrdiff_t funcpos = shiori_OnLoopLimit.Find(*pvm, L"shiori.OnLoopLimit");
if (funcpos >= 0) {
//_argv[0] = dicname _argv[1] = linenum
CValue arg(F_TAG_ARRAY, 0/*dmy*/);
arg.array().emplace_back(CValueSub(dicfilename));
arg.array().emplace_back(CValueSub(st.linecount));
pvm->function_exec().func[funcpos].Execute(arg);
}
else {
pvm->logger().Error(E_E, 98, dicfilename, st.linecount);
}
}
i = st.jumpto;
break;
}
case ST_SWITCH: { // switch
yaya::int_t sw_index = GetFormulaAnswer(lvar, st).GetValueInt();
if (sw_index < 0)
sw_index = BRACE_SWITCH_OUT_OF_RANGE;
ExecutionInBraceResult info = ExecuteInBrace(i + 2, lvar, sw_index, exitcode, NULL, 0);
i = info.linenum;
output.Append(info.Output());
}
break;
case ST_FOREACH: // foreach
Foreach(lvar, output, i, exitcode, UpperLvCandidatePool, inpool_to_next);
i = st.jumpto;
break;
case ST_BREAK: // break
exitcode = ST_BREAK;
break;
case ST_CONTINUE: // continue
exitcode = ST_CONTINUE;
break;
case ST_RETURN: // return
exitcode = ST_RETURN;
break;
default: // 未知のステートメント
pvm->logger().Error(E_E, 82, dicfilename, st.linecount);
break;
};
if (exec_end)
break;
if (exitcode != ST_NOP)
FeedLineToTail(i);
}
#undef POOL_TO_NEXT
// 候補から出力を選び出す 入れ子の深さが0なら重複回避が働く
if (inpool&&!ispoolbegin) {
std::vector<CValue>& thepool = UpperLvCandidatePool->rbegin()->array;
std::vector<CValue>& thispool = output.values[0].array;
if(notpoolblock || inmutiarea)
thepool.insert(thepool.end(), output.Output());
else
thepool.insert(thepool.end(), thispool.begin(), thispool.end());
output.clear();
}
if(meltblock){
std::vector<CValue>& pool_of_uplv = UpperLvCandidatePool->rbegin()->array;
CValue result = output.Output();
if (result.GetType() == F_TAG_ARRAY) {
for(size_t j = 0; j < result.array_size(); ++j) {
pool_of_uplv.emplace_back(CValue(result.array()[j]));
}
}
else
pool_of_uplv.emplace_back(result);
output.clear();
}
// 終了時の処理
lvar.DelDepth();
return CFunction::ExecutionInBraceResult( output,i );
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::Foreach
* 機能概要: foreach処理を行います
* 返値 : 0/1=ループ脱出/ループ実行
*
* 実際に送るのは"}"の1つ手前の行の位置です
* -----------------------------------------------------------------------
*/
void CFunction::Foreach(CLocalVariable &lvar, CSelecter &output,size_t line,int &exitcode, std::vector<CVecValue>* UpperLvCandidatePool, bool inpool)
{
CStatement &st0 = statement[line];
CStatement &st1 = statement[line + 1];
// 代入値を求める
// 注意:foreach中の副作用を回避するため必ず参照にはしないこと
const CValue value = GetFormulaAnswer(lvar, st0);
// 代入値の要素数を求める
// 簡易配列かつ変数からの取得の場合、その変数に設定されているデリミタを取得する
bool isPseudoarray = false;
ptrdiff_t sz;
std::vector<yaya::string_t> s_array;
if (value.IsString()) {
isPseudoarray = true;
yaya::string_t delimiter = VAR_DELIMITER;
if (st0.cell_size() == 1) {
if (st0.cell()[0].value_GetType() == F_TAG_VARIABLE) {
delimiter = pvm->variable().GetDelimiter(st0.cell()[0].index);
}
else if (st0.cell()[0].value_GetType() == F_TAG_LOCALVARIABLE)
delimiter = lvar.GetDelimiter(st0.cell()[0].name);
}
else {
CCell *l_cell = &(st0.cell()[st0.serial()[st0.serial_size() - 1].tindex]);
if (l_cell->value_GetType() == F_TAG_VARIABLE)
delimiter = pvm->variable().GetDelimiter(l_cell->index);
else if (l_cell->value_GetType() == F_TAG_LOCALVARIABLE)
delimiter = lvar.GetDelimiter(l_cell->name);
else if (l_cell->value_GetType() == F_TAG_ARRAYORDER) {
l_cell = &(st0.cell()[st0.serial()[st0.serial_size() - 1].tindex - 1]);
if (l_cell->value_GetType() == F_TAG_VARIABLE)
delimiter = pvm->variable().GetDelimiter(l_cell->index);
else if (l_cell->value_GetType() == F_TAG_LOCALVARIABLE)
delimiter = lvar.GetDelimiter(l_cell->name);
}
}
sz = SplitToMultiString(value.GetValueString(), &s_array, delimiter);
}
else if (value.IsArray()) {
sz = value.array_size();
}
else {
sz = -1;
}
CValue t_value;
int type;
for(int foreachcount = 0; foreachcount < sz; ++foreachcount ) {
// 代入する要素値を取得
if (isPseudoarray) {
t_value = s_array[foreachcount];
}
else {// F_TAG_ARRAY
t_value = value.array()[foreachcount];
}
// 代入
type = st1.cell()[0].value_GetType();
if ( type == F_TAG_VARIABLE ) {
pvm->variable().SetValue(st1.cell()[0].index, t_value);
}
else if ( type == F_TAG_LOCALVARIABLE ) {
lvar.SetValue(st1.cell()[0].name, t_value);
}
else {
pvm->logger().Error(E_E, 28, dicfilename, st1.linecount);
break;
}
t_value = ExecuteInBrace(line + 3, lvar, BRACE_LOOP, exitcode, UpperLvCandidatePool, inpool);
output.Append(t_value);
if (exitcode == ST_BREAK) {
exitcode = ST_NOP;
break;
}
else if (exitcode == ST_RETURN)
break;
else if (exitcode == ST_CONTINUE)
exitcode = ST_NOP;
}
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::GetFormulaAnswer
* 機能概要: 数式を演算して結果を返します
* -----------------------------------------------------------------------
*/
const CValue& CFunction::GetFormulaAnswer(CLocalVariable &lvar, CStatement &st)
{
size_t o_index = 0;
if ( st.serial_size() ) { //高速化用
for (std::vector<CSerial>::iterator it = st.serial().begin(); it != st.serial().end(); it++) {
o_index = it->tindex;
CCell &o_cell = st.cell()[o_index];
if (o_cell.value_GetType() >= F_TAG_ORIGIN_VALUE) {
o_cell.ansv() = GetValueRefForCalc(o_cell, st, lvar);
break;
}
CCell *s_cell = &(st.cell()[it->index[0]]);
CCell *d_cell = NULL;
if ( it->index.size() >= 2 ) {
d_cell = &(st.cell()[it->index[1]]);
}
switch(o_cell.value_GetType()) {
case F_TAG_COMMA:
{
std_shared_ptr<CValue> tmp_ansv = o_cell.ansv_shared_create();
if (Comma(*tmp_ansv.get(), it->index, st, lvar)) {
pvm->logger().Error(E_E, 33, L",", dicfilename, st.linecount);
}
o_cell.ansv_shared() = tmp_ansv;
}
break;
case F_TAG_EQUAL:
case F_TAG_EQUAL_D:
case F_TAG_PLUSEQUAL:
case F_TAG_PLUSEQUAL_D:
case F_TAG_MINUSEQUAL:
case F_TAG_MINUSEQUAL_D:
case F_TAG_MULEQUAL:
case F_TAG_MULEQUAL_D:
case F_TAG_DIVEQUAL:
case F_TAG_DIVEQUAL_D:
case F_TAG_SURPEQUAL:
case F_TAG_SURPEQUAL_D:
case F_TAG_COMMAEQUAL:
{
std_shared_ptr<CValue> tmp_ansv = o_cell.ansv_shared_create();
if (Subst(o_cell.value_GetType(), *tmp_ansv.get(), it->index, st, lvar)) {
pvm->logger().Error(E_E, 33, L"=", dicfilename, st.linecount);
}
o_cell.ansv_shared() = tmp_ansv;
}
break;
case F_TAG_PLUS:
{
// 演算順序を必ず守るため、一旦左辺の結果を取ってから右辺と演算する
// これを怠ると右辺から先に計算する
// 以降同じ
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = lv + GetValueRefForCalc(*d_cell, st, lvar);
break;
}
case F_TAG_MINUS:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = lv - GetValueRefForCalc(*d_cell, st, lvar);
break;
}
case F_TAG_MUL:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = lv * GetValueRefForCalc(*d_cell, st, lvar);
break;
}
case F_TAG_DIV:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = lv / GetValueRefForCalc(*d_cell, st, lvar);
break;
}
case F_TAG_SURP:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = lv % GetValueRefForCalc(*d_cell, st, lvar);
break;
}
case F_TAG_IFEQUAL:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = lv == GetValueRefForCalc(*d_cell, st, lvar);
break;
}
case F_TAG_IFDIFFER:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = lv != GetValueRefForCalc(*d_cell, st, lvar);
break;
}
case F_TAG_IFGTEQUAL:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = lv >= GetValueRefForCalc(*d_cell, st, lvar);
break;
}
case F_TAG_IFLTEQUAL:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = lv <= GetValueRefForCalc(*d_cell, st, lvar);
break;
}
case F_TAG_IFGT:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = lv > GetValueRefForCalc(*d_cell, st, lvar);
break;
}
case F_TAG_IFLT:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = lv < GetValueRefForCalc(*d_cell, st, lvar);
break;
}
case F_TAG_IFIN:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv().SetType(F_TAG_INT);
o_cell.ansv().i_value = _in_(lv,GetValueRefForCalc(*d_cell, st, lvar));
break;
}
case F_TAG_IFNOTIN:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv().SetType(F_TAG_INT);
o_cell.ansv().i_value = not_in_(lv,GetValueRefForCalc(*d_cell, st, lvar));
break;
}
case F_TAG_OR:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = CValue(lv.GetTruth() || GetValueRefForCalc(*d_cell, st, lvar).GetTruth());
break;
}
case F_TAG_AND:
{
const CValue& lv = GetValueRefForCalc(*s_cell, st, lvar);
o_cell.ansv() = CValue(lv.GetTruth() && GetValueRefForCalc(*d_cell, st, lvar).GetTruth());
break;
}
case F_TAG_FUNCPARAM:
{
std_shared_ptr<CValue> tmp_ansv = o_cell.ansv_shared_create();
if (ExecFunctionWithArgs(*tmp_ansv.get(), it->index, st, lvar)) {
pvm->logger().Error(E_E, 33, pvm->function_exec().func[st.cell()[it->index[0]].index].name, dicfilename, st.linecount);
}
o_cell.ansv_shared() = tmp_ansv;
}
break;
case F_TAG_SYSFUNCPARAM:
if (ExecSystemFunctionWithArgs(o_cell, it->index, st, lvar))
pvm->logger().Error(E_E, 33, CSystemFunction::GetNameFromIndex(st.cell()[it->index[0]].index), dicfilename, st.linecount);
break;
case F_TAG_ARRAYORDER:
if (Array(o_cell, it->index, st, lvar))
pvm->logger().Error(E_E, 33, L",", dicfilename, st.linecount);
break;
case F_TAG_FEEDBACK:
if (Feedback(o_cell, it->index, st, lvar))
pvm->logger().Error(E_E, 33, L"&", dicfilename, st.linecount);
break;
case F_TAG_EXC:
o_cell.ansv().SetType(F_TAG_INT);
o_cell.ansv().i_value = !GetValueRefForCalc(*d_cell, st, lvar).GetTruth();
break;
default:
pvm->logger().Error(E_E, 34, dicfilename, st.linecount);
return emptyvalue;
};
}
}
return st.cell()[o_index].ansv();
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::GetValueRefForCalc
* 機能概要: 与えられた項に対応する値へのポインタを取得します
* -----------------------------------------------------------------------
*/
const CValue& CFunction::GetValueRefForCalc(CCell &cell, CStatement &st, CLocalVariable &lvar)
{
// 即値はv、関数/変数/演算子項ならansvから取得 関数/変数の場合その値や実行結果が取得される
// %[n]処理
if (cell.value_GetType() == F_TAG_SYSFUNCPARAM) {
if ( cell.index == CSystemFunction::HistoryIndex() ) {
ExecHistoryP2(cell, st);
}
}
// 演算が完了している(はずの)項ならそれを返す
if (cell.value_GetType() < F_TAG_ORIGIN_VALUE)
return cell.ansv();
// 即値ならそれをそのまま返す
if (cell.value_GetType() <= F_TAG_STRING)
return cell.value();
// 関数なら実行して結果を、変数ならその内容を返す
switch(cell.value_GetType()) {
case F_TAG_STRING_EMBED:
SolveEmbedCell(cell, st, lvar);
return cell.ansv();
case F_TAG_SYSFUNC: {
CValue arg(F_TAG_ARRAY, 0/*dmy*/);
std::vector<CCell *> pcellarg; //dummy
std::vector<CValue> pvaluearg; //dummy
cell.ansv() = pvm->sysfunction().Execute(cell.index, arg, pcellarg, pvaluearg, lvar, st.linecount, this);
return cell.ansv();
}
case F_TAG_USERFUNC: {
CValue arg(F_TAG_ARRAY, 0/*dmy*/);
CLocalVariable t_lvar(*pvm);
cell.ansv() = pvm->function_exec().func[cell.index].Execute(arg, t_lvar);
return cell.ansv();
}
case F_TAG_VARIABLE:
if (pvm->variable().GetPtr(cell.index) != NULL) {
return pvm->variable().GetPtr(cell.index)->call_watcher(*pvm,cell.ansv());
}
else {
return emptyvalue;
}
case F_TAG_LOCALVARIABLE:
if (lvar.GetPtr(cell.name) != NULL) {
return lvar.GetPtr(cell.name)->call_watcher(*pvm,cell.ansv());
}
else {
return emptyvalue;
}
default:
pvm->logger().Error(E_E, 16, dicfilename, st.linecount);
return emptyvalue;
};
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::ReindexUserFunctions
* 機能概要: F_TAG_USERFUNCのindexを再設定します
* -----------------------------------------------------------------------
*/
int CFunction::ReindexUserFunctions(void)
{
int error = 0;
for ( size_t i = 0 ; i < statement.size() ; ++i ) {
CStatement &st = statement[i];
for ( size_t j = 0 ; j < st.cell_size() ; ++j ) {
CCell &cl = st.cell()[j];
if ( cl.value_GetType() == F_TAG_USERFUNC ) {
ptrdiff_t index = pvm->function_parse().GetFunctionIndexFromName(cl.name);
if ( index < 0 ) {
pvm->logger().Error(E_E, 71, dicfilename, st.linecount);
error += 1;
}
else {
cl.index = index;
}
}
}
}
return error;
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::SolveEmbedCell
* 機能概要: %埋め込み付文字列項の値を求めます
* -----------------------------------------------------------------------
*/
void CFunction::SolveEmbedCell(CCell &cell, CStatement &st, CLocalVariable &lvar)
{
// 分割する位置を求める
int solve_src; // 種別 0/1/2/3=ローカル変数/変数/関数/システム関数
size_t max_len = 0; // 最長一致検索用
if (cell.value_const().s_value[0] == L'_') {
// ローカル変数
solve_src = 0;
max_len = lvar.GetMacthedLongestNameLength(cell.value_const().s_value);
}
else {
// 変数
solve_src = 1;
max_len = pvm->variable().GetMacthedLongestNameLength(cell.value_const().s_value);
// 関数
size_t t_len = 0;
for(std::vector<CFunction>::iterator it = pvm->function_exec().func.begin(); it != pvm->function_exec().func.end(); it++)
if (!it->name.compare(0,it->namelen,cell.value_const().s_value,0,it->namelen))
if (t_len < it->namelen)
t_len = it->namelen;
if (t_len > max_len) {
solve_src = 2;
max_len = t_len;
}
// システム関数
if ( max_len < (size_t)CSystemFunction::GetMaxNameLength() ) {
t_len = CSystemFunction::FindIndexLongestMatch(cell.value_const().s_value,max_len);
if (t_len > max_len) {
solve_src = 3;
max_len = t_len;
}
}
}
// 存在しなければ全体が文字列ということになる
if (!max_len) {
cell.ansv() = L"%" + cell.value_const().s_value;
cell.emb_ansv() = L"%" + cell.value_const().s_value;
return;
}
// 関数/システム関数の場合は引数部分を探す
size_t len = cell.value_const().s_value.size();
if (solve_src >= 2) {
size_t depth = 1;
size_t i = 0;
for(i = max_len + 1; i < len; i++) {
depth += ((cell.value_const().s_value[i] == L'(') - (cell.value_const().s_value[i] == L')'));
if (!depth)
break;
}
if (!depth)
max_len = i + 1;
}
// 配列部分を探す
size_t depth = 1;
size_t i = 0;
for(i = max_len + 1; i < len; i++) {
if (!depth && cell.value_const().s_value[i] != L'[')
break;
depth += ((cell.value_const().s_value[i] == L'[') - (cell.value_const().s_value[i] == L']'));
}
if (!depth)
max_len = i;
// 埋め込まれた要素とそれ以降の文字列に分割する
//yaya::string_t s_value(cell.value_const().s_value.substr(0, max_len));
//yaya::string_t d_value(cell.value_const().s_value.substr(max_len, len - max_len));
yaya::string_t::const_iterator it_split = cell.value_const().s_value.begin() + max_len;
yaya::string_t s_value(cell.value_const().s_value.begin(),it_split);
yaya::string_t d_value(it_split,cell.value_const().s_value.end());
// 埋め込まれた要素を数式に変換する 失敗なら全体が文字列
CStatement t_state(ST_FORMULA, st.linecount);
if (pvm->parser0().ParseEmbedString(s_value, t_state, dicfilename, st.linecount)) {
cell.ansv() = L"%" + cell.value_const().s_value;
cell.emb_ansv() = L"%" + cell.value_const().s_value;
return;
}
// 埋め込み要素の値を取得して応答文字列を作成
yaya::string_t result = GetFormulaAnswer(lvar, t_state).GetValueString();
cell.emb_ansv() = result;
cell.ansv() = result + d_value;
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::Comma
* 機能概要: ,演算子を処理します
*
* 返値 : 0/1=成功/エラー
* -----------------------------------------------------------------------
*/
char CFunction::Comma(CValue &answer, std::vector<size_t> &sid, CStatement &st, CLocalVariable &lvar)
{
// 結合して配列値を作成
CValueArray t_array;
for(std::vector<size_t>::iterator it = sid.begin(); it != sid.end(); it++) {
const CValue &addv = GetValueRefForCalc(st.cell()[*it], st, lvar);
if (addv.GetType() == F_TAG_ARRAY) {
t_array.insert(t_array.end(), addv.array().begin(), addv.array().end());
}
else {
t_array.emplace_back(CValueSub(addv));
}
}
answer.SubstToArray(t_array);
return 0;
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::CommaAdd
* 機能概要: ,=演算子を処理します
*
* 返値 : 0/1=成功/エラー
* -----------------------------------------------------------------------
*/
char CFunction::CommaAdd(CValue &answer, std::vector<size_t> &sid, CStatement &st, CLocalVariable &lvar)
{
if ( answer.GetType() != F_TAG_ARRAY ) {
CValueSub st(answer);
answer.SetType(F_TAG_ARRAY);
answer.array().emplace_back(st);
}
CValueArray &t_array = answer.array();
std::vector<size_t>::iterator it = sid.begin();
it++; //最初=左辺は代入先なので飛ばす
for( ; it != sid.end(); it++) {
const CValue &addv = GetValueRefForCalc(st.cell()[*it], st, lvar);
if (addv.GetType() == F_TAG_ARRAY) {
t_array.insert(t_array.end(), addv.array().begin(), addv.array().end());
}
else {
t_array.emplace_back(CValueSub(addv));
}
}
return 0;
}
/* -----------------------------------------------------------------------
* 関数名 : CFunction::Subst
* 機能概要: 代入演算子を処理します
*
* 返値 : 0/1=成功/エラー
* -----------------------------------------------------------------------
*/
char CFunction::Subst(int type, CValue &answer, std::vector<size_t> &sid, CStatement &st, CLocalVariable &lvar)
{
CCell *sid_0_cell = &(st.cell()[sid[0]]);
CCell *sid_1_cell = &(st.cell()[sid[1]]);
int sid_0_cell_type = sid_0_cell->value_GetType();
//既存変数への代入の場合だけは特殊扱いする
if ( sid_0_cell_type == F_TAG_VARIABLE || sid_0_cell_type == F_TAG_LOCALVARIABLE ) {
CVariable* pSubstTo;
if ( sid_0_cell_type == F_TAG_VARIABLE ) {
pSubstTo = pvm->variable().GetPtr(sid_0_cell->index);
}
else {
pSubstTo = lvar.GetPtr(sid_0_cell->name);
}
CValue varback;
if ( pSubstTo ) {
varback = pSubstTo->value();
CValue &substTo = pSubstTo->value();
answer.array_clear();
switch(type) {
case F_TAG_EQUAL:
case F_TAG_EQUAL_D:
substTo = GetValueRefForCalc(*sid_1_cell, st, lvar);
break;
case F_TAG_PLUSEQUAL:
case F_TAG_PLUSEQUAL_D:
substTo += GetValueRefForCalc(*sid_1_cell, st, lvar);
break;
case F_TAG_MINUSEQUAL:
case F_TAG_MINUSEQUAL_D:
substTo -= GetValueRefForCalc(*sid_1_cell, st, lvar);
break;
case F_TAG_MULEQUAL:
case F_TAG_MULEQUAL_D:
substTo *= GetValueRefForCalc(*sid_1_cell, st, lvar);
break;
case F_TAG_DIVEQUAL: