-- Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons
-- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
--
-- | A plugin for the Haskell interpreter for the unlambda language
--
-- http://www.madore.org/~david/programs/unlambda/
module Lambdabot.Plugin.Novelty.Unlambda (unlambdaPlugin) where

import Lambdabot.Config.Novelty
import Lambdabot.Plugin
import Lambdabot.Util.Process
import Text.Regex.TDFA

unlambdaPlugin :: Module ()
unlambdaPlugin :: Module ()
unlambdaPlugin = forall st. Module st
newModule
    { moduleCmds :: ModuleT () LB [Command (ModuleT () LB)]
moduleCmds = forall (m :: * -> *) a. Monad m => a -> m a
return
        [ (String -> Command Identity
command String
"unlambda")
            { help :: Cmd (ModuleT () LB) ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"unlambda <expr>. Evaluate an unlambda expression"
            , process :: String -> Cmd (ModuleT () LB) ()
process = \String
msg -> do
                String
binary <- forall (m :: * -> *) a. MonadConfig m => Config a -> m a
getConfig Config String
unlambdaBinary
                forall (m :: * -> *). MonadIO m => IO String -> Cmd m ()
ios80 (String -> String -> (String -> String) -> IO String
run String
binary String
msg String -> String
scrub)
            }
        ]
    }

scrub :: String -> String
scrub :: String -> String
scrub = [String] -> String
unlines forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
take Int
6 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (Char
' 'forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
cleanit

cleanit :: String -> String
cleanit :: String -> String
cleanit String
s | String
s forall source source1 target.
(RegexMaker Regex CompOption ExecOption source,
 RegexContext Regex source1 target) =>
source1 -> source -> target
=~ String
terminated = String
"Terminated\n"
          | Bool
otherwise       = String
s
    where terminated :: String
terminated = String
"waitForProc"