on 2d29699 help sign in
99 lines 1.9 kB history raw
1module Generate.JavaScript.Helpers where
3import Language.ECMAScript3.Syntax
6localRuntime :: String
7localRuntime =
8 "_elm"
11varDecl :: String -> Expression () -> VarDecl ()
12varDecl x expr =
13 VarDecl () (var x) (Just expr)
16make :: [String] -> Expression ()
17make moduleName =
18 obj (moduleName ++ ["make"]) <| ref localRuntime
21useLazy :: [String] -> String -> Expression ()
22useLazy moduleName functionName =
23 DotRef () (make moduleName) (var functionName)
26-- Creating Variables
28var :: String -> Id ()
29var name =
30 Id () name
33ref :: String -> Expression ()
34ref name =
35 VarRef () (var name)
38prop :: String -> Prop ()
39prop name =
40 PropId () (var name)
43string = StringLit ()
46obj :: [String] -> Expression ()
47obj vars =
48 case vars of
49 x:xs -> foldl (DotRef ()) (ref x) (map var xs)
50 [] -> error "dotSep must be called on a non-empty list of variables"
53-- Function Calls
55(<|) :: Expression () -> Expression () -> Expression ()
56(<|) f x =
57 CallExpr () f [x]
60ret :: Expression () -> Statement ()
61ret e =
62 ReturnStmt () (Just e)
65(==>) :: [String] -> Expression () -> Expression ()
66(==>) args e =
67 FuncExpr () Nothing (map var args) [ ret e ]
70function :: [String] -> [Statement ()] -> Expression ()
71function args stmts =
72 FuncExpr () Nothing (map var args) stmts
75call :: Expression () -> [Expression ()] -> Expression ()
76call =
77 CallExpr ()
80-- Checks
82equal :: Expression () -> Expression () -> Expression ()
83equal a b =
84 InfixExpr () OpStrictEq a b
87instanceof :: String -> Expression () -> Expression ()
88instanceof tipe x =
89 InfixExpr () OpLAnd (typeof "object" x) (InfixExpr () OpInstanceof x (ref tipe))
92typeof :: String -> Expression () -> Expression ()
93typeof tipe x =
94 equal (PrefixExpr () PrefixTypeof x) (string tipe)
97member :: String -> Expression () -> Expression ()
98member field x =
99 InfixExpr () OpIn (string field) x