Execute HQ9+: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 8: Line 8:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F hello()
<syntaxhighlight lang="11l">F hello()
print(‘Hello, world!’)
print(‘Hello, world!’)


Line 56: Line 56:
L(i) src.lowercase()
L(i) src.lowercase()
I i C dispatch
I i C dispatch
dispatch[i]()</lang>
dispatch[i]()</syntaxhighlight>


=={{header|8080 Assembly}}==
=={{header|8080 Assembly}}==
Line 66: Line 66:
Alternatively, DDT can be used, though you will have to set up the FCB by hand.)
Alternatively, DDT can be used, though you will have to set up the FCB by hand.)


<lang 8080asm>putch: equ 2 ; Write character
<syntaxhighlight lang="8080asm">putch: equ 2 ; Write character
puts: equ 9 ; Write string
puts: equ 9 ; Write string
fopen: equ 15 ; Open file
fopen: equ 15 ; Open file
Line 204: Line 204:
nl: db 13,10,'$'
nl: db 13,10,'$'
accum: db 0 ; Accumulator
accum: db 0 ; Accumulator
src: equ $ ; Program source</lang>
src: equ $ ; Program source</syntaxhighlight>


{{out}}
{{out}}
Line 236: Line 236:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>PROC Run(CHAR ARRAY code)
<syntaxhighlight lang="action!">PROC Run(CHAR ARRAY code)
BYTE i,a
BYTE i,a
CHAR c
CHAR c
Line 263: Line 263:
PROC Main()
PROC Main()
Run("9++hQ+q9H+")
Run("9++hQ+q9H+")
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Execute_HQ9+.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Execute_HQ9+.png Screenshot from Atari 8-bit computer]
Line 285: Line 285:
=={{header|Agena}}==
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
Tested with Agena 2.9.5 Win32
<lang agena># HQ9+ interpreter
<syntaxhighlight lang="agena"># HQ9+ interpreter


# execute an HQ9+ program in the code string - code is not case sensitive
# execute an HQ9+ program in the code string - code is not case sensitive
Line 332: Line 332:
hq9( code )
hq9( code )
until code = ""
until code = ""
epocs;</lang>
epocs;</syntaxhighlight>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Translation of DWScript. the accumulator is global.
Translation of DWScript. the accumulator is global.
<lang algol68># the increment-only accumulator #
<syntaxhighlight lang="algol68"># the increment-only accumulator #
INT hq9accumulator := 0;
INT hq9accumulator := 0;


Line 382: Line 382:
read( ( code, newline ) );
read( ( code, newline ) );
hq9( code )
hq9( code )
END</lang>
END</syntaxhighlight>


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
Based on ALGOL 68 (which is a translation of DWScript)...
Based on ALGOL 68 (which is a translation of DWScript)...
<lang algolw>begin
<syntaxhighlight lang="algolw">begin


procedure writeBottles( integer value bottleCount ) ;
procedure writeBottles( integer value bottleCount ) ;
Line 437: Line 437:
hq9( code, codeLength + 1 )
hq9( code, codeLength + 1 )
end
end
end.</lang>
end.</syntaxhighlight>


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang ApplesoftBasic>100 INPUT "HQ9+ : "; I$
<syntaxhighlight lang="applesoftbasic">100 INPUT "HQ9+ : "; I$
110 LET J$ = I$ + CHR$(13)
110 LET J$ = I$ + CHR$(13)
120 LET H$ = "HELLO, WORLD!"
120 LET H$ = "HELLO, WORLD!"
Line 458: Line 458:
260 PRINT B - 1 " " B$ W$
260 PRINT B - 1 " " B$ W$
270 NEXT B
270 NEXT B
280 NEXT I</lang>
280 NEXT I</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>hq9: function [source][
<syntaxhighlight lang="rebol">hq9: function [source][
acc: 0
acc: 0
loop split source 'ch [
loop split source 'ch [
Line 477: Line 477:


acc: hq9 {+qhp;+9Q}
acc: hq9 {+qhp;+9Q}
print ["accumulator:" acc]</lang>
print ["accumulator:" acc]</syntaxhighlight>


{{out}}
{{out}}
Line 488: Line 488:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>; http://www.autohotkey.com/forum/viewtopic.php?p=356268#356268
<syntaxhighlight lang="autohotkey">; http://www.autohotkey.com/forum/viewtopic.php?p=356268#356268


testCode := "hq9+HqQ+Qq"
testCode := "hq9+HqQ+Qq"
Line 515: Line 515:
}
}
Return output
Return output
}</lang>
}</syntaxhighlight>


=={{header|BASIC256}}==
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
# Intérprete de HQ9+
# Intérprete de HQ9+


