1module Generate.JavaScript where
3import Control.Arrow (first,(***))
4import Control.Applicative ((<$>),(<*>))
5import Control.Monad.State
6import qualified Data.List as List
7import qualified Data.Map as Map
8import qualified Data.Set as Set
10import qualified Generate.Cases as Case
11import qualified Generate.Markdown as MD
12import SourceSyntax.Everything
13import SourceSyntax.Location
14import qualified Transform.SortDefinitions as SD
15import Language.ECMAScript3.Syntax
16import Language.ECMAScript3.PrettyPrint
17import Parse.Helpers (jsReserveds)
19makeSafe :: String -> String
20makeSafe = List.intercalate "." . map dereserve . split . deprime
22 deprime = map (\c -> if c == '\'' then '$' else c)
23 dereserve x = case Set.member x jsReserveds of
30 case break (=='.') str of
31 (x,'.':rest) | isOp x -> vars ++ [x ++ '.' : rest]
32 | otherwise -> go (vars ++ [x]) rest
35var name = Id () (makeSafe name)
36ref name = VarRef () (var name)
37prop name = PropId () (var name)
38f <| x = CallExpr () f [x]
39args ==> e = FuncExpr () Nothing (map var args) [ ReturnStmt () (Just e) ]
40function args stmts = FuncExpr () Nothing (map var args) stmts
44dotSep (x:xs) = foldl (DotRef ()) (ref x) (map var xs)
48 VarDecl () (var x) (Just expr)
50include alias moduleName =
51 varDecl alias (obj (moduleName ++ ".make") <| ref "elm")
55 [ varDecl "N" (obj "Elm.Native")
56 , include "_N" "N.Utils"
57 , include "_L" "N.List"
58 , include "_E" "N.Error"
59 , include "_J" "N.JavaScript"
60 , varDecl "$moduleName" (string name)
65 Chr c -> obj "_N.chr" <| string [c]
67 IntNum n -> IntLit () n
68 FloatNum n -> NumLit () n
69 Boolean b -> BoolLit () b
71expression :: LExpr () () -> State Int (Expression ())
72expression (L span expr) =
74 Var x -> return $ ref x
75 Literal lit -> return $ literal lit
78 do lo' <- expression lo
80 return $ obj "_L.range" `call` [lo',hi']
84 return $ DotRef () e' (var x)
88 return $ obj "_N.remove" `call` [string x, e']
93 return $ obj "_N.insert" `call` [string x, v', e']
97 fs' <- forM fs $ \(f,v) -> do
99 return $ ArrayLit () [string f, v']
100 return $ obj "_N.replace" `call` [ArrayLit () fs', e']
103 do fields' <- forM fields $ \(f,e) -> do
104 (,) f <$> expression e
105 let fieldMap = List.foldl' combine Map.empty fields'
106 return $ ObjectLit () $ (PropId () (var "_"), hidden fieldMap) : visible fieldMap
108 combine r (k,v) = Map.insertWith (++) k [v] r
109 prop = PropId () . var
110 hidden fs = ObjectLit () . map (prop *** ArrayLit ()) .
111 Map.toList . Map.filter (not . null) $ Map.map tail fs
112 visible fs = map (first prop) . Map.toList $ Map.map head fs
114 Binop op e1 e2 -> binop span op e1 e2
116 Lambda p e@(L s _) ->
117 do body' <- expression body
118 return $ case length args < 2 || length args > 9 of
119 True -> foldr (==>) body' (map (:[]) args)
120 False -> ref ("F" ++ show (length args)) <| (args ==> body')
122 (args, body) = foldr depattern ([], innerBody) (zip patterns [0..])
124 depattern (pattern,n) (args, body) =
126 PVar x -> (args ++ [x], body)
127 _ -> let arg = "arg" ++ show n
128 in (args ++ [arg], L s (Case (L s (Var arg)) [(pattern, body)]))
130 (patterns, innerBody) = collect [p] e
132 collect patterns lexpr@(L _ expr) =
134 Lambda p e -> collect (p:patterns) e
135 _ -> (patterns, lexpr)
138 do func' <- expression func
139 args' <- mapM expression args
140 return $ case args' of
141 [arg] -> func' <| arg
142 _ | length args' <= 9 -> ref aN `call` (func':args')
143 | otherwise -> foldl1 (<|) (func':args')
145 aN = "A" ++ show (length args)
146 (func, args) = getArgs e1 [e2]
149 (L _ (App f arg)) -> getArgs f (arg : args)
153 do let (defs',e') = SD.flattenLets defs e
154 stmts <- concat <$> mapM definition defs'
156 return $ function [] (stmts ++ [ ReturnStmt () (Just exp) ]) `call` []
159 do branches' <- forM branches $ \(b,e) -> (,) <$> expression b <*> expression e
160 return $ case last branches of
161 (L _ (Var "Basics.otherwise"), e) -> ifs (init branches') (snd (last branches'))
163 (obj "_E.If" `call` [ ref "$moduleName", string (show span) ])
165 ifs branches finally = foldr iff finally branches
166 iff (if', then') else' = CondExpr () if' then' else'
169 do (tempVar,initialMatch) <- Case.toMatch cases
170 (revisedMatch, stmt) <-
172 L _ (Var x) -> return (Case.matchSubst [(tempVar,x)] initialMatch, [])
173 _ -> do e' <- expression e
174 return (initialMatch, [VarDeclStmt () [varDecl tempVar e']])
175 match' <- match span revisedMatch
176 return (function [] (stmt ++ match') `call` [])
179 do es' <- mapM expression es
180 return $ obj "_J.toList" <| ArrayLit () es'
183 do es' <- mapM expression es
184 return $ ObjectLit () (ctor : fields es')
186 ctor = (prop "ctor", string (makeSafe name))
187 fields = zipWith (\n e -> (prop ("_" ++ show n), e)) [0..]
189 Markdown md _ -> return $ obj "Text.text" <| string (pad ++ MD.toHtml md ++ pad)
190 where pad = "<div style=\"height:0;width:0;\"> </div>"
192definition :: Def () () -> State Int [Statement ()]
195 TypeAnnotation _ _ -> return []
197 Def pattern expr@(L span _) -> do
198 expr' <- expression expr
199 let assign x = varDecl x expr'
203 let op = LBracket () (ref "_op") (string x) in
204 return [ ExprStmt () $ AssignExpr () OpAssign op expr' ]
206 return [ VarDeclStmt () [ assign x ] ]
209 let setField f = varDecl f (dotSep ["$",f]) in
210 return [ VarDeclStmt () (assign "$" : map setField fields) ]
212 PData name patterns | vars /= Nothing ->
214 Just vs -> return [ VarDeclStmt () (setup (zipWith decl vs [0..])) ]
216 vars = getVars patterns
219 PVar x : rest -> (x:) `fmap` getVars rest
223 decl x n = varDecl x (dotSep ["$","_" ++ show n])
225 | isTuple name = assign "$" : vars
226 | otherwise = safeAssign : vars
228 safeAssign = varDecl "$" (CondExpr () if' expr' exception)
229 if' = InfixExpr () OpStrictEq (obj "$.ctor") (string name)
230 exception = obj "_E.Case" `call` [ref "$moduleName", string (show span)]
233 do defs' <- concat <$> mapM toDef vars
234 return (VarDeclStmt () [assign "$"] : defs')
236 vars = Set.toList $ SD.boundVars pattern
238 toDef y = definition $
239 Def (PVar y) (L span $ Case (mkVar "$") [(pattern, mkVar y)])
241match :: (Show a) => a -> Case.Match () () -> State Int [Statement ()]
244 Case.Match name clauses mtch' ->
245 do (isChars, clauses') <- unzip <$> mapM (clause span name) clauses
246 mtch'' <- match span mtch'
247 return (SwitchStmt () (format isChars (access name)) clauses' : mtch'')
249 isLiteral p = case p of
250 Case.Clause (Right _) _ _ -> True
252 access name = if any isLiteral clauses then ref name else dotSep [name,"ctor"]
254 | or isChars = InfixExpr () OpAdd e (string "")
258 return [ ExprStmt () (obj "_E.Case" `call` [ref "$moduleName", string (show span)]) ]
260 Case.Break -> return [BreakStmt () Nothing]
262 do e' <- expression e
263 return [ ReturnStmt () (Just e') ]
264 Case.Seq ms -> concat <$> mapM (match span) (dropEnd [] ms)
269 Case.Other _ -> acc ++ [m]
270 _ -> dropEnd (acc ++ [m]) ms
272clause span variable (Case.Clause value vars mtch) =
273 (,) isChar . CaseClause () pattern <$> match span (Case.matchSubst (zip vars vars') mtch)
275 vars' = map (\n -> variable ++ "._" ++ show n) [0..]
278 Right (Chr c) -> (True, string [c])
279 _ -> (,) False $ case value of
280 Right (Boolean b) -> BoolLit () b
281 Right lit -> literal lit
282 Left name -> string $ case List.elemIndices '.' name of
284 is -> drop (last is + 1) name
287jsModule :: MetadataModule () () -> String
289 show . prettyPrint $ setup (Just "Elm") (names modul ++ ["make"]) ++
290 [ assign ("Elm" : names modul ++ ["make"]) (function ["elm"] programStmts) ]
292 thisModule = dotSep ("elm" : names modul ++ ["values"])
294 concat [ setup (Just "elm") (names modul ++ ["values"])
295 , [ IfSingleStmt () thisModule (ReturnStmt () (Just thisModule)) ]
296 , [ internalImports (List.intercalate "." (names modul)) ]
297 , concatMap jsImport (imports modul)
298 , concatMap importEvent (foreignImports modul)
299 , [ assign ["_op"] (ObjectLit () []) ]
300 , concat $ evalState (mapM definition . fst . SD.flattenLets [] $ program modul) 0
301 , map exportEvent $ foreignExports modul
303 , [ ReturnStmt () (Just thisModule) ]
306 jsExports = assign ("elm" : names modul ++ ["values"]) (ObjectLit () exs)
308 exs = map entry . filter (not . isOp) $ "_op" : exports modul
309 entry x = (PropId () (var x), ref x)
313 [x] -> VarDeclStmt () [ varDecl x expr ]
315 AssignExpr () OpAssign (LDot () (dotSep (init path)) (last path)) expr
317 jsImport (modul,_) = setup Nothing path ++ [ include ]
320 include = assign path $ dotSep ("Elm" : path ++ ["make"]) <| ref "elm"
322 setup namespace path = map create paths
324 create name = assign name (InfixExpr () OpLOr (dotSep name) (ObjectLit () []))
325 paths = case namespace of
326 Nothing -> tail . init $ List.inits path
327 Just nmspc -> drop 2 . init . List.inits $ nmspc : path
329 addId js = InfixExpr () OpAdd (string (js++"_")) (obj "elm.id")
331 importEvent (js,base,elm,_) =
332 [ VarDeclStmt () [ varDecl elm $ obj "Signal.constant" <| evalState (expression base) 0 ]
334 obj "document.addEventListener" `call`
337 [ ExprStmt () $ obj "elm.notify" `call` [dotSep [elm,"id"], obj "e.value"] ]
341 exportEvent (js,elm,_) =
346 [ VarDeclStmt () [varDecl "e" $ obj "document.createEvent" <| string "Event"]
348 obj "e.initEvent" `call` [ addId js, BoolLit () True, BoolLit () True ]
349 , ExprStmt () $ AssignExpr () OpAssign (LDot () (ref "e") "value") (ref "v")
350 , ExprStmt () $ obj "document.dispatchEvent" <| ref "e"
351 , ReturnStmt () (Just $ ref "v")
358 do es <- mapM expression (e1 : collect [] e2)
359 return $ ["$"] ==> foldr (<|) (ref "$") es
361 do e2' <- expression e2
362 es <- mapM expression (collect [] e1)
363 return $ foldr (<|) e2' es
365 do e1' <- expression e1
367 return $ obj "_L.append" `call` [e1', e2']
368 "::" -> expression (L span (Data "::" [e1,e2]))
370 do e1' <- expression e1
372 return $ case Map.lookup op opDict of
374 Nothing -> ref "A2" `call` [ func, e1', e2' ]
378 L _ (Binop op e1 e2) | op == "Basics.." -> collect (es ++ [e1]) e2
384 func | isOp operator = BracketRef () (dotSep (init parts ++ ["_op"])) (string operator)
385 | otherwise = dotSep parts
388 operator = last parts
390 opDict = Map.fromList (infixOps ++ specialOps)
392 specialOp str func = [ (str, func), ("Basics." ++ str, func) ]
393 infixOp str op = specialOp str (InfixExpr () op)
400 , infixOp "&&" OpLAnd
405 [ specialOp "^" $ \a b -> obj "Math.pow" `call` [a,b]
406 , specialOp "|>" $ flip (<|)
407 , specialOp "==" $ \a b -> obj "_N.eq" `call` [a,b]
408 , specialOp "/=" $ \a b -> PrefixExpr () PrefixLNot (obj "_N.eq" `call` [a,b])
409 , specialOp "<" $ cmp OpLT 0
410 , specialOp ">" $ cmp OpGT 0
411 , specialOp "<=" $ cmp OpLT 1
412 , specialOp ">=" $ cmp OpGT (-1)
413 , specialOp "div" $ \a b -> InfixExpr () OpBOr (InfixExpr () OpDiv a b) (IntLit () 0)
416 cmp op n a b = InfixExpr () op (obj "_N.cmp" `call` [a,b]) (IntLit () n)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list