Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 1 commit
  • 1 file changed
  • 0 comments
  • 1 contributor

Showing 1 changed file with 46 additions and 0 deletions. Show diff stats Hide diff stats

  1. 46  plugins/bf.py
46  plugins/bf.py
... ...
@@ -0,0 +1,46 @@
  1
+from __future__ import division
  2
+from __future__ import print_function
  3
+from __future__ import unicode_literals
  4
+
  5
+def bf(message_data, bot):
  6
+    jumps_forward = {}
  7
+    jumps_backward = {}
  8
+    jump_stack = []
  9
+    program = message_data["parsed"]
  10
+    for command_index in range(len(program)):
  11
+        command = program[command_index]
  12
+        if command == "[":
  13
+            jump_stack.append(command_index)
  14
+        if command == "]":
  15
+            left = jump_stack.pop()
  16
+            jumps_forward[left] = command_index
  17
+            jumps_backward[command_index] = left
  18
+    command_index = 0
  19
+    cells = {0: 0}
  20
+    current_cell = 0
  21
+    output_buffer = ""
  22
+    while command_index < len(program):
  23
+        if program[command_index] == ">":
  24
+            current_cell += 1
  25
+            if current_cell not in cells:
  26
+                cells[current_cell] = 0
  27
+        if program[command_index] == "<":
  28
+            current_cell -= 1
  29
+            if current_cell not in cells:
  30
+                cells[current_cell] = 0
  31
+        if program[command_index] == "+":
  32
+            cells[current_cell] += 1
  33
+        if program[command_index] == "-":
  34
+            cells[current_cell] -= 1
  35
+        if program[command_index] == ".":
  36
+            output_buffer += unichr(cells[current_cell])
  37
+        if program[command_index] == "[" and cells[current_cell] == 0:
  38
+            command_index = jumps_forward[command_index]
  39
+        if program[command_index] == "]" and cells[current_cell] != 0:
  40
+            command_index = jumps_backward[command_index]
  41
+        command_index += 1
  42
+    return output_buffer
  43
+
  44
+commands = {"bf": bf}
  45
+triggers = []
  46
+

No commit comments for this range

Something went wrong with that request. Please try again.