-
Notifications
You must be signed in to change notification settings - Fork 1
/
mode.rb
1252 lines (1116 loc) · 32.7 KB
/
mode.rb
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
# coding: utf-8
require_relative 'point2d'
require_relative 'direction'
require 'prime'
require 'date'
class Mode
# List of operators which should not be ignored while in string mode.
STRING_CMDS = "\"'\\/_|"
def initialize(state)
@state = state
end
def is_char? val
val && (val >= 0 && val <= 0xD7FF || val >= 0xE000 && val <= 0x10FFFF)
end
def process
raise NotImplementedError
end
# Returns true when the resulting cell is a command.
def move
raw_move if @state.cell == "'".ord
raw_move
cell = @state.cell
case cell
when '/'.ord, '\\'.ord
@state.dir = @state.dir.reflect cell.chr_utf_8
@state.toggle_mode
return false
when '_'.ord, '|'.ord
@state.dir = @state.dir.reflect cell.chr_utf_8
return false
end
return true if @state.string_mode
@state.print_debug_info if cell == '`'.ord
is_char?(cell) && self.class::OPERATORS.has_key?(cell.chr_utf_8)
end
# Moves the IP a single cell without regard for mirrors, walls or no-ops.
# Does respect grid boundaries.
def raw_move
raise NotImplementedError
end
def push val
@state.push val
end
def pop
raise NotImplementedError
end
def push_return
@state.push_return
end
def pop_return
@state.pop_return
end
def peek_return
@state.peek_return
end
def peek
val = pop
push val
val
end
end
class Cardinal < Mode
OPERATORS = {
'@' => :terminate,
'<' => :move_west,
'>' => :move_east,
'^' => :move_north,
'v' => :move_south,
'{' => :turn_left,
'}' => :turn_right,
'#' => :trampoline,
'$' => :cond_trampoline,
'=' => :cond_sign,
'&' => :repeat_iterator,
'~' => :swap,
'.' => :dup,
';' => :discard,
',' => :rotate_stack,
'0' => :digit, '1' => :digit, '2' => :digit, '3' => :digit, '4' => :digit, '5' => :digit, '6' => :digit, '7' => :digit, '8' => :digit, '9' => :digit,
'+' => :add,
'-' => :sub,
'*' => :mul,
':' => :div,
'%' => :mod,
'!' => :store_tape,
'?' => :load_tape,
'[' => :mp_left,
']' => :mp_right,
'(' => :search_left,
')' => :search_right,
'"' => :leave_string_mode,
"'" => :escape,
'I' => :input,
'O' => :output,
'i' => :raw_input,
'o' => :raw_output,
'A' => :bitand,
'B' => :divisors,
'C' => :binomial,
'D' => :deduplicate,
'E' => :power,
'F' => :divides,
'G' => :gcd,
'H' => :abs,
'J' => :jump_raw,
'K' => :return_raw,
'L' => :lcm,
'M' => :argc,
'N' => :bitnot,
'P' => :factorial,
'Q' => :convert,
'R' => :negate,
'S' => :replace_divisors,
'T' => :sleep,
'U' => :random,
'V' => :bitor,
'W' => :discard_return,
'X' => :bitxor,
'Y' => :unpack,
'Z' => :pack,
'a' => :const_10,
'b' => :random_swap,
'c' => :prime_factors,
'd' => :stack_depth,
'e' => :const_m1,
'f' => :prime_factor_pairs,
'g' => :get_cell,
'h' => :inc,
'j' => :jump,
'k' => :return,
'l' => :clear_bits,
'm' => :floor,
'n' => :not,
'p' => :put_cell,
'q' => :get_mp,
'r' => :range,
's' => :sortswap,
't' => :dec,
'u' => :set_bits,
'w' => :push_return,
'x' => :extract_bit,
'y' => :bitif,
'z' => :drop_small_factors,
}
OPERATORS.default = :nop
def raw_move
@state.ip += @state.dir.vec
@state.wrap
end
def pop
val = nil
loop do
val = @state.pop
if val.is_a?(String)
found = false
val.scan(/(?:\A|(?!\G))-?\d+/) { push $&.to_i; found = true }
next if !found
val = @state.pop
end
break
end
val || 0
end
def process cmd
opcode = OPERATORS[cmd]
case opcode
when :nop
raise "No-op reached process(). This shouldn't happen."
when :terminate
@state.done = true
when :move_east
@state.dir = East.new
when :move_west
@state.dir = West.new
when :move_south
@state.dir = South.new
when :move_north
@state.dir = North.new
when :turn_left
@state.dir = @state.dir.left
when :turn_right
@state.dir = @state.dir.right
when :trampoline
@state.skip_next
when :cond_trampoline
@state.skip_next if pop == 0
when :cond_sign
val = pop
if val < 0
@state.dir = @state.dir.left
elsif val > 0
@state.dir = @state.dir.right
end
when :repeat_iterator
@state.add_iterator pop
when :jump
push_return
y = pop
x = pop
@state.jump(x,y)
when :return
@state.jump(*pop_return)
when :jump_raw
y = pop
x = pop
@state.jump(x,y)
when :return_raw
@state.jump(*peek_return)
when :push_return
push_return
when :discard_return
pop_return
when :get_cell
y = pop
x = pop
push @state.cell(Point2D.new(x,y))
when :put_cell
y = pop
x = pop
v = pop
@state.put_cell(Point2D.new(x,y), v)
when :store_tape
@state.tape[@state.mp] = pop
when :load_tape
push (@state.tape[@state.mp] || -1)
when :mp_left
@state.mp -= 1
when :mp_right
@state.mp += 1
when :search_left
val = pop
(@state.mp-1).downto([*@state.tape.keys, 0].min-1).each do |i|
if @state.tape[i] == val
@state.mp = i
break
end
end
when :search_right
val = pop
(@state.mp+1..[*@state.tape.keys, 0].max+1).each do |i|
if @state.tape[i] == val
@state.mp = i
break
end
end
when :get_mp
push @state.mp
when :leave_string_mode
@state.stack += @state.current_string
when :escape
old_ip = @state.ip
raw_move
push @state.cell
@state.ip = old_ip
when :input
char = @state.in_str.getc
while char && char.scrub('') == ''
char = @state.in_str.getc
end
push(char ? char.ord : -1)
when :output
# Will throw an error when value isn't a valid code point
val = pop
if is_char?(val)
val.chr_utf_8.unpack('C*').each{|c| @state.out_str.putc c }
end
when :raw_input
push(@state.in_str.getbyte || -1)
when :raw_output
@state.out_str.putc pop
when :argc
push @state.args.size
when :digit
push cmd.to_i
when :add
push(pop + pop)
when :sub
y = pop
push(pop - y)
when :mul
push(pop * pop)
when :div
y = pop
push(pop / y)
when :mod
y = pop
push(pop % y)
when :inc
push(pop+1)
when :dec
push(pop-1)
when :abs
push(pop.abs)
when :power
y = pop
x = pop
if y < 0
# Compute integer root of the absolute base, preserving sign of base
if x < 0
r = 0
# If I'm ever not lazy I'll replace this with a binary search...
r -= 1 while (-r)**-y < -x
push r
else
r = 0
# If I'm ever not lazy I'll replace this with a binary search...
r += 1 until r**-y > x
push (r-1)
end
else
push x**y
end
when :bitand
push(pop & pop)
when :bitnot
push(~pop)
when :bitor
push(pop | pop)
when :bitxor
push(pop ^ pop)
when :bitif
z = pop
y = pop
x = pop
push(x&y | ~x&z)
when :clear_bits
x = pop
if x > 0
msb = Math.log2(x).floor
elsif x < -1
msb = Math.log2(~x).floor
else
msb = 0
end
push (x & -(2**msb))
when :set_bits
x = pop
if x > 0
msb = Math.log2(x).floor
elsif x < -1
msb = Math.log2(~x).floor
else
msb = 0
end
push (x | (2**msb-1))
when :extract_bit
y = pop
x = pop
if y >= 0
push x[y]
else
if x > 0
msb = Math.log2(x).floor
elsif x < -1
msb = Math.log2(~x).floor
else
msb = -1
end
push x[msb+y+1]
end
when :factorial
val = pop
if val >= 0
push (1..val).reduce(1, :*)
else
push (val..-1).reduce(1, :*)
end
when :binomial
k = pop
n = pop
k = n-k if n > 0 && k > n/2
if k < 0
push 0
else
prod = 1
(1..k).each do |i|
prod *= n
prod /= i
n -= 1
end
push prod
end
when :negate
push -pop
when :prime_factors
n = pop
if n == 0
push 0
else
Prime.prime_division(n).each{ |p,n| n.times{ push p } }
end
when :prime_factor_pairs
n = pop
if n == 0
push 0
push 1
else
Prime.prime_division(n).flatten.each{ |x| push x }
end
when :deduplicate
n = pop
if n == 0
push 0
else
push Prime.int_from_prime_division(Prime.prime_division(n).map{ |p,n| [p,1]})
end
when :divides
y = pop
x = pop
if y != 0 && x % y == 0
push y
else
push 0
end
when :gcd
push (pop.gcd pop)
when :lcm
push (pop.lcm pop)
when :floor
y = pop.abs
x = pop
push (x/y)*y
when :replace_divisors
z = pop
y = pop
x = pop
if x == 0
push 0
elsif y == 1 || y == -1
if z == y
push x
elsif z == 0
push 0
else
loop { next }
end
else
order = 0
while x%y == 0
order += 1
x /= y
end
x *= z**order
push x
end
when :divisors
n = pop
sgn = n <=> 0
n = n.abs
k = 1
small_divs = []
large_divs = []
while k*k <= n
if n%k == 0
small_divs << k
large_divs << n/k if k*k != n
end
k += 1
end
(small_divs + large_divs.reverse).each {|k| push k*sgn}
when :drop_small_factors
k = pop
n = pop
if n.abs > 1
factors = Prime.prime_division(n.abs)
n_dropped = 0
while !factors.empty? && factors[0][0] <= k.abs
pr, m = factors.shift
n_dropped += m
end
n = Prime.int_from_prime_division(factors) * (n <=> 0)
if k < 0 && n_dropped.odd?
n *= -1
end
end
push n
when :pack
y = pop
x = pop
# Map integers to naturals
sgn = x <=> 0
x = x*sgn*2 + [0, sgn].min
sgn = y <=> 0
y = y*sgn*2 + [0, sgn].min
# Map two naturals to one
z = (x+y)*(x+y+1)/2 + y
# Map the natural back to an integer
z = (-1)**z * ((z+1)/2)
push z
when :unpack
z = pop
# Map the integer to a positive natural
sgn = z <=> 0
z = z*sgn*2 + [0, sgn].min
# Map the natural to two
y = z
x = 0
while x < y
x += 1
y -= x
end
x -= y
# Map the naturals back to integers
x = (-1)**x * ((x+1)/2)
y = (-1)**y * ((y+1)/2)
push x
push y
when :not
push (pop == 0 ? 1 : 0)
when :range
val = pop
if val >= 0
0.upto(val) {|i| push i}
else
(-val).downto(0) {|i| push i}
end
when :random
val = pop
if val > 0
push rand val
elsif val == 0
push 0
else
push -(rand val)
end
when :random_swap
top = pop
second = pop
top, second = [top, second].shuffle
push second
push top
when :sortswap
top = pop
second = pop
top, second = second, top if top < second
push second
push top
when :swap
top = pop
second = pop
push top
push second
when :dup
top = pop
push top
push top
when :discard
pop
when :stack_depth
push @state.stack.size
when :rotate_stack
n = pop
if n > 0
push(@state.stack.delete_at(-n-1) || 0)
elsif n < 0
top = @state.stack.pop || 0 # Do this manually to prevent type conversion
@state.stack = [0]*[-n-@state.stack.size, 0].max + @state.stack
@state.stack.insert(n-1, top)
end
when :convert
n = pop
n.times.map{pop}.reverse.each{|v| push v}
when :sleep
sleep [0, pop/1000.0].max
when :const_10
push 10
when :const_m1
push -1
end
end
end
class Ordinal < Mode
OPERATORS = {
'@' => :terminate,
'0' => :digit, '1' => :digit, '2' => :digit, '3' => :digit, '4' => :digit, '5' => :digit, '6' => :digit, '7' => :digit, '8' => :digit, '9' => :digit,
'+' => :superimpose,
'-' => :drop,
'*' => :concat,
':' => :occurrences,
'%' => :split,
'<' => :ensure_west,
'>' => :ensure_east,
'^' => :ensure_north,
'v' => :ensure_south,
'{' => :turn_left,
'}' => :turn_right,
'#' => :trampoline,
'$' => :cond_trampoline,
'=' => :cond_cmp,
'&' => :fold_iterator,
'~' => :swap,
'.' => :dup,
';' => :discard,
',' => :permute_stack,
'!' => :store_register,
'?' => :load_register,
'[' => :register_left,
']' => :register_right,
'(' => :search_left,
')' => :search_right,
'"' => :leave_string_mode,
"'" => :escape,
'I' => :input,
'O' => :output,
'i' => :raw_input,
'o' => :raw_output,
'A' => :intersection,
'B' => :substrings,
'C' => :subsequences,
'D' => :deduplicate,
'E' => :riffle,
'F' => :find,
'G' => :longest_common_substring,
'H' => :trim,
'J' => :jump_raw,
'K' => :return_raw,
'L' => :shortest_common_superstring,
'M' => :argv,
'N' => :complement,
'P' => :permutations,
'Q' => :reverse_stack,
'R' => :reverse,
'S' => :replace,
'T' => :datetime,
'U' => :random_choice,
'V' => :union,
'W' => :discard_return,
'X' => :symdifference,
'Y' => :unzip,
'Z' => :zip,
'a' => :const_lf,
'b' => :shuffle,
'c' => :characters,
'd' => :push_joined_stack,
'e' => :const_empty,
'f' => :runs,
'g' => :get_diagonal,
'h' => :head,
'j' => :jump,
'k' => :return,
'l' => :lower_case,
'm' => :truncate_to_shorter,
'u' => :upper_case,
'n' => :not,
'p' => :put_diagonal,
'q' => :join_tape,
'r' => :expand_ranges,
's' => :sort,
't' => :tail,
'w' => :push_return,
'x' => :permute,
'y' => :transliterate,
'z' => :discard_up_to,
#'(' => ,
#')' => ,
#'!' => ,
#'$' => ,
#'&' => ,
#',' => ,
#'.' => ,
#';' => ,
#'=' => ,
#'?' => ,
#'`' => ,
#'A' => ,
# ...
#'Z' => ,
#'a' => ,
# ...
#'z' => ,
}
OPERATORS.default = :nop
def raw_move
if @state.width == 1 || @state.height == 1
return
end
new_pos = @state.ip + @state.dir.vec + @state.storage_offset
@state.dir = @state.dir.reflect('|') if new_pos.x < 0 || new_pos.x >= @state.width
@state.dir = @state.dir.reflect('_') if new_pos.y < 0 || new_pos.y >= @state.height
@state.ip += @state.dir.vec
end
def pop
val = @state.pop
val ? val.to_s : ''
end
def scan_source label
ip_dir = @state.dir
grid = @state.grid
while !ip_dir.is_a? NorthEast
grid = grid.transpose.reverse
ip_dir = ip_dir.left
end
height = grid.size
width = height == 0 ? 0 : grid[0].size
positions = []
(0..width+height-2).map do |d|
min_x = [0,d-height+1].max
max_x = [width-1,d].min
line = (min_x..max_x).map do |x|
y = d - x
grid[y][x].chr_utf_8
end.join
line.scan(/(?=#{Regexp.escape(label)})/) do
x = min_x + $`.size + label.size - 1
y = d-x
positions << [x,y]
end
end
ip_dir = @state.dir
while !ip_dir.is_a? NorthEast
ip_dir = ip_dir.left
positions.map! {|x, y| [grid.size - y - 1, x]}
grid = grid.reverse.transpose
end
positions
end
def process cmd
opcode = OPERATORS[cmd]
case opcode
when :nop
raise "No-op reached process(). This shouldn't happen."
when :terminate
@state.done = true
when :ensure_west
@state.dir = @state.dir.reflect '|' if @state.dir.vec.x > 0
when :ensure_east
@state.dir = @state.dir.reflect '|' if @state.dir.vec.x < 0
when :ensure_north
@state.dir = @state.dir.reflect '_' if @state.dir.vec.y > 0
when :ensure_south
@state.dir = @state.dir.reflect '_' if @state.dir.vec.y < 0
when :turn_left
@state.dir = @state.dir.left
when :turn_right
@state.dir = @state.dir.right
when :trampoline
@state.skip_next
when :cond_trampoline
@state.skip_next if pop == ''
when :cond_cmp
top = pop
second = pop
if top > second
@state.dir = @state.dir.left
elsif top < second
@state.dir = @state.dir.right
end
when :fold_iterator
@state.add_iterator pop
when :jump
label = pop
positions = scan_source(label)
if !positions.empty?
push_return
@state.jump(*positions[0])
end
when :return
@state.jump(*pop_return)
when :jump_raw
label = pop
positions = scan_source(label)
@state.jump(*positions[0]) if !positions.empty?
when :return_raw
@state.jump(*peek_return)
when :push_return
push_return
when :discard_return
pop_return
when :get_diagonal
label = pop
positions = scan_source(label)
if !positions.empty?
cursor = Point2D.new(*positions[0]) + @state.dir.vec
string = ''
while is_char? @state.cell(cursor)
string << @state.cell(cursor)
cursor += @state.dir.vec
end
push string
end
when :put_diagonal
label = pop
value = pop
positions = scan_source(label)
if !positions.empty?
cursor = Point2D.new(*positions[0]) + @state.dir.vec
value.each_char {|c|
@state.put_cell(cursor, c.ord)
cursor += @state.dir.vec
}
end
when :store_register
i = @state.rp
pop.each_char do |c|
@state.tape[i] = c.ord
i += 1
end
@state.tape[i] = -1
when :load_register
push @state.read_register
when :register_left
@state.rp -= 1 while is_char? @state.tape[@state.rp-1]
@state.rp -= 1
@state.rp -= 1 while is_char? @state.tape[@state.rp-1]
when :register_right
@state.rp += 1 while is_char? @state.tape[@state.rp]
@state.rp += 1
when :search_left
needle = pop
string = ""
cursor = @state.rp-1
cursor -= 1 while is_char? @state.tape[cursor-1]
(cursor-2).downto([*@state.tape.keys, 0].min-1).each do |i|
if is_char?(@state.tape[i])
string << @state.tape[i]
elsif string.reverse[needle]
@state.rp = i+1
break
else
string = ""
end
end
when :search_right
needle = pop
string = ""
cursor = @state.rp
cursor += 1 while is_char? @state.tape[cursor]
(cursor+1..[*@state.tape.keys, 0].max+1).each do |i|
if is_char?(@state.tape[i])
string << @state.tape[i]
elsif string[needle]
@state.rp = i - string.size
break
else
string = ""
end
end
when :join_tape
push @state.tape.keys.sort.map{|i| @state.tape[i]}.select{|v| is_char?(v)}.map(&:chr_utf_8).join
when :leave_string_mode
push @state.current_string.select{|c| is_char? c }.map(&:chr_utf_8).join
when :escape
old_ip = @state.ip
old_dir = @state.dir
raw_move
if is_char?(@state.cell)
push @state.cell.chr_utf_8
else
push ''
end
@state.ip = old_ip
@state.dir = old_dir
when :digit
push(pop + cmd)
when :input
line = @state.in_str.gets
push(line ? line.scrub('').chomp : '')
when :output
pop.unpack('C*').each{|c| @state.out_str.putc c }
@state.out_str.puts
when :raw_input
str = @state.in_str.read
push(str ? str.scrub('') : '')
when :raw_output
pop.unpack('C*').each{|c| @state.out_str.putc c }
when :argv
arg = ARGV.shift || ""
push(arg.dup.force_encoding(Encoding::UTF_8).scrub(''))
when :superimpose
top = pop
second = pop
result = ""
[top.size, second.size].max.times do |i|
result << [top[i] || 0.chr_utf_8, second[i] || 0.chr_utf_8].max
end
push result
when :concat
top = pop
second = pop
push(second + top)
when :drop
y = pop
x = pop
result = x.chars
x.scan(/(?=#{Regexp.escape(y)})/) do
y.size.times do |i|