on 427586a help sign in
118 lines 2.2 kB history raw
1module Generate.JavaScript.Helpers where
3import Language.ECMAScript3.Syntax
5import qualified Reporting.Region as R
8localRuntime :: String
9localRuntime =
10 "_elm"
13varDecl :: String -> Expression () -> VarDecl ()
14varDecl x expr =
15 VarDecl () (var x) (Just expr)
18make :: [String] -> Expression ()
19make moduleName =
20 obj (moduleName ++ ["make"]) <| ref localRuntime
23useLazy :: [String] -> String -> Expression ()
24useLazy moduleName functionName =
25 DotRef () (make moduleName) (var functionName)
28-- Creating Variables
30var :: String -> Id ()
31var name =
32 Id () name
35ref :: String -> Expression ()
36ref name =
37 VarRef () (var name)
40prop :: String -> Prop ()
41prop name =
42 PropId () (var name)
45string = StringLit ()
48obj :: [String] -> Expression ()
49obj vars =
50 case vars of
51 x:xs -> foldl (DotRef ()) (ref x) (map var xs)
52 [] -> error "dotSep must be called on a non-empty list of variables"
55-- Specific Utilities
57localModuleName :: String
58localModuleName =
59 "$moduleName"
62throw :: String -> R.Region -> Expression ()
63throw kind region =
64 let args =
65 [ ref localModuleName
66 , string (R.toString region)
67 ]
68 in
69 obj ["_U",kind] `call` args
72-- Function Calls
74(<|) :: Expression () -> Expression () -> Expression ()
75(<|) f x =
76 CallExpr () f [x]
79ret :: Expression () -> Statement ()
80ret e =
81 ReturnStmt () (Just e)
84(==>) :: [String] -> Expression () -> Expression ()
85(==>) args e =
86 FuncExpr () Nothing (map var args) [ ret e ]
89function :: [String] -> [Statement ()] -> Expression ()
90function args stmts =
91 FuncExpr () Nothing (map var args) stmts
94call :: Expression () -> [Expression ()] -> Expression ()
95call =
96 CallExpr ()
99-- Checks
101equal :: Expression () -> Expression () -> Expression ()
102equal a b =
103 InfixExpr () OpStrictEq a b
106instanceof :: String -> Expression () -> Expression ()
107instanceof tipe x =
108 InfixExpr () OpLAnd (typeof "object" x) (InfixExpr () OpInstanceof x (ref tipe))
111typeof :: String -> Expression () -> Expression ()
112typeof tipe x =
113 equal (PrefixExpr () PrefixTypeof x) (string tipe)
116member :: String -> Expression () -> Expression ()
117member field x =
118 InfixExpr () OpIn (string field) x