Class: Esolang::Interpreters::Paintfuck

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(code, iterations, width, height) ⇒ Paintfuck

Initializes a new instance of the Paintfuck interpreter.

Parameters:

  • code (String)

    The Paintfuck code to interpret.

  • iterations (Integer)

    The number of iterations for the Paintfuck program.

  • width (Integer)

    The width of the Paintfuck canvas.

  • height (Integer)

    The height of the Paintfuck canvas.



15
16
17
18
19
20
21
22
# File 'lib/interpreters/paintfuck_interpreter.rb', line 15

def initialize(code, iterations, width, height)
  super(code.gsub(/[^nesw\*\[\]]/, ''))
  @iterations = iterations
  @width = width
  @height = height
  @data_grid = generate_zero_grid
  @grid_pointer = [0, 0]
end

Instance Method Details

#runString

Executes the interpretation of the Paintfuck code.

Returns:

  • (String)

    The result of the Paintfuck interpretation.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/interpreters/paintfuck_interpreter.rb', line 27

def run
  while running? do
    case command
    when 'n' then move_up
    when 'e' then move_right
    when 's' then move_down
    when 'w' then move_left
    when '*' then flip
    when '[' then loop_begin
    when ']' then loop_end
    end

    @code_pointer += 1
    @iterations -= 1
  end

  output
end