Execute HQ9+: Difference between revisions

Content added Content deleted
(Added Kotlin)
Line 1,006: Line 1,006:
return out;
return out;
}</lang>
}</lang>

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

fun hq9plus(code: String) {
var acc = 0
val sb = StringBuilder()
for (c in code) {
sb.append(
when (c) {
'h', 'H' -> "Hello, world!\n"
'q', 'Q' -> code + "\n"
'9'-> {
val sb2 = StringBuilder()
for (i in 99 downTo 1) {
val s = if (i > 1) "s" else ""
sb2.append("$i bottle$s of beer on the wall\n")
sb2.append("$i bottle$s of beer\n")
sb2.append("Take one down, pass it around\n")
}
sb2.append("No more bottles of beer on the wall!\n")
sb2.toString()
}

'+' -> { acc++; "" } // yeah, it's weird!
else -> throw IllegalArgumentException("Code contains illegal operation '$c'")
}
)
}
println(sb)
}

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

{{out}}
<pre>
Hello, world!
hq9+
99 bottles of beer on the wall
99 bottles of beer
Take one down, pass it around
....
2 bottles of beer on the wall
2 bottles of beer
Take one down, pass it around
1 bottle of beer on the wall
1 bottle of beer
Take one down, pass it around
No more bottles of beer on the wall!
</pre>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==