-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.hs
67 lines (55 loc) · 1.82 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE RecordWildCards #-}
module Main where
import StringParser
import System.Environment (getArgs)
import Text.Parsec (parse)
import Control.Monad.Random
import System.IO
charify :: MonadRandom m => Tok c -> m [c]
charify (Tok {..}) = concat <$> do
n <- repeatTime getSuffix
replicateM (fromInteger n) $ case getToken of
SingalChar c ->
return [c]
CharRange cs -> do
c <- uniform cs
return [c]
SubExpr ts ->
concat <$> (sequence $ charify <$> ts)
MultiWaySubExpr tss -> do
ts <- uniform tss
concat <$> (sequence $ charify <$> ts)
repeatTime :: MonadRandom m => Suffix -> m Integer
repeatTime s = case s of
Nil -> return 1
Repeat n -> return n
RepeatRange (l, u) -> uniform [l..u]
Some -> randomlyRise simpleRiseChance 1
Many -> randomlyRise simpleRiseChance 0
randomlyRise :: MonadRandom m => Double -> Integer -> m Integer
randomlyRise chance n = do
dice <- getRandom :: forall m2. MonadRandom m2 => m2 Double
if dice < chance then
randomlyRise chance (n+1)
else return n
noInputMessage :: String
noInputMessage = "No args detached...\n" ++
"Entering interactive mode...\n"
mkRandomSyntax :: String -> IO String
mkRandomSyntax s = case parse mainParser "" s of
Left err -> return $ show err
Right res -> (fmap concat. sequence . fmap charify) res
mainInteract :: IO ()
mainInteract = do
putStr "> "
line <- getLine
cho <- mkRandomSyntax line
putStrLn cho
main :: IO ()
main = do
args <- getArgs
hSetBuffering stdout NoBuffering
case args of
[] -> putStr noInputMessage >> forever mainInteract
_ -> forM args (\l -> mkRandomSyntax l >>= putStrLn) >> return ()