on master help sign in
Merge branch 'master' of https://github.com/evancz/Elm
commitba5cd880d476b9d2bc6975acba0f3b63d6a3db7f authorevancz <info@elm-lang.org> dateThu 6 Jun 2013, 22:30 (13 years ago) parent44fecef152ad77
8 files changed+52−14
compiler/Gen/Cases.hs+10−7 compiler/Model/Ast.hs+3−1 compiler/Parse/Parser.hs−1 compiler/Parse/Patterns.hs+21−4 compiler/Transform/Rename.hs+4 compiler/Transform/Replace.hs+1 compiler/Types/Constrain.hs+9 compiler/Types/Substitutions.hs+4−1
compiler/Gen/Cases.hs+10−7
@@ -59,13 +59,16 @@ matchVar :: [String] -> [([Pattern],CExpr)] -> Match -> GuidCounter Match
5959matchVar (v:vs) cs def = match vs (map subVar cs) def
6060 where
6161 subVar (p:ps, ce@(L t s e)) =
62− let loc = L t s in
63− (ps, case p of
64− PVar x -> L t s $ subst x (Var v) e
65− PAnything -> ce
66− PRecord fs ->
67− loc $ foldr (\x -> subst x (Access (loc (Var v)) x)) e fs
68− )
62+ let
63+ loc = L t s
64+ subOnePattern (PVar x) = subst x (Var v) e
65+ subOnePattern (PAsVar x p) =
66+ subst x (Var v) e
67+ subOnePattern PAnything = e
68+ subOnePattern (PRecord fs) =
69+ foldr (\x -> subst x (Access (loc (Var v)) x)) e fs
70+ in
71+ (ps, L t s $ subOnePattern p)
6972
7073matchCon :: [String] -> [([Pattern],CExpr)] -> Match -> GuidCounter Match
7174matchCon (v:vs) cs def = (flip (Match v) def) `liftM` mapM toClause css
compiler/Model/Ast.hs+3−1
@@ -8,7 +8,7 @@ import Types.Types
88import qualified Text.Pandoc as Pandoc
99import Data.Data
1010
11−data Module = Module [String] Exports Imports [Statement]
11+data Module = Module [String] Exports Imports [Statement] deriving (Show)
1212
1313type Exports = [String]
1414
@@ -19,6 +19,7 @@ data ImportMethod = As String | Importing [String] | Hiding [String]
1919
2020data Pattern = PData String [Pattern]
2121 | PRecord [String]
22+ | PAsVar String Pattern
2223 | PVar String
2324 | PAnything
2425 deriving (Eq, Data, Typeable)
@@ -82,6 +83,7 @@ instance Show Pattern where
8283 case p of
8384 PRecord fs -> brkt (intercalate ", " fs)
8485 PVar x -> x
86+ PAsVar x p -> x ++ "@(" ++ show p ++ ")"
8587 PAnything -> "_"
8688 PData "Cons" [hd@(PData "Cons" _),tl] ->
8789 parens (show hd) ++ " :: " ++ show tl
compiler/Parse/Parser.hs−1
@@ -14,7 +14,6 @@ import Parse.Types
1414import Parse.Modules
1515import Parse.Foreign
1616
17−
1817statement = choice (typeAlias:defs) <|> def <?> "datatype or variable definition"
1918 where defs = map ((:[]) <$>) [ foreignDef, datatype ]
2019
compiler/Parse/Patterns.hs+21−4
@@ -3,7 +3,7 @@ module Parse.Patterns (patternTerm, patternExpr, makeLambda, flattenPatterns) wh
33
44import Ast
55import Located
6−import Control.Applicative ((<$>),(<*>))
6+import Control.Applicative ((<$>),(<*>),(*>),pure)
77import Control.Monad
88import Control.Monad.State
99import Data.Char (isUpper)
@@ -19,6 +19,12 @@ patternBasic =
1919 return $ if isUpper c then PData x [] else PVar x
2020 ]
2121
22+patternMaybeAsVar :: Pattern -> IParser Pattern
23+patternMaybeAsVar x =
24+ choice [ PAsVar <$>
25+ (optional whitespace *> reserved "as" *> whitespace *> lowVar) <*> pure x,
26+ pure x ]
27+
2228patternRecord :: IParser Pattern
2329patternRecord = PRecord <$> brackets (commaSep1 lowVar)
2430
@@ -30,14 +36,17 @@ patternList :: IParser Pattern
3036patternList = plist <$> braces (commaSep patternExpr)
3137
3238patternTerm :: IParser Pattern
33−patternTerm = choice [ patternRecord, patternTuple, patternList, patternBasic ]
34− <?> "pattern"
39+patternTerm = (choice [ patternRecord, patternTuple, patternList,
40+ patternBasic ])
41+ <?> "pattern"
3542
3643patternConstructor :: IParser Pattern
3744patternConstructor = PData <$> capVar <*> spacePrefix patternTerm
3845
3946patternExpr :: IParser Pattern
40−patternExpr = foldr1 pcons <$> consSep1 (patternConstructor <|> patternTerm) <?> "pattern"
47+patternExpr = (patternMaybeAsVar =<<
48+ (foldr1 pcons <$> consSep1 (patternConstructor <|> patternTerm)))
49+ <?> "pattern"
4150
4251makeLambda :: [Pattern] -> CExpr -> GuidCounter CExpr
4352makeLambda pats body = go (reverse pats) body
@@ -53,6 +62,10 @@ extract pattern body@(L t s _) =
5362 case pattern of
5463 PAnything -> return $ fn "_" body
5564 PVar x -> return $ fn x body
65+ PAsVar x PAnything -> return $ fn x body
66+ PAsVar x p -> do
67+ (x', body') <- extract p body
68+ return $ fn x (loc $ Let [FnDef x' [] (loc $ Var x)] body')
5669 PData name ps -> do
5770 x <- guid
5871 let a = '_' : show x
@@ -97,6 +110,10 @@ matchSingle pat exp@(L t s _) p =
97110 PVar x ->
98111 return [ FnDef x [] (loc $ Case exp [(pat, loc $ Var x)]) ]
99112
113+ PAsVar x p' -> do
114+ subPat <- matchSingle p' (loc $ Var x) p'
115+ return $ (FnDef x [] (loc $ Case exp [(pat, loc $ Var x)])):subPat
116+
100117 PRecord fs -> do
101118 a <- (\x -> '_' : show x) `liftM` guid
102119 let toDef f = FnDef f [] (loc $ Access (loc $ Var a) f)
compiler/Transform/Rename.hs+4
@@ -130,6 +130,10 @@ patternExtend pattern env =
130130 case pattern of
131131 PAnything -> return (PAnything, env)
132132 PVar x -> first PVar `liftM` extend env x
133+ PAsVar x p -> do
134+ (x', env') <- extend env x
135+ (p', env'') <- patternExtend p env'
136+ return (PAsVar x' p', env'')
133137 PData name ps ->
134138 first (PData name . reverse) `liftM` foldM f ([], env) ps
135139 where f (rps,env') p = do (rp,env'') <- patternExtend p env'
compiler/Transform/Replace.hs+1
@@ -33,6 +33,7 @@ patternVars pattern =
3333 case pattern of
3434 PData _ ps -> unions (map patternVars ps)
3535 PVar x -> singleton x
36+ PAsVar _ p -> patternVars p
3637 PAnything -> empty
3738
3839depth :: Expr -> Integer
compiler/Types/Constrain.hs+9
@@ -259,6 +259,15 @@ patternGen loc tipe as pattern =
259259 b <- beta
260260 let cs = map (loc . (b :=:) . VarT) (Map.findWithDefault [] v as)
261261 return ( Map.delete v as, Set.fromList (loc (b :=: tipe) : cs), b )
262+ PAsVar v p -> do
263+ b <- beta
264+ let cs = map (loc . (b :=:) . VarT) (Map.findWithDefault [] v as)
265+ (as', cs', tipe') <- patternGen loc b as p
266+ return (Map.delete v as',
267+ cs' `Set.union`
268+ (Set.fromList $ (loc (b :=: tipe)):(loc (b :=: tipe')):cs),
269+ b)
270+
262271 PData name ps -> do
263272 constr <- guid
264273 output <- beta
compiler/Types/Substitutions.hs+4−1
@@ -39,7 +39,10 @@ instance Subst Scheme where
3939instance Subst Constraint where
4040 subst ss (t1 :=: t2) = subst ss t1 :=: subst ss t2
4141 subst ss (t :<: super) = subst ss t :<: super
42− subst ss (x :<<: poly) = x :<<: subst ss poly
42+ subst ss (x :<<: poly) = (case lookup x ss of
43+ Just (VarT y) -> y
44+ _ -> x
45+ ) :<<: subst ss poly
4346
4447instance Subst a => Subst (Located a) where
4548 subst ss (L str span c) = L str span (subst ss c)