All pastes #2097975 Raw Edit

Someone

public text v1 · immutable
#2097975 ·published 2012-01-01 16:25 UTC
rendered paste body
import sys


def happy_new_year():
    print "Happy new year"


def hello():
    print "Hello, world!"


def quine():
    print src


def bottles():
    for i in range(99, 2, -1):
        print "%d bottles of beer on the wall" % i
        print "%d bottles of beer" % i
        print "Take one down, pass it around"
        print "%d bottles of beer on the wall" % (i - 1)
        print

    print "2 bottles of beer on the wall"
    print "2 bottles of beer"
    print "Take one down, pass it around"
    print "1 bottle of beer on the wall"
    print

    print "1 bottle of beer on the wall"
    print "1 bottle of beer"
    print "Take one down, pass it around"
    print "No more bottles of beer on the wall"
    print

    print "No more bottles of beer on the wall"
    print "No more bottles of beer on the wall"
    print "Go to the store and buy some more"
    print "99 bottles of beer on the wall."
    print


def incr():
    acc += 1

if len(sys.argv) != 2:
    print "Usage: ./yhq9p.py script.yhq9"
    sys.exit(1)
else:
    f = sys.argv[1]

try:
    s = open(f, "r")
except IOError, e:
    print "Can't open file: " + str(e.args[1])
    sys.exit(1)

acc = 0
src = s.read()

# Implement interpreter using a dispatch table
dispatch = {
     'y': happy_new_year,
     'h': hello,
     'q': quine,
     '9': bottles,
     '+': incr
     }

for i in src.lower():
    if i in dispatch:
        dispatch[i]()