Skip to content

Instantly share code, notes, and snippets.

@mkfmnn
Created October 7, 2017 06:45
Show Gist options
  • Save mkfmnn/83ad3dd8f669b8d7b91f778828ec2419 to your computer and use it in GitHub Desktop.
Save mkfmnn/83ad3dd8f669b8d7b91f778828ec2419 to your computer and use it in GitHub Desktop.
Bubblegum compiler
#!/usr/bin/env python3
# Script for turning ascii input into valid Bubblegum programs
# https://esolangs.org/wiki/Bubblegum
import hashlib, lzma, sys, zlib
def bb96encode(code):
buf = []
a = 0
for byte in code:
digit = 96 if byte == 10 else byte - 31
assert 1 <= digit <= 96
a = 96 * a + digit
while a:
r = a % 256
buf.append(r)
a //= 256
return bytes(reversed(buf))
def deflate(data):
compress = zlib.compressobj(9, wbits=-zlib.MAX_WBITS)
deflated = compress.compress(data)
deflated += compress.flush()
return deflated
def do_lzma(data):
return lzma.compress(
code,
format=lzma.FORMAT_RAW,
filters=[{'id': lzma.FILTER_LZMA2, 'preset': 9 | lzma.PRESET_EXTREME}])
def dump(src, length=16):
result=[]
for i in range(0, len(src), length):
s = src[i:i+length]
hexa = ' '.join(["%02x" % x for x in s])
result.append("%08x: %-*s \n" % (i, length*3, hexa))
return ''.join(result)
# TODO while unlikely to arise accidentally, to be complete we should ensure
# that the result of e.g. bbase96 isn't a valid deflate or lzma2 stream; if it
# were, we would need to use a less-efficient encoding to ensure the correct
# ouput.
codecs = {'DEFLATE':deflate, 'LZMA2':do_lzma, 'BBASE96':bb96encode}
code = sys.stdin.buffer.read()
best = None
print("Input length:", len(code), "bytes", file=sys.stderr)
for name, codec in codecs.items():
opt = codec(code)
print("%s:" % name, len(opt), file=sys.stderr)
if best is None or len(opt) < len(best):
best = opt
print(dump(best), end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment