-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
27 lines (18 loc) · 875 Bytes
/
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
module Main where
import System.Environment (getArgs)
import Control.Monad (foldM)
printHelp = putStrLn . unlines $
["Usage: macroista input"]
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:_) = Just x
includeFiles content = foldM includeDirective "" $ lines content where
includeDirective content' line = do
let filename = let words' = words line in safeHead words' >>= (\firstWord -> if firstWord == "@include" then Just (unwords $ tail words') else Nothing)
case filename of
Nothing -> return (content' ++ "\n" ++ line)
Just f -> readFile f >>= (\fc -> return (content' ++ "\n" ++ fc))
main :: IO ()
main = getArgs >>= \arguments ->
case safeHead arguments of Nothing -> printHelp
Just filename -> readFile filename >>= includeFiles >>= putStrLn