HQ9+

From Esolang
(Redirected from HQ9)
Jump to navigation Jump to search

HQ9+ is a joke language with four instructions:

Although the language is not of serious interest by itself, it can be useful to implement HQ9+ in a new esoteric programming language, since doing so proves that all the tasks above, except the quine, are possible. (Implementing Q proves instead that a cat program is possible.) HQ9+ was created in 2001 by Cliff L. Biffle.

Given that the original description of the language includes an example program containing a lowercase "q", it seems reasonable to assume that instructions in HQ9+ are case-insensitive.

Implementations

User:Earthrulerr was here. I made a implementation in C++ with User:imcute!


#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
using namespace std;


int main() { 
	system("clear");
	cout << "HQ9+ recreated in C++ by @earthrulerr and @imcute-aaaa" << endl;
	sleep(4);
	system("clear");
	string input;
	int acc=0;
  int loop = 1;
  while (loop == 1) {
	cin>>input;
	if (input == "h"){
	cout << "Hello World" << endl; 
	}
	else if(input == "q"){
		cout<<"Q";
	}
	else if (input == "9"){
		int bottle = 99;
    	while (bottle > 0){
	    	cout << bottle << " bottles of beer on the wall,"
	        << bottle << " bottles of beer." << endl;
	    	bottle--;
	    	cout << "Take one down and pass it around" << bottle << " " << "bottles of beer on the wall./n" << endl;
	    	if (bottle == 1){
	        break;
	    	}
	    }
	    cout << "1 bottle of beer on the wall, 1 bottle of bear." << endl;
	    cout << "Take one down and pass it around, no more bottles of beer on the wall./n" << endl;
	    cout << "No more bottles of beer on the wall, no more bottls of beer." << endl;
	    cout << "Go to the store and buy some more, 99 bottles of beer on the wall.";

    return 0;
	}
  else if(input == "+"){
      cout << ++acc << endl; 
    }
   }
}

User:Earthrulerr created a Bash implementation too:

acc=0
loop=1

while [ $loop -eq 1 ]
do
    read -p "> " input

    if [ $input == "H" ]
    then
        echo "Hello World"
    elif [ $input == "Q" ]
    then
        echo "q"
    elif [ $input == "9" ]
    then
        bottle=99
        while [ $bottle -gt 0 ]
        do
            echo "$bottle bottles of beer on the wall, $bottle bottles of beer."
            bottle=$((bottle-1))
            echo "Take one down and pass it around, $bottle bottles of beer on the wall."
            if [ $bottle -eq 1 ]
            then
                break
            fi
        done
        echo "1 bottle of beer on the wall, 1 bottle of bear."
        echo "Take one down and pass it around, no more bottles of beer on the wall."
        echo "No more bottles of beer on the wall, no more bottls of beer."
        echo "Go to the store and buy some more, 99 bottles of beer on the wall."
    elif [ $input == "+" ]
    then
        acc=$((acc+1))
        echo "$acc"
    fi
done

User:A was here and here is an implementation (in C):

#include <stdio.h>
int main()
{
    unsigned long accumulator = 0;
    char c[1000];
    for(int i=0;;i++)
    {
        scanf("%c",&c[i]);
        if(c[i]=='\n') break;
    }
    for(int i=0;c[i]!='\0';i++)
    {
        if(c[i]=='H') printf("Hello, World!\n");
        else if(c[i]=='Q') printf("%.*s", (int)sizeof c, c);
        else if(c[i]=='9')
        {
                for(int j=99;j>0;j--)
                {
                    printf("%d bottles of beer on the wall,\n%d bottles of beer.\n", j, j);
                    printf("Take one down, pass it around,\n%d bottles of beer on the wall.\n", j-1);
                }
                printf("1 bottle of beer on the wall,\n1 bottle of beer.\nTake one down, pass it around,\nno more bottles of beer on the wall.\n");
        }
        else if(c[i]=='+') accumulator++;
    }

    return 0;
}

Lanmonster fixed the Q instruction and added the + instruction. Qh4os fixed the 9 instruction. Chris Pressey also fixed the 9 instruction.

A python intepreter by User:Esolanger12345:

