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