on 92be75c help sign in
elk/src/Transform/Interface.hs
57 lines 1.9 kB history raw
1module Transform.Interface (filterExports) where
3import qualified Data.Map as Map
4import qualified AST.Module as Module
5import qualified AST.Type as Type
6import qualified AST.Variable as Var
8filterExports :: Module.Interface -> Module.Interface
9filterExports interface =
10 interface
11 { Module.iTypes =
12 Map.fromList (concatMap getTypes exportedValues)
13 , Module.iAliases =
14 Map.fromList (concatMap getAliases exportedValues)
15 , Module.iAdts =
16 Map.fromList (concatMap getAdts exportedValues)
17 }
18 where
19 exportedValues :: [Var.Value]
20 exportedValues = Module.iExports interface
22 get :: Map.Map String a -> String -> [(String, a)]
23 get dict x =
24 case Map.lookup x dict of
25 Just t -> [(x,t)]
26 Nothing -> []
28 getTypes :: Var.Value -> [(String, Type.CanonicalType)]
29 getTypes value =
30 case value of
31 Var.Value x -> getType x
32 Var.Alias _ -> []
33 Var.ADT _ (Var.Listing ctors _) -> concatMap getType ctors
35 getType :: String -> [(String, Type.CanonicalType)]
36 getType name =
37 get (Module.iTypes interface) name
39 getAliases :: Var.Value -> [(String, ([String], Type.CanonicalType))]
40 getAliases value =
41 case value of
42 Var.Value _ -> []
43 Var.Alias name -> get (Module.iAliases interface) name
44 Var.ADT _ _ -> []
46 getAdts :: Var.Value -> [(String, Module.AdtInfo String)]
47 getAdts value =
48 case value of
49 Var.Value _ -> []
50 Var.Alias _ -> []
51 Var.ADT name (Var.Listing exportedCtors _) ->
52 case Map.lookup name (Module.iAdts interface) of
53 Nothing -> []
54 Just (tvars, ctors) ->
55 [(name, (tvars, filter isExported ctors))]
56 where
57 isExported (ctor, _) = ctor `elem` exportedCtors