on 267f708 help sign in
elk/src/AST/Expression/Canonical.hs
72 lines 1.5 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module AST.Expression.Canonical
3 ( Expr, Expr'
4 , Def(..)
5 , Facts(..), dummyFacts
6 , SortedDefs(..), toSortedDefs
7 )
8 where
10import qualified AST.Expression.General as General
11import qualified AST.Pattern as Pattern
12import qualified AST.Type as Type
13import qualified AST.Variable as Var
14import qualified Reporting.Annotation as A
15import qualified Reporting.Region as R
18{-| Canonicalized expressions. All variables are fully resolved to the module
19they came from.
20-}
21type Expr =
22 General.Expr R.Region Def Var.Canonical Type.Canonical
25type Expr' =
26 General.Expr' R.Region Def Var.Canonical Type.Canonical
29data Def
30 = Def Facts Pattern.Canonical Expr (Maybe (A.Located Type.Canonical))
33data Facts = Facts
34 { dependencies :: [Var.TopLevel]
35 }
38dummyFacts :: Facts
39dummyFacts =
40 Facts (error "This should be set by Canonicalize.Sort")
44-- SORTED DEFS
47data SortedDefs
48 = NoMain [Def]
49 | YesMain [Def] Def [Def]
52toSortedDefs :: Expr -> SortedDefs
53toSortedDefs (A.A _ expr) =
54 case expr of
55 General.Let defs body ->
56 foldr defCons (toSortedDefs body) defs
58 _ ->
59 NoMain []
62defCons :: Def -> SortedDefs -> SortedDefs
63defCons def@(Def _ (A.A _ pattern) _ _) sortedDefs =
64 case (pattern, sortedDefs) of
65 (Pattern.Var "main", NoMain defs) ->
66 YesMain [] def defs
68 (_, NoMain defs) ->
69 NoMain (def : defs)
71 (_, YesMain defs main rest) ->
72 YesMain (def : defs) main rest