Execute HQ9+/Python: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
imported>Katsumi
No edit summary
 
(7 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{collection|RCHQ9+}}
{{collection|RCHQ9+}}{{implementation|HQ9+}}


This example implements only 'H','Q' and '9' because the author did not explained what '+' do.
This [[Python]] example implements 'H', 'Q', '9' and '+'.


<lang python>import sys
<syntaxhighlight lang="python">
import sys


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


def quine():
def quine():
print src
print(src)


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


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


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


print "No more bottles of beer on the wall"
print("No more bottles of beer on the wall")
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("Go to the store and buy some more")
print "99 bottles of beer on the wall."
print("99 bottles of beer on the wall.")
print
print()

def incr():
acc +=1


if len(sys.argv) != 2:
if len(sys.argv) != 2:
print "Usage: ./hq9p.py script.hq9"
print("Usage: ./hq9p.py script.hq9")
sys.exit(1)
sys.exit(1)
else:
else:
Line 46: Line 50:
s = open(f,"r")
s = open(f,"r")
except IOError, e:
except IOError, e:
print "Can't open file: " + str(e.args[1])
print("Can't open file: " + str(e.args[1]))
sys.exit(1)
sys.exit(1)


acc = 0
src = s.read()
src = s.read()

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


for i in src.lower():
for i in src.lower():
if i == 'h':
if i in dispatch:
hello()
dispatch[i]()
</syntaxhighlight>
elif i == 'q':
quine()
elif i == '9':
bottles()
else:
pass # Comment...
</lang>

Latest revision as of 14:36, 24 September 2023

Execute HQ9+/Python is part of RCHQ9+. You may find other members of RCHQ9+ at Category:RCHQ9+.
Execute HQ9+/Python is an implementation of HQ9+. Other implementations of HQ9+.

This Python example implements 'H', 'Q', '9' and '+'.

import sys

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: ./hq9p.py script.hq9")
    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 = {
     'h': hello,
     'q': quine,
     '9': bottles,
     '+': incr
     }

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