{-# LANGUAGE ScopedTypeVariables #-}
import           System.Environment
import           Control.Applicative ((<$>))
import           Control.Monad (void)
import qualified Kwil.Parser as KwilP
import qualified Kwil.Compiler as KwilC
import qualified Text.Parsec as P
import           Text.Parsec.Error (ParseError)
import qualified Text.Parsec.Prim as Pr
import           Text.ParserCombinators.Parsec
import           Kwil.Lexer

main = do
     args <- getArgs
     programInput <- readFile (if not (null args)
                                  then head args
                                  else error "Missing file argument")
     let tokens = parseFile programInput (head args) in
      case P.runParser KwilP.program () "example.kwil" tokens of
        (Left pe) -> do
          putStrLn "Programming Parsing Failed"
          print pe
        (Right p) ->
          if length args >= 2 && (args !! 1 == "compile") then do
            preludeInput <- readFile "Kwil/Kwil.js"
            putStrLn preludeInput
            putStrLn "\n"
            putStrLn $ KwilC.compileProgram p
          else
            putStrLn "No More Evaluator"
filenameToJS filename = do
  programInput <- readFile filename
  let tokens = parseFile programInput filename in
    case P.runParser KwilP.program () filename tokens of
      (Right p) -> print $ KwilC.compileProgram p
