{-|
Module      : IRTS.JavaScript.AST
Description : The JavaScript AST.

License     : BSD3
Maintainer  : The Idris Community.
-}

{-# LANGUAGE DeriveDataTypeable, OverloadedStrings, PatternGuards #-}

module IRTS.JavaScript.AST
  ( JsExpr(..)
  , JsStmt(..)
  , jsAst2Text
  , jsStmt2Text
  , jsLazy
  , jsCurryLam
  , jsCurryApp
  , jsAppN
  , jsExpr2Stmt
  , jsStmt2Expr
  , jsSetVar
  ) where

import Data.Char
import Data.Data
import Data.Text (Text)
import qualified Data.Text as T
import Numeric

data JsStmt
  = JsEmpty
  | JsComment Text
  | JsExprStmt JsExpr
  | JsFun Text
          [Text]
          JsStmt
  | JsSeq JsStmt
          JsStmt
  | JsReturn JsExpr
  | JsDecVar Text
             JsExpr
  | JsDecConst Text
               JsExpr
  | JsDecLet Text
             JsExpr
  | JsSet JsExpr
          JsExpr
  | JsIf JsExpr
         JsStmt
         (Maybe JsStmt)
  | JsSwitchCase JsExpr
                 [(JsExpr, JsStmt)]
                 (Maybe JsStmt)
  | JsError JsExpr
  | JsForever JsStmt
  | JsContinue
  | JsBreak
  deriving (Int -> JsStmt -> ShowS
[JsStmt] -> ShowS
JsStmt -> String
(Int -> JsStmt -> ShowS)
-> (JsStmt -> String) -> ([JsStmt] -> ShowS) -> Show JsStmt
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [JsStmt] -> ShowS
$cshowList :: [JsStmt] -> ShowS
show :: JsStmt -> String
$cshow :: JsStmt -> String
showsPrec :: Int -> JsStmt -> ShowS
$cshowsPrec :: Int -> JsStmt -> ShowS
Show, JsStmt -> JsStmt -> Bool
(JsStmt -> JsStmt -> Bool)
-> (JsStmt -> JsStmt -> Bool) -> Eq JsStmt
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: JsStmt -> JsStmt -> Bool
$c/= :: JsStmt -> JsStmt -> Bool
== :: JsStmt -> JsStmt -> Bool
$c== :: JsStmt -> JsStmt -> Bool
Eq, Typeable JsStmt
Constr
DataType
Typeable JsStmt
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> JsStmt -> c JsStmt)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c JsStmt)
-> (JsStmt -> Constr)
-> (JsStmt -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c JsStmt))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c JsStmt))
-> ((forall b. Data b => b -> b) -> JsStmt -> JsStmt)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> JsStmt -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> JsStmt -> r)
-> (forall u. (forall d. Data d => d -> u) -> JsStmt -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> JsStmt -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> JsStmt -> m JsStmt)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> JsStmt -> m JsStmt)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> JsStmt -> m JsStmt)
-> Data JsStmt
JsStmt -> Constr
JsStmt -> DataType
(forall b. Data b => b -> b) -> JsStmt -> JsStmt
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> JsStmt -> c JsStmt
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c JsStmt
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> JsStmt -> u
forall u. (forall d. Data d => d -> u) -> JsStmt -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> JsStmt -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> JsStmt -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> JsStmt -> m JsStmt
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> JsStmt -> m JsStmt
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c JsStmt
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> JsStmt -> c JsStmt
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c JsStmt)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c JsStmt)
$cJsBreak :: Constr
$cJsContinue :: Constr
$cJsForever :: Constr
$cJsError :: Constr
$cJsSwitchCase :: Constr
$cJsIf :: Constr
$cJsSet :: Constr
$cJsDecLet :: Constr
$cJsDecConst :: Constr
$cJsDecVar :: Constr
$cJsReturn :: Constr
$cJsSeq :: Constr
$cJsFun :: Constr
$cJsExprStmt :: Constr
$cJsComment :: Constr
$cJsEmpty :: Constr
$tJsStmt :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> JsStmt -> m JsStmt
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> JsStmt -> m JsStmt
gmapMp :: (forall d. Data d => d -> m d) -> JsStmt -> m JsStmt
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> JsStmt -> m JsStmt
gmapM :: (forall d. Data d => d -> m d) -> JsStmt -> m JsStmt
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> JsStmt -> m JsStmt
gmapQi :: Int -> (forall d. Data d => d -> u) -> JsStmt -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> JsStmt -> u
gmapQ :: (forall d. Data d => d -> u) -> JsStmt -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> JsStmt -> [u]
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> JsStmt -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> JsStmt -> r
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> JsStmt -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> JsStmt -> r
gmapT :: (forall b. Data b => b -> b) -> JsStmt -> JsStmt
$cgmapT :: (forall b. Data b => b -> b) -> JsStmt -> JsStmt
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c JsStmt)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c JsStmt)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c JsStmt)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c JsStmt)
dataTypeOf :: JsStmt -> DataType
$cdataTypeOf :: JsStmt -> DataType
toConstr :: JsStmt -> Constr
$ctoConstr :: JsStmt -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c JsStmt
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c JsStmt
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> JsStmt -> c JsStmt
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> JsStmt -> c JsStmt
$cp1Data :: Typeable JsStmt
Data, Typeable)

