-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstdlib.kat
763 lines (717 loc) · 19.5 KB
/
stdlib.kat
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
in $true: 1;
in $false: 0;
def arr;
(** Returns an array table containing all the arguments passed to the function ($_). **)
return $_;
ok;
def array;
(** Returns an array table containing all the arguments passed to the function ($_). **)
return $_;
ok;
def tuple;
# Generates a table with every value in $_ as its keys and 1s as its values.
# For use with ::.
$tuple: table;
for $_;
$tuple[$_[$_r]]: 1;
ok;
return $tuple;
ok;
def in_array;
(** Returns if $_[1] is found in the array table $_[2]. **)
in $needle: $_[1];
in $haystack: $_[2];
in $i: 1;
while $haystack[$i];
if $haystack[$i] = $needle;
return $i;
ok;
in $i: $i + 1;
ok;
return 0;
ok;
def print_arr;
(** Prints an array, sorted. **)
in $array: $_[1];
in $i: 1;
printc("(");
until $i > len($array);
printc("<");
printc($array[$i]);
printc(">");
$i: $i + 1;
unless $i > len($array);
printc(", ");
ok;
ok;
print(")");
ok;
def arrprint;
print_arr($_[1]);
ok;
def ceil;
(** Rounds $_[1] to the nearest higher integer. **)
return floor($_[1]) + 1;
ok;
def append;
(** Appends the value $_[2] to the array table $_[1] (in-place) and returns said table. **)
in $table: $_[1];
in $table[len($_[1]) + 1]: $_[2];
return $table;
ok;
def init_table;
(** Returns an table with the alternating values passed to the function as key-value pairs.
For example calling init_table("a", "b", "c", "d") will return the value containing the keys
"a" and "c" so that table{a} is "b" and table{c} is "d". **)
in $table: table;
in $i: 1;
in $key: "";
while is($_[$i]);
in $token: $_[$i];
if $i % 2 = 1;
in $key: $token;
ok;
if $i % 2 = 0;
in $table[$key]: $token;
ok;
in $i: $i + 1;
ok;
return $table;
ok;
def join;
(** Returns its parameters joined as a string. **)
in $result: "";
in $i: 1;
while is($_[$i]);
in $token: $_[$i];
in $result: $result & $token;
in $i: $i + 1;
ok;
return $result;
ok;
def arrjoin;
(** Joins the array table $_[1] using $_[2] as a delimiter. If $_[2] is not set, it defaults to "". **)
if len($_) = 0;
return "";
ok;
in $result: "";
in $tokens: $_[1];
in $separator: "";
if is($_[2]);
in $separator: $_[2];
ok;
in $i: 1;
while is($tokens[$i]);
in $result: $result & $separator & $tokens[$i];
in $i: $i + 1;
ok;
return substr($result, len($separator) + 1);
ok;
def join_with;
(** Returns its parameters joined as a string using the first parameter as the joiner. **)
in $result: "";
in $joiner: $_[1];
in $i: 2;
while is($_[$i]);
in $token: $_[$i];
in $j: "";
if $i > 2;
$j: $joiner;
ok;
in $result: $result & $j & $token;
in $i: $i + 1;
ok;
return $result;
ok;
def index;
(** Searches for the value $_[2] in the array table $_[1]. If it's found, returns its index. Otherwise returns 0. **)
in $haystack: $_[1];
in $needle: $_[2];
if len($haystack) < len($needle);
return 0;
ok;
in $i: 1;
while substr($haystack, $i, len($needle));
if $_r = $needle;
return $i;
ok;
in $i: $i + 1;
ok;
return 0;
ok;
def replace_slow;
(** Replaces all instances of the string $_[2] in the string $_[1] by $_[3]. **)
in $haystack: $_[1];
in $needle: $_[2];
in $replacement: $_[3];
if len($haystack) < len($needle);
return $haystack;
ok;
$parsed_text: "";
$buffer: "";
for $haystack;
$char: $haystack[$_r];
$buffer: $buffer & $char;
if len($buffer) = len($needle);
if $buffer = $needle;
$parsed_text: $parsed_text & $replacement;
$buffer: "";
else;
$parsed_text: $parsed_text & $buffer[1];
$buffer: substr($buffer, 2);
ok;
ok;
ok;
$parsed_text: $parsed_text & $buffer;
return $parsed_text;
ok;
def starts_with;
(** Returns true if the string $_[1] starts with the string $_[2]. **)
in $string: $_[1];
in $prefix: $_[2];
if len($prefix) > len($string);
return $false;
ok;
return substr($string, 1, len($prefix)) = $prefix;
ok;
def ends_with;
(** Returns true if the string $_[1] ends with the string $_[2]. **)
in $string: $_[1];
in $prefix: $_[2];
if len($prefix) > len($string);
return $false;
ok;
return substr($string, -len($prefix), len($prefix)) = $prefix;
ok;
def is_whitespace;
(** Returns true if the string $_[1] is one or more whitespace characters. An empty string is considered whitespace. **)
in $string: $_[1];
if !len($string);
return $true;
elif len($string) = 1;
return $string :: "\n\t ";
else;
in $i: 1;
while $i <= len($string);
if !($string[$i] :: "\n\t ");
return $false;
ok;
in $i: $i + 1;
ok;
ok;
return $true;
ok;
def ltrim;
(** Left-trims the string $_[1]. If $_[2] is set, it trims the value in $_[2]. Otherwise it trims whitespace. **)
in $string: $_[1];
in $trim_ws: $true;
if is($_[2]);
in $trim_ws: $false;
in $needle: $_[2];
ok;
(* If trimming whitespace *)
if $trim_ws;
in $i: 1;
while $i <= len($string);
if !is_whitespace(substr($string, $i, 1));
break;
ok;
in $i: $i + 1;
ok;
return substr($string, $i);
ok;
(* If trimming text *)
unless $_r;
if len($needle) > len($string);
return $string;
ok;
in $i: 1;
while $i + len($needle) - 1 <= len($string);
if substr($string, $i, len($needle)) <> $needle;
break;
ok;
in $i: $i + len($needle);
ok;
return substr($string, $i);
ok;
ok;
def rtrim;
(** Right-trims the string $_[1]. If $_[2] is set, it trims the value in $_[2]. Otherwise it trims whitespace. **)
in $string: $_[1];
in $trim_ws: $true;
if is($_[2]);
in $trim_ws: $false;
in $needle: $_[2];
ok;
(* If trimming whitespace *)
if $trim_ws;
in $i: len($string);
while $i >= 1;
if !is_whitespace(substr($string, $i, 1));
break;
ok;
in $i: $i - 1;
ok;
return substr($string, 0, $i);
ok;
(* If trimming text *)
unless $_r;
if len($needle) > len($string);
return $string;
ok;
in $i: (len($string) - len($needle)) + 1;
while $i >= 1;
if substr($string, $i, len($needle)) <> $needle;
in $i: $i + len($needle) - 1;
break;
ok;
in $i: $i - len($needle);
ok;
return substr($string, 1, $i);
ok;
ok;
def trim;
(** Left and right-trims the string $_[1]. If $_[2] is set, it trims the value in $_[2]. Otherwise it trims whitespace. **)
in $string: $_[1];
if is($_[2]);
return rtrim(ltrim($string, $_[2]), $_[2]);
ok;
return rtrim(ltrim($string));
ok;
def prototype;
(** Creates a shallow copy of object $_[1] and returns it. **)
$base_object: $_[1];
$copy_object: table;
for $base_object;
$copy_object[$_r]: $base_object[$_r];
ok;
return $copy_object;
ok;
def printcs;
(** Prints comma separated values **)
print(arrjoin($_, ", "));
ok;
def shift_arr;
(** Shifts the array $_[1] a number of $_[2] times to the left. For example, if [a, b, c] is shifted one
time, it becomes [b, c, a]. If it's shifted twice, it becomes [c, a, b]. **)
$array: $_[1];
$max: len($array);
$count: $_[2] % $max;
if $count <= 0;
return;
ok;
until $count = 0;
$index: 1;
$swap: $array[1];
while $index < $max;
$array[$index]: $array[$index + 1];
$index: $index + 1;
ok;
$array[$max]: $swap;
$count: $count - 1;
ok;
ok;
def ucasec;
$letters: "abcdefghijklmnopqrstuvwxyz";
$letters_u: "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$index: 1;
while $index <= len($letters);
if $letters[$index] = $_[1];
return $letters_u[$index];
ok;
$index: $index + 1;
ok;
return $_[1];
ok;
def lcasec;
$letters: "abcdefghijklmnopqrstuvwxyz";
$letters_u: "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$index: 1;
while $index <= len($letters);
if $letters_u[$index] = $_[1];
return $letters[$index];
ok;
$index: $index + 1;
ok;
return $_[1];
ok;
def ucase;
$index: 1;
$buffer: "";
while $index <= len($_[1]);
$buffer: $buffer & ucasec($_[1][$index]);
$index: $index + 1;
ok;
return $buffer;
ok;
def lcase;
$index: 1;
$buffer: "";
while $index <= len($_[1]);
$buffer: $buffer & lcasec($_[1][$index]);
$index: $index + 1;
ok;
return $buffer;
ok;
def ucasefirst;
$index: 2;
$buffer: "";
if len($_[1]) >= 1;
$buffer: ucasec($_[1][1]);
ok;
while $index <= len($_[1]);
$buffer: $buffer & lcasec($_[1][$index]);
$index: $index + 1;
ok;
return $buffer;
ok;
def title;
$index: 1;
$buffer: "";
$last_char_space: $true;
while $index <= len($_[1]);
$char: $_[1][$index];
if is_whitespace($char);
$last_char_space: $true;
$buffer: $buffer & $char;
elif !$last_char_space;
$buffer: $buffer & lcasec($_[1][$index]);
else;
$buffer: $buffer & ucasec($_[1][$index]);
$last_char_space: $false;
ok;
$index: $index + 1;
ok;
return $buffer;
ok;
def halt;
(** Prints an error message and exits. **)
print(arrjoin($_));
exit(1);
ok;
def parse_json;
$json: trim($_[1]);
$json: substr($json, 2, len($json) - 2);
$in_key_part: $true;
$parsing_string: $false;
$current_key: "";
$current_value: "";
$list_depth: 0;
$obj_depth: 0;
$json_table: table;
for $json;
$char: $json[$_r];
if $in_key_part;
if $char = "\"" && !$parsing_string;
$current_key: "";
$parsing_string: $true;
elif $char = "\"" && $parsing_string;
$parsing_string: $false;
elif $parsing_string;
$current_key: $current_key & $char;
elif !$parsing_string && $char = ":";
$in_key_part: $false;
$current_value: "";
$obj_depth: 0;
$list_depth: 0;
ok;
else;
if $char = "\"" && !$parsing_string && $obj_depth = 0 && $list_depth = 0;
$parsing_string: $true;
$current_value: "s";
elif $char = "\"" && $parsing_string && $obj_depth = 0 && $list_depth = 0;
$parsing_string: $false;
elif !$parsing_string && $char = "," && $obj_depth = 0 && $list_depth = 0;
json_aux_append_value($json_table, $current_key, $current_value);
$in_key_part: $true;
else;
if $char = "{" && !$parsing_string;
if $obj_depth = 0 && $list_depth = 0;
$current_value: "o";
ok;
$obj_depth: $obj_depth + 1;
elif $char = "[" && !$parsing_string;
if $obj_depth = 0 && $list_depth = 0;
$current_value: "l";
ok;
$list_depth: $list_depth + 1;
elif $char = "}" && !$parsing_string;
$obj_depth: $obj_depth - 1;
elif $char = "]" && !$parsing_string;
$list_depth: $list_depth - 1;
ok;
$current_value: $current_value & $char;
ok;
ok;
ok;
# Last value
if len($current_value);
json_aux_append_value($json_table, $current_key, $current_value);
ok;
return $json_table;
ok;
def json_aux_append_value;
$json_table: $_[1];
$current_key: $_[2];
$current_value: $_[3];
if $current_value[1] = "o";
$json_table[$current_key]: parse_json(substr($current_value, 2));
elif $current_value[1] = "l";
$json_table[$current_key]: json_aux_parse_list(substr($current_value, 2));
elif $current_value[1] = "s";
$json_table[$current_key]: substr($current_value, 2);
else;
$current_value: trim($current_value);
if !len($current_value);
# Do nothing
elif lcase($current_value) = "true";
$json_table[$current_key]: $true;
elif lcase($current_value) = "false";
$json_table[$current_key]: $false;
else;
$json_table[$current_key]: 0 + $current_value;
ok;
ok;
ok;
def json_aux_parse_list;
$list: trim($_[1]);
$list: substr($list, 2, len($list) - 2);
$in_string: $false;
$list_depth: 0;
$obj_depth: 0;
$buffer: "";
$list_table: table;
for $list;
$char: $list[$_r];
if $char = "\"" && !$in_string && $obj_depth = 0 && $list_depth = 0;
$in_string: $true;
$buffer: "s";
elif $char = "\"" && $in_string && $obj_depth = 0 && $list_depth = 0;
$in_string: $false;
elif $char = "," && !$in_string && $obj_depth = 0 && $list_depth = 0;
json_aux_append_value($list_table, len($list_table) + 1, $buffer);
else;
if $char = "{" && !$in_string;
if $obj_depth = 0 && $list_depth = 0;
$buffer: "o";
ok;
$obj_depth: $obj_depth + 1;
elif $char = "[" && !$in_string;
if $obj_depth = 0 && $list_depth = 0;
$buffer: "l";
ok;
$list_depth: $list_depth + 1;
elif $char = "}" && !$in_string;
$obj_depth: $obj_depth - 1;
elif $char = "]" && !$in_string;
$list_depth: $list_depth - 1;
ok;
$buffer: $buffer & $char;
ok;
ok;
if len($buffer);
json_aux_append_value($list_table, len($list_table) + 1, $buffer);
ok;
return $list_table;
ok;
def is_numeric;
# Returns true if $_[1] is a number, false otherwise.
$string: trim($_[1]);
$found_point: $false;
$last_char_dot: $false;
$numlen: 0;
if !len($string);
return $false;
ok;
$index: 1;
if $string[1] :: "+-";
$index: 2;
ok;
while $index <= len($string);
$char: $string[$index];
if $char = ".";
if $found_point;
return $false;
ok;
if $numlen = 0;
return $false;
ok;
$last_char_dot: $true;
$found_point: $true;
elif !($char :: "0123456789");
return $false;
else;
$numlen: $numlen + 1;
$last_char_dot: $false;
ok;
$index: $index + 1;
ok;
return $numlen > 0 && !$last_char_dot;
ok;
def to_num;
# If a value is numeric, it returns its numeric representation. Otherwise it returns 0.
if is_numeric($_[1]);
return 0 + $_[1];
else;
return 0;
ok;
ok;
def last;
# Returns the last value of an array
return $_[len($_)];
ok;
def call_api;
# Calls the an API using $_[1] as the endpoint and $_[2+]
# as data fields in the payload. Returns the response of the api as plain text or "" if failed.
$url: $_[1];
$command: join("curl -X POST '", $url, "'");
$i: 2;
while is($_[$i]);
in $payload: $_[$i];
in $command: $command & " -d '" & $payload & "'";
in $i: $i + 1;
ok;
exec($command);
if $_exitcode != 0;
# print("Error when calling endpoint: ", $_stderr);
return "";
ok;
return trim($_stdout);
ok;
def get_http;
# Gets content of the webpage at $_[1] or "" if it failed.
$url: $_[1];
$command: join("curl '", $url, "'");
exec($command);
if $_exitcode != 0;
return "";
ok;
return $_stdout;
ok;
def plural;
# Used for plurals.
# If $_[1] is 1, returns "" or $_[2] if set.
# If $_[1] isn't 1, returns "s" or $_[3] if set.
if $_[1] = 1;
if is($_[2]);
return $_[2];
else;
return "";
ok;
else;
if is($_[3]);
return $_[3];
else;
return "s";
ok;
ok;
ok;
def wssplit;
# Splits string $_[1] using whitespace.
# If $_[2] is set, it splits that max amount of times.
# If $_[3] is true, empty tokens are ignored from the split.
$max: -1;
$ignore: $false;
if is($_[2]);
$max: $_[2];
ok;
if is($_[3]);
$ignore: $_[3];
ok;
return explode($_[1], arr(" ", "\n", "\t"), $max, $ignore);
ok;
def datetime;
# Returns a table containing:
# {dow}: day of week (1-7)
# {date}: current date (1-31)
# {month}: current month (1-12)
# {year}: current year (XXXX)
# {hour}: current hour (24 hour format)
# {h12}: current hour (12 hour format)
# {ampm}: "AM" or "PM"
# {min}: current minutes (0-59)
# {sec}: current seconds (0-59)
exec("date +\"%u %d %m %Y %H %I %p %M %S\"");
$time: table;
$tokens: wssplit($_stdout, -1, $true);
$time{dow}: $tokens[1];
$time{date}: $tokens[2];
$time{day}: $time{date};
$time{month}: $tokens[3];
$time{year}: $tokens[4];
$time{hour}: $tokens[5];
$time{h12}: $tokens[6];
$time{ampm}: $tokens[7];
$time{min}: $tokens[8];
$time{sec}: $tokens[9];
return $time;
ok;
def dataset;
# Creates a table with its keys being every element in $_ and each with a value of $true.
$set: table;
for $_;
$set[$_[$_r]]: $true;
ok;
return $set;
ok;
def arrdataset;
# Creates a table with its keys being every element in the array $_[1] and each with a value of $true.
$set: table;
for $_[1];
$set[$_[1][$_r]]: $true;
ok;
return $set;
ok;
def sort;
# Sorts $_[1] an array of values from lower to higher. If $_[2] is true, the table is sorted
# from higher to lower.
in $array: $_[1];
if is($_[2]);
in $low_to_high: $_[2];
else;
in $low_to_high: $true;
ok;
in $i: 1;
in $j: 2;
until $i = len($array);
until $j = len($array) + 1;
if $low_to_high;
if $array[$j] < $array[$i];
in $swap: $array[$i];
in $array[$i]: $array[$j];
in $array[$j]: $swap;
ok;
else;
if $array[$j] > $array[$i];
in $swap: $array[$i];
in $array[$i]: $array[$j];
in $array[$j]: $swap;
ok;
ok;
in $j: $j + 1;
ok;
in $i: $i + 1;
in $j: $i + 1;
ok;
return $array;
ok;
def abs;
# Turns $_[1] into a number and returns its absolute value.
in $num: to_num($_[1]);
if $num < 0;
in $num: -$num;
ok;
return $num;
ok;
def arrcount;
# Given an array of values $_[1] and a value $_[2], counts how many times the value appears in the array.
in $array: $_[1];
in $value: $_[2];
in $count: 0;
for $array;
if $array[$_r] = $value;
in $count: $count + 1;
ok;
ok;
return $count;
ok;