Skip to content

Commit

Permalink
Module sorting using a module header getter
Browse files Browse the repository at this point in the history
  • Loading branch information
natefaubion committed Mar 27, 2021
1 parent f6e2fc1 commit 4a04fe1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion parse-package-set/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ main = runAff_ (either throwException mempty) do
let
mods = Array.mapMaybe _.mbModule moduleResults

liftEffect case sortModules mods of
liftEffect case sortModules identity mods of
Sorted sorted -> Console.log $ Array.intercalate " "
[ "Successfully sorted module graph for"
, show (Array.length sorted)
Expand Down
23 changes: 13 additions & 10 deletions src/PureScript/CST/ModuleGraph.purs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,31 @@ import PureScript.CST.Types (ImportDecl(..), ModuleHeader(..), ModuleName, Name(

type Graph a = Map a (Set a)

moduleGraph :: forall e. Array (ModuleHeader e) -> Graph ModuleName
moduleGraph = Map.fromFoldable <<< map go
moduleGraph :: forall e a. (a -> ModuleHeader e) -> Array a -> Graph ModuleName
moduleGraph k = Map.fromFoldable <<< map (go <<< k)
where
go (ModuleHeader { name: Name { name }, imports }) =
Tuple name (Set.fromFoldable (map getImportName imports))

getImportName (ImportDecl { "module": Name { name } }) = name

data ModuleSort e
= Sorted (Array (ModuleHeader e))
| CycleDetected (Array (ModuleHeader e))
data ModuleSort a
= Sorted (Array a)
| CycleDetected (Array a)

sortModules :: forall e. Array (ModuleHeader e) -> ModuleSort e
sortModules moduleHeaders = do
sortModules :: forall e a. (a -> ModuleHeader e) -> Array a -> ModuleSort a
sortModules k moduleHeaders = do
let
knownModuleHeaders :: Map ModuleName (ModuleHeader e)
getModuleName :: ModuleHeader e -> ModuleName
getModuleName (ModuleHeader { name: Name { name } }) = name

knownModuleHeaders :: Map ModuleName a
knownModuleHeaders =
moduleHeaders
# map (\header@(ModuleHeader { name: Name { name } }) -> Tuple name header)
# map (\a -> Tuple (getModuleName (k a)) a)
# Map.fromFoldable

graph = moduleGraph moduleHeaders
graph = moduleGraph k moduleHeaders
lookupModuleHeaders = Array.mapMaybe (flip Map.lookup knownModuleHeaders) <<< List.toUnfoldable

case topoSort graph of
Expand Down

0 comments on commit 4a04fe1

Please sign in to comment.