Sha256: af086b8e23d36795fa39552b4fa335085876da3282905c5f87c3b52415d3e29d

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

require_relative 'base_interpreter'

module Esolang
  module Interpreters
    # The Smallfuck class represents an interpreter for the Smallfuck esoteric programming language.
    class Smallfuck < BaseInterpreter
      # Initializes a new instance of the Smallfuck interpreter.
      #
      # @param code [String] The Smallfuck code to interpret.
      # @param tape [String] The initial tape state for the Smallfuck program.
      def initialize(code, tape)
        super(code.gsub(/[^<>\*\[\]]/, ''))
        @tape = tape.chars.map(&:to_i)
      end

      # Executes the interpretation of the Smallfuck code.
      #
      # @return [String] The result of the Smallfuck interpretation.
      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

      private

      # Checks if the interpreter is still running (code execution is not completed).
      #
      # @return [Boolean] Returns true if the interpreter is still running, otherwise false.
      def running?
        @tape_pointer < @tape.length &&
        @tape_pointer >= 0 &&
        super
      end

      # Retrieves or updates the value of the current bit on the tape.
      #
      # @param new_value [Integer] The new value for the current bit (optional).
      # @return [Integer] The value of the current bit.
      def current_bit(new_value = nil)
        @tape[@tape_pointer] = new_value unless new_value.nil?

        @tape[@tape_pointer]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
esolang-0.1.4 lib/interpreters/smallfuck_interpreter.rb
esolang-0.1.3 lib/interpreters/smallfuck_interpreter.rb
esolang-0.1.2 lib/interpreters/smallfuck_interpreter.rb