https://github.com/seliopou/typo
Raw File
Tip revision: cc64ec38603a50c6543b3834d47b4ff0431c2b3e authored by Spiros Eliopoulos on 13 July 2014, 15:16:52 UTC
compiler: port to template haskell
Tip revision: cc64ec3
Main.hs
module Main ( main ) where

import Control.Monad ( when )

import System.Console.GetOpt
import System.Environment
import System.Exit
import System.IO

import Language.Typo.Config
import Language.Typo.Compiler ( compile )
import Language.Typo.Parser ( parse )


main :: IO ()
main = do
    (fs, _) <- arguments

    when (Help `elem` fs) $ do
      putStrLn help
      exitSuccess

    let config = fromFlags [f | TCFlag f <- fs]
    text <- hGetContents stdin
    case parse "<stdin>" text of
      Left  error   -> print error
      Right program -> do
        putStr (compile config program)

data Flag = TCFlag TypoFlag | Help
  deriving ( Eq, Ord )

arguments :: IO ([Flag], [String])
arguments = do
  (fs, as, errs) <- (getOpt RequireOrder options) `fmap` getArgs 
  case errs of
    []-> return (fs, as)
    _ -> do
      putStr (concat errs ++ help)
      exitFailure

options :: [OptDescr Flag]
options =
  [ Option "h" ["help"]       (NoArg Help)                "print this message"
  , Option []  ["anf"]        (NoArg (TCFlag ANormalize)) "print A-normalized program"
  , Option []  ["no-prelude"] (NoArg (TCFlag NoPrelude))  "do not add prelude to compiled code"
  ]

help :: String
help = usageInfo header options
  where header = "USAGE: typoc OPTS"
back to top