HQ9-

From Esolang
Jump to navigation Jump to search

HQ9- is a derivative of HQ9+ by User:None1.

Commands

HQ9- and HQ9+ has only 2 diffenerces:

  1. HQ9-'s accumulator only supports non-positive numbers.
  2. HQ9- has the - command for decrement instead of the + command for increment in HQ9+.

Example Programs

Hello World

H

Quine

Q

99 bottles of beer

9

Decrements the accumulator

-

From an end user's perspective, it is equivalent to doing nothing.

Decreases the accumulator by 3

---

From an end user's perspective, it is equivalent to doing nothing.

Implementation

Based on the work of User:A

#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;
}

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

import java.util.HashMap;  
import java.util.Map;  
import java.util.Scanner;  
  
public class HQ9MinusInterpreter {  
      
    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();  
    }  
}

An interpreter by Python made by PSTF:

class HQ9MinusInterpreter:  
    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 = HQ9MinusInterpreter() 
while True: 
    code = input("Enter the command: ")
    if code == "exit":
        print("Thank you for using!")
        break
    else:
        interpreter.interpret(code)