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) []
61 (Type.Type (Var.fromModule moduleName name))
66 C.Ctor (Var.fromModule moduleName tag) (map (A.A region . C.localVar) vars)
68 definition tag (buildFunction vars body) region (foldr Type.Lambda tbody tipes)
70 map tagToDef constructors
73aliasToDefs :: ModuleName.Canonical -> A.Commented (Decl.Alias Type.Canonical) -> Maybe C.Def
74aliasToDefs moduleName (A.A (region,_) (Decl.Type name tvars tipe)) =
76 Type.Record fields Nothing ->
80 (Var.fromModule moduleName name)
81 (zip tvars (map Type.Var tvars))
88 generateArgs (length args) []
92 C.Record (zip (map fst fields) (map (A.A region . C.localVar) vars))
94 Just $ definition name (buildFunction vars record) region (foldr Type.Lambda resultType args)
100generateArgs :: Int -> [Text] -> [Text]
105 generateArgs (n - 1) (Text.pack ('v' : show n) : args)
108buildFunction :: [Text] -> C.Expr -> C.Expr
109buildFunction vars body@(A.A ann _) =
111 (\pattern expr -> A.A ann (C.Lambda pattern expr))
113 (map (A.A ann . P.Var) vars)
116definition :: Text -> C.Expr -> Region -> Type.Canonical -> C.Def
117definition name expr@(A.A ann _) region tipe =
120 (A.A ann (P.Var name))
122 (Just (A.A region tipe))
129effectsToDefs :: ModuleName.Canonical -> Effects.Canonical -> [C.Def]
130effectsToDefs moduleName effects =
135 Effects.Port ports ->
138 Effects.Manager _ info ->
140 cmdToDef (A.A region name) managerType =
143 (A.A region (C.Cmd moduleName managerType))
145 (Type.cmd moduleName name)
147 subToDef (A.A region name) managerType =
150 (A.A region (C.Sub moduleName managerType))
152 (Type.sub moduleName name)
154 case Effects._managerType info of
155 Effects.CmdManager cmd ->
156 [ cmdToDef cmd Effects.Cmds
159 Effects.SubManager sub ->
160 [ subToDef sub Effects.Subs
163 Effects.FxManager cmd sub ->
164 [ cmdToDef cmd Effects.Both
165 , subToDef sub Effects.Both
169portToDef :: A.Commented Effects.PortCanonical -> C.Def
170portToDef (A.A (region, _) (Effects.PortCanonical name kind tipe)) =
171 definition name (A.A region (toPortExpr name kind)) region tipe
174toPortExpr :: Text -> Effects.Kind -> C.Expr'
175toPortExpr name kind =
177 Effects.Outgoing tipe ->
178 C.OutgoingPort name tipe
180 Effects.Incoming tipe ->
181 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