-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalc.js
3389 lines (2630 loc) · 92.2 KB
/
calc.js
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
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.calc = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";
var lex = require("./lex");
var parse = require("./parse");
var run = require("./run");
var print = require("./print");
function calc(code_, max_time) {
if(code_.substr(0, 5) != "calc=") {
throw "Error: there is no \"calc=\".";
}
var code = code_.substr(5, code_.length);
try {
var tokens = lex(code);
var ast = parse(tokens);
return run(ast, max_time, calc);
} catch(err) {
throw "calc=" + err_to_str(err);
}
}
var err_to_str = err => !(err instanceof Error)
? err
: "\n" + err.stack.split("\n").slice(0, 2).join("\n\t");
module.exports = {calc, print, err_to_str};
},{"./lex":3,"./parse":4,"./print":5,"./run":7}],2:[function(require,module,exports){
var help_menu = `
CALC=
Welcome to calc=, the stack language for chats! For a basic tutorial, type "calc= tut". If you already know stack based programming, use "calc= adv_tut".
Demos:
* Fibonacci: "calc= fib = {n -> 0 1 {x y -> y x y +} n iter drop} ; 0 9 .. $fib map"
* Factorial: "calc= 1 5 .. 1 $* fold"
`;
var tut_pages = [`
INTRODUCTION (0)
calc= is a programming language for chats. Every program starts with "calc=" and then has a series of instructions to follow. calc= is a stack based (or concatenative) language, which means all of it's computations will be done using a stack. You can push things on top, then pop them off to use them. Try the program "calc= 5 1 -", then proceed to the next page of the tutorial. To access a page use "calc= number tut", where you replace "number" by the page number. The next page of this tutorial is at "calc= 1 tut".
`,`
FIRST EXAMPLE (1)
As you can see, "calc= 5 1 -" gives back "calc= 4". This is because "5" pushed a "5" on top of the stack, then "1" pushed a "1". Finally "-" popped the top two elements from the stack, "5" and "1", and subtracted them to form "4". There are the four basic operators: "+", "-", "*", "/".
`,`
MULTIPLE OPERATIONS (2)
The problem now is: how do you make more complicated calculations like "4 * 3 - 2". In calc=, there is no order of operations, because each symbol is read from left to right, you always know which goes first. In the case of "4 * 3 - 2", you would write "calc= 4 3 * 2 -". In the normal math example, "*" goes first so in calc= it will be towards the front of the program. In the normal math example, you last thing you do is subtract, so it will be at the very end of the calc= program. Now on to a more complex example: how would write "2 + (5 - 1) / 3"? Try to write it yourself, the answer is on the next page.
`,`
COMPLEX EXAMPLE (3)
In calc=, "2 + (5 - 1) / 3" would be written as "calc= 5 1 - 3 / 2 +" (there are other ways, but this is the most straight forward one). You can see that parenthesis are not necesarry in calc= because the order is always explicit, "-" goes first, then "/", and finally "+".
`,`
CALLING FUNCTIONS (4)
To call any function, you just need to provide it's arguments and write it's name. For example, the absolute value function, shortened to "abs", can be used like so: "calc= -2 abs" -> "calc=2". Use "calc= page" to find functions you need.
`,`
MAKING VARIABLES (5)
Variables in calc= are not like normal mathematical variables. They are most similar to constants. For example: "calc= x = 3 ; x 4 +" defines a variable x as 3, then uses it. To define a variable, all you need to do is write the name, an equals sign (=), it's value, and a somecolon (;) to finish it. Here is a more complex example: "calc= x = 2 sqrt ; y = 6 2 + ; x 2 / y". x is set to the square root of 2, y is 6 + 2, and the result is x / 2 and y.
`,`
CUSTOM FUNCTIONS (6)
You can make your own functions as you would make a variable. For example, the square function would be defined and possibly used like so: "calc= square = {x -> x x *} ; 4 square" -> "calc=16". Here, the function definition starts at the "{" and ends at the "}". The variable names before the "->" are the arguments, in that case, "x" is the only argument. What comes after the "->" is the function body. When the function, gets called, the arguments get defined and the body gets called.
`,`
LISTS (7)
The next useful feature would be lists. Lists are somewhat similar to mathematical sets, except that elements inside it are ordered and may be repeated. The syntax for lists is like so: "calc= [3, 6, 1]", makes the list containing, in that order, 3, 6, and 1. Lists are the best way to manipulate multiple values at once in calc=. One of the ways to manipulate these values is to "map" over them. For example, to add 1 to all the elements in a list, you would map over it with the function that adds one. The function that adds one would be "calc= {x -> x 1 +}". To actually map over a list, use the function "map", possible like so: "calc= [3, 6, 1] {x -> x 1 +} map" giving "calc=[4, 7, 2]".
`,`
THANK YOU (8)
That's the entire basic tutorial for now. If you want to help add to it, you can contribute at https://github.com/Camto/calc .
`];
var adv_tut_pages = [`
INTRODUCTION (0)
calc= is a fairly typical concatenative language, with it's biggest difference being that it's meant to be run on one line. Every program has to start with "calc=", for compatibility with chat bots.
First up is numbers, calc= only has floating points, which are used to approximate integers. calc= numbers have the same syntax as any other language.
To access different pages of the tutorial, use "calc= number adv_tut" with the page number, "calc= 1 adv_tut", for example.
`,`
BUILT-INS, FUNCTIONS, AND OPERATORS (1)
When accessing these tutorial pages, you're using a built-in function, namely "adv_tut". You can use "calc= page" to go to the built-in index (which I recommend you check now), or "calc= $name page", where "name" is the name of a built-in or operator, for example "calc= $adv_tut page" or "calc= $+ page". By the way, almost every built-in has one or more aliases, which are alternate names you can use to refer to it, so you don't have to remember the main name if you're used to another one.
As for defining your own functions, you can define an anonymous function with "{arguments -> body}", for example "calc= {w h -> w 2 * h 2 * +}" pushes the perimited function to the stack (The "+" and "*" are operators, found at "calc= "operator" page"). But because functions can do what they want with the stack, this can be rewritten as "calc= {2 * swap 2 * +}". It may be harder to read, but calc= isn't meant to be especially readable, it's meant to be quick to write.
`,`
VARIABLES (2)
As part of the main program or function body (after the argument's "->"), you can have variable definitions. Variables are defined with "name = expression ; body", where expression is any calc= expression and body can be more variables or an expression. For example:
"calc= pythagoras = {x = dup fst ; y = snd ; x 2 ^ y 2 ^ + sqrt} ; "pythagoras of [3, 4] is" [3, 4] pythagoras "pythagoras of [5, 12] is" [5, 12] pythagoras"
(the lists and strings will be explained on the next page)
Variables are mutable using the built-ins at "calc= "state" page" and references (explained at "calc= 5 adv_tut"), but this is not recommended, as there is almost always a better way to do it. Also, as implemented now, mutation works well but still has a few bugs.
`,`
LISTS AND STRINGS (3)
Lists are the only composite data type in calc=, and have a syntax of "calc= [1, 2, "string"]". Strings are for the most part treated as lists of characters, and if a built-in that works with lists doesn't work with strings, then it will be fixed in a later calc= version.
As with the rest of the language, lists can abuse of the stack for more consiceness, for example "calc= [1, 2, 3]" will give the same result as "calc= 1 2 3 [,,]". The edge case here is that "calc= [1]" will not be the same as "calc= 1 []" because "[]" refers to an empty list, not one to be filled with one element. Instead, use "calc= 1 [id]", because "id" is a built-in that does nothing.
`,`
FUNCTIONAL LIST MANIPULATION (4)
Since calc= is an almost pure functional programming language, the list manipulation is done in a functional way, with "map", "filter", "fold", and more at "calc= "list" page". For more info on those three most important ones, visit their pages.
`,`
REFERENCES (5)
You can push references of variables, arguments, built-ins, or operators to the stack with "$". This means you can do
"calc= square = {2 ^} ; 1 5 .. $square map"
instead of
"calc= square = {2 ^} ; 1 5 .. {square} map"
and
"calc= 1 5 .. 1 $* fold"
instead of
"calc= 1 5 .. 1 {*} fold"
`,`
THANK YOU (6)
That's the entire advanced tutorial for now. If you want to help add to it, you can contribute at https://github.com/Camto/calc .
`];
var find_page_tip = `To find the help page of a built-in or an operator, just run "calc= $name page", where "name" is the name of the built-in or the operator.`
var page_index = `
HELP PAGES
This is the help page index!
${find_page_tip}
The built-ins are sorted by these categories:
* Help functions. "calc= "help" page"
* Basic functions. "calc= "basic" page"
* Flow control. "calc= "control" page"
* Stack functions. "calc= "stack" page"
* List functions. "calc= "list" page"
* Math functions. "calc= "math" page"
* Function functions. "calc= "function" page"
* State functions. "calc= "state" page"
* Operators. "calc= "operator" page"
`;
var page_categories = {
help: `
HELP FUNCTIONS
${find_page_tip}
These are the functions that explain calc=:
* page - Built-in/operator documentation.
* tut - Basic tutorial.
* adv_tut - Advanced tutorial.
`,
basic: `
BASIC FUNCTIONS
${find_page_tip}
These are the fundamental functions:
* type - Get type.
* true
* false
* num_to_str - Convert number to string.
* str_to_num - Convert string to number.
* eval - Evaluate calc= program.
`,
control: `
FLOW CONTROL
${find_page_tip}
There are the functions for controllin the program flow:
* if - If/else statement.
`,
stack: `
STACK FUNCTIONS
${find_page_tip}
These are the functions that manipulate the stack:
* dup - Duplicate.
* swap
* stack_reverse_n - Reverse top n items.
* drop
* drop_n - Drop n items.
* rot - Rotate.
* unrot - Rotate backward.
* roll - Roll.
* unroll - Roll backward.
* nip - Nip under.
* tuck - Tuck under.
* over - Duplicate over.
`,
list: `
LIST FUNCTIONS
${find_page_tip}
These are the functions for list manipulation:
* map - Map over list.
* fold - Fold list to the left.
* foldr - Fold list to the right.
* filter - Filter list with predicate.
* length - Length of.
* head - First item.
* snd - Second item.
* last - Last item.
* back_snd - Before last item.
* nth - N-th item.
* back_nth - N-th item from the end.
* init - All but first item.
* tail - All but last item.
* body - All but first and last item.
* reverse - Reverse list.
* reverse_n - Reverse last n items of list.
* pop_n - Remove last n items of list.
* elem - Is item in list.
* join - Join list into string.
* split - Split string into list.
* expl - Explode list items.
* group - Group into list.
* copy_group - Copy and group into list.
* box - Box item into list.
* copy_box - Copy and box item into list.
* group_all - Group all items into list.
* copy_group_all - Copy and group all items into list.
* list_dup - Duplicate top of list.
* list_swap - Swap top of list.
* list_rot - Rotate top of list.
* list_unrot - Rotate top of list backward.
* list_roll - Roll top of list.
* list_unroll - Roll top of list backward.
* list_nip - Nip under list.
* list_tuck - Tuck under list.
* list_over - Duplicate over on list.
`,
"math": `
MATH FUNCTIONS
${find_page_tip}
These are the mathematical functions:
* pi - The constant pi.
* tau - The constant tau.
* e - The constant e.
* abs - Absolute value.
* round - Round to the nearest integer.
* ceil - Round upwards.
* floor - Round downwards.
* max - Biggest or longest.
* min - Smallest or shortest.
* sgn - Sign of number.
* evn - Is number even.
* odd - Is number odd.
* rand - Generate random number.
* cos - Cosine of angle in radians.
* sin - Sine of angle in radians.
* tan - Tangent of angle in radians.
* sec - Secant of angle in radians.
* csc - Cosecant of angle in radians.
* cot - Cotangent of angle in radians.
* sqrt - Square root.
* cbrt - Cube root.
* root - N-th root.
* log - Logarithm.
* ln - Natural logarithm.
`,
"function": `
FUNCTION FUNCTIONS
${find_page_tip}
These are the functions that manipulate other functions:
* call - Call function or built-in.
* iter - Iterate over.
* id - Identity function.
* comp - Compose functions.
`,
state: `
STATE FUNCTIONS
${find_page_tip}
These are functions that manipulate the program state:
* set - Set variable.
* inc - Increment variable.
* dec - Decrement variable.
`,
operator: `
OPERATORS
${find_page_tip}
These are the calc= operators:
* "+" - Addition or concatenation.
* "-" - Subtraction or slicing.
* "*" - Multiplication, list-like repitition, or cartiesian products.
* "/" - Division.
* "^" - Exponetiation.
* "%" - Modulo.
* ".." - To from range.
* "&" - Logical and.
* "|" - Logical or.
* "!" - Logical not.
* "=" - Are equals.
* "!=" - Aren't equals.
* "<" - Less than or shorter than.
* ">" - More than or longer than.
* "<=" - Less than, equal to, longer than, or same length.
* ">=" - More than, equal to, shorter than, or same length.
`
};
var built_in_warning = "!WARNING: This function is discouraged from being used, the only reason it is here is for the few cases in which it is necessary!";
var help_pages = {
help: aliases => `
HELP MENU
Usage: "calc= help"
Aliases: ${aliases}.
It shows the main calc= menu.
`,
page: aliases => `
BUILT-IN/OPERATOR DOCUMENTATION
Usage: "calc= page" or "calc= $command page", where "command" is the name of a built-in or an operator.
Aliases: ${aliases}.
Examples:
* "calc= $+ page"
* "calc= $page page"
Without any arguments, it shows how to use the documentation. With a symbol or operator, it shows the help page of the built-in or operator.
`,
tut: aliases => `
BASIC TUTORIAL
Usage: "calc= tut" or "calc= n tut", where "n" is the tutorial page number.
Aliases: ${aliases}.
It shows the basic tutorial page.
`,
adv_tut: aliases => `
ADVANCED TUTORIAL
Usage: "calc= adv_tut" or "calc= n adv_tut", where "n" is the tutorial page number.
Aliases: ${aliases}.
It shows the advanced tutorial page.
`,
type: aliases => `
GET TYPE
Usage: "calc= value type", where "value" is any value.
Aliases: ${aliases}.
Examples:
* "calc= 1 type" -> "calc=number"
* "calc= {} type" -> "calc=function"
Returns the type of any given value. These can be: number, string, list, function, symbol, or operator.
`,
"true": aliases => `
TRUE
Usage: "calc= true".
Aliases: ${aliases}.
Examples:
* "calc= true" -> "calc=1"
Returns the simplest truthy value, 1.
`,
"false": aliases => `
FALSE
Usage: "calc= false".
Aliases: ${aliases}.
Examples:
* "calc= false" -> "calc=0"
Returns the simplest falsy value, 0.
`,
num_to_str: aliases => `
CONVERT NUMBER TO STRING
Usage: "calc= number num_to_str", where "number" is any number.
Aliases: ${aliases}.
Examples:
* "calc= 5 num_to_str" -> "calc=5"
* "calc= 5 num_to_str type" -> "calc=string"
It returns the number as a string.
`,
str_to_num: aliases => `
CONVERT STRING TO NUMBER
Usage: "calc= string str_to_num", where "string" is a string representing a number.
Aliases: ${aliases}.
Examples:
* "calc= "5" str_to_num" -> "calc=5"
* "calc= "5" str_to_num type" -> "calc=number"
It returns the string as a number.
`,
eval: aliases => `
EVALUATE CALC= PROGRAM
${built_in_warning}
Usage: "calc= program eval", where "program" is a string representing a calc= program.
Aliases: ${aliases}.
Examples:
* "calc= "calc= 1 2 + 6" eval" -> "calc=[3, 6]"
* "calc= "calc= " 5 num_to_str " 4 +" + + eval" -> "calc=[9]"
* "calc= "" {"calc= \"" swap "\" 'a+" + + eval fst} 3 iter" -> "calc=aaa"
* "calc= "calc= \\"calc= 1\\"eval" eval" -> "calc=[[1]]"
It returns the stack of the result of the program.
`,
"if": aliases => `
IF/ELSE STATEMENT
Usage: "calc= bool if_true if_false if", where "bool" is a truthy of falsy value, "if_true" is the branch for if the condition is true, and "if_false" is the alternate branch.
Aliases: ${aliases}.
Examples:
* "calc= 3 true {1+} {1-} if" -> "calc=4"
* "calc= 3 0 {1+} {1-} if" -> "calc=2"
* "calc= "oigb" $5 $3 if" -> "calc=5"
* "calc= false $5 $3 if" -> "calc=3"
If the condition is true, it executes the first branch, if not, then it executes the other branch.
For different types, there are different ways to test if it is truthy:
* Numbers that are 0 are falsy others are truthy.
* String and Lists are treated as a number of their length, emptiness being 0, or falsy.
* Functions, symbols, and operators are all truthy.
`,
dup: aliases => `
DUPLICATE
Usage: "calc= a dup", where "a" is any value.
Aliases: ${aliases}.
Examples:
* "calc= 5 dup" -> "calc=5 5"
* "calc= "ble" "str" dup" -> "calc=ble str str"
It duplicates the top value of the stack. You can use dup to use some value without discarding it.
`,
swap: aliases => `
SWAP
Usage: "calc= a b swap", where "a" and "b" can be any value.
Aliases: ${aliases}.
Examples:
* "calc= 2 5 swap" -> "calc=5 2"
* "calc= "gh" [1, 2, 3] swap" -> "calc=[1, 2, 3] gh"
It swaps the top 2 values of the stack. This means you can use a value underneath another.
`,
stack_reverse_n: aliases => `
REVERSE TOP N ITEMS
${built_in_warning}
Usage: "calc= n ... m num stack_reversen", where "num" is the amount of items you want to reverse and "n ... m" are the items.
Aliases: ${aliases}.
Examples:
* "calc= 'a 'd 'y 'f 4 stack_reversen" -> "calc=f y d a"
* "calc= 10 6 .. expl 1 5 .. expl 10 stack_reversen" -> "calc= 5 4 3 2 1 6 7 8 9 10"
It reverses the top num items of the stack.
`,
drop: aliases => `
DROP
Usage: "calc= a drop", where "a" is any value.
Aliases: ${aliases}.
Examples:
* "calc= 1 2 3 drop" -> "calc=1 2"
* "calc= 'a 'b [] drop" -> "calc=a b"
It removes the top item of the stack.
`,
drop_n: aliases => `
DROP N ITEMS
${built_in_warning}
Usage: "calc= n ... m num dropn", where "num" is the amount of items you want to drop and "n ... m" are the items.
Aliases: ${aliases}.
Examples:
* "calc= 'a 'b 'c 'd 2 dropn" -> "calc=a b"
* "calc= 10 1 .. expl 5 dropn" -> "calc=10 9 8 7 6"
It drops the top num items of the stack. It should be called with a known, fixed number or you could remove the wrong items, which would cause very strange bugs.
`,
rot: aliases => `
ROTATE
Usage: "calc= a b c rot", where "a", "b", and "c" can be any value.
Aliases: ${aliases}.
Examples:
* "calc= 'a 'b 'c rot" -> "calc=c a b"
* "calc= "as" [] 1 rot" -> "calc=1 as []"
It rotates the top three items of the stack to the right. To do this with more items you would use roll, but it is not recommended.
`,
unrot: aliases => `
ROTATE BACKWARD
Usage: "calc= a b c unrot", where "a", "b", and "c" can be any value.
Aliases: ${aliases}.
Examples:
* "calc= 'a 'b 'c unrot" -> "calc=b c a"
* "calc= "as" [] 1 unrot" -> "calc=[] 1 as"
It rotates the top three items of the stack to the left. To do this with more items you would use unroll, but it is not recommended.
`,
roll: aliases => `
ROLL
${built_in_warning}
Usage: "calc= n ... m num roll", where "num" is the amount of items you want to roll and "n ... m" are the items.
Aliases: ${aliases}.
Examples:
* "calc= 'a 'b 'c 'd 'e 'f 4 roll" -> "calc=a b f c d e"
* "calc= 1 5 .. {n -> 1 n .. expl n roll n group} map" -> "calc=[[1], [2, 1], [3, 1, 2], [4, 1, 2, 3], [5, 1, 2, 3, 4]]"
It rotates the top num items of the stack to the left. If you want to roll 2 or 3 three items, please check out swap and rot.
`,
unroll: aliases => `
ROLL BACKWARD
${built_in_warning}
Usage: "calc= n ... m num unroll", where "num" is the amount of items you want to roll backward and "n ... m" are the items.
Aliases: ${aliases}.
Examples:
* "calc= 'a 'b 'c 'd 'e 'f 4 unroll" -> "calc=a b d e f c"
* "calc= 1 5 .. {n -> 1 n .. expl n unroll n group} map" -> "calc=[[1], [2, 1], [2, 3, 1], [2, 3, 4, 1], [2, 3, 4, 5, 1]]"
It rotates the top num items of the stack to the right. If you want to roll backward 2 or 3 three items, please check out swap and unrot.
`,
nip: aliases => `
NIP UNDER
Usage: "calc= a b nip", where "a" and "b" can be any value.
Aliases: ${aliases}.
Examples:
* "calc= 'a 'b 'c nip" -> "calc=a c"
* "calc= 6 1 78 2 nip" -> "calc=6 1 2"
It removes the item under the top one. Equivalent to "swap drop".
`,
tuck: aliases => `
TUCK UNDER
Usage: "calc= a b nip", where "a" and "b" can be any value.
Aliases: ${aliases}.
Examples:
* "calc= 'a 'b 'c tuck" -> "calc=a c b c"
* "calc= 1 [2] tuck" -> "calc=[2] 1 [2]"
It puts the top item of the stack underneath the next. Equivalent to "dup rot" or "swap over".
`,
over: aliases => `
DUPLICATE OVER
Usage: "calc= a b over", where "a" and "b" can be any value.
Aliases: ${aliases}.
Examples:
* "calc= 'a 'b over" -> "calc=a b a"
* "calc= 4 98 2 over" -> "calc=4 98 2 98"
It puts a duplicate of the second item from the top of the stack on top of the stack. Equivalent to "swap dup rot" or "swap tuck".
`,
map: aliases => `
MAP OVER LIST
Usage: "calc= list func map", where "list" is a list and "func" is any function.
Aliases: ${aliases}.
Examples:
* "calc= 3 6 .. {1+} map" -> "calc=[4, 5, 6, 7]"
* "calc= [true, false, true] $! map" -> "calc=[0, 1, 0]"
It applies the function to each element in the list. The function should take one argument and return one value, or else bugs might ensue.
`,
fold: aliases => `
FOLD LIST TO THE LEFT
Usage: "calc= list starting reducer fold", where "list" is a list, "starting" is the starting value for the folding, and "reducer" is the function for folding the list.
Aliases: ${aliases}.
Examples:
* "calc= 1 5 .. 1 $* fold" -> "calc=120"
* "calc= ["foo", "bar", "baz"] "" {", " + +} fold" -> "calc=foo, bar, baz, "
It applies the function to the last result of the function (the accumulator) to the next element of the list. The first accumulator is the "starting" argument.
Example explanation: "calc= 1 5 .. 1 $* fold".
* [1, 2, 3, 4, 5] The list.
* 1 The starting accumulator.
* $* The reducer.
The argument on the left is the accumulator and the one on the right is the next element in the list.
1 * 1 -> 1
1 * 2 -> 2
2 * 3 -> 6
6 * 4 -> 24
24 * 5 -> 120
Then 120 is returned.
`,
foldr: aliases => `
FOLD LIST TO THE RIGHT
Usage: "calc= list starting reducer foldr", where "list" is a list, "starting" is the starting value for the folding, and "reducer" is the function for folding the list.
Aliases: ${aliases}.
Examples:
* "calc= 1 5 .. 1 $* foldr" -> "calc=120"
* "calc= ["foo", "bar", "baz"] "" {", " + +} foldr" -> "calc=baz, bar, foo, "
It applies the function to the last result of the function (the accumulator) to the next element of the list, starting from the end. The first accumulator is the "starting" argument.
Example explanation: "calc= 1 5 .. 1 $* foldr".
* [1, 2, 3, 4, 5] The list.
* 1 The starting accumulator.
* $* The reducer.
The argument on the left is the accumulator and the one on the right is the next element in the list.
1 * 5 -> 5
5 * 4 -> 20
20 * 3 -> 60
60 * 2 -> 120
120 * 1 -> 120
Then 120 is returned.
`,
filter: aliases => `
FILTER LIST WITH PREDICATE
Usage: "calc= list pred filter", where "list" is a list and "pred" is any predicate (function that returns a boolean).
Aliases: ${aliases}.
Examples:
* "calc= -5 5 .. {2 >} filter" -> "calc=[3, 4, 5]"
* "calc= [-3 3 .., 1 2 .., -5 -3 .., [0, 3], [5, 1]] {0 elem !} filter" -> "calc=[[1, 2], [-5, -4, -3], [5, 1]]"
* "calc= ["", "as", [], [1], 0, 1, {}, {+}, $0] {} filter" -> "calc=[as, [1], 1, { -> <function definition>}, { -> <function definition>}, 0]"
It calls the predicate on each element in the list, keeping only the elements that return truthy values.
`,
length: aliases => `
LENGTH OF
Usage: "calc= list_like length", where "list_like" is a list or a string (like a list of characters).
Aliases: ${aliases}.
Examples:
* "calc= [0, 1, 2] length" -> "calc=3"
* "calc= "dbfj" length" -> "calc=4"
It returns the length of the list_like.
`,
head: aliases => `
FIRST ITEM
Usage: "calc= list head", where "list" is a list.
Aliases: ${aliases}.
Examples:
* "calc= [6, 2, 7] head" -> "calc=6"
* "calc= 6 3 .. head" -> "calc=6"
It returns the first item of the list.
`,
snd: aliases => `
SECOND ITEM
Usage: "calc= list snd", where "list" is a list.
Aliases: ${aliases}.
Examples:
* "calc= [6, 2, 7] snd" -> "calc=2"
* "calc= 6 3 .. snd" -> "calc=5"
It returns the second item of the list.
`,
last: aliases => `
LAST ITEM
Usage: "calc= list last", where "list" is a list.
Aliases: ${aliases}.
Examples:
* "calc= [6, 2, 7] last" -> "calc=7"
* "calc= 6 3 .. last" -> "calc=3"
It returns the last item of the list.
`,
back_snd: aliases => `
BEFORE LAST ITEM
Usage: "calc= list back_snd", where "list" is a list.
Aliases: ${aliases}.
Examples:
* "calc= [6, 2, 7] back_snd" -> "calc=2"
* "calc= 6 3 .. back_snd" -> "calc=4"
It returns the before last item of the list.
`,
nth: aliases => `
N-TH ITEM
Usage: "calc= list idx nth", where "idx" is an index into the list and "list" is a list.
Aliases: ${aliases}.
Examples:
* "calc= [866, 346, 987, 43] 2 nth" -> "calc=987"
* "calc= 3 8 .. 3 nth" -> "calc=6"
It returns the n-th item of the list, where idx is that n. The list is 0 indexed, meaning the first item is at index 0.
`,
back_nth: aliases => `
N-TH ITEM FROM THE END
Usage: "calc= list idx back_nth", where "idx" is an index into the list and "list" is a list.
Aliases: ${aliases}.
Examples:
* "calc= [866, 346, 987, 43] 2 back_nth" -> "calc=346"
* "calc= 3 8 .. 3 back_nth" -> "calc=5"
It returns the n-th item from the back of the list, where idx is that n. The list is 0 indexed, meaning the last item is at index 0.
`,
init: aliases => `
ALL BUT FIRST ITEM
Usage: "calc= list init", where "list" is a list.
Aliases: ${aliases}.
Examples:
* "calc= [6, 2, 7] init" -> "calc=[6, 2]"
* "calc= 6 3 .. init" -> "calc=[6, 5, 4]"
It returns all but the first item of the list.
`,
tail: aliases => `
ALL BUT LAST ITEM
Usage: "calc= list tail", where "list" is a list.
Aliases: ${aliases}.
Examples:
* "calc= [6, 2, 7] tail" -> "calc=[2, 7]"
* "calc= 6 3 .. tail" -> "calc=[5, 4, 3]"
It returns all but the last item of the list.
`,
body: aliases => `
ALL BUT FIRST AND LAST ITEM
Usage: "calc= list body", where "list" is a list.
Aliases: ${aliases}.
Examples:
* "calc= [6, 2, 7] body" -> "calc=[2]"
* "calc= 6 3 .. body" -> "calc=[5, 4]"
It returns all but the first and last item of the list.
`,
reverse: aliases => `
REVERSE LIST
Usage: "calc= list reverse", where "list" is a list.
Aliases: ${aliases}.
Examples:
* "calc= 4 9 .. reverse" -> "calc=[9, 8, 7, 6, 5, 4]"
* "calc= [7, 34, $-, "as", [], {}] reverse" -> "calc=[{ -> <function definition>}, [], as, -, 34, 7]"
It reverses the list.
`,
reverse_n: aliases => `
REVERSE LAST N ITEMS OF LIST
Usage: "calc= list num reverse", where "list" is a list and "num" is an index into the list.
Aliases: ${aliases}.
Examples:
* "calc= 1 6 .. 4 reverse_n" -> "calc=[1, 2, 6, 5, 4, 3]"
* "calc= [7, 34, $-, "as", [], {}] 3 reverse_n" -> "calc=[7, 34, -, { -> <function definition>}, [], as]"
It reverses only the last num items of the list.
`,
pop_n: aliases => `
REMOVE LAST N ITEMS OF LIST
Usage: "calc= list num pop_n", where "list" is a list and "num" is an index into the list.
Aliases: ${aliases}.
Examples:
* "calc= 1 6 .. 4 pop_n" -> "calc=[1, 2]"
* "calc= [7, 34, $-, "as", [], {}] 3 pop_n" -> "calc=[7, 34, -]"
It removes the last num items of the list.
`,
elem: aliases => `
IS ITEM IN LIST
Usage: "calc= list item elem", where "list" is the list and "item" is the item to test for.
Aliases: ${aliases}.
Examples:
* "calc= [7, {}, 4, 23, 'a, $+, 4, []] 4 elem" -> "calc=1"
* "calc= [7, {}, 23, 'a, $+, []] 4 elem" -> "calc=0"
* "calc= [-3 3 .., 1 2 .., -5 -3 .., [0, 3], [5, 1]] {0 elem !} filter" -> "calc=[[1, 2], [-5, -4, -3], [5, 1]]"
It returns true if the item is in the list, else it returns false.
`,
join: aliases => `
JOIN LIST INTO STRING
Usage: "calc= list joiner join", where "list" is a list of strings and "joiner" is the string to put in between each item.
Aliases: ${aliases}.
Examples:
* "calc= ['a, 'b, 'c] ': join" -> "calc=a:b:c"
* "calc= ["as", "fdg", "bb"] "" join" -> "calc=asfdgbb"
It returns the list as a string with the joiner in between each string in the list.
`,
split: aliases => `
SPLIT STRING INTO LIST
Usage: "calc= string splitter split", where "string" is a string and "splitter" is a string used to split the other.
Aliases: ${aliases}.
Examples:
* "calc= "1,2,3,4" "," split" -> "calc=[1, 2, 3, 4]"
* "calc= "iiodddoisoisoiso" "" split" -> "calc=[i, i, o, d, d, d, o, i, s, o, i, s, o, i, s, o]"
It returns the string split into a list where it matches the splitter.
`,
expl: aliases => `
EXPLODE LIST ITEMS
${built_in_warning}
Usage: "calc= list expl", where "list" is a list.