Class: Esolang::Interpreters::Smallfuck

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(code, tape) ⇒ Smallfuck

Initializes a new instance of the Smallfuck interpreter.

Parameters:

  • code (String)

    The Smallfuck code to interpret.

  • tape (String)

    The initial tape state for the Smallfuck program.



13
14
15
16
# File 'lib/interpreters/smallfuck_interpreter.rb', line 13

def initialize(code, tape)
  super(code.gsub(/[^<>\*\[\]]/, ''))
  @tape = tape.chars.map(&:to_i)
end

Instance Method Details

#runString

Executes the interpretation of the Smallfuck code.

Returns:

  • (String)

    The result of the Smallfuck interpretation.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/interpreters/smallfuck_interpreter.rb', line 21

def run
  while running? do
    case command
    when '>' then move_right
    when '<' then move_left
    when '*' then flip
    when '[' then loop_begin
    when ']' then loop_end
    end
    @code_pointer += 1
  end
  @tape.join
end