summaryrefslogtreecommitdiff
path: root/tool/Compiler.hs
blob: 1cdc9fd946670b3a40e4b9b6e521a135e80a06e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE RecordWildCards #-}
import Options.Applicative
import Data.Aeson
import qualified Data.ByteString.Lazy as B
import LambdaCube.Compiler.Driver
import System.FilePath
import Paths_lambdacube_compiler (getDataDir)

data Config
  = Config
  { srcName :: String
  , backend :: Backend
  , sourceDir :: FilePath
  }

sample :: Parser Config
sample = Config
  <$> argument str (metavar "SOURCE_FILE")
  <*> flag OpenGL33 WebGL1 (long "webgl" <> help "generate WebGL 1.0 pipeline" )
  <*> pure "/lc"

main :: IO ()
main = do
  cabalDataDir <- getDataDir
  cfg <- execParser opts
  compile (cfg {sourceDir = cabalDataDir </> "lc"})
  where
    opts = info (helper <*> sample)
      ( fullDesc
     <> progDesc "compiles LambdaCube graphics pipeline source to JSON IR"
     <> header "LambdaCube 3D compiler" )

compile :: Config -> IO ()
compile Config{..} = do
  let dropExt n | takeExtension n == ".lc"  = dropExtension n
      dropExt n = n
      baseName = dropExt srcName
  pplRes <- compileMain [".", sourceDir] backend baseName
  case pplRes of
    Left err -> putStrLn err
    Right ppl -> do
      B.writeFile (baseName <> ".json") $ encode ppl