Task

Implement a    HQ9+    interpreter or compiler.




func eval(code) {
    var accumulator = 0
    var opcodes = (
        "h": () => print("Hello, World!"),
        "q": () => print(code),
        "9": () => {
                var quantity = 99
                while quantity > 1 {
                    print("\(quantity) bottles of beer on the wall, \(quantity) bottles of beer.")
                    print("Take one down and pass it around, \(quantity - 1) bottles of beer.")
                    quantity -= 1
                }
                print("1 bottle of beer on the wall, 1 bottle of beer.")
                print("Take one down and pass it around, no more bottles of beer on the wall.\n")
                print("No more bottles of beer on the wall, no more bottles of beer.")
                print("Go to the store and buy some more, 99 bottles of beer on the wall.")
            },
        "+": () => { accumulator += 1 }
    )
 
    for c in code {
        opcodes[c.Lower()]()
    }
}