-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathvariableSystem.cpp
More file actions
1595 lines (1496 loc) · 60.6 KB
/
variableSystem.cpp
File metadata and controls
1595 lines (1496 loc) · 60.6 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
// variableSystem.cpp - manage user variables ($variables)
#include "common.h"
int bbb = 0;
#ifdef INFORMATION
There are 5 kinds of variables.
1. User variables beginning wih $(regular and transient which begin with $$)
2. Wildcard variables beginning with _
3. Fact sets beginning with @
4. Function variables beginning with ^
5. System variables beginning with %
#endif
HEAPREF userVariableThreadList = NULL;
STACKREF stackedUserVariableThreadList = NULL;
int impliedSet = ALREADY_HANDLED; // what fact set is involved in operation
int impliedWild = ALREADY_HANDLED; // what wildcard is involved in operation
char impliedOp = 0; // for impliedSet, what op is in effect += =
HEAPREF variableChangedThreadlist = NULL;
char* nullLocal = "``";
char* nullGlobal = "";
int wildcardIndex = 0;
char wildcardOriginalText[MAX_WILDCARDS + 1][MAX_MATCHVAR_SIZE + 1]; // spot wild cards can be stored
char wildcardCanonicalText[MAX_WILDCARDS + 1][MAX_MATCHVAR_SIZE + 1]; // spot wild cards can be stored
unsigned int wildcardPosition[MAX_WILDCARDS + 1]; // spot it started and ended in sentence
char wildcardSeparator[2];
bool wildcardSeparatorGiven = false;
bool legacyMatch = true;
// list of active variables needing saving
WORDP tracedFunctionsList[MAX_TRACED_FUNCTIONS];
HEAPREF kernelVariableThreadList = NULL;
HEAPREF botVariableThreadList = NULL;
unsigned int tracedFunctionsIndex;
unsigned int modifiedTraceVal = 0;
bool modifiedTrace = false;
unsigned int modifiedTimingVal = 0;
bool modifiedTiming = false;
void InitVariableSystem()
{
wildcardSeparatorGiven = false;
*wildcardSeparator = ' ';
wildcardSeparator[1] = 0;
kernelVariableThreadList = botVariableThreadList = userVariableThreadList = NULL;
tracedFunctionsIndex = 0;
}
void OpenBotVariables()
{
}
void CloseBotVariables()
{
NoteBotVariables(); // these go into level 1
LockLayer(); // dont write out details of layer
}
void SetBotVariable(char* word)
{
char* eq = strchr(word, '=');
if (eq)
{
*eq = 0;
*word = USERVAR_PREFIX;
if (eq[1] == '"')
{
++eq;
size_t len = strlen(eq);
if (eq[len - 1] == '"') eq[len - 1] = 0;
}
SetUserVariable(word, eq + 1);
// need to restore the initial markers so that a restart can process them
*eq = '=';
*word = 'V';
}
}
static void ReadBotVariable(char* file)
{
char buffer[5000];
char word[MAX_WORD_SIZE];
FILE* in = FopenReadOnly(file);
if (in)
{
while (ReadALine(buffer, in, MAX_WORD_SIZE) >= 0)
{
char* x = strchr(buffer, '='); // var assign maybe
if (*buffer == 'V' && x && x[1] == '"')
{
strcpy(word, buffer);
}
else ReadCompiledWord(buffer, word);
if (*word == 'V')
SetBotVariable(word); // these are level 1 values
}
FClose(in);
}
}
void InitBotVariables(int argc, char** argv)
{
OpenBotVariables();
// the fixed cs init files
ReadBotVariable(configFile);
ReadBotVariable(configFile2);
char word[MAX_WORD_SIZE];
for (int i = 1; i < argc; ++i) // load individual variable declarations on command line
{
strcpy(word, argv[i]);
if (*word == '"' && word[1] == 'V')
{
memmove(word, word + 1, strlen(word));
size_t len = strlen(word);
if (word[len - 1] == '"') word[len - 1] = 0;
}
if (*word == 'V') SetBotVariable(word); // predefined bot variable in level 1
}
for (int i = 0; i < configLinesLength; i++) // load url-remote supplemental config lines
{
char* line = configLines[i];
if (*line == 'V') SetBotVariable(line); // these are level 1 values
}
CloseBotVariables();
kernelVariableThreadList = botVariableThreadList; // switch variables to kernel
botVariableThreadList = NULL;
}
void ReadVariables(const char* name)
{
FILE* in = FopenReadOnly(name);
if (!in) return;
ReadALine(readBuffer, in); // checkstamp
int stamp = atoi(readBuffer);
if (stamp != CHECKSTAMPRAW)
{
FClose(in);
EraseTopicFiles(0, "0");
EraseTopicFiles(1, "1");
printf("Erase your TOPIC folder, rerun CS and recompile your bot. Data formats have changed\r\n");
ReportBug("FATAL: Erase your TOPIC folder, rerun CS and recompile your bot. Data formats have changed\r\n");
return;
}
int oldlanguage = language_bits;
while (ReadALine(readBuffer, in) >= 0)
{
if (*readBuffer == '#') break; // #`end variables
char word[MAX_WORD_SIZE];
language_bits = oldlanguage;
char* ptr = ReadToken(readBuffer, word);
char* eq = strchr(word, '=');
if (!eq) ReportBug((char*)"Bad fact file user var assignment %s", word);
else
{
*eq = 0;
SetUserVariable(word, eq + 1);
WORDP D = FindWord(word);
if (monitorChange)
{
AddWordItem(D, false);
D->internalBits |= BIT_CHANGED;
}
}
}
}
int GetWildcardID(char* x) // wildcard id is "_10" or "_3"
{
if (!IsDigit(x[1])) return ILLEGAL_MATCHVARIABLE;
unsigned int n = x[1] - '0';
char c = x[2];
if (IsDigit(c)) n = (n * 10) + (c - '0');
return (n > MAX_WILDCARDS) ? ILLEGAL_MATCHVARIABLE : n;
}
static void CompleteWildcard()
{
WORDP D = FindWord(wildcardCanonicalText[wildcardIndex]);
if (D && D->properties & D->internalBits & UPPERCASE_HASH) // but may not be found if original has plural or such or if uses _
{
strcpy(wildcardCanonicalText[wildcardIndex], D->word);
}
++wildcardIndex;
if (wildcardIndex > MAX_WILDCARDS) wildcardIndex = 0;
}
static void FlipSeparator(char* word, char* buffer)
{
char oppositeSeparator = 0;
if (*wildcardSeparator == ' ') oppositeSeparator = '_';
else if (*wildcardSeparator == '_') oppositeSeparator = ' ';
if (strchr(word, *wildcardSeparator)) // need to change wanted notation to what we are supposed to return
{
strcpy(buffer, word);
char* at = buffer;
while ((at = strchr(at, *wildcardSeparator))) *at = oppositeSeparator;
}
}
static void JoinMatch(unsigned int start, unsigned int end, unsigned int index, bool inpattern,MARKDATA* hitdata)
{
// ignore invalid index
if (index > MAX_WILDCARDS) return;
// concatenate the match value
bool started = false;
bool proper = false;
unsigned int realend = end & REMOVE_SUBJECT;
if (hitdata && hitdata->disjoint && !unmarked[hitdata->disjoint])
{
strcat(wildcardOriginalText[index], wordStarts[hitdata->disjoint]);
strcat(wildcardOriginalText[index], "_");
strcat(wildcardCanonicalText[index], wordCanonical[hitdata->disjoint] ? wordCanonical[hitdata->disjoint] : wordStarts[hitdata->disjoint]);
strcat(wildcardCanonicalText[index], "_");
}
// generate the std expected memorizations
for (unsigned int i = start; i <= realend; ++i)
{
if (unmarked[i] || i < 1 || i > wordCount) continue; // ignore words masked or off end
char* word = wordStarts[i];
if (!word) continue;
if (started)
{
// no separator on japanese words in japanese
unsigned char japanletter[8];
int kind = 0;
bool japanese = false;
if (!japanese) japanese = csapicall == TEST_PATTERN && IsJapanese(word, (unsigned char*)&japanletter, kind);
if (!kind)
{
strcat(wildcardOriginalText[index], wildcardSeparator);
strcat(wildcardCanonicalText[index], wildcardSeparator);
}
}
else started = true;
if (IsUpperCase(*word) && word[1]) proper = true; // may be proper name
strcat(wildcardOriginalText[index], word);
strcat(wildcardCanonicalText[index], wordCanonical[i] ? wordCanonical[i] : word);
}
// we have found phrases using appropriate wildcard separator to be used
if (start == realend || proper) {} // single word or proper name
// if not a proper name and any are unknown, the composite is unknown unless it is an entire sentence grab, then preserve pieces
else if (strstr(wildcardCanonicalText[index], "unknown-word") && (start != 1 || realend != wordCount))
strcpy(wildcardCanonicalText[index], "unknown-word");
// proper names canonical is same, but merely having 1 uppercase is not proper if multiword "I live here" _*
WORDP D = NULL;
char* word = AllocateBuffer();
WORDP foundword = (hitdata && hitdata->word) ? Meaning2Word(hitdata->word) : NULL;
// specific uppercase form requested, but we will not use it if it was not from user input but merely ^mark value
int len = strlen(wildcardOriginalText[index]);
int lenc = strlen(wildcardCanonicalText[index]);
if (foundword) D = foundword; // need a specific capitalization ?
if (!legacyMatch)
{
if (D) // use what we matched in canonical from concept, leaving original alone
{
// for a concept match, we have what user typed (original) and the actual cased match (canonical)
strcpy(wildcardCanonicalText[index], D->word); // we match a specific request, it becomes canonical automatically
}
}
else
{
if (D && D->internalBits & UPPERCASE_HASH) // we match a request, it becomes original and canonical
{
strcpy(wildcardOriginalText[index], D->word);
strcpy(wildcardCanonicalText[index], D->word);
}
}
if (!D) // we do what we can with the match when not from concept set
{
D = FindWord(wildcardOriginalText[index], len);
if (D) strcpy(wildcardOriginalText[index], D->word); // use capitalization from dictionary
else if (start != end) // consider if alternate is in dictionary (we used a separator)
{
FlipSeparator(wildcardOriginalText[index], word);
D = FindWord(word, len);
if (D) strcpy(wildcardOriginalText[index], D->word);
}
D = FindWord(wildcardCanonicalText[index], lenc);
if (D)
{
strcpy(wildcardCanonicalText[index], D->word); // use capitalization from dictionary
}
else if (start != realend) // consider if alternate is in dictionary (we used a separator)
{
FlipSeparator(wildcardCanonicalText[index], word);
D = FindWord(word, lenc);
if (D) strcpy(wildcardCanonicalText[index], D->word);
}
}
FreeBuffer();
if (trace & TRACE_OUTPUT && !inpattern && CheckTopicTrace()) Log(USERLOG,"_%d=%s/%s ", index, wildcardOriginalText[index], wildcardCanonicalText[index]);
}
void SetWildCardGiven(unsigned int start, unsigned int end, bool inpattern, int index,MARKDATA* hitdata)
{
// ignore invalid index
if (index > MAX_WILDCARDS) return;
if (end < start) end = start; // matched within a token
if (end > wordCount)
{
if (start != end) end = wordCount + 1; // for start==end we allow being off end, eg _>
else start = end = wordCount + 1;
}
*wildcardOriginalText[index] = 0;
*wildcardCanonicalText[index] = 0;
while (unmarked[start]) ++start; // skip over unmarked words at start
while (unmarked[end]) --end; // skip over unmarked words at end
wildcardPosition[index] = start | WILDENDSHIFT(end);
if (start == 0 || wordCount == 0 || (end == 0 && start != 1)) // null match, like _{ .. }
{
}
else JoinMatch(start, end, index, inpattern,hitdata); // did match
}
void SetWildCardNull()
{
SetWildCardGivenValue((char*)"", (char*)"",(unsigned int) -1, (unsigned int)-1, wildcardIndex,NULL);
CompleteWildcard();
}
void SetWildCardGivenValue(char* original, char* canonical,unsigned int start,unsigned int end, int index,MARKDATA* hitdata)
{
// ignore invalid index
if (index > MAX_WILDCARDS) return;
unsigned int realend = end & REMOVE_SUBJECT;
if (realend < start) realend = end = start; // matched within a token
if (realend > wordCount && start != realend) realend = wordCount; // for start==end we allow being off end, eg _>
*wildcardOriginalText[index] = 0;
*wildcardCanonicalText[index] = 0;
if ((int)start <= 0 || wordCount == 0 || (realend <= 0 && start != 1)) // null match, like _{ .. }
{
}
else JoinMatch(start, end, index, false,hitdata); // did match
if (start == 0) start = end = 1;
if (start == -1) start = end = 0;
if (!*original) end = start = 0; // no match found, indicate no position
wildcardPosition[index] = start | WILDENDSHIFT(end);
}
void SetWildCardIndexStart(int index)
{
wildcardIndex = index;
// CANNOT do this while pearl is badly coded legal bot
//wildcardPosition[index] = 0;
//*wildcardOriginalText[index] = 0;
//*wildcardCanonicalText[index] = 0;
}
void SetWildCard(char* value, char* canonicalValue, const char* index, unsigned int position)
{
// adjust values to assign
if (!value) value = "";
if (!canonicalValue) canonicalValue = "";
if (strlen(value) > MAX_MATCHVAR_SIZE)
{
value[MAX_MATCHVAR_SIZE] = 0;
ReportBug((char*)"Too long matchvariable original value %s", value);
}
if (strlen(canonicalValue) > MAX_MATCHVAR_SIZE)
{
canonicalValue[MAX_MATCHVAR_SIZE] = 0;
ReportBug((char*)"Too long matchvariable canonical value %s", value);
}
while (value[0] == ' ') ++value;
while (canonicalValue && canonicalValue[0] == ' ') ++canonicalValue;
// store the values
if (index) wildcardIndex = GetWildcardID((char*)index);
strcpy(wildcardOriginalText[wildcardIndex], value);
strcpy(wildcardCanonicalText[wildcardIndex], (canonicalValue) ? canonicalValue : value);
wildcardPosition[wildcardIndex] = position | WILDENDSHIFT(position);
CompleteWildcard();
}
char* GetwildcardText(unsigned int i, bool canon)
{
if (i > MAX_WILDCARDS) return "";
return canon ? wildcardCanonicalText[i] : wildcardOriginalText[i];
}
char* GetUserVariable(const char* word, bool nojson)
{
if (!dictionaryLocked && !compiling && !rebooting && currentBeforeLayer != LAYER_BOOT) return "";
int len = 0;
char* separator = (nojson) ? NULL : (char*)strchr(word, '.');
char* bracket = (nojson) ? NULL : (char*)strchr(word, '[');
if (!separator && bracket) separator = bracket;
if (bracket && bracket < separator) separator = bracket; // this happens first
if (separator) len = separator - word;
bool localvar = strstr(word, "$_") != NULL; // local var anywhere in the chain?
char* item = NULL;
char* answer;
char flagsValue[20];
*flagsValue = 0;
WORDP D = FindWord(word, len, LOWERCASE_LOOKUP);
if (!D)
{
if (trace & TRACE_VARIABLE && separator) Log(USERLOG, "%s''", word);
goto NULLVALUE; // no such variable
}
item = D->w.userValue;
if (!item)
{
if (trace & TRACE_VARIABLE && separator) Log(USERLOG, "%s`null`'", D->word);
goto NULLVALUE;
}
if (localvar && *item == LCLVARDATA_PREFIX && item[1] == LCLVARDATA_PREFIX) item += 2; // skip `` marker
if (trace & TRACE_VARIABLE && separator) Log(USERLOG, "%s", D->word);
if (separator) // json object or array follows this
{
LOOPDEEPER:
if (trace & TRACE_VARIABLE && separator) Log(USERLOG, "`%s`%c", item,*separator);
FACT* factvalue = NULL;
if (IsDigitWord(item, AMERICAN_NUMBERS) && (!strnicmp(separator, ".subject", 8) || !strnicmp(separator, ".verb", 5) || !strnicmp(separator, ".object", 7) || !strnicmp(separator, ".flags", 6))) // fact id?
{
int val = atoi(item);
factvalue = Index2Fact(val);
if (!factvalue) goto NULLVALUE;
}
else D = FindWord(item); // the basic item
if (!D)
{
if (trace & TRACE_VARIABLE) Log(USERLOG, "``%s",separator+1);
goto NULLVALUE;
}
// auto indirect if variable has a variable value
if (separator && D->w.userValue && D->w.userValue[0] == '$')
{
D = FindWord(D->w.userValue);
}
if (*separator == '.' && !strnicmp(item, "ja-", 3)) // dot into array means find value as object
{
WORDP key = FindWord(separator + 1); // only can go 1 level here
FACT* F = GetSubjectNondeadHead(D);
while (F)
{
if (Meaning2Word(F->object) == key) break;
F = GetSubjectNondeadNext(F);
}
if (!F) goto NULLVALUE;
answer = Meaning2Word(F->verb)->word;
goto ANSWER;
}
if (*separator == '.' && strncmp(item, "jo-", 3) && !factvalue) goto NULLVALUE; // cannot be dotted
//else if (*separator == '[' && strncmp(item, "ja-", 3)) goto NULLVALUE; // cannot be indexed
// is there more later
char* separator1 = (char*)strchr(separator + 1, '.'); // more dot like $x.y.z?
char* bracket1 = (char*)strchr(separator + 1, '['); // more [ like $x[y][z]?
if (!bracket1) bracket1 = (char*)strchr(separator + 1, ']'); // is there a closer as final
if (bracket1 && !separator1) separator1 = bracket1;
if (bracket1 && bracket1 < separator1) separator1 = bracket1;
// get the label after this current separator
char* label = (char*)separator + 1;
if (separator1)
{
len = (separator1 - label);
if (*(separator1 - 1) == ']') --len; // prior close of array ref
}
else len = 0;
WORDP key = NULL;
char* pendingkey = label;
char originalkeyname[SMALL_WORD_SIZE];
if (factvalue)
{
if (separator[1] == 's' || separator[1] == 'S') label = "subject";
else if (separator[1] == 'v' || separator[1] == 'V') label = "verb";
else if (separator[1] == 'f' || separator[1] == 'F') label = "flags";
else label = "object";
strcpy(originalkeyname, label);
}
else if (*separator == '.') // it is a field
{
if ((*label != '_' && *label != '\'') || (separator[1] == '_' && !IsDigit(separator[2])) || (separator[1] == '\'' && separator[2] == '_' && !IsDigit(separator[3]))) // any variable ref will be in dictionary as will field name
{
if (*label == '\\') ++label; // escaped $, not indirection
key = FindWord(label, len); // json is case sensitive- we are NOT
if (!key)
{
if (trace & TRACE_VARIABLE)
{
char name[MAX_WORD_SIZE];
strncpy(name, label, len);
name[len] = 0;
Log(USERLOG, "%s``", name);
}
goto NULLVALUE; // dont recognize such a name
}
label = key->word; // actual key name was given
if (trace & TRACE_VARIABLE) Log(USERLOG, "%s", label);
strcpy(originalkeyname, label);
pendingkey = label;
}
if (separator[1] == '$') // indirection
{
label = GetUserVariable(key->word, false); // key is a variable name , go get it to use real key
strcpy(originalkeyname, key->word);
if (trace & TRACE_VARIABLE) Log(USERLOG, "%s`%s`", key->word, label);
key = FindWord(label);
if (!key) goto NULLVALUE; // cannot find
}
else if ((separator[1] == '_' && IsDigit(separator[2])) || (separator[1] == '\'' && separator[2] == '_' && IsDigit(separator[3]))) // indirection key match variable
{
int index = (separator[1] == '_') ? atoi(separator + 2) : atoi(separator + 3);
sprintf(originalkeyname, "_%d", index);
if (index >= 0 && index <= MAX_WILDCARDS)
{
if (separator[1] == '_') strcpy(label, wildcardCanonicalText[index]);
else strcpy(label, wildcardOriginalText[index]);
if (trace & TRACE_VARIABLE) Log(USERLOG, "_%d`%s`",index, label);
key = FindWord(label);
}
if (!key) goto NULLVALUE; // cannot find
}
}
else // it is an index - of either array OR object
{
if (IsDigit(*label) || (*label == '-' && label[1] == '1'))
{
char* end = strchr(label, ']');
key = FindWord(label, end - label);
if (!key)
{
if (*label != '-' || !IsDigit(label[1])) goto NULLVALUE;
key = StoreWord("-1", AS_IS );
}
label = key->word;
sprintf(originalkeyname, "%s", label);
if (trace & TRACE_VARIABLE) Log(USERLOG, "%s", label);
}
else if (*label == '$') // indirect via user variable
{
char val[MAX_WORD_SIZE];
strncpy(val, label, len);
val[len] = 0;
label = GetUserVariable(val, false); // key is a variable name or index, go get it to use real key
if (trace & TRACE_VARIABLE) Log(USERLOG, "%s`%s`", val,label);
if (IsDigit(*label)) key = FindWord(label);
else if (!IsDigit(*label)) goto NULLVALUE;
sprintf(originalkeyname, "%s", val);
}
else if ((*label == '_' && IsDigit(label[1])) || (*label == '\'' && label[1] == '_' && IsDigit(label[2]))) // indirection key match variable
{
int index = (*label == '_') ? atoi(label + 1) : atoi(label + 2);
sprintf(originalkeyname, "_%d", index);
if (index >= 0 && index <= MAX_WILDCARDS)
{
if (separator[1] == '_') strcpy(label, wildcardCanonicalText[index]);
else strcpy(label, wildcardOriginalText[index]);
if (trace & TRACE_VARIABLE) Log(USERLOG, "%d`%s`", index, label);
if (IsDigit(*label)) key = FindWord(label);
}
if (!IsDigit(*label)) goto NULLVALUE;
}
else goto NULLVALUE; // not a number or indirect
}
if (factvalue) // $x.subject
{
if (separator[1] == 's' || separator[1] == 'S') answer = Meaning2Word(factvalue->subject)->word;
else if (separator[1] == 'v' || separator[1] == 'V') answer = Meaning2Word(factvalue->verb)->word;
else if (separator[1] == 'f' || separator[1] == 'F')
{
sprintf(flagsValue, "%u", factvalue->flags);
answer = flagsValue;
}
else answer = Meaning2Word(factvalue->object)->word;
separator = separator1;
if (!separator) goto ANSWER;
item = answer;
goto LOOPDEEPER;
}
MEANING verb = MakeMeaning(key);
int selected = atoi(label);
FACT* F = GetSubjectNondeadHead(D);
if (!strnicmp(D->word,"jo-",3) && bracket1 && *bracket1 == ']' && !bracket1[1]) // index into json object
{
int count = 0;
while (F)
{
if (count++ == selected) break;
if (selected == -1 && GetSubjectNondeadNext(F) == NULL) break; // first
F = GetSubjectNondeadNext(F);
}
if (!F) goto NULLVALUE;
answer = Meaning2Word(F->verb)->word;
goto ANSWER;
}
while (F)
{
if (F->verb == verb || selected == -1 )// newest fact
{
answer = Meaning2Word(F->object)->word;
if (!strcmp(answer, "null"))
{
item = "``";
return item + 2; // null value for locals
}
// does it continue?
if (separator1 && *separator1 == ']') ++separator1; // after current key/index there is another
if (separator1 && *separator1) // after current key/index there is another
{
item = answer;
separator = separator1;
goto LOOPDEEPER;
}
goto ANSWER;
}
F = GetSubjectNondeadNext(F);
}
goto NULLVALUE;
}
answer = item;
// OLD NO LONGER VALID? if item is in fact & there are problems return (*item == '&') ? (item + 1) : item; // value is quoted or not
ANSWER:
if (trace & TRACE_VARIABLE && separator) Log(USERLOG, "`%s` ", answer);
if (localvar)
{
char* limit;
char* ans = InfiniteStack(limit, "GetUserVariable"); // has complete
strcpy(ans, "``");
strcpy(ans + 2, answer);
CompleteBindStack();
return ans + 2;
}
return answer;
NULLVALUE:
if (trace & TRACE_VARIABLE && separator) Log(USERLOG, "`` ");
return (localvar) ? (nullLocal + 2) : nullGlobal; // null value for locals
}
void ClearUserVariableSetFlags()
{
HEAPREF varthread = userVariableThreadList;
while (varthread)
{
uint64 D;
varthread = UnpackHeapval(varthread, D, discard, discard);
RemoveInternalFlag((WORDP)D, VAR_CHANGED);
}
}
void ShowChangedVariables()
{
HEAPREF varthread = userVariableThreadList;
while (varthread)
{
uint64 Dx;
varthread = UnpackHeapval(varthread, Dx, discard, discard);
WORDP D = (WORDP)Dx;
char* value = D->w.userValue;
if (value && *value) Log(1, (char*)"%s = %s\r\n", D->word, value);
else Log(1, (char*)"%s = null\r\n", D->word);
}
}
void PrepareVariableChange(WORDP D, char* word, bool init)
{
if (D->word[1] == '_') // tmp var
{
if (init) D->w.userValue = NULL;
}
else if (D->internalBits & BOTVAR) {} // system vars have already been inited
else if (!(D->internalBits & VAR_CHANGED)) // not changed already this volley
{
userVariableThreadList = AllocateHeapval(HV1_WORDP,userVariableThreadList, (uint64)D);
D->internalBits |= VAR_CHANGED; // bypasses even locked preexisting variables
if (init) D->w.userValue = NULL;
}
else if (memoryMarkThreadList)
{
// There has been a memory mark, track changes to existing variables
uint64 memory;
UnpackHeapval(memoryMarkThreadList, memory, discard);
if ((uint64)D->w.userValue >= memory)
{
memoryVariableChangesThreadList = AllocateHeapval(HV1_WORDP|HV2_STRING,memoryVariableChangesThreadList,
(uint64)D, (uint64)D->w.userValue);// save name
}
}
}
static void HandleMonitoredEngineVariables(const char* var, char* word, bool assignment)
{
if (var[1] != 'c' || var[2] != 's' || var[3] != '_') return; // not a monitored $cs_ variable
// all variables are in lower case after compilation
if (!strcmp(var, (char*)"$cs_json_array_defaults"))
{
int64 val = 0;
if (word && *word) ReadInt64(word, val);
jsonDefaults = (int)val;
}
// tokencontrol changes are noticed by the engine
else if (!strcmp(var, (char*)"$cs_language"))
{
SetLanguage(word);
}
// id to use for user json composite creation
else if (!strcmp(var, (char*)"$cs_jid"))
{
int64 val = 0;
if (word && *word) ReadInt64(word, val);
builduserjid = (int)val;
}
// cs_float changes are noticed by the engine
else if (!strcmp(var, (char*)"$cs_fullfloat"))
fullfloat = (word && *word) ? true : false;
// tokencontrol changes are noticed by the engine
else if (!strcmp(var, (char*)"$cs_token"))
{
int64 val = 0;
if (word && *word) ReadInt64(word, val);
else
{
val = (DO_INTERJECTION_SPLITTING | DO_SUBSTITUTE_SYSTEM | DO_NUMBER_MERGE | DO_PROPERNAME_MERGE | DO_SPELLCHECK);
if (!stricmp(current_language, "ENGLISH")) val |= DO_PARSE;
}
tokenControl = val;
}
// cs_numbers changes are noticed by the engine (india, french, other)
else if (!strcmp(var, (char*)"$cs_numbers"))
{
if (!word) numberStyle = AMERICAN_NUMBERS;
else if (!stricmp(word, "indian")) numberStyle = INDIAN_NUMBERS;
else if (!stricmp(word, "french")) numberStyle = FRENCH_NUMBERS;
else numberStyle = AMERICAN_NUMBERS;
if (numberStyle == FRENCH_NUMBERS)
{
numberComma = '.';
numberPeriod = ',';
}
else
{
numberComma = ',';
numberPeriod = '.';
}
}
else if (!strcmp(var, (char*)"$cs_trace"))
{
int64 val = 0;
if (word && *word) ReadInt64(word, val);
trace = (unsigned int)val;
if (assignment) // remember script changed it
{
modifiedTraceVal = trace;
modifiedTrace = true;
}
}
else if (!strcmp(var, (char*)"$cs_time"))
{
int64 val = 0;
if (word && *word) ReadInt64(word, val);
timing = (unsigned int)val;
if (assignment) // remember script changed it
{
modifiedTimingVal = timing;
modifiedTiming = true;
}
}
// output random choice selection
else if (!strcmp(var, (char*)"$cs_outputchoice"))
{
int64 val = -1;
if (word && *word) ReadInt64(word, val);
outputchoice = (unsigned int)val;
}
else if (!strcmp(var, (char*)"$cs_response"))
{
int64 val = 0;
if (word && *word) ReadInt64(word, val);
else val = ALL_RESPONSES;
responseControl = (unsigned int)val;
}
else if (!strcmp(var, (char*)"$cs_botid"))
{
int64 val = 0;
if (word && *word) ReadInt64(word, val);
myBot = (uint64)val;
}
else if (!strcmp(var, (char*)"$cs_wildcardseparator"))
{
wildcardSeparatorGiven = true;
if (!word) *wildcardSeparator = 0;
else if (*word == '\\') *wildcardSeparator = word[2];
else *wildcardSeparator = (*word == '"') ? word[1] : *word; // 1st char in string if need be
}
else if (!strcmp(var, (char*)"$cs_randIndex"))
{
int rand = 0;
if (word && *word) ReadInt(word, rand);
randIndex = rand;
}
}
void SetAPIVariable(WORDP D, char* value) // coming from api
{
// check for json assign
if (strchr(D->word, '.') || strchr(D->word, '['))
{
JSONVariableAssign(D->word, value);
}
else // normal var
{
if (D->w.userValue == value) return;
if (D->word[1] != '_' && D->word[1] != '$' && (!D->w.userValue || !value || strcmp(D->w.userValue, value))) // only permanent variables get tracked
{
variableChangedThreadlist = AllocateHeapval(HV1_WORDP|HV2_STRING|HV3_INT,variableChangedThreadlist,
(uint64)D, (uint64)D->w.userValue, (uint64)D->internalBits);// save name
}
D->w.userValue = value;
if (D->word[1] == '^') // function definition assignment
{
variableChangedThreadlist = MakeFunctionDefinition(value); // change internal bits AFTER save of value
}
}
}
void SetUserVariable(const char* var, char* word, bool assignment,bool reuse)
{
WORDP D = StoreWord(var,AS_IS); // find or create the var.
if (!D) return; // ran out of memory
#ifndef DISCARDTESTING
if (debugVar) (*debugVar)((char*)var, word);
#endif
// adjust value
if (word) // has a nonnull value?
{
if (!*word || !stricmp(word, (char*)"null")) word = NULL; // really is null
else // some value
{
if (D->w.userValue && !strcmp(word, D->w.userValue))
return; // no change is happening
bool purelocal = (D->word[1] == LOCALVAR_PREFIX);
// if (purelocal) word = AllocateStack(word, 0, true); // trying to save on permanent space
//else
if (!reuse) word = AllocateHeap(word, 0, 1, false, purelocal); // we may be restoring old value which doesnt need allocation
if (!word) return; // no memory?
}
}
else if (!D->w.userValue) return; // clear to null already
PrepareVariableChange(D, word, !(csapicall == TEST_PATTERN || csapicall == TEST_OUTPUT));
if (planning && !documentMode) // handle undoable assignment (cannot use text sharing as done in document mode)
{
if (D->w.userValue == NULL) SpecialFact(MakeMeaning(D), (MEANING)1, 0);
else SpecialFact(MakeMeaning(D), (MEANING)(D->w.userValue - heapBase), 0);
}
if (csapicall == TEST_PATTERN || csapicall == TEST_OUTPUT) SetAPIVariable(D, word);
else D->w.userValue = word;
HandleMonitoredEngineVariables(var, word,assignment);
if (trace && D->internalBits & MACRO_TRACE)
{
char pattern[110];
char label[MAX_LABEL_SIZE];
GetPattern(currentRule, label, pattern, true,100); // go to output
Log(ECHOUSERLOG, "%s -> %s at %s.%d.%d %s %s\r\n", D->word, word, GetTopicName(currentTopicID), TOPLEVELID(currentRuleID), REJOINDERID(currentRuleID), label, pattern);
}
}
static FunctionResult DoMath(char* oldValue, char* moreValue, char* result, char op, char* fullop)
{
if (!stricmp(fullop, "and") || !stricmp(fullop, "or")) return FAILRULE_BIT; // not math
if (*oldValue == '_') oldValue = GetwildcardText(GetWildcardID(oldValue), true); // onto a wildcard
else if (*oldValue == USERVAR_PREFIX) oldValue = GetUserVariable(oldValue,false); // onto user variable
else if (*oldValue == '^') oldValue = FNVAR(oldValue); // onto function argument
else if (*oldValue && !IsNumberStarter(*oldValue)) return FAILRULE_BIT; // illegal
if (*moreValue == '_') moreValue = GetwildcardText(GetWildcardID(moreValue), true);
else if (*moreValue == USERVAR_PREFIX) moreValue = GetUserVariable(moreValue, false);
else if (*moreValue == '^') moreValue = FNVAR(moreValue );
else if (*oldValue && !IsNumberStarter(*moreValue)) return FAILRULE_BIT; // illegal
// perform numeric op
bool floating = false;
if (strchr(oldValue, '.') || strchr(moreValue, '.') || op == '/') floating = true;
if (floating)
{
double newval = Convert2Double(oldValue);
double more = Convert2Double(moreValue);
if (op == '-') newval -= more;
else if (op == '*') newval *= more;
else if (op == '/') {
if (more == 0) return FAILRULE_BIT; // cannot divide by 0
newval /= more;
}
else if (op == '%')
{
if (more == 0) return FAILRULE_BIT;
int64 ivalue = (int64)newval;
int64 morval = (int64)more;
newval = (double)(ivalue % morval);
}
else newval += more;
WriteFloat(result, newval);
if (trace & TRACE_OUTPUT && CheckTopicTrace()) Log(USERLOG," %s ", result);
}
else
{
int64 newval;
ReadInt64(oldValue, newval);
int64 more;
ReadInt64(moreValue, more);
if (op == '-') newval -= more;
else if (op == '*') newval *= more;
else if (op == '/')
{
if (more == 0) return FAILRULE_BIT; // cannot divide by 0
newval /= more;
}
else if (op == '%')
{
if (more == 0) return FAILRULE_BIT;
newval %= more;
}
else if (op == '|')
{
newval |= more;
if (fullop[1] == '^') newval ^= more;
}
else if (op == '&') newval &= more;
else if (op == '^') newval ^= more;
else if (op == '<') newval <<= more;
else if (op == '>') newval >>= more;
else newval += more;
char tracex[MAX_WORD_SIZE];
sprintf(result, (char*)"%lld", newval);
if (trace & TRACE_OUTPUT && CheckTopicTrace())
{
sprintf(tracex, "0x%016llx", newval);
Log(USERLOG," %s/%s ", result, tracex);
}
}
return NOPROBLEM_BIT;
}
FunctionResult Add2UserVariable(char* var, char* moreValue, char* op, char* originalArg)
{
// get original value
char minusflag = *op;
char* oldValue;
if (*var == '_') oldValue = GetwildcardText(GetWildcardID(var), true); // onto a wildcard
else if (*var == USERVAR_PREFIX) oldValue = GetUserVariable(var, false); // onto user variable
else if (*var == '^') oldValue = FNVAR(var ); // onto function argument
else if (*var == SYSVAR_PREFIX) oldValue = SystemVariable(var, NULL);
else return FAILRULE_BIT; // illegal
// get augment value
if (*moreValue == '_') moreValue = GetwildcardText(GetWildcardID(moreValue), true);
else if (*moreValue == USERVAR_PREFIX) moreValue = GetUserVariable(moreValue, false);
else if (*moreValue == '^') moreValue = FNVAR(moreValue );
// try json array set op?
if (IsValidJSONName(oldValue, 'a'))
{
if (*op != '+') return FAILRULE_BIT;
FunctionResult result = NOPROBLEM_BIT;
WORDP old = FindWord(oldValue);
bool dup = (oldValue[3] == 't') ? (oldValue[4] == '+') : (oldValue[3] == '+');
char junk[10];
if (IsValidJSONName(moreValue, 'a'))
{
char* limit;
FACT* F = GetSubjectNondeadHead(FindWord(moreValue));
FACT** stack = (FACT**)InfiniteStack(limit, "array merge");
int index = 0;
while (F) // stack object key data
{
if (F->flags & JSON_ARRAY_FACT) stack[index++] = F;
F = GetSubjectNondeadNext(F);
}
CompleteBindStack64(index, (char*)stack);
for (int i = index - 1; i >= 0; --i)
{
F = stack[i];