accumulator=0
cmds=input(">>> ")
for cmd in cmds:
    if cmd.lower() == "h":
        print("Hello, world!")
    elif cmd.lower() == "q":
        print(cmds)
    elif cmd == "9":
        for i in range(99, 2, -1):
            print(str(i)+" bottles of beer on the wall, "+str(i)+" bottles of beer.\nTake one down, pass it around, "+str(i-1)+" bottles of beer on the wall.")
        print("2 bottles of beer on the wall, 2 bottles of beer.\nTake one down, pass it around, 1 bottle of beer on the wall.")
        print("1 bottle of beer on the wall, 1 bottle of beer.\nTake one down, pass it around, No bottles of beer on the wall.")
        print("No bottles of beer on the wall, No bottles of beer.\nGo to the store, buy some more, 99 bottles of beer on the wall.")
    elif cmd == "+":
        accumulator+=1
input()

An interpreter made by User:Yes in BASIC

10 PRINT "HQ9+ INTERPETER"
20 LET A=0
30 INPUT ">>>";A$
40 FOR X=1 TO LEN(A$)
50 IF MID$(A$,X,1)="H" THEN PRINT "HELLO, WORLD!"
60 IF MID$(A$,X,1)="Q" THEN PRINT A$
70 IF MID$(A$,X,1)="9" THEN GOTO 110
80 IF MID$(A$,X,1)="+" THEN LET A=A+1
90 NEXT X
100 END
110 FOR B=99 TO 1 STEP -1
120 PRINT B;" BOTTLES OF BEER ON THE WALL,":PRINT B;" BOTTLES OF BEER":PRINT "TAKE ONE DOWN,":PRINT "PASS IT AROUND":PRINT B-1;" BOTTLES OF BEER ON THE WALL"
130 NEXT B
140 PRINT "0 BOTTLES OF BEER ON THE WALL":PRINT "0 BOTTLES OF BEER": PRINT "GO TO THE STORE AND BUY SOME MORE,":PRINT "99 BOTTLES OF BEER ON THE WALL"
150 GOTO 90

Note: This was made in an Applesoft BASIC interpeter here: [1]

An interpreter made by User:Ohead for Node.js. Programs are entered using the argument after the JS file.

function hq9(sourceCode) {
	let sourceArray = sourceCode.split('');
	let output = ``;
	let accumulator = 0;
	sourceArray.forEach(char=>{
		switch (char) {
			case 'H':
				output += 'hello, world\n';
			break;
			case 'Q':
				output += sourceCode + '\n';
			break;
			case '9':
				let plural = (val) => {
					if (val == 1)
						return ''
					else
						return 's';
				};
				for (let i = 99; i > 0; i--) {
					output += `${i} bottle${plural(i)} of beer on the wall, ${i} bottle${plural(i)} of beer
	Take one down, pass it around ${i-1} bottle${plural(i-1)} of beer on the wall
	`
				};
			break;
			case '+':
				accumulator++;
			break;
		};
	});
	return output;
};
console.log(hq9(process.argv[2]));

An interpreter in Java maded by PSTF and his 文心一言:

import java.util.HashMap;  
import java.util.Map;  
import java.util.Scanner;  
  
public class HQ9PlusInterpreter {  
      
    private static int counter = 0;  
      
    private static Map<String, Runnable> commands = new HashMap<>();  
      
    static {  
        commands.put("H", () -> System.out.println("Hello, world!"));  
        commands.put("Q", () -> System.out.println("Q"));  
        commands.put("9", () -> {   
            String s = "s";
            for (int beers=99; beers>-1;){
                System.out.print(beers + " bottle" + s + " of beer on the wall, ");
                System.out.println(beers + " bottle" + s + " of beer, ");
                if (beers==0){
                    System.out.print("Go to the store, buy some more, ");
                    System.out.println("99 bottles of beer on the wall.\n");
                }
                else{
                    System.out.print("Take one down, pass it around, ");
                    s = (--beers == 1)?"":"s";
                    System.out.println(beers + " bottle" + s + " of beer on the wall.\n");
                }
            }
        });  
        commands.put("+", () -> counter++);  
    }  
      
