Execute HQ9+: Difference between revisions

m
(6 intermediate revisions by 5 users not shown)
Line 8:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F hello()
print(‘Hello, world!’)
 
Line 56:
L(i) src.lowercase()
I i C dispatch
dispatch[i]()</langsyntaxhighlight>
 
=={{header|8080 Assembly}}==
Line 66:
Alternatively, DDT can be used, though you will have to set up the FCB by hand.)
 
<langsyntaxhighlight lang="8080asm">putch: equ 2 ; Write character
puts: equ 9 ; Write string
fopen: equ 15 ; Open file
Line 204:
nl: db 13,10,'$'
accum: db 0 ; Accumulator
src: equ $ ; Program source</langsyntaxhighlight>
 
{{out}}
Line 236:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Run(CHAR ARRAY code)
BYTE i,a
CHAR c
Line 263:
PROC Main()
Run("9++hQ+q9H+")
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Execute_HQ9+.png Screenshot from Atari 8-bit computer]
Line 285:
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
<langsyntaxhighlight lang="agena"># HQ9+ interpreter
 
# execute an HQ9+ program in the code string - code is not case sensitive
Line 332:
hq9( code )
until code = ""
epocs;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Translation of DWScript. the accumulator is global.
<langsyntaxhighlight lang="algol68"># the increment-only accumulator #
INT hq9accumulator := 0;
 
Line 382:
read( ( code, newline ) );
hq9( code )
END</langsyntaxhighlight>
 
=={{header|ALGOL W}}==
Based on ALGOL 68 (which is a translation of DWScript)...
<langsyntaxhighlight lang="algolw">begin
 
procedure writeBottles( integer value bottleCount ) ;
Line 437:
hq9( code, codeLength + 1 )
end
end.</langsyntaxhighlight>
 
=={{header|Applesoft BASIC}}==
<langsyntaxhighlight ApplesoftBasiclang="applesoftbasic">100 INPUT "HQ9+ : "; I$
110 LET J$ = I$ + CHR$(13)
120 LET H$ = "HELLO, WORLD!"
Line 458:
260 PRINT B - 1 " " B$ W$
270 NEXT B
280 NEXT I</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">hq9: function [source][
acc: 0
loop split source 'ch [
Line 477:
 
acc: hq9 {+qhp;+9Q}
print ["accumulator:" acc]</langsyntaxhighlight>
 
{{out}}
Line 488:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">; http://www.autohotkey.com/forum/viewtopic.php?p=356268#356268
 
testCode := "hq9+HqQ+Qq"
Line 515:
}
Return output
}</langsyntaxhighlight>
 
=={{header|BASIC256BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
# Intérprete de HQ9+
 
Line 565 ⟶ 566:
until false
end
</syntaxhighlight>
</lang>
 
==={{header|BBC BASIC}}===
<langsyntaxhighlight lang="bbcbasic"> PROChq9plus("hq9+HqQ+Qq")
END
Line 589 ⟶ 590:
ENDCASE
NEXT i%
ENDPROC</langsyntaxhighlight>
'''Output:'''
<pre>
Line 618 ⟶ 619:
Takes a single line HQ9+ program from stdin, and displays the output.
 
<langsyntaxhighlight lang="bqn">Pl ← {(𝕩≠1)/"s"}
Lwr ← +⟜(32×1="A["⊸⍋)
nn ← {(•Fmt 𝕨)∾" "∾𝕩}´¨∾{
Line 635 ⟶ 636:
}
 
•Out¨HQ9 •GetLine@</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">void runCode(const char *code)
{
int c_len = strlen(code);
Line 672 ⟶ 673:
}
}
};</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
 
<langsyntaxhighlight lang="csharp">
using System;
using System.Collections.Generic;
Line 698 ⟶ 699:
}
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
Basically the same as the C example, although this has been C++'ified with strings and streams.
<langsyntaxhighlight lang="cpp">void runCode(string code)
{
int c_len = code.length();
Line 736 ⟶ 737:
}
}
};</langsyntaxhighlight>
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void run() {
void eval(String code) {
Line 776 ⟶ 777:
eval("hq9+");
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(ns anthony.random.hq9plus
(:require [clojure.string :as str]))
 
Line 801 ⟶ 802:
\9 (bottles)
\+ (reset! accumulator (inc @accumulator)))
(if-not (= (inc pointer) (count commands)) (recur (inc pointer))))))</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% This program uses the "get_argv" function from PCLU's "useful.lib"
 
