summaryrefslogtreecommitdiff
path: root/tool/Compiler.hs
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-03-02 12:56:36 +0100
committerPéter Diviánszky <divipp@gmail.com>2016-03-02 12:56:36 +0100
commit2863e31d34d0131d2dd3cc1f4df6c399c23187c9 (patch)
tree7067d295dca3c7b77d3ed398d0bb0b7cdca98b28 /tool/Compiler.hs
parent66255f38134ea5af170d37affc5582744d50bc7d (diff)
first version of parse only
Diffstat (limited to 'tool/Compiler.hs')
-rw-r--r--tool/Compiler.hs81
1 files changed, 42 insertions, 39 deletions
diff --git a/tool/Compiler.hs b/tool/Compiler.hs
index ccc50d7d..359b23d6 100644
--- a/tool/Compiler.hs
+++ b/tool/Compiler.hs
@@ -1,4 +1,4 @@
1{-# LANGUAGE RecordWildCards #-} 1import Control.Monad
2import Options.Applicative 2import Options.Applicative
3import Data.Aeson 3import Data.Aeson
4import qualified Data.ByteString.Lazy as B 4import qualified Data.ByteString.Lazy as B
@@ -8,53 +8,56 @@ import Paths_lambdacube_compiler (version)
8 8
9import LambdaCube.Compiler 9import LambdaCube.Compiler
10 10
11data Config 11addInfo i p = info (helper <*> p) i
12 = Config
13 { srcName :: String
14 , backend :: Backend
15 , includePaths :: [FilePath]
16 , pretty :: Bool
17 , output :: Maybe String
18 }
19
20sample :: Parser Config
21sample = Config
22 <$> argument str (metavar "SOURCE_FILE")
23 <*> flag OpenGL33 WebGL1 (long "webgl" <> help "generate WebGL 1.0 pipeline" )
24 <*> pure ["."]
25 <*> switch (long "pretty" <> help "pretty prints pipeline")
26 <*> optional (strOption (long "output" <> short 'o' <> metavar "FILENAME" <> help "output file name"))
27 12
28main :: IO () 13main :: IO ()
29main = compile =<< execParser opts 14main = join $ execParser $ addInfo i $ subparser (
15 command "compile" (addInfo (progDesc "compiles LambdaCube3D source to JSON IR") compile')
16 <> command "parse" (addInfo (progDesc "parses LambdaCube3D source") $ parse
17 <$> argument str (metavar "SOURCE_FILE")
18 <*> flag OpenGL33 WebGL1 (long "webgl" <> help "generate WebGL 1.0 pipeline" )
19 <*> pure ["."]
20 <*> optional (strOption (long "output" <> short 'o' <> metavar "FILENAME" <> help "output file name"))
21 )
22 <> command "pretty" (addInfo (progDesc "pretty prints JSON IR") $ prettyPrint
23 <$> argument str (metavar "SOURCE_FILE")
24 <*> optional (strOption (long "output" <> short 'o' <> metavar "FILENAME" <> help "output file name"))
25 )) <|> compile'
30 where 26 where
31 opts = info (helper <*> sample) 27 compile' = (compile
32 ( fullDesc 28 <$> argument str (metavar "SOURCE_FILE")
33 <> progDesc "compiles LambdaCube graphics pipeline source to JSON IR" 29 <*> flag OpenGL33 WebGL1 (long "webgl" <> help "generate WebGL 1.0 pipeline" )
34 <> header ("LambdaCube 3D compiler " ++ showVersion version)) 30 <*> pure ["."]
31 <*> optional (strOption (long "output" <> short 'o' <> metavar "FILENAME" <> help "output file name"))
32 )
35 33
36compile :: Config -> IO () 34 i = fullDesc
37compile cfg@Config{..} = do 35 <> progDesc "executes command (default to compile if no command is given)"
36 <> header ("LambdaCube 3D compiler " ++ showVersion version)
37
38prettyPrint srcName output = do
39 let baseName = dropExtension srcName
40 withOutName n = maybe n id output
41 json <- B.readFile srcName
42 case eitherDecode json :: Either String Pipeline of
43 Left err -> putStrLn err
44 Right ppl -> writeFile (withOutName $ baseName <> ".ppl") $ prettyShowUnlines ppl
45
46parse srcName backend includePaths output = do
47 pplRes <- parseModule includePaths srcName
48 case pplRes of
49 Left err -> fail err
50 Right ppl -> maybe (putStrLn ppl) (`writeFile` ppl) output
51
52compile srcName backend includePaths output = do
38 let ext = takeExtension srcName 53 let ext = takeExtension srcName
39 baseName | ext == ".lc" = dropExtension srcName 54 baseName | ext == ".lc" = dropExtension srcName
40 | otherwise = srcName 55 | otherwise = srcName
41 withOutName n = maybe n id output 56 withOutName n = maybe n id output
42 case ext of 57 do
43 ".json" | pretty -> prettyPrint cfg
44 _ -> do
45 pplRes <- compileMain includePaths backend srcName 58 pplRes <- compileMain includePaths backend srcName
46 case pplRes of 59 case pplRes of
47 Left err -> fail err 60 Left err -> fail err
48 Right ppl -> case pretty of 61 Right ppl -> B.writeFile (withOutName $ baseName <> ".json") $ encode ppl
49 False -> B.writeFile (withOutName $ baseName <> ".json") $ encode ppl 62-- True -> writeFile (withOutName $ baseName <> ".ppl") $ prettyShowUnlines ppl
50 True -> writeFile (withOutName $ baseName <> ".ppl") $ prettyShowUnlines ppl
51
52prettyPrint :: Config -> IO ()
53prettyPrint Config{..} = do
54 let baseName = dropExtension srcName
55 withOutName n = maybe n id output
56 json <- B.readFile srcName
57 case eitherDecode json :: Either String Pipeline of
58 Left err -> putStrLn err
59 Right ppl -> writeFile (withOutName $ baseName <> ".ppl") $ prettyShowUnlines ppl
60 63