-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fundamental.clc
5044 lines (4024 loc) · 124 KB
/
Fundamental.clc
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
native struct int implements ICloneable, IComparableMore, IInspectable includes OperatorModule
{
static int test_method()
{
return 1;
}
int test_method2()
{
int.test_method();
return 2;
}
native long toLong();
native short toShort();
native byte toByte();
native uint toUInt();
native float toFloat();
native double toDouble();
native char toChar();
virtual native String toString();
int times() with void block(int num) {
for(int i=0; i<self; i++) {
bool break_existance = block(i);
if(break_existance) {
break;
}
}
return self;
}
int times() with void block() {
for(int i=0; i<self; i++) {
bool break_existance = block();
if(break_existance) {
break;
}
}
return self;
}
int toInt() {
return new int(self);
}
bool toBool() {
return self != 0;
}
native virtual void setValue(int value);
virtual int hashValue() {
return self;
}
virtual int clone() throws Exception{
int new_object = new int();
new_object.setValue(self);
return new_object;
}
virtual int dup() {
return self.clone();
}
}
native struct byte implements IComparableMore, ICloneable, IInspectable includes OperatorModule
{
native virtual void setValue(byte value);
native int toInt();
native long toLong();
byte toByte() {
return new byte(self);
}
virtual String toString() {
return self.toInt().toString();
}
virtual int hashValue() {
return self.toInt();
}
virtual byte clone() throws Exception{
byte new_object = new byte();
new_object.setValue(self);
return new_object;
}
virtual byte dup() {
return self.clone();
}
byte(int value) {
self.setValue(value.toByte());
}
}
native struct short implements ICloneable, IComparableMore, IInspectable includes OperatorModule
{
native virtual void setValue(short value);
native int toInt();
native long toLong();
short toShort() {
return new short(self);
}
virtual String toString() {
return self.toInt().toString();
}
virtual int hashValue() {
return self.toInt();
}
virtual short clone() throws Exception{
short new_object = new short();
new_object.setValue(self);
return new_object;
}
virtual short dup() {
return self.clone();
}
short(int value) {
self.setValue(value.toShort());
}
}
native struct uint implements ICloneable, IComparableMore, IInspectable includes OperatorModule
{
native virtual void setValue(uint value);
native int toInt();
native long toLong();
virtual native String toString();
uint toUInt() {
return new uint(self);
}
virtual int hashValue() {
return self.toInt();
}
virtual uint clone() throws Exception{
uint new_object = new uint();
new_object.setValue(self);
return new_object;
}
virtual uint dup() {
return self.clone();
}
uint(int value) {
self.setValue(value.toUInt());
}
}
native struct long implements ICloneable, IComparableMore, IInspectable includes OperatorModule
{
native virtual void setValue(long value);
native byte toByte();
native short toShort();
native int toInt();
native uint toUInt();
native float toFloat();
native double toDouble();
native char toChar();
long toLong() {
return new long(self);
}
virtual native String toString();
virtual int hashValue() {
return self.toInt();
}
virtual long clone() throws Exception{
long new_object = new long();
new_object.setValue(self);
return new_object;
}
virtual long dup() {
return self.clone();
}
long(int value) {
self.setValue(value.toLong());
}
}
native struct char implements ICloneable, IComparableMore, IInspectable
{
native virtual void setValue(char value);
native int toInt();
virtual native String toString();
virtual int hashValue() {
return self.toInt();
}
char toChar() {
return new char(self);
}
virtual char clone() throws Exception{
char new_object = new char();
new_object.setValue(self);
return new_object;
}
virtual char dup() {
return self.clone();
}
virtual bool operator==(char@Nullable value) {
if(self.type() == Null || value.type() == Null) {
return self.type() == Null && value.type() == Null;
}
return self.toInt() == value.toInt();
}
virtual bool operator!=(char@Nullable value) {
return !(self == value);
}
virtual bool operator <(char value) {
return self.toInt() < value.toInt();
}
virtual bool operator <=(char value) {
return self.toInt() <= value.toInt();
}
virtual bool operator >(char value) {
return self.toInt() > value.toInt();
}
virtual bool operator >=(char value) {
return self.toInt() >= value.toInt();
}
char upcase() {
char result = 0.toChar();
if(self >= 'a' && self <= 'z') {
result = (self.toInt() - 'a'.toInt() + 'A'.toInt()).toChar();
}
else {
result = self;
}
return result;
}
char downcase() {
char result = 0.toChar();
if(self >= 'A' && self <= 'Z') {
result = (self.toInt() - 'A'.toInt() + 'a'.toInt()).toChar();
}
else {
result = self;
}
return result;
}
bool isControlCharacter() {
return self >= 0x00.toChar() && self < ' ' || self == 0x7F.toChar();
}
bool isAsciiCharacter() {
return self >= 0x00.toChar() && self <= 0xFF.toChar();
}
bool isDigitCharacter() {
return self >= '0' && self <= '9';
}
bool isAlphabetCharacter() {
return self >= 'a' && self <= 'z' || self >= 'A' && self <= 'Z';
}
bool isNonControlCharacter() {
return self >= ' ' && self <= '~';
}
}
native struct float implements ICloneable, IInspectable, IComparableMore includes OperatorModuleForDouble
{
native int toInt();
native double toDouble();
virtual native String toString();
float toFloat() {
return new float(self);
}
float(int value) {
self.setValue(value.toDouble());
}
native virtual void setValue(float value);
virtual float clone() throws Exception{
float new_object = new float();
new_object.setValue(self);
return new_object;
}
virtual float dup() {
return self.clone();
}
}
native struct double implements ICloneable, IInspectable, IComparableMore includes OperatorModuleForDouble
{
native int toInt();
native float toFloat();
virtual native String toString();
native virtual void setValue(double value);
double(int value) {
self.setValue(value.toDouble());
}
double toDouble() {
return new double(self);
}
virtual double clone() throws Exception{
double new_object = new double();
new_object.setValue(self);
return new_object;
}
virtual double dup() {
return self.clone();
}
}
native struct bool implements IComparable, ICloneable, IInspectable
{
native virtual void setValue(bool value);
bool toBool() {
return new bool(self);
}
virtual bool clone() throws Exception{
bool new_object = new bool();
new_object.setValue(self);
return new_object;
}
virtual bool dup() {
return self.clone();
}
virtual String toString() {
if(self) {
return "true";
}
else {
return "false";
}
}
int toInt() {
if(self) {
return 1;
}
else {
return 0;
}
}
bool operator !() {
return \!self;
}
bool operator &&(bool value) {
return self \&& value;
}
bool operator ||(bool value) {
return self \|| value;
}
virtual bool operator==(bool@Nullable value) {
return self \== value;
}
virtual bool operator !=(bool@Nullable value) {
return self \!= value;
}
}
native struct pointer implements IComparable, IInspectable, ICloneable
{
native virtual void setValue(pointer value);
native virtual String toString();
pointer toPointer() {
return new pointer(self);
}
native bool equals(pointer right);
virtual pointer clone() {
pointer new_object = new pointer();
new_object.setValue(self);
return new_object;
}
virtual pointer dup() {
return self.clone();
}
native void forward(int size=1) throws NullPointerException, Exception;
native void backward(int size=1) throws NullPointerException, Exception;
native byte getByte() throws NullPointerException;
native short getShort() throws NullPointerException;
native uint getUInt() throws NullPointerException;
native long getLong() throws NullPointerException;
virtual bool operator==(pointer@Nullable right) {
if(self.type() == Null || right.type() == Null) {
return self.type() == Null && right.type() == Null;
}
return self.equals(right);
}
virtual bool operator!=(pointer@Nullable right) {
return !(self == right);
}
}
enum Encoding
{
Binary, ASCII, Utf8, EucJP, SJIS
}
abstract native class Regex implements IComparable, ICloneable
{
abstract virtual String source();
abstract virtual bool ignoreCase();
abstract virtual bool multiLine();
abstract virtual bool global();
abstract virtual Encoding encode();
abstract virtual void setIgnoreCase(bool ignore_case);
abstract virtual void setMultiLine(bool mutiline);
abstract virtual void setGlobal(bool global);
abstract virtual void setEncode(Encoding encode);
abstract virtual void compile(String source, bool ignore_case, bool mutiline, bool global, Encoding encode) throws InvalidRegexException;
virtual bool operator==(Regex@Nullable right) {
if(self.type() == Null || right.type() == Null) {
return self.type() == Null && right.type() == Null;
}
return self.source() == right.source() && self.ignoreCase() == right.ignoreCase() && self.multiLine() == right.multiLine() && self.global() == right.global() && self.encode() == right.encode();
}
virtual bool operator !=(Regex@Nullable right) {
return !(self == right);
}
virtual void setValue(Regex value) {
}
virtual Regex dup() {
return self;
}
virtual Regex clone() {
return self;
}
}
native class OnigurumaRegex implements ICloneable, IInspectable extends Regex
{
native virtual String source();
native virtual bool ignoreCase();
native virtual bool multiLine();
native virtual Encoding encode();
native virtual bool global();
native virtual void setIgnoreCase(bool ignore_case);
native virtual void setMultiLine(bool mutiline);
native virtual void setGlobal(bool global);
native virtual void setEncode(Encoding encode);
native virtual void setValue(OnigurumaRegex regex);
native virtual void compile(String source, bool ignore_case, bool mutiline, bool global, Encoding encode) throws InvalidRegexException;
OnigurumaRegex(String source, bool ignore_case=false, bool multiline=false, bool global=false, Encoding encode=Encoding.Utf8)
{
self.compile(source, ignore_case, multiline, global, encode);
}
virtual OnigurumaRegex clone() throws Exception{
return new OnigurumaRegex(self.source(), self.ignoreCase(), self.multiLine(), self.global(), self.encode());
}
virtual OnigurumaRegex dup() {
OnigurumaRegex regex = new OnigurumaRegex();
regex.setValue(self);
return regex;
}
virtual String toString() {
return self.source();
}
}
native class StringBuffer implements IComparable, ICloneable, IInspectable
{
native virtual void setValue(StringBuffer value);
native virtual String toString();
native int length();
native virtual void setValue(String value);
StringBuffer(String value) {
self.setValue(value);
}
virtual StringBuffer clone() {
return new StringBuffer(self);
}
virtual StringBuffer dup() {
return self.clone();
}
native StringBuffer append(String value);
StringBuffer operator+=(String value) {
return self.append(value);
}
native StringBuffer append(char c);
StringBuffer operator+=(char value) {
return self.append(value);
}
StringBuffer clear() {
self.setValue("");
return self;
}
virtual bool operator==(StringBuffer@Nullable right) {
if(self.type() == Null || right.type() == Null) {
return self.type() != Null || right.type() != Null;
}
return self.toString() == right.toString();
}
virtual bool operator!=(StringBuffer@Nullable right) {
return !(self == right);
}
}
native class Parser implements IComparable, ICloneable, IInspectable
{
native virtual void setValue(Parser value);
native virtual String toString() throws Exception;
native long point() throws Exception;
native void setString(String value);
native void setPoint(long point);
native bool end() throws Exception;
native char getChar();
native String getString(int num) throws Exception;
native Parser forward(int num=1);
native Parser backward(int num=1);
Parser(String value) {
self.setString(value);
}
virtual Parser clone() {
return new Parser(self);
}
virtual Parser dup() {
return self.clone();
}
virtual bool operator==(Parser@Nullable right) {
if(self.type() == Null || right.type() == Null) {
return self.type() != Null || right.type() != Null;
}
return self.toString() == right.toString() && self.point() == right.point();
}
virtual bool operator!=(Parser@Nullable right) {
return !(self == right);
}
}
native class String implements IComparableMore, ICloneable, IHashKey, IInspectable
{
native virtual void setValue(String value);
native int toInt();
native double toDouble();
native int length();
native char@CheckForNull char(int index);
native char replace(int index, char character) throws RangeException;
native Bytes toBytes() throws ConvertingStringCodeException;
native int cmp(String right, bool ignore_case=false);
native void match(Regex regex, int@Nullable offset=0, int count=1) with void block(int begin, int end, Array<Range> group_strings);
native void matchReverse(Regex regex, int@Nullable offset=null, int count=1) with void block(int begin, int end, Array<Range> group_strings);
virtual void outputValueForInterpreter() {
("=> \"" + self.toString().chomp() + "\"").println();
}
virtual String toString() {
return new String(self);
}
Parser toParser() {
return new Parser(self);
}
bool operator =~ (Regex regex, Array<String>@Nullable group_strings=null) throws Exception
{
bool result = false;
if(!(group_strings == null || group_strings.type() == Array<String> && group_strings.length() == 0))
{
throw new Exception("Invalid group_string parametor");
}
self.match(regex, 0, 1) {
|int begin, int end, Array<Range> group_strings2|
if(group_strings != null) {
for(int i=0; i<group_strings2.length(); i++) {
Range range = group_strings2[i];
String group_string = self[range.head()..range.tail()];
group_strings.add(group_string);
}
}
result = true;
break;
}
return result;
}
virtual String clone() throws Exception{
String new_string = new String();
new_string.setValue(self);
return new_string;
}
virtual String dup() {
return self.clone();
}
virtual int hashValue() {
int hash_value = 0;
for(int i=0; i<self.length(); i++) {
hash_value += self.char(i).toInt();
}
return hash_value;
}
void print() {
Clover.print(self);
}
void println() {
Clover.println(self);
}
char@CheckForNull operator[] (int index) {
return self.char(index);
}
char operator[]= (int index, char character) throws RangeException {
return self.replace(index, character);
}
String operator[] (Range range) {
Range corrected_range = new Range();
range.correct(corrected_range, self.length());
StringBuffer result = new StringBuffer();
if(self.length() == 0) {
}
else if(corrected_range.head() < corrected_range.tail()) {
for(int i=corrected_range.head(); i<corrected_range.tail(); i++) {
result += self[i];
}
}
else {
for(int i=corrected_range.head()-1; i>=corrected_range.tail(); i--) {
result += self[i];
}
}
return result.toString();
}
String operator +(String value) {
return self \+ value;
}
String operator+= (String string) {
self.setValue(self + string);
return self;
}
virtual bool operator==(String@Nullable right) {
return (self \== right);
}
virtual bool operator!=(String@Nullable right) {
return !(self \== right);
}
virtual bool operator>(String right) {
return self.cmp(right) == 1;
}
virtual bool operator<(String right) {
return self.cmp(right) == -1;
}
virtual bool operator>=(String right) {
return self.cmp(right) == 1 || self \== right;
}
virtual bool operator<=(String right) {
return self.cmp(right) == -1 || self \== right;
}
String operator *(int num) {
StringBuffer new_string = new StringBuffer();
for(int i=0; i<num; i++) {
new_string += self;
}
return new_string.toString();
}
bool asciiOnly() {
for(int i=0; i<self.length(); i++) {
if(!self[i].isAsciiCharacter()) {
return false;
}
}
return true;
}
String capitalize() {
StringBuffer result = new StringBuffer();
if(self.length() > 0) {
result += self[0].upcase();
}
if(self.length() > 1) {
result += self[1..null];
}
return result.toString();
}
String each() with void block(char c) {
for(int i=0; i<self.length(); i++) {
bool break_existance = block(self[i]);
if(break_existance) {
break;
}
}
return self;
}
String chomp(String separator=null) {
String result = "";
if(separator == null) {
if(self.length() >= 2) {
if(self[-2] == '\r' && self[-1] == '\n') {
result = self[0..-2];
}
else if(self[-1] == '\r' || self[-1] == '\n') {
result = self[0..-1];
}
else {
result = self.clone();
}
}
else if(self.length() >= 1) {
if(self[-1] == '\r' || self[-1] == '\n') {
result = self[0..-1];
}
else {
result = self.clone();
}
}
}
else {
if(self.length() >= separator.length()) {
if(self[self.length()-separator.length()..null] == separator) {
result = self[0..self.length()-separator.length()];
}
else {
result = self.clone();
}
}
}
return result;
}
String chop() {
String result = "";
if(self[-2] == '\r' && self[-1] == '\n') {
result = self[0..-2];
}
else {
result = self[0..-1];
}
return result;
}
int count(char character) {
int count = 0;
for(int i=0; i<self.length(); i++) {
if(self[i] == character) {
count++;
}
}
return count;
}
int count(Regex regex) {
int c = 0;
regex.setGlobal(true);
self.match(regex, 0, 1) {
|int begin, int end, Array<Range> group_strings2|
c++;
}
return c;
}
String delete(char character) {
StringBuffer result = new StringBuffer();
Parser parser = self.toParser();
while(!parser.end()) {
char c = parser.getChar();
if(c != character) {
result += c;
}
parser.forward(1);
}
return result.toString();
}
String delete(Regex regex) {
StringBuffer result = new StringBuffer();
int begin_before = 0;
//regex.setGlobal(true);
self.match(regex, 0, 1) {
|int begin, int end, Array<Range> group_strings2|
if(begin_before < begin) {
result += self[begin_before..begin];
}
begin_before = end;
}
if(begin_before < self.length()) {
result += self[begin_before..null];
}
return result.toString();
}
String downcase() {
StringBuffer result = new StringBuffer();
Parser parser = self.toParser();
while(!parser.end()) {
result += parser.getChar().downcase();
parser.forward(1);
}
return result.toString();
}
Array<String> lines(String separator="\n") {
Array<String> result = new Array<String>();
StringBuffer line = new StringBuffer();
Parser parser = self.toParser();
while(!parser.end()) {
if(parser.getString(separator.length()) == separator) {
line.append(separator);
result.add(line.toString());
line.clear();
parser.forward(separator.length());
}
else {
line.append(parser.getChar());
parser.forward(1);
}
}
if(line.toString() != "") {
result.add(line.toString());
}
return result;
}
String sub(Regex regex, String replacement) {
StringBuffer result = new StringBuffer();
int begin_before = 0;
self.match(regex, 0, 1) {
|int begin, int end, Array<Range> group_strings2|
if(begin_before < begin) {
result += self[begin_before..begin];
}
result += replacement;
begin_before = end;
}
if(begin_before < self.length()) {
result += self[begin_before..null];
}
return result.toString();
}
String sub(Regex regex) with String block(Array<String> group_strings, String prematch, String match, String postmatch)
{
StringBuffer result = new StringBuffer();
int begin_before = 0;
self.match(regex, 0, 1) {
|int begin, int end, Array<Range> group_strings|
if(begin_before < begin) {
result += self[begin_before..begin];
}
/// call block ///
Array<String> group_strings2 = new Array<String>();
group_strings.each() { |Range range|
group_strings2.add(self[range]);
}
String prematch = self[0..begin];
String match = self[begin..end];