Execute SNUSP/Ruby: Difference between revisions

m
→‎Core SNUSP: IO improvements
(add Ruby)
 
m (→‎Core SNUSP: IO improvements)
Line 2:
With gratitude to [[RCSNUSP/Tcl]]
== Core SNUSP ==
<lang ruby>class$stdout.sync CoreSNUSP= true
$stdin.sync = true
 
class CoreSNUSP
Bounce = {
:ruld => {:right => :up, :left => :down, :up => :right, :down => :left },
Line 19 ⟶ 22:
"." => :write,
"," => :read,
'"' => :dump,
})
 
def initialize(text, args={})
@data = Hash.new(0)
@dptr = 0
Line 28 ⟶ 32:
@width = @program[0].nil? ? 0 : @program[0].size
@pdir = :right
@input = args[:input]
end
 
Line 77 ⟶ 82:
@dptr += 1
end
 
def left
@dptr -= 1
end
 
def incr
if @dptr < 0
Line 87 ⟶ 94:
p ["data:",@dptr, @data[@dptr]] if $DEBUG
end
 
def decr
if @dptr < 0
Line 94 ⟶ 102:
p ["data:",@dptr, @data[@dptr]] if $DEBUG
end
 
def ruld
p @pdir if $DEBUG
Line 99 ⟶ 108:
p @pdir if $DEBUG
end
 
def lurd
p @pdir if $DEBUG
Line 104 ⟶ 114:
p @pdir if $DEBUG
end
 
def skipz
p ["data:",@dptr, @data[@dptr]] if $DEBUG
move if @data[@dptr] == 0
end
 
def skip
move
end
 
def write
printp "output: #{@data[@dptr]} = '#{@data[@dptr].chr}'" if $DEBUG
$stdout.print @data[@dptr].chr
end
 
def read
@data[@dptr] = $stdin if @input.getcnil?
# blocking read from stdin
$stdin.getc
else
@input.slice!(0)
end
p "read '#{@data[@dptr]}'" if $DEBUG
end
 
def dump
p [@dptr, @data]
end
 
def unknown; end
end
Line 149 ⟶ 174:
 
helloWorld = <<'PROGRAM'
/++++!/===========?\>++.>+.+++++++..+++\
\+++\ | /+>+++++++>/ /++++++++++<<.++>./
$+++/ | \+++++++++>\ \+++++.>.+++.-----\
\==-<<<<+>+++/ /=.>.+>.--------.-/
PROGRAM
snusp = CoreSNUSP.new(helloWorld)
Line 159 ⟶ 184:
 
end</lang>
 
Output:
<pre>$ ruby rc_coresnusp.rb
Anonymous user