module Language.Fay.Print where
import Language.Fay.Types
import Data.List
import Data.String
import Language.Haskell.Exts.Syntax
import Prelude hiding (exp)
import Text.JSON
instance Printable JsLit where
printJS (JsChar char) = encode [char]
printJS (JsStr str) = encode str
printJS (JsInt int) = show int
printJS (JsFloating rat) = show rat
printJS (JsBool b) = if b then "true" else "false"
instance Printable QName where
printJS qname =
case qname of
Qual moduleName name -> printJS moduleName ++ "$$" ++ printJS name
UnQual name -> printJS name
Special con -> printJS con
instance Printable SpecialCon where
printJS specialCon =
case specialCon of
UnitCon -> printJS (Qual "Fay" (Ident "unit"))
ListCon -> printJS (Qual "Fay" (Ident "emptyList"))
FunCon -> printJS (Qual "Fay" (Ident "funCon"))
TupleCon boxed n -> printJS (Qual "Fay"
(Ident (if boxed == Boxed
then "boxed"
else "unboxed" ++
"TupleOf" ++ show n)))
Cons -> printJS (Qual "Fay" (Ident "cons"))
UnboxedSingleCon -> printJS (Qual "Fay" (Ident "unboxedSingleCon"))
instance Printable ModuleName where
printJS (ModuleName moduleName) =
jsEncodeName moduleName
instance Printable Name where
printJS name =
case name of
Ident ident -> jsEncodeName ident
Symbol sym -> jsEncodeName sym
instance Printable [JsStmt] where
printJS = concat . map (printJS)
instance Printable JsStmt where
printJS (JsVar name expr) =
(unwords ["var",printJS name,"=",printJS expr ++ ";"])
printJS (JsUpdate name expr) =
(unwords [printJS name,"=",printJS expr ++ ";"])
printJS (JsSetProp name prop expr) =
(concat [printJS name,".",printJS prop," = ",printJS expr ++ ";"])
printJS (JsIf exp thens elses) =
concat
[("if (" ++ printJS exp ++ ") {")
,printJS thens] ++
if (length elses > 0)
then concat ["} else {"
,printJS elses ++ "}"]
else "}"
printJS (JsEarlyReturn exp) =
("return " ++ printJS exp ++ ";")
printJS (JsThrow exp) =
("throw " ++ printJS exp ++ ";")
printJS (JsWhile cond stmts) =
unwords ["while (" ++ printJS cond ++ ") {"
,printJS stmts
,"}"]
printJS JsContinue = "continue;"
instance Printable JsExp where
printJS (JsRawExp name) = name
printJS (JsThrowExp exp) =
"(function(){ throw (" ++ printJS exp ++ "); })()"
printJS (JsFun params stmts ret) =
concat ["function("
,intercalate "," (map (printJS) params)
,"){"
,printJS stmts
] ++
case ret of
Just ret' ->
concat ["return "
,printJS ret'
,";"
,"}"]
Nothing -> "}"
printJS JsNull = "null"
printJS (JsSequence exprs) =
intercalate "," (map (printJS) exprs)
printJS (JsName name) = printJS name
printJS (JsApp op args) =
printJS (if isFunc op then JsParen op else op) ++
"(" ++
intercalate "," (map (printJS) args) ++
")"
where isFunc JsFun{..} = True; isFunc _ = False
printJS (JsLit lit) = printJS lit
printJS (JsParen exp) = "(" ++ printJS exp ++ ")"
printJS (JsTernaryIf cond conseq alt) =
concat [printJS cond ++ " ? "
, (printJS conseq) ++ " : "
, (printJS alt)]
printJS (JsList exps) =
"[" ++
intercalate "," (map (printJS) exps) ++
"]"
printJS (JsNew name args) =
"new " ++ printJS (JsApp (JsName name) args)
printJS (JsInstanceOf exp classname) =
printJS exp ++ " instanceof " ++ printJS classname
printJS (JsIndex i exp) =
"(" ++ printJS exp ++ ")[" ++ show i ++ "]"
printJS (JsEq exp1 exp2) =
printJS exp1 ++ " === " ++ printJS exp2
printJS (JsGetProp exp prop) =
printJS exp ++ "." ++ printJS prop
printJS (JsUpdateProp name prop expr) =
(concat ["(",printJS name,".",printJS prop," = ",printJS expr,")"])
printJS (JsInfix op x y) =
printJS x ++ " " ++ op ++ " " ++ printJS y
printJS (JsGetPropExtern exp prop) =
printJS exp ++ "['" ++ printJS prop ++ "']"
printJS (JsUpdatePropExtern name prop expr) =
(concat ["(",printJS name,"['",printJS prop,"'] = ",printJS expr,")"])
jsEncodeName :: String -> String
jsEncodeName ":tmp" = "$tmp"
jsEncodeName ":thunk" = "$"
jsEncodeName ":this" = "this"
jsEncodeName "null" = "_$null"
jsEncodeName "this" = "_$this"
jsEncodeName name =
if isPrefixOf "$_" name
then name
else concat . map encode $ name
where
encode c | elem c allowed = [c]
| otherwise = escapeChar c
allowed = ['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9'] ++ "_"
escapeChar c = "$" ++ charId c ++ "$"
charId c = show (fromEnum c)
instance IsString ModuleName where
fromString = ModuleName
instance IsString JsName where
fromString = UnQual . Ident