class Brainclusterfuck::Lexer

Attributes

instruction_chars[R]
tokens[R]

Public Class Methods

new(instruction_string) click to toggle source
# File lib/brainclusterfuck/lexer.rb, line 5
def initialize(instruction_string)
  @instruction_chars = sanitize(instruction_string).split('').freeze
  @tokens = tokenize(@instruction_chars)
end

Private Instance Methods

sanitize(string) click to toggle source
# File lib/brainclusterfuck/lexer.rb, line 11
def sanitize(string)
  string.gsub(/[^\[\]+\-><\.]/, '')
end
tokenize(instruction_chars) click to toggle source
# File lib/brainclusterfuck/lexer.rb, line 15
def tokenize(instruction_chars)
  instruction_chars.map do |c|
    case c
    when '+'
      :v_incr
    when '-'
      :v_decr
    when '>'
      :p_incr
    when '<'
      :p_decr
    when '.'
      :print
    when '['
      :loop_start
    when ']'
      :loop_end
    else
      raise 'wtf?'
    end
  end
end