HQ9+のインタプリタを作ってみた

30分プログラム、その737。HQ9+のインタプリタを作ってみました。
HQ9+の仕様はHQ9+ - Wikipediaを見てください。要するに、おどろくほど実用性ををもたないネタ言語です。

使い方

sml> run "HHQ+HQ++";
val it = "Hello,world!Hello,world!HHQ+HQ++Hello,world!HHQ+HQ++" : string

ソースコード

fun beer 0 =
    ["No more bottles of beer on the wall, no more bottles of beer.",
     "Go to the store and buy some more, 99 bottles of beer on the wall."]
  | beer 1 =
    "1 bottle of beer on the wall, 1 bottle of beer."::
    "Take one down and pass it around, no more bottles of beer on the wall."::
    "" ::
    beer 0
  | beer n =
    (String.concat [Int.toString n,
		    " bottles of beer on the wall, ",
		    Int.toString n,
		    " bottles of beer."]) ::
    (String.concat ["Take one down and pass it around, ",
		    Int.toString (n-1),
		    " bottles of beer on the wall."]) ::
    "" ::
    beer (n-1)

fun runChar self c =
    case c of
	#"H" => ["Hello,world!"]
      | #"Q" => [self]
      | #"9" => beer 99
      | _ => []

fun run src =
    String.concat
	(List.concat (List.map (runChar src) (explode src)));