on master help sign in
Merge branch 'remove-multi-if'
commit1f28b4c151d5a280c4c670c00f6e1db56e1614f4 authorEvan Czaplicki <info@elm-lang.org> dateThu 27 Aug 2015, 18:25 (11 years ago) parentc5efca9b17a1a3
10 files changed+52−71
src/AST/Expression/General.hs+19−21 src/Canonicalize.hs+2−2 src/Canonicalize/Sort.hs+2−2 src/Generate/JavaScript.hs+2−2 src/Nitpick/PatternMatches.hs+1−1 src/Optimize/Substitute.hs+2−2 src/Optimize/TailCalls.hs+2−2 src/Parse/Expression.hs+19−36 src/Type/Constrain/Expression.hs+1−1 src/Validate.hs+2−2
src/AST/Expression/General.hs+19−21
@@ -49,7 +49,7 @@ data Expr' ann def var typ
4949 | Binop var (Expr ann def var typ) (Expr ann def var typ)
5050 | Lambda (Pattern.Pattern ann var) (Expr ann def var typ)
5151 | App (Expr ann def var typ) (Expr ann def var typ)
52− | MultiIf [(Expr ann def var typ, Expr ann def var typ)] (Expr ann def var typ)
52+ | If [(Expr ann def var typ, Expr ann def var typ)] (Expr ann def var typ)
5353 | Let [def] (Expr ann def var typ)
5454 | Case (Expr ann def var typ) [(Pattern.Pattern ann var, Expr ann def var typ)]
5555 | Data String [Expr ann def var typ]
@@ -161,27 +161,25 @@ instance (P.Pretty def, P.Pretty var, Var.ToString var) => P.Pretty (Expr' ann d
161161 func:args =
162162 map (P.pretty dealiaser True) (collectApps expr ++ [arg])
163163
164− MultiIf [(condition, thenBranch)] elseBranch ->
165− P.parensIf needsParens $ P.sep $
166− [ P.text "if" <+> P.pretty dealiaser False condition <+> P.text "then"
167− , P.nest 4 (P.pretty dealiaser False thenBranch)
168− , P.text "else"
169− , P.nest 4 (P.pretty dealiaser False elseBranch)
170− ]
164+ If branches finally ->
165+ let
166+ prettyFinally =
167+ P.nest 4 (P.pretty dealiaser False finally)
171168
172− MultiIf branches finally ->
173− P.parensIf needsParens $
174− P.text "if" $$ nest 3 (vcat $ map iff branches ++ [final])
175− where
176− iff (condition, branch) =
177− P.text "|" <+>
178− P.hang
179− (P.pretty dealiaser False condition <+> P.text "->")
180− 2
181− (P.pretty dealiaser False branch)
182−
183− final =
184− P.hang (P.text "| True -> ") 2 (P.pretty dealiaser False finally)
169+ prettyBranch (condition, thenBranch) (firstLine, rest) =
170+ ( P.text "if" <+> P.pretty dealiaser False condition <+> P.text "then"
171+ ,
172+ [ P.nest 4 (P.pretty dealiaser False thenBranch)
173+ , P.text "else" <+> firstLine
174+ ]
175+ ++ rest
176+ )
177+ in
178+ P.parensIf needsParens $ P.sep $ uncurry (:) $
179+ foldr
180+ prettyBranch
181+ ( P.empty, [prettyFinally] )
182+ branches
185183
186184 Let defs body ->
187185 P.parensIf needsParens $
src/Canonicalize.hs+2−2
@@ -416,8 +416,8 @@ expression env (A.A region validExpr) =
416416 App func arg ->
417417 App <$> go func <*> go arg
418418
419− MultiIf branches finally ->
420− MultiIf
419+ If branches finally ->
420+ If
421421 <$> T.traverse go' branches
422422 <*> go finally
423423 where
src/Canonicalize/Sort.hs+2−2
@@ -74,8 +74,8 @@ reorder (A.A ann expression) =
7474 App func arg ->
7575 App <$> reorder func <*> reorder arg
7676
77− MultiIf branches finally ->
78− MultiIf
77+ If branches finally ->
78+ If
7979 <$> mapM (\(cond,branch) -> (,) <$> reorder cond <*> reorder branch) branches
8080 <*> reorder finally
8181
src/Generate/JavaScript.hs+2−2
@@ -202,7 +202,7 @@ exprToCode tcc annotatedExpr@(A.A _ expr) =
202202 Let defs body ->
203203 generateLet tcc defs body
204204
205− MultiIf branches finally ->
205+ If branches finally ->
206206 generateIf tcc branches finally
207207
208208 Case expr branches ->
@@ -410,7 +410,7 @@ crushIfsHelp visitedBranches unvisitedBranches finally =
410410 case unvisitedBranches of
411411 [] ->
412412 case finally of
413− A.A _ (MultiIf subBranches subFinally) ->
413+ A.A _ (If subBranches subFinally) ->
414414 crushIfsHelp visitedBranches subBranches subFinally
415415
416416 _ ->
src/Nitpick/PatternMatches.hs+1−1
@@ -136,7 +136,7 @@ checkExpression tagDict (A.A region expression) =
136136 E.App func arg ->
137137 go2 func arg
138138
139− E.MultiIf branches finally ->
139+ E.If branches finally ->
140140 F.traverse_ (uncurry go2) branches
141141 <* go finally
142142
src/Optimize/Substitute.hs+2−2
@@ -32,8 +32,8 @@ subst old new expression =
3232 App func arg ->
3333 App (f func) (f arg)
3434
35− MultiIf branches finally ->
36− MultiIf (map (f *** f) branches) (f finally)
35+ If branches finally ->
36+ If (map (f *** f) branches) (f finally)
3737
3838 Let defs body ->
3939 if anyShadow
src/Optimize/TailCalls.hs+2−2
@@ -147,12 +147,12 @@ findTailCalls context annExpr@(A.A _ expression) =
147147 Result isTailCall (List.foldl' apply optFunc optArgs)
148148
149149
150− MultiIf branches finally ->
150+ If branches finally ->
151151 let
152152 crawlBranch (cond,branch) =
153153 (,) <$> justConvert cond <*> keepLooking branch
154154 in
155− MultiIf
155+ If
156156 <$> T.traverse crawlBranch branches
157157 <*> keepLooking finally
158158
src/Parse/Expression.hs+19−36
@@ -18,8 +18,6 @@ import qualified AST.Literal as L
1818import qualified AST.Pattern as P
1919import qualified AST.Variable as Var
2020import qualified Reporting.Annotation as A
21−import qualified Reporting.Crash as Crash
22−import qualified Reporting.Region as R
2321
2422
2523-------- Basic Terms --------
@@ -197,8 +195,7 @@ appExpr =
197195
198196expr :: IParser Source.Expr
199197expr =
200− addLocation (choice [ letExpr, caseExpr ])
201− <|> ifExpr
198+ addLocation (choice [ letExpr, caseExpr, ifExpr ])
202199 <|> lambdaExpr
203200 <|> binaryExpr
204201 <?> "an expression"
@@ -209,45 +206,31 @@ binaryExpr =
209206 Binop.binops appExpr lastExpr anyOp
210207 where
211208 lastExpr =
212− addLocation (choice [ letExpr, caseExpr ])
213− <|> ifExpr
209+ addLocation (choice [ letExpr, caseExpr, ifExpr ])
214210 <|> lambdaExpr
215211 <?> "an expression"
216212
217213
218−ifExpr :: IParser Source.Expr
214+ifExpr :: IParser Source.Expr'
219215ifExpr =
220− do start <- getMyPosition
221− try (reserved "if")
222− whitespace
223− makeExpr <- normal <|> multiIf
224− end <- getMyPosition
225− return (makeExpr (R.Region start end))
226− where
227− normal =
228− do condition <- expr
229− padded (reserved "then")
230− thenBranch <- expr
231− whitespace <?> "an 'else' branch"
232− reserved "else" <?> "an 'else' branch"
233− whitespace
234− elseBranch <- expr
235− return $ \region -> A.A region (E.MultiIf [(condition, thenBranch)] elseBranch)
236−
237− multiIf =
238− do branches <- spaceSep1 multiBranch
239− return $ \region -> A.A region (E.MultiIf branches (crash region))
216+ ifHelp []
240217
241− crash region =
242− A.A region (E.Crash (Crash.IncompleteMultiIf region))
243218
244− multiBranch =
245− do string "|"
246− whitespace
247− condition <- expr
248− padded rightArrow
249− branch <- expr
250− return (condition, branch)
219+ifHelp :: [(Source.Expr, Source.Expr)] -> IParser Source.Expr'
220+ifHelp branches =
221+ do try (reserved "if")
222+ whitespace
223+ condition <- expr
224+ padded (reserved "then")
225+ thenBranch <- expr
226+ whitespace <?> "an 'else' branch"
227+ reserved "else" <?> "an 'else' branch"
228+ whitespace
229+ let newBranches = (condition, thenBranch) : branches
230+ choice
231+ [ ifHelp newBranches
232+ , E.If (reverse newBranches) <$> expr
233+ ]
251234
252235
253236lambdaExpr :: IParser Source.Expr
src/Type/Constrain/Expression.hs+1−1
@@ -98,7 +98,7 @@ constrain env annotatedExpr@(A.A region expression) tipe =
9898 in
9999 constrainApp env region f args tipe
100100
101− E.MultiIf branches finally ->
101+ E.If branches finally ->
102102 constrainIf env region branches finally tipe
103103
104104 E.Case expr branches ->
src/Validate.hs+2−2
@@ -213,8 +213,8 @@ expression (A.A ann sourceExpression) =
213213 <$> expression funcExpr
214214 <*> expression argExpr
215215
216− MultiIf branches finally ->
217− MultiIf
216+ If branches finally ->
217+ If
218218 <$> T.traverse both branches
219219 <*> expression finally
220220