2module Generate.JavaScript (generate) where
4import Control.Arrow (first,(***))
5import Control.Applicative ((<$>),(<*>))
6import Control.Monad.State
7import qualified Data.List as List
8import qualified Data.Map as Map
9import qualified Data.Set as Set
11import qualified Generate.Cases as Case
12import qualified Generate.Markdown as MD
13import qualified SourceSyntax.Helpers as Help
14import SourceSyntax.Literal
15import SourceSyntax.Pattern as Pattern
16import SourceSyntax.Location
17import SourceSyntax.Expression
18import SourceSyntax.Module
19import Language.ECMAScript3.Syntax
20import Language.ECMAScript3.PrettyPrint
21import qualified Transform.SafeNames as MakeSafe
23split :: String -> [String]
27 case break (=='.') str of
28 (x,_:rest) | Help.isOp x -> vars ++ [x ++ '.' : rest]
29 | otherwise -> go (vars ++ [x]) rest
33ref name = VarRef () (var name)
34prop name = PropId () (var name)
35f <| x = CallExpr () f [x]
36args ==> e = FuncExpr () Nothing (map var args) [ ReturnStmt () (Just e) ]
37function args stmts = FuncExpr () Nothing (map var args) stmts
43 x:xs -> foldl (DotRef ()) (ref x) (map var xs)
44 [] -> error "dotSep must be called on a non-empty list of variables"
48varDecl :: String -> Expression () -> VarDecl ()
50 VarDecl () (var x) (Just expr)
52include alias moduleName =
53 varDecl alias (obj (moduleName ++ ".make") <| ref "_elm")
57 [ varDecl "_N" (obj "Elm.Native")
58 , include "_U" "_N.Utils"
59 , include "_L" "_N.List"
60 , include "_E" "_N.Error"
61 , include "_J" "_N.JavaScript"
62 , varDecl "$moduleName" (string name)
65literal :: Literal -> Expression ()
68 Chr c -> obj "_U.chr" <| string [c]
70 IntNum n -> IntLit () n
71 FloatNum n -> NumLit () n
72 Boolean b -> BoolLit () b
74expression :: LExpr -> State Int (Expression ())
75expression (L span expr) =
77 Var x -> return $ ref x
78 Literal lit -> return $ literal lit
81 do lo' <- expression lo
83 return $ obj "_L.range" `call` [lo',hi']
87 return $ DotRef () e' (var x)
91 return $ obj "_U.remove" `call` [string x, e']
96 return $ obj "_U.insert" `call` [string x, v', e']
100 fs' <- forM fs $ \(f,v) -> do
102 return $ ArrayLit () [string f, v']
103 return $ obj "_U.replace" `call` [ArrayLit () fs', e']
106 do fields' <- forM fields $ \(f,e) -> do
107 (,) f <$> expression e
108 let fieldMap = List.foldl' combine Map.empty fields'
109 return $ ObjectLit () $ (PropId () (var "_"), hidden fieldMap) : visible fieldMap
111 combine r (k,v) = Map.insertWith (++) k [v] r
112 prop = PropId () . var
113 hidden fs = ObjectLit () . map (prop *** ArrayLit ()) .
114 Map.toList . Map.filter (not . null) $ Map.map tail fs
115 visible fs = map (first prop) . Map.toList $ Map.map head fs
117 Binop op e1 e2 -> binop span op e1 e2
119 Lambda p e@(L s _) ->
120 do (args, body) <- foldM depattern ([], innerBody) (reverse patterns)
121 body' <- expression body
122 return $ case length args < 2 || length args > 9 of
123 True -> foldr (==>) body' (map (:[]) args)
124 False -> ref ("F" ++ show (length args)) <| (args ==> body')
126 depattern (args, body) pattern =
128 PVar x -> return (args ++ [x], body)
129 _ -> do arg <- Case.newVar
130 return (args ++ [arg], L s (Case (L s (Var arg)) [(pattern, body)]))
132 (patterns, innerBody) = collect [p] e
134 collect patterns lexpr@(L _ expr) =
136 Lambda p e -> collect (p:patterns) e
137 _ -> (patterns, lexpr)
140 do func' <- expression func
141 args' <- mapM expression args
142 return $ case args' of
143 [arg] -> func' <| arg
144 _ | length args' <= 9 -> ref aN `call` (func':args')
145 | otherwise -> foldl1 (<|) (func':args')
147 aN = "A" ++ show (length args)
148 (func, args) = getArgs e1 [e2]
151 (L _ (App f arg)) -> getArgs f (arg : args)
155 do let (defs',e') = flattenLets defs e
156 stmts <- concat <$> mapM definition defs'
158 return $ function [] (stmts ++ [ ReturnStmt () (Just exp) ]) `call` []
161 do branches' <- forM branches $ \(b,e) -> (,) <$> expression b <*> expression e
162 return $ case last branches of
163 (L _ (Var "Basics.otherwise"), _) -> safeIfs branches'
164 (L _ (Literal (Boolean True)), _) -> safeIfs branches'
165 _ -> ifs branches' (obj "_E.If" `call` [ ref "$moduleName", string (show span) ])
167 safeIfs branches = ifs (init branches) (snd (last branches))
168 ifs branches finally = foldr iff finally branches
169 iff (if', then') else' = CondExpr () if' then' else'
172 do (tempVar,initialMatch) <- Case.toMatch cases
173 (revisedMatch, stmt) <-
175 L _ (Var x) -> return (Case.matchSubst [(tempVar,x)] initialMatch, [])
176 _ -> do e' <- expression e
177 return (initialMatch, [VarDeclStmt () [varDecl tempVar e']])
178 match' <- match span revisedMatch
179 return (function [] (stmt ++ match') `call` [])
182 do es' <- mapM expression es
183 return $ obj "_J.toList" <| ArrayLit () es'
186 do es' <- mapM expression es
187 return $ ObjectLit () (ctor : fields es')
189 ctor = (prop "ctor", string name)
190 fields = zipWith (\n e -> (prop ("_" ++ show n), e)) [0..]
192 Markdown uid doc es ->
193 do es' <- mapM expression es
194 return $ obj "Text.markdown" `call` (string md : string uid : es')
196 pad = "<div style=\"height:0;width:0;\"> </div>"
197 md = pad ++ MD.toHtml doc ++ pad
199 GLShader _uid src _tipe ->
200 return . literal $ Str src
202 PortIn name _ _ handler ->
203 do handler' <- expression handler
204 return $ obj "Native.Ports.portIn" `call` [ string name, handler' ]
206 PortOut name _ signal ->
207 do signal' <- expression signal
208 return $ obj "Native.Ports.portOut" `call` [ string name, signal' ]
210definition :: Def -> State Int [Statement ()]
211definition (Definition pattern expr@(L span _) _) = do
212 expr' <- expression expr
213 let assign x = varDecl x expr'
217 let op = LBracket () (ref "_op") (string x) in
218 return [ ExprStmt () $ AssignExpr () OpAssign op expr' ]
220 return [ VarDeclStmt () [ assign x ] ]
223 let setField f = varDecl f (dotSep ["$",f]) in
224 return [ VarDeclStmt () (assign "$" : map setField fields) ]
226 PData name patterns | vars /= Nothing ->
227 return [ VarDeclStmt () (setup (zipWith decl (maybe [] id vars) [0..])) ]
229 vars = getVars patterns
232 PVar x : rest -> (x:) `fmap` getVars rest
236 decl x n = varDecl x (dotSep ["$","_" ++ show n])
238 | Help.isTuple name = assign "$" : vars
239 | otherwise = assign "$raw" : safeAssign : vars
241 safeAssign = varDecl "$" (CondExpr () if' (obj "$raw") exception)
242 if' = InfixExpr () OpStrictEq (obj "$raw.ctor") (string name)
243 exception = obj "_E.Case" `call` [ref "$moduleName", string (show span)]
246 do defs' <- concat <$> mapM toDef vars
247 return (VarDeclStmt () [assign "$"] : defs')
249 vars = Set.toList $ Pattern.boundVars pattern
251 toDef y = let expr = L span $ Case (mkVar "$") [(pattern, mkVar y)]
252 in definition $ Definition (PVar y) expr Nothing
254match :: (Show a) => a -> Case.Match -> State Int [Statement ()]
257 Case.Match name clauses mtch' ->
258 do (isChars, clauses') <- unzip <$> mapM (clause span name) clauses
259 mtch'' <- match span mtch'
260 return (SwitchStmt () (format isChars (access name)) clauses' : mtch'')
262 isLiteral p = case p of
263 Case.Clause (Right _) _ _ -> True
265 access name = if any isLiteral clauses then ref name else dotSep [name,"ctor"]
267 | or isChars = InfixExpr () OpAdd e (string "")
271 return [ ExprStmt () (obj "_E.Case" `call` [ref "$moduleName", string (show span)]) ]
273 Case.Break -> return [BreakStmt () Nothing]
275 do e' <- expression e
276 return [ ReturnStmt () (Just e') ]
277 Case.Seq ms -> concat <$> mapM (match span) (dropEnd [] ms)
282 Case.Other _ -> acc ++ [m]
283 _ -> dropEnd (acc ++ [m]) ms
285clause span variable (Case.Clause value vars mtch) =
286 (,) isChar . CaseClause () pattern <$> match span (Case.matchSubst (zip vars vars') mtch)
288 vars' = map (\n -> variable ++ "._" ++ show n) [0..]
291 Right (Chr c) -> (True, string [c])
292 _ -> (,) False $ case value of
293 Right (Boolean b) -> BoolLit () b
294 Right lit -> literal lit
295 Left name -> string $ case List.elemIndices '.' name of
297 is -> drop (last is + 1) name
299flattenLets defs lexpr@(L _ expr) =
301 Let ds body -> flattenLets (defs ++ ds) body
304generate :: MetadataModule -> String
305generate unsafeModule =
306 show . prettyPrint $ setup (Just "Elm") (names modul ++ ["make"]) ++
307 [ assign ("Elm" : names modul ++ ["make"]) (function ["_elm"] programStmts) ]
309 modul = MakeSafe.metadataModule unsafeModule
310 thisModule = dotSep ("_elm" : names modul ++ ["values"])
313 [ setup (Just "_elm") (names modul ++ ["values"])
314 , [ IfSingleStmt () thisModule (ReturnStmt () (Just thisModule)) ]
315 , [ internalImports (List.intercalate "." (names modul)) ]
316 , concatMap jsImport . Set.toList . Set.fromList . map fst $ imports modul
317 , [ assign ["_op"] (ObjectLit () []) ]
318 , concat $ evalState (mapM definition . fst . flattenLets [] $ program modul) 0
320 , [ ReturnStmt () (Just thisModule) ]
323 jsExports = assign ("_elm" : names modul ++ ["values"]) (ObjectLit () exs)
325 exs = map entry . filter (not . Help.isOp) $ "_op" : exports modul
326 entry x = (PropId () (var x), ref x)
330 [x] -> VarDeclStmt () [ varDecl x expr ]
332 AssignExpr () OpAssign (LDot () (dotSep (init path)) (last path)) expr
334 jsImport modul = setup Nothing path ++ [ include ]
337 include = assign path $ dotSep ("Elm" : path ++ ["make"]) <| ref "_elm"
339 setup namespace path = map create paths
341 create name = assign name (InfixExpr () OpLOr (dotSep name) (ObjectLit () []))
342 paths = case namespace of
343 Nothing -> tail . init $ List.inits path
344 Just nmspc -> drop 2 . init . List.inits $ nmspc : path
349 do es <- mapM expression (e1 : collect [] e2)
350 return $ ["$"] ==> foldr (<|) (ref "$") es
352 do e2' <- expression e2
353 es <- mapM expression (collect [] e1)
354 return $ foldr (<|) e2' es
356 do e1' <- expression e1
358 return $ obj "_L.append" `call` [e1', e2']
359 "::" -> expression (L span (Data "::" [e1,e2]))
361 do e1' <- expression e1
363 return $ case Map.lookup op opDict of
365 Nothing -> ref "A2" `call` [ func, e1', e2' ]
369 L _ (Binop op e1 e2) | op == "Basics.." -> collect (es ++ [e1]) e2
372 func | Help.isOp operator = BracketRef () (dotSep (init parts ++ ["_op"])) (string operator)
373 | otherwise = dotSep parts
376 operator = last parts
378 opDict = Map.fromList (infixOps ++ specialOps)
380 specialOp str func = [ (str, func), ("Basics." ++ str, func) ]
381 infixOp str op = specialOp str (InfixExpr () op)
388 , infixOp "&&" OpLAnd
393 [ specialOp "^" $ \a b -> obj "Math.pow" `call` [a,b]
394 , specialOp "|>" $ flip (<|)
395 , specialOp "==" $ \a b -> obj "_U.eq" `call` [a,b]
396 , specialOp "/=" $ \a b -> PrefixExpr () PrefixLNot (obj "_U.eq" `call` [a,b])
397 , specialOp "<" $ cmp OpLT 0
398 , specialOp ">" $ cmp OpGT 0
399 , specialOp "<=" $ cmp OpLT 1
400 , specialOp ">=" $ cmp OpGT (-1)
401 , specialOp "div" $ \a b -> InfixExpr () OpBOr (InfixExpr () OpDiv a b) (IntLit () 0)
404 cmp op n a b = InfixExpr () op (obj "_U.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