RCHQ9+/Go

From Rosetta Code
Revision as of 01:33, 5 May 2011 by Sonia (talk | contribs) (Go solution)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
RCHQ9+/Go is an implementation of HQ9+. Other implementations of HQ9+.
RCHQ9+/Go is part of RCHQ9+. You may find other members of RCHQ9+ at Category:RCHQ9+.

This interpreter accepts HQ9+ source as the first command line argument. <lang go>package main

import (

   "fmt"
   "os"

)

func main() {

   if len(os.Args) < 2 {
       return
   }
   for _, c := range os.Args[1] {
       switch c {
       case 'H':
           fmt.Println("hello, world")
       case 'Q':
           fmt.Printf("%s%c%s%c\n", q, 96, q, 96)
       case '9':
           const l1t = " of beer on the wall"
           const l2t = " of beer.\nTake one down, pass it around,\n"
           const l1p = " bottles"+l1t
           const l2p = " bottles"+l2t
           fmt.Print(99, l1p, ",\n", 99, l2p)
           for n := 98; n > 1; n-- {
               fmt.Print(n, l1p, ".\n\n", n, l1p, ",\n", n, l2p)
           }
           fmt.Print("One bottle"+l1t+".\n\n")
           fmt.Print("One bottle"+l1t+",\nOne bottle"+l2t+"No"+l1p+".\n")
       case '+':
           a++
       }
   }

}

var a int var q = `package main

import (

   "fmt"
   "os"

)

func main() {

   if len(os.Args) < 2 {
       return
   }
   for _, c := range os.Args[1] {
       switch c {
       case 'H':
           fmt.Println("hello, world")
       case 'Q':
           fmt.Printf("%s%c%s%c\n", q, 96, q, 96)
       case '9':
           const l1t = " of beer on the wall"
           const l2t = " of beer.\nTake one down, pass it around,\n"
           const l1p = " bottles"+l1t
           const l2p = " bottles"+l2t
           fmt.Print(99, l1p, ",\n", 99, l2p)
           for n := 98; n > 1; n-- {
               fmt.Print(n, l1p, ".\n\n", n, l1p, ",\n", n, l2p)
           }
           fmt.Print("One bottle"+l1t+".\n\n")
           fmt.Print("One bottle"+l1t+",\nOne bottle"+l2t+"No"+l1p+".\n")
       case '+':
           a++
       }
   }

}

var a int var q = `</lang> (Gosh that made cut and paste easier...cut once, paste twice...)

Anyway, example session:

$ HQ9+ H+H
hello, world
hello, world

Another, showing compile link and run of Q output

$ HQ9+ Q >q.go
$ 8g q.go
$ 8l q.8
$ 8.out H
hello, world