author | alfadur |
Sat, 23 Mar 2019 03:44:11 +0300 | |
changeset 14722 | 16024046d458 |
parent 10015 | 4feced261c68 |
permissions | -rw-r--r-- |
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 | 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 | 26 |
let symbols = ["PAS2C", "FPC"] ++ (map extractString $ filter isSymbol flags) |
7979 | 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 | 32 |
hPutStrLn stdout $ "Symbols defined: " ++ (intercalate ", " symbols) |
7979 | 33 |
hPutStrLn stdout $ "----------------------------" |
9982 | 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 | 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 | 45 |
data Flag = HelpMessage |
46 |
| Name String |
|
47 |
| Input String |
|
48 |
| Output String |
|
49 |
| Alternate String |
|
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 | 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 | 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 | 70 |
isSymbol (Symbol _) = True |
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 | 79 |
Option ['a'] ["alternate"] (ReqArg Alternate "DIR") "alternate input directory, for out of source builds", |
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 |