5
\$\begingroup\$

There was a prior question about golfing an HQ9+ compiler that translates HQ9+ code into the language it is written in, but that was closed as a duplicate of the interpreter challenge because converting an interpreter to a compiler is seen as trivial. This question is based on the closed one, but has some changes.

Write an HQ9+ transpiler in a language of your choice. Your submission should be written in some language X, and should take input of HQ9+ source and output a program in another language Y.

The H command prints Hello, world!.

Q outputs the program's code. For this challenge, it must print the transpiled code, not the HQ9+ source, and must not use cheating quine methods. This deviates from the HQ9+ "specification," but is intended to further distinguish this challenge from the interpreter one. For the avoidance of doubt, using eval-type functions or methods such as JavaScript's function.toString() are not considered cheating for the purpose of this challenge.

The 9 command prints the lyrics to "99 Bottles of Beer." You may use any reasonable variant of the lyrics.

In theory, + increments the accumulator. However, because the accumulator cannot be addressed, + is effectively a no-op.

H, Q, and 9 may, but are not required to, print trailing newlines. + must not produce any output, not even whitespace.

Input must be case-insensitive; hq, hQ, Hq, and HQ are the same program. Behavior with unrecognized characters is undefined.

Rules

  1. Language X should satisfy the traditional definition of a language
  2. Standard loopholes disallowed
  3. Your solution must be testable
  4. Y cannot be HQ9+ or an extension of HQ9+
  5. Score is calculated as length of transpiler, plus length of output for H, plus length of output for Q, plus length of output for 9, plus length of output for +, plus length of output for HQ9+
  6. Submissions compete by language pairs; and HQ9+ to JavaScript transpiler in Python does not compete with an HQ9+ to C transpiler also written in Python.
\$\endgroup\$
4
  • \$\begingroup\$ Should H, 9, or Q output a trailing newline after their respective commands? \$\endgroup\$
    – ATaco
    Dec 4, 2023 at 1:06
  • \$\begingroup\$ @ATaco it's up to you. \$\endgroup\$
    – Someone
    Dec 4, 2023 at 1:21
  • \$\begingroup\$ "in another language Y" Based on the RProgN2 answer, where both X and Y are the same, I assume 'another' can be removed since using the same language is allowed? \$\endgroup\$ Dec 4, 2023 at 8:49
  • \$\begingroup\$ @KevinCruijssen my intention was for them to be different languages, to differentiate this from the closed compiler question, but since there's already an answer with X and Y the same and this doesn't have any close votes, it's okay if they're the same. \$\endgroup\$
    – Someone
    Dec 4, 2023 at 16:23

3 Answers 3

6
\$\begingroup\$

RProgN 2 (outputs RProgN 2), 190 + 17 + 9 + 116 + 1 + 140 = 473 bytes

l]`qEL²"«` .x="w?]`9EL²"$99{]]1-'%i J on the wall
%i J!
You take one down, pass it around
%i J on the wall!'`J'bottles of beer'rF}Si`
.y="w?
'[^h9q]'Ør`h"'Hello, world!'p"r
`9'yp'r`q"xp"r` .

Try it online!

Uses an ultra simple 99 bottles representation. Includes newlines between commands, though instances of p can be swapped for w to remove them at no extra cost. Conditionally includes a the chunk of code for 99 bottles and the quine only if they're needed.

\$\endgroup\$
3
  • \$\begingroup\$ What language is the output? \$\endgroup\$
    – Someone
    Dec 4, 2023 at 1:23
  • \$\begingroup\$ @Somebody RProgN2, I missed the score requirement, so I'll golf this a little more. \$\endgroup\$
    – ATaco
    Dec 4, 2023 at 1:24
  • 1
    \$\begingroup\$ The variant of 99 Bottles of Beer you have is fine, but I've seen a lot of versions without "You," so maybe you could save some bytes by removing that? \$\endgroup\$
    – Someone
    Dec 4, 2023 at 5:12
5
\$\begingroup\$

Retina 0.8.2 (outputs Actually or Seriously), 14 bytes, score 23

T`l9+`LN_
\B
.

Try it online! Link includes test cases. Explanation: Port of @KevinCruijssen's 05AB1E answer.

T`l9+`LN_

Uppercase the input, change 9 to N, and delete +.

\B
.

Insert .s between pairs of letters. (This also means that the output for + is ., but it's worth it for the golfiness.)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ It looks like Q is a cheating quine, which is explicitly forbidden? \$\endgroup\$
    – ATaco
    Dec 4, 2023 at 19:59
4
\$\begingroup\$

05AB1E (outputs Actually or Seriously), score: 20

10 bytes + 2 for H + 2 for Q + 2 for 9 - 1 since a trailing . is not necessary + 0 for + + 5 for HQ9+

u9'N:áS'.ý

Output language is either Actually or Seriously.
It'll output Hello, World! and the quine with a single trailing newline, and 99 bottles of beer with two trailing newlines.

Try it online.

Try the output for Hq9+h in Actually.
Try the output for Hq9+h in Seriously.

Explanation:

05AB1E:

u            # Uppercase all letters in the (implicit) input-string
 9'N:       '# Replace all 9s with "N"s
     á       # Remove the "+"s by only leaving letters
      S      # Convert the string to a list of characters
       '.ý  '# Join it back together with "." delimiter
             # (after which the result is output implicitly)

Actually / Seriously:

  • H: Push "Hello, World!" to the stack
  • N: Push the entire 99 bottles of beer lyrics (with trailing newline) to the stack
  • Q: Push the program's source code to the stack
  • .: Pop and print a string with trailing newline
  • Implicitly print the trailing string on the stack which we haven't explicitly printed with a .
\$\endgroup\$
1
  • 1
    \$\begingroup\$ It looks like Q is a cheating quine, which is explicitly forbidden? \$\endgroup\$
    – ATaco
    Dec 4, 2023 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.