summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--src/LambdaCube/Compiler.hs13
-rw-r--r--tool/Compiler.hs81
3 files changed, 56 insertions, 41 deletions
diff --git a/Makefile b/Makefile
index 44cd956a..e23e7326 100644
--- a/Makefile
+++ b/Makefile
@@ -7,6 +7,9 @@ LCDIR=~/.cabal/share/x86_64-linux-ghc-7.10.3/lambdacube-compiler-0.5.0.0/lc
7repl: 7repl:
8 cd test && ghci -Wall -fno-warn-name-shadowing -fno-warn-unused-matches -fno-warn-missing-signatures -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns -fno-warn-type-defaults -i../src -i../dist/build/autogen runTests.hs 8 cd test && ghci -Wall -fno-warn-name-shadowing -fno-warn-unused-matches -fno-warn-missing-signatures -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns -fno-warn-type-defaults -i../src -i../dist/build/autogen runTests.hs
9 9
10repllc:
11 cd tool && ghci -Wall -fno-warn-name-shadowing -fno-warn-unused-matches -fno-warn-missing-signatures -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns -fno-warn-type-defaults -i../src -i../dist/build/autogen Compiler.hs
12
10copylc: 13copylc:
11 cp lc/*.lc $(LCDIR) 14 cp lc/*.lc $(LCDIR)
12 15
diff --git a/src/LambdaCube/Compiler.hs b/src/LambdaCube/Compiler.hs
index 4f0aba11..56d3db8a 100644
--- a/src/LambdaCube/Compiler.hs
+++ b/src/LambdaCube/Compiler.hs
@@ -16,7 +16,7 @@ module LambdaCube.Compiler
16 , MM, runMM 16 , MM, runMM
17 , catchErr 17 , catchErr
18 , ioFetch, decideFilePath 18 , ioFetch, decideFilePath
19 , getDef, compileMain, preCompile 19 , getDef, compileMain, parseModule, preCompile
20 , removeFromCache 20 , removeFromCache
21 21
22 , compilePipeline 22 , compilePipeline
@@ -239,6 +239,12 @@ compileMain :: [FilePath] -> IR.Backend -> MName -> IO (Either String IR.Pipelin
239compileMain path backend fname 239compileMain path backend fname
240 = fmap snd $ runMM (ioFetch path) $ compilePipeline' (const ()) backend fname 240 = fmap snd $ runMM (ioFetch path) $ compilePipeline' (const ()) backend fname
241 241
242parseModule :: [FilePath] -> MName -> IO (Either String String)
243parseModule path fname = runMM (ioFetch path) $ loadModule snd Nothing (Left fname) <&> \case
244 Left err -> Left err
245 Right (fname, (src, Left err)) -> Left err
246 Right (fname, (src, Right (pm, infos, _))) -> Right $ pPrintStmts infos
247
242-- used by the compiler-service of the online editor 248-- used by the compiler-service of the online editor
243preCompile :: (MonadMask m, MonadIO m) => [FilePath] -> [FilePath] -> Backend -> FilePath -> IO (String -> m (Either String IR.Pipeline, (Infos, String))) 249preCompile :: (MonadMask m, MonadIO m) => [FilePath] -> [FilePath] -> Backend -> FilePath -> IO (String -> m (Either String IR.Pipeline, (Infos, String)))
244preCompile paths paths' backend mod = do 250preCompile paths paths' backend mod = do
@@ -256,4 +262,7 @@ preCompile paths paths' backend mod = do
256 Left "Main" -> return $ Right ("./Main.lc", "Main", return src) 262 Left "Main" -> return $ Right ("./Main.lc", "Main", return src)
257 n -> ioFetch paths' imp n 263 n -> ioFetch paths' imp n
258 where 264 where
259 ex = second (unlines . map ((++"\n") . removeEscs . ppShow)) 265 ex = second pPrintStmts
266
267pPrintStmts = unlines . map ((++"\n") . removeEscs . ppShow)
268
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