Debian Codegraph @

src/Lambdabot/Plugin/Novelty/BF.hs

raw
1 -- Copyright (c) 2006 Jason Dagit - http://www.codersbase.com/
2 -- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
3
4 -- | A plugin for the Haskell interpreter for the brainf*ck language
5 -- http://www.muppetlabs.com/~breadbox/bf/
6 module Lambdabot.Plugin.Novelty.BF (bfPlugin) where
7
8 import Lambdabot.Config.Novelty
9 import Lambdabot.Plugin
10 import Lambdabot.Util.Process
11
12 import Data.Char
13 import Text.Regex.TDFA
14
15 bfPlugin :: Module ()
16 bfPlugin = newModule
17 { moduleCmds = return
18 [ (command "bf")
19 { help = say "bf <expr>. Evaluate a brainf*ck expression"
20 , process = \msg -> do
21 bf <- getConfig bfBinary
22 ios80 (run bf msg scrub)
23 }
24 ]
25 }
26
27 -- Clean up output
28 scrub :: String -> String
29 scrub = unlines . take 6 . map (' ':) . filter (not.null) . map cleanit . lines
30
31 cleanit :: String -> String
32 cleanit s | s =~ terminated = "Terminated\n"
33 | otherwise = filter printable s
34 where terminated = "waitForProc"
35 -- the printable ascii chars are in the range [32 .. 126]
36 -- according to wikipedia:
37 -- http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
38 printable x = 31 < ord x && ord x < 127
39