module DDC.Driver.Command.ToPHP
( cmdToPHPFromFile
, cmdToPHPSourceTetraFromFile
, cmdToPHPSourceTetraFromString
, cmdToPHPCoreFromFile
, cmdToPHPCoreFromString)
where
import DDC.Driver.Stage
import DDC.Driver.Command.Compile
import DDC.Driver.Interface.Source
import DDC.Build.Pipeline
import DDC.Build.Language
import DDC.Core.Fragment
import DDC.Base.Pretty
import System.FilePath
import System.Directory
import Control.Monad.Trans.Except
import Control.Monad.IO.Class
import Control.Monad
import DDC.Build.Interface.Store (Store)
import DDC.Core.Exp.Annot.AnTEC
cmdToPHPFromFile
:: Config
-> Store
-> FilePath
-> ExceptT String IO ()
cmdToPHPFromFile config store filePath
| ".ds" <- takeExtension filePath
= cmdToPHPSourceTetraFromFile config store filePath
| Just language <- languageOfExtension (takeExtension filePath)
= cmdToPHPCoreFromFile config language filePath
| otherwise
= let ext = takeExtension filePath
in throwE $ "Cannot convert '" ++ ext ++ "' files to PHP."
cmdToPHPSourceTetraFromFile
:: Config
-> Store
-> FilePath
-> ExceptT String IO ()
cmdToPHPSourceTetraFromFile config store filePath
= do
exists <- liftIO $ doesFileExist filePath
when (not exists)
$ throwE $ "No such file " ++ show filePath
cmdCompileRecursive config False store filePath
src <- liftIO $ readFile filePath
cmdToPHPSourceTetraFromString config store (SourceFile filePath) src
cmdToPHPSourceTetraFromString
:: Config
-> Store
-> Source
-> String
-> ExceptT String IO ()
cmdToPHPSourceTetraFromString config store source str
= let
pipeLoad
= pipeText (nameOfSource source) (lineStartOfSource source) str
$ stageSourceTetraLoad config source store
[ PipeCoreReannotate annotTail
[ PipeCoreAsTetra
[ PipeTetraToPHP SinkStdout ]]]
in do
errs <- liftIO pipeLoad
case errs of
[] -> return ()
es -> throwE $ renderIndent $ vcat $ map ppr es
cmdToPHPCoreFromFile
:: Config
-> Language
-> FilePath
-> ExceptT String IO ()
cmdToPHPCoreFromFile config language filePath
= do
exists <- liftIO $ doesFileExist filePath
when (not exists)
$ throwE $ "No such file " ++ show filePath
src <- liftIO $ readFile filePath
cmdToPHPCoreFromString config language (SourceFile filePath) src
cmdToPHPCoreFromString
:: Config
-> Language
-> Source
-> String
-> ExceptT String IO ()
cmdToPHPCoreFromString config language source str
| Language bundle <- language
, fragment <- bundleFragment bundle
, profile <- fragmentProfile fragment
= do
let fragName = profileName profile
let compile
| fragName == "Tetra"
= liftIO
$ pipeText (nameOfSource source) (lineStartOfSource source) str
$ stageTetraLoad config source
[ PipeCoreReannotate (const ())
[ PipeCoreAsTetra
[ PipeTetraToPHP SinkStdout ]]]
| otherwise
= throwE $ "Cannot convert '" ++ fragName ++ "'modules to PHP."
errs <- compile
case errs of
[] -> return ()
es -> throwE $ renderIndent $ vcat $ map ppr es