Class: Yarbf::BfInterpreter::BfCell

Inherits:
Object
  • Object
show all
Defined in:
lib/yarbf.rb

Overview

Cell of the Brainfuck tape.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position, cell_size = 8, value = 0) ⇒ BfCell

Returns a new instance of BfCell.



326
327
328
329
330
# File 'lib/yarbf.rb', line 326

def initialize(position, cell_size = 8, value = 0)
  @position = position
  @cell_size = cell_size
  @value = value
end

Instance Attribute Details

#cell_sizeObject

Returns the value of attribute cell_size.



324
325
326
# File 'lib/yarbf.rb', line 324

def cell_size
  @cell_size
end

#positionObject (readonly)

Returns the value of attribute position.



323
324
325
# File 'lib/yarbf.rb', line 323

def position
  @position
end

#valueObject

Returns the value of attribute value.



324
325
326
# File 'lib/yarbf.rb', line 324

def value
  @value
end

Instance Method Details

#decrease(decrement = 1, wrap_around = false) ⇒ Object

Decrease the value of a cell.

decrement

Value to decrease by. Default is 1.

wrap_around

Whether to wrap around. Default is false.



358
359
360
# File 'lib/yarbf.rb', line 358

def decrease(decrement = 1, wrap_around = false)
  self.increase(-decrement, wrap_around)
end

#increase(increment = 1, wrap_around = false) ⇒ Object

Increase the value of a cell.

increment

Value to increase by. Default is 1.

wrap_around

Whether to wrap around. Default is false.



343
344
345
346
347
348
349
350
# File 'lib/yarbf.rb', line 343

def increase(increment = 1, wrap_around = false)
  if !wrap_around &&
      (@value + increment < 0 || @value + increment >= (1 << @cell_size))
    fail 'Overflow or underflow happened while forbidden!'
  else
    @value = (@value + increment) % (1 << @cell_size)
  end
end

#inspectObject Also known as: to_s



332
333
334
# File 'lib/yarbf.rb', line 332

def inspect
  "{ pos: #{@position}, value: #{@value} }"
end