-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainModule.hs
43 lines (36 loc) · 1.82 KB
/
MainModule.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
{-# LANGUAGE Haskell2010 #-}
-- Contains the command line argument parser and feedback displayer that can be used for the main
-- modules of the executables.
module MainModule where
import Feedback
import Control.Monad
import Data.List
import Data.Maybe
import System.IO
import Debug.Trace
import System.Environment
import System.Exit
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as B
-- The actions performed by each executable can be characterised by this type: a sequence of bytes
-- (containing text encoded in either ASCII or UTF-8) is read from stdin or a file and transformed
-- into another byte sequence that is to be send to standard out. The Bytestring are lazy.
type ProgramOperation = ByteString -> Feedback ByteString
-- Creates a 'main' function from a ProgramOperation.
makeMain :: ProgramOperation -> IO ()
makeMain op = do args <- getArgs
-- Use the argument as input file name, or use stdin when there are no arguments.
input <- case args of
[] -> B.getContents
[path] -> B.readFile path
let feedback = op input
case runFeedback feedback of
(Just outp, msg) | not $ hasError feedback ->
do -- Dump warnings, if any.
when (msg /= "") $ hPutStrLn stderr msg
-- Print output.
B.putStr outp
(_, msg) -> do -- Compilation failed. Print errors/warnings.
hPutStrLn stderr msg
-- Terminate with an exit code indicating failure.
exitFailure