Skip to content

Instantly share code, notes, and snippets.

@vegitron
Created September 14, 2016 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vegitron/0ae01edd2da7bf32c890b602231208f0 to your computer and use it in GitHub Desktop.
Save vegitron/0ae01edd2da7bf32c890b602231208f0 to your computer and use it in GitHub Desktop.
import re
import sys
class BFBuild(object):
byte_var_table = {}
current_var_index = 0
python = ""
def __init__(self, python):
self.python = python
def process_line(self, line):
if not re.match("[^ ]", line):
return
matches = re.match("print '(.*)'", line)
if matches:
self.process_print_static_string(line)
return
matches = re.match("print (.+)", line)
if matches:
self.process_print_variable(line)
return
matches = re.match("([^ ]+)[ ]*=[ ]*'(.)'", line)
if matches:
self.process_set_char_variable(line)
return
print "Line: ", line
raise Exception("I only know a little bit of python: print '<string literal>', <variable_name> = '<one char>', print <variable_name>. Sorry!")
def process_print_variable(self, line):
matches = re.match("print (.*)", line)
variable_name = matches.group(1)
if variable_name not in self.byte_var_table:
raise Exception("Undefined variable: %s" % variable_name)
self.emit_move_to_var_index(self.byte_var_table[variable_name])
self.emit_print_current_index()
self.emit_print_string("\n")
def process_set_char_variable(self, line):
matches = re.match("([^ ]+)[ ]*=[ ]*'(.)", line)
value = matches.group(1)
variable_name = matches.group(1)
variable_value = matches.group(2)
if not variable_name in self.byte_var_table:
# Intentionally leaving cell 0 empty
self.byte_var_table[variable_name] = len(self.byte_var_table)+1
self.emit_move_to_var_index(self.byte_var_table[variable_name])
self.emit_zero_current_index()
self.emit_set_current_index_value(ord(variable_value))
def process_print_static_string(self, line):
matches = re.match("print '(.*)'", line)
value = matches.group(1)
self.emit_print_string(value)
self.emit_print_string("\n")
def emit_print_string(self, value):
self.emit_move_to_var_index(0)
for char in value:
next_ord = ord(char)
self.emit_zero_current_index()
self.emit_set_current_index_value(next_ord)
self.emit_print_current_index()
def emit_zero_current_index(self):
print "[-] Set the current index to 0"
def emit_set_current_index_value(self, value):
for i in range(value):
sys.stdout.write("+")
print " Setting value to %s" % value
def emit_move_to_var_index(self, index):
if index > self.current_var_index:
for i in range(index - self.current_var_index):
sys.stdout.write(">")
print "Move to variable index %s" % index
elif index < self.current_var_index:
for i in range(self.current_var_index - index):
sys.stdout.write("<")
print "Move to variable index %s" % index
else:
print "noop: Already at variable index %s" % index
self.current_var_index = index
def emit_print_current_index(self):
print "."
def emit_bf(self):
for line in self.python.split("\n"):
self.process_line(line)
s1 = """
var1 = 'H'
var2 = 'e'
var3 = 'l'
var4 = 'l'
ovariable = 'o'
print var1
print var2
print var3
print var4
print ovariable
print 'This is a literal'
var1='m'
var2='o'
var3='d'
var4='i'
ovariable='t'
print var1
print var2
print var3
print var4
print ovariable
"""
BFBuild(s1).emit_bf()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment