tools/pas2c/Main.hs
branchwebgl
changeset 9982 24ea101fdc7f
parent 8836 7a474fcc343d
child 10015 4feced261c68
equal deleted inserted replaced
9980:a666e4eefd27 9982:24ea101fdc7f
     3 import System.Console.GetOpt
     3 import System.Console.GetOpt
     4 import System.Environment
     4 import System.Environment
     5 import System.Exit
     5 import System.Exit
     6 import System.IO
     6 import System.IO
     7 import Data.Maybe( fromMaybe, isJust, fromJust )
     7 import Data.Maybe( fromMaybe, isJust, fromJust )
     8 import Data.List (find)
     8 import Data.List (find, intercalate)
     9 import Control.Monad
     9 import Control.Monad
    10 import Pas2C
    10 import Pas2C
    11 
    11 
    12 main = do
    12 main = do
    13     args <- getArgs
    13     args <- getArgs
    21           (flags, [],      []) | enoughFlags flags -> do
    21           (flags, [],      []) | enoughFlags flags -> do
    22                 let m = flag flags isName
    22                 let m = flag flags isName
    23                 let i = flag flags isInput
    23                 let i = flag flags isInput
    24                 let o = flag flags isOutput
    24                 let o = flag flags isOutput
    25                 let a = fromMaybe o $ liftM extractString $ find isAlt flags
    25                 let a = fromMaybe o $ liftM extractString $ find isAlt flags
       
    26                 let symbols = ["PAS2C", "FPC"] ++ (map extractString $ filter isSymbol flags)
    26                 hPutStrLn stdout $ "--------Pas2C Config--------"
    27                 hPutStrLn stdout $ "--------Pas2C Config--------"
    27                 hPutStrLn stdout $ "Main module: " ++ m
    28                 hPutStrLn stdout $ "Main module: " ++ m
    28                 hPutStrLn stdout $ "Input path : " ++ i
    29                 hPutStrLn stdout $ "Input path : " ++ i
    29                 hPutStrLn stdout $ "Output path: " ++ o
    30                 hPutStrLn stdout $ "Output path: " ++ o
    30                 hPutStrLn stdout $ "Altern path: " ++ a
    31                 hPutStrLn stdout $ "Altern path: " ++ a
       
    32                 hPutStrLn stdout $ "Symbols defined: " ++ (intercalate ", " symbols)
    31                 hPutStrLn stdout $ "----------------------------"
    33                 hPutStrLn stdout $ "----------------------------"
    32                 pas2C m (i++"/") (o++"/") (a++"/")
    34                 pas2C m (i++"/") (o++"/") (a++"/") symbols
    33                 hPutStrLn stdout $ "----------------------------"
    35                 hPutStrLn stdout $ "----------------------------"
    34                       | otherwise ->  error $ usageInfo header options
    36                       | otherwise ->  error $ usageInfo header options
    35           (_,     nonOpts, [])     -> error $ "unrecognized arguments: " ++ unwords nonOpts
    37           (_,     nonOpts, [])     -> error $ "unrecognized arguments: " ++ unwords nonOpts
    36           (_,     _,       msgs)   -> error $ usageInfo header options
    38           (_,     _,       msgs)   -> error $ usageInfo header options
    37     where 
    39     where 
    38         header = "Freepascal to C conversion! Please specify -n -i -o options.\n"
    40         header = "Freepascal to C conversion! Please specify -n -i -o options.\n"
    39         enoughFlags f = and $ map (isJust . flip find f) [isName, isInput, isOutput]
    41         enoughFlags f = and $ map (isJust . flip find f) [isName, isInput, isOutput]
    40         flag f = extractString . fromJust . flip find f
    42         flag f = extractString . fromJust . flip find f
    41 
    43 
    42 
    44 
    43 data Flag = HelpMessage | Name String | Input String | Output String | Alternate String
    45 data Flag = HelpMessage
       
    46           | Name String
       
    47           | Input String
       
    48           | Output String
       
    49           | Alternate String
       
    50           | Symbol String
    44 
    51 
    45 
    52 
    46 extractString :: Flag -> String
    53 extractString :: Flag -> String
    47 extractString (Name s) = s
    54 extractString (Name s) = s
    48 extractString (Input s) = s
    55 extractString (Input s) = s
    49 extractString (Output s) = s
    56 extractString (Output s) = s
    50 extractString (Alternate s) = s
    57 extractString (Alternate s) = s
       
    58 extractString (Symbol s) = s
    51 extractString _ = undefined
    59 extractString _ = undefined
    52 
    60 
    53 isName, isInput, isOutput, isAlt :: Flag -> Bool
    61 isName, isInput, isOutput, isAlt, isSymbol :: Flag -> Bool
    54 isName (Name _) = True
    62 isName (Name _) = True
    55 isName _ = False
    63 isName _ = False
    56 isInput (Input _) = True
    64 isInput (Input _) = True
    57 isInput _ = False
    65 isInput _ = False
    58 isOutput (Output _) = True
    66 isOutput (Output _) = True
    59 isOutput _ = False
    67 isOutput _ = False
    60 isAlt (Alternate _) = True
    68 isAlt (Alternate _) = True
    61 isAlt _ = False
    69 isAlt _ = False
       
    70 isSymbol (Symbol _) = True
       
    71 isSymbol _ = False
    62 
    72 
    63 options :: [OptDescr Flag]
    73 options :: [OptDescr Flag]
    64 options = [
    74 options = [
    65     Option ['h'] ["help"]      (NoArg HelpMessage)      "print this help message",
    75     Option ['h'] ["help"]      (NoArg HelpMessage)      "print this help message",
    66     Option ['n'] ["name"]      (ReqArg Name "MAIN")     "name of the main Pascal module",
    76     Option ['n'] ["name"]      (ReqArg Name "MAIN")     "name of the main Pascal module",
    67     Option ['i'] ["input"]     (ReqArg Input "DIR")     "input directory, where .pas files will be read",
    77     Option ['i'] ["input"]     (ReqArg Input "DIR")     "input directory, where .pas files will be read",
    68     Option ['o'] ["output"]    (ReqArg Output "DIR")    "output directory, where .c/.h files will be written",
    78     Option ['o'] ["output"]    (ReqArg Output "DIR")    "output directory, where .c/.h files will be written",
    69     Option ['a'] ["alternate"] (ReqArg Alternate "DIR") "alternate input directory, for out of source builds"
    79     Option ['a'] ["alternate"] (ReqArg Alternate "DIR") "alternate input directory, for out of source builds",
       
    80     Option ['d'] ["define"]    (ReqArg Symbol "SYMBOL") "define symbol"
    70   ]
    81   ]
    71 
    82