Line 565: Line 565:
until false
until false
end
end
</syntaxhighlight>
</lang>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
<lang bbcbasic> PROChq9plus("hq9+HqQ+Qq")
<syntaxhighlight lang="bbcbasic"> PROChq9plus("hq9+HqQ+Qq")
END
END
Line 589: Line 589:
ENDCASE
ENDCASE
NEXT i%
NEXT i%
ENDPROC</lang>
ENDPROC</syntaxhighlight>
'''Output:'''
'''Output:'''
<pre>
<pre>
Line 618: Line 618:
Takes a single line HQ9+ program from stdin, and displays the output.
Takes a single line HQ9+ program from stdin, and displays the output.


<lang bqn>Pl ← {(𝕩≠1)/"s"}
<syntaxhighlight lang="bqn">Pl ← {(𝕩≠1)/"s"}
Lwr ← +⟜(32×1="A["⊸⍋)
Lwr ← +⟜(32×1="A["⊸⍋)
nn ← {(•Fmt 𝕨)∾" "∾𝕩}´¨∾{
nn ← {(•Fmt 𝕨)∾" "∾𝕩}´¨∾{
Line 635: Line 635:
}
}


•Out¨HQ9 •GetLine@</lang>
•Out¨HQ9 •GetLine@</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
<lang c>void runCode(const char *code)
<syntaxhighlight lang="c">void runCode(const char *code)
{
{
int c_len = strlen(code);
int c_len = strlen(code);
Line 672: Line 672:
}
}
}
}
};</lang>
};</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==


<lang csharp>
<syntaxhighlight lang="csharp">
using System;
using System;
using System.Collections.Generic;
using System.Collections.Generic;
Line 698: Line 698:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|C++}}==
=={{header|C++}}==
Basically the same as the C example, although this has been C++'ified with strings and streams.
Basically the same as the C example, although this has been C++'ified with strings and streams.
<lang cpp>void runCode(string code)
<syntaxhighlight lang="cpp">void runCode(string code)
{
{
int c_len = code.length();
int c_len = code.length();
Line 736: Line 736:
}
}
}
}
};</lang>
};</syntaxhighlight>


=={{header|Ceylon}}==
=={{header|Ceylon}}==
<lang ceylon>shared void run() {
<syntaxhighlight lang="ceylon">shared void run() {
void eval(String code) {
void eval(String code) {
Line 776: Line 776:
eval("hq9+");
eval("hq9+");
}</lang>
}</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang clojure>(ns anthony.random.hq9plus
<syntaxhighlight lang="clojure">(ns anthony.random.hq9plus
(:require [clojure.string :as str]))
(:require [clojure.string :as str]))


Line 801: Line 801:
\9 (bottles)
\9 (bottles)
\+ (reset! accumulator (inc @accumulator)))
\+ (reset! accumulator (inc @accumulator)))
(if-not (= (inc pointer) (count commands)) (recur (inc pointer))))))</lang>
(if-not (= (inc pointer) (count commands)) (recur (inc pointer))))))</syntaxhighlight>


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>% This program uses the "get_argv" function from PCLU's "useful.lib"
<syntaxhighlight lang="clu">% This program uses the "get_argv" function from PCLU's "useful.lib"


hq9plus = cluster is load, run
hq9plus = cluster is load, run
Line 861: Line 861:
fn: file_name := file_name$parse(sequence[string]$bottom(get_argv()))
fn: file_name := file_name$parse(sequence[string]$bottom(get_argv()))
hq9plus$run(hq9plus$load(fn))
hq9plus$run(hq9plus$load(fn))
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre>$ cat test.hq
<pre>$ cat test.hq
Line 881: Line 881:


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Exec-Hq9.
PROGRAM-ID. Exec-Hq9.


Line 924: Line 924:


GOBACK
GOBACK
.</lang>
.</syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 931: Line 931:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.string;
<syntaxhighlight lang="d">import std.stdio, std.string;


void main(in string[] args) {
void main(in string[] args) {
Line 966: Line 966:
}
}
}
}
}</lang>
}</syntaxhighlight>
=={{header|Delphi}}==
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.SysUtils}}
{{Trans|C}}
{{Trans|C}}
<syntaxhighlight lang="delphi">
<lang Delphi>
uses
uses
System.SysUtils;
System.SysUtils;
Line 1,003: Line 1,003:
end;
end;
end;
end;
end;</lang>
end;</syntaxhighlight>


=={{header|DWScript}}==
=={{header|DWScript}}==


{{Trans|D}}
{{Trans|D}}
<lang dwscript>procedure RunCode(code : String);
<syntaxhighlight lang="dwscript">procedure RunCode(code : String);
var
var
i : Integer;
i : Integer;
Line 1,035: Line 1,035:
end;
end;
end;
end;
end;</lang>
end;</syntaxhighlight>


