git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Import Prim without cheating
commite8cc611c63b8ae058135ad30474dcad0bda4a4c6 authorGary Burgess <gary.burgess@gmail.com> dateTue 15 Jul 2014, 20:48 (12 years ago) parentc4186db
2 files changed+17−15
src/Language/PureScript.hs+16−13 src/Language/PureScript/Sugar/Names.hs+1−2
src/Language/PureScript.hs+16−13
@@ -70,7 +70,7 @@ compile = compile' initEnvironment
7070
7171compile' :: Environment -> Options -> [Module] -> Either String (String, String, Environment)
7272compile' env opts ms = do
73− (sorted, _) <- sortModules $ if optionsNoPrelude opts then ms else (map importPrelude ms)
73+ (sorted, _) <- sortModules $ map importPrim $ if optionsNoPrelude opts then ms else (map importPrelude ms)
7474 (desugared, nextVar) <- stringifyErrorStack True $ runSupplyT 0 $ desugar sorted
7575 (elaborated, env') <- runCheck' opts env $ forM desugared $ typeCheckModule mainModuleIdent
7676 regrouped <- stringifyErrorStack True $ createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
@@ -170,7 +170,7 @@ make :: (Functor m, Applicative m, Monad m, MonadMake m) => FilePath -> Options
170170make outputDir opts ms = do
171171 let filePathMap = M.fromList (map (\(fp, Module mn _ _) -> (mn, fp)) ms)
172172
173− (sorted, graph) <- liftError $ sortModules $ if optionsNoPrelude opts then map snd ms else (map (importPrelude . snd) ms)
173+ (sorted, graph) <- liftError $ sortModules $ map importPrim $ if optionsNoPrelude opts then map snd ms else (map (importPrelude . snd) ms)
174174
175175 toRebuild <- foldM (\s (Module moduleName' _ _) -> do
176176 let filePath = runModuleName moduleName'
@@ -241,16 +241,19 @@ reverseDependencies g = combine [ (dep, mn) | (mn, deps) <- g, dep <- deps ]
241241 combine = M.fromList . map ((fst . head) &&& map snd) . groupBy ((==) `on` fst) . sortBy (compare `on` fst)
242242
243243-- |
244−-- Add an import declaration for the Prelude to a module if it does not already explicitly import
245−-- it.
244+-- Add an import declaration for a module if it does not already explicitly import it.
246245--
247−importPrelude :: Module -> Module
248−importPrelude m@(Module mn decls exps) =
249− if isPreludeImport `any` decls || mn == prelude then m
250− else Module mn (preludeImport : decls) exps
246+addDefaultImport :: ModuleName -> Module -> Module
247+addDefaultImport toImport m@(Module mn decls exps) =
248+ if isExistingImport `any` decls || mn == toImport then m
249+ else Module mn (ImportDeclaration toImport Nothing Nothing : decls) exps
251250 where
252− prelude = ModuleName [ProperName C.prelude]
253− isPreludeImport (ImportDeclaration (ModuleName [ProperName mn']) _ _) | mn' == C.prelude = True
254− isPreludeImport (PositionedDeclaration _ d) = isPreludeImport d
255− isPreludeImport _ = False
256− preludeImport = ImportDeclaration prelude Nothing Nothing
251+ isExistingImport (ImportDeclaration mn' _ _) | mn' == toImport = True
252+ isExistingImport (PositionedDeclaration _ d) = isExistingImport d
253+ isExistingImport _ = False
254+
255+importPrim :: Module -> Module
256+importPrim = addDefaultImport (ModuleName [ProperName C.prim])
257+
258+importPrelude :: Module -> Module
259+importPrelude = addDefaultImport (ModuleName [ProperName C.prelude])
src/Language/PureScript/Sugar/Names.hs+1−2
@@ -387,8 +387,7 @@ resolveImports env (Module currentModule decls _) =
387387 -- module (where Nothing indicates everything is to be imported), and optionally a qualified name
388388 -- for the module
389389 scope :: M.Map ModuleName (Maybe SourcePos, Maybe ExplicitImports, Maybe ModuleName)
390− scope = M.insert (ModuleName [ProperName C.prim]) (Nothing, Nothing, Nothing) $
391− M.insert currentModule (Nothing, Nothing, Nothing) (findImports decls)
390+ scope = M.insert currentModule (Nothing, Nothing, Nothing) (findImports decls)
392391
393392 resolveImport' :: ImportEnvironment -> (ModuleName, (Maybe SourcePos, Maybe ExplicitImports, Maybe ModuleName)) -> Either ErrorStack ImportEnvironment
394393 resolveImport' imp (mn, (pos, explImports, impQual)) = do