Skip to content

Commit a80fa9a

Browse files
committed
add ImportDeclaration type checker
1 parent 7bc6843 commit a80fa9a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Language/PureScript/Names.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ newtype ProperName = ProperName { runProperName :: String } deriving (Eq, Ord, D
3030
instance Show ProperName where
3131
show = runProperName
3232

33-
data ModulePath = ModulePath [ProperName] deriving (Show, Eq, Ord, Data, Typeable)
33+
data ModulePath = ModulePath [ProperName] deriving (Eq, Ord, Data, Typeable)
34+
35+
instance Show ModulePath where
36+
show (ModulePath segments) = intercalate "." $ map show segments
3437

3538
subModule :: ModulePath -> ProperName -> ModulePath
3639
subModule (ModulePath mp) name = ModulePath (mp ++ [name])

src/Language/PureScript/TypeChecker.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,26 @@ typeCheckAll (FixityDeclaration _ name : rest) = do
117117
typeCheckAll (ModuleDeclaration name decls : rest) = do
118118
withModule name $ typeCheckAll decls
119119
typeCheckAll rest
120+
typeCheckAll (ImportDeclaration modulePath idents : rest) = do
121+
env <- getEnv
122+
omp <- checkModulePath `fmap` get
123+
rethrow errorMessage $
124+
guardWith "module does not exist" $ moduleExists env
125+
case idents of
126+
Nothing -> do
127+
let idents = map snd $ filterModule env
128+
rethrow errorMessage $
129+
bindIdents idents omp env
130+
typeCheckAll rest
131+
Just idents -> do
132+
rethrow errorMessage $
133+
bindIdents idents omp env
134+
typeCheckAll rest
135+
where errorMessage = (("Error importing " ++ show modulePath ++ ": ") ++)
136+
filterModule env = filter (\(m, _) -> m == modulePath) (M.keys (names env))
137+
moduleExists env = not $ null $ filterModule env
138+
bindIdents idents omp env =
139+
forM_ idents $ \ident ->
140+
case M.lookup (modulePath, ident) (names env) of
141+
Nothing -> throwError $ show modulePath ++ "." ++ show ident ++ " is undefined"
142+
Just (pt, _) -> putEnv (env { names = M.insert (omp, ident) (pt, Alias modulePath ident) (names env) })

0 commit comments

Comments
 (0)