=={{header|Dyalect}}==
=={{header|Dyalect}}==


<lang dyalect>func eval(code) {
<syntaxhighlight lang="dyalect">func eval(code) {
var accumulator = 0
var accumulator = 0
var opcodes = (
var opcodes = (
Line 1,062: Line 1,062:
opcodes[c.Lower()]()
opcodes[c.Lower()]()
}
}
}</lang>
}</syntaxhighlight>


=={{header|E}}==
=={{header|E}}==
Line 1,072: Line 1,072:
===Impure approach===
===Impure approach===


<lang ela>open unsafe.console char unsafe.cell imperative
<syntaxhighlight lang="ela">open unsafe.console char unsafe.cell imperative
eval src = eval' src
eval src = eval' src
Line 1,092: Line 1,092:
(show x) " bottles of beer\r\n"
(show x) " bottles of beer\r\n"
"Take one down, pass it around\r\n"
"Take one down, pass it around\r\n"
`seq` bottles xs</lang>
`seq` bottles xs</syntaxhighlight>


===Pure version===
===Pure version===
Line 1,098: Line 1,098:
An interpreter itself has no side effects:
An interpreter itself has no side effects:


<lang ela>open list char
<syntaxhighlight lang="ela">open list char
eval src = eval' src 0
eval src = eval' src 0
Line 1,116: Line 1,116:
++ show x ++ " bottles of beer\r\n"
++ show x ++ " bottles of beer\r\n"
++ "Take one down, pass it around\r\n"
++ "Take one down, pass it around\r\n"
++ bottles xs</lang>
++ bottles xs</syntaxhighlight>


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:
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:


<lang ela>open imperative monad io
<syntaxhighlight lang="ela">open imperative monad io


print_and_eval src = do
print_and_eval src = do
Line 1,127: Line 1,127:
where print x = do putStrLn x
where print x = do putStrLn x


print_and_eval "HQ9+" ::: IO</lang>
print_and_eval "HQ9+" ::: IO</syntaxhighlight>


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang Erlang>% hq9+ Erlang implementation (JWL)
<syntaxhighlight lang="erlang">% hq9+ Erlang implementation (JWL)
% http://www.erlang.org/
% http://www.erlang.org/
-module(hq9p).
-module(hq9p).
Line 1,187: Line 1,187:
main(Compiled, Prog, 0).
main(Compiled, Prog, 0).


</syntaxhighlight>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: combinators command-line formatting interpolate io kernel
<syntaxhighlight lang="factor">USING: combinators command-line formatting interpolate io kernel
math math.ranges multiline namespaces sequences ;
math math.ranges multiline namespaces sequences ;
IN: rosetta-code.hq9+
IN: rosetta-code.hq9+
Line 1,222: Line 1,222:
: main ( -- ) command-line get first interpret-HQ9+ ;
: main ( -- ) command-line get first interpret-HQ9+ ;


MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
Test run on the command line:
Test run on the command line:
Line 1,242: Line 1,242:
=={{header|Forth}}==
=={{header|Forth}}==


<lang forth>variable accumulator
<syntaxhighlight lang="forth">variable accumulator
: H cr ." Hello, world!" ;
: H cr ." Hello, world!" ;
: Q cr 2dup type ;
: Q cr 2dup type ;
Line 1,252: Line 1,252:
i 1 [ get-current literal ] search-wordlist
i 1 [ get-current literal ] search-wordlist
if execute else true abort" invalid HQ9+ instruction"
if execute else true abort" invalid HQ9+ instruction"
then loop 2drop ;</lang>
then loop 2drop ;</syntaxhighlight>


