on 2a6e92b help sign in
elk/src/Transform/Interface.hs
60 lines 1.6 kB history raw
1module Transform.Interface (filterExports) where
3import qualified Data.Map as Map
4import qualified Data.Maybe as Maybe
5import qualified AST.Module as Module
6import qualified AST.Variable as Var
9filterExports :: Module.Interface -> Module.Interface
10filterExports interface =
11 interface
12 { Module.iTypes =
13 Map.fromList $
14 Maybe.mapMaybe
15 (get (Module.iTypes interface))
16 (Var.getValues exports ++ ctors)
18 , Module.iAliases =
19 Map.fromList $
20 Maybe.mapMaybe
21 (get (Module.iAliases interface))
22 (Var.getAliases exports)
24 , Module.iAdts =
25 Map.fromList
26 (Maybe.mapMaybe (trimUnions interface) unions)
27 }
28 where
29 exports :: [Var.Value]
30 exports =
31 Module.iExports interface
33 unions :: [(String, Var.Listing String)]
34 unions =
35 Var.getUnions exports
37 ctors :: [String]
38 ctors =
39 concatMap (\(_, Var.Listing ctorList _) -> ctorList) unions
42get :: Map.Map String a -> String -> Maybe (String, a)
43get dict key =
44 case Map.lookup key dict of
45 Nothing -> Nothing
46 Just value -> Just (key, value)
49trimUnions
50 :: Module.Interface
51 -> (String, Var.Listing String)
52 -> Maybe (String, Module.AdtInfo String)
53trimUnions interface (name, Var.Listing exportedCtors _) =
54 case Map.lookup name (Module.iAdts interface) of
55 Nothing -> Nothing
56 Just (tvars, ctors) ->
57 let isExported (ctor, _) =
58 ctor `elem` exportedCtors
59 in
60 Just (name, (tvars, filter isExported ctors))