on d41a656 help sign in
45 lines 1.3 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Generate.JavaScript.Helpers where
4import qualified AST.Helpers as Help
5import Language.ECMAScript3.Syntax
7dolarize :: String -> String
8dolarize x = map (\c -> if c == '.' then '$' else c) x
10split :: String -> [String]
11split = go []
12 where
13 go vars str =
14 case break (=='.') str of
15 (x,_:rest) | Help.isOp x -> vars ++ [x ++ '.' : rest]
16 | otherwise -> go (vars ++ [x]) rest
17 (x,[]) -> vars ++ [x]
19var :: String -> Id ()
20var name = Id () name
22ref :: String -> Expression ()
23ref name = VarRef () (var name)
25prop name = PropId () (var name)
26f <| x = CallExpr () f [x]
27ret e = ReturnStmt () (Just e)
28args ==> e = FuncExpr () Nothing (map var args) [ ret e ]
29function args stmts = FuncExpr () Nothing (map var args) stmts
30call = CallExpr ()
31string = StringLit ()
33dotSep :: [String] -> Expression ()
34dotSep vars =
35 case vars of
36 x:xs -> foldl (DotRef ()) (ref x) (map var xs)
37 [] -> error "dotSep must be called on a non-empty list of variables"
39obj = dotSep . split
41equal a b = InfixExpr () OpStrictEq a b
42instanceof tipe x =
43 InfixExpr () OpLAnd (typeof "object" x) (InfixExpr () OpInstanceof x (ref tipe))
44typeof tipe x = equal (PrefixExpr () PrefixTypeof x) (string tipe)
45member field x = InfixExpr () OpIn (string field) x