diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2016-02-14 01:13:28 +0100 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2016-02-14 01:13:40 +0100 |
commit | cc11c0743bbab50f0f9d7bcc33fd41533a280870 (patch) | |
tree | 29d043291dfb02b15b2fd3bd4719e7998451003c /tool | |
parent | 809846c4a19eed59ecdaf286fdc97fc54cea1170 (diff) |
improve lc
Diffstat (limited to 'tool')
-rw-r--r-- | tool/Compiler.hs | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/tool/Compiler.hs b/tool/Compiler.hs index 3d5076c8..e1984f80 100644 --- a/tool/Compiler.hs +++ b/tool/Compiler.hs | |||
@@ -15,6 +15,7 @@ data Config | |||
15 | , backend :: Backend | 15 | , backend :: Backend |
16 | , includePaths :: [FilePath] | 16 | , includePaths :: [FilePath] |
17 | , pretty :: Bool | 17 | , pretty :: Bool |
18 | , output :: Maybe String | ||
18 | } | 19 | } |
19 | 20 | ||
20 | sample :: Parser Config | 21 | sample :: Parser Config |
@@ -23,6 +24,7 @@ sample = Config | |||
23 | <*> flag OpenGL33 WebGL1 (long "webgl" <> help "generate WebGL 1.0 pipeline" ) | 24 | <*> flag OpenGL33 WebGL1 (long "webgl" <> help "generate WebGL 1.0 pipeline" ) |
24 | <*> pure ["."] | 25 | <*> pure ["."] |
25 | <*> switch (long "pretty" <> help "pretty prints pipeline") | 26 | <*> switch (long "pretty" <> help "pretty prints pipeline") |
27 | <*> optional (strOption (long "output" <> short 'o' <> metavar "FILENAME" <> help "output file name")) | ||
26 | 28 | ||
27 | main :: IO () | 29 | main :: IO () |
28 | main = compile =<< execParser opts | 30 | main = compile =<< execParser opts |
@@ -33,27 +35,29 @@ main = compile =<< execParser opts | |||
33 | <> header ("LambdaCube 3D compiler " ++ showVersion version)) | 35 | <> header ("LambdaCube 3D compiler " ++ showVersion version)) |
34 | 36 | ||
35 | compile :: Config -> IO () | 37 | compile :: Config -> IO () |
36 | compile Config{..} = do | 38 | compile cfg@Config{..} = do |
37 | let ext = takeExtension srcName | 39 | let ext = takeExtension srcName |
38 | baseName | ext == ".lc" = dropExtension srcName | 40 | baseName | ext == ".lc" = dropExtension srcName |
39 | | otherwise = srcName | 41 | | otherwise = srcName |
42 | withOutName n = maybe n id output | ||
40 | case ext of | 43 | case ext of |
41 | ".json" | pretty -> prettyPrint srcName | 44 | ".json" | pretty -> prettyPrint cfg |
42 | _ -> do | 45 | _ -> do |
43 | pplRes <- compileMain includePaths backend baseName | 46 | pplRes <- compileMain includePaths backend baseName |
44 | case pplRes of | 47 | case pplRes of |
45 | Left err -> putStrLn err | 48 | Left err -> fail err |
46 | Right ppl -> case pretty of | 49 | Right ppl -> case pretty of |
47 | False -> B.writeFile (baseName <> ".json") $ encode ppl | 50 | False -> B.writeFile (withOutName $ baseName <> ".json") $ encode ppl |
48 | True -> writeFile (baseName <> ".ppl") $ ppUnlines $ PP.ppShow ppl | 51 | True -> writeFile (withOutName $ baseName <> ".ppl") $ ppUnlines $ PP.ppShow ppl |
49 | 52 | ||
50 | prettyPrint :: String -> IO () | 53 | prettyPrint :: Config -> IO () |
51 | prettyPrint srcName = do | 54 | prettyPrint Config{..} = do |
52 | let baseName = dropExtension srcName | 55 | let baseName = dropExtension srcName |
56 | withOutName n = maybe n id output | ||
53 | json <- B.readFile srcName | 57 | json <- B.readFile srcName |
54 | case eitherDecode json :: Either String Pipeline of | 58 | case eitherDecode json :: Either String Pipeline of |
55 | Left err -> putStrLn err | 59 | Left err -> putStrLn err |
56 | Right ppl -> writeFile (baseName <> ".ppl") $ ppUnlines $ PP.ppShow ppl | 60 | Right ppl -> writeFile (withOutName $ baseName <> ".ppl") $ ppUnlines $ PP.ppShow ppl |
57 | 61 | ||
58 | ppUnlines :: String -> String | 62 | ppUnlines :: String -> String |
59 | ppUnlines = goPP 0 | 63 | ppUnlines = goPP 0 |