on 7de84bc help sign in
91 lines 1.6 kB history raw
1module Generate.JavaScript.Helpers where
3import Language.ECMAScript3.Syntax
7localRuntime :: String
8localRuntime =
9 "_elm"
12varDecl :: String -> Expression () -> VarDecl ()
13varDecl x expr =
14 VarDecl () (var x) (Just expr)
17refOrObject :: String -> Expression ()
18refOrObject name =
19 InfixExpr () OpLOr (ref name) (ObjectLit () [])
23-- Creating Variables
26var :: String -> Id ()
27var name =
28 Id () name
31ref :: String -> Expression ()
32ref name =
33 VarRef () (var name)
36prop :: String -> Prop ()
37prop name =
38 PropId () (var name)
41obj :: [String] -> Expression ()
42obj vars =
43 case vars of
44 x:xs ->
45 foldl (DotRef ()) (ref x) (map var xs)
47 [] ->
48 error "dotSep must be called on a non-empty list of variables"
52-- Function Calls
55(<|) :: Expression () -> Expression () -> Expression ()
56(<|) f x =
57 CallExpr () f [x]
60function :: [String] -> [Statement ()] -> Expression ()
61function args stmts =
62 FuncExpr () Nothing (map var args) stmts
65call :: Expression () -> [Expression ()] -> Expression ()
66call =
67 CallExpr ()
71-- Checks
74equal :: Expression () -> Expression () -> Expression ()
75equal a b =
76 InfixExpr () OpStrictEq a b
79instanceof :: String -> Expression () -> Expression ()
80instanceof tipe x =
81 InfixExpr () OpLAnd (typeof "object" x) (InfixExpr () OpInstanceof x (ref tipe))
84typeof :: String -> Expression () -> Expression ()
85typeof tipe x =
86 equal (PrefixExpr () PrefixTypeof x) (StringLit () tipe)
89member :: String -> Expression () -> Expression ()
90member field x =
91 InfixExpr () OpIn (StringLit () field) x