Advertisement
Guest User

Kenkeiras - Esoprint.py

a guest
Jul 27th, 2010
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4.   esoprint.py, encodes in esoteric language code
  5.  
  6.   Supported languages:
  7.   - Brainfuck
  8.   - Ook!
  9.   - Whitespace
  10.   - Zombie # Blindly coded, not tested
  11.  
  12.   Maybe future supported languages:
  13.   - Tink
  14.   - Piet
  15.   - Befunge
  16.   - False
  17.   - Q-BAL # When I find a interpreter
  18.   - Malbolge # Maybe too big...
  19.   - Beatnik # Really ¿?
  20.   - Unlambda
  21.  
  22.   Not future supported languages:
  23.   # It's not a logical thing to make a computer generate this!
  24.   - Sheakspere
  25.   - Haiku
  26.   - Chef
  27.   # Make's it boring
  28.   - Whenever
  29.   # Impossible
  30.   - HQ9+
  31.   - BIT
  32.  
  33. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  34.   Copyright (C) 2010 kenkeiras <kenkeiras@gmail.com>
  35.  
  36.            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  37.                    Version 2, December 2004
  38.  
  39. Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  40.  
  41. Everyone is permitted to copy and distribute verbatim or modified
  42. copies of this license document, and changing it is allowed as long
  43. as the name is changed.
  44.  
  45.            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  46.   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  47.  
  48.  0. You just DO WHAT THE FUCK YOU WANT TO.
  49.  
  50. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  51.  
  52. Usage:
  53.  
  54. ./esoprint.py [-h]  [-l <language>] [-o <output file>] [<data> [<data> | -n [...] ] | -n]
  55. -h (--help): prints this help
  56. -l (--language): Set's output language, currently supported: Ook!, Brainf*ck, Whitespace, Zombie
  57. -o (--output) : Set's output file (to stdout by default)
  58. Whatever else is considered as data, the -n (--newline) is considered a line jump
  59.  
  60. """
  61.  
  62. # Language dictionary elements for BrainFuck-like langs
  63. # init: program initalization
  64. # inc
  65. # dec
  66. # push
  67. # pop
  68. # show
  69. # # get [Actually unused]
  70.  
  71. def usage():
  72.     import sys
  73.     print sys.argv[0]+" [-h]  [-l <language>] [-o <output file>] [<data> [<data> | -n [...] ] | -n] "
  74.     print "-h (--help): prints this help"
  75.     print "-l (--language): Set's output language"
  76.     print "-o (--output) : Set's output file (to stdout by default)"
  77.     print "Whatever else is considered as data, the -n (--newline) is considered a line jump"
  78.     print "If no data is entered, it will be received from STDIN"
  79.     print "Supported languages:"
  80.     print """   - Brainfuck
  81.   - Ook!
  82.   - Whitespace
  83.   - Zombie
  84. """
  85.  
  86. brainfuck = {
  87.     "init": "",
  88.     "inc":"+",
  89.     "dec":"-",
  90.     "push":">",
  91.     "pop":"<",
  92.     "show":".",
  93.     "get":",",    #Unused
  94.     "loop":"[",  # Unused
  95.     "endloop":"]" # Unused
  96. }
  97.  
  98. ook = {
  99.     "init": "",
  100.     "inc":"Ook. Ook. ",
  101.     "dec":"Ook! Ook! ",
  102.     "push":"Ook. Ook? ",
  103.     "pop":"Ook? Ook. ",
  104.     "show":"Ook! Ook. ",
  105.     "get":"Ook. Ook! ",    # Unused
  106.     "loop":"Ook! Ook? ",  # Unused
  107.     "endloop":"Ook? Ook! " # Unused
  108. }
  109.  
  110. # Encodes in a Zombie program
  111. def zombie_encode(data):
  112.     z = "Dundun is a zombie\n"
  113.     z += "    summon\n"
  114.     z += "    task talk\n"
  115.     for p in data.split("\n"):
  116.         z += "        say \""+str( p.replace("\"","\\\"") )+"\"\n"
  117.     z += "    animate\n"
  118.     z += "animate"
  119.  
  120.  
  121.     return z
  122.  
  123. # Encodes in a Whitespace program
  124. def whitespace_encode(data):
  125.     for c in data:
  126.  
  127.         w += "  " # Push into the stack: [Space] [Space]
  128.  
  129.         w += " " # Element, always positive [Space]
  130.  
  131.         # Character to binary (needed)
  132.         b = bin( ord( c ) )
  133.         w += b[ 2 : ].replace("0"," ").replace("1","\t") #   Space: 0 || Tab: 1
  134.         w += "\n" # Element end: [LF]
  135.  
  136.         w += "\t\n  " # Output stack top: [Tab][LF][Space][Space]
  137.  
  138.     w += "\n\n\n" # Ending [LF][LF]
  139.     return w
  140.  
  141. # Encodes in a Brainf*ck-like language
  142. def stack_encode(lang, data):
  143.     s = lang['init']
  144.     n = 0
  145.     for c in data:
  146.         i = ord( c )
  147.         while ( n > i ):
  148.             s += lang['dec']
  149.             n -= 1
  150.  
  151.         while ( n < i ):
  152.             s += lang['inc']
  153.             n += 1
  154.  
  155.         s += lang['show']
  156.    
  157.     return s
  158.  
  159. bf_like = {"brainfuck":brainfuck,"ook":ook}
  160. langlist = {"whitespace":whitespace_encode,"zombie":zombie_encode}
  161.  
  162. if __name__ == "__main__":
  163.     import sys
  164.     argc=len(sys.argv)
  165.     lang=""
  166.     data=""
  167.     out = ""
  168.     i=1
  169.     while (i < argc):
  170.         if (sys.argv[i] == "-l") or (sys.argv[i] == "--language"):
  171.             i += 1
  172.             lang = sys.argv[i]
  173.  
  174.         elif (sys.argv[i] == "-o") or (sys.argv[i] == "--output"):
  175.             i += 1
  176.             out = sys.argv[i]
  177.  
  178.         elif (sys.argv[i] == "-h") or (sys.argv[i] == "--help"):
  179.             usage()
  180.             sys.exit(0)
  181.  
  182.         elif (sys.argv[i] == "-n") or (sys.argv[i] == "--newline"):
  183.             data += "\n"
  184.  
  185.         else:
  186.             data = (data+" "+sys.argv[i]).strip()
  187.        
  188.         i+=1
  189.     if (lang == ""):
  190.         lang = raw_input("Select language: ")
  191.  
  192.     lang = lang.lower()
  193.  
  194.     if (lang not in langlist) and (lang not in bf_like):
  195.         print "Unknown language (lenguaje desconocido)"
  196.         sys.exit(1)
  197.  
  198.     if (data == ""):
  199.         for line in sys.stdin:
  200.             data += line
  201.  
  202.     if (data == "\n"):
  203.         print "Sin entrada, saliendo..."
  204.         sys.exit(0)
  205.  
  206.     if (lang in langlist):
  207.         result = langlist[lang](data)
  208.     else:
  209.         result = stack_encode(bf_like[lang],data)
  210.  
  211.     if (out == ""):
  212.         print result
  213.     else:
  214.         f = open(out,"w")
  215.         f.write(result)
  216.         f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement