on 63243ca help sign in
elk/compiler/Transform/Definition.hs
31 lines 1.1 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Transform.Definition where
4import Control.Applicative ((<$>))
5import qualified SourceSyntax.Pattern as P
6import SourceSyntax.Expression
7import qualified Transform.Expression as Expr
9combineAnnotations :: [ParseDef] -> Either String [Def]
10combineAnnotations = go
11 where
12 msg x = "Syntax Error: The type annotation for '" ++ x ++
13 "' must be directly above its definition."
15 exprCombineAnnotations = Expr.crawlLet combineAnnotations
17 go defs =
18 case defs of
19 TypeAnnotation name tipe : Def pat@(P.PVar name') expr : rest | name == name' ->
20 do expr' <- exprCombineAnnotations expr
21 let def = Definition pat expr' (Just tipe)
22 (:) def <$> go rest
24 TypeAnnotation name _ : _ -> Left (msg name)
26 Def pat expr : rest ->
27 do expr' <- exprCombineAnnotations expr
28 let def = Definition pat expr' Nothing
29 (:) def <$> go rest
31 [] -> return []