Task

Implement a    HQ9+    interpreter or compiler.




open unsafe.console char unsafe.cell imperative
 
eval src = eval' src
     where eval' [] = ()
           eval' (x::xs) | be 'H' = h() `seq` eval' xs
                         | be 'Q' = q() `seq` eval' xs
                         | be '9' = n() `seq` eval' xs
                         | be '+' = p() `seq` eval' xs
                         | else = fail ("Unrecognized " ++ x)
                         where r = ref 0
                               be c = char.upper x == c
                               h () = writen "Hello, world!"
                               q () = writen src
                               p () = r.+
                               n () = bottles [99,98..1]
                                  where bottles [] = ()
                                        bottles (x::xs) = rec write 
                                          (show x) " bottles of beer of the wall\r\n"
                                          (show x) " bottles of beer\r\n"
                                          "Take one down, pass it around\r\n"
                                          `seq` bottles xs