{-|
Module      : IRTS.CodegenJavaScript
Description : The JavaScript code generator.

License     : BSD3
Maintainer  : The Idris Community.
-}
{-# LANGUAGE OverloadedStrings, PatternGuards #-}
module IRTS.CodegenJavaScript (codegenJavaScript
                             , codegenNode
                             , JSTarget(..)
                             ) where

import Data.Char
import Data.Text (Text)
import qualified Data.Text as T
import IRTS.CodegenCommon
import IRTS.JavaScript.Codegen
import System.Directory
import System.FilePath


data JSTarget = Node | JavaScript deriving JSTarget -> JSTarget -> Bool
(JSTarget -> JSTarget -> Bool)
-> (JSTarget -> JSTarget -> Bool) -> Eq JSTarget
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: JSTarget -> JSTarget -> Bool
$c/= :: JSTarget -> JSTarget -> Bool
== :: JSTarget -> JSTarget -> Bool
$c== :: JSTarget -> JSTarget -> Bool
Eq


htmlHeader :: Text
htmlHeader :: Text
htmlHeader =
  [Text] -> Text
T.concat [ "<html>\n"
           , " <head>\n"
           , "  <meta charset='utf-8'>\n"
           , " </head>\n"
           , " <body>\n"
           , "  <script type='text/javascript'>\n"
           ]

htmlFooter :: Text
htmlFooter :: Text
htmlFooter =
  [Text] -> Text
T.concat [ "\n  </script>\n"
           , " </body>\n"
           , "</html>"
           ]

codegenJavaScript :: CodeGenerator
codegenJavaScript :: CodeGenerator
codegenJavaScript ci :: CodegenInfo
ci =
  let (h :: Text
h, f :: Text
f) = if ((Char -> Char) -> [Char] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char]
takeExtension ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ CodegenInfo -> [Char]
outputFile CodegenInfo
ci) [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== ".html" then
                  (Text
htmlHeader, Text
htmlFooter)
                  else ("","")
  in CGConf -> CodeGenerator
codegenJs (CGConf :: Text -> Text -> [Char] -> [Char] -> CGConf
CGConf { header :: Text
header = Text
h
                       , footer :: Text
footer = Text
f
                       , jsbnPath :: [Char]
jsbnPath = "jsbn/jsbn-browser.js"
                       , extraRunTime :: [Char]
extraRunTime = "Runtime-javascript.js"
                       }
               )
               CodegenInfo
ci

codegenNode :: CodeGenerator
codegenNode :: CodeGenerator
codegenNode ci :: CodegenInfo
ci =
  if CodegenInfo -> Bool
interfaces CodegenInfo
ci then
    CGConf -> CodeGenerator
codegenJs (CGConf :: Text -> Text -> [Char] -> [Char] -> CGConf
CGConf { header :: Text
header = ""
                      , footer :: Text
footer = ""
                      , jsbnPath :: [Char]
jsbnPath = "jsbn/jsbn-browser.js"
                      , extraRunTime :: [Char]
extraRunTime = "Runtime-node.js"
                      }
              )
              CodegenInfo
ci
    else
      do
        CGConf -> CodeGenerator
codegenJs (CGConf :: Text -> Text -> [Char] -> [Char] -> CGConf
CGConf { header :: Text
header = "#!/usr/bin/env node\n"
                          , footer :: Text
footer = ""
                          , jsbnPath :: [Char]
jsbnPath = "jsbn/jsbn-browser.js"
                          , extraRunTime :: [Char]
extraRunTime = "Runtime-node.js"
                          }
                  )
                  CodegenInfo
ci
        [Char] -> Permissions -> IO ()
setPermissions (CodegenInfo -> [Char]
outputFile CodegenInfo
ci) (Permissions
emptyPermissions { readable :: Bool
readable   = Bool
True
                                                         , executable :: Bool
executable = Bool
True
                                                         , writable :: Bool
writable   = Bool
True
                                                         })