Class: Brainfuck

Inherits:
Object
  • Object
show all
Defined in:
lib/r-fxxk.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Brainfuck

Returns a new instance of Brainfuck.



2
3
4
5
6
# File 'lib/r-fxxk.rb', line 2

def initialize(options = {})
  self.class.default_mapping.each do |key, default|
    operations[key] = options.has_key?(key) ? options[key] : default
  end
end

Class Method Details

.bf_mappingObject



8
9
10
# File 'lib/r-fxxk.rb', line 8

def self.bf_mapping
  @bf_operations ||= {nxt: '>', prv: '<', inc: '+', dec: '-', put: '.',  get: ',', opn: '[', cls: ']' }
end

.default_mappingObject



12
13
14
# File 'lib/r-fxxk.rb', line 12

def self.default_mapping
  @default_mapping ||= bf_mapping.clone
end

Instance Method Details

#compile(src) ⇒ Object



26
27
28
# File 'lib/r-fxxk.rb', line 26

def compile(src)
  Brainfuck.new.translate(self, src)
end

#fuck(src) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/r-fxxk.rb', line 48

def fuck(src)
  src = compile(src)
  ptr = 0
  cur = 0
  cell = Array.new(3000) { 0 }
  output = []
  inv = self.class.bf_mapping.invert
  reg = Regexp.compile "(#{self.class.bf_mapping.values.map{|v| Regexp.quote(v) }.join('|')})"
  while matches = reg.match(src, cur)
    next_cur = nil
    case inv[matches[1]]
    when :nxt
      ptr += 1
    when :prv
      ptr -= 1
    when :inc
      cell[ptr] += 1
    when :dec
      cell[ptr] -= 1
    when :put
      output << cell[ptr].chr
    when :get
    when :opn
      if cell[ptr] == 0
        open_count = 1
        buf_cur = cur
        while open_count > 0
          open_count.times do
            next_cur = src.index(self.class.bf_mapping[:cls], buf_cur)
            open_count = src[buf_cur+1..next_cur].count(self.class.bf_mapping[:opn])
            buf_cur = next_cur
          end
        end
        next_cur = next_cur + 1
      end
    when :cls
      close_count = 1
      buf_cur = cur
      while close_count > 0
        close_count.times do
          next_cur = src.rindex(self.class.bf_mapping[:opn], buf_cur)
          close_count = src[next_cur..buf_cur-1].count(self.class.bf_mapping[:cls])
          buf_cur = next_cur
        end
      end
    end
    cur = next_cur || src.index(reg, cur) + matches[1].length
  end
  output.join
end

#hello_worldObject



44
45
46
# File 'lib/r-fxxk.rb', line 44

def hello_world
  translate(Brainfuck, '>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]<.>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.[-]>++++++++[<++++>-]<+.[-]++++++++++.')
end

#operationsObject



16
17
18
# File 'lib/r-fxxk.rb', line 16

def operations
  @operations ||= {}
end

#translate(other, src) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/r-fxxk.rb', line 30

def translate(other, src)
  other = other.new if other.kind_of?(Class)
  cur = 0
  inv = other.operations.invert
  reg = Regexp.compile "(#{other.operations.values.map{|v| Regexp.quote(v) }.join('|')})"
  dst = ''
  while matches = reg.match(src, cur)
    op = inv[matches[1]]
    dst += operations[op]
    cur = src.index(reg, cur) + matches[1].length
  end
  dst
end