=={{header|Fortran}}==
=={{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.
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 <lang Fortran>"bottle" // IF (B.NE.1) THEN "s" FI // " of beer"</lang> 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...
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 <syntaxhighlight lang="fortran">"bottle" // IF (B.NE.1) THEN "s" FI // " of beer"</syntaxhighlight> 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.
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: Line 1,263:
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.
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.
SUBROUTINE HQ9(CODE) !Implement the rather odd HQ9+ instruction set.
CHARACTER*(*) CODE !One operation code per character.
CHARACTER*(*) CODE !One operation code per character.
Line 1,298: Line 1,298:
PROGRAM POKE
PROGRAM POKE
CALL HQ9("hq9")
CALL HQ9("hq9")
END</lang>
END</syntaxhighlight>


To show that the juggling works,
To show that the juggling works,
Line 1,326: Line 1,326:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
' Intérprete de HQ9+
' Intérprete de HQ9+
' FB 1.05.0 Win64
' FB 1.05.0 Win64
Line 1,377: Line 1,377:
Loop While Inkey <> Chr(27)
Loop While Inkey <> Chr(27)
End
End
</syntaxhighlight>
</lang>


=={{header|Go}}==
=={{header|Go}}==
Line 1,384: Line 1,384:


=={{header|Golo}}==
=={{header|Golo}}==
<lang golo>module hq9plus
<syntaxhighlight lang="golo">module hq9plus


function main = |args| {
function main = |args| {
Line 1,424: Line 1,424:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==
Line 1,431: Line 1,431:


=={{header|Haxe}}==
=={{header|Haxe}}==
<lang javascript>// live demo: http://try.haxe.org/#2E7D4
<syntaxhighlight lang="javascript">// live demo: http://try.haxe.org/#2E7D4
static function hq9plus(code:String):String {
static function hq9plus(code:String):String {
var out:String = "";
var out:String = "";
Line 1,451: Line 1,451:
}
}
return out;
return out;
}</lang>
}</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Process HQ9+ from command line arguments and input until an error or end-of file.
Process HQ9+ from command line arguments and input until an error or end-of file.
<lang Icon>procedure main(A)
<syntaxhighlight lang="icon">procedure main(A)
repeat writes("Enter HQ9+ code: ") & HQ9(get(A)|read()|break)
repeat writes("Enter HQ9+ code: ") & HQ9(get(A)|read()|break)
end
end
Line 1,481: Line 1,481:
}
}
return
return
end</lang>
end</syntaxhighlight>


=={{header|Inform 7}}==
=={{header|Inform 7}}==


<lang inform7>HQ9+ is a room.
<syntaxhighlight lang="inform7">HQ9+ is a room.


After reading a command:
After reading a command:
Line 1,507: Line 1,507:
say "[M - 1] bottle[s] of beer on the wall[paragraph break]";
say "[M - 1] bottle[s] of beer on the wall[paragraph break]";
otherwise if C is "+":
otherwise if C is "+":
increase accumulator by 1.</lang>
increase accumulator by 1.</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==


From [[99_Bottles_of_Beer#J|99 Bottles of Beer]]
From [[99_Bottles_of_Beer#J|99 Bottles of Beer]]
<lang J>bob =: ": , ' bottle' , (1 = ]) }. 's of beer'"_
<syntaxhighlight lang="j">bob =: ": , ' bottle' , (1 = ]) }. 's of beer'"_
bobw=: bob , ' on the wall'"_
bobw=: bob , ' on the wall'"_
beer=: bobw , ', ' , bob , '; take one down and pass it around, ' , bobw@<:</lang>
beer=: bobw , ', ' , bob , '; take one down and pass it around, ' , bobw@<:</syntaxhighlight>


The rest of the interpreter:
The rest of the interpreter:
<lang J>H=: smoutput bind 'Hello, world!'
<syntaxhighlight lang="j">H=: smoutput bind 'Hello, world!'
Q=: smoutput @ [
Q=: smoutput @ [
hq9=: smoutput @: (beer"0) bind (1+i.-99)
hq9=: smoutput @: (beer"0) bind (1+i.-99)
hqp=: (A=:1)1 :'0 0$A=:A+m[y'@]
hqp=: (A=:1)1 :'0 0$A=:A+m[y'@]


hq9p=: H`H`Q`Q`hq9`hqp@.('HhQq9+' i. ])"_ 0~</lang>
hq9p=: H`H`Q`Q`hq9`hqp@.('HhQq9+' i. ])"_ 0~</syntaxhighlight>


Example use:
Example use:


<lang J> hq9p 'hqQQq'
<syntaxhighlight lang="j"> hq9p 'hqQQq'
Hello, world!
Hello, world!
hqQQq
hqQQq
hqQQq
hqQQq
hqQQq
hqQQq
hqQQq</lang>
hqQQq</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
Line 1,539: Line 1,539:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
The function below executes a HQ9+ program and returns the program output as a string.
The function below executes a HQ9+ program and returns the program output as a string.
<lang javascript>function hq9plus(code) {
<syntaxhighlight lang="javascript">function hq9plus(code) {
var out = '';
var out = '';
var acc = 0;
var acc = 0;
Line 1,561: Line 1,561:
}
}
return out;
return out;
}</lang>
}</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}
<lang julia>hello() = println("Hello, world!")
<syntaxhighlight lang="julia">hello() = println("Hello, world!")
quine() = println(src)
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
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: Line 1,595:
for i in lowercase(src)
for i in lowercase(src)
if haskey(dispatch, i) dispatch[i]() end
if haskey(dispatch, i) dispatch[i]() end
end</lang>
end</syntaxhighlight>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.3
<syntaxhighlight lang="scala">// version 1.1.3


fun hq9plus(code: String) {
fun hq9plus(code: String) {
Line 1,630: Line 1,630:
val code = args[0] // pass in code as command line argument (using hq9+)
val code = args[0] // pass in code as command line argument (using hq9+)
hq9plus(code)
hq9plus(code)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,650: Line 1,650:


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<lang lb>'Try this hq9+ program - "hq9+HqQ+Qq"
<syntaxhighlight lang="lb">'Try this hq9+ program - "hq9+HqQ+Qq"
Prompt "Please input your hq9+ program."; code$
Prompt "Please input your hq9+ program."; code$
Print hq9plus$(code$)
Print hq9plus$(code$)
Line 1,682: Line 1,682:
Next i
Next i
hq9plus$ = Left$(hq9plus$, (Len(hq9plus$) - 2))
hq9plus$ = Left$(hq9plus$, (Len(hq9plus$) - 2))
End Function</lang>
End Function</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>
<syntaxhighlight lang="lua">
function runCode( code )
function runCode( code )
local acc, lc = 0
local acc, lc = 0
Line 1,706: Line 1,706:
end
end
end
end
</syntaxhighlight>
</lang>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==


<lang>hq9plus[program_] :=
<syntaxhighlight lang="text">hq9plus[program_] :=
Module[{accumulator = 0, bottle},
Module[{accumulator = 0, bottle},
bottle[n_] :=
bottle[n_] :=
Line 1,719: Line 1,719:
"\ntake one down, pass it around\n" <> bottle[n - 1] <>
"\ntake one down, pass it around\n" <> bottle[n - 1] <>
" on the wall" <> If[n == 1, "", "\n\n"], {n, 99, 1, -1}]],
" on the wall" <> If[n == 1, "", "\n\n"], {n, 99, 1, -1}]],
"+", accumulator++], {chr, Characters@program}]; accumulator]</lang>
"+", accumulator++], {chr, Characters@program}]; accumulator]</syntaxhighlight>