hq9plus = cluster is load, run
Line 861 ⟶ 862:
fn: file_name := file_name$parse(sequence[string]$bottom(get_argv()))
hq9plus$run(hq9plus$load(fn))
end start_up</langsyntaxhighlight>
{{out}}
<pre>$ cat test.hq
Line 881 ⟶ 882:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Exec-Hq9.
 
Line 924 ⟶ 925:
 
GOBACK
.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Line 931 ⟶ 932:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.string;
 
void main(in string[] args) {
Line 966 ⟶ 967:
}
}
}</langsyntaxhighlight>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|C}}
<syntaxhighlight lang="delphi">
<lang Delphi>
uses
System.SysUtils;
Line 1,003 ⟶ 1,004:
end;
end;
end;</langsyntaxhighlight>
 
=={{header|DWScript}}==
 
{{Trans|D}}
<langsyntaxhighlight lang="dwscript">procedure RunCode(code : String);
var
i : Integer;
Line 1,035 ⟶ 1,036:
end;
end;
end;</langsyntaxhighlight>
 
=={{header|Dyalect}}==
 
<langsyntaxhighlight lang="dyalect">func eval(code) {
var accumulator = 0
var opcodes = (
Line 1,060 ⟶ 1,061:
for c in code {
opcodes[c.LoweLower()]()
}
}</langsyntaxhighlight>
 
=={{header|E}}==
 
See [[Execute HQ9+/E]].
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc run code$ . .
for c$ in strchars code$
if c$ = "Q"
print code$
elif c$ = "H"
print "Hello, world!"
elif c$ = "9"
for b = 99 downto 1
print b & " bottles of beer on the wall"
print b & " bottles of beer"
print "Take one down, pass it around"
print b & " bottles of beer on the wall"
print ""
.
elif c$ = "+"
acc += 1
print acc
.
.
.
run "HQ9+"
</syntaxhighlight>
 
=={{header|Ela}}==
Line 1,072 ⟶ 1,098:
===Impure approach===
 
<langsyntaxhighlight lang="ela">open unsafe.console char unsafe.cell imperative
eval src = eval' src
Line 1,092 ⟶ 1,118:
(show x) " bottles of beer\r\n"
"Take one down, pass it around\r\n"
`seq` bottles xs</langsyntaxhighlight>
 
===Pure version===
Line 1,098 ⟶ 1,124:
An interpreter itself has no side effects:
 
<langsyntaxhighlight lang="ela">open list char
eval src = eval' src 0
Line 1,116 ⟶ 1,142:
++ show x ++ " bottles of beer\r\n"
++ "Take one down, pass it around\r\n"
++ bottles xs</langsyntaxhighlight>
 
It slightly alters an original HQ9+ specification. HQ9+ is an impure language that does console output. However console output is the only interaction that a user can see when executing HQ9+ program. This interpreter doesn't output to console but instead generates a list with all outputs. An accumulator is moved to the interpter arguments and the need for a reference cell is eliminated. Once an interpreter completes a client code can output to console using monads like so:
 
<langsyntaxhighlight lang="ela">open imperative monad io
 
print_and_eval src = do
Line 1,127 ⟶ 1,153:
where print x = do putStrLn x
 
print_and_eval "HQ9+" ::: IO</langsyntaxhighlight>
 
=={{header|Erlang}}==
<langsyntaxhighlight Erlanglang="erlang">% hq9+ Erlang implementation (JWL)
% http://www.erlang.org/
-module(hq9p).
Line 1,187 ⟶ 1,213:
main(Compiled, Prog, 0).
 
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators command-line formatting interpolate io kernel
math math.ranges multiline namespaces sequences ;
IN: rosetta-code.hq9+
Line 1,222 ⟶ 1,248:
: main ( -- ) command-line get first interpret-HQ9+ ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
Test run on the command line:
Line 1,242 ⟶ 1,268:
=={{header|Forth}}==
 
<langsyntaxhighlight lang="forth">variable accumulator
: H cr ." Hello, world!" ;
: Q cr 2dup type ;
Line 1,252 ⟶ 1,278:
i 1 [ get-current literal ] search-wordlist
if execute else true abort" invalid HQ9+ instruction"
then loop 2drop ;</langsyntaxhighlight>
 
=={{header|Fortran}}==
This is F77 style except for the END SUBROUTINE HQ9, since F90+ allows the END statement to name its subroutine, and more seriously, the SELECT CASE construction that avoids interminable IF ... THEN ... ELSE IF ... sequences or even, a computed GO TO. The obvious data structure is the CHARACTER type, introduced with F77.
 
The only difficulty lies in the phasing of the various components of the recital (note the lines ending with commas or periods), and especially, producing correct grammar for the singular case. One could simply produce the likes of *"1 bottles of beer", or perhaps "1 bottle(s) of beer" but having been hounded for decades by compilers quibbling over syntax trivia, a certain sensitivity has arisen. For this case, the requirement is to append a "s" or not to "bottle" and the task is quite vexing because Fortran does not allow within expressions syntax such as <langsyntaxhighlight Fortranlang="fortran">"bottle" // IF (B.NE.1) THEN "s" FI // " of beer"</langsyntaxhighlight> so alternative schemes must be devised. There are many possibilities. The output line could be written piecemeal using the "non-advancing" options introduced in F90 with the "s" being written or not, or, the output line could be developed piecemeal in a CHARACTER variable in a similar way then written in one go. Alternatively, a character variable SUFFIX could be employed, which contains either "s" or " " with its usage being <code>..."bottle"//SUFFIX(1:LSTNB(SUFFIX))//...</code> where function LSTNB fingers the last non-blank character (if function TRIM or LEN_TRIM are unavailable), or, with F2003 there is a facility whereby SUFFIX can be declared with a varying length so as to be either "s" or "". Still another ploy would be to replace the "s" by a "null" character (character code zero) that will be passed over by the device showing the output. Or maybe not...
 
However, because the tail end of the recital does not conform to the structure of the earlier verses, it seemed easier to combine the singular case with the coda, especially since "No bottles" is to be produced instead of "0 bottles". It would be easy enough to devise a function CARDINAL(N) that would return "Ninety-nine", ... "One", "No" but the required code would swamp the rest of the project.
Line 1,263 ⟶ 1,289:
So, there is a careful factorisation of the text phrases into FORMAT and WRITE statements. Note that "free-format" output (as with <code>WRITE (6,*)</code>) starts in the second column, whereas formatted output starts in the first column. Inspection of the code file HQ9.exe shows that the compiler has recognised that the multiple appearances of the text literals "bottles" (three) and "bottle" (two) are the same and there is only one value of each constant in the code file. However, it has not noticed that the text "bottle" can be extracted from "bottles", which could in turn be found within a larger text literal "No bottles of beer on the wall" which also contains the subsequence " on the wall" - perhaps the code to do this would consume more space than would be saved by having a single multiple-use text constant in the code for those, or perhaps the problem is just too difficult in general to be worth the effort of devising and executing a worthwhile analysis, given that only a few bytes might be saved in a code file of 480Kb. This of course must contain the format interpretation subsystem and so forth, not just the code for the Fortran source. Even so, this program (with minor changes to the syntax) could be written in Fortran IV for an IBM1130, and would run in a computer with a total memory size of 8Kb. On such systems, much thought would go in to minimising space lost to verbose texts and good exposition as well as such reuse opportunities: gaining access to 32Kb or even 64Kb systems would be a great relief. But these days, memory space is not at a premium, and we are told that modern compilers produce excellent code.
 
<syntaxhighlight lang="fortran">
<lang Fortran>
SUBROUTINE HQ9(CODE) !Implement the rather odd HQ9+ instruction set.
CHARACTER*(*) CODE !One operation code per character.
Line 1,298 ⟶ 1,324:
PROGRAM POKE
CALL HQ9("hq9")
END</langsyntaxhighlight>
 
To show that the juggling works,
Line 1,326 ⟶ 1,352:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
' Intérprete de HQ9+
' FB 1.05.0 Win64
Line 1,377 ⟶ 1,403:
Loop While Inkey <> Chr(27)
End
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
Line 1,384 ⟶ 1,410:
 
=={{header|Golo}}==
<langsyntaxhighlight lang="golo">module hq9plus
 
function main = |args| {
Line 1,424 ⟶ 1,450:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Haskell}}==
Line 1,431 ⟶ 1,457:
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="javascript">// live demo: http://try.haxe.org/#2E7D4
static function hq9plus(code:String):String {
var out:String = "";
Line 1,451 ⟶ 1,477:
}
return out;
}</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Process HQ9+ from command line arguments and input until an error or end-of file.
<langsyntaxhighlight Iconlang="icon">procedure main(A)
repeat writes("Enter HQ9+ code: ") & HQ9(get(A)|read()|break)
end
Line 1,481 ⟶ 1,507:
}
return
end</langsyntaxhighlight>
 
