-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyntaxTree.java
executable file
·3437 lines (3141 loc) · 121 KB
/
SyntaxTree.java
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
import java.math.BigDecimal;
import java.util.*;
public class SyntaxTree {
public static ValueBase objectToValue(Object object) {
try {
if (object == null) {
return new SyntaxTree.Null();
} else if (object instanceof BigDecimal) {
return new SyntaxTree.Number((BigDecimal)object);
} else if (object instanceof String) {
return new SyntaxTree.Text((String)object);
} else if (object instanceof Integer) {
return new SyntaxTree.Number((int)object);
} else if (object instanceof Byte) {
return new SyntaxTree.Number((byte)object);
} else if (object instanceof ArrayList) {
return List.fromArrayList((ArrayList<ValueBase>) object);
} else if (object instanceof Boolean || (boolean) object || !((boolean) object)) {
return new SyntaxTree.Boolean((boolean)object);
}
} catch (ClassCastException ignore) {}
assert object instanceof ValueBase;
return (ValueBase)object;
}
private static final HashMap<String, ValueBase> variables = new HashMap<>();
public final static ArrayList<String> staticFunctions = new ArrayList<>();
public final static ArrayList<String> staticParameters = new ArrayList<>();
public static HashMap<String, ValueBase> getVariables() {
return variables;
}
private static final HashMap<String, ProgramBase> functions = new HashMap<>();
public static HashMap<String, ProgramBase> getFunctions() {
return functions;
}
private static final HashMap<String, ArrayList<SetVariable>> classesParameters = new HashMap<>();
public static HashMap<String, ArrayList<SetVariable>> getClassesParameters() {
return classesParameters;
}
private static final ArrayList<String> classesWithInit = new ArrayList<>();
private static final ArrayList<String> touchedVariables = new ArrayList<>();
private static final HashMap<String, ArrayList<String>> classesParents = new HashMap<>();
public static HashMap<String, ArrayList<String>> getClassesParents() {
return classesParents;
}
private static int id = 0;
public static int getId() {
return id;
}
public static void setId(int id) {
SyntaxTree.id = id;
}
public static void touchFunctionsFromClass(ProgramBase program, String className) {
if (program instanceof Programs) {
for (ProgramBase program1 : ((Programs) program).getPrograms()) {
touchFunctionsFromClass(program1, className);
}
} else if (program instanceof Function) {
functions.put("#C" + className + "#" + ((Function) program).getFunctionName(), null);
}
}
public static ArrayList<String> getClassesWithInit() {
return classesWithInit;
}
private static Data data = new Data();
public static Data getData() {
return data;
}
public static void setData(Data data) {
SyntaxTree.data = data;
}
private static String lastNameSpace = "@";
private static int location = 0;
private static String nextNameSpace() {
if (lastNameSpace.charAt(location) == 'z') {
location++;
lastNameSpace += "@";
}
StringBuilder stringBuilder = new StringBuilder(lastNameSpace);
char toReplace = (char) (lastNameSpace.charAt(location) + 1);
if (toReplace == '[') {
toReplace += 6;
}
stringBuilder.setCharAt(location, toReplace);
lastNameSpace = stringBuilder.toString();
return lastNameSpace;
}
public static void resetNameSpaces() {
lastNameSpace = "@";
location = 0;
}
public static void declareNativeFunction(String parent, String name, int argumentCount) {
name += ":N#" + argumentCount + "#" + parent;
if (data.getFunctions().containsKey(name)) {
Errors.error(ErrorCodes.ERROR_FUNCTION_REDECLARATION, name);
}
data.getFunctions().put(name, null);
}
public static void deleteNativeFunction(String parent, String name, int argumentCount) {
data.getFunctions().remove(name + ":N#" + argumentCount + "#" + parent);
}
public static class Number extends ValueBase {
public Number(BigDecimal number) {
this.setData(number);
}
public Number(double number) {
this.setData(new BigDecimal(number));
}
}
public static class Text extends ValueBase {
public Text(String text) {
this.setData(text);
}
}
public static class Boolean extends ValueBase {
public Boolean(boolean bool) {
this.setData(bool);
}
@Override
public String toString() {
return ((boolean)getData()? "True" : "False");
}
}
public static class Null extends ValueBase {
public Null() {
this.setData(null);
}
}
public static class List extends ValueBase {
public List(ValueBase... data) {
this.setData(new ArrayList<>(Arrays.asList(data)));
}
@Override
public Object getData() {
ArrayList<ValueBase> list = (ArrayList<ValueBase>) super.getData();
for (int i = 0; i < list.size(); i++) {
if (!(list.get(i) instanceof Number || list.get(i) instanceof Text || list.get(i) instanceof Boolean || list.get(i) instanceof Null || list.get(i) instanceof List)) {
list.set(i, (ValueBase) list.get(i).getData());
}
}
return super.getData();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder("[");
boolean isFirst = true;
for (ValueBase value : (ArrayList<ValueBase>) getData()) {
if (isFirst) {
isFirst = false;
} else {
stringBuilder.append(", ");
}
if (value instanceof CreateInstance) value = getTextFromInstance(value);
stringBuilder.append(value.toString());
}
return stringBuilder.append("]").toString();
}
public static List fromArrayList(ArrayList<ValueBase> arrayList) {
List list = new List();
list.setData(arrayList);
return list;
}
}
public static class Append extends ValueBase {
private final ValueBase list;
private final ValueBase value;
public Append(ValueBase list, ValueBase value) {
this.list = list;
this.value = value;
}
@Override
public ValueBase getData() {
ValueBase list = this.list;
ValueBase value = this.value;
list.setConfigData(getConfigData());
value.setConfigData(getConfigData());
if (!(list instanceof Number || list instanceof Text || list instanceof Boolean || list instanceof Null || list instanceof List)) {
list = (ValueBase)list.getData();
}
if (!(value instanceof Number || value instanceof Text || value instanceof Boolean || value instanceof Null || value instanceof List)) {
value = (ValueBase)value.getData();
}
if (list instanceof List) {
ArrayList<ValueBase> arrayList = (ArrayList<ValueBase>) ((ArrayList<ValueBase>) list.getData()).clone();
arrayList.add(value);
return List.fromArrayList(arrayList);
} else {
Errors.error(ErrorCodes.ERROR_TYPE, "data is not List (Append)");
return new SyntaxTree.Null();
}
}
public ValueBase getList() {
return list;
}
public ValueBase getValue() {
return value;
}
}
public static class Insert extends ValueBase {
private final ValueBase list;
private final ValueBase value;
private final ValueBase index;
public Insert(ValueBase list, ValueBase value, ValueBase index) {
this.list = list;
this.value = value;
this.index = index;
}
@Override
public ValueBase getData() {
ValueBase list = this.list;
ValueBase value = this.value;
ValueBase index = this.index;
list.setConfigData(getConfigData());
value.setConfigData(getConfigData());
index.setConfigData(getConfigData());
if (!(list instanceof Number || list instanceof Text || list instanceof Boolean || list instanceof Null || list instanceof List)) {
list = (ValueBase)list.getData();
}
if (!(value instanceof Number || value instanceof Text || value instanceof Boolean || value instanceof Null || value instanceof List)) {
value = (ValueBase)value.getData();
}
if (!(index instanceof Number || index instanceof Text || index instanceof Boolean || index instanceof Null || index instanceof List)) {
index = (ValueBase)index.getData();
}
if (list instanceof List && index instanceof Number) {
ArrayList<ValueBase> arrayList = (ArrayList<ValueBase>) ((ArrayList<ValueBase>) list.getData()).clone();
int i = ((BigDecimal)index.getData()).intValue();
if (i > arrayList.size()) {
Null _null = new Null();
arrayList.ensureCapacity(i);
while (arrayList.size() < i) {
arrayList.add(_null);
}
}
arrayList.add(i, value);
return List.fromArrayList(arrayList);
} else {
Errors.error(ErrorCodes.ERROR_TYPE, "data is not List AND/OR index is not Number (Insert)");
return new SyntaxTree.Null();
}
}
public ValueBase getList() {
return list;
}
public ValueBase getValue() {
return value;
}
public ValueBase getIndex() {
return index;
}
}
public static class Set extends ValueBase {
private final ValueBase list;
private final ValueBase value;
private final ValueBase index;
public Set(ValueBase list, ValueBase value, ValueBase index) {
this.list = list;
this.value = value;
this.index = index;
}
@Override
public ValueBase getData() {
ValueBase list = this.list;
ValueBase value = this.value;
ValueBase index = this.index;
list.setConfigData(getConfigData());
value.setConfigData(getConfigData());
index.setConfigData(getConfigData());
if (!(list instanceof Number || list instanceof Text || list instanceof Boolean || list instanceof Null || list instanceof List)) {
list = (ValueBase)list.getData();
}
if (!(value instanceof Number || value instanceof Text || value instanceof Boolean || value instanceof Null || value instanceof List)) {
value = (ValueBase)value.getData();
}
if (!(index instanceof Number || index instanceof Text || index instanceof Boolean || index instanceof Null || index instanceof List)) {
index = (ValueBase)index.getData();
}
if (list instanceof List && index instanceof Number) {
ArrayList<ValueBase> arrayList = (ArrayList<ValueBase>) ((ArrayList<ValueBase>) list.getData()).clone();
arrayList.set(((BigDecimal)index.getData()).intValue(), value);
return List.fromArrayList(arrayList);
} else {
Errors.error(ErrorCodes.ERROR_TYPE, "data is not List AND/OR index is not Number (Set)");
return new SyntaxTree.Null();
}
}
public ValueBase getList() {
return list;
}
public ValueBase getValue() {
return value;
}
public ValueBase getIndex() {
return index;
}
}
public static class Get extends ValueBase {
private final ValueBase list;
private final ValueBase index;
public Get(ValueBase list, ValueBase index) {
this.list = list;
this.index = index;
}
@Override
public ValueBase getData() {
ValueBase list = this.list;
ValueBase index = this.index;
list.setConfigData(getConfigData());
index.setConfigData(getConfigData());
if (!(list instanceof Number || list instanceof Text || list instanceof Boolean || list instanceof Null || list instanceof List)) {
list = (ValueBase)list.getData();
}
if (!(index instanceof Number || index instanceof Text || index instanceof Boolean || index instanceof Null || index instanceof List)) {
index = (ValueBase)index.getData();
}
if (list instanceof List && index instanceof Number) {
return ((ArrayList<ValueBase>) list.getData()).get(((BigDecimal)index.getData()).intValue());
} else {
Errors.error(ErrorCodes.ERROR_TYPE, "data is not List AND/OR index is not Number (Get)");
return new SyntaxTree.Null();
}
}
public ValueBase getList() {
return list;
}
public ValueBase getIndex() {
return index;
}
}
public static class GetSize extends ValueBase {
private final ValueBase list;
public GetSize(ValueBase list) {
this.list = list;
}
@Override
public ValueBase getData() {
ValueBase list = this.list;
list.setConfigData(getConfigData());
if (!(list instanceof Number || list instanceof Text || list instanceof Boolean || list instanceof Null || list instanceof List)) {
list = (ValueBase)list.getData();
}
if (list instanceof List) {
return new Number(((ArrayList) list.getData()).size());
} else {
Errors.error(ErrorCodes.ERROR_TYPE, "data is not List (GetSize)");
return new SyntaxTree.Null();
}
}
public ValueBase getList() {
return list;
}
}
public static class IndexOf extends ValueBase {
private final ValueBase list;
private ValueBase fromIndex;
private final ValueBase value;
private boolean last = false;
public IndexOf(ValueBase list, ValueBase data) {
this.list = list;
this.value = data;
}
public IndexOf(ValueBase list, ValueBase data, ValueBase fromIndex) {
this.list = list;
this.value = data;
this.fromIndex = fromIndex;
}
public boolean isLastIndexOf() {
return last;
}
public IndexOf setLastIndexOf(boolean last) {
this.last = last;
return this;
}
@Override
public ValueBase getData() {
ValueBase list = this.list;
list.setConfigData(getConfigData());
if (!(list instanceof Number || list instanceof Text || list instanceof Boolean || list instanceof Null || list instanceof List)) {
list = (ValueBase)list.getData();
}
ValueBase data = this.value;
data.setConfigData(getConfigData());
if (!(data instanceof Number || data instanceof Text || data instanceof Boolean || data instanceof Null || data instanceof List)) {
data = (ValueBase)data.getData();
}
ValueBase fromIndex = this.fromIndex;
if (fromIndex != null) {
fromIndex.setConfigData(getConfigData());
}
if (fromIndex != null && !(fromIndex instanceof Number || fromIndex instanceof Text || fromIndex instanceof Boolean || fromIndex instanceof Null || fromIndex instanceof List)) {
fromIndex = (ValueBase)fromIndex.getData();
}
if (list instanceof List) {
if (!last) {
if (this.fromIndex == null) {
return new Number(((ArrayList) list.getData()).indexOf(data));
} else if (fromIndex instanceof Number) {
int res = ((ArrayList) list.getData()).subList(((BigDecimal) fromIndex.getData()).intValue(), ((ArrayList) list.getData()).size()).indexOf(data);
if (res != -1) {
res += ((BigDecimal) fromIndex.getData()).intValue();
}
return new Number(res);
} else {
Errors.error(ErrorCodes.ERROR_TYPE, "fromIndex is not Number (IndexOf)");
return new Null();
}
} else {
if (this.fromIndex == null) {
return new Number(((ArrayList) list.getData()).lastIndexOf(data));
} else if (fromIndex instanceof Number) {
int res = ((ArrayList) list.getData()).subList(0, ((BigDecimal) fromIndex.getData()).intValue() + 1).lastIndexOf(data);
return new Number(res);
} else {
Errors.error(ErrorCodes.ERROR_TYPE, "fromIndex is not Number (IndexOf)");
return new Null();
}
}
} else {
Errors.error(ErrorCodes.ERROR_TYPE, "data is not List (IndexOf)");
return new SyntaxTree.Null();
}
}
public ValueBase getList() {
return list;
}
public ValueBase getValue() {
return value;
}
public ValueBase getFromIndex() {
return fromIndex;
}
}
public static class Sort extends ValueBase {
private ValueBase list;
private boolean sortByNumber;
public Sort(ValueBase list, boolean sortByNumber) {
this.list = list;
this.sortByNumber = sortByNumber;
}
@Override
public ValueBase getData() {
ValueBase list = this.list;
list.setConfigData(getConfigData());
if (!(list instanceof Number || list instanceof Text || list instanceof Boolean || list instanceof Null || list instanceof List)) {
list = (ValueBase)list.getData();
}
if (list instanceof List) {
ArrayList<ValueBase> arrayList = (ArrayList<ValueBase>) ((ArrayList<ValueBase>) list.getData()).clone();
if (sortByNumber) {
arrayList.sort((o1, o2) -> {
if (o1 instanceof Number && o2 instanceof Number) {
return ((BigDecimal) o1.getData()).compareTo((BigDecimal) o2.getData());
} else {
return -1;
}
});
} else {
arrayList.sort(Comparator.comparing(Object::toString));
}
return List.fromArrayList(arrayList);
} else {
Errors.error(ErrorCodes.ERROR_TYPE, "data is not List (Sort)");
return new SyntaxTree.Null();
}
}
public ValueBase getList() {
return list;
}
public boolean isSortByNumber() {
return sortByNumber;
}
}
public static class Variable extends ValueBase implements java.io.Serializable {
private String variableName;
private boolean error = true;
private boolean useInstanceName = false;
private ValueBase instance;
private boolean addInstanceName = false;
public boolean isUseInstanceName() {
return useInstanceName;
}
public void setUseInstanceName(boolean useInstanceName) {
this.useInstanceName = useInstanceName;
}
public Variable(String variableName) {
this.variableName = variableName;
}
public Variable setAddInstanceName(boolean addInstanceName) {
this.addInstanceName = addInstanceName;
return this;
}
public boolean isAddInstanceName() {
return addInstanceName;
}
public Variable fromInstance(ValueBase value) {
if (value != null) useInstanceName = true;
instance = value;
return this;
}
public ValueBase getInstance() {
return instance;
}
@Override
public Object getData() {
if (instance != null) {
String[] splitInstance = instance.toString().split(":");
if (addInstanceName && !variableName.startsWith("#C"))
variableName = "#C" + splitInstance[1] + "#" + variableName;
getConfigData().setInstanceName(splitInstance[0]);
}
if (variableName.startsWith("#C")) variableName = variableName.replace("#F", "");
ValueBase tmp = data.getVariables().get(variableName + (useInstanceName? getConfigData().getInstanceName():""));
if (error && tmp == null) Errors.error(ErrorCodes.ERROR_VARIABLE_DOES_NOT_EXISTS, variableName);
if (!error && tmp == null) tmp = new SyntaxTree.Null();
return tmp;
}
public Variable setVariableName(String variableName) {
this.variableName = variableName;
return this;
}
public String getVariableName() {
return variableName;
}
public Variable setError(boolean error) {
this.error = error;
return this;
}
}
public static class Increase extends ValueBase implements java.io.Serializable {
private final Variable variable;
private final boolean isPostfix;
private final SetVariable setVariable;
public SetVariable getVariableSetter() {
return setVariable;
}
public Variable getVariable() {
return variable;
}
public boolean isPostfix() {
return isPostfix;
}
public Increase(Variable variable, boolean isPostfix) {
this.variable = variable;
this.isPostfix = isPostfix;
setVariable = new SetVariable(variable.getVariableName(), new Null());
}
@Override
public Object getData() {
ValueBase tmp = (ValueBase) variable.getData();
setVariable.value = new Add(tmp, new Number(1));
setVariable.eval();
return isPostfix? tmp:variable.getData();
}
}
public static class Decrease extends ValueBase implements java.io.Serializable {
private final Variable variable;
private final boolean isPostfix;
private final SetVariable setVariable;
public SetVariable getVariableSetter() {
return setVariable;
}
public Variable getVariable() {
return variable;
}
public boolean isPostfix() {
return isPostfix;
}
public Decrease(Variable variable, boolean isPostfix) {
this.variable = variable;
this.isPostfix = isPostfix;
setVariable = new SetVariable(variable.getVariableName(), new Null());
}
@Override
public Object getData() {
ValueBase tmp = (ValueBase) variable.getData();
setVariable.value = new Sub(tmp, new Number(1));
setVariable.eval();
return isPostfix? tmp:variable.getData();
}
}
public static class SetVariable extends ProgramBase implements java.io.Serializable {
private String variableName;
private ValueBase value;
private boolean isDeclaration = false;
private boolean checkDeclarationInRuntime = false;
private boolean useInstanceName = false;
private ValueBase instance = null;
private boolean addInstanceName = false;
private boolean isStatic = false;
private boolean sameNameExists = false;
public boolean getIsDeclaration() {
return isDeclaration;
}
public SetVariable setIsDeclaration(boolean isDeclaration) {
this.isDeclaration = isDeclaration;
return this;
}
public SetVariable setAddInstanceName(boolean addInstanceName) {
this.addInstanceName = addInstanceName;
return this;
}
public boolean isAddInstanceName() {
return addInstanceName;
}
public boolean getCheckDeclarationInRuntime() {
return checkDeclarationInRuntime;
}
public void setVariableValue(ValueBase value) {
this.value = value;
}
public SetVariable setCheckDeclarationInRuntime(boolean checkDeclarationInRuntime) {
this.checkDeclarationInRuntime = checkDeclarationInRuntime;
return this;
}
public SetVariable(String variableName, ValueBase value, boolean isDeclaration) {
sameNameExists = getVariables().containsKey(variableName);
this.variableName = variableName;
this.value = value;
this.isDeclaration = isDeclaration;
checkDeclaration();
if (!data.getVariables().containsKey(variableName)) data.getVariables().put(variableName, null);
}
public SetVariable(String variableName, ValueBase value, boolean isDeclaration, boolean checkDeclarationInRuntime) {
sameNameExists = getVariables().containsKey(variableName);
this.variableName = variableName;
this.value = value;
this.isDeclaration = isDeclaration;
this.checkDeclarationInRuntime = checkDeclarationInRuntime;
if (!checkDeclarationInRuntime) {
checkDeclaration();
}
if (!data.getVariables().containsKey(variableName)) data.getVariables().put(variableName, null);
}
public SetVariable(String variableName, ValueBase value) {
sameNameExists = getVariables().containsKey(variableName);
this.variableName = variableName;
this.value = value;
if (!data.getVariables().containsKey(variableName)) data.getVariables().put(variableName, null);
}
public void checkDeclaration() {
if (!variableName.startsWith("#C")) {
if (isDeclaration && data.getVariables().get(variableName) != null) {
Errors.error(ErrorCodes.ERROR_VARIABLE_REDECLARATION, variableName);
}
}
if (!isDeclaration && data.getVariables().get(variableName + (useInstanceName? data.getInstanceName():"")) == null) {
Errors.error(ErrorCodes.ERROR_VARIABLE_NOT_DECLARED, variableName);
}
}
public boolean isStatic() {
return isStatic;
}
public SetVariable setStatic(boolean aStatic) {
isStatic = aStatic;
return this;
}
@Override
void eval() {
if (instance != null) {
String[] splitInstance = instance.toString().split(":");
getData().setInstanceName(splitInstance[0]);
if (addInstanceName && !variableName.startsWith("#C"))
variableName = "#C" + splitInstance[1] + "#" + variableName;
}
if (variableName.startsWith("#C")) variableName = variableName.replace("#F", "");
if (checkDeclarationInRuntime) checkDeclaration();
ValueBase value = this.value;
if (!(value instanceof Number || value instanceof Text || value instanceof Boolean || value instanceof Null || value instanceof CreateInstance || value instanceof List)) {
value = (ValueBase)value.getData();
}
if (value instanceof CreateInstance) {
value.getData();
}
data.getVariables().put(variableName + (useInstanceName? data.getInstanceName():""), value);
}
public String getVariableName() {
return variableName;
}
public SetVariable setVariableName(String variableName) {
if (!sameNameExists) data.getVariables().remove(this.variableName);
this.variableName = variableName;
data.getVariables().put(variableName, null);
return this;
}
public ValueBase getVariableValue() {
return value;
}
@Override
protected Object clone() {
return new SetVariable(variableName, value)
.setIsDeclaration(isDeclaration)
.setCheckDeclarationInRuntime(checkDeclarationInRuntime);
}
public boolean isUseInstanceName() {
return useInstanceName;
}
public void setUseInstanceName(boolean useInstanceName) {
this.useInstanceName = useInstanceName;
}
public SetVariable fromInstance(ValueBase instance) {
this.instance = instance;
if (instance != null) useInstanceName = true;
return this;
}
public ValueBase getInstance() {
return instance;
}
}
public static class Function extends ProgramBase implements java.io.Serializable {
private String functionName;
private ProgramBase program;
private final String[] args;
private boolean error = true;
private boolean isStatic = false;
public Function(String functionName, ProgramBase program, boolean error, String... args) {
this.args = args;
this.error = error;
StringBuilder finalFunctionName = new StringBuilder(functionName).append(":");
for (String string : args) {
finalFunctionName.append(",").append(string);
}
this.functionName = finalFunctionName.toString();
if (data.getFunctions().containsKey(this.functionName)) touchedVariables.add(this.functionName);
else data.getFunctions().put(this.functionName, null);
this.program = NameSpaces.addNameSpaces("#F" + this.functionName, program, new ArrayList<>(Arrays.asList(args)));
}
public Function(String functionName, ProgramBase program, String... args) {
this.args = args;
StringBuilder finalFunctionName = new StringBuilder(functionName).append(":");
for (String string : args) {
finalFunctionName.append(",").append(string);
}
this.functionName = finalFunctionName.toString();
if (data.getFunctions().containsKey(this.functionName)) touchedVariables.add(this.functionName);
else data.getFunctions().put(this.functionName, null);
this.program = program;
program = NameSpaces.addNameSpaces("#F" + this.functionName, program, new ArrayList<>(Arrays.asList(args)));
}
@Override
void eval() {
if (error) {
String[] splitFunctionName = functionName.split(":");
for (String name : data.getFunctions().keySet()) {
String[] splitName = name.split(":");
if (name.startsWith(splitFunctionName[0] + ":") && splitName.length > 1 && splitFunctionName.length > 1 &&
splitName[1].split(",").length == splitFunctionName[1].split(",").length &&
!name.equals(functionName)) {
Errors.error(ErrorCodes.ERROR_FUNCTION_REDECLARATION, this.functionName);
}
}
if (data.getFunctions().get(this.functionName) != null) {
Errors.error(ErrorCodes.ERROR_FUNCTION_REDECLARATION, this.functionName);
}
}
data.getFunctions().put(functionName, program);
}
public String getFunctionName() {
return functionName;
}
public void setFunctionName(String functionName) {
if (functionName.startsWith("#C"))
data.getFunctions().remove(this.functionName);
for (String string : touchedVariables) {
data.getFunctions().put(string, null);
}
this.functionName = functionName;
if (data.getFunctions().get(this.functionName) != null) {
Errors.error(ErrorCodes.ERROR_FUNCTION_REDECLARATION, this.functionName);
}
data.getFunctions().put(this.functionName, null);
}
public ProgramBase getProgram() {
return program;
}
public String[] getArgs() {
return args;
}
public Function setStatic(boolean aStatic) {
isStatic = aStatic;
return this;
}
public boolean isStatic() {
return isStatic;
}
}
public static class CallFunction extends ValueBase implements java.io.Serializable {
private String functionName;
private ProgramBase[] programs;
private final ValueBase[] args;
private boolean isRecursion = false;
private ValueBase instance = null;
private boolean addInstanceName = false;
private boolean nativeFunction = false;
public CallFunction(String functionName, ValueBase... args) {
this.functionName = functionName;
this.args = args;
}
void findFunction() {
if (functionName.contains(":")) return;
boolean aFunctionWithThisNameExists = false;
boolean isVarArg = false;
programs = new ProgramBase[args.length];
ArrayList<String> params = new ArrayList<>();
for (Map.Entry<String, ProgramBase> entry : data.getFunctions().entrySet()) {
if (entry.getKey().split(":")[0].equals(functionName)) {
String previousFunctionName = functionName;
this.functionName = entry.getKey();
aFunctionWithThisNameExists = true;
if (this.functionName.split(":").length > 1) {
if (this.functionName.split(":")[1].startsWith("N#")) {
if (!("" + args.length).equals(this.functionName.split(":")[1].substring(2).split("#")[0])) {
this.functionName = previousFunctionName;
continue;
}
programs = new ProgramBase[args.length + 3];
int i = 0;
for (; i < args.length; i++) {
programs[i] = new OpCode.PutToVM(args[i]);
}
programs[i] = new OpCode.PutToVM(new SyntaxTree.Text(functionName.split(":")[0]));
String[] tmp = functionName.split("#");
programs[++i] = new OpCode.PutToVM(new SyntaxTree.Text(tmp[tmp.length - 1]));
programs[++i] = new OpCode(new SyntaxTree.Number(VM.DLCALL));
nativeFunction = true;
} else {
for (String string : this.functionName.split(":")[1].split(",")) {
if (string.equals("")) continue;
params.add(string);
}
}
}
if (!nativeFunction && params.size() >= 1 && params.get(params.size() - 1).equals("%args")) {
if (params.size() - 1 <= args.length) {
int len = params.size();
if (len == 0) len = 1;
programs = new ProgramBase[len];
isVarArg = true;
ArrayList<ValueBase> args1 = new ArrayList<>(Arrays.asList(args).subList(params.size() - 1, args.length));
programs[params.size() - 1] = new SyntaxTree.SetVariable((!this.functionName.startsWith("#C") ? "#F" : "") + this.functionName + ":args", List.fromArrayList(args1));
}
} else if (!isVarArg && !nativeFunction && (params.size() != args.length)) {
this.functionName = previousFunctionName;
params.clear();
}
}
}
if (!isVarArg && !nativeFunction && aFunctionWithThisNameExists && (params.size() != args.length || !functionName.contains(":"))) {
Errors.error(ErrorCodes.ERROR_ARGS_NOT_MATCH, functionName);
}
if (!nativeFunction && aFunctionWithThisNameExists) {
int i = 0;
boolean hasF = true;
for (ValueBase value : args) {
if (params.get(i).equals("%args")) break;
if (this.functionName.startsWith("#C")) hasF = false;
programs[i] = new SyntaxTree.SetVariable((hasF ? "#F" : "") + this.functionName + ":" + params.get(i++), value);
}
}
}
@Override
public ValueBase getData() {
if (instance != null) {
ValueBase instance = this.instance;
if (!(instance instanceof Number || instance instanceof Text || instance instanceof Boolean || instance instanceof Null || instance instanceof List || instance instanceof CreateInstance)) {
instance = (ValueBase) instance.getData();
}
if (instance instanceof Text) {
// The function is being called on a string literal (e.g. "Hello".replace("H", "h"))
String string = (String) instance.getData();
if (functionName.equals("replace")) {
if (args.length != 2) {
Errors.error(ErrorCodes.ERROR_ARGS_NOT_MATCH, functionName);
return new Null();
}
return new Text(string.replace(args[0].toString(), args[1].toString()));
} else if (functionName.equals("charAt")) {
if (args.length != 1) {
Errors.error(ErrorCodes.ERROR_ARGS_NOT_MATCH, functionName);
return new Null();
}
if (!(args[0] instanceof Number || args[0] instanceof Text || args[0] instanceof Boolean || args[0] instanceof Null || args[0] instanceof List || args[0] instanceof CreateInstance)) {
args[0] = (ValueBase) args[0].getData();
}
if (args[0] instanceof Number) {
return new Text("" + string.charAt(((BigDecimal) args[0].getData()).intValue()));
} else {