=={{header|MiniScript}}==
=={{header|MiniScript}}==
<lang MiniScript>code = input("Enter HQ9+ program: ")
<syntaxhighlight lang="miniscript">code = input("Enter HQ9+ program: ")


sing = function()
sing = function()
Line 1,740: Line 1,740:
if c == "9" then sing
if c == "9" then sing
if c == "+" then accumulator = accumulator + 1
if c == "+" then accumulator = accumulator + 1
end for</lang>
end for</syntaxhighlight>
{{out}}
{{out}}
<pre>Enter HQ9+ program: hq9+
<pre>Enter HQ9+ program: hq9+
Line 1,756: Line 1,756:


=={{header|Nanoquery}}==
=={{header|Nanoquery}}==
<lang Nanoquery>import Nanoquery.IO
<syntaxhighlight lang="nanoquery">import Nanoquery.IO


// a function to handle fatal errors
// a function to handle fatal errors
Line 1,812: Line 1,812:
accum += 1
accum += 1
end
end
end</lang>
end</syntaxhighlight>


=={{header|NetRexx}}==
=={{header|NetRexx}}==
Line 1,821: Line 1,821:
Modify contents of the program variable as you see fit.
Modify contents of the program variable as you see fit.


<lang nim>
<syntaxhighlight lang="nim">
var program = "9hHqQ+"
var program = "9hHqQ+"
var i = 0
var i = 0
Line 1,853: Line 1,853:
else:
else:
echo("Unknown command: ", token)
echo("Unknown command: ", token)
</syntaxhighlight>
</lang>


=={{header|NS-HUBASIC}}==
=={{header|NS-HUBASIC}}==
<lang NS-HUBASIC>10 INPUT "INPUT HQ9+ CODE: ",I$
<syntaxhighlight lang="ns-hubasic">10 INPUT "INPUT HQ9+ CODE: ",I$
20 B$="S"
20 B$="S"
30 W$=" ON THE WALL"
30 W$=" ON THE WALL"
Line 1,875: Line 1,875:
180 PRINT B-1 " BOTTLE"B$" OF BEER" W$
180 PRINT B-1 " BOTTLE"B$" OF BEER" W$
190 NEXT
190 NEXT
200 NEXT</lang>
200 NEXT</syntaxhighlight>


