Class: Rbfunge::Memory_Stack
- Inherits:
-
Object
- Object
- Rbfunge::Memory_Stack
- Defined in:
- lib/rbfunge/memory_stack.rb
Overview
memory stack for Befunge programs
Instance Method Summary collapse
-
#dup ⇒ Object
duplicate the top item.
-
#initialize ⇒ Memory_Stack
constructor
initialize the memory.
-
#pop ⇒ Object
pop the first top element off the stack.
-
#push(value) ⇒ Object
push an element onto the stack.
-
#swap ⇒ Object
swap the top two items in the stack.
Constructor Details
#initialize ⇒ Memory_Stack
initialize the memory
6 7 8 |
# File 'lib/rbfunge/memory_stack.rb', line 6 def initialize @stack = [] end |
Instance Method Details
#dup ⇒ Object
duplicate the top item
29 30 31 32 33 |
# File 'lib/rbfunge/memory_stack.rb', line 29 def dup top = pop push top push top end |
#pop ⇒ Object
pop the first top element off the stack
11 12 13 |
# File 'lib/rbfunge/memory_stack.rb', line 11 def pop return @stack.empty? ? 0 : @stack.pop end |
#push(value) ⇒ Object
push an element onto the stack
16 17 18 |
# File 'lib/rbfunge/memory_stack.rb', line 16 def push(value) @stack.push value end |
#swap ⇒ Object
swap the top two items in the stack
21 22 23 24 25 26 |
# File 'lib/rbfunge/memory_stack.rb', line 21 def swap first = pop second = pop push first push second end |