on master help sign in
cosmetic: remove some helper functions that are not used too much
806e4c4Evan Czaplicki
src/Generate/JavaScript/Expression.hs+7−4
@@ -47,7 +47,8 @@ toStatementList :: Code -> [Statement ()]
47toStatementList code =
47toStatementList code =
48 case code of
48 case code of
49 JsExpr expr ->
49 JsExpr expr ->
50− [ ret expr ]
50+ [ ReturnStmt () (Just expr) ]
51+
51 JsBlock stmts ->
52 JsBlock stmts ->
52 stmts
53 stmts
53
54
@@ -56,9 +57,11 @@ toStatement :: Code -> Statement ()
56toStatement code =
57toStatement code =
57 case code of
58 case code of
58 JsExpr expr ->
59 JsExpr expr ->
59− ret expr
60+ ReturnStmt () (Just expr)
61+
60 JsBlock [stmt] ->
62 JsBlock [stmt] ->
61 stmt
63 stmt
64+
62 JsBlock stmts ->
65 JsBlock stmts ->
63 BlockStmt () stmts
66 BlockStmt () stmts
64
67
@@ -193,7 +196,7 @@ generateCode expr =
193 do jsMembers <- mapM generateJsExpr members
196 do jsMembers <- mapM generateJsExpr members
194 jsExpr $ ObjectLit () (ctor : fields jsMembers)
197 jsExpr $ ObjectLit () (ctor : fields jsMembers)
195 where
198 where
196− ctor = (prop "ctor", string tag)
199+ ctor = (prop "ctor", StringLit () tag)
197 fields =
200 fields =
198 zipWith (\n e -> (prop ("_" ++ show n), e)) [ 0 :: Int .. ]
201 zipWith (\n e -> (prop ("_" ++ show n), e)) [ 0 :: Int .. ]
199
202
@@ -254,7 +257,7 @@ generateFunctionWithArity rawArgs code =
254 (lastArg:otherArgs) = reverse args
257 (lastArg:otherArgs) = reverse args
255 innerBody = function [lastArg] (toStatementList code)
258 innerBody = function [lastArg] (toStatementList code)
256 in
259 in
257− foldl (\body arg -> function [arg] [ret body]) innerBody otherArgs
260+ foldl (\body arg -> function [arg] [ReturnStmt () (Just body)]) innerBody otherArgs
258
261
259
262
260-- GENERATE IFS
263-- GENERATE IFS
src/Generate/JavaScript/Helpers.hs+7−17
@@ -40,14 +40,14 @@ prop name =
40 PropId () (var name)
40 PropId () (var name)
41
41
42
42
43−string = StringLit ()
44−
45−
46obj :: [String] -> Expression ()
43obj :: [String] -> Expression ()
47obj vars =
44obj vars =
48 case vars of
45 case vars of
46+ x:xs ->
49− x:xs -> foldl (DotRef ()) (ref x) (map var xs)
47+ foldl (DotRef ()) (ref x) (map var xs)
48+
49+ [] ->
50− [] -> error "dotSep must be called on a non-empty list of variables"
50+ error "dotSep must be called on a non-empty list of variables"
51
51
52
52
53-- Function Calls
53-- Function Calls
@@ -57,16 +57,6 @@ obj vars =
57 CallExpr () f [x]
57 CallExpr () f [x]
58
58
59
59
60−ret :: Expression () -> Statement ()
61−ret e =
62− ReturnStmt () (Just e)
63−
64−
65−(==>) :: [String] -> Expression () -> Expression ()
66−(==>) args e =
67− FuncExpr () Nothing (map var args) [ ret e ]
68−
69−
70function :: [String] -> [Statement ()] -> Expression ()
60function :: [String] -> [Statement ()] -> Expression ()
71function args stmts =
61function args stmts =
72 FuncExpr () Nothing (map var args) stmts
62 FuncExpr () Nothing (map var args) stmts
@@ -91,9 +81,9 @@ instanceof tipe x =
91
81
92typeof :: String -> Expression () -> Expression ()
82typeof :: String -> Expression () -> Expression ()
93typeof tipe x =
83typeof tipe x =
94− equal (PrefixExpr () PrefixTypeof x) (string tipe)
84+ equal (PrefixExpr () PrefixTypeof x) (StringLit () tipe)
95
85
96
86
97member :: String -> Expression () -> Expression ()
87member :: String -> Expression () -> Expression ()
98member field x =
88member field x =
99− InfixExpr () OpIn (string field) x
89+ InfixExpr () OpIn (StringLit () field) x
src/Generate/JavaScript/Port.hs+13−13
@@ -19,7 +19,7 @@ task name expr portType =
19 _Task "perform" `call` [ expr ]
19 _Task "perform" `call` [ expr ]
20
20
21 T.Signal _ _ ->
21 T.Signal _ _ ->
22− _Task "performSignal" `call` [ string name, expr ]
22+ _Task "performSignal" `call` [ StringLit () name, expr ]
23
23
24
24
25
25
@@ -77,7 +77,7 @@ check x jsType continue =
77 foldl1 (InfixExpr () op) (map ($ value) checks)
77 foldl1 (InfixExpr () op) (map ($ value) checks)
78
78
79 throw =
79 throw =
80− obj ["_U","badPort"] `call` [ string (typeToString jsType), x ]
80+ obj ["_U","badPort"] `call` [ StringLit () (typeToString jsType), x ]
81
81
82 checks =
82 checks =
83 case jsType of
83 case jsType of
@@ -96,22 +96,22 @@ inbound name portType =
96 case portType of
96 case portType of
97 T.Normal tipe ->
97 T.Normal tipe ->
98 _Port "inbound" `call`
98 _Port "inbound" `call`
99− [ string name
99+ [ StringLit () name
100− , string (show (P.pretty Map.empty False tipe))
100+ , StringLit () (show (P.pretty Map.empty False tipe))
101 , toTypeFunction tipe
101 , toTypeFunction tipe
102 ]
102 ]
103
103
104 T.Signal _root arg ->
104 T.Signal _root arg ->
105 _Port "inboundSignal" `call`
105 _Port "inboundSignal" `call`
106− [ string name
106+ [ StringLit () name
107− , string (show (P.pretty Map.empty False arg))
107+ , StringLit () (show (P.pretty Map.empty False arg))
108 , toTypeFunction arg
108 , toTypeFunction arg
109 ]
109 ]
110
110
111
111
112toTypeFunction :: T.Canonical -> Expression ()
112toTypeFunction :: T.Canonical -> Expression ()
113toTypeFunction tipe =
113toTypeFunction tipe =
114− ["v"] ==> toType tipe (ref "v")
114+ function ["v"] [ ReturnStmt () (Just (toType tipe (ref "v"))) ]
115
115
116
116
117toType :: T.Canonical -> Expression () -> Expression ()
117toType :: T.Canonical -> Expression () -> Expression ()
@@ -186,7 +186,7 @@ toTuple types x =
186 (prop "ctor", ctor) : zipWith convert [0..] types
186 (prop "ctor", ctor) : zipWith convert [0..] types
187
187
188 ctor =
188 ctor =
189− string ("_Tuple" ++ show (length types))
189+ StringLit () ("_Tuple" ++ show (length types))
190
190
191 convert n t =
191 convert n t =
192 ( prop ('_':show n)
192 ( prop ('_':show n)
@@ -200,15 +200,15 @@ outbound :: String -> Expression () -> T.Port T.Canonical -> Expression ()
200outbound name expr portType =
200outbound name expr portType =
201 case portType of
201 case portType of
202 T.Normal tipe ->
202 T.Normal tipe ->
203− _Port "outbound" `call` [ string name, fromTypeFunction tipe, expr ]
203+ _Port "outbound" `call` [ StringLit () name, fromTypeFunction tipe, expr ]
204
204
205 T.Signal _ arg ->
205 T.Signal _ arg ->
206− _Port "outboundSignal" `call` [ string name, fromTypeFunction arg, expr ]
206+ _Port "outboundSignal" `call` [ StringLit () name, fromTypeFunction arg, expr ]
207
207
208
208
209fromTypeFunction :: T.Canonical -> Expression ()
209fromTypeFunction :: T.Canonical -> Expression ()
210fromTypeFunction tipe =
210fromTypeFunction tipe =
211− ["v"] ==> fromType tipe (ref "v")
211+ function ["v"] [ ReturnStmt () (Just (fromType tipe (ref "v"))) ]
212
212
213
213
214fromType :: T.Canonical -> Expression () -> Expression ()
214fromType :: T.Canonical -> Expression () -> Expression ()
@@ -230,7 +230,7 @@ fromType tipe x =
230 func body =
230 func body =
231 function (take numArgs args)
231 function (take numArgs args)
232 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
232 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
233− , ret (fromType (last ts) (ref "_r"))
233+ , ReturnStmt () (Just (fromType (last ts) (ref "_r")))
234 ]
234 ]
235
235
236 T.Var _ ->
236 T.Var _ ->
@@ -250,7 +250,7 @@ fromType tipe x =
250 T.Type name : [t]
250 T.Type name : [t]
251 | Var.isMaybe name ->
251 | Var.isMaybe name ->
252 CondExpr ()
252 CondExpr ()
253− (equal (DotRef () x (var "ctor")) (string "Nothing"))
253+ (equal (DotRef () x (var "ctor")) (StringLit () "Nothing"))
254 (NullLit ())
254 (NullLit ())
255 (fromType t (DotRef () x (var "_0")))
255 (fromType t (DotRef () x (var "_0")))
256
256
src/Generate/JavaScript/Variable.hs+14−7
@@ -1,4 +1,11 @@
1−module Generate.JavaScript.Variable (fresh, canonical, define, safe) where
1+module Generate.JavaScript.Variable
2+ ( fresh
3+ , canonical
4+ , modulePrefix
5+ , define
6+ , safe
7+ )
8+ where
2
9
3import qualified Control.Monad.State as State
10import qualified Control.Monad.State as State
4import qualified Data.List as List
11import qualified Data.List as List
@@ -8,7 +15,6 @@ import qualified Language.ECMAScript3.Syntax as JS
8import qualified AST.Helpers as Help
15import qualified AST.Helpers as Help
9import qualified AST.Module.Name as ModuleName
16import qualified AST.Module.Name as ModuleName
10import qualified AST.Variable as Var
17import qualified AST.Variable as Var
11−import qualified Elm.Package as Pkg
12import qualified Generate.JavaScript.Helpers as JS
18import qualified Generate.JavaScript.Helpers as JS
13
19
14
20
@@ -44,7 +50,7 @@ define name body =
44canonical :: Var.Canonical -> JS.Expression ()
50canonical :: Var.Canonical -> JS.Expression ()
45canonical (Var.Canonical home name) =
51canonical (Var.Canonical home name) =
46 if Help.isOp name then
52 if Help.isOp name then
47− JS.BracketRef () (addRoot home "_op") (JS.string name)
53+ JS.BracketRef () (addRoot home "_op") (JS.StringLit () name)
48
54
49 else
55 else
50 addRoot home (safe name)
56 addRoot home (safe name)
@@ -62,12 +68,12 @@ addRoot home name =
62 Var.BuiltIn ->
68 Var.BuiltIn ->
63 JS.ref name
69 JS.ref name
64
70
65− Var.Module moduleName ->
71+ Var.Module (ModuleName.Canonical _ moduleName) ->
66− JS.DotRef () (JS.ref (canonicalModuleName moduleName)) (JS.Id () name)
72+ JS.DotRef () (JS.ref (modulePrefix moduleName)) (JS.Id () name)
67
73
68
74
69−canonicalModuleName :: ModuleName.Canonical -> String
75+modulePrefix :: ModuleName.Raw -> String
70−canonicalModuleName (ModuleName.Canonical (Pkg.Name _user _project) moduleName) =
76+modulePrefix moduleName =
71 '$' : List.intercalate "$" moduleName
77 '$' : List.intercalate "$" moduleName
72
78
73
79
@@ -103,4 +109,5 @@ jsReserveds =
103 -- reserved by the Elm runtime system
109 -- reserved by the Elm runtime system
104 , "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9"
110 , "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9"
105 , "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"
111 , "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"
112+ , JS.localRuntime
106 ]
113 ]