data JsExpr
  = JsNull
  | JsUndefined
  | JsThis
  | JsLambda [Text]
             JsStmt
  | JsApp JsExpr
          [JsExpr]
  | JsNew JsExpr
          [JsExpr]
  | JsPart JsExpr
           Text
  | JsMethod JsExpr
             Text
             [JsExpr]
  | JsVar Text
  | JsArrayProj JsExpr
                JsExpr
  | JsObj [(Text, JsExpr)]
  | JsProp JsExpr
           Text
  | JsInt Int
  | JsBool Bool
  | JsInteger Integer
  | JsDouble Double
  | JsStr String
  | JsArray [JsExpr]
  | JsErrorExp JsExpr
  | JsUniOp Text
            JsExpr
  | JsBinOp Text
            JsExpr
            JsExpr
  | JsForeign Text
              [JsExpr]
  | JsB2I JsExpr
  | JsForce JsExpr
  deriving (Int -> JsExpr -> ShowS
[JsExpr] -> ShowS
JsExpr -> String
(Int -> JsExpr -> ShowS)
-> (JsExpr -> String) -> ([JsExpr] -> ShowS) -> Show JsExpr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [JsExpr] -> ShowS
$cshowList :: [JsExpr] -> ShowS
show :: JsExpr -> String
$cshow :: JsExpr -> String
showsPrec :: Int -> JsExpr -> ShowS
$cshowsPrec :: Int -> JsExpr -> ShowS
Show, JsExpr -> JsExpr -> Bool
(JsExpr -> JsExpr -> Bool)
-> (JsExpr -> JsExpr -> Bool) -> Eq JsExpr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: JsExpr -> JsExpr -> Bool
$c/= :: JsExpr -> JsExpr -> Bool
== :: JsExpr -> JsExpr -> Bool
$c== :: JsExpr -> JsExpr -> Bool
Eq, Typeable JsExpr
Constr
DataType
Typeable JsExpr
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> JsExpr -> c JsExpr)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c JsExpr)
-> (JsExpr -> Constr)
-> (JsExpr -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c JsExpr))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c JsExpr))
-> ((forall b. Data b => b -> b) -> JsExpr -> JsExpr)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> JsExpr -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> JsExpr -> r)
-> (forall u. (forall d. Data d => d -> u) -> JsExpr -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> JsExpr -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> JsExpr -> m JsExpr)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> JsExpr -> m JsExpr)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> JsExpr -> m JsExpr)
-> Data JsExpr
JsExpr -> Constr
JsExpr -> DataType
(forall b. Data b => b -> b) -> JsExpr -> JsExpr
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> JsExpr -> c JsExpr
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c JsExpr
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> JsExpr -> u
forall u. (forall d. Data d => d -> u) -> JsExpr -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> JsExpr -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> JsExpr -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> JsExpr -> m JsExpr
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> JsExpr -> m JsExpr
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c JsExpr
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> JsExpr -> c JsExpr
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c JsExpr)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c JsExpr)
$cJsForce :: Constr
$cJsB2I :: Constr
$cJsForeign :: Constr
$cJsBinOp :: Constr
$cJsUniOp :: Constr
$cJsErrorExp :: Constr
$cJsArray :: Constr
$cJsStr :: Constr
$cJsDouble :: Constr
$cJsInteger :: Constr
$cJsBool :: Constr
$cJsInt :: Constr
$cJsProp :: Constr
$cJsObj :: Constr
$cJsArrayProj :: Constr
$cJsVar :: Constr
$cJsMethod :: Constr
$cJsPart :: Constr
$cJsNew :: Constr
$cJsApp :: Constr
$cJsLambda :: Constr
$cJsThis :: Constr
$cJsUndefined :: Constr
$cJsNull :: Constr
$tJsExpr :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> JsExpr -> m JsExpr
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> JsExpr -> m JsExpr
gmapMp :: (forall d. Data d => d -> m d) -> JsExpr -> m JsExpr
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> JsExpr -> m JsExpr
gmapM :: (forall d. Data d => d -> m d) -> JsExpr -> m JsExpr
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> JsExpr -> m JsExpr
gmapQi :: Int -> (forall d. Data d => d -> u) -> JsExpr -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> JsExpr -> u
gmapQ :: (forall d. Data d => d -> u) -> JsExpr -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> JsExpr -> [u]
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> JsExpr -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> JsExpr -> r
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> JsExpr -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> JsExpr -> r
gmapT :: (forall b. Data b => b -> b) -> JsExpr -> JsExpr
$cgmapT :: (forall b. Data b => b -> b) -> JsExpr -> JsExpr
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c JsExpr)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c JsExpr)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c JsExpr)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c JsExpr)
dataTypeOf :: JsExpr -> DataType
$cdataTypeOf :: JsExpr -> DataType
toConstr :: JsExpr -> Constr
$ctoConstr :: JsExpr -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c JsExpr
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c JsExpr
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> JsExpr -> c JsExpr
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> JsExpr -> c JsExpr
$cp1Data :: Typeable JsExpr
Data, Typeable)

