tools/pas2c/Main.hs
author unc0rr
Tue, 02 Apr 2013 23:43:39 +0400
branchwebgl
changeset 8836 7a474fcc343d
parent 7979 a3974abc62d3
child 9982 24ea101fdc7f
permissions -rw-r--r--
Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7965
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
     1
module Main( main ) where
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
     2
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
     3
import System.Console.GetOpt
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
     4
import System.Environment
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
     5
import System.Exit
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
     6
import System.IO
8836
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
     7
import Data.Maybe( fromMaybe, isJust, fromJust )
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
     8
import Data.List (find)
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
     9
import Control.Monad
7965
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    10
import Pas2C
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    11
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    12
main = do
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    13
    args <- getArgs
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    14
    if length args == 0
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    15
    then do
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    16
        name <- getProgName
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    17
        hPutStrLn stderr $ usageInfo header options
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    18
        exitFailure
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    19
    else do
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    20
        case getOpt RequireOrder options args of
8836
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    21
          (flags, [],      []) | enoughFlags flags -> do
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    22
                let m = flag flags isName
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    23
                let i = flag flags isInput
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    24
                let o = flag flags isOutput
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    25
                let a = fromMaybe o $ liftM extractString $ find isAlt flags
7979
a3974abc62d3 verbose pas2c for parsed args
koda
parents: 7975
diff changeset
    26
                hPutStrLn stdout $ "--------Pas2C Config--------"
8836
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    27
                hPutStrLn stdout $ "Main module: " ++ m
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    28
                hPutStrLn stdout $ "Input path : " ++ i
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    29
                hPutStrLn stdout $ "Output path: " ++ o
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    30
                hPutStrLn stdout $ "Altern path: " ++ a
7979
a3974abc62d3 verbose pas2c for parsed args
koda
parents: 7975
diff changeset
    31
                hPutStrLn stdout $ "----------------------------"
8836
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    32
                pas2C m (i++"/") (o++"/") (a++"/")
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    33
                hPutStrLn stdout $ "----------------------------"
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    34
                      | otherwise ->  error $ usageInfo header options
7965
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    35
          (_,     nonOpts, [])     -> error $ "unrecognized arguments: " ++ unwords nonOpts
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    36
          (_,     _,       msgs)   -> error $ usageInfo header options
8836
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    37
    where 
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    38
        header = "Freepascal to C conversion! Please specify -n -i -o options.\n"
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    39
        enoughFlags f = and $ map (isJust . flip find f) [isName, isInput, isOutput]
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    40
        flag f = extractString . fromJust . flip find f
7965
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    41
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    42
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    43
data Flag = HelpMessage | Name String | Input String | Output String | Alternate String
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    44
8836
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    45
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    46
extractString :: Flag -> String
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    47
extractString (Name s) = s
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    48
extractString (Input s) = s
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    49
extractString (Output s) = s
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    50
extractString (Alternate s) = s
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    51
extractString _ = undefined
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    52
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    53
isName, isInput, isOutput, isAlt :: Flag -> Bool
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    54
isName (Name _) = True
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    55
isName _ = False
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    56
isInput (Input _) = True
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    57
isInput _ = False
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    58
isOutput (Output _) = True
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    59
isOutput _ = False
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    60
isAlt (Alternate _) = True
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    61
isAlt _ = False
7a474fcc343d Bring some sanity here. Feel free to specify parameters in any order and in any notation (full name or one-letter).
unc0rr
parents: 7979
diff changeset
    62
7965
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    63
options :: [OptDescr Flag]
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    64
options = [
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    65
    Option ['h'] ["help"]      (NoArg HelpMessage)      "print this help message",
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    66
    Option ['n'] ["name"]      (ReqArg Name "MAIN")     "name of the main Pascal module",
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    67
    Option ['i'] ["input"]     (ReqArg Input "DIR")     "input directory, where .pas files will be read",
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    68
    Option ['o'] ["output"]    (ReqArg Output "DIR")    "output directory, where .c/.h files will be written",
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    69
    Option ['a'] ["alternate"] (ReqArg Alternate "DIR") "alternate input directory, for out of source builds"
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    70
  ]
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    71