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
|
Global functions
================
Many of the following functions can also be meaningfully applied to scopes. This would add nothing to understand though.
Hence, these effects are not discussed here, but in [execution.md](execution.md).
`/`
---
This is the identity function.
1 / dump
0000000000000001
`|`
---
Takes a name from the stack and resolves it. Disregarding possible execution modes, the
resolved object is never executed.
"add" | dump
<function: 00006000001019B0>
|add dump
<function: 00006000001019B0>
`?`
---
Implements the usual ternary operator.
1 ==yesno
# 0 =yesno
yesno "Yesno was non-zero" "Yesno was zero" ? dump
This is particularly useful with function objects.
yesno { "Now handling yes-case" dump }
{ "Now handling no-case" dump } ? *
`=`
---
Takes a name and a value from the stack. Assigns a new value to the name (which must
be defined before).
0 ==i
1 =i
`*`
---
Executes the top stack element. See [execution.md](execution.md) for the full story.
{ "Hello World!" dump } *
"Hello World!"
`|=`
----
Takes a name and a function from the stack. Resolves the name without
execution (like `|`) runs the function and then assigns the topmost
stack element back to name (like `=`).
0 ==long_variable_name
{ 1 add } |=long_variable_name
long_variable_name dump # now 1
`[`
---
Puts an array-begin marker on the stack.
`]`
---
Scans the stack for the topmost array-begin marker and constructs an array containing all
objects between the marker and the stack top.
[ /a /b /c ] dump
[
"a"
"b"
"c"
]
Note that the stack marker can be moved around with `-` like all other objects.
/a [ -01 ] dump
[
"a"
]
`=[]`
-----
Assigns an array or string cell. It takes the array/string, the target index and the new value
from the stack.
[ /a /b /c ] ==arr
/X 1 arr =[]
arr dump
[
"a"
"X"
"c"
]
"abc" ==s
s dump
"abc"
64 1 s =[]
s dump
"a@c"
`len`
-----
Takes an array or string from the stack and returns its length.
"foo" len dump
0000000000000003
`cat`
-----
Takes two arrays or strings from the stack and concatenates them.
"foo" "bar" cat dump
"foobar"
`dearray`
---------
Takes a length and an array from the stack and puts array indices from 0 to length (exclusive) on the stack.
During dereference, the array index is taken modulo the actual array length.
[ /a /b /c ] _ len dearray
dump dump dump
"c"
"b"
"a"
[ 1 0 ] 4 dearray
dump dump dump dump
0000000000000000
0000000000000001
0000000000000000
0000000000000001
`range`
-------
Takes a end and a start value from the stack and creates an array containing all integers
from start to end (exclusive).
0 4 range dump
[
0000000000000000
0000000000000001
0000000000000002
0000000000000003
]
`<`
---
Switches to a new scope object which has the previous current scope as parent scope.
`>`
---
Pushes the current scope to the stack and switches the current scope to its parent.
<
0 ==i
> .i dump
0000000000000000
`>'`
----
Takes a scope object *p* from the stack. Pushes the current scope *s* onto the stack.
Sets the current scope to the parent of *s* and afterwards sets the parent of *s* to *p*.
See [scopes.md](scopes.md) for an example.
`scope`
-------
Pushes the current scope to the stack.
<
0 ==i
scope keys dump > --
[
"i"
]
`die`
-----
Takes a string from the stack and outputs it to the standard error stream. Afterwards terminates the program.
"Oops" die
`quoted`
--------
Returns the current quoting level of the parser. This is always 0 unless somehow invoked during evaluation of
some defq'ed code.
{ quoted dump } /q defq
q
0000000000000000
{ q } --
0000000000000001
{ { q } } --
0000000000000002
{ quoted dump } /p deff
p
0000000000000000
{ p } --
{ { p } } --
{ p } *
0000000000000000
{ { p } } *
dump
<function: 00006000001B6740>
`def`-x family, `==?`, `=*?`, `==`, `=*`, `==:`, `=*:`
------------------------------------------------------
Takes a name and a value from the stack and maps the name to the value in the current scope. Depending on the
suffix of def, various execution and optimization modes are associated with the name. See [scopes.md](scopes.md) for details.
`''`
----
Takes a new output type, a new input type and a function object from the stack and returns a new function object
with the specified input and output types, executing the original function object. See [execution.md](execution.md) about types.
[ /a /ab /abc /abcd ] len dump
0000000000000003
[ /a /ab /abc /abcd ] |len [ 0 ] [ 0 ] '' * dump
[
0000000000000001
0000000000000002
0000000000000003
0000000000000004
]
`'`
---
Like `''`, but instead of full specifications, just takes a string consisting of digits, a `.` and more
digits. The earlier digits specify scalar input types, the later digits specify scalar output types.
[ /a /ab /abc /abcd ] len dump
0000000000000003
[ /a /ab /abc /abcd ] |len '0.0 * dump
[
0000000000000001
0000000000000002
0000000000000003
0000000000000004
]
`'*`
----
A shortcut for `'<string> *`.
`;`
---
Concatenate two functions, i.e. take functions *g* and *f* from the stack and return a new function object which
executes *f* and then executes *g*.
{ 1 add } { 2 mul } ; /f deff
3 f dump
0000000000000008
`--`
----
Drops the top stack element.
1 2 -- dump
0000000000000001
`_`
---
Duplicates the top stack element.
1 _ dump dump
0000000000000001
0000000000000001
`-`
---
Takes a string from the stack. This string may consist of digits and the `*` character. This string defines a stack
shuffling as follows. The highest digit plus one is the number of stack elements shuffled. The digits denote stack
elements, counting from top to bottom, i.e. `0` is stack top, `1` is next below `0` and so on. First all relevant
elements are removed from the stack, then the string is evaluated in order. Each digit pushes the respective element
back onto the stack, each `*` invokes the `*` function once.
[ /a /b /c -1001 ] dump
[
"a"
"b"
"c"
"c"
"b"
]
1 2 { 3 mul } -10*20* dump
0000000000000003
0000000000000006
`eq`
----
Takes two ints or strings from the stack. Returns one if they are equal, zero otherwise.
`neq`
-----
Takes two ints or strings from the stack. Returns one if they are non-equal, zero if they are.
`add`
-----
Adds two integers or floats.
`sub`
-----
Substracts two integers or floats.
5 3 sub dump
0000000000000002
`band`
------
Binary and between two integers.
`bor`
-----
Binary or between two integers.
`bxor`
------
Binary xor between two integers.
`gt`
----
Compares two integers or floats. If the first is greater than the second, return one, otherwise zero.
4 5 gt dump
0000000000000000
5 5 gt dump
0000000000000000
6 5 gt dump
0000000000000001
`ge`
----
Compares two integers or floats. If the first is greater than or equal to the second, return one, otherwise zero.
4 5 gt dump
0000000000000000
5 5 gt dump
0000000000000001
6 5 gt dump
0000000000000001
`lt`
----
Compares two integers or floats. If the first is less than the second, return one, otherwise zero.
4 5 gt dump
0000000000000001
5 5 gt dump
0000000000000000
6 5 gt dump
0000000000000000
`le`
----
Compares two integers or floats. If the first is less than or equal to the second, return one, otherwise zero.
4 5 gt dump
0000000000000001
5 5 gt dump
0000000000000001
6 5 gt dump
0000000000000000
`mul`
-----
Multiplies two integers or floats.
2 3 mul dump
0000000000000006
2.0 3.0 mul dump
+5.9999999999e0
`div`
-----
Divides two (signed) integers or floats. The integer version truncates the result towards zero.
5 2 div dump
0000000000000002
5 2 neg div dump
-0000000000000002
`mod`
-----
Computes the remainder of a signed integer division. The result is always positive.
5 2 mod dump
0000000000000001
5 neg 2 mod dump
0000000000000001
`udiv`
-----
Divides two unsigned integers. Truncates the result towards zero. Use this only
if you need full 64bit unsigned integer arithmetic.
5 2 udiv dump
0000000000000002
5 2 neg udiv dump
0000000000000000
5 neg 2 udiv dump
7FFFFFFFFFFFFFFD
`umod`
-----
Computes the remainder of an unsigned integer division. The result is always
positive. Use this only if you need full 64bit unsigned integer arithmetic.
5 2 umod dump
0000000000000001
5 neg 3 mod dump
0000000000000001
5 neg 3 umod dump
0000000000000002
`and`
-----
Logical and of two integers, i.e. one if both are non-zero, zero otherwise.
`or`
----
Logical or of two integers, i.e. one if any is non-zero, zero otherwise.
`xor`
-----
Logical xor of two integers, i.e. one if exactly one is non-zero, zero otherwise.
`bnot`
------
Bitwise integer negation.
`neg`
-----
Numerical integer negation.
5 3 neg add dump
0000000000000002
`not`
-----
Logical integer negation, i.e. one if zero previously, zero otherwise.
`rep`
-----
Takes a function and a count from the stack. Executes the function as many times as specified.
3 { /a dump } rep
"a"
"a"
"a"
`loop`
------
Takes a function object *b* and a function object *p* from the stack. Then it executes *p* and takes
one integer from the stack. If this integer is non-zero, executes *b* and then restarts with executing *p*.
This repeats until *p* returns zero.
0 ==i
{ i 3 lt } {
i dump
i 1 add =i
} loop
0000000000000000
0000000000000001
0000000000000002
0 { _ 3 lt } { _ dump 1 add } loop
0000000000000000
0000000000000001
0000000000000002
`each`
------
Takes a function object and an array or string from the stack. For each element of the array or string, pushes
that element, then executes the function.
[ /a /b /c ] |dump each
"a"
"b"
"c"
`.`
---
Takes a name and a scope from the stack. Resolves the name in the scope.
< 5 ==i > .i dump
0000000000000005
`.|`
----
Takes a name and a scope from the stack. Resolves the name in the scope, but never executes it.
<
1 ==i
{ 2 } =*j
> ==s
s .i dump
0000000000000001
s .j dump
0000000000000002
s .|i dump
0000000000000001
s .|j dump
<function: 00006000008E5700>
`.?`
----
Takes a name and a scope from the stack. If the name can be resolved in the scope, returns 1, otherwise returns 0.
< 0 ==i > ==s
s .?i dump
0000000000000001
s .?j dump
0000000000000000
`.?'`
-----
Takes a name and a scope from the stack. If the name can be resolved in the scope without its parent,
returns 1, otherwise returns 0.
< 0 ==i > ==s
s .?i dump
0000000000000001
s .?'i dump
0000000000000001
s .?s dump
0000000000000001
s .?'s dump
0000000000000000
`keys`
------
Takes a scope object from the stack. Returns an array of the names defined directly in the scope.
<
0 ==i
1 ==j
> keys dump
[
"i"
"j"
]
`dom`
-----
Takes an array or string from the stack, determines its length and returns an array containing
the integers from zero to length minus one. If the array or string would be a partial functions from
integers to elements, this is the domain of this function.
"foo" dom dump
[
0000000000000000
0000000000000001
0000000000000002
]
`!!`
----
Takes a function object and creates a coroutine which resumes execution at the start of this function object.
The call and data stack of this new coroutine are initially empty. See [coroutines.md](coroutines.md) for details and examples.
`!!'`
-----
Takes a function object *c* and a function object *m*. Creates a new coroutine which resumes execution at the start
of *c*. The call stack and data stack of this coroutine are copied from the current stack (after *c* and *m* have been
removed). Afterwards push the coroutine object and execute *m*. See [coroutines.md](coroutines.md) for details and examples.
`!`
---
Takes a count and a target coroutine from the stack. Moves as many elements as specified from the current
stack to the stack of the specified coroutine. Additionally, push the current coroutine to the target coroutine's
stack. Then continue execution in the specified coroutine. See [coroutines.md](coroutines.md) for details and examples.
`{`
---
Increases the parser quote level by one and pushes a quote begin marker onto the stack.
`}`, `}'`, `}"`, `}_`
---------------------
Decreases the parser quote level by one. Searches for the topmost quote begin marker on the stack and collects
all stack elements above it into a function object. See [quoting.md](quoting.md) for details.
`regex`
-------
Takes a regular expression and a string from the stack. Matches the regular expression against the string.
If unsuccessful, returns a zero. If successful, pushes all subgroups in onto the stack, starting with the last.
Finally, pushes a one onto the stack.
"The quick brown ..." "( ...).*(br.wn)" regex dump dump dump
0000000000000001
" qui"
"brown"
"The quick brown ..." "xxx" regex dump
0000000000000000
"The quick brown ..." { "(..)(.*)" regex } |dump loop
"Th"
"e "
"qu"
"ic"
"k "
"br"
"ow"
"n "
".."
`globals`
---------
Returns an array containing all names defined in the global scope.
`curry`
-------
Takes a typed function and curry it, i.e. return a function object which takes the last argument and returns a function
object which takes the second-to-last argument and returns a function object ... up to the final function object which
takes the first argument, executes the original function on all arguments thus acquired and returns the result.
|sub curry ==a
a dump
<function: 00006000008379E0>
2 a * ==b
b dump
<function: 0000600000832400>
5 b * ==c
c dump
0000000000000003
`||`
----
First `|`, then `curry`. Resolves a name in the current scope and immediately curries the result.
`**`
----
Takes an object from the stack. If it is a function or an array, execute it (see [execution.md](execution.md) for details).
Then execute `**` again on the result.
5 2 ||sub ** dump
0000000000000003
`dump`
------
Takes an object from the stack and outputs some representation of it to the standard error stream.
`include`
---------
Takes a filename from the stack. Executes the content of the file as code in the current scope.
`via`
-----
Takes a name and a scope *s* from the stack. Constructs a new function which takes a string from the stack
and resolves it in *s*. `deffst`s this new function object to the name.
<
5 ==i
{ "hi" dump } =*greet
> ":" via
:i dump
0000000000000005
:greet
"hi"
`fold`
------
Takes a function object and an array from the stack. Pushes the first array element. For each remaining element,
this element is first pushed and the function object then executed.
[ /foo /bar /quux ] |cat fold dump
"foobarquux"
`reverse`
---------
Reverses an array.
[ /a /b /c ] reverse dump
[
"c"
"b"
"a"
]
`any`
-----
Takes an array. Returns one if any of its elements are non-zero.
/b ==s
[ /a /b /c ] s eq dump
[
0000000000000000
0000000000000001
0000000000000000
]
[ /a /b /c ] s eq any dump
0000000000000001
`all`
-----
Takes an array. Returns one if all of its elements are non-zero.
[ 1 2 3 4 ] 5 lt all dump
0000000000000001
`grep`
------
Takes a function object *p* and an array from the stack. Each element of the array in turn is pushed and *p* invoked.
Returns a new array consisting of those elements for which *p* returned non-zero.
[ 1 2 3 4 5 6 ] { 2 mod } grep dump
[
0000000000000001
0000000000000003
0000000000000005
]
`indices`
---------
Takes a function object *p* and an array from the stack. Each element of the array in turn is pushed and *p* invoked.
Returns a new array consisting of those element indices for which *p* returned non-zero.
[ /a /b /b /a /c ] { /a eq } indices dump
[
0000000000000000
0000000000000003
]
`index`
-------
Takes a function object *p* and an array from the stack. Starting from the start of the array, each element is pushed
and *p* invoked. Returns the lowest array index for which *p* returns non-zero, or -1 if no such index exists.
[ /b /a /b /b /a ] { /a eq } index dump
0000000000000001
`assert`
--------
Takes the top stack element. Terminates the program if it is zero.
`values`
--------
Takes a scope object. Returns an array consisting of the values of the scope's members.
`conds`
-------
Takes an array of function objects. Evaluates the objects at even indices. For the first of them returning non-zero,
execute the function object after it.
{ [
{ _ [ 5 3 ] mod not all } { "FizzBuzz" dump }
{ _ 3 mod not } { "Fizz" dump }
{ _ 5 mod not } { "Buzz" dump }
{ 1 } { dump }
] conds } ==f
1 18 range f each
0000000000000001
0000000000000002
"Fizz"
0000000000000004
"Buzz"
"Fizz"
0000000000000007
0000000000000008
"Fizz"
"Buzz"
000000000000000B
"Fizz"
000000000000000D
000000000000000E
"FizzBuzz"
0000000000000010
0000000000000011
`max`
-----
Takes two integers. Returns the larger one.
`min`
-----
Takes two integers. Returns the smaller one.
`,`
---
Puts a position marker on the stack.
`,--`
-----
Deletes the topmost position marker within the stack and compacts the stack contents to cover the hole.
`--,`
-----
Deletes whatever is below the topmost position marker in the stack and compacts the stack contents to cover the hole.
`_,`
----
Copies the stack element below the topmost position marker to the top of the stack.
`,_`
----
Copies the stack element above the topmost position marker to the top of the stack.
`,---`
------
Deletes all stack contents from the top to (and including) the topmost position marker.
|