translateChar :: Char -> String
translateChar :: Char -> String
translateChar Char
ch
  | Char
'\b'   <- Char
ch       = String
"\\b"
  | Char
'\f'   <- Char
ch       = String
"\\f"
  | Char
'\n'   <- Char
ch       = String
"\\n"
  | Char
'\r'   <- Char
ch       = String
"\\r"
  | Char
'\t'   <- Char
ch       = String
"\\t"
  | Char
'\v'   <- Char
ch       = String
"\\v"
  | Char
'\\'   <- Char
ch       = String
"\\\\"
  | Char
'\"'   <- Char
ch       = String
"\\\""
  | Char
'\''   <- Char
ch       = String
"\\\'"
  | Char -> Int
ord Char
ch Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0x20      = String
"\\x" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> ShowS
pad Int
2 (Int -> ShowS
forall a. (Integral a, Show a) => a -> ShowS
showHex (Char -> Int
ord Char
ch) String
"")
  | Char -> Int
ord Char
ch Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0x7f      = [Char
ch]  -- 0x7f '\DEL'
  | Char -> Int
ord Char
ch Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0xff     = String
"\\x" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> ShowS
pad Int
2 (Int -> ShowS
forall a. (Integral a, Show a) => a -> ShowS
showHex (Char -> Int
ord Char
ch) String
"")
  | Char -> Int
ord Char
ch Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0xffff   = String
"\\u" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> ShowS
pad Int
4 (Int -> ShowS
forall a. (Integral a, Show a) => a -> ShowS
showHex (Char -> Int
ord Char
ch) String
"")
  | Char -> Int
ord Char
ch Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x10ffff = String
"\\u{" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> ShowS
forall a. (Integral a, Show a) => a -> ShowS
showHex (Char -> Int
ord Char
ch) String
"}"
  | Bool
otherwise          = ShowS
forall a. HasCallStack => String -> a
error ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ String
"Invalid Unicode code point U+" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> ShowS
forall a. (Integral a, Show a) => a -> ShowS
showHex (Char -> Int
ord Char
ch) String
""
  where
    pad :: Int -> String -> String
    pad :: Int -> ShowS
pad Int
n String
s = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s) Char
'0' String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s

