'-d' option to pas2c webgl
authorunc0rr
Thu, 09 Jan 2014 23:54:40 +0400
branchwebgl
changeset 9982 24ea101fdc7f
parent 9980 a666e4eefd27
child 9984 84835d0ceb64
'-d' option to pas2c
project_files/hwc/CMakeLists.txt
tools/pas2c/Main.hs
tools/pas2c/Pas2C.hs
tools/pas2c/PascalPreprocessor.hs
--- a/project_files/hwc/CMakeLists.txt	Wed Jan 08 23:49:08 2014 +0100
+++ b/project_files/hwc/CMakeLists.txt	Thu Jan 09 23:54:40 2014 +0400
@@ -49,6 +49,8 @@
                         -i "${CMAKE_SOURCE_DIR}/hedgewars"
                         -o "${CMAKE_CURRENT_BINARY_DIR}"
                         -a "${CMAKE_CURRENT_BINARY_DIR}"
+                        -d "ENDIAN_LITTLE"
+                        -d "DEBUGFILE"
                    DEPENDS pas2c                     #converter tool
                            ${engine_sources_pas}     #original pascal file
                   )
--- a/tools/pas2c/Main.hs	Wed Jan 08 23:49:08 2014 +0100
+++ b/tools/pas2c/Main.hs	Thu Jan 09 23:54:40 2014 +0400
@@ -5,7 +5,7 @@
 import System.Exit
 import System.IO
 import Data.Maybe( fromMaybe, isJust, fromJust )
-import Data.List (find)
+import Data.List (find, intercalate)
 import Control.Monad
 import Pas2C
 
@@ -23,13 +23,15 @@
                 let i = flag flags isInput
                 let o = flag flags isOutput
                 let a = fromMaybe o $ liftM extractString $ find isAlt flags
+                let symbols = ["PAS2C", "FPC"] ++ (map extractString $ filter isSymbol flags)
                 hPutStrLn stdout $ "--------Pas2C Config--------"
                 hPutStrLn stdout $ "Main module: " ++ m
                 hPutStrLn stdout $ "Input path : " ++ i
                 hPutStrLn stdout $ "Output path: " ++ o
                 hPutStrLn stdout $ "Altern path: " ++ a
+                hPutStrLn stdout $ "Symbols defined: " ++ (intercalate ", " symbols)
                 hPutStrLn stdout $ "----------------------------"
-                pas2C m (i++"/") (o++"/") (a++"/")
+                pas2C m (i++"/") (o++"/") (a++"/") symbols
                 hPutStrLn stdout $ "----------------------------"
                       | otherwise ->  error $ usageInfo header options
           (_,     nonOpts, [])     -> error $ "unrecognized arguments: " ++ unwords nonOpts
@@ -40,7 +42,12 @@
         flag f = extractString . fromJust . flip find f
 
 
-data Flag = HelpMessage | Name String | Input String | Output String | Alternate String
+data Flag = HelpMessage
+          | Name String
+          | Input String
+          | Output String
+          | Alternate String
+          | Symbol String
 
 
 extractString :: Flag -> String
@@ -48,9 +55,10 @@
 extractString (Input s) = s
 extractString (Output s) = s
 extractString (Alternate s) = s
+extractString (Symbol s) = s
 extractString _ = undefined
 
-isName, isInput, isOutput, isAlt :: Flag -> Bool
+isName, isInput, isOutput, isAlt, isSymbol :: Flag -> Bool
 isName (Name _) = True
 isName _ = False
 isInput (Input _) = True
@@ -59,6 +67,8 @@
 isOutput _ = False
 isAlt (Alternate _) = True
 isAlt _ = False
+isSymbol (Symbol _) = True
+isSymbol _ = False
 
 options :: [OptDescr Flag]
 options = [
@@ -66,6 +76,7 @@
     Option ['n'] ["name"]      (ReqArg Name "MAIN")     "name of the main Pascal module",
     Option ['i'] ["input"]     (ReqArg Input "DIR")     "input directory, where .pas files will be read",
     Option ['o'] ["output"]    (ReqArg Output "DIR")    "output directory, where .c/.h files will be written",
-    Option ['a'] ["alternate"] (ReqArg Alternate "DIR") "alternate input directory, for out of source builds"
+    Option ['a'] ["alternate"] (ReqArg Alternate "DIR") "alternate input directory, for out of source builds",
+    Option ['d'] ["define"]    (ReqArg Symbol "SYMBOL") "define symbol"
   ]
 
--- a/tools/pas2c/Pas2C.hs	Wed Jan 08 23:49:08 2014 +0100
+++ b/tools/pas2c/Pas2C.hs	Thu Jan 09 23:54:40 2014 +0400
@@ -96,8 +96,8 @@
 docToLower :: Doc -> Doc
 docToLower = text . map toLower . render
 
-pas2C :: String -> String -> String -> String -> IO ()
-pas2C fn inputPath outputPath alternateInputPath = do
+pas2C :: String -> String -> String -> String -> [String] -> IO ()
+pas2C fn inputPath outputPath alternateInputPath symbols = do
     s <- flip execStateT initState $ f fn
     renderCFiles s outputPath
     where
@@ -111,7 +111,7 @@
             print ("Preprocessing '" ++ fileName ++ ".pas'... ")
             fc' <- liftIO
                 $ tryJust (guard . isDoesNotExistError)
-                $ preprocess inputPath alternateInputPath (fileName ++ ".pas")
+                $ preprocess inputPath alternateInputPath (fileName ++ ".pas") symbols
             case fc' of
                 (Left a) -> do
                     modify (Map.insert fileName (System []))
--- a/tools/pas2c/PascalPreprocessor.hs	Wed Jan 08 23:49:08 2014 +0100
+++ b/tools/pas2c/PascalPreprocessor.hs	Thu Jan 09 23:54:40 2014 +0400
@@ -17,19 +17,9 @@
         , (try $ string "//") >> manyTill anyChar (try newline) >> return "\n"
         ]
 
-
-initDefines = Map.fromList [
-    ("FPC", "")
-    , ("PAS2C", "")
-    , ("DEBUGFILE", "")
---    , ("WEBGL", "")
---    , ("AI_MAINTHREAD", "")
-    , ("ENDIAN_LITTLE", "")
-    ]
-
-preprocess :: String -> String -> String -> IO String
-preprocess inputPath alternateInputPath fn = do
-    r <- runParserT (preprocessFile (inputPath ++ fn)) (initDefines, [True]) "" ""
+preprocess :: String -> String -> String -> [String] -> IO String
+preprocess inputPath alternateInputPath fn symbols = do
+    r <- runParserT (preprocessFile (inputPath ++ fn)) (Map.fromList $ map (\s -> (s, "")) symbols, [True]) "" ""
     case r of
          (Left a) -> do
              hPutStrLn stderr (show a)