1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Canonicalize.Body (flatten) where
5import qualified Data.Maybe as Maybe
6import qualified Data.Text as Text
9import qualified AST.Declaration as Decl
10import qualified AST.Effects as Effects
11import qualified AST.Expression.Canonical as C
12import qualified AST.Module.Name as ModuleName
13import qualified AST.Pattern as P
14import qualified AST.Type as Type
15import qualified AST.Variable as Var
16import qualified Canonicalize.Sort as Sort
17import qualified Reporting.Annotation as A
18import Reporting.Error.Canonicalize (Error)
19import Reporting.Region (Region)
20import Reporting.Result (Result)
26 => ModuleName.Canonical
29 -> Result i w Error C.Expr
30flatten moduleName (Decl.Decls defs unions aliases _) effects =
34 [ concatMap (unionToDefs moduleName) unions
35 , Maybe.mapMaybe (aliasToDefs moduleName) aliases
36 , effectsToDefs moduleName effects
41 A.A (error "this annotation should never be needed")
43 Sort.definitions $ loc $
44 C.Let allDefs (loc $ C.SaveEnv moduleName effects)
51unionToDefs :: ModuleName.Canonical -> A.Commented (Decl.Union Type.Canonical) -> [C.Def]
52unionToDefs moduleName (A.A (region,_) (Decl.Type name tvars constructors)) =
54 tagToDef (tag, tipes) =
57 generateArgs (length tipes) []
60 Type.Type (Var.fromModule moduleName name) (map Type.Var tvars)
64 C.Ctor (Var.fromModule moduleName tag) (map (A.A region . C.localVar) vars)
66 definition tag (buildFunction vars body) region (foldr Type.Lambda tbody tipes)
68 map tagToDef constructors
71aliasToDefs :: ModuleName.Canonical -> A.Commented (Decl.Alias Type.Canonical) -> Maybe C.Def
72aliasToDefs moduleName (A.A (region,_) (Decl.Type name tvars tipe)) =
74 Type.Record fields Nothing ->
78 (Var.fromModule moduleName name)
79 (zip tvars (map Type.Var tvars))
86 generateArgs (length args) []
90 C.Record (zip (map fst fields) (map (A.A region . C.localVar) vars))
92 Just $ definition name (buildFunction vars record) region (foldr Type.Lambda resultType args)
98generateArgs :: Int -> [Text] -> [Text]
103 generateArgs (n - 1) (Text.pack ('v' : show n) : args)
106buildFunction :: [Text] -> C.Expr -> C.Expr
107buildFunction vars body@(A.A ann _) =
109 (\pattern expr -> A.A ann (C.Lambda pattern expr))
111 (map (A.A ann . P.Var) vars)
114definition :: Text -> C.Expr -> Region -> Type.Canonical -> C.Def
115definition name expr@(A.A ann _) region tipe =
118 (A.A ann (P.Var name))
120 (Just (A.A region tipe))
127effectsToDefs :: ModuleName.Canonical -> Effects.Canonical -> [C.Def]
128effectsToDefs moduleName effects =
133 Effects.Port ports ->
136 Effects.Manager _ info ->
138 cmdToDef (A.A region name) managerType =
141 (A.A region (C.Cmd moduleName managerType))
143 (Type.cmd moduleName name)
145 subToDef (A.A region name) managerType =
148 (A.A region (C.Sub moduleName managerType))
150 (Type.sub moduleName name)
152 case Effects._managerType info of
153 Effects.CmdManager cmd ->
154 [ cmdToDef cmd Effects.Cmds
157 Effects.SubManager sub ->
158 [ subToDef sub Effects.Subs
161 Effects.FxManager cmd sub ->
162 [ cmdToDef cmd Effects.Both
163 , subToDef sub Effects.Both
167portToDef :: A.Commented Effects.PortCanonical -> C.Def
168portToDef (A.A (region, _) (Effects.PortCanonical name kind tipe)) =
169 definition name (A.A region (toPortExpr name kind)) region tipe
172toPortExpr :: Text -> Effects.Kind -> C.Expr'
173toPortExpr name kind =
175 Effects.Outgoing tipe ->
176 C.OutgoingPort name tipe
178 Effects.Incoming tipe ->
179 C.IncomingPort name tipe
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list