    public static void main(String[] args) {  
        Scanner scanner = new Scanner(System.in);  
        System.out.println("The HQ9+ interpreter by PrySigneToFry"); 
        System.out.println("Enter HQ9+ commands (type 'exit' to quit):\n");  
          
        while (true) {  
            System.out.print("> ");  
            String command = scanner.nextLine();  
              
            if (command.equalsIgnoreCase("exit")) {  
                break;  
            }  
              
            Runnable action = commands.get(command);  
              
            if (action != null) {  
                action.run();  
            } else {  
                System.out.println("Unknown command: " + command);  
            }  
              
            // Print the current counter value after each command (except for '9' which is too verbose)  
            if (!command.equals("9")) {  
                System.out.println("Counter: " + counter);  
            }  
        }  
          
        scanner.close();  
    }  
}

User:MihaiEso has a interpreter in Batch, based on the FHC's interpreter:

@echo off
title HQ9+ Interpreter
:exe
cls
echo Type one of these characters (H, Q, 9, +) and we execute it...
set /p fhc=
if /i %fhc% EQU H goto hello
if /i %fhc% EQU Q goto quine
if /i %fhc% EQU 9 goto 99bottle
if /i %fhc% EQU + goto plus
cls
echo Error! You chose a wrong character. Only these are accepted: H, Q, 9 and +
pause >nul
goto exe
:quine
cls
echo Q
pause >nul
goto exe
:hello
cls
echo Hello, world!
pause >nul
goto exe
:99bottle
set x=99
set /a y=%x%-1
:loop
echo %x% bottles of beer on the wall,
echo %x% bottles of beer.
echo Take one down, pass it around,
echo %y% bottles of beer on the wall.
echo.
set /a x=%x%-1
set /a y=%x%-1
if %x% EQU 1 (
echo 1 bottle of beer on the wall,
echo 1 bottle of beer.
echo Take one down, pass it around,
echo No bottles of beer on the wall.
pause >nul
goto exe
)
goto loop
:plus
set acc=%acc%+1
goto exe

An interpreter by Python made by PSTF:

class HQ9PlusInterpreter:  
    def __init__(self):  
        self.counter = 0  
  
    def interpret(self, code):  
        lines = code.split('\n') 
        for line in lines:  
            line = line.strip()  
            if line == 'H':  
                print("Hello, world!")  
            elif line == 'Q':  
                print(code)  
            elif line == '9':  
                for i in range(99, -1, -1):  
                    print(i) # This command is simply print a number sequence count from 99 to 0.
            elif line == '+':  
                self.counter += 1  
                print(f"Counter is now: {self.counter}")  
            else:  
                print(f"Unknown command: {line}")  
  
# Example usage:  
print("HQ9+ interpreter by PSTF")
interpreter = HQ9PlusInterpreter() 
while True: 
    code = input("Enter the command: ")
    if code == "exit":
        print("Thank you for using!")
        break
    else:
        interpreter.interpret(code)

See also

  • HQ9++, an object-oriented extension of HQ9+.
  • HQ9+-, an extension of HQ9++ with the - operator for debugging purposes.
  • HQ9+~, an extension of HQ9+ which is Turing-complete.
  • HQ9F+, an extension of HQ9+ with the F operator for FizzBuzz.
  • FHQ9+-, an extension of HQ9+- with the F operator for FizzBuzz.
  • CHIQRSX9+, another HQ9+ extension supposedly Turing complete.
  • HQ9+B, an ℒ-complete extension of HQ9+.
  • HQ9+2D, a 2-D extension of HQ9+.
  • H9+, with one fewer instruction but still capable of all of the tasks.
  • FISHQ9+, HQ9+ and deadfish combined.
  • HI9+, which replaces Q with an instruction that prints the interpreter's source code.
  • +, where there is only +, and other characters are ignored.
  • Hq9eFuck, HQ9+, brainfuck and Deep Thought combined.
  • BrainfisHQ9+, HQ9+, brainfuck and deadfish combined.
  • ACHEQUEUENINETHOUSANDPLUS, a more powerful derivative.
  • AHQ9+-, a derivative where the accumulator actually has a use.
  • HQ9~ or HQ9Tilde, an extension of HQ9+ with a lot of new functions.

External resources