Skip to content

Instantly share code, notes, and snippets.

@fis
Last active December 26, 2015 13:39
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 fis/7159823 to your computer and use it in GitHub Desktop.
Save fis/7159823 to your computer and use it in GitHub Desktop.
Very simple brainfuck-to-MarioLANG translation construction. Assumes that walking over "s when there are explicit arrows is safe. Seems to be the case for the single extant MarioLANG interpreter.
##########################
###
### Severely updated version!
### (now says "1 bottle" and
### contains no extra "0" verse)
###
##########################
### 99 Bottles of Beer ###
### coded in Brainfuck ###
### with explanations ###
##########################
#
# This Bottles of Beer program
# was written by Andrew Paczkowski
# Coder Alias: thepacz
# three_halves_plus_one@yahoo.com
#####
> 0 in the zeroth cell
+++++++>++++++++++[<+++++>-] 57 in the first cell or "9"
+++++++>++++++++++[<+++++>-] 57 in second cell or "9"
++++++++++ 10 in third cell
>+++++++++ 9 in fourth cell
##########################################
### create ASCII chars in higher cells ###
##########################################
>>++++++++[<++++>-] " "
>++++++++++++++[<+++++++>-] b
+>+++++++++++[<++++++++++>-] o
++>+++++++++++++++++++[<++++++>-] t
++>+++++++++++++++++++[<++++++>-] t
>++++++++++++[<+++++++++>-] l
+>++++++++++[<++++++++++>-] e
+>+++++++++++++++++++[<++++++>-] s
>++++++++[<++++>-] " "
+>+++++++++++[<++++++++++>-] o
++>++++++++++[<++++++++++>-] f
>++++++++[<++++>-] " "
>++++++++++++++[<+++++++>-] b
+>++++++++++[<++++++++++>-] e
+>++++++++++[<++++++++++>-] e
>+++++++++++++++++++[<++++++>-] r
>++++++++[<++++>-] " "
+>+++++++++++[<++++++++++>-] o
>+++++++++++[<++++++++++>-] n
>++++++++[<++++>-] " "
++>+++++++++++++++++++[<++++++>-] t
++++>++++++++++[<++++++++++>-] h
+>++++++++++[<++++++++++>-] e
>++++++++[<++++>-] " "
++>+++++++++++++[<+++++++++>-] w
+>++++++++++++[<++++++++>-] a
>++++++++++++[<+++++++++>-] l
>++++++++++++[<+++++++++>-] l
>+++++[<++>-] LF
++>+++++++++++++++++++[<++++++>-] t
+>++++++++++++[<++++++++>-] a
+++>+++++++++++++[<++++++++>-] k
+>++++++++++[<++++++++++>-] e
>++++++++[<++++>-] " "
+>+++++++++++[<++++++++++>-] o
>+++++++++++[<++++++++++>-] n
+>++++++++++[<++++++++++>-] e
>++++++++[<++++>-] " "
>++++++++++[<++++++++++>-] d
+>+++++++++++[<++++++++++>-] o
++>+++++++++++++[<+++++++++>-] w
>+++++++++++[<++++++++++>-] n
>++++++++[<++++>-] " "
+>++++++++++++[<++++++++>-] a
>+++++++++++[<++++++++++>-] n
>++++++++++[<++++++++++>-] d
>++++++++[<++++>-] " "
++>+++++++++++[<++++++++++>-] p
+>++++++++++++[<++++++++>-] a
+>+++++++++++++++++++[<++++++>-] s
+>+++++++++++++++++++[<++++++>-] s
>++++++++[<++++>-] " "
+>+++++++++++++[<++++++++>-] i
++>+++++++++++++++++++[<++++++>-] t
>++++++++[<++++>-] " "
+>++++++++++++[<++++++++>-] a
>+++++++++++++++++++[<++++++>-] r
+>+++++++++++[<++++++++++>-] o
>+++++++++++++[<+++++++++>-] u
>+++++++++++[<++++++++++>-] n
>++++++++++[<++++++++++>-] d
>+++++[<++>-] LF
+++++++++++++ CR
[<]>>>> go back to fourth cell
#################################
### initiate the display loop ###
#################################
[ loop
< back to cell 3
[ loop
[>]<< go to last cell and back to LF
.. output 2 newlines
[<]> go to first cell
###################################
#### begin display of characters###
###################################
#
#.>.>>>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>
#X X b o t t l e s o f b e e r
#.>.>.>.>.>.>.>.>.>.>.>.
#o n t h e w a l l N
#[<]> go to first cell
#.>.>>>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>>>>>>>>>>>>>.>
#X X b o t t l e s o f b e e r N
#.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>
#t a k e o n e d o w n a n d p a s s
#.>.>.>.>.>.>.>.>.>.
#i t a r o u n d N
#####
[<]>> go to cell 2
- subtract 1 from cell 2
< go to cell 1
########################
### display last line ##
########################
#
#.>.>>>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>
#X X b o t t l e s o f b e e r
#.>.>.>.>.>.>.>.>.>.>.
#o n t h e w a l l
#####
[<]>>>- go to cell 3/subtract 1
] end loop when cell 3 is 0
++++++++++ add 10 to cell 3
<++++++++++ back to cell 2/add 10
<- back to cell 1/subtract 1
[>]<. go to last line/carriage return
[<]> go to first line
########################
### correct last line ##
########################
#
#.>.>>>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>
#X X b o t t l e s o f b e e r
#.>.>.>.>.>.>.>.>.>.>.
#o n t h e w a l l
#####
[<]>>>>- go to cell 4/subtract 1
] end loop when cell 4 is 0
##############################################################
### By this point verses 99\10 are displayed but to work ###
### with the lower numbered verses in a more readable way ###
### we initiate a new loop for verses 9{CODE} that will not ###
### use the fourth cell at all ###
##############################################################
+ add 1 to cell four (to keep it non\zero)
<-- back to cell 3/subtract 2
[ loop
[>]<< go to last cell and back to LF
.. output 2 newlines
[<]> go to first cell
###################################
#### begin display of characters###
###################################
#
#>.>>>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>
# X b o t t l e s o f b e e r
#.>.>.>.>.>.>.>.>.>.>.>.
#o n t h e w a l l N
#[<]> go to first cell
#>.>>>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>>>>>>>>>>>>>.>
# X b o t t l e s o f b e e r N
#.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>
#t a k e o n e d o w n a n d p a s s
#.>.>.>.>.>.>.>.>.>.
#i t a r o u n d N
#####
[<]>> go to cell 2
- subtract 1 from cell 2
########################
### display last line ##
########################
#
#.>>>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>
#X b o t t l e s o f b e e r
#.>.>.>.>.>.>.>.>.>.>.
#o n t h e w a l l
#####
[<]>>>- go to cell 3/subtract 1
] end loop when cell 3 is 0
+ add 1 to cell 3 to keep it non\zero
[>]<. go to last line/carriage return
[<]> go to first line
########################
### correct last line ##
########################
#
#>.>>>.>.>.>.>.>.>.>>.>.>.>.>.>.>.>.>.>
# X b o t t l e o f b e e r
#.>.>.>.>.>.>.>.>.>.>.<<<<.
#o n t h e w a l l
#####
[>]<< go to last cell and back to LF
.. output 2 newlines
[<]> go to first line
#########################
### the final verse ##
#########################
#
#>.>>>.>.>.>.>.>.>.>>.>.>.>.>.>.>.>.>.>
# X b o t t l e o f b e e r
#.>.>.>.>.>.>.>.>.>.>.>.
#o n t h e w a l l N
#[<]> go to first cell
#>.>>>.>.>.>.>.>.>.>>.>.>.>.>.>.>.>.>>>>>>>>>>>>>.>
# X b o t t l e o f b e e r N
#.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>
#t a k e o n e d o w n a n d p a s s
#.>.>.>.>.>.>.>.>.>.
#i t a r o u n d N
#[>]< go to last line
#<<<.<<.<<<.
# n o
#[<]>>>> go to fourth cell
#>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>
# b o t t l e s o f b e e r
#.>.>.>.>.>.>.>.>.>.>.>.
#o n t h e w a l l N
#####fin##
.)+++++++)++++++++++[! [<>+++++++)++++++++++[! [<>++++++++++)+++++++++))++++++++[! [<>)++++++++++++++[! [<>+)+++++++++++[! [<>++)+++++++++++++++++++[! [<>++)+++++++++++++++++++[! [<>)++++++++++++[! [<>+)++++++++++[! [<>+)+++++++++++++++++++[! [<>)++++++++[! [<>+)+++++++++++[! [<>++)++++++++++[! [<>)++++++++[! [<>)++++++++++++++[! [<>+)++++++++++[! [<>+)++++++++++[! [<>)+++++++++++++++++++[! [<>)++++++++[! [<>+)+++++++++++[! [<>)+++++++++++[! [<>)++++++++[! [<>++)+++++++++++++++++++[! [<>++++)++++++++++[! [<>+)++++++++++[! [<>)++++++++[! [<>++)+++++++++++++[! [<>+)++++++++++++[! [<>)++++++++++++[! [<>)++++++++++++[! [<>)+++++[! [<>++)+++++++++++++++++++[! [<>+)++++++++++++[! [<>+++)+++++++++++++[! [<>+)++++++++++[! [<>)++++++++[! [<>+)+++++++++++[! [<>)+++++++++++[! [<>+)++++++++++[! [<>)++++++++[! [<>)++++++++++[! [<>+)+++++++++++[! [<>++)+++++++++++++[! [<>)+++++++++++[! [<>)++++++++[! [<>+)++++++++++++[! [<>)+++++++++++[! [<>)++++++++++[! [<>)++++++++[! [<>++)+++++++++++[! [<>+)++++++++++++[! [<>+)+++++++++++++++++++[! [<>+)+++++++++++++++++++[! [<>)++++++++[! [<>+)+++++++++++++[! [<>++)+++++++++++++++++++[! [<>)++++++++[! [<>+)++++++++++++[! [<>)+++++++++++++++++++[! [<>+)+++++++++++[! [<>)+++++++++++++[! [<>)+++++++++++[! [<>)++++++++++[! [<>)+++++[! [<>+++++++++++++[! [<>))))[! [<>+(--[! [<>+[! [<>(.[! [<>)).))).).).).).).).)).).).).).).).).).).).).).).).).).).).).((((.[! [<>((..[! [<>)).))).).).).).).).)).).).).).).).).).).).).).).).).).).).).).[! [<>)).))).).).).).).).)).).).).).).).).))))))))))))).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).[! [<>((((.((.(((.[! [<>))))).).).).).).).).).).).).).).).).).).).).).).).).).).).).).
=====================#=========""===================#=========""===============================#========""================#===========""==============#==============""=======================#==========""=======================#==========""==============#=============""=============#==============""======================#==========""==========#========""==============#==============""==============#==============""==========#========""================#===========""=============#==============""=============#==============""=====================#==========""==========#========""==============#==============""=============#==============""==========#========""=======================#==========""================#==============""=============#==============""==========#========""=================#=============""===============#============""==============#=============""==============#=============""=======#======""=======================#==========""===============#============""==================#============""=============#==============""==========#========""==============#==============""=============#==============""=============#==============""==========#========""============#==============""==============#==============""=================#=============""=============#==============""==========#========""===============#============""=============#==============""============#==============""==========#========""===============#==============""===============#============""======================#==========""======================#==========""==========#========""================#============""=======================#==========""==========#========""===============#============""=====================#==========""==============#==============""===============#=============""=============#==============""============#==============""=======#======""==============#==""=====#==================================================================================================================================================================================================================================================================================================================================================================================================================""=====#=========================================================================================================================================================================================================================================================================================""==#==""===#==""==================================================================#==""=====#==""===============================================================#==""=====================================================================================================================#==""=============#==""==============================================================
>(+++++)-[!! >(+++++)-[!! >(++++)-[!! >(+++++++)-[!! >(++++++++++)-[!! >(++++++)-[!! >(++++++)-[!! >(+++++++++)-[!! >(++++++++++)-[!! >(++++++)-[!! >(++++)-[!! >(++++++++++)-[!! >(++++++++++)-[!! >(++++)-[!! >(+++++++)-[!! >(++++++++++)-[!! >(++++++++++)-[!! >(++++++)-[!! >(++++)-[!! >(++++++++++)-[!! >(++++++++++)-[!! >(++++)-[!! >(++++++)-[!! >(++++++++++)-[!! >(++++++++++)-[!! >(++++)-[!! >(+++++++++)-[!! >(++++++++)-[!! >(+++++++++)-[!! >(+++++++++)-[!! >(++)-[!! >(++++++)-[!! >(++++++++)-[!! >(++++++++)-[!! >(++++++++++)-[!! >(++++)-[!! >(++++++++++)-[!! >(++++++++++)-[!! >(++++++++++)-[!! >(++++)-[!! >(++++++++++)-[!! >(++++++++++)-[!! >(+++++++++)-[!! >(++++++++++)-[!! >(++++)-[!! >(++++++++)-[!! >(++++++++++)-[!! >(++++++++++)-[!! >(++++)-[!! >(++++++++++)-[!! >(++++++++)-[!! >(++++++)-[!! >(++++++)-[!! >(++++)-[!! >(++++++++)-[!! >(++++++)-[!! >(++++)-[!! >(++++++++)-[!! >(++++++)-[!! >(++++++++++)-[!! >(+++++++++)-[!! >(++++++++++)-[!! >(++++++++++)-[!! >(++)-[!! >([!! >([! [<>++++++++++(++++++++++(-[! [<>(.[! [<>).).))).).).).).).).).).).).).).).).).).).).).).).).).).).).).[! [<>))))-[!! >[! [<>((..[! [<>)).))).).).).).).).).).).).).).).).).).).).).).).).).).).).).).[! [<>)).))).).).).).).).).).).).).).).).).))))))))))))).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).[! [<>))-.))).).).).).).).).).).).).).).).).).).).).).).).).).).).).[! [<>)))-[!! >)[!! >([!! >)[!! >([!! >([!! >)[!! >([!!
"=========## "=========## "========## "===========## "==============## "==========## "==========## "=============## "==============## "==========## "========## "==============## "==============## "========## "===========## "==============## "==============## "==========## "========## "==============## "==============## "========## "==========## "==============## "==============## "========## "=============## "============## "=============## "=============## "======## "==========## "============## "============## "==============## "========## "==============## "==============## "==============## "========## "==============## "==============## "=============## "==============## "========## "============## "==============## "==============## "========## "==============## "============## "==========## "==========## "========## "============## "==========## "========## "============## "==========## "==============## "=============## "==============## "==============## "======## "==## "==#==============================================================================================================================================================================================================================================================================================""========================#==""===#==""===============================================================#==""======## "=#==""=====#==""================================================================#==""======================================================================================================================#==""===============================================================#==""=====## "==## "==## "==## "==## "==## "==## "==##
>[! [<>((..[! [<>).).))).).).).).).).).).).).).).).).).).).).).).).).).).).).).).[! [<>).).))).).).).).).).).).).).).).).).).))))))))))))).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).).[! [<>))-(.).))).).).).).).).).).).).).).).).).).).).).).).).).).).).).[! [<>)))-[!! >)[!! >([!! >([!! >)[!! >([!! >([!! >([!! >([!!
"=#==""=====#==""=================================================================#==""=======================================================================================================================#==""==================================================================#==""=====## "==## "==## "==## "==## "==## "==## "==## "==##
>)[!! >([!! >([!! >([!! >([!!
"==## "==## "==## "==## "==##
#! /usr/bin/env python3
import sys
# parse some brainfuck
def parse(code):
ops = []
while code:
if code[0] == '[':
body, code, wasloop = parse(code[1:])
if not wasloop: raise Exception('mismatched [')
ops.append({ 'loop': body })
continue
if code[0] == ']':
return ops, code[1:], True
if code[0] in '+-<>.,':
ops.append(code[0])
code = code[1:]
return ops, '', False
code, _, wasloop = parse(sys.stdin.read())
if wasloop: raise Exception('mismatched ]')
# output some mario
mariops = { '<': '(', '>': ')', '+': '+', '-': '-', '.': '.', ',': ',' }
def mario(ops):
lines = ['', '']
for op in ops:
if type(op) is str:
lines = [lines[0] + mariops[op],
lines[1] + '='] + [l + ' ' for l in lines[2:]]
continue
submario = mario(op['loop'])
if len(lines) < 2 + len(submario):
at = len(lines[0])
lines.extend([' '*at for _ in range(len(lines), 2+len(submario))])
sublen = len(submario[0])
top = [lines[0] + '[!' + ' '*sublen + '[<>',
lines[1] + '=#' + '='*sublen + '=""',
lines[2] + ' >' + submario[0] + '[!!',
lines[3] + ' "' + submario[1] + '=##']
rest = [lines[4+i] + ' ' + subl + ' ' for i, subl in enumerate(submario[2:])]
space = [l + ' '*(sublen+5) for l in lines[2+len(submario):]]
lines = top + rest + space
return lines
for line in mario(code):
print(line)
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
++++++++++[! [<>)++.)+.+++++++..+++.)++.((+++++++++++++++.).+++.------.--------.)+.).
===========#===============================""=====================================================================
>)+++++++)++++++++++)+++)+((((-[!!
"===============================##
Copyright (c) 2013 Heikki Kallasjoki
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment