1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
local Brainfuck
do
  local _class_0
  local _base_0 = {
    maximum_runtime = 16384,
    shift_right = function(self)
      self.memory_pointer = self.memory_pointer + 1
      if self.matrix[self.memory_pointer] == nil then
        self.matrix[self.memory_pointer] = 0
      end
    end,
    shift_left = function(self)
      self.memory_pointer = self.memory_pointer - 1
      if self.matrix[self.memory_pointer] == nil then
        self.matrix[self.memory_pointer] = 0
      end
    end,
    increment = function(self)
      self.matrix[self.memory_pointer] = (self.matrix[self.memory_pointer] + 1) % 256
    end,
    decrement = function(self)
      self.matrix[self.memory_pointer] = (self.matrix[self.memory_pointer] - 1) % 256
    end,
    jez_forward = function(self)
      if self:get_value() == 0 then
        local depth = 0
        while true do
          self:move_forward()
          local _exp_0 = self:get_current_char()
          if "[" == _exp_0 then
            depth = depth + 1
          elseif "]" == _exp_0 then
            if depth == 0 then
              return nil
            else
              depth = depth - 1
            end
          elseif nil == _exp_0 then
            return nil
          end
        end
      end
    end,
    jnz_backward = function(self)
      if self:get_value() ~= 0 then
        local depth = 0
        while true do
          self:move_backward()
          local _exp_0 = self:get_current_char()
          if "[" == _exp_0 then
            if depth == 0 then
              return nil
            else
              depth = depth - 1
            end
          elseif "]" == _exp_0 then
            depth = depth + 1
          elseif "" == _exp_0 then
            return nil
          end
        end
      end
    end,
    read_input = function(self)
      local char = string.sub(self.input, 1, 1)
      if char ~= "" then
        self:set_value(string.byte(char))
      else
        self:set_value(0)
      end
      self.input = string.sub(self.input, 2)
    end,
    move_forward = function(self)
      self.code_pointer = self.code_pointer + 1
    end,
    move_backward = function(self)
      self.code_pointer = self.code_pointer - 1
    end,
    get_char = function(self, n)
      return string.sub(self.code, n, n)
    end,
    get_current_char = function(self)
      return self:get_char(self.code_pointer)
    end,
    get_value = function(self)
      return self.matrix[self.memory_pointer]
    end,
    set_value = function(self, value)
      self.matrix[self.memory_pointer] = value
    end,
    append_input = function(self, input)
      self.input = self.input .. input
    end,
    step = function(self)
      local ret = nil
      local _exp_0 = self:get_current_char()
      if "[" == _exp_0 then
        self:jez_forward()
      elseif "]" == _exp_0 then
        self:jnz_backward()
      elseif "+" == _exp_0 then
        self:increment()
      elseif "-" == _exp_0 then
        self:decrement()
      elseif "." == _exp_0 then
        ret = string.char(self:get_value())
        if self.output_callback ~= nil then
          output_callback(ret)
        end
      elseif "," == _exp_0 then
        self:read_input()
      elseif ">" == _exp_0 then
        self:shift_right()
      elseif "<" == _exp_0 then
        self:shift_left()
      elseif "" == _exp_0 then
        return false
      end
      self:move_forward()
      return ret
    end,
    run = function(self)
      local output = ""
      for i = 1, self.maximum_runtime do
        local step = self:step()
        if step == false then
          break
        elseif step ~= nil then
          output = output .. step
        end
      end
      return output
    end
  }
  _base_0.__index = _base_0
  _class_0 = setmetatable({
    __init = function(self, code, input, output_callback)
      self.code = code
      self.input = input
      self.output_callback = output_callback
      self.matrix = {
        0
      }
      self.memory_pointer = 1
      self.code_pointer = 1
    end,
    __base = _base_0,
    __name = "Brainfuck"
  }, {
    __index = _base_0,
    __call = function(cls, ...)
      local _self_0 = setmetatable({}, _base_0)
      cls.__init(_self_0, ...)
      return _self_0
    end
  })
  _base_0.__class = _class_0
  Brainfuck = _class_0
end
_BF = { }
_BF.programs = { }
addhook("always", "_BF.step_programs")
_BF.step_programs = function()
  for key, program in pairs(_BF.programs) do
    _BF.current_program = program
    local step = program.instance:step()
    if step == false then
      table.remove(_BF.programs, key)
    elseif step == "\0" then
      local write_input = (string.sub(program.output_buffer, 1, 1)) == "!"
      program.output_buffer = write_input and (string.sub(program.output_buffer, 2)) or program.output_buffer
      local ret = (loadstring(program.output_buffer))()
      if write_input then
        local _exp_0 = type(ret)
        if "string" == _exp_0 then
          program.instance:append_input(ret)
        elseif "number" == _exp_0 then
          if ret < 255.5 and ret >= -0.5 then
            program.instance:append_input(string.char(math.floor(a + 0.5)))
          else
            program.instance:append_input(tostring(ret))
          end
        else
          program.instance:append_input(tostring(ret))
        end
      end
      program.output_buffer = ""
    elseif step ~= nil then
      program.output_buffer = program.output_buffer .. step
    end
  end
end
_BF.add_program = function(program, input, callback)
  input = input == nil and "" or input
  local program_table = {
    output_buffer = "",
    instance = Brainfuck(program, input, callback)
  }
  return table.insert(_BF.programs, program_table)
end