on master help sign in
style: use modern style guide
8f8469bEvan Czaplicki
src/Generate/JavaScript/Helpers.hs+45−13
@@ -7,59 +7,91 @@ import Language.ECMAScript3.Syntax
7-- Creating Variables
7-- Creating Variables
8
8
9var :: String -> Id ()
9var :: String -> Id ()
10−var name = Id () name
10+var name =
11+ Id () name
12+
11
13
12ref :: String -> Expression ()
14ref :: String -> Expression ()
15+ref name =
13−ref name = VarRef () (var name)
16+ VarRef () (var name)
17+
14
18
15prop :: String -> Prop ()
19prop :: String -> Prop ()
20+prop name =
16−prop name = PropId () (var name)
21+ PropId () (var name)
22+
17
23
18string = StringLit ()
24string = StringLit ()
19
25
26+
20obj :: [String] -> Expression ()
27obj :: [String] -> Expression ()
21obj vars =
28obj vars =
22 case vars of
29 case vars of
23 x:xs -> foldl (DotRef ()) (ref x) (map var xs)
30 x:xs -> foldl (DotRef ()) (ref x) (map var xs)
24 [] -> error "dotSep must be called on a non-empty list of variables"
31 [] -> error "dotSep must be called on a non-empty list of variables"
25
32
33+
26-- Specific Utilities
34-- Specific Utilities
27
35
28localModuleName :: String
36localModuleName :: String
29−localModuleName = "$moduleName"
37+localModuleName =
38+ "$moduleName"
39+
30
40
31throw :: String -> Region -> Expression ()
41throw :: String -> Region -> Expression ()
32throw kind region =
42throw kind region =
43+ let args =
44+ [ ref localModuleName
33− obj ["_U",kind] `call` [ ref localModuleName, string (PP.renderPretty region) ]
45+ , string (PP.renderPretty region)
46+ ]
47+ in
48+ obj ["_U",kind] `call` args
49+
34
50
35-- Function Calls
51-- Function Calls
36
52
37(<|) :: Expression () -> Expression () -> Expression ()
53(<|) :: Expression () -> Expression () -> Expression ()
54+(<|) f x =
38−(<|) f x = CallExpr () f [x]
55+ CallExpr () f [x]
56+
39
57
40ret :: Expression () -> Statement ()
58ret :: Expression () -> Statement ()
59+ret e =
41−ret e = ReturnStmt () (Just e)
60+ ReturnStmt () (Just e)
61+
42
62
43(==>) :: [String] -> Expression () -> Expression ()
63(==>) :: [String] -> Expression () -> Expression ()
64+(==>) args e =
44−(==>) args e = FuncExpr () Nothing (map var args) [ ret e ]
65+ FuncExpr () Nothing (map var args) [ ret e ]
66+
45
67
46function :: [String] -> [Statement ()] -> Expression ()
68function :: [String] -> [Statement ()] -> Expression ()
69+function args stmts =
47−function args stmts = FuncExpr () Nothing (map var args) stmts
70+ FuncExpr () Nothing (map var args) stmts
71+
48
72
49call :: Expression () -> [Expression ()] -> Expression ()
73call :: Expression () -> [Expression ()] -> Expression ()
74+call =
50−call = CallExpr ()
75+ CallExpr ()
76+
51
77
52-- Checks
78-- Checks
53
79
54equal :: Expression () -> Expression () -> Expression ()
80equal :: Expression () -> Expression () -> Expression ()
81+equal a b =
55−equal a b = InfixExpr () OpStrictEq a b
82+ InfixExpr () OpStrictEq a b
83+
56
84
57instanceof :: String -> Expression () -> Expression ()
85instanceof :: String -> Expression () -> Expression ()
58instanceof tipe x =
86instanceof tipe x =
59 InfixExpr () OpLAnd (typeof "object" x) (InfixExpr () OpInstanceof x (ref tipe))
87 InfixExpr () OpLAnd (typeof "object" x) (InfixExpr () OpInstanceof x (ref tipe))
60
88
89+
61typeof :: String -> Expression () -> Expression ()
90typeof :: String -> Expression () -> Expression ()
91+typeof tipe x =
62−typeof tipe x = equal (PrefixExpr () PrefixTypeof x) (string tipe)
92+ equal (PrefixExpr () PrefixTypeof x) (string tipe)
93+
63
94
64member :: String -> Expression () -> Expression ()
95member :: String -> Expression () -> Expression ()
96+member field x =
65−member field x = InfixExpr () OpIn (string 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
8import qualified Generate.JavaScript.Helpers as JS
8import qualified Generate.JavaScript.Helpers as JS
9import qualified Language.ECMAScript3.Syntax as JS
9import qualified Language.ECMAScript3.Syntax as JS
10
10
11+
11swap :: Char -> Char -> Char -> Char
12swap :: Char -> Char -> Char -> Char
13+swap from to c =
12−swap from to c = if c == from then to else c
14+ if c == from then to else c
15+
13
16
14canonical :: Var.Canonical -> JS.Expression ()
17canonical :: Var.Canonical -> JS.Expression ()
15canonical (Var.Canonical home name) =
18canonical (Var.Canonical home name) =
@@ -23,19 +26,23 @@ canonical (Var.Canonical home name) =
23 Var.BuiltIn -> []
26 Var.BuiltIn -> []
24 Var.Module path -> [ moduleName path ]
27 Var.Module path -> [ moduleName path ]
25
28
29+
26moduleName :: Module.Name -> String
30moduleName :: Module.Name -> String
27moduleName name =
31moduleName name =
28 '$' : List.intercalate "$" name
32 '$' : List.intercalate "$" name
29
33
34+
30varName :: String -> String
35varName :: String -> String
31varName x = map (swap '\'' '$') x'
36varName x = map (swap '\'' '$') x'
32 where
37 where
33 x' = if Set.member x jsReserveds then '$' : x else x
38 x' = if Set.member x jsReserveds then '$' : x else x
34
39
40+
35value :: Module.Name -> String -> JS.Expression ()
41value :: Module.Name -> String -> JS.Expression ()
36value home name =
42value home name =
37 canonical (Var.Canonical (Var.Module home) name)
43 canonical (Var.Canonical (Var.Module home) name)
38
44
45+
39jsReserveds :: Set.Set String
46jsReserveds :: Set.Set String
40jsReserveds = Set.fromList
47jsReserveds = Set.fromList
41 [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
48 [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"