2013-06-14 01:00:24 +00:00
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
|
|
|
module SourceSyntax.Module where
|
|
|
|
|
|
|
|
import Data.Data
|
2013-07-21 04:08:08 +00:00
|
|
|
import Data.Binary
|
2013-06-14 01:00:24 +00:00
|
|
|
import Data.List (intercalate)
|
2013-07-16 19:42:37 +00:00
|
|
|
import qualified Data.Map as Map
|
2013-07-21 04:08:08 +00:00
|
|
|
import Control.Applicative ((<$>), (<*>))
|
2013-07-22 12:42:45 +00:00
|
|
|
import Control.Arrow (second)
|
2013-07-16 19:42:37 +00:00
|
|
|
|
2013-07-22 12:42:45 +00:00
|
|
|
import SourceSyntax.Expression (LExpr)
|
2013-07-16 19:42:37 +00:00
|
|
|
import SourceSyntax.Declaration
|
|
|
|
import SourceSyntax.Type
|
2013-06-14 01:00:24 +00:00
|
|
|
import System.FilePath (joinPath)
|
2013-07-21 04:08:08 +00:00
|
|
|
import qualified Type.Type as Type
|
2013-06-14 01:00:24 +00:00
|
|
|
|
2013-06-14 03:25:00 +00:00
|
|
|
data Module tipe var =
|
2013-07-16 19:42:37 +00:00
|
|
|
Module [String] Exports Imports [Declaration tipe var]
|
2013-06-14 03:25:00 +00:00
|
|
|
deriving (Show)
|
2013-06-14 01:00:24 +00:00
|
|
|
|
|
|
|
type Exports = [String]
|
|
|
|
|
|
|
|
type Imports = [(String, ImportMethod)]
|
|
|
|
data ImportMethod = As String | Importing [String] | Hiding [String]
|
|
|
|
deriving (Eq, Ord, Show, Data, Typeable)
|
|
|
|
|
2013-07-16 19:42:37 +00:00
|
|
|
data MetadataModule t v = MetadataModule {
|
2013-07-17 17:24:40 +00:00
|
|
|
names :: [String],
|
|
|
|
path :: FilePath,
|
|
|
|
exports :: [String],
|
|
|
|
imports :: [(String, ImportMethod)],
|
2013-07-19 16:02:24 +00:00
|
|
|
program :: LExpr t v,
|
2013-07-21 04:08:08 +00:00
|
|
|
types :: Map.Map String Type.Variable,
|
2013-07-17 17:24:40 +00:00
|
|
|
fixities :: [(Assoc, Int, String)],
|
|
|
|
aliases :: [(String, [String], Type)],
|
|
|
|
datatypes :: [ (String, [String], [(String,[Type])]) ],
|
2013-07-16 19:42:37 +00:00
|
|
|
foreignImports :: [(String, LExpr t v, String, Type)],
|
|
|
|
foreignExports :: [(String, String, Type)]
|
|
|
|
}
|
2013-07-21 04:08:08 +00:00
|
|
|
|
2013-07-21 20:50:48 +00:00
|
|
|
type Interfaces = Map.Map String ModuleInterface
|
|
|
|
|
2013-07-21 04:08:08 +00:00
|
|
|
data ModuleInterface = ModuleInterface {
|
|
|
|
iTypes :: Map.Map String Type,
|
2013-07-21 20:50:48 +00:00
|
|
|
iAdts :: [(String, [String], [(String,[Type])])]
|
2013-07-21 04:08:08 +00:00
|
|
|
} deriving Show
|
|
|
|
|
|
|
|
instance Binary ModuleInterface where
|
|
|
|
put modul = put (iTypes modul) >> put (iAdts modul)
|
2013-07-22 12:42:45 +00:00
|
|
|
get = ModuleInterface <$> get <*> get
|
|
|
|
|
|
|
|
|
|
|
|
canonicalize :: String -> ImportMethod -> ModuleInterface -> ModuleInterface
|
|
|
|
canonicalize prefix importMethod interface =
|
|
|
|
let addPrefix name = prefix ++ "." ++ name
|
|
|
|
|
|
|
|
newName (tipe,_,_) =
|
|
|
|
case importMethod of
|
|
|
|
As name -> (tipe, name ++ "." ++ tipe)
|
|
|
|
Hiding vs -> (tipe, if tipe `elem` vs then addPrefix tipe else tipe)
|
|
|
|
Importing vs -> (tipe, if tipe `elem` vs then tipe else addPrefix tipe)
|
|
|
|
newNames = Map.fromList . map newName $ iAdts interface
|
|
|
|
|
|
|
|
rename name = Map.findWithDefault name name newNames
|
|
|
|
|
|
|
|
renameADT (name, tvars, ctors) =
|
|
|
|
(rename name, tvars, map (second (map (renameType rename))) ctors)
|
|
|
|
in ModuleInterface {
|
|
|
|
iTypes = Map.map (renameType rename) (iTypes interface),
|
|
|
|
iAdts = map renameADT (iAdts interface)
|
|
|
|
}
|
|
|
|
|
|
|
|
renameType :: (String -> String) -> Type -> Type
|
|
|
|
renameType find tipe =
|
|
|
|
let rnm = renameType find in
|
|
|
|
case tipe of
|
|
|
|
Lambda a b -> Lambda (rnm a) (rnm b)
|
|
|
|
Var x -> Var x
|
|
|
|
Data name ts -> Data (find name) (map rnm ts)
|
|
|
|
EmptyRecord -> EmptyRecord
|
|
|
|
Record fields ext -> Record (Map.map (map rnm) fields) (rnm ext)
|