spitewaste 0.1.011 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,18 +54,29 @@ _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
- dup push 2 mod
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
59
76
 
60
77
  $_to_roman(r, v) {
61
78
  push `v` :divmod swap push `r` swap :strrep
62
- push -2 load swap :strcat push -2 swap store
79
+ @-2 swap :strcat ^-2
63
80
  }
64
81
 
65
82
  ; converts the number N to a string of roman numerals R
@@ -77,3 +94,17 @@ to_roman: push -2,0 store
77
94
  $_to_roman("X", 10) $_to_roman("IX", 9)
78
95
  $_to_roman("V", 5) $_to_roman("IV", 4)
79
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: push -1 load slide 2 ret
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 push -1 swap store ; preserve N because array operations
138
+ dup ^-1 ; preserve N because array operations
143
139
  :isqrt push 1 swap :range dup ; 1..isqrt(N)
144
- reject (push -1 load swap mod) :arydup ; get first half of divisors
145
- map (push -1 load swap div) :arycat ; map first half to second half
146
- push -1 load copy 2 dup mul sub jz _divisors_square ret
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,16 +1,39 @@
1
- import math ; pow
1
+ import array ; sorted?
2
+ import math ; pow
3
+ import string ; strtoa
2
4
 
3
- srand: push $seed swap store ret
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 3,13,10244807 mul mul mul
8
- push 2,32 :pow mod
13
+ push 48271 mul
14
+ push 2,31 :pow $-- mod
9
15
  store load ret
10
16
 
11
- rand_range: ; [a b]
12
- copy 1 sub :rand swap mod add ret
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
13
21
 
14
- dice:
15
- push -2 swap store push 1 :aryfill
16
- map (push -2 load :rand_range) ret
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
- ratpow:
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
@@ -1,6 +1,6 @@
1
1
  ;;; Heavy-handed stack manipulation
2
2
 
3
- ; These subrtouines do some pretty intricate stack-based operations, relying
3
+ ; These subroutines do some pretty intricate stack-based operations, relying
4
4
  ; heavily on clobbering the heap in order to maintain their "bookkeeping".
5
5
  ; Many of them use heap addresses -10 and lower, unbounded, so they're only
6
6
  ; meant to be used in a pinch or when there just isn't much of an alternative.
@@ -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: ; [n]
19
+ _roll_keep:
20
20
  dup jz _roll_remove
21
21
  push -10 :dec
22
- swap push -10 load swap store
22
+ swap @-10 swap store
23
23
  push 1 sub jump _roll_keep
24
- _roll_remove:
25
- push 10 sub load
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 push -9 swap store ; preserve element to bury
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 push -10 load swap store
42
+ swap @-10 swap store
45
43
  push 1 sub jump _bury_keep
46
44
  _bury_restore:
47
45
  push 9 sub load
48
- push -10 load :_roll_restore pop ret
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: push -1 swap store push -10 dup store
64
+ to_a: ^-1 push -10 dup store
67
65
  _to_a_loop:
68
- dup push -1 load sub jz _to_a_sentinel
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 push -10 load sub jz _to_a_done
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 push -1 swap store :npop push -1 load ret
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
- ; ! [""] => ERROR TODO: Should just be a no-op.
211
- strchop: dup :strlen push 1 sub push 0 swap :strslice ret
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 push 1 store ; number of found substrings
226
- push -2 swap store ; stash delimiter to allow some stack juggling
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 push -2 load
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 push -3 load
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
- push -2 swap store
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
- push -2 load :strlen
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,9 +288,8 @@ _strcountc_done: swap slide 2 ret
287
288
  ; ["eunoia" "aeiou"] => [5]
288
289
  ; ["why" "aeiou"] => [0]
289
290
  strcount:
290
- swap push -2 swap store
291
- :strunpack push 0 :to_a
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
@@ -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
- push -3 swap store
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 push -2 load swap :strindex
308
+ dup @-2 swap :strindex
312
309
  dup jn _strtrans_no
313
- push -3 load swap :charat
310
+ @-3 swap :charat
314
311
  slide 1 ret
315
312
  _strtrans_no: pop ret
316
313
 
@@ -344,8 +341,8 @@ _strsqueeze_skip: pop jump _strsqueeze_loop
344
341
  _strsqueeze_done: pop :strpack :strrev ret
345
342
 
346
343
  $_strdel(cmp) {
347
- :strunpack push -1 load :strlen
348
- select (push -2 load swap :strindex `cmp`)
344
+ :strunpack @-1 :strlen
345
+ select (@-2 swap :strindex `cmp`)
349
346
  pop :strpack ret
350
347
  }
351
348
 
@@ -368,11 +365,13 @@ _strdel_comp: $_strdel(:pos?)
368
365
  ; ["ABC"] => [198]
369
366
  ; ["012"] => [147]
370
367
  ; ["a"] => [97]
371
- ; [""] => [] TODO: bug, should be 0
372
- strsum: :strunpack push 0 :to_a reduce (add) ret
368
+ ; [""] => [0]
369
+ strsum: dup jz _strsum_empty
370
+ :strunpack push 0 :to_a reduce (add) ret
371
+ _strsum_empty: ret
373
372
 
374
373
  ; rotates the string S to the left N times, wrapping
375
- ; [S N]
374
+ ; [S N] => [S']
376
375
  ;
377
376
  ; ["abc" 0] => ["abc"]
378
377
  ; ["abcd" 1] => ["bcda"]
@@ -381,9 +380,44 @@ strsum: :strunpack push 0 :to_a reduce (add) ret
381
380
  strrotl: push 128 swap copy 2 :strlen mod :pow :divmod :strcat ret
382
381
 
383
382
  ; rotates the string S to the right N times, wrapping
384
- ; [S N]
383
+ ; [S N] => [S']
385
384
  ;
386
385
  ; ["abcd" 1] => ["dabc"]
387
386
  ; ["abcd" 5] => ["dabc"]
388
387
  ; ["foodbar" 3] => ["barfood"]
389
- strrotr: push 0 swap sub :strrotl ret
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