indent :: Text -> Text
indent :: Text -> Text
indent Text
x =
  let l :: [Text]
l = Text -> [Text]
T.lines Text
x
      il :: [Text]
il = (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (\Text
y -> Int -> Text -> Text
T.replicate Int
4 Text
" " Text -> Text -> Text
`T.append` Text
y) [Text]
l
  in [Text] -> Text
T.unlines [Text]
il

jsCurryLam :: [Text] -> JsExpr -> JsExpr
jsCurryLam :: [Text] -> JsExpr -> JsExpr
jsCurryLam [] JsExpr
body = JsExpr
body
jsCurryLam (Text
x:[Text]
xs) JsExpr
body = [Text] -> JsStmt -> JsExpr
JsLambda [Text
x] (JsStmt -> JsExpr) -> JsStmt -> JsExpr
forall a b. (a -> b) -> a -> b
$ JsExpr -> JsStmt
JsReturn (JsExpr -> JsStmt) -> JsExpr -> JsStmt
forall a b. (a -> b) -> a -> b
$ [Text] -> JsExpr -> JsExpr
jsCurryLam [Text]
xs JsExpr
body

jsCurryApp :: JsExpr -> [JsExpr] -> JsExpr
jsCurryApp :: JsExpr -> [JsExpr] -> JsExpr
jsCurryApp JsExpr
fn [] = JsExpr
fn
jsCurryApp JsExpr
fn [JsExpr]
args = (JsExpr -> JsExpr -> JsExpr) -> JsExpr -> [JsExpr] -> JsExpr
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\JsExpr
ff JsExpr
aa -> JsExpr -> [JsExpr] -> JsExpr
JsApp JsExpr
ff [JsExpr
aa]) JsExpr
fn [JsExpr]
args

jsAppN :: Text -> [JsExpr] -> JsExpr
jsAppN :: Text -> [JsExpr] -> JsExpr
jsAppN Text
fn [JsExpr]
args = JsExpr -> [JsExpr] -> JsExpr
JsApp (Text -> JsExpr
JsVar Text
fn) [JsExpr]
args

jsSetVar :: Text -> JsExpr -> JsStmt
jsSetVar :: Text -> JsExpr -> JsStmt
jsSetVar Text
n JsExpr
x = JsExpr -> JsExpr -> JsStmt
JsSet (Text -> JsExpr
JsVar Text
n) JsExpr
x

jsStmt2Text :: JsStmt -> Text
jsStmt2Text :: JsStmt -> Text
jsStmt2Text JsStmt
JsEmpty = Text
""
jsStmt2Text (JsComment Text
c) = [Text] -> Text
T.unlines ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text
"// " Text -> Text -> Text
`T.append`) ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines Text
c
jsStmt2Text (JsExprStmt JsExpr
e) = [Text] -> Text
T.concat [JsExpr -> Text
jsAst2Text JsExpr
e, Text
";"]
jsStmt2Text (JsReturn JsExpr
x) = [Text] -> Text
T.concat [Text
"return ", JsExpr -> Text
jsAst2Text JsExpr
x, Text
";"]
jsStmt2Text (JsDecVar Text
name JsExpr
exp) =
  [Text] -> Text
T.concat [Text
"var ", Text
name, Text
" = ", JsExpr -> Text
jsAst2Text JsExpr
exp, Text
";"]
jsStmt2Text (JsDecConst Text
name JsExpr
exp) =
  [Text] -> Text
T.concat [Text
"const ", Text
name, Text
" = ", JsExpr -> Text
jsAst2Text JsExpr
exp, Text
";"]
jsStmt2Text (JsDecLet Text
name JsExpr
exp) =
  [Text] -> Text
T.concat [Text
"let ", Text
name, Text
" = ", JsExpr -> Text
jsAst2Text JsExpr
exp, Text
";"]
jsStmt2Text (JsFun Text
name [Text]
args JsStmt
body) =
  [Text] -> Text
T.concat
    [ Text
"function "
    , Text
name
    , Text
"("
    , Text -> [Text] -> Text
T.intercalate Text
", " [Text]
args
    , Text
"){\n"
    , Text -> Text
indent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ JsStmt -> Text
jsStmt2Text JsStmt
body
    , Text
"}\n"
    ]
