@@ -56,12 +56,15 @@ class ProgramError < Exception; end
56
56
57
57
OPERATORS . default = [ :wall ]
58
58
59
- def self . run ( src , debug_level = 0 )
60
- new ( src , debug_level ) . run
59
+ def self . run ( src , debug_level = 0 , in_str = $stdin , out_str = $stdout , max_ticks = - 1 )
60
+ new ( src , debug_level , in_str , out_str , max_ticks ) . run
61
61
end
62
62
63
- def initialize ( src , debug_level = false )
63
+ def initialize ( src , debug_level = false , in_str = $stdin , out_str = $stdout , max_ticks = - 1 )
64
64
@debug_level = debug_level
65
+ @in_str = in_str
66
+ @out_str = out_str
67
+ @max_ticks = max_ticks
65
68
66
69
@grid = parse ( src )
67
70
@height = @grid . size
@@ -95,7 +98,10 @@ def run
95
98
@ip += @dir . vec
96
99
97
100
@tick += 1
101
+ break if @max_ticks > -1 && @tick >= @max_ticks
98
102
end
103
+
104
+ @max_ticks > -1 && @tick >= @max_ticks
99
105
end
100
106
101
107
private
@@ -227,7 +233,7 @@ def process cmd
227
233
byte = read_byte
228
234
push_main ( byte ? byte . ord : -1 )
229
235
when :output_char
230
- $> << ( pop_main % 256 ) . chr
236
+ @out_str . print ( pop_main % 256 ) . chr
231
237
when :input_int
232
238
val = 0
233
239
sign = 1
@@ -258,7 +264,7 @@ def process cmd
258
264
259
265
push_main ( sign *val )
260
266
when :output_int
261
- $> << pop_main
267
+ @out_str . print pop_main
262
268
when :output_newline
263
269
puts
264
270
@@ -403,23 +409,8 @@ def read_byte
403
409
result = @next_byte
404
410
@next_byte = nil
405
411
else
406
- result = STDIN . read ( 1 )
412
+ result = @in_str . read ( 1 )
407
413
end
408
414
result
409
415
end
410
- end
411
-
412
- case ARGV [ 0 ]
413
- when "-d"
414
- debug_level = 1
415
- when "-D"
416
- debug_level = 2
417
- else
418
- debug_level = 0
419
- end
420
-
421
- if debug_level > 0
422
- ARGV . shift
423
- end
424
-
425
- Labyrinth . run ( ARGF . read , debug_level )
416
+ end
0 commit comments