{-|
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
$c== :: JSTarget -> JSTarget -> Bool
== :: JSTarget -> JSTarget -> Bool
$c/= :: JSTarget -> JSTarget -> Bool
/= :: JSTarget -> JSTarget -> Bool
Eq


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

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

codegenJavaScript :: CodeGenerator
codegenJavaScript :: CodeGenerator
codegenJavaScript CodegenInfo
ci =
  let (Text
h, 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
== [Char]
".html" then
                  (Text
htmlHeader, Text
htmlFooter)
                  else (Text
"",Text
"")
  in CGConf -> CodeGenerator
codegenJs (CGConf { header :: Text
header = Text
h
                       , footer :: Text
footer = Text
f
                       , jsbnPath :: [Char]
jsbnPath = [Char]
"jsbn/jsbn-browser.js"
                       , extraRunTime :: [Char]
extraRunTime = [Char]
"Runtime-javascript.js"
                       }
               )
               CodegenInfo
ci

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