git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
add ImportDeclaration type checker
a80fa9aBogdan Popa
src/Language/PureScript/Names.hs+4−1
@@ -30,7 +30,10 @@ newtype ProperName = ProperName { runProperName :: String } deriving (Eq, Ord, D
30instance Show ProperName where
30instance Show ProperName where
31 show = runProperName
31 show = runProperName
32
32
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
34
37
35subModule :: ModulePath -> ProperName -> ModulePath
38subModule :: ModulePath -> ProperName -> ModulePath
36subModule (ModulePath mp) name = ModulePath (mp ++ [name])
39subModule (ModulePath mp) name = ModulePath (mp ++ [name])
src/Language/PureScript/TypeChecker.hs+23
@@ -117,3 +117,26 @@ typeCheckAll (FixityDeclaration _ name : rest) = do
117typeCheckAll (ModuleDeclaration name decls : rest) = do
117typeCheckAll (ModuleDeclaration name decls : rest) = do
118 withModule name $ typeCheckAll decls
118 withModule name $ typeCheckAll decls
119 typeCheckAll rest
119 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) })