on master help sign in
style: use modern style guide
commit8f8469b8298f5833c19b67d50cbf0f26513a625b authorEvan Czaplicki <info@elm-lang.org> dateSun 30 Nov 2014, 17:04 (12 years ago) parent4c0cab2
2 files changed+53−14
src/Generate/JavaScript/Helpers.hs+45−13 src/Generate/JavaScript/Variable.hs+8−1
src/Generate/JavaScript/Helpers.hs+45−13
@@ -7,59 +7,91 @@ import Language.ECMAScript3.Syntax
77-- Creating Variables
88
99var :: String -> Id ()
10−var name = Id () name
10+var name =
11+ Id () name
12+
1113
1214ref :: String -> Expression ()
13−ref name = VarRef () (var name)
15+ref name =
16+ VarRef () (var name)
17+
1418
1519prop :: String -> Prop ()
16−prop name = PropId () (var name)
20+prop name =
21+ PropId () (var name)
22+
1723
1824string = StringLit ()
1925
26+
2027obj :: [String] -> Expression ()
2128obj vars =
2229 case vars of
2330 x:xs -> foldl (DotRef ()) (ref x) (map var xs)
2431 [] -> error "dotSep must be called on a non-empty list of variables"
2532
33+
2634-- Specific Utilities
2735
2836localModuleName :: String
29−localModuleName = "$moduleName"
37+localModuleName =
38+ "$moduleName"
39+
3040
3141throw :: String -> Region -> Expression ()
3242throw kind region =
33− obj ["_U",kind] `call` [ ref localModuleName, string (PP.renderPretty region) ]
43+ let args =
44+ [ ref localModuleName
45+ , string (PP.renderPretty region)
46+ ]
47+ in
48+ obj ["_U",kind] `call` args
49+
3450
3551-- Function Calls
3652
3753(<|) :: Expression () -> Expression () -> Expression ()
38−(<|) f x = CallExpr () f [x]
54+(<|) f x =
55+ CallExpr () f [x]
56+
3957
4058ret :: Expression () -> Statement ()
41−ret e = ReturnStmt () (Just e)
59+ret e =
60+ ReturnStmt () (Just e)
61+
4262
4363(==>) :: [String] -> Expression () -> Expression ()
44−(==>) args e = FuncExpr () Nothing (map var args) [ ret e ]
64+(==>) args e =
65+ FuncExpr () Nothing (map var args) [ ret e ]
66+
4567
4668function :: [String] -> [Statement ()] -> Expression ()
47−function args stmts = FuncExpr () Nothing (map var args) stmts
69+function args stmts =
70+ FuncExpr () Nothing (map var args) stmts
71+
4872
4973call :: Expression () -> [Expression ()] -> Expression ()
50−call = CallExpr ()
74+call =
75+ CallExpr ()
76+
5177
5278-- Checks
5379
5480equal :: Expression () -> Expression () -> Expression ()
55−equal a b = InfixExpr () OpStrictEq a b
81+equal a b =
82+ InfixExpr () OpStrictEq a b
83+
5684
5785instanceof :: String -> Expression () -> Expression ()
5886instanceof tipe x =
5987 InfixExpr () OpLAnd (typeof "object" x) (InfixExpr () OpInstanceof x (ref tipe))
6088
89+
6190typeof :: String -> Expression () -> Expression ()
62−typeof tipe x = equal (PrefixExpr () PrefixTypeof x) (string tipe)
91+typeof tipe x =
92+ equal (PrefixExpr () PrefixTypeof x) (string tipe)
93+
6394
6495member :: String -> Expression () -> Expression ()
65−member field x = InfixExpr () OpIn (string field) x
96+member field x =
97+ InfixExpr () OpIn (string field) x
src/Generate/JavaScript/Variable.hs+8−1
@@ -8,8 +8,11 @@ import qualified Data.Set as Set
88import qualified Generate.JavaScript.Helpers as JS
99import qualified Language.ECMAScript3.Syntax as JS
1010
11+
1112swap :: Char -> Char -> Char -> Char
12−swap from to c = if c == from then to else c
13+swap from to c =
14+ if c == from then to else c
15+
1316
1417canonical :: Var.Canonical -> JS.Expression ()
1518canonical (Var.Canonical home name) =
@@ -23,19 +26,23 @@ canonical (Var.Canonical home name) =
2326 Var.BuiltIn -> []
2427 Var.Module path -> [ moduleName path ]
2528
29+
2630moduleName :: Module.Name -> String
2731moduleName name =
2832 '$' : List.intercalate "$" name
2933
34+
3035varName :: String -> String
3136varName x = map (swap '\'' '$') x'
3237 where
3338 x' = if Set.member x jsReserveds then '$' : x else x
3439
40+
3541value :: Module.Name -> String -> JS.Expression ()
3642value home name =
3743 canonical (Var.Canonical (Var.Module home) name)
3844
45+
3946jsReserveds :: Set.Set String
4047jsReserveds = Set.fromList
4148 [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"