module Main where
import Parsing.Lexer (lexer)
import Parsing.Spec (parser)
import Simplify.ConcreteToAbstract (cs2as)
import Simplify.Simplifier (simplifier)
import Simplify.CoreToTarget (core2target)
import Simplify.LiftStrictness (lift_strictness)
import Simplify.TargetToIML (target2iml, stepR, mkValOpRules)
import Simplify.TargetToFunconModules (target2fmodule)
import Print.HaskellModule (cbs2module)
--import Print.JavaClasses (cbs2classes)
--import Print.Ascii (cbs2ascii)
import Types.FunconModule (FunconModule)
import IML.CodeGen.LaTeX
import IML.Printer
import IML.Trans.ProMan
import IML.Grammar.Specs (Spec(Spec),AnyDecls(ARuleDecl))
import CCO.Component (Component, ioRun)
import Control.Monad (when)
import Data.List (isPrefixOf)
import Control.Arrow ((>>>))
import System.Environment (getArgs)
main = do all_args <- getArgs
let (options,args) = (filter pred all_args, filter (not . pred) all_args)
where pred = isPrefixOf "--"
case args of
[] | any (== ("--iml-value-operations")) options ->
run_iml (Spec (map ARuleDecl mkValOpRules)) options
[cbsf] -> run cbsf Nothing Nothing options
[cbsf,srcdir] -> run cbsf (Just srcdir) Nothing options
[cbsf,srcdir,lang] -> run cbsf (Just srcdir) (Just lang) options
_ -> putStrLn $
"version CBS-beta\n\
\usage: cbsc <CBS-PATH> <SRC-DIR> <LANG>\n\
\CBS-PATH: path to the .cbs file\n\
\ for which code is to be generated.\n\
\ The file should be within a directory named \"Funcons\".\n\
\SRC-DIR: the source-directory in which the code is to be generated.\n\
\LANG: the language for which the .cbs file contains a specification.\n"
run cbsfile srcdir lang options = do
when (not toIML) (putStrLn ("Generating " ++ cbsfile))
cbs_contents <- readFile cbsfile
let core2target'
| "--generate-congruences" `elem` options = core2target >>> lift_strictness
| otherwise = core2target
target <- ioRun (lexer >>> parser >>> cs2as pholder >>> simplifier
>>> core2target') cbs_contents
if toIML
then do let ran_options = IML.Trans.ProMan.runOptions options
iml <- runComponentIO ran_options target2iml target
run_iml iml options
else do fmodule <- ioRun (target2fmodule pholder) target
m_contents <- ioRun ((cbs2 options) cbsfile srcdir lang) fmodule
case m_contents of
Nothing -> putStrLn "No funcons, types or entities to generate"
Just make_contents -> make_contents
where pholder = any (== "--generate-unspecified-funcons") options
toIML = any (== "--IML") options
cbs2 :: [String] -> FilePath -> Maybe FilePath -> Maybe String ->
CCO.Component.Component FunconModule (Maybe (IO ()))
cbs2 options {-| "--java" `elem` options = cbs2classes
| "--ascii" `elem` options = cbs2ascii
| otherwise -} = cbs2module
run_iml iml options = do
let chain | "--LaTeX" `elem` options = spec2latex_module >>> component_ show
| otherwise = spec2highstring
printable <- runComponentIO (IML.Trans.ProMan.runOptions options) chain iml
putStrLn printable