tools/pas2c/Main.hs
author sheepluva
Sat, 30 Apr 2016 00:02:33 +0200
changeset 11784 3ec3048c339c
parent 10015 4feced261c68
permissions -rw-r--r--
koda told me that putting in one finger wasn't enough
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 )
9982
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
     8
import Data.List (find, intercalate)
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
     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
9982
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    26
                let symbols = ["PAS2C", "FPC"] ++ (map extractString $ filter isSymbol flags)
7979
a3974abc62d3 verbose pas2c for parsed args
koda
parents: 7975
diff changeset
    27
                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
    28
                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
    29
                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
    30
                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
    31
                hPutStrLn stdout $ "Altern path: " ++ a
9982
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    32
                hPutStrLn stdout $ "Symbols defined: " ++ (intercalate ", " symbols)
7979
a3974abc62d3 verbose pas2c for parsed args
koda
parents: 7975
diff changeset
    33
                hPutStrLn stdout $ "----------------------------"
9982
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    34
                pas2C m (i++"/") (o++"/") (a++"/") symbols
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
    35
                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
    36
                      | 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
    37
          (_,     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
    38
          (_,     _,       msgs)   -> error $ usageInfo header options
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 9982
diff changeset
    39
    where
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
    40
        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
    41
        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
    42
        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
    43
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    44
9982
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    45
data Flag = HelpMessage
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    46
          | Name String
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    47
          | Input String
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    48
          | Output String
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    49
          | Alternate String
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    50
          | Symbol String
7965
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    51
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
    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
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
    54
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
    55
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
    56
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
    57
extractString (Alternate s) = s
9982
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    58
extractString (Symbol s) = s
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
    59
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
    60
9982
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    61
isName, isInput, isOutput, isAlt, isSymbol :: Flag -> Bool
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
    62
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
    63
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
    64
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
    65
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
    66
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
    67
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
    68
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
    69
isAlt _ = False
9982
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    70
isSymbol (Symbol _) = True
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    71
isSymbol _ = False
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
    72
7965
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    73
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
    74
options = [
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    75
    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
    76
    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
    77
    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
    78
    Option ['o'] ["output"]    (ReqArg Output "DIR")    "output directory, where .c/.h files will be written",
9982
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    79
    Option ['a'] ["alternate"] (ReqArg Alternate "DIR") "alternate input directory, for out of source builds",
24ea101fdc7f '-d' option to pas2c
unc0rr
parents: 8836
diff changeset
    80
    Option ['d'] ["define"]    (ReqArg Symbol "SYMBOL") "define symbol"
7965
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    81
  ]
b518458f83e6 pas2c is now fully parametric with command line options (maybe unC0Rr needs to clean this up a little)
koda
parents:
diff changeset
    82