@@ -1,4 +1,4 @@
1−module Parse.Expression (term, annotation, definition, expr, def) where
1+module Parse.Expression (term, expr, def) where
22
33import qualified Data.List as List
44import Text.Parsec hiding (newline, spaces)
@@ -15,7 +15,6 @@ import qualified AST.Expression.General as E
1515import qualified AST.Expression.Source as Source
1616import qualified AST.Literal as L
1717import qualified AST.Pattern as P
18−import qualified AST.Type as T
1918import qualified AST.Variable as Var
2019import qualified Reporting.Annotation as A
2120
@@ -267,8 +266,7 @@ letExpr :: IParser Source.Expr'
267266letExpr =
268267 do try (reserved "let")
269268 whitespace
270− defs <-
271− Indent.block (def <* whitespace)
269+ defs <- Indent.block (def <* whitespace)
272270 whitespace
273271 reserved "in"
274272 whitespace
@@ -277,49 +275,38 @@ letExpr =
277275
278276def :: IParser Source.Def
279277def =
278+ addLocation $ Indent.withPos $
279+ do start <- defStart
280+ case start of
281+ A.A _ (P.Var name) ->
282+ do whitespace
283+ choice
284+ [ do hasType
285+ whitespace
286+ tipe <- Type.expr
287+ return $ Source.Annotation name tipe
288+ , do args <- spacePrefix Pattern.term
289+ padded equals
290+ body <- expr
291+ return $ Source.Definition start (makeFunction args body)
292+ ]
293+
294+ _ ->
295+ do args <- spacePrefix Pattern.term
296+ padded equals
297+ body <- expr
298+ return $ Source.Definition start (makeFunction args body)
299+
300+
301+
302+defStart :: IParser P.Raw
303+defStart =
280304 choice
281− [ annotation Source.Annotation
282− , definition Source.Definition
305+ [ addLocation (try (P.Var <$> parens Binop.infixOp))
306+ , Pattern.term
283307 ]
284308
285309
286−
287−-- TYPE ANNOTATION
288−
289−
290−annotation :: (String -> T.Raw -> a) -> IParser (A.Located a)
291−annotation creator =
292− let
293− start =
294− do v <- lowVar <|> parens Binop.infixOp
295− padded hasType
296− return v
297− in
298− addLocation (creator <$> try start <*> Type.expr)
299−
300−
301−
302−-- DEFINITION
303−
304−
305−definition :: (P.Raw -> Source.Expr -> a) -> IParser (A.Located a)
306−definition creator =
307− addLocation $
308− Indent.withPos $
309− do (name, args) <- defStart
310− padded equals
311− body <- expr
312− return . creator name $ makeFunction args body
313−
314−
315310makeFunction :: [P.Raw] -> Source.Expr -> Source.Expr
316311makeFunction args body@(A.A ann _) =
317312 foldr (\arg body' -> A.A ann $ E.Lambda arg body') body args
318−
319−
320−defStart :: IParser (P.Raw, [P.Raw])
321−defStart =
322− expecting "the definition of a value (x = ...)" $
323− do starter <- try Pattern.term <|> addLocation (P.Var <$> parens Binop.infixOp)
324− args <- spacePrefix Pattern.term
325− return (starter, args)