=={{header|OCaml}}==
=={{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.
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.


<lang ocaml>let hq9p line =
<syntaxhighlight lang="ocaml">let hq9p line =
let accumulator = ref 0 in
let accumulator = ref 0 in
for i = 0 to (String.length line - 1) do
for i = 0 to (String.length line - 1) do
Line 1,888: Line 1,888:
| '9' -> beer 99
| '9' -> beer 99
| '+' -> incr accumulator
| '+' -> incr accumulator
done</lang>
done</syntaxhighlight>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
Line 1,894: Line 1,894:


The lyrics are based on the reference implementation. The endline and case-insensitivity are from an example in the spec.
The lyrics are based on the reference implementation. The endline and case-insensitivity are from an example in the spec.
<lang parigp>beer(n)={
<syntaxhighlight lang="parigp">beer(n)={
if(n == 1,
if(n == 1,
print("1 bottle of beer on the wall");
print("1 bottle of beer on the wall");
Line 1,916: Line 1,916:
if(v[i] == "+", accum++, error("Nasal demons"))
if(v[i] == "+", accum++, error("Nasal demons"))
)
)
};</lang>
};</syntaxhighlight>


Sample input/output:
Sample input/output:
Line 1,927: Line 1,927:
==={{header|Free Pascal}}===
==={{header|Free Pascal}}===
{{trans|Delphi}}
{{trans|Delphi}}
<lang pascal>program HQ9;
<syntaxhighlight lang="pascal">program HQ9;


procedure runCode(code: string);
procedure runCode(code: string);
Line 1,962: Line 1,962:
runCode('QqQh');
runCode('QqQh');
//runCode('HQ9+');// output to long
//runCode('HQ9+');// output to long
END.</lang>
END.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,972: Line 1,972:
=={{header|Perl}}==
=={{header|Perl}}==
This implementation uses the ''switch'' feature.
This implementation uses the ''switch'' feature.
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use warnings;
use strict;
use strict;
Line 2,023: Line 2,023:
return 'Take one down and pass it around' if $n > 0;
return 'Take one down and pass it around' if $n > 0;
return 'Go to the store and buy some more';
return 'Go to the store and buy some more';
}</lang>
}</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<span style="color: #000080;font-style:italic;">-- copied from [[99_Bottles_of_Beer#Phix|99_Bottles_of_Beer]]</span>
Line 2,077: Line 2,077:
<span style="color: #000000;">hq9</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"h9+HqQ+Qq"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">hq9</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"h9+HqQ+Qq"</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,104: Line 2,104:


=={{header|php}}==
=={{header|php}}==
<lang php>
<syntaxhighlight lang="php">
/*
/*
H Prints "Hello, world!"
H Prints "Hello, world!"
Line 2,163: Line 2,163:
function printError($chr) {
function printError($chr) {
echo "Invalid input: ". $chr;
echo "Invalid input: ". $chr;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,174: Line 2,174:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de hq9+ (Code)
<syntaxhighlight lang="picolisp">(de hq9+ (Code)
(let Accu 0
(let Accu 0
(for C (chop Code)
(for C (chop Code)
Line 2,188: Line 2,188:
(prinl) ) )
(prinl) ) )
("+" (inc 'Accu)) ) )
("+" (inc 'Accu)) ) )
Accu ) )</lang>
Accu ) )</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
Line 2,198: Line 2,198:


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.
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)
function Invoke-HQ9PlusInterpreter ([switch]$Global)
{
{
Line 2,242: Line 2,242:


Set-Alias -Name HQ9+ -Value Invoke-HQ9PlusInterpreter
Set-Alias -Name HQ9+ -Value Invoke-HQ9PlusInterpreter
</syntaxhighlight>
</lang>
Example sessions:
Example sessions:
<pre>
<pre>
Line 2,278: Line 2,278:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure hq9plus(code.s)
<syntaxhighlight lang="purebasic">Procedure hq9plus(code.s)
Protected accumulator, i, bottles
Protected accumulator, i, bottles
For i = 1 To Len(code)
For i = 1 To Len(code)
Line 2,305: Line 2,305:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
CloseConsole()
EndIf</lang>
EndIf</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
Line 2,313: Line 2,313:
=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery>$ "bottles.qky" loadfile ( if required, the source code for this can be found at
<syntaxhighlight lang="quackery">$ "bottles.qky" loadfile ( if required, the source code for this can be found at
http://rosettacode.org/wiki/99_bottles_of_beer#Quackery )
http://rosettacode.org/wiki/99_bottles_of_beer#Quackery )


Line 2,338: Line 2,338:
accumulator take echo ] is HQ9+ ( $ --> )
accumulator take echo ] is HQ9+ ( $ --> )


$ "HH+QQQQ+" HQ9+</lang>
$ "HH+QQQQ+" HQ9+</syntaxhighlight>


{{Out}}
{{Out}}
Line 2,358: Line 2,358:
strictly case-sensitive.
strictly case-sensitive.


<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
; if we `for` over the port, we won't have the program in memory for 'Q'
; if we `for` over the port, we won't have the program in memory for 'Q'
(define (parse-HQ9+ the-program)
(define (parse-HQ9+ the-program)
Line 2,405: Line 2,405:
(check-equal? (with-output-to-string (lambda () (parse-HQ9+ (make-string 10000 #\+)))) "")
(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'
;;; you can jolly well read (and sing along to) the output of '9'
)</lang>
)</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
Line 2,412: Line 2,412:
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.
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.


<lang perl6>class HQ9Interpreter {
<syntaxhighlight lang="raku" line>class HQ9Interpreter {
has @!code;
has @!code;
has $!accumulator;
has $!accumulator;
Line 2,448: Line 2,448:
$hq9.run("hHq+++Qq");
$hq9.run("hHq+++Qq");
say '';
say '';
$hq9.run("Jhq.k+hQ");</lang>
$hq9.run("Jhq.k+hQ");</syntaxhighlight>


Output:
Output:
Line 2,468: Line 2,468:


Or start a REPL (Read Execute Print Loop) and interact at the command line:
Or start a REPL (Read Execute Print Loop) and interact at the command line:
<lang perl6>my $hq9 = HQ9Interpreter.new;
<syntaxhighlight lang="raku" line>my $hq9 = HQ9Interpreter.new;
while 1 {
while 1 {
my $in = prompt('HQ9+>').chomp;
my $in = prompt('HQ9+>').chomp;
last unless $in.chars;
last unless $in.chars;
$hq9.run($in)
$hq9.run($in)
}</lang>
}</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
Note that the actual text of the &nbsp; ''Hello, world!'' &nbsp; message can differ among definitions.
Note that the actual text of the &nbsp; ''Hello, world!'' &nbsp; message can differ among definitions.
<lang rexx>/*REXX program implements the HQ9+ language. ───────────────────────────────────────*/
<syntaxhighlight lang="rexx">/*REXX program implements the HQ9+ language. ───────────────────────────────────────*/
arg pgm . /*obtain optional argument.*/
arg pgm . /*obtain optional argument.*/
accumulator=0 /*assign default to accum. */
accumulator=0 /*assign default to accum. */
Line 2,507: Line 2,507:
return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return ''; return "s" /*a simple pluralizer.*/</lang>
s: if arg(1)==1 then return ''; return "s" /*a simple pluralizer.*/</syntaxhighlight>
'''output''' &nbsp; when using the input of: &nbsp; <tt> HHH </tt>
'''output''' &nbsp; when using the input of: &nbsp; <tt> HHH </tt>
<pre>
<pre>
Line 2,516: Line 2,516:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Execute HQ9
# Project : Execute HQ9


Line 2,546: Line 2,546:
off
off
next
next
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,574: Line 2,574:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>use std::env;
<syntaxhighlight lang="rust">use std::env;


// HQ9+ requires that '+' increments an accumulator, but it's inaccessible (and thus, unused).
// HQ9+ requires that '+' increments an accumulator, but it's inaccessible (and thus, unused).
Line 2,605: Line 2,605:
fn main() {
fn main() {
execute(&env::args().nth(1).unwrap());
execute(&env::args().nth(1).unwrap());
}</lang>
}</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>def hq9plus(code: String) : String = {
<syntaxhighlight lang="scala">def hq9plus(code: String) : String = {
var out = ""
var out = ""
var acc = 0
var acc = 0
Line 2,642: Line 2,642:


println(hq9plus("HQ9+"))
println(hq9plus("HQ9+"))
</syntaxhighlight>
</lang>


=={{header|Seed7}}==
=={{header|Seed7}}==
The program below accepts the HQ9+ program as command line parameter:
The program below accepts the HQ9+ program as command line parameter:


<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const proc: runCode (in string: code) is func
const proc: runCode (in string: code) is func
Line 2,678: Line 2,678:
runCode(argv(PROGRAM)[1]);
runCode(argv(PROGRAM)[1]);
end if;
end if;
end func;</lang>
end func;</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>class HQ9Interpreter {
<syntaxhighlight lang="ruby">class HQ9Interpreter {
has pointer;
has pointer;
has accumulator;
has accumulator;
Line 2,712: Line 2,712:
}
}
}
}
}</lang>
}</syntaxhighlight>


