on master help sign in
cosmetic: remove some helper functions that are not used too much
commit806e4c49c68be40343d5eb2c72d18392df1a8593 authorEvan Czaplicki <info@elm-lang.org> dateWed 30 Sep 2015, 00:14 (11 years ago) parent73d2c2c
4 files changed+41−41
src/Generate/JavaScript/Expression.hs+7−4 src/Generate/JavaScript/Helpers.hs+7−17 src/Generate/JavaScript/Port.hs+13−13 src/Generate/JavaScript/Variable.hs+14−7
src/Generate/JavaScript/Expression.hs+7−4
@@ -47,7 +47,8 @@ toStatementList :: Code -> [Statement ()]
4747toStatementList code =
4848 case code of
4949 JsExpr expr ->
50− [ ret expr ]
50+ [ ReturnStmt () (Just expr) ]
51+
5152 JsBlock stmts ->
5253 stmts
5354
@@ -56,9 +57,11 @@ toStatement :: Code -> Statement ()
5657toStatement code =
5758 case code of
5859 JsExpr expr ->
59− ret expr
60+ ReturnStmt () (Just expr)
61+
6062 JsBlock [stmt] ->
6163 stmt
64+
6265 JsBlock stmts ->
6366 BlockStmt () stmts
6467
@@ -193,7 +196,7 @@ generateCode expr =
193196 do jsMembers <- mapM generateJsExpr members
194197 jsExpr $ ObjectLit () (ctor : fields jsMembers)
195198 where
196− ctor = (prop "ctor", string tag)
199+ ctor = (prop "ctor", StringLit () tag)
197200 fields =
198201 zipWith (\n e -> (prop ("_" ++ show n), e)) [ 0 :: Int .. ]
199202
@@ -254,7 +257,7 @@ generateFunctionWithArity rawArgs code =
254257 (lastArg:otherArgs) = reverse args
255258 innerBody = function [lastArg] (toStatementList code)
256259 in
257− foldl (\body arg -> function [arg] [ret body]) innerBody otherArgs
260+ foldl (\body arg -> function [arg] [ReturnStmt () (Just body)]) innerBody otherArgs
258261
259262
260263-- GENERATE IFS
src/Generate/JavaScript/Helpers.hs+7−17
@@ -40,14 +40,14 @@ prop name =
4040 PropId () (var name)
4141
4242
43−string = StringLit ()
44−
45−
4643obj :: [String] -> Expression ()
4744obj vars =
4845 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"
46+ x:xs ->
47+ foldl (DotRef ()) (ref x) (map var xs)
48+
49+ [] ->
50+ error "dotSep must be called on a non-empty list of variables"
5151
5252
5353-- Function Calls
@@ -57,16 +57,6 @@ obj vars =
5757 CallExpr () f [x]
5858
5959
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−
7060function :: [String] -> [Statement ()] -> Expression ()
7161function args stmts =
7262 FuncExpr () Nothing (map var args) stmts
@@ -91,9 +81,9 @@ instanceof tipe x =
9181
9282typeof :: String -> Expression () -> Expression ()
9383typeof tipe x =
94− equal (PrefixExpr () PrefixTypeof x) (string tipe)
84+ equal (PrefixExpr () PrefixTypeof x) (StringLit () tipe)
9585
9686
9787member :: String -> Expression () -> Expression ()
9888member 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 =
1919 _Task "perform" `call` [ expr ]
2020
2121 T.Signal _ _ ->
22− _Task "performSignal" `call` [ string name, expr ]
22+ _Task "performSignal" `call` [ StringLit () name, expr ]
2323
2424
2525
@@ -77,7 +77,7 @@ check x jsType continue =
7777 foldl1 (InfixExpr () op) (map ($ value) checks)
7878
7979 throw =
80− obj ["_U","badPort"] `call` [ string (typeToString jsType), x ]
80+ obj ["_U","badPort"] `call` [ StringLit () (typeToString jsType), x ]
8181
8282 checks =
8383 case jsType of
@@ -96,22 +96,22 @@ inbound name portType =
9696 case portType of
9797 T.Normal tipe ->
9898 _Port "inbound" `call`
99− [ string name
100− , string (show (P.pretty Map.empty False tipe))
99+ [ StringLit () name
100+ , StringLit () (show (P.pretty Map.empty False tipe))
101101 , toTypeFunction tipe
102102 ]
103103
104104 T.Signal _root arg ->
105105 _Port "inboundSignal" `call`
106− [ string name
107− , string (show (P.pretty Map.empty False arg))
106+ [ StringLit () name
107+ , StringLit () (show (P.pretty Map.empty False arg))
108108 , toTypeFunction arg
109109 ]
110110
111111
112112toTypeFunction :: T.Canonical -> Expression ()
113113toTypeFunction tipe =
114− ["v"] ==> toType tipe (ref "v")
114+ function ["v"] [ ReturnStmt () (Just (toType tipe (ref "v"))) ]
115115
116116
117117toType :: T.Canonical -> Expression () -> Expression ()
@@ -186,7 +186,7 @@ toTuple types x =
186186 (prop "ctor", ctor) : zipWith convert [0..] types
187187
188188 ctor =
189− string ("_Tuple" ++ show (length types))
189+ StringLit () ("_Tuple" ++ show (length types))
190190
191191 convert n t =
192192 ( prop ('_':show n)
@@ -200,15 +200,15 @@ outbound :: String -> Expression () -> T.Port T.Canonical -> Expression ()
200200outbound name expr portType =
201201 case portType of
202202 T.Normal tipe ->
203− _Port "outbound" `call` [ string name, fromTypeFunction tipe, expr ]
203+ _Port "outbound" `call` [ StringLit () name, fromTypeFunction tipe, expr ]
204204
205205 T.Signal _ arg ->
206− _Port "outboundSignal" `call` [ string name, fromTypeFunction arg, expr ]
206+ _Port "outboundSignal" `call` [ StringLit () name, fromTypeFunction arg, expr ]
207207
208208
209209fromTypeFunction :: T.Canonical -> Expression ()
210210fromTypeFunction tipe =
211− ["v"] ==> fromType tipe (ref "v")
211+ function ["v"] [ ReturnStmt () (Just (fromType tipe (ref "v"))) ]
212212
213213
214214fromType :: T.Canonical -> Expression () -> Expression ()
@@ -230,7 +230,7 @@ fromType tipe x =
230230 func body =
231231 function (take numArgs args)
232232 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
233− , ret (fromType (last ts) (ref "_r"))
233+ , ReturnStmt () (Just (fromType (last ts) (ref "_r")))
234234 ]
235235
236236 T.Var _ ->
@@ -250,7 +250,7 @@ fromType tipe x =
250250 T.Type name : [t]
251251 | Var.isMaybe name ->
252252 CondExpr ()
253− (equal (DotRef () x (var "ctor")) (string "Nothing"))
253+ (equal (DotRef () x (var "ctor")) (StringLit () "Nothing"))
254254 (NullLit ())
255255 (fromType t (DotRef () x (var "_0")))
256256
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
29
310import qualified Control.Monad.State as State
411import qualified Data.List as List
@@ -8,7 +15,6 @@ import qualified Language.ECMAScript3.Syntax as JS
815import qualified AST.Helpers as Help
916import qualified AST.Module.Name as ModuleName
1017import qualified AST.Variable as Var
11−import qualified Elm.Package as Pkg
1218import qualified Generate.JavaScript.Helpers as JS
1319
1420
@@ -44,7 +50,7 @@ define name body =
4450canonical :: Var.Canonical -> JS.Expression ()
4551canonical (Var.Canonical home name) =
4652 if Help.isOp name then
47− JS.BracketRef () (addRoot home "_op") (JS.string name)
53+ JS.BracketRef () (addRoot home "_op") (JS.StringLit () name)
4854
4955 else
5056 addRoot home (safe name)
@@ -62,12 +68,12 @@ addRoot home name =
6268 Var.BuiltIn ->
6369 JS.ref name
6470
65− Var.Module moduleName ->
66− JS.DotRef () (JS.ref (canonicalModuleName moduleName)) (JS.Id () name)
71+ Var.Module (ModuleName.Canonical _ moduleName) ->
72+ JS.DotRef () (JS.ref (modulePrefix moduleName)) (JS.Id () name)
6773
6874
69−canonicalModuleName :: ModuleName.Canonical -> String
70−canonicalModuleName (ModuleName.Canonical (Pkg.Name _user _project) moduleName) =
75+modulePrefix :: ModuleName.Raw -> String
76+modulePrefix moduleName =
7177 '$' : List.intercalate "$" moduleName
7278
7379
@@ -103,4 +109,5 @@ jsReserveds =
103109 -- reserved by the Elm runtime system
104110 , "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9"
105111 , "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"
112+ , JS.localRuntime
106113 ]