git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Import Prim without cheating
e8cc611Gary Burgess
src/Language/PureScript.hs+16−13
@@ -70,7 +70,7 @@ compile = compile' initEnvironment
70
70
71compile' :: Environment -> Options -> [Module] -> Either String (String, String, Environment)
71compile' :: Environment -> Options -> [Module] -> Either String (String, String, Environment)
72compile' env opts ms = do
72compile' 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)
74 (desugared, nextVar) <- stringifyErrorStack True $ runSupplyT 0 $ desugar sorted
74 (desugared, nextVar) <- stringifyErrorStack True $ runSupplyT 0 $ desugar sorted
75 (elaborated, env') <- runCheck' opts env $ forM desugared $ typeCheckModule mainModuleIdent
75 (elaborated, env') <- runCheck' opts env $ forM desugared $ typeCheckModule mainModuleIdent
76 regrouped <- stringifyErrorStack True $ createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
76 regrouped <- stringifyErrorStack True $ createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
@@ -170,7 +170,7 @@ make :: (Functor m, Applicative m, Monad m, MonadMake m) => FilePath -> Options
170make outputDir opts ms = do
170make outputDir opts ms = do
171 let filePathMap = M.fromList (map (\(fp, Module mn _ _) -> (mn, fp)) ms)
171 let filePathMap = M.fromList (map (\(fp, Module mn _ _) -> (mn, fp)) ms)
172
172
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)
174
174
175 toRebuild <- foldM (\s (Module moduleName' _ _) -> do
175 toRebuild <- foldM (\s (Module moduleName' _ _) -> do
176 let filePath = runModuleName moduleName'
176 let filePath = runModuleName moduleName'
@@ -241,16 +241,19 @@ reverseDependencies g = combine [ (dep, mn) | (mn, deps) <- g, dep <- deps ]
241 combine = M.fromList . map ((fst . head) &&& map snd) . groupBy ((==) `on` fst) . sortBy (compare `on` fst)
241 combine = M.fromList . map ((fst . head) &&& map snd) . groupBy ((==) `on` fst) . sortBy (compare `on` fst)
242
242
243-- |
243-- |
244−-- Add an import declaration for the Prelude to a module if it does not already explicitly import
244+-- Add an import declaration for a module if it does not already explicitly import it.
245−-- it.
246--
245--
247−importPrelude :: Module -> Module
246+addDefaultImport :: ModuleName -> Module -> Module
248−importPrelude m@(Module mn decls exps) =
247+addDefaultImport toImport m@(Module mn decls exps) =
249− if isPreludeImport `any` decls || mn == prelude then m
248+ if isExistingImport `any` decls || mn == toImport then m
250− else Module mn (preludeImport : decls) exps
249+ else Module mn (ImportDeclaration toImport Nothing Nothing : decls) exps
251 where
250 where
252− prelude = ModuleName [ProperName C.prelude]
253− isPreludeImport (ImportDeclaration (ModuleName [ProperName mn']) _ _) | mn' == C.prelude = True
251+ isExistingImport (ImportDeclaration mn' _ _) | mn' == toImport = True
254− isPreludeImport (PositionedDeclaration _ d) = isPreludeImport d
252+ isExistingImport (PositionedDeclaration _ d) = isExistingImport d
255− isPreludeImport _ = False
253+ isExistingImport _ = False
256− preludeImport = ImportDeclaration prelude Nothing Nothing
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 _) =
387 -- module (where Nothing indicates everything is to be imported), and optionally a qualified name
387 -- module (where Nothing indicates everything is to be imported), and optionally a qualified name
388 -- for the module
388 -- for the module
389 scope :: M.Map ModuleName (Maybe SourcePos, Maybe ExplicitImports, Maybe ModuleName)
389 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)
392
391
393 resolveImport' :: ImportEnvironment -> (ModuleName, (Maybe SourcePos, Maybe ExplicitImports, Maybe ModuleName)) -> Either ErrorStack ImportEnvironment
392 resolveImport' :: ImportEnvironment -> (ModuleName, (Maybe SourcePos, Maybe ExplicitImports, Maybe ModuleName)) -> Either ErrorStack ImportEnvironment
394 resolveImport' imp (mn, (pos, explImports, impQual)) = do
393 resolveImport' imp (mn, (pos, explImports, impQual)) = do