jsStmt2Text (JsIf JsExpr
cond JsStmt
conseq (Just next :: JsStmt
next@(JsIf JsExpr
_ JsStmt
_ Maybe JsStmt
_))) =
  [Text] -> Text
T.concat
    [ Text
"if("
    , JsExpr -> Text
jsAst2Text JsExpr
cond
    , Text
") {\n"
    , Text -> Text
indent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ JsStmt -> Text
jsStmt2Text JsStmt
conseq
    , Text
"} else "
    , JsStmt -> Text
jsStmt2Text JsStmt
next
    ]
jsStmt2Text (JsIf JsExpr
cond JsStmt
conseq (Just JsStmt
alt)) =
  [Text] -> Text
T.concat
    [ Text
"if("
    , JsExpr -> Text
jsAst2Text JsExpr
cond
    , Text
") {\n"
    , Text -> Text
indent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ JsStmt -> Text
jsStmt2Text JsStmt
conseq
    , Text
"} else {\n"
    , Text -> Text
indent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ JsStmt -> Text
jsStmt2Text JsStmt
alt
    , Text
"}\n"
    ]
jsStmt2Text (JsIf JsExpr
cond JsStmt
conseq Maybe JsStmt
Nothing) =
  [Text] -> Text
T.concat [Text
"if(", JsExpr -> Text
jsAst2Text JsExpr
cond, Text
") {\n", Text -> Text
indent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ JsStmt -> Text
jsStmt2Text JsStmt
conseq, Text
"}\n"]
jsStmt2Text (JsSwitchCase JsExpr
exp [(JsExpr, JsStmt)]
l Maybe JsStmt
d) =
  [Text] -> Text
