SHARE
TWEET

Bitoven Compiler

a guest Sep 20th, 2014 259 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Run with python compiler.py inputfile outputfile.
  2. letters = map(chr,range(65,91))
  3. letters.extend(map(chr, range(97,123)))
  4. import sys
  5. def cmplr(bitoven):
  6.     compiled = """
  7. import sys
  8. class Register:
  9.    def __init__(self):
  10.        self.value = 0
  11.    def add(self, num):
  12.        self.value+=num
  13.        self.value%=256
  14.    def sub(self, num):
  15.        self.value-=num
  16.        self.value%=256
  17.  
  18. def main(input_file):\n"""
  19.     compiled = compiled[1:]
  20.    
  21.     depth = 0
  22.     varlist = []
  23.     for i in filter(lambda x:(x in letters) and not (x in varlist), bitoven):
  24.         if i in varlist:
  25.             continue
  26.         varlist.append(i)
  27.         compiled+='\t'+i+" = Register()\n"
  28.     parsed_program = formatter(bitoven)
  29.     depth = 1
  30.     for i in parsed_program:
  31.         if type(i)==str:
  32.             if i[0]=='[':
  33.                 compiled+="\t"*depth+"while "+i[1]+".value:\n"
  34.                 depth+=1
  35.             elif i==']':
  36.                 depth-=1
  37.                 compiled=="\n"
  38.             elif i[0] in letters:
  39.                 variable = i[0]
  40.                 if i[1] == "?":
  41.                     compiled+="\t"*depth + variable +".value = ord(input_file.read(1))\n"
  42.                 elif i[1] == "!":
  43.                     compiled+="\t"*depth+"sys.stdout.write(chr("+variable+".value))\n"
  44.                 else:
  45.                     print "Invalid program"
  46.                     return None
  47.         elif type(i)==tuple:
  48.             variable = i[0][0]
  49.             if i[0][1] in "+-":
  50.                 funct = {'+': 'add', '-': 'sub'}[i[0][1]]
  51.                 compiled+="\t"*depth + variable+"."+funct+"("+str(i[1])+")\n"
  52.     r = """
  53. if __name__=="__main__":
  54.    input = open(sys.argv[1], "r+")
  55.    main(input)
  56. """
  57.     r=r[1:-1]
  58.     compiled+=r
  59.     return compiled
  60.  
  61. def formatter(inp):
  62.     result = []
  63.     i=0
  64.     while i < len(inp):
  65.         if inp[i]=='[':
  66.             result.append(inp[i]+inp[i+1])
  67.             i+=2
  68.             continue
  69.         elif inp[i]==']':
  70.             result.append(']')
  71.         elif inp[i] in letters:
  72.             if inp[i+1] in "?!":
  73.                 result.append(inp[i]+inp[i+1])
  74.             elif inp[i+1] in "+-":
  75.                 reg = inp[i]
  76.                 i+=1
  77.                 calculation = inp[i]
  78.                 count = 0
  79.                 while i<len(inp):
  80.                     if inp[i]==calculation:
  81.                         count+=1
  82.                         i+=1
  83.                     else:
  84.                         break
  85.                 i-=1
  86.                 result.append((reg+calculation,count))
  87.             else:
  88.                 return -1
  89.         i+=1
  90.     return result
  91.  
  92. if __name__ == "__main__":
  93.     file_in = open(sys.argv[1], "r+")
  94.     z = file_in.read()
  95.     file_out = open(sys.argv[2], "w+")
  96.     file_out.write(cmplr(z))
  97.     file_in.close()
  98.     file_out.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top