on dedbd23 help sign in
elk/src/Transform/Definition.hs
35 lines 1.3 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Transform.Definition where
4import Control.Applicative ((<$>))
5import qualified AST.Pattern as P
6import qualified AST.Expression.Source as Source
7import qualified AST.Expression.Valid as Valid
8import qualified Transform.Expression as Expr
10combineAnnotations :: [Source.Def] -> Either String [Valid.Def]
11combineAnnotations = go
12 where
13 msg x = "Syntax Error: The type annotation for '" ++ x ++
14 "' must be directly above its definition."
16 exprCombineAnnotations = Expr.crawlLet combineAnnotations
18 go defs =
19 case defs of
20 Source.TypeAnnotation name tipe : rest ->
21 case rest of
22 Source.Definition pat@(P.Var name') expr : rest'
23 | name == name' ->
24 do expr' <- exprCombineAnnotations expr
25 let def = Valid.Definition pat expr' (Just tipe)
26 (:) def <$> go rest'
28 _ -> Left (msg name)
30 Source.Definition pat expr : rest ->
31 do expr' <- exprCombineAnnotations expr
32 let def = Valid.Definition pat expr' Nothing
33 (:) def <$> go rest
35 [] -> return []