T.concat
    [ Text
"switch("
    , JsExpr -> Text
jsAst2Text JsExpr
exp
    , Text
"){\n"
    , Text -> Text
indent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.concat ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ ((JsExpr, JsStmt) -> Text) -> [(JsExpr, JsStmt)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (JsExpr, JsStmt) -> Text
case2Text [(JsExpr, JsStmt)]
l
    , Text -> Text
indent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Maybe JsStmt -> Text
default2Text Maybe JsStmt
d
    , Text
"}\n"
    ]
  where
    case2Text :: (JsExpr, JsStmt) -> Text
    case2Text :: (JsExpr, JsStmt) -> Text
case2Text (JsExpr
x, JsStmt
y) =
      [Text] -> Text
T.concat
        [ Text
"case "
        , JsExpr -> Text
jsAst2Text JsExpr
x
        , Text
":\n"
        , Text -> Text
indent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.concat [JsStmt -> Text
jsStmt2Text JsStmt
y, Text
";\nbreak;\n"]
        ]
    default2Text :: Maybe JsStmt -> Text
    default2Text :: Maybe JsStmt -> Text
default2Text Maybe JsStmt
Nothing = Text
""
    default2Text (Just JsStmt
z) =
      [Text] -> Text
T.concat [Text
"default:\n", Text -> Text
indent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.concat [JsStmt -> Text
jsStmt2Text JsStmt
z, Text
";\nbreak;\n"]]
jsStmt2Text (JsError JsExpr
t) = [Text] -> Text
T.concat [Text
"$JSRTS.die(", JsExpr -> Text
jsAst2Text JsExpr
t, Text
");\n"]
jsStmt2Text (JsForever JsStmt
x) =
  [Text] -> Text
T.concat [Text
"for(;;) {\n", Text -> Text
indent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ JsStmt -> Text
jsStmt2Text JsStmt
x, Text
"}\n"]
jsStmt2Text JsStmt
JsContinue = Text
"continue;"
jsStmt2Text JsStmt
JsBreak = Text
"break;"
jsStmt2Text (JsSeq JsStmt
JsEmpty JsStmt
y) = JsStmt -> Text
jsStmt2Text JsStmt
y
jsStmt2Text (JsSeq JsStmt
x JsStmt
JsEmpty) = JsStmt -> Text
jsStmt2Text JsStmt
x
jsStmt2Text (JsSeq JsStmt
x JsStmt
y) = [Text] -> Text
T.concat [JsStmt -> Text
jsStmt2Text JsStmt
x, Text
"\n", JsStmt -> Text
jsStmt2Text JsStmt
y]
jsStmt2Text (JsSet JsExpr
term JsExpr
exp) =
  [Text] -> Text
T.concat [JsExpr -> Text
jsAst2Text JsExpr
term, Text
" = ", JsExpr -> Text
jsAst2Text JsExpr
exp, Text
";"]

jsAst2Text :: JsExpr -> Text
jsAst2Text :: JsExpr -> Text
jsAst2Text JsExpr
JsNull = Text
"null"
jsAst2Text JsExpr
JsUndefined = Text
"(void 0)"
jsAst2Text JsExpr
JsThis = Text
"this"
jsAst2Text (JsLambda [Text]
args JsStmt
body) =
  [Text] -> Text
T.concat
    [ Text
"(function"
    , Text
"("
    , Text -> [Text] -> Text
T.intercalate Text
", " [Text]
args
    , Text
"){\n"
    , Text -> Text
indent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ JsStmt -> Text
jsStmt2Text JsStmt
body
    , Text
"})"
    ]
jsAst2Text (JsApp JsExpr
fn [JsExpr]
args) =
  [Text] -> Text
T.concat [JsExpr -> Text
jsAst2Text JsExpr
fn, Text
"(", Text -> [Text] -> Text
T.intercalate Text
", " ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (JsExpr -> Text) -> [JsExpr] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map JsExpr -> Text
jsAst2Text [JsExpr]
args, Text
")"]
jsAst2Text (JsNew JsExpr
fn [JsExpr]
args) =
  [Text] -> Text
T.concat [Text
"new ", JsExpr -> Text
jsAst2Text JsExpr
fn, Text
"(", Text -> [Text] -> Text
T.intercalate Text
", " ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (JsExpr -> Text) -> [JsExpr] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map JsExpr -> Text
jsAst2Text [JsExpr]
args, Text
")"]
jsAst2Text (JsMethod JsExpr
obj Text
name [JsExpr]
args) =
  [Text] -> Text
T.concat
    [ JsExpr -> Text
jsAst2Text JsExpr
obj
    , Text
"."
    , Text
name
    , Text
"("
    , Text -> [Text] -> Text
T.intercalate Text
", " ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (JsExpr -> Text) -> [JsExpr] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map JsExpr -> Text
jsAst2Text [JsExpr]
args
    , Text
")"
    ]
jsAst2Text (JsPart JsExpr
obj Text
name) =
  [Text] -> Text
T.concat [JsExpr -> Text
jsAst2Text JsExpr
obj, Text
"[", String -> Text
T.pack (Text -> String
forall a. Show a => a -> String
show Text
name), Text
"]"]
jsAst2Text (JsVar Text
x) = Text
x
jsAst2Text (JsObj [(Text, JsExpr)]
props) =
  [Text] -> Text
T.concat
    [ Text
"({"
    , Text -> [Text] -> Text
T.intercalate
        Text
", "
        (((Text, JsExpr) -> Text) -> [(Text, JsExpr)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (\(Text
name, JsExpr
val) -> [Text] -> Text
T.concat [Text
name, Text
": ", JsExpr -> Text
jsAst2Text JsExpr
val]) [(Text, JsExpr)]
props)
    , Text
"})"
    ]
jsAst2Text (JsProp JsExpr
object Text
name) = [Text] -> Text
T.concat [JsExpr -> Text
jsAst2Text JsExpr
object, Text
".", Text
name]
jsAst2Text (JsArrayProj JsExpr
i JsExpr
exp) =
  [Text] -> Text
T.concat [JsExpr -> Text
jsAst2Text JsExpr
exp, Text
"[", JsExpr -> Text
jsAst2Text JsExpr
i, Text
"]"]
jsAst2Text (JsInt Int
i) = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
i
jsAst2Text (JsBool Bool
True) = String -> Text
T.pack String
"true"
jsAst2Text (JsBool Bool
False) = String -> Text
T.pack String
"false"
jsAst2Text (JsDouble Double
d) = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Double -> String
forall a. Show a => a -> String
show Double
d
jsAst2Text (JsInteger Integer
i) = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Integer -> String
forall a. Show a => a -> String
show Integer
i
jsAst2Text (JsStr String
s) =   Text
"\"" Text -> Text -> Text
`T.append` String -> Text
T.pack ((Char -> String) -> ShowS
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Char -> String
translateChar String
s) Text -> Text -> Text
`T.append` Text
"\""
jsAst2Text (JsArray [JsExpr]
l) =
  [Text] -> Text
T.concat [Text
"[", Text -> [Text] -> Text
T.intercalate Text
", " ((JsExpr -> Text) -> [JsExpr] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map JsExpr -> Text
jsAst2Text [JsExpr]
l), Text
"]"]
jsAst2Text (JsErrorExp JsExpr
t) =
  [Text] -> Text
T.concat [Text
"$JSRTS.throw(new Error(  ", JsExpr -> Text
jsAst2Text JsExpr
t, Text
"))"]
jsAst2Text (JsBinOp Text
op JsExpr
a1 JsExpr
a2) =
  [Text] -> Text
T.concat [Text
"(", JsExpr -> Text
jsAst2Text JsExpr
a1, Text
" ", Text
op, Text
" ", JsExpr -> Text
jsAst2Text JsExpr
a2, Text
")"]
jsAst2Text (JsUniOp Text
op JsExpr
a) = [Text] -> Text
T.concat [Text
"(", Text
op, JsExpr -> Text
jsAst2Text JsExpr
a, Text
")"]
jsAst2Text (JsForeign Text
code [JsExpr]
args) =
  let args_repl :: Text -> t -> [Text] -> Text
args_repl Text
c t
i [] = Text
c
      args_repl Text
c t
i (Text
t:[Text]
r) =
        Text -> t -> [Text] -> Text
args_repl (Text -> Text -> Text -> Text
T.replace (Text
"%" Text -> Text -> Text
`T.append` String -> Text
T.pack (t -> String
forall a. Show a => a -> String
show t
i)) ([Text] -> Text
T.concat [Text
"(", Text
t, Text
")"]) Text
c) (t
i t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) [Text]
r
  in [Text] -> Text
T.concat [Text
"(", Text -> Integer -> [Text] -> Text
forall t. (Show t, Num t) => Text -> t -> [Text] -> Text
args_repl Text
code Integer
0 ((JsExpr -> Text) -> [JsExpr] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map JsExpr -> Text
jsAst2Text [JsExpr]
args), Text
")"]
jsAst2Text (JsB2I JsExpr
x) = JsExpr -> Text
jsAst2Text (JsExpr -> Text) -> JsExpr -> Text
forall a b. (a -> b) -> a -> b
$ Text -> JsExpr -> JsExpr -> JsExpr
JsBinOp Text
"+" JsExpr
x (Int -> JsExpr
JsInt Int
0)
jsAst2Text (JsForce JsExpr
e) = [Text] -> Text
T.concat [Text
"$JSRTS.force(", JsExpr -> Text
jsAst2Text JsExpr
e, Text
")"]

jsLazy :: JsExpr -> JsExpr
jsLazy :: JsExpr -> JsExpr
jsLazy JsExpr
e = JsExpr -> [JsExpr] -> JsExpr
JsNew (JsExpr -> Text -> JsExpr
JsProp (Text -> JsExpr
JsVar Text
"$JSRTS") Text
"Lazy") [([Text] -> JsStmt -> JsExpr
JsLambda [] (JsStmt -> JsExpr) -> JsStmt -> JsExpr
forall a b. (a -> b) -> a -> b
$ JsExpr -> JsStmt
JsReturn JsExpr
e)]

jsExpr2Stmt :: JsExpr -> JsStmt
jsExpr2Stmt :: JsExpr -> JsStmt
jsExpr2Stmt = JsExpr -> JsStmt
JsExprStmt

jsStmt2Expr :: JsStmt -> JsExpr
jsStmt2Expr :: JsStmt -> JsExpr
jsStmt2Expr (JsExprStmt JsExpr
x) = JsExpr
x
jsStmt2Expr JsStmt
x = JsExpr -> [JsExpr] -> JsExpr
JsApp ([Text] -> JsStmt -> JsExpr
JsLambda [] JsStmt
x) []