on master help sign in
Cosmetic: import the prefixed version of `Generate.Cases`
ea38376Evan Czaplicki
compiler/Generate/Cases.hs+3−3
@@ -1,4 +1,4 @@
1−module Generate.Cases (caseToMatch, Match (..), Clause (..), matchSubst) where
1+module Generate.Cases (toMatch, Match (..), Clause (..), matchSubst) where
2
2
3import Control.Applicative ((<$>),(<*>))
3import Control.Applicative ((<$>),(<*>))
4import Control.Arrow (first,second)
4import Control.Arrow (first,second)
@@ -13,8 +13,8 @@ import SourceSyntax.Expression
13import Transform.Substitute
13import Transform.Substitute
14
14
15
15
16−caseToMatch :: [(Pattern, LExpr t v)] -> State Int (String, Match t v)
16+toMatch :: [(Pattern, LExpr t v)] -> State Int (String, Match t v)
17−caseToMatch patterns = do
17+toMatch patterns = do
18 v <- newVar
18 v <- newVar
19 (,) v <$> match [v] (map (first (:[])) patterns) Fail
19 (,) v <$> match [v] (map (first (:[])) patterns) Fail
20
20
compiler/Generate/JavaScript.hs+15−12
@@ -8,7 +8,7 @@ import qualified Data.Map as Map
8import qualified Data.Set as Set
8import qualified Data.Set as Set
9import qualified Text.Pandoc as Pan
9import qualified Text.Pandoc as Pan
10
10
11−import Generate.Cases
11+import qualified Generate.Cases as Case
12import SourceSyntax.Everything
12import SourceSyntax.Everything
13import SourceSyntax.Location
13import SourceSyntax.Location
14import qualified Transform.SortDefinitions as SD
14import qualified Transform.SortDefinitions as SD
@@ -166,10 +166,10 @@ expression (L span expr) =
166 iff (if', then') else' = CondExpr () if' then' else'
166 iff (if', then') else' = CondExpr () if' then' else'
167
167
168 Case e cases ->
168 Case e cases ->
169− do (tempVar,initialMatch) <- caseToMatch cases
169+ do (tempVar,initialMatch) <- Case.toMatch cases
170 (revisedMatch, stmt) <-
170 (revisedMatch, stmt) <-
171 case e of
171 case e of
172− L _ (Var x) -> return (matchSubst [(tempVar,x)] initialMatch, [])
172+ L _ (Var x) -> return (Case.matchSubst [(tempVar,x)] initialMatch, [])
173 _ -> do e' <- expression e
173 _ -> do e' <- expression e
174 return (initialMatch, [VarDeclStmt () [varDecl tempVar e']])
174 return (initialMatch, [VarDeclStmt () [varDecl tempVar e']])
175 match' <- match span revisedMatch
175 match' <- match span revisedMatch
@@ -239,33 +239,36 @@ definition def =
239 toDef y = definition $
239 toDef y = definition $
240 Def (PVar y) (L span $ Case (mkVar "$") [(pattern, mkVar y)])
240 Def (PVar y) (L span $ Case (mkVar "$") [(pattern, mkVar y)])
241
241
242+match :: (Show a) => a -> Case.Match () () -> State Int [Statement ()]
242match span mtch =
243match span mtch =
243 case mtch of
244 case mtch of
244− Match name clauses mtch' ->
245+ Case.Match name clauses mtch' ->
245 do clauses' <- mapM (clause span name) clauses
246 do clauses' <- mapM (clause span name) clauses
246 mtch'' <- match span mtch'
247 mtch'' <- match span mtch'
247 return (SwitchStmt () (access name) clauses' : mtch'')
248 return (SwitchStmt () (access name) clauses' : mtch'')
248 where
249 where
249 isLiteral p = case p of
250 isLiteral p = case p of
250− Clause (Right _) _ _ -> True
251+ Case.Clause (Right _) _ _ -> True
251 _ -> False
252 _ -> False
252 access name = if any isLiteral clauses then ref name else dotSep [name,"ctor"]
253 access name = if any isLiteral clauses then ref name else dotSep [name,"ctor"]
253
254
255+ Case.Fail ->
254− Fail -> return [ ExprStmt () (obj "_E.Case" `call` [ref "$moduleName", string (show span)]) ]
256+ return [ ExprStmt () (obj "_E.Case" `call` [ref "$moduleName", string (show span)]) ]
257+
255− Break -> return []
258+ Case.Break -> return []
256− Other e ->
259+ Case.Other e ->
257 do e' <- expression e
260 do e' <- expression e
258 return [ ReturnStmt () (Just e') ]
261 return [ ReturnStmt () (Just e') ]
259− Seq ms -> concat <$> mapM (match span) (dropEnd [] ms)
262+ Case.Seq ms -> concat <$> mapM (match span) (dropEnd [] ms)
260 where
263 where
261 dropEnd acc [] = acc
264 dropEnd acc [] = acc
262 dropEnd acc (m:ms) =
265 dropEnd acc (m:ms) =
263 case m of
266 case m of
264− Other _ -> acc ++ [m]
267+ Case.Other _ -> acc ++ [m]
265 _ -> dropEnd (acc ++ [m]) ms
268 _ -> dropEnd (acc ++ [m]) ms
266
269
267−clause span variable (Clause value vars mtch) =
270+clause span variable (Case.Clause value vars mtch) =
268− CaseClause () pattern <$> match span (matchSubst (zip vars vars') mtch)
271+ CaseClause () pattern <$> match span (Case.matchSubst (zip vars vars') mtch)
269 where
272 where
270 vars' = map (\n -> variable ++ "._" ++ show n) [0..]
273 vars' = map (\n -> variable ++ "._" ++ show n) [0..]
271 pattern = case value of
274 pattern = case value of