spitewaste 0.1.010 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +7 -6
- data/lib/spitewaste.rb +4 -0
- data/lib/spitewaste/assembler.rb +1 -1
- data/lib/spitewaste/emitters/image.rb +1 -1
- data/lib/spitewaste/libspw/array.spw +118 -19
- data/lib/spitewaste/libspw/bits.spw +9 -5
- data/lib/spitewaste/libspw/docs.json +6279 -1
- data/lib/spitewaste/libspw/fun.spw +56 -4
- data/lib/spitewaste/libspw/io.spw +13 -11
- data/lib/spitewaste/libspw/math.spw +7 -11
- data/lib/spitewaste/libspw/random.spw +33 -6
- data/lib/spitewaste/libspw/rational.spw +30 -1
- data/lib/spitewaste/libspw/stack.spw +22 -13
- data/lib/spitewaste/libspw/string.spw +79 -26
- data/lib/spitewaste/libspw/terminal.spw +14 -0
- data/lib/spitewaste/libspw/test.spw +3 -0
- data/lib/spitewaste/libspw/util.spw +97 -21
- data/lib/spitewaste/parsers/fucktional.rb +7 -3
- data/lib/spitewaste/parsers/spitewaste.rb +21 -9
- data/lib/spitewaste/version.rb +1 -1
- metadata +3 -2
@@ -36,6 +36,12 @@ _isop_resume: sub add swap push 128 div jump _isop_loop
|
|
36
36
|
_isop_no: dup jump _isop_resume
|
37
37
|
_isop_done: pop ret
|
38
38
|
|
39
|
+
$_do_collatz() {
|
40
|
+
dup push 2 mod
|
41
|
+
swap copy 1 push 2 mul $++ mul
|
42
|
+
push 2 copy 2 sub div add
|
43
|
+
}
|
44
|
+
|
39
45
|
; returns the elements of the Collatz sequence for integer N as a pseudo-array
|
40
46
|
; ! may run forever on some as-yet-unknown input
|
41
47
|
; [N] => [A]
|
@@ -48,11 +54,57 @@ _isop_done: pop ret
|
|
48
54
|
collatz: push 1 ; sequence length
|
49
55
|
_collatz_loop:
|
50
56
|
copy 1 dup push 1 sub jz _collatz_done
|
51
|
-
|
52
|
-
swap copy 1 push 2 mul $++ mul
|
53
|
-
push 2 copy 2 sub div add
|
54
|
-
swap $++ jump _collatz_loop
|
57
|
+
$_do_collatz() swap $++ jump _collatz_loop
|
55
58
|
_collatz_done: pop ret
|
56
59
|
|
60
|
+
; returns the length L of the Collatz sequence for integer N
|
61
|
+
; ! may run forever on some as-yet-unknown input
|
62
|
+
; [N] => [L]
|
63
|
+
;
|
64
|
+
; [1] => [1]
|
65
|
+
; [4] => [3]
|
66
|
+
; [7] => [17]
|
67
|
+
; [189] => [107]
|
68
|
+
collatz_len: push 1 swap
|
69
|
+
_collatz_len_loop:
|
70
|
+
dup $-- jz _collatz_done
|
71
|
+
$_do_collatz() swap $++ swap jump _collatz_len_loop
|
72
|
+
copy 1 dup push 1 sub jz _collatz_done
|
73
|
+
|
57
74
|
; ruby:
|
58
75
|
; push "'" :strcat push "ruby -e 'p " swap :strcat shell ret
|
76
|
+
|
77
|
+
$_to_roman(r, v) {
|
78
|
+
push `v` :divmod swap push `r` swap :strrep
|
79
|
+
@-2 swap :strcat ^-2
|
80
|
+
}
|
81
|
+
|
82
|
+
; converts the number N to a string of roman numerals R
|
83
|
+
; [N] => [R]
|
84
|
+
;
|
85
|
+
; [2020] => ["MMXX"]
|
86
|
+
; [1666] => ["MDCLXVI"]
|
87
|
+
; [1337] => ["MCCCXXXVII"]
|
88
|
+
; [94] => ["XCIV"]
|
89
|
+
to_roman: push -2,0 store
|
90
|
+
$_to_roman("M", 1000) $_to_roman("CM", 900)
|
91
|
+
$_to_roman("D", 500) $_to_roman("CD", 400)
|
92
|
+
$_to_roman("C", 100) $_to_roman("XC", 90)
|
93
|
+
$_to_roman("L", 50) $_to_roman("XL", 40)
|
94
|
+
$_to_roman("X", 10) $_to_roman("IX", 9)
|
95
|
+
$_to_roman("V", 5) $_to_roman("IV", 4)
|
96
|
+
$_to_roman("I", 1) push 2 sub load ret
|
97
|
+
|
98
|
+
; applies the ROT13 "cipher" to the string S
|
99
|
+
; [S] => [S']
|
100
|
+
;
|
101
|
+
; ["gnat"] => ["tang"]
|
102
|
+
; ["purely"] => ["cheryl"]
|
103
|
+
; ["cat.PNG"] => ["png.CAT"]
|
104
|
+
; ["Hello, world."] => ["Uryyb, jbeyq."]
|
105
|
+
; ["a123z"] => ["n123m"]
|
106
|
+
ROT13:
|
107
|
+
push "AZ" :strexpand push "az" :strexpand :strcat
|
108
|
+
push "NZ" :strexpand push "AM" :strexpand
|
109
|
+
push "nz" :strexpand push "am" :strexpand
|
110
|
+
:strcat :strcat :strcat :strtrans ret
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import string ; strcat, strpack, strrev, strunpack
|
2
2
|
|
3
3
|
; prints the character at the top of the stack until terminating zero
|
4
|
+
; [0 ... C] => []
|
4
5
|
print: :strunpack
|
5
6
|
_print_loop:
|
6
7
|
dup jz _print_done
|
@@ -8,34 +9,35 @@ _print_loop:
|
|
8
9
|
_print_done: pop ret
|
9
10
|
|
10
11
|
; print with newline
|
12
|
+
; [0 ... C] => []
|
11
13
|
println: :print push 10 ochr ret
|
12
14
|
|
13
|
-
;;;
|
14
|
-
|
15
15
|
; reads a line of input onto the top of the stack as a packed string
|
16
16
|
; ! clobbers heap address -1
|
17
|
+
; [] => [L]
|
17
18
|
getline: push 0 ; terminator for strpack
|
18
|
-
|
19
|
-
; read characters onto the stack until newline (10) or EOF (-1)
|
20
19
|
_getline_loop:
|
21
20
|
push -1 dup ichr load
|
22
21
|
dup jn _getline_eof
|
23
22
|
dup push 10 sub jz _getline_done
|
24
23
|
jump _getline_loop
|
25
|
-
|
26
24
|
_getline_eof: pop
|
27
25
|
_getline_done: :strpack :strrev ret
|
28
26
|
|
29
|
-
|
30
|
-
|
27
|
+
; displays the string S then reads a line of input
|
28
|
+
; [S] => [L]
|
31
29
|
prompt: :print :getline ret
|
32
30
|
|
33
|
-
|
34
|
-
|
35
|
-
; consume stdin until EOF
|
31
|
+
; consume stdin until EOF into the string S
|
32
|
+
; [] => [S]
|
36
33
|
readall: push 0 ; accumulated string
|
37
34
|
_readall_loop:
|
38
35
|
:getline dup jz _readall_done
|
39
36
|
:strcat jump _readall_loop
|
40
|
-
|
41
37
|
_readall_done: pop ret
|
38
|
+
|
39
|
+
; returns the contents C of the file at path P (a string)
|
40
|
+
; NON-STANDARD! This function makes use of the `shell` instruction, which is
|
41
|
+
; only(?) available in the Spiceweight Whitespace interpreter.
|
42
|
+
; [P] => [C]
|
43
|
+
readfile: push "cat " swap :strcat shell ret
|
@@ -1,8 +1,7 @@
|
|
1
1
|
import array ; arycat, arydup
|
2
2
|
import util ; eq, inc, range
|
3
3
|
|
4
|
-
; returns B raised to the power E
|
5
|
-
; ! TODO: support negative exponents (return Rational)
|
4
|
+
; returns B raised to the power E; if E must be negative, use ratpow instead
|
6
5
|
; [B E] => [B**E]
|
7
6
|
;
|
8
7
|
; [0 0] => [1], [0 9] => [0], [9 0] => [1]
|
@@ -52,7 +51,7 @@ ilog: push -1,0 store ; accumulator at -1
|
|
52
51
|
_ilog_loop: ; [n b]
|
53
52
|
swap copy 1 div dup jz _ilog_done
|
54
53
|
push -1 :inc swap jump _ilog_loop
|
55
|
-
_ilog_done:
|
54
|
+
_ilog_done: @-1 slide 2 ret
|
56
55
|
|
57
56
|
; returns the greatest common divisor of A and B
|
58
57
|
; [A B] => [gcd(A, B)]
|
@@ -112,10 +111,7 @@ abs: dup :sign mul ret
|
|
112
111
|
; [42 6] => [7 0]
|
113
112
|
; [ 1 5] => [0 1]
|
114
113
|
; ! [9 0] => [!!] TODO: find a way to expect exceptions
|
115
|
-
divmod:
|
116
|
-
push -1 swap store
|
117
|
-
dup push -1 load div
|
118
|
-
swap push -1 load mod ret
|
114
|
+
divmod: ^-1 dup @-1 div swap @-1 mod ret
|
119
115
|
|
120
116
|
; returns whether N is greater than 0
|
121
117
|
; [N] => [N > 0]
|
@@ -139,11 +135,11 @@ neg?: :sign push -1 :eq ret
|
|
139
135
|
; [25] => [1 5 25 3] ; no duplicate for perfect squares
|
140
136
|
; [60] => [1 2 3 4 5 6 60 30 20 15 12 10 12]
|
141
137
|
divisors: ; [n]
|
142
|
-
dup
|
138
|
+
dup ^-1 ; preserve N because array operations
|
143
139
|
:isqrt push 1 swap :range dup ; 1..isqrt(N)
|
144
|
-
reject (
|
145
|
-
map (
|
146
|
-
|
140
|
+
reject (@-1 swap mod) :arydup ; get first half of divisors
|
141
|
+
map (@-1 swap div) :arycat ; map first half to second half
|
142
|
+
@-1 copy 2 dup mul sub jz _divisors_square ret
|
147
143
|
_divisors_square: slide 1 $-- ret ; de-duplicate when N is a perfect square
|
148
144
|
|
149
145
|
; returns the number of ways to choose K elements from a set of N
|
@@ -1,12 +1,39 @@
|
|
1
|
-
import
|
1
|
+
import array ; sorted?
|
2
|
+
import math ; pow
|
3
|
+
import string ; strtoa
|
2
4
|
|
3
|
-
|
5
|
+
; seeds the random number generator with integer S
|
6
|
+
; [S] => []
|
7
|
+
srand: ^$seed ret
|
4
8
|
|
9
|
+
; returns the next number N in the linear congruential generator (better MINSTD)
|
10
|
+
; [] => [N]
|
5
11
|
rand:
|
6
12
|
push $seed dup dup load
|
7
|
-
push
|
8
|
-
push 2,
|
13
|
+
push 48271 mul
|
14
|
+
push 2,31 :pow $-- mod
|
9
15
|
store load ret
|
10
16
|
|
11
|
-
|
12
|
-
|
17
|
+
; returns a random integer I between A and B (inclusive)
|
18
|
+
; [A B] => [I]
|
19
|
+
rand_range:
|
20
|
+
$++ copy 1 sub :rand swap mod add ret
|
21
|
+
|
22
|
+
; returns an array A of N random integers between 1 and D (inclusive)
|
23
|
+
; [N D] => [A]
|
24
|
+
dice: ^-2 dup ^-1 times (push 1 @-2 :rand_range) @-1 ret
|
25
|
+
|
26
|
+
; shuffles the array A in-place using the modern Fisher-Yates algorithm
|
27
|
+
; [A] => [A']
|
28
|
+
shuffle: dup $-- ^-3
|
29
|
+
_shuffle_loop:
|
30
|
+
push 0 @-3 :rand_range @-3 :aryswap
|
31
|
+
push -3 :dec @-3 push -1 mul jn _shuffle_loop ret
|
32
|
+
|
33
|
+
; shuffles the characters of the string S, producing a random anagram
|
34
|
+
; [S] => [S']
|
35
|
+
strfry: :strtoa :shuffle pop :strpack ret
|
36
|
+
|
37
|
+
; sorts the array A if you're lucky
|
38
|
+
; [A] => [A']
|
39
|
+
bogosort: :shuffle :arydup :sorted? jz bogosort ret
|
@@ -169,8 +169,37 @@ ratinv:
|
|
169
169
|
; [R(4,2) 5] => [R(32,1)]
|
170
170
|
; [R(-3,7) 0] => [R(1,1)]
|
171
171
|
; [R(-3,14) 2] => [R(9,196)]
|
172
|
-
|
172
|
+
; [R(5,1) -1] => [R(1,5)]
|
173
|
+
; [R(2,5) -2] => [R(25,4)]
|
174
|
+
; [R(-3,14) -3] => [R(-2744,27)]
|
175
|
+
ratpow: dup jn _ratpow_neg
|
173
176
|
swap push 2 :divmod push 2 mul $--
|
174
177
|
swap $RAT :divmod copy 2 mul
|
175
178
|
copy 3 :pow swap copy 3 :pow
|
176
179
|
swap :to_r :ratsimp slide 2 ret
|
180
|
+
_ratpow_neg: push -1 mul swap :ratinv swap :ratpow ret
|
181
|
+
|
182
|
+
; converts the rational number R to a "pseudo-float" with a whole (W) part
|
183
|
+
; and a fractional (F) part composed of P digits after the decimal point
|
184
|
+
; [R P] => [W F]
|
185
|
+
;
|
186
|
+
; [R(22,7) 2] => [3 14]
|
187
|
+
; [R(355,113) 6] => [3 141592]
|
188
|
+
; [R(8675,309) 10] => [28 744336569] TODO: leading 0 is lost (bug)
|
189
|
+
; [R(2,4) 3] => [0 500]
|
190
|
+
to_f: ^-2 :from_r :divmod push 0 swap
|
191
|
+
_to_f_loop:
|
192
|
+
@-1 :divmod swap
|
193
|
+
copy 2 push 10 mul add
|
194
|
+
swap push 10 mul
|
195
|
+
push 2 :dig
|
196
|
+
push -2 dup :dec load :neg? jz _to_f_loop pop ret
|
197
|
+
|
198
|
+
; converts the rational number R to a string S of the form
|
199
|
+
; "<simplified numerator>/<simplified denominator>"
|
200
|
+
; [R] => [S]
|
201
|
+
;
|
202
|
+
; [R(22,7)] => ["22/7"]
|
203
|
+
; [R(2,4)] => ["1/2"]
|
204
|
+
; [R(99,9)] => ["11/1"]
|
205
|
+
ratstr: :ratsimp :from_r push 2 map (:to_s) push '/' :strjoinc ret
|
@@ -16,15 +16,13 @@ import util ; dec
|
|
16
16
|
; [1 2 3 4 5 3] => [1 3 4 5 2]
|
17
17
|
roll:
|
18
18
|
push -10 dup store ; current heap index kept at -10
|
19
|
-
_roll_keep:
|
19
|
+
_roll_keep:
|
20
20
|
dup jz _roll_remove
|
21
21
|
push -10 :dec
|
22
|
-
swap
|
22
|
+
swap @-10 swap store
|
23
23
|
push 1 sub jump _roll_keep
|
24
|
-
_roll_remove:
|
25
|
-
|
26
|
-
swap push -10 swap store
|
27
|
-
_roll_restore: ; i
|
24
|
+
_roll_remove: push 10 sub load swap ^-10
|
25
|
+
_roll_restore:
|
28
26
|
dup load swap push 1 add
|
29
27
|
dup push 10 add jz _roll_done
|
30
28
|
jump _roll_restore
|
@@ -37,15 +35,15 @@ _roll_done: load ret
|
|
37
35
|
; [1 2 3 4 5 8 5] => [8 1 2 3 4 5]
|
38
36
|
bury:
|
39
37
|
push -10 dup store ; current heap index kept at -10
|
40
|
-
swap
|
38
|
+
swap ^-9 ; preserve element to bury
|
41
39
|
_bury_keep: ; [n]
|
42
40
|
dup jz _bury_restore
|
43
41
|
push -10 :dec
|
44
|
-
swap
|
42
|
+
swap @-10 swap store
|
45
43
|
push 1 sub jump _bury_keep
|
46
44
|
_bury_restore:
|
47
45
|
push 9 sub load
|
48
|
-
|
46
|
+
@-10 :_roll_restore pop ret
|
49
47
|
|
50
48
|
|
51
49
|
; "digs" out the Ith element of the stack and discards it
|
@@ -63,14 +61,14 @@ dig: :roll pop ret
|
|
63
61
|
;
|
64
62
|
; [-1 9 8 7 -1] => [7 8 9 3]
|
65
63
|
; [0 'c' 'b' 'a' 0] => ['a' 'b' 'c' 3]
|
66
|
-
to_a:
|
64
|
+
to_a: ^-1 push -10 dup store
|
67
65
|
_to_a_loop:
|
68
|
-
dup
|
66
|
+
dup @-1 sub jz _to_a_sentinel
|
69
67
|
push -10 dup :dec load
|
70
68
|
swap store jump _to_a_loop
|
71
69
|
_to_a_sentinel: pop push -10
|
72
70
|
_to_a_restore:
|
73
|
-
dup
|
71
|
+
dup @-10 sub jz _to_a_done
|
74
72
|
push 1 sub dup load swap
|
75
73
|
jump _to_a_restore
|
76
74
|
_to_a_done: push -10 swap sub ret
|
@@ -88,7 +86,7 @@ _npop_done: pop ret
|
|
88
86
|
;
|
89
87
|
; [1 2 3 4 5 2] => [1 2 5]
|
90
88
|
; [1 2 3 4 1] => [1 2 4]
|
91
|
-
nslide: swap
|
89
|
+
nslide: swap ^-1 :npop @-1 ret
|
92
90
|
|
93
91
|
; copies the Nth element to the top of the stack; this does exactly what
|
94
92
|
; a `copy N` instruction would do, but we don't always know N in advance
|
@@ -106,3 +104,14 @@ _ncopy_restore:
|
|
106
104
|
dup push 9 add jz _ncopy_done
|
107
105
|
dup load swap push 1 add jump _ncopy_restore
|
108
106
|
_ncopy_done: pop ret
|
107
|
+
|
108
|
+
; swaps the two arrays at the top of the stack
|
109
|
+
; [A B] => [B A]
|
110
|
+
;
|
111
|
+
; [1 2 3 3 3 2 1 3] => [3 2 1 3 1 2 3 3]
|
112
|
+
; [5 6 2 9 7 5 3 1 5] => [9 7 5 3 1 5 5 6 2]
|
113
|
+
; [0 0 2 4 2 0 3] => [4 2 0 3 0 0 2]
|
114
|
+
; [1 1 2 1] => [2 1 1 1]
|
115
|
+
swapary:
|
116
|
+
push -10 :aryheap push -11 @-9 sub :aryheap
|
117
|
+
push -10 :heapary push -11 @-9 sub :heapary ret
|
@@ -207,8 +207,10 @@ center: push ' ' :centerc ret
|
|
207
207
|
; ["foobar"] => ["fooba"]
|
208
208
|
; ["abc"] => ["ab"]
|
209
209
|
; ["a"] => [""]
|
210
|
-
;
|
211
|
-
strchop: dup
|
210
|
+
; [""] => [""]
|
211
|
+
strchop: dup jz _strchop_empty
|
212
|
+
dup :strlen push 1 sub push 0 swap :strslice ret
|
213
|
+
_strchop_empty: ret
|
212
214
|
|
213
215
|
; splits string S on delimiting character C, leaving the resultant substrings
|
214
216
|
; on the stack as a pseudo-array (length at top of stack)
|
@@ -222,13 +224,13 @@ strchop: dup :strlen push 1 sub push 0 swap :strslice ret
|
|
222
224
|
; ["foo,,bar" ','] => ["foo" "" "bar" 3]
|
223
225
|
; ["/foo/bar/" '/'] => ["" "foo" "bar" "" 4]
|
224
226
|
strsplit:
|
225
|
-
push -3
|
226
|
-
|
227
|
+
push -3,1 store ; number of found substrings
|
228
|
+
^-2 ; stash delimiter to allow some stack juggling
|
227
229
|
_strsplit_loop:
|
228
|
-
dup dup
|
230
|
+
dup dup @-2
|
229
231
|
:strindex dup jn _strsplit_done ; done when index of delimiter is -1
|
230
232
|
push 0 swap :strslice
|
231
|
-
swap copy 1
|
233
|
+
swap copy 1 @-3
|
232
234
|
swap :strlen
|
233
235
|
swap push -3 swap push 1 add store ; update number of found
|
234
236
|
push 1 add push 128 swap :pow div ; shrink haystack
|
@@ -246,11 +248,10 @@ lines: push 10 :strsplit ret
|
|
246
248
|
; ["foo" "bar" "baz" 3 '--'] => ["foo--bar--baz"]
|
247
249
|
; ["foo" 1 "?!"] => ["foo"]
|
248
250
|
strjoinc:
|
249
|
-
dup :strlen pop ; get delimiter length into -1
|
250
|
-
|
251
|
-
map (push -2 load :strcat) ; add delimiter to all elements
|
251
|
+
dup :strlen pop ^-2 ; get delimiter length into -1
|
252
|
+
map (@-2 :strcat) ; add delimiter to all elements
|
252
253
|
swap push 128 copy 1 :strlen
|
253
|
-
|
254
|
+
@-2 :strlen
|
254
255
|
sub :pow mod swap ; remove delimiter from last and flow into strjoin
|
255
256
|
|
256
257
|
; concatenates the pseudo-array of strings A into string S
|
@@ -287,13 +288,12 @@ _strcountc_done: swap slide 2 ret
|
|
287
288
|
; ["eunoia" "aeiou"] => [5]
|
288
289
|
; ["why" "aeiou"] => [0]
|
289
290
|
strcount:
|
290
|
-
swap push
|
291
|
-
|
292
|
-
map (push -2 load swap :strcountc)
|
291
|
+
swap ^-2 :strunpack push 0 :to_a
|
292
|
+
map (@-2 swap :strcountc)
|
293
293
|
reduce (add) ret
|
294
294
|
|
295
295
|
; translates all characters in A to the corresponding characters in B
|
296
|
-
; in
|
296
|
+
; in string S, ; similar to the `tr` utility in Unix. A and B must be
|
297
297
|
; of the same length. TODO: make this smarter (ranges, length mismatch)
|
298
298
|
; ! clobbers heap addresses -1, -2, and -3
|
299
299
|
; [S A B] => [S']
|
@@ -301,16 +301,13 @@ strcount:
|
|
301
301
|
; ["abcd" "abc" "xyz"] => ["xyzd"]
|
302
302
|
; ["foobar" "oba" "ele"] => ["feeler"]
|
303
303
|
; ["abcdcba" "abcd" "xyz|"] => ["xyz|zyx"]
|
304
|
-
strtrans:
|
305
|
-
|
306
|
-
push -2 swap store
|
307
|
-
dup :strlen push -1 swap store
|
308
|
-
:strunpack push -1 load
|
304
|
+
strtrans: ^-3 ^-2
|
305
|
+
dup :strlen ^-1 :strunpack @-1
|
309
306
|
map (:_strtrans) pop :strpack ret
|
310
307
|
_strtrans:
|
311
|
-
dup
|
308
|
+
dup @-2 swap :strindex
|
312
309
|
dup jn _strtrans_no
|
313
|
-
|
310
|
+
@-3 swap :charat
|
314
311
|
slide 1 ret
|
315
312
|
_strtrans_no: pop ret
|
316
313
|
|
@@ -343,17 +340,38 @@ _strsqueeze_loop: ; [s]
|
|
343
340
|
_strsqueeze_skip: pop jump _strsqueeze_loop
|
344
341
|
_strsqueeze_done: pop :strpack :strrev ret
|
345
342
|
|
343
|
+
$_strdel(cmp) {
|
344
|
+
:strunpack @-1 :strlen
|
345
|
+
select (@-2 swap :strindex `cmp`)
|
346
|
+
pop :strpack ret
|
347
|
+
}
|
348
|
+
|
349
|
+
; returns the string S with all characters in string T removed, like `tr -d`.
|
350
|
+
; If the first character of T is '^', instead only those characters are kept.
|
351
|
+
; [S T] => [S']
|
352
|
+
;
|
353
|
+
; ["abc123" "abc"] => ["123"]
|
354
|
+
; ["abc123" "123"] => ["abc"]
|
355
|
+
; ["abcba12321" "abc"] => ["12321"]
|
356
|
+
; ["abc12321cba" "^2ac"] => ["ac22ca"]
|
357
|
+
; ["facetious" "^aeiou"] => ["aeiou"]
|
358
|
+
strdel: push -1 copy 2 store push -2 copy 1 store
|
359
|
+
push 0 :charat push '^' :neq jz _strdel_comp $_strdel(:neg?)
|
360
|
+
_strdel_comp: $_strdel(:pos?)
|
361
|
+
|
346
362
|
; returns the sum of the ordinal values of the characters in string S
|
347
363
|
; [S] => [N]
|
348
364
|
;
|
349
365
|
; ["ABC"] => [198]
|
350
366
|
; ["012"] => [147]
|
351
367
|
; ["a"] => [97]
|
352
|
-
; [""] => []
|
353
|
-
strsum:
|
368
|
+
; [""] => [0]
|
369
|
+
strsum: dup jz _strsum_empty
|
370
|
+
:strunpack push 0 :to_a reduce (add) ret
|
371
|
+
_strsum_empty: ret
|
354
372
|
|
355
373
|
; rotates the string S to the left N times, wrapping
|
356
|
-
; [S N]
|
374
|
+
; [S N] => [S']
|
357
375
|
;
|
358
376
|
; ["abc" 0] => ["abc"]
|
359
377
|
; ["abcd" 1] => ["bcda"]
|
@@ -362,9 +380,44 @@ strsum: :strunpack push 0 :to_a reduce (add) ret
|
|
362
380
|
strrotl: push 128 swap copy 2 :strlen mod :pow :divmod :strcat ret
|
363
381
|
|
364
382
|
; rotates the string S to the right N times, wrapping
|
365
|
-
; [S N]
|
383
|
+
; [S N] => [S']
|
366
384
|
;
|
367
385
|
; ["abcd" 1] => ["dabc"]
|
368
386
|
; ["abcd" 5] => ["dabc"]
|
369
387
|
; ["foodbar" 3] => ["barfood"]
|
370
|
-
strrotr: push
|
388
|
+
strrotr: push -1 mul :strrotl ret
|
389
|
+
|
390
|
+
; gets the characters of the string S onto the stack as a pseudo-array, but
|
391
|
+
; with a leading 0 on the assumption that it'll eventually be repacked
|
392
|
+
;
|
393
|
+
; ["abc"] => [0 99 98 97 3]
|
394
|
+
strtoa: dup :strlen pop :strunpack @-1 $++ ret
|
395
|
+
|
396
|
+
; frobnicates the string S by XORing all its bytes with 42
|
397
|
+
; [S] => [S']
|
398
|
+
;
|
399
|
+
; ["foobar"] => ["LEEHKX"]
|
400
|
+
; ["LEEHKX"] => ["foobar"]
|
401
|
+
memfrob: :strtoa map (push 42 :bxor) pop :strpack ret
|
402
|
+
|
403
|
+
; returns 1 if the string S begins with substring T, 0 otherwise
|
404
|
+
; [S T] => [0 | 1]
|
405
|
+
;
|
406
|
+
; ["foobar" "foo"] => [1]
|
407
|
+
; ["foobar" "boo"] => [0]
|
408
|
+
; ["abc123" "123"] => [0]
|
409
|
+
; [" foo" " "] = [1]
|
410
|
+
strbegins?:
|
411
|
+
dup :strlen copy 2 swap push 0 swap
|
412
|
+
:strslice :eq slide 1 ret
|
413
|
+
|
414
|
+
; returns 1 if the string S ends with substring T, 0 otherwise
|
415
|
+
; [S T] => [0 | 1]
|
416
|
+
;
|
417
|
+
; ["foobar" "bar"] => [1]
|
418
|
+
; ["foobar" "foo"] => [0]
|
419
|
+
; ["abc123" "abc"] => [0]
|
420
|
+
; ["foo " " "] = [1]
|
421
|
+
strends?:
|
422
|
+
:strrev dup :strlen copy 2 :strrev swap push 0 swap
|
423
|
+
:strslice :eq slide 1 ret
|