on master help sign in
Merge branch 'master' of https://github.com/elm-lang/elm-compiler into avoidNameConflicts
commiteb1f9d6159b9615680fea569beb1f859d05b22ce authorJoeyEremondi <joey@eremondi.com> dateThu 27 Aug 2015, 19:49 (11 years ago) parentcadeba61f28b4c
12 files changed+98−257
src/AST/Expression/General.hs+21−48 src/Canonicalize.hs+4−10 src/Canonicalize/Declaration.hs+5−13 src/Canonicalize/Sort.hs+4−15 src/Generate/JavaScript.hs+3−12 src/Nitpick/PatternMatches.hs+2−8 src/Optimize/Substitute.hs+4−10 src/Optimize/TailCalls.hs+4−15 src/Parse/Expression.hs+44−86 src/Parse/Helpers.hs+1−6 src/Type/Constrain/Expression.hs+2−19 src/Validate.hs+4−15
src/AST/Expression/General.hs+21−48
@@ -49,14 +49,12 @@ 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]
5656 | Access (Expr ann def var typ) String
57− | Remove (Expr ann def var typ) String
58− | Insert (Expr ann def var typ) String (Expr ann def var typ)
59− | Modify (Expr ann def var typ) [(String, Expr ann def var typ)]
57+ | Update (Expr ann def var typ) [(String, Expr ann def var typ)]
6058 | Record [(String, Expr ann def var typ)]
6159 -- for type checking and code gen only
6260 | Port (PortImpl (Expr ann def var typ) typ)
@@ -163,27 +161,25 @@ instance (P.Pretty def, P.Pretty var, Var.ToString var) => P.Pretty (Expr' ann d
163161 func:args =
164162 map (P.pretty dealiaser True) (collectApps expr ++ [arg])
165163
166− MultiIf [(condition, thenBranch)] elseBranch ->
167− P.parensIf needsParens $ P.sep $
168− [ P.text "if" <+> P.pretty dealiaser False condition <+> P.text "then"
169− , P.nest 4 (P.pretty dealiaser False thenBranch)
170− , P.text "else"
171− , P.nest 4 (P.pretty dealiaser False elseBranch)
172− ]
164+ If branches finally ->
165+ let
166+ prettyFinally =
167+ P.nest 4 (P.pretty dealiaser False finally)
173168
174− MultiIf branches finally ->
175− P.parensIf needsParens $
176− P.text "if" $$ nest 3 (vcat $ map iff branches ++ [final])
177− where
178− iff (condition, branch) =
179− P.text "|" <+>
180− P.hang
181− (P.pretty dealiaser False condition <+> P.text "->")
182− 2
183− (P.pretty dealiaser False branch)
184−
185− final =
186− 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
187183
188184 Let defs body ->
189185 P.parensIf needsParens $
@@ -228,30 +224,7 @@ instance (P.Pretty def, P.Pretty var, Var.ToString var) => P.Pretty (Expr' ann d
228224 Access record field ->
229225 P.pretty dealiaser True record <> P.text "." <> P.text field
230226
231− Remove record field ->
232− P.braces (P.pretty dealiaser False record <+> P.text "-" <+> P.text field)
233−
234− Insert (A.A _ (Remove record oldField)) newField expr ->
235− P.braces $
236− P.hsep
237− [ P.pretty dealiaser False record
238− , P.text "-"
239− , P.text oldField
240− , P.text "|"
241− , P.text newField
242− , P.equals
243− , P.pretty dealiaser False expr
244− ]
245−
246− Insert record field expr ->
247− P.braces $
248− P.pretty dealiaser False record
249− <+> P.text "|"
250− <+> P.text field
251− <+> P.equals
252− <+> P.pretty dealiaser False expr
253−
254− Modify record fields ->
227+ Update record fields ->
255228 P.braces $
256229 P.hang
257230 (P.pretty dealiaser False record <+> P.text "|")
src/Canonicalize.hs+4−10
@@ -393,14 +393,8 @@ expression env (A.A region validExpr) =
393393 Access record field ->
394394 Access <$> go record <*> Result.ok field
395395
396− Remove record field ->
397− flip Remove field <$> go record
398−
399− Insert record field expr ->
400− flip Insert field <$> go record <*> go expr
401−
402− Modify record fields ->
403− Modify
396+ Update record fields ->
397+ Update
404398 <$> go record
405399 <*> T.traverse (\(field,expr) -> (,) field <$> go expr) fields
406400
@@ -422,8 +416,8 @@ expression env (A.A region validExpr) =
422416 App func arg ->
423417 App <$> go func <*> go arg
424418
425− MultiIf branches finally ->
426− MultiIf
419+ If branches finally ->
420+ If
427421 <$> T.traverse go' branches
428422 <*> go finally
429423 where
src/Canonicalize/Declaration.hs+5−13
@@ -38,28 +38,20 @@ toDefs moduleName (A.A (region,_) decl) =
3838 in
3939 [ definition ctor (buildFunction body vars) region (foldr T.Lambda tbody tipes) ]
4040
41− D.TypeAlias name tvars tipe@(T.Record fields ext) ->
41+ D.TypeAlias name tvars tipe@(T.Record fields Nothing) ->
4242 [ definition name (buildFunction record vars) region (foldr T.Lambda result args) ]
4343 where
4444 result =
4545 T.Aliased (typeVar name) (zip tvars (map T.Var tvars)) (T.Holey tipe)
4646
4747 args =
48− map snd fields ++ maybe [] (:[]) ext
48+ map snd fields
4949
50− var = loc . E.localVar
51− vars = take (length args) infiniteArgs
52−
53− efields =
54− zip (map fst fields) (map var vars)
50+ vars =
51+ take (length args) infiniteArgs
5552
5653 record =
57− case ext of
58− Nothing ->
59− loc (E.Record efields)
60−
61− Just _ ->
62− foldl (\r (f,v) -> loc (E.Insert r f v)) (var (last vars)) efields
54+ loc (E.Record (zip (map fst fields) (map (loc . E.localVar) vars)))
6355
6456 -- Type aliases must be added to an extended equality dictionary,
6557 -- but they do not require any basic constraints.
src/Canonicalize/Sort.hs+4−15
@@ -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
@@ -84,19 +84,8 @@ reorder (A.A ann expression) =
8484 <$> reorder record
8585 <*> return field
8686
87− Remove record field ->
88− Remove
89− <$> reorder record
90− <*> return field
91−
92− Insert record field expr ->
93− Insert
94− <$> reorder record
95− <*> return field
96− <*> reorder expr
97−
98− Modify record fields ->
99− Modify
87+ Update record fields ->
88+ Update
10089 <$> reorder record
10190 <*> mapM (\(field,expr) -> (,) field <$> reorder expr) fields
10291
src/Generate/JavaScript.hs+3−12
@@ -161,16 +161,7 @@ exprToCode tcc annotatedExpr@(A.A _ expr) =
161161 do record' <- toJsExpr record
162162 jsExpr $ DotRef () record' (var (Var.varName field))
163163
164− Remove record field ->
165− do record' <- toJsExpr record
166− jsExpr $ _Utils "remove" `call` [ string (Var.varName field), record' ]
167−
168− Insert record field value ->
169− do value' <- toJsExpr value
170− record' <- toJsExpr record
171− jsExpr $ _Utils "insert" `call` [ string (Var.varName field), value', record' ]
172−
173− Modify record fields ->
164+ Update record fields ->
174165 do record' <- toJsExpr record
175166 fields' <-
176167 forM fields $ \(field, value) ->
@@ -211,7 +202,7 @@ exprToCode tcc annotatedExpr@(A.A _ expr) =
211202 Let defs body ->
212203 generateLet tcc defs body
213204
214− MultiIf branches finally ->
205+ If branches finally ->
215206 generateIf tcc branches finally
216207
217208 Case expr branches ->
@@ -419,7 +410,7 @@ crushIfsHelp visitedBranches unvisitedBranches finally =
419410 case unvisitedBranches of
420411 [] ->
421412 case finally of
422− A.A _ (MultiIf subBranches subFinally) ->
413+ A.A _ (If subBranches subFinally) ->
423414 crushIfsHelp visitedBranches subBranches subFinally
424415
425416 _ ->
src/Nitpick/PatternMatches.hs+2−8
@@ -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
@@ -159,13 +159,7 @@ checkExpression tagDict (A.A region expression) =
159159 E.Access record _field ->
160160 go record
161161
162− E.Remove record _field ->
163− go record
164−
165− E.Insert record _field value ->
166− go2 record value
167−
168− E.Modify record fields ->
162+ E.Update record fields ->
169163 go record
170164 <* F.traverse_ (go . snd) fields
171165
src/Optimize/Substitute.hs+4−10
@@ -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
@@ -75,14 +75,8 @@ subst old new expression =
7575 Access record field ->
7676 Access (f record) field
7777
78− Remove record field ->
79− Remove (f record) field
80−
81− Insert record field value ->
82− Insert (f record) field (f value)
83−
84− Modify record fields ->
85− Modify (f record) (map (second f) fields)
78+ Update record fields ->
79+ Update (f record) (map (second f) fields)
8680
8781 Record fields ->
8882 Record (map (second f) fields)
src/Optimize/TailCalls.hs+4−15
@@ -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
@@ -179,19 +179,8 @@ findTailCalls context annExpr@(A.A _ expression) =
179179 <$> justConvert record
180180 <*> pure field
181181
182− Remove record field ->
183− Remove
184− <$> justConvert record
185− <*> pure field
186−
187− Insert record field expr ->
188− Insert
189− <$> justConvert record
190− <*> pure field
191− <*> justConvert expr
192−
193− Modify record fields ->
194− Modify
182+ Update record fields ->
183+ Update
195184 <$> justConvert record
196185 <*> T.traverse (mapSnd justConvert) fields
197186
src/Parse/Expression.hs+44−86
@@ -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 --------
@@ -136,65 +134,40 @@ parensTerm =
136134
137135recordTerm :: IParser Source.Expr
138136recordTerm =
139− addLocation $
140− brackets (choice [ misc, record ])
141− where
142− record =
143− E.Record <$> commaSep field
144−
145− field =
146− do label <- rLabel
147− patterns <- spacePrefix Pattern.term
148− padded equals
149− body <- expr
150− return (label, makeFunction patterns body)
151−
152− misc =
153− do nextParser <- try miscStarter
154− whitespace
155− nextParser
156−
157− miscStarter =
158− do start <- getMyPosition
159− record <- addLocation (E.rawVar <$> rLabel)
137+ addLocation $ brackets $ choice $
138+ [ do starter <- try (addLocation rLabel)
160139 whitespace
161140 choice
162− [ string "-" >> return (afterMinus start record)
163− , string "|" >> return (afterBar record)
141+ [ update starter
142+ , literal starter
164143 ]
144+ , return (E.Record [])
145+ ]
146+ where
147+ update (A.A ann starter) =
148+ do try (string "|")
149+ whitespace
150+ fields <- commaSep1 field
151+ return (E.Update (A.A ann (E.rawVar starter)) fields)
165152
166− afterMinus start record =
167− do field <- rLabel
168− end <- getMyPosition
169− let subRecord' = E.Remove record field
170− maybe <- optionMaybe (try (whitespace >> string "|"))
171− case maybe of
172− Nothing ->
173− return subRecord'
174− Just _ ->
175− do whitespace
176− field <- rLabel
177− padded equals
178− E.Insert (A.at start end subRecord') field <$> expr
179−
180− afterBar record =
181− do field <- rLabel
153+ literal (A.A _ starter) =
154+ do try equals
155+ whitespace
156+ value <- expr
182157 whitespace
183158 choice
184− [ do equals
159+ [ do try comma
185160 whitespace
186− E.Insert record field <$> expr
187− , do leftArrow
188− whitespace
189− value <- expr
190− updates <- spaceyPrefixBy comma update
191− return (E.Modify record ((field,value) : updates))
161+ fields <- commaSep field
162+ return (E.Record ((starter, value) : fields))
163+ , return (E.Record [(starter, value)])
192164 ]
193165
194− update =
195− do lbl <- rLabel
196− padded leftArrow
197− (,) lbl <$> expr
166+ field =
167+ do key <- rLabel
168+ padded equals
169+ value <- expr
170+ return (key, value)
198171
199172
200173term :: IParser Source.Expr
@@ -222,8 +195,7 @@ appExpr =
222195
223196expr :: IParser Source.Expr
224197expr =
225− addLocation (choice [ letExpr, caseExpr ])
226− <|> ifExpr
198+ addLocation (choice [ letExpr, caseExpr, ifExpr ])
227199 <|> lambdaExpr
228200 <|> binaryExpr
229201 <?> "an expression"
@@ -234,45 +206,31 @@ binaryExpr =
234206 Binop.binops appExpr lastExpr anyOp
235207 where
236208 lastExpr =
237− addLocation (choice [ letExpr, caseExpr ])
238− <|> ifExpr
209+ addLocation (choice [ letExpr, caseExpr, ifExpr ])
239210 <|> lambdaExpr
240211 <?> "an expression"
241212
242213
243−ifExpr :: IParser Source.Expr
214+ifExpr :: IParser Source.Expr'
244215ifExpr =
245− do start <- getMyPosition
246− try (reserved "if")
247− whitespace
248− makeExpr <- normal <|> multiIf
249− end <- getMyPosition
250− return (makeExpr (R.Region start end))
251− where
252− normal =
253− do condition <- expr
254− padded (reserved "then")
255− thenBranch <- expr
256− whitespace <?> "an 'else' branch"
257− reserved "else" <?> "an 'else' branch"
258− whitespace
259− elseBranch <- expr
260− return $ \region -> A.A region (E.MultiIf [(condition, thenBranch)] elseBranch)
216+ ifHelp []
261217
262− multiIf =
263− do branches <- spaceSep1 multiBranch
264− return $ \region -> A.A region (E.MultiIf branches (crash region))
265218
266− crash region =
267− A.A region (E.Crash (Crash.IncompleteMultiIf region))
268−
269− multiBranch =
270− do string "|"
271− whitespace
272− condition <- expr
273− padded rightArrow
274− branch <- expr
275− 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+ ]
276234
277235
278236lambdaExpr :: IParser Source.Expr
src/Parse/Helpers.hs+1−6
@@ -137,11 +137,6 @@ rightArrow =
137137 string "->" <|> string "\8594" <?> "->"
138138
139139
140−leftArrow :: IParser String
141−leftArrow =
142− string "<-" <?> "<- for a record update"
143−
144−
145140hasType :: IParser String
146141hasType =
147142 string ":" <?> "the \"has type\" symbol ':'"
@@ -628,6 +623,6 @@ processAs processor s =
628623 , T.opStart = undefined
629624 , T.opLetter = undefined
630625 , T.reservedNames = reserveds
631− , T.reservedOpNames = [":", "->", "<-", "|"]
626+ , T.reservedOpNames = [":", "->", "|"]
632627 , T.caseSensitive = True
633628 }
src/Type/Constrain/Expression.hs+2−19
@@ -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 ->
@@ -118,24 +118,7 @@ constrain env annotatedExpr@(A.A region expression) tipe =
118118 exists $ \t ->
119119 constrain env expr (record (Map.singleton label [tipe]) t)
120120
121− E.Remove expr label ->
122− exists $ \t ->
123− constrain env expr (record (Map.singleton label [t]) tipe)
124−
125− E.Insert expr label value ->
126− exists $ \valueType ->
127− exists $ \recordType ->
128− do valueCon <- constrain env value valueType
129− recordCon <- constrain env expr recordType
130− let newRecordType =
131− record (Map.singleton label [valueType]) recordType
132− return $ CAnd
133− [ valueCon
134− , recordCon
135− , CEqual Error.Record region tipe newRecordType
136− ]
137−
138− E.Modify expr fields ->
121+ E.Update expr fields ->
139122 exists $ \t ->
140123 do oldVars <- Monad.forM fields $ \_ -> variable Flexible
141124 let oldFields = ST.fieldMap (zip (map fst fields) (map varN oldVars))
src/Validate.hs+4−15
@@ -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
@@ -223,19 +223,8 @@ expression (A.A ann sourceExpression) =
223223 <$> expression record
224224 <*> return field
225225
226− Remove record field ->
227− Remove
228− <$> expression record
229− <*> return field
230−
231− Insert record field expr ->
232− Insert
233− <$> expression record
234− <*> return field
235− <*> expression expr
236−
237− Modify record fields ->
238− Modify
226+ Update record fields ->
227+ Update
239228 <$> expression record
240229 <*> T.traverse second fields
241230