git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Sort modules based on dependencies
11d65b2Phil Freeman
examples/failing/ModuleDeps.pursnew file+7
@@ -0,0 +1,7 @@
1+module M1 where
2+
3+import M2
4+
5+module M2 where
6+
7+import M1
examples/passing/ModuleDeps.pursnew file+13
@@ -0,0 +1,13 @@
1+module M1 where
2+
3+import M2
4+
5+foo = M3.baz
6+
7+module M2 where
8+
9+bar = M3.baz
10+
11+module M3 where
12+
13+baz = 1
purescript.cabal+11−10
@@ -11,8 +11,8 @@ synopsis: PureScript Programming Language Compiler
11description: A small compile-to-JS language with extensible records and type-safe blocks
11description: A small compile-to-JS language with extensible records and type-safe blocks
12category: Language
12category: Language
13author: Phil Freeman <paf31@cantab.net>
13author: Phil Freeman <paf31@cantab.net>
14−data-dir: ""
15data-files: libraries/prelude/prelude.purs
14data-files: libraries/prelude/prelude.purs
15+data-dir: ""
16
16
17library
17library
18 build-depends: base >=4 && <5, cmdtheline -any, containers -any,
18 build-depends: base >=4 && <5, cmdtheline -any, containers -any,
@@ -29,6 +29,7 @@ library
29 Language.PureScript.Values
29 Language.PureScript.Values
30 Language.PureScript.Scope
30 Language.PureScript.Scope
31 Language.PureScript.Sugar
31 Language.PureScript.Sugar
32+ Language.PureScript.ModuleDependencies
32 Language.PureScript.Sugar.CaseDeclarations
33 Language.PureScript.Sugar.CaseDeclarations
33 Language.PureScript.Sugar.DoNotation
34 Language.PureScript.Sugar.DoNotation
34 Language.PureScript.Sugar.TypeDeclarations
35 Language.PureScript.Sugar.TypeDeclarations
@@ -63,32 +64,32 @@ library
63 exposed: True
64 exposed: True
64 buildable: True
65 buildable: True
65 hs-source-dirs: src
66 hs-source-dirs: src
67+ other-modules:
66
68
67executable psc
69executable psc
68 build-depends: base >=4 && <5, cmdtheline -any, containers -any,
70 build-depends: base >=4 && <5, cmdtheline -any, containers -any,
69 directory -any, filepath -any, mtl -any, parsec -any,
71 directory -any, filepath -any, mtl -any, parsec -any,
70 purescript -any, syb -any, transformers -any, utf8-string -any
72 purescript -any, syb -any, transformers -any, utf8-string -any
71 main-is: Main.hs
73 main-is: Main.hs
72− hs-source-dirs: psc
73 buildable: True
74 buildable: True
75+ hs-source-dirs: psc
74 other-modules:
76 other-modules:
75 ghc-options: -Wall -O2 -fno-warn-unused-do-bind
77 ghc-options: -Wall -O2 -fno-warn-unused-do-bind
76
78
77executable psci
79executable psci
78− build-depends: base >=4 && <5, containers -any,
80+ build-depends: base >=4 && <5, containers -any, mtl -any,
79− mtl -any, parsec -any, haskeline <= 0.7.1.1,
81+ parsec -any, haskeline <=0.7.1.1, purescript -any, syb -any,
80− purescript -any, syb -any, transformers -any, utf8-string -any,
82+ transformers -any, utf8-string -any, process -any
81− process -any
82 main-is: Main.hs
83 main-is: Main.hs
83− hs-source-dirs: psci
84 buildable: True
84 buildable: True
85+ hs-source-dirs: psci
85 other-modules:
86 other-modules:
86 ghc-options: -Wall -O2
87 ghc-options: -Wall -O2
87
88
88test-suite tests
89test-suite tests
89− build-depends: base >=4 && <5, containers -any,
90+ build-depends: base >=4 && <5, containers -any, directory -any,
90− directory -any, filepath -any, mtl -any, parsec -any,
91+ filepath -any, mtl -any, parsec -any, purescript -any, syb -any,
91− purescript -any, syb -any, transformers -any, utf8-string -any
92+ transformers -any, utf8-string -any
92 type: exitcode-stdio-1.0
93 type: exitcode-stdio-1.0
93 main-is: Main.hs
94 main-is: Main.hs
94 buildable: True
95 buildable: True
src/Language/PureScript.hs+3−1
@@ -25,6 +25,7 @@ import Language.PureScript.TypeChecker as P
25import Language.PureScript.Pretty as P
25import Language.PureScript.Pretty as P
26import Language.PureScript.Sugar as P
26import Language.PureScript.Sugar as P
27import Language.PureScript.Options as P
27import Language.PureScript.Options as P
28+import Language.PureScript.ModuleDependencies as P
28
29
29import Data.List (intercalate)
30import Data.List (intercalate)
30import Control.Monad (when, forM)
31import Control.Monad (when, forM)
@@ -33,7 +34,8 @@ import qualified Data.Map as M
33
34
34compile :: Options -> [Module] -> Either String (String, String, Environment)
35compile :: Options -> [Module] -> Either String (String, String, Environment)
35compile opts ms = do
36compile opts ms = do
37+ sorted <- sortModules ms
36− desugared <- desugar ms
38+ desugared <- desugar sorted
37 (elaborated, env) <- runCheck $ forM desugared $ \(Module moduleName decls) -> Module moduleName <$> typeCheckAll (ModuleName moduleName) decls
39 (elaborated, env) <- runCheck $ forM desugared $ \(Module moduleName decls) -> Module moduleName <$> typeCheckAll (ModuleName moduleName) decls
38 regrouped <- createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
40 regrouped <- createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
39 let js = concatMap (flip (moduleToJs opts) env) $ regrouped
41 let js = concatMap (flip (moduleToJs opts) env) $ regrouped
src/Language/PureScript/ModuleDependencies.hsnew file+61
@@ -0,0 +1,61 @@
1+-----------------------------------------------------------------------------
2+--
3+-- Module : Language.PureScript.ModuleDependencies
4+-- Copyright : (c) Phil Freeman 2013
5+-- License : MIT
6+--
7+-- Maintainer : Phil Freeman <paf31@cantab.net>
8+-- Stability : experimental
9+-- Portability :
10+--
11+-- |
12+--
13+-----------------------------------------------------------------------------
14+
15+module Language.PureScript.ModuleDependencies (
16+ sortModules
17+) where
18+
19+import Data.Data
20+import Data.Graph
21+import Data.Generics
22+import Data.List (nub, intersect)
23+import Control.Applicative ((<$>))
24+
25+import Language.PureScript.Declarations
26+import Language.PureScript.Names
27+import Language.PureScript.Values
28+import Language.PureScript.Types
29+
30+sortModules :: [Module] -> Either String [Module]
31+sortModules ms = do
32+ let verts = map (\m -> (m, getModuleName m, usedModules m)) ms
33+ mapM toModule $ stronglyConnComp verts
34+
35+collapseBindingGroups :: [Declaration] -> [Declaration]
36+collapseBindingGroups ds = concatMap go ds
37+ where
38+ go (DataBindingGroupDeclaration ds) = ds
39+ go (BindingGroupDeclaration ds) = map (\(ident, val) -> ValueDeclaration ident [] Nothing val) ds
40+ go other = [other]
41+
42+usedModules :: (Data d) => d -> [ProperName]
43+usedModules = nub . everything (++) (mkQ [] qualifiedIdents `extQ` qualifiedProperNames `extQ` imports)
44+ where
45+ qualifiedIdents :: Qualified Ident -> [ProperName]
46+ qualifiedIdents (Qualified (Just (ModuleName pn)) _) = [pn]
47+ qualifiedIdents _ = []
48+ qualifiedProperNames :: Qualified ProperName -> [ProperName]
49+ qualifiedProperNames (Qualified (Just (ModuleName pn)) _) = [pn]
50+ qualifiedProperNames _ = []
51+ imports :: Declaration -> [ProperName]
52+ imports (ImportDeclaration (ModuleName pn) _) = [pn]
53+ imports _ = []
54+
55+getModuleName :: Module -> ProperName
56+getModuleName (Module pn _) = pn
57+
58+toModule :: SCC Module -> Either String Module
59+toModule (AcyclicSCC m) = return m
60+toModule (CyclicSCC [m]) = return m
61+toModule (CyclicSCC _) = Left "Cycle in module dependencies"
src/Language/PureScript/Sugar/BindingGroups.hs−1
@@ -29,7 +29,6 @@ import Language.PureScript.Declarations
29import Language.PureScript.Names
29import Language.PureScript.Names
30import Language.PureScript.Values
30import Language.PureScript.Values
31import Language.PureScript.Types
31import Language.PureScript.Types
32−import Debug.Trace (trace)
33
32
34createBindingGroupsModule :: [Module] -> Either String [Module]
33createBindingGroupsModule :: [Module] -> Either String [Module]
35createBindingGroupsModule = mapM $ \(Module name ds) -> Module name <$> createBindingGroups ds
34createBindingGroupsModule = mapM $ \(Module name ds) -> Module name <$> createBindingGroups ds