=={{header|Inform 7}}==
 
<langsyntaxhighlight lang="inform7">HQ9+ is a room.
 
After reading a command:
Line 1,507 ⟶ 1,533:
say "[M - 1] bottle[s] of beer on the wall[paragraph break]";
otherwise if C is "+":
increase accumulator by 1.</langsyntaxhighlight>
 
=={{header|J}}==
 
From [[99_Bottles_of_Beer#J|99 Bottles of Beer]]
<langsyntaxhighlight Jlang="j">bob =: ": , ' bottle' , (1 = ]) }. 's of beer'"_
bobw=: bob , ' on the wall'"_
beer=: bobw , ', ' , bob , '; take one down and pass it around, ' , bobw@<:</langsyntaxhighlight>
 
The rest of the interpreter:
<langsyntaxhighlight Jlang="j">H=: smoutput bind 'Hello, world!'
Q=: smoutput @ [
hq9=: smoutput @: (beer"0) bind (1+i.-99)
hqp=: (A=:1)1 :'0 0$A=:A+m[y'@]
 
hq9p=: H`H`Q`Q`hq9`hqp@.('HhQq9+' i. ])"_ 0~</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight Jlang="j"> hq9p 'hqQQq'
Hello, world!
hqQQq
hqQQq
hqQQq
hqQQq</langsyntaxhighlight>
 
=={{header|Java}}==
Line 1,539 ⟶ 1,565:
=={{header|JavaScript}}==
The function below executes a HQ9+ program and returns the program output as a string.
<langsyntaxhighlight lang="javascript">function hq9plus(code) {
var out = '';
var acc = 0;
Line 1,561 ⟶ 1,587:
}
return out;
}</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
<langsyntaxhighlight lang="julia">hello() = println("Hello, world!")
quine() = println(src)
bottles() = for i = 99:-1:1 print("\n$i bottles of beer on the wall\n$i bottles of beer\nTake one down, pass it around\n$(i-1) bottles of beer on the wall\n") end
Line 1,595 ⟶ 1,621:
for i in lowercase(src)
if haskey(dispatch, i) dispatch[i]() end
end</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.3
 
fun hq9plus(code: String) {
Line 1,630 ⟶ 1,656:
val code = args[0] // pass in code as command line argument (using hq9+)
hq9plus(code)
}</langsyntaxhighlight>
 
{{out}}
Line 1,650 ⟶ 1,676:
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">'Try this hq9+ program - "hq9+HqQ+Qq"
Prompt "Please input your hq9+ program."; code$
Print hq9plus$(code$)
Line 1,682 ⟶ 1,708:
Next i
hq9plus$ = Left$(hq9plus$, (Len(hq9plus$) - 2))
End Function</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">
function runCode( code )
local acc, lc = 0
Line 1,706 ⟶ 1,732:
end
end
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
<syntaxhighlight lang="text">hq9plus[program_] :=
Module[{accumulator = 0, bottle},
bottle[n_] :=
Line 1,719 ⟶ 1,745:
"\ntake one down, pass it around\n" <> bottle[n - 1] <>
" on the wall" <> If[n == 1, "", "\n\n"], {n, 99, 1, -1}]],
"+", accumulator++], {chr, Characters@program}]; accumulator]</langsyntaxhighlight>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">code = input("Enter HQ9+ program: ")
 
sing = function()
Line 1,740 ⟶ 1,766:
if c == "9" then sing
if c == "+" then accumulator = accumulator + 1
end for</langsyntaxhighlight>
{{out}}
<pre>Enter HQ9+ program: hq9+
Line 1,756 ⟶ 1,782:
 
=={{header|Nanoquery}}==
<langsyntaxhighlight Nanoquerylang="nanoquery">import Nanoquery.IO
 
// a function to handle fatal errors
Line 1,812 ⟶ 1,838:
accum += 1
end
end</langsyntaxhighlight>
 
=={{header|NetRexx}}==
Line 1,821 ⟶ 1,847:
Modify contents of the program variable as you see fit.
 
<langsyntaxhighlight lang="nim">
var program = "9hHqQ+"
var i = 0
Line 1,853 ⟶ 1,879:
else:
echo("Unknown command: ", token)
</syntaxhighlight>
</lang>
 
=={{header|NS-HUBASIC}}==
<langsyntaxhighlight NSlang="ns-HUBASIChubasic">10 INPUT "INPUT HQ9+ CODE: ",I$
20 B$="S"
30 W$=" ON THE WALL"
Line 1,875 ⟶ 1,901:
180 PRINT B-1 " BOTTLE"B$" OF BEER" W$
190 NEXT
200 NEXT</langsyntaxhighlight>
 
=={{header|OCaml}}==
Regrettably, HQ9+ suffers from remarkably poor implementations, even though the spec nailed down every aspect of the language (apart from the exact lyrics of the '9' operation, this obviously to allow for localization.) What's worse, the only implementation linked from the spec, when it was accessible, was an OCaml work that <i>refused to implement the '+' operation</i> among its several other deviations. The following code borrows 'beer' from its page.
 
<langsyntaxhighlight lang="ocaml">let hq9p line =
let accumulator = ref 0 in
for i = 0 to (String.length line - 1) do
Line 1,888 ⟶ 1,914:
| '9' -> beer 99
| '+' -> incr accumulator
done</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Line 1,894 ⟶ 1,920:
 
The lyrics are based on the reference implementation. The endline and case-insensitivity are from an example in the spec.
<langsyntaxhighlight lang="parigp">beer(n)={
if(n == 1,
print("1 bottle of beer on the wall");
Line 1,916 ⟶ 1,942:
if(v[i] == "+", accum++, error("Nasal demons"))
)
};</langsyntaxhighlight>
 
Sample input/output:
Line 1,927 ⟶ 1,953:
==={{header|Free Pascal}}===
{{trans|Delphi}}
<langsyntaxhighlight lang="pascal">program HQ9;
 
procedure runCode(code: string);
Line 1,962 ⟶ 1,988:
runCode('QqQh');
//runCode('HQ9+');// output to long
END.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,972 ⟶ 1,998:
=={{header|Perl}}==
This implementation uses the ''switch'' feature.
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 2,023 ⟶ 2,049:
return 'Take one down and pass it around' if $n > 0;
return 'Go to the store and buy some more';
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #000080;font-style:italic;">-- copied from [[99_Bottles_of_Beer#Phix|99_Bottles_of_Beer]]</span>
Line 2,077 ⟶ 2,103:
<span style="color: #000000;">hq9</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"h9+HqQ+Qq"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,103 ⟶ 2,129:
</pre>
 
=={{header|phpPHP}}==
<langsyntaxhighlight lang="php">
/*
H Prints "Hello, world!"
Line 2,163 ⟶ 2,189:
function printError($chr) {
echo "Invalid input: ". $chr;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,174 ⟶ 2,200:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de hq9+ (Code)
(let Accu 0
(for C (chop Code)
Line 2,188 ⟶ 2,214:
(prinl) ) )
("+" (inc 'Accu)) ) )
Accu ) )</langsyntaxhighlight>
 
=={{header|PowerShell}}==
Line 2,198 ⟶ 2,224:
 
As far as I can tell, there are no errors in HQ9+; but, supposing there are, a 'Default' could be added to the switch statement.
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Invoke-HQ9PlusInterpreter ([switch]$Global)
{
Line 2,242 ⟶ 2,268:
 
Set-Alias -Name HQ9+ -Value Invoke-HQ9PlusInterpreter
</syntaxhighlight>
</lang>
Example sessions:
<pre>
Line 2,278 ⟶ 2,304:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure hq9plus(code.s)
Protected accumulator, i, bottles
For i = 1 To Len(code)
Line 2,305 ⟶ 2,331:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
Line 2,313 ⟶ 2,339:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery">$ "bottles.qky" loadfile ( if required, the source code for this can be found at
http://rosettacode.org/wiki/99_bottles_of_beer#Quackery )
 
Line 2,338 ⟶ 2,364:
accumulator take echo ] is HQ9+ ( $ --> )
 
$ "HH+QQQQ+" HQ9+</langsyntaxhighlight>
 
{{Out}}
Line 2,358 ⟶ 2,384:
strictly case-sensitive.
 
<langsyntaxhighlight lang="racket">#lang racket
; if we `for` over the port, we won't have the program in memory for 'Q'
(define (parse-HQ9+ the-program)
Line 2,405 ⟶ 2,431:
(check-equal? (with-output-to-string (lambda () (parse-HQ9+ (make-string 10000 #\+)))) "")
;;; you can jolly well read (and sing along to) the output of '9'
)</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 2,412 ⟶ 2,438:
The spec is kind of vague about how to do error handling... and whether white space is significant... and how the accumulator should be accessed... and pretty much everything else too.
 
<syntaxhighlight lang="raku" perl6line>class HQ9Interpreter {
has @!code;
has $!accumulator;
Line 2,448 ⟶ 2,474:
$hq9.run("hHq+++Qq");
say '';
$hq9.run("Jhq.k+hQ");</langsyntaxhighlight>
 
Output:
Line 2,468 ⟶ 2,494:
 
Or start a REPL (Read Execute Print Loop) and interact at the command line:
<syntaxhighlight lang="raku" perl6line>my $hq9 = HQ9Interpreter.new;
while 1 {
my $in = prompt('HQ9+>').chomp;
last unless $in.chars;
$hq9.run($in)
}</langsyntaxhighlight>
 
=={{header|REXX}}==
Note that the actual text of the &nbsp; ''Hello, world!'' &nbsp; message can differ among definitions.
<langsyntaxhighlight lang="rexx">/*REXX program implements the HQ9+ language. ───────────────────────────────────────*/
arg pgm . /*obtain optional argument.*/
accumulator=0 /*assign default to accum. */
Line 2,507 ⟶ 2,533:
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return ''; return "s" /*a simple pluralizer.*/</langsyntaxhighlight>
'''output''' &nbsp; when using the input of: &nbsp; <tt> HHH </tt>
<pre>
Line 2,516 ⟶ 2,542:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Execute HQ9
 
Line 2,546 ⟶ 2,572:
off
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,574 ⟶ 2,600:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::env;
 
// HQ9+ requires that '+' increments an accumulator, but it's inaccessible (and thus, unused).
Line 2,605 ⟶ 2,631:
fn main() {
execute(&env::args().nth(1).unwrap());
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">def hq9plus(code: String) : String = {
var out = ""
var acc = 0
Line 2,642 ⟶ 2,668:
 
println(hq9plus("HQ9+"))
</syntaxhighlight>
</lang>
 
=={{header|Seed7}}==
The program below accepts the HQ9+ program as command line parameter:
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: runCode (in string: code) is func
Line 2,678 ⟶ 2,704:
runCode(argv(PROGRAM)[1]);
end if;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">class HQ9Interpreter {
has pointer;
has accumulator;
Line 2,712 ⟶ 2,738:
}
}
}</langsyntaxhighlight>
 
Usage:
<langsyntaxhighlight lang="ruby">var hq9 = HQ9Interpreter();
hq9.run("hHq+++Qq");</langsyntaxhighlight>
 
{{out}}
Line 2,728 ⟶ 2,754:
 
Or start a REPL (Read Execute Print Loop) and interact at the command line:
<langsyntaxhighlight lang="ruby">var hq9 = HQ9Interpreter();
loop {
var in = read('HQ9+>', String) \\ break;
hq9.run(in)
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 2,748 ⟶ 2,774:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">import "os" for Process
 
var hq9plus = Fn.new { |code|
Line 2,780 ⟶ 2,806:
} else {
hq9plus.call(args[0])
}</langsyntaxhighlight>
 
{{out}}
Line 2,786 ⟶ 2,812:
 
=={{header|x86 Assembly}}==
<syntaxhighlight lang="x86 assembly">
<lang X86 Assembly>
 
;ds:si: pointer to asciiz string containing HQ9++ source code
Line 2,919 ⟶ 2,945:
.dataBeerSong3: db 0, " bottles of beer on the wall", 0
 
</syntaxhighlight>
</lang>
 
=={{header|XSLT}}==
Line 2,929 ⟶ 2,955:
Requires <code>bottles.xsl</code> (below).
 
<langsyntaxhighlight lang="xml"><?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- bottles.xsl defines $entire-bottles-song -->
Line 2,981 ⟶ 3,007:
</xsl:if>
</xsl:template>
</xsl:stylesheet></langsyntaxhighlight>
 
=====Details=====
Line 2,987 ⟶ 3,013:
Input to this sheet is given by placing the entire source as a single <code><nowiki><code/></nowiki></code> element. For example, to run the example program <code>qqqq</code>, use the sheet to transform the document
 
<syntaxhighlight lang ="xml"><code>qqqq</code></langsyntaxhighlight>
 
Newlines are added in roughly the same places as in the C version. For example, the program <code>qqqq</code> results in four lines of output rather than one long line.
Line 2,999 ⟶ 3,025:
Requires <code>bottles.xsl</code> (below)
 
<langsyntaxhighlight lang="xml"><?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- bottles.xsl defines $entire-bottles-song -->
Line 3,108 ⟶ 3,134:
</xsl:choose>
</xsl:template>
</xsl:stylesheet></langsyntaxhighlight>
 
=====Details=====
Line 3,116 ⟶ 3,142:
Input to this sheet is given by placing one or more sources as <code><nowiki><code/></nowiki></code> elements. For example, to run the example program <code>qqqq</code>, use the sheet to transform the document
 
<syntaxhighlight lang ="xml"><code>qqqq</code></langsyntaxhighlight>
 
or the programs <code>qqqq</code> and <code>++++</code> can be run in the same pass by transforming
 
<langsyntaxhighlight lang="xml"><programs>
<code>qqqq</code>
<code>++++</code>
</programs></langsyntaxhighlight>
 
The output document is a <code><nowiki><results/></nowiki></code> element containing a <code><nowiki><result/></nowiki></code> element for each <code><nowiki><code/></nowiki></code> element processed from the input. If a <code>+</code> appeared in the program, the <code><nowiki><result/></nowiki></code> element will indicate the final value of the accumulator in its <code>accumulator</code> attribute. For example, the output for the latter example, would be
 
<langsyntaxhighlight lang="xml"><results><result>qqqq
qqqq
qqqq
qqqq
</result><result accumulator="4"/></results></langsyntaxhighlight>
 
====bottles.xsl====
Line 3,137 ⟶ 3,163:
This sheet defines a value for the variable <code>$entire-bottles-song</code> (see [[99 Bottles of Beer]] for the general idea).
 
<langsyntaxhighlight lang="xml"><?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:lo="urn:uuid:59afd337-03a8-49d9-a7a8-8e2cbc4ef9cc">
<!-- Note: xmlns:lo is defined as a sort of pseudo-private namespace -->
Line 3,191 ⟶ 3,217:
</xsl:variable>
 
</xsl:stylesheet></langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn runHQ9(code){
acc:=0;
foreach c in (code){
Line 3,216 ⟶ 3,242:
(n==0 and "No more bottles" or (n==1 and "1 bottle" or "" + n + " bottles"))
+ " of beer"
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">runHQ9("90HQ+junk");</langsyntaxhighlight>
{{out}}
<pre>
1,979

edits