Class: Esolang::Interpreters::Boolfuck

Inherits:
BaseInterpreter show all
Defined in:
lib/interpreters/boolfuck_interpreter.rb

Overview

The Boolfuck class represents an interpreter for the Boolfuck esoteric programming language.

Instance Method Summary collapse

Constructor Details

#initialize(code, input = '') ⇒ Boolfuck

Initializes a new instance of the Boolfuck interpreter.

Parameters:

  • code (String)

    The Boolfuck code to interpret.

  • input (String) (defaults to: '')

    The input for the Boolfuck program (optional).



13
14
15
16
17
18
# File 'lib/interpreters/boolfuck_interpreter.rb', line 13

def initialize(code, input = '')
  super(code.gsub(/[^,\.;<>\+\[\]]/, ''))
  @input = chars_to_bits(input)
  @output = []
  @tape = Hash.new(0)
end

Instance Method Details

#runString

Executes the interpretation of the Boolfuck code.

Returns:

  • (String)

    The result of the Boolfuck interpretation.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/interpreters/boolfuck_interpreter.rb', line 23

def run
  while running? do
    case command
    when ',' then input_to_tape
    when ';' then tape_to_output_array
    when '>' then move_right
    when '<' then move_left
    when '+' then flip
    when '[' then loop_begin
    when ']' then loop_end
    end

    @code_pointer += 1
  end

  translate_output_bits_to_chars
end