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