Usage:
Usage:
<lang ruby>var hq9 = HQ9Interpreter();
<syntaxhighlight lang="ruby">var hq9 = HQ9Interpreter();
hq9.run("hHq+++Qq");</lang>
hq9.run("hHq+++Qq");</syntaxhighlight>


{{out}}
{{out}}
Line 2,728: Line 2,728:


Or start a REPL (Read Execute Print Loop) and interact at the command line:
Or start a REPL (Read Execute Print Loop) and interact at the command line:
<lang ruby>var hq9 = HQ9Interpreter();
<syntaxhighlight lang="ruby">var hq9 = HQ9Interpreter();
loop {
loop {
var in = read('HQ9+>', String) \\ break;
var in = read('HQ9+>', String) \\ break;
hq9.run(in)
hq9.run(in)
}</lang>
}</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
Line 2,748: Line 2,748:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang ecmascript>import "os" for Process
<syntaxhighlight lang="ecmascript">import "os" for Process


var hq9plus = Fn.new { |code|
var hq9plus = Fn.new { |code|
Line 2,780: Line 2,780:
} else {
} else {
hq9plus.call(args[0])
hq9plus.call(args[0])
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,786: Line 2,786:


=={{header|x86 Assembly}}==
=={{header|x86 Assembly}}==
<syntaxhighlight lang="x86 assembly">
<lang X86 Assembly>


;ds:si: pointer to asciiz string containing HQ9++ source code
;ds:si: pointer to asciiz string containing HQ9++ source code
Line 2,919: Line 2,919:
.dataBeerSong3: db 0, " bottles of beer on the wall", 0
.dataBeerSong3: db 0, " bottles of beer on the wall", 0


</syntaxhighlight>
</lang>


=={{header|XSLT}}==
=={{header|XSLT}}==
Line 2,929: Line 2,929:
Requires <code>bottles.xsl</code> (below).
Requires <code>bottles.xsl</code> (below).


<lang xml><?xml version="1.0"?>
<syntaxhighlight lang="xml"><?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- bottles.xsl defines $entire-bottles-song -->
<!-- bottles.xsl defines $entire-bottles-song -->
Line 2,981: Line 2,981:
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:template>
</xsl:stylesheet></lang>
</xsl:stylesheet></syntaxhighlight>


=====Details=====
=====Details=====
Line 2,987: Line 2,987:
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
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


<lang xml><code>qqqq</code></lang>
<syntaxhighlight lang="xml"><code>qqqq</code></syntaxhighlight>


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.
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: Line 2,999:
Requires <code>bottles.xsl</code> (below)
Requires <code>bottles.xsl</code> (below)


<lang xml><?xml version="1.0"?>
<syntaxhighlight lang="xml"><?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- bottles.xsl defines $entire-bottles-song -->
<!-- bottles.xsl defines $entire-bottles-song -->
Line 3,108: Line 3,108:
</xsl:choose>
</xsl:choose>
</xsl:template>
</xsl:template>
</xsl:stylesheet></lang>
</xsl:stylesheet></syntaxhighlight>


=====Details=====
=====Details=====
Line 3,116: Line 3,116:
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
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


<lang xml><code>qqqq</code></lang>
<syntaxhighlight lang="xml"><code>qqqq</code></syntaxhighlight>


or the programs <code>qqqq</code> and <code>++++</code> can be run in the same pass by transforming
or the programs <code>qqqq</code> and <code>++++</code> can be run in the same pass by transforming


<lang xml><programs>
<syntaxhighlight lang="xml"><programs>
<code>qqqq</code>
<code>qqqq</code>
<code>++++</code>
<code>++++</code>
</programs></lang>
</programs></syntaxhighlight>


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
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


<lang xml><results><result>qqqq
<syntaxhighlight lang="xml"><results><result>qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
</result><result accumulator="4"/></results></lang>
</result><result accumulator="4"/></results></syntaxhighlight>


====bottles.xsl====
====bottles.xsl====
Line 3,137: Line 3,137:
This sheet defines a value for the variable <code>$entire-bottles-song</code> (see [[99 Bottles of Beer]] for the general idea).
This sheet defines a value for the variable <code>$entire-bottles-song</code> (see [[99 Bottles of Beer]] for the general idea).


<lang xml><?xml version="1.0"?>
<syntaxhighlight 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">
<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 -->
<!-- Note: xmlns:lo is defined as a sort of pseudo-private namespace -->
Line 3,191: Line 3,191:
</xsl:variable>
</xsl:variable>


</xsl:stylesheet></lang>
</xsl:stylesheet></syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn runHQ9(code){
<syntaxhighlight lang="zkl">fcn runHQ9(code){
acc:=0;
acc:=0;
foreach c in (code){
foreach c in (code){
Line 3,216: Line 3,216:
(n==0 and "No more bottles" or (n==1 and "1 bottle" or "" + n + " bottles"))
(n==0 and "No more bottles" or (n==1 and "1 bottle" or "" + n + " bottles"))
+ " of beer"
+ " of beer"
}</lang>
}</syntaxhighlight>
<lang zkl>runHQ9("90HQ+junk");</lang>
<syntaxhighlight lang="zkl">runHQ9("90HQ+junk");</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>