@@ -1,20 +1,22 @@
1−module Parse.Expr (def,term) where
1+module Parse.Expression (def,term) where
22
33import Control.Applicative ((<$>), (<*>))
4−import Control.Monad
5−import Data.Char (isSymbol, isDigit)
64import Data.List (foldl')
75import Text.Parsec hiding (newline,spaces)
86import Text.Parsec.Indent
97import qualified Text.Pandoc as Pan
108
11−import Parse.Library
12−import Parse.Patterns
13−import Parse.Binops
14−import Parse.Types
9+import Parse.Helpers
10+import qualified Parse.Pattern as Pattern
11+import qualified Parse.Type as Type
12+import Parse.Binop
13+import Parse.Literal
1514
16−import Ast
17−import Located
15+import SourceSyntax.Location as Location
16+import SourceSyntax.Pattern hiding (tuple,list)
17+import qualified SourceSyntax.Literal as Literal
18+import SourceSyntax.Expression
19+import SourceSyntax.Declaration (Declaration(Definition))
1820
1921import Guid
2022import Types.Types (Type (VarT), Scheme (Forall))
@@ -24,36 +26,19 @@ import System.IO.Unsafe
2426
2527-------- Basic Terms --------
2628
27−numTerm :: IParser Expr
28−numTerm = toExpr <$> (preNum <?> "number")
29− where toExpr n | '.' `elem` n = FloatNum (read n)
30− | otherwise = IntNum (read n)
31− preNum = (++) <$> many1 digit <*> option "" postNum
32− postNum = do try $ lookAhead (string "." >> digit)
33− string "."
34− ('.':) <$> many1 digit
35−
36−strTerm :: IParser Expr
37−strTerm = liftM Str . expecting "string" . betwixt '"' '"' . many $
38− backslashed <|> satisfy (/='"')
39−
4029varTerm :: IParser Expr
4130varTerm = toVar <$> var <?> "variable"
4231
43−toVar v = case v of "True" -> Boolean True
44− "False" -> Boolean False
32+toVar v = case v of "True" -> Literal (Literal.Boolean True)
33+ "False" -> Literal (Literal.Boolean False)
4534 _ -> Var v
4635
47−chrTerm :: IParser Expr
48−chrTerm = Chr <$> betwixt '\'' '\'' (backslashed <|> satisfy (/='\''))
49− <?> "character"
50−
5136accessor :: IParser Expr
5237accessor = do
5338 start <- getPosition
5439 lbl <- try (string "." >> rLabel)
5540 end <- getPosition
56− let loc e = addLoc ("." ++ lbl) (pos start end e)
41+ let loc e = Location.add ("." ++ lbl) (Location.at start end e)
5742 return (Lambda "_" (loc $ Access (loc $ Var "_") lbl))
5843
5944
@@ -76,7 +61,7 @@ parensTerm = parens $ choice
7661 [ do start <- getPosition
7762 op <- try anyOp
7863 end <- getPosition
79− let loc = pos start end
64+ let loc = Location.at start end
8065 return . loc . Lambda "x" . loc . Lambda "y" . loc $
8166 Binop op (loc $ Var "x") (loc $ Var "y")
8267 , do start <- getPosition
@@ -84,24 +69,24 @@ parensTerm = parens $ choice
8469 commas <- comma >> many (whitespace >> comma)
8570 end <- getPosition
8671 let vars = map (('v':) . show) [ 0 .. length commas + 1 ]
87− loc = pos start end
72+ loc = Location.at start end
8873 return $ foldr (\x e -> loc $ Lambda x e)
8974 (loc . tuple $ map (loc . Var) vars) vars
9075 , do start <- getPosition
9176 es <- commaSep expr
9277 end <- getPosition
9378 return $ case es of [e] -> e
94− _ -> pos start end (tuple es)
79+ _ -> Location.at start end (tuple es)
9580 ]
9681
9782recordTerm :: IParser LExpr
9883recordTerm = brackets $ choice [ misc, addLocation record ]
9984 where field = do
100− fDefs <- (:) <$> (PVar <$> rLabel) <*> spacePrefix patternTerm
85+ fDefs <- (:) <$> (PVar <$> rLabel) <*> spacePrefix Pattern.term
10186 whitespace
10287 e <- string "=" >> whitespace >> expr
10388 n <- sourceLine <$> getPosition
104− runAt (1000 * n) $ flattenPatterns fDefs e
89+ runAt (1000 * n) $ Pattern.flatten fDefs e
10590 extract [ FnDef f args exp ] = return (f,args,exp)
10691 extract _ = fail "Improperly formed record field."
10792 record = Record <$> (mapM extract =<< commaSep field)
@@ -127,7 +112,7 @@ recordTerm = brackets $ choice [ misc, addLocation record ]
127112
128113
129114term :: IParser LExpr
130−term = addLocation (choice [ numTerm, strTerm, chrTerm, listTerm, accessor ])
115+term = addLocation (choice [ Literal <$> literal, listTerm, accessor ])
131116 <|> accessible (addLocation varTerm <|> parensTerm <|> recordTerm)
132117 <?> "basic term (4, x, 'c', etc.)"
133118
@@ -138,7 +123,7 @@ appExpr = do
138123 tlist <- spaceSep1 term
139124 return $ case tlist of
140125 t:[] -> t
141− t:ts -> foldl' (\f x -> epos f x $ App f x) t ts
126+ t:ts -> foldl' (\f x -> Location.merge f x $ App f x) t ts
142127
143128-------- Normal Expressions --------
144129
@@ -154,7 +139,7 @@ ifExpr = reserved "if" >> whitespace >> (normal <|> multiIf)
154139 thenBranch <- expr
155140 whitespace <?> "an 'else' branch" ; reserved "else" <?> "an 'else' branch" ; whitespace
156141 elseBranch <- expr
157− return $ MultiIf [(bool, thenBranch), (notLocated (Var "otherwise"), elseBranch)]
142+ return $ MultiIf [(bool, thenBranch), (Location.none (Var "otherwise"), elseBranch)]
158143 multiIf = MultiIf <$> spaceSep1 iff
159144 where iff = do string "|" ; whitespace
160145 b <- expr ; whitespace ; string "->" ; whitespace
@@ -163,10 +148,10 @@ ifExpr = reserved "if" >> whitespace >> (normal <|> multiIf)
163148lambdaExpr :: IParser LExpr
164149lambdaExpr = do char '\\' <|> char '\x03BB' <?> "anonymous function"
165150 whitespace
166− pats <- spaceSep1 patternTerm
151+ pats <- spaceSep1 Pattern.term
167152 whitespace ; arrow ; whitespace
168153 e <- expr
169− return . run $ makeLambda pats e
154+ return . run $ Pattern.makeLambda pats e
170155
171156defSet :: IParser [Def]
172157defSet = concat <$> block (do d <- anyDef ; whitespace ; return d)
@@ -182,7 +167,7 @@ caseExpr :: IParser Expr
182167caseExpr = do
183168 reserved "case"; whitespace; e <- expr; whitespace; reserved "of"; whitespace
184169 Case e <$> (with <|> without)
185− where case_ = do p <- patternExpr; whitespace; arrow; whitespace
170+ where case_ = do p <- Pattern.expr; whitespace; arrow; whitespace
186171 (,) p <$> expr
187172 with = brackets (semiSep1 (case_ <?> "cases { x -> ... }"))
188173 without = block (do c <- case_ ; whitespace ; return c)
@@ -192,15 +177,15 @@ expr = addLocation (choice [ ifExpr, letExpr, caseExpr ])
192177 <|> binaryExpr
193178 <?> "an expression"
194179
195−funcDef = try (do p1 <- try patternTerm ; infics p1 <|> func p1)
196− <|> ((:[]) <$> patternExpr)
180+funcDef = try (do p1 <- try Pattern.term ; infics p1 <|> func p1)
181+ <|> ((:[]) <$> Pattern.expr)
197182 <?> "the definition of a variable (x = ...)"
198− where func p@(PVar v) = (p:) <$> spacePrefix patternTerm
183+ where func p@(PVar v) = (p:) <$> spacePrefix Pattern.term
199184 func p = do try (lookAhead (whitespace >> string "="))
200185 return [p]
201186 infics p1 = do
202187 o:p <- try (whitespace >> anyOp)
203− p2 <- (whitespace >> patternTerm)
188+ p2 <- (whitespace >> Pattern.term)
204189 return $ if o == '`' then [ PVar $ takeWhile (/='`') p, p1, p2 ]
205190 else [ PVar (o:p), p1, p2 ]
206191
@@ -210,10 +195,10 @@ assignExpr = withPos $ do
210195 whitespace
211196 e <- string "=" >> whitespace >> expr
212197 n <- sourceLine <$> getPosition
213− runAt (1000 * n) $ flattenPatterns fDefs e
198+ runAt (1000 * n) $ Pattern.flatten fDefs e
214199
215200anyDef =
216− ((\d -> [d]) <$> typeAnnotation) <|>
201+ ((\d -> [d]) <$> Type.annotation) <|>
217202 assignExpr
218203
219204def = map Definition <$> anyDef