|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
# ==============================================================================
#
# RubyFORTH -- Copyright (C) 2007-8, Marc Simpson (GPL).
#
# Stack module
#
# ==============================================================================
class Stack
def initialize(size)
@contents = Array.new(size)
@offset = -1
end
def nth(n)
@contents[@offset - n]
end
def tos
if @offset < 0
puts "Error: stack underflow"
false
else
@contents[@offset]
end
end
def push(n)
@offset += 1
@contents[@offset] = n
end
def pop
if @offset < 0
puts "Error: stack underflow"
@offset = -1
throw("toplevel")
return
end
@offset -= 1
@contents[@offset + 1]
end
def _contents
0.upto(@offset) do |i|
elt = @contents[i]
elt = "'#{elt}'" if elt.class == String
print "#{elt} "
end
print "\n"
end
def depth
@offset + 1
end
def contents
print "<#{@offset + 1}> "
self._contents
end
def swap
x = self.pop ; y = self.pop
self.push(x) ; self.push(y)
end
def reset
@offset = -1
end
def each
0.upto(@offset) { |i| yield @contents[i] }
end
end