|
13 | 13 | ----------------------------------------------------------------------------- |
14 | 14 |
|
15 | 15 | module Language.PureScript.BindingGroups ( |
16 | | - bindingGroups |
| 16 | + createBindingGroups |
17 | 17 | ) where |
18 | 18 |
|
| 19 | +import Data.Graph |
| 20 | +import Data.List (intersect) |
| 21 | + |
19 | 22 | import Language.PureScript.Declarations |
| 23 | +import Language.PureScript.Names |
| 24 | +import Language.PureScript.Scope (usedNames) |
20 | 25 |
|
21 | | -bindingGroups :: [Declaration] -> [[Declaration]] |
22 | | -bindingGroups = undefined |
| 26 | +createBindingGroups :: [Declaration] -> [Declaration] |
| 27 | +createBindingGroups ds = |
| 28 | + let |
| 29 | + values = filter isValueDecl ds |
| 30 | + nonValues = filter (not . isValueDecl) ds |
| 31 | + allIdents = map getIdent values |
| 32 | + verts = map (\d -> (d, getIdent d, usedNames d `intersect` allIdents)) values |
| 33 | + sorted = map toBindingGroup $ stronglyConnComp verts |
| 34 | + in |
| 35 | + map handleModuleDeclaration nonValues ++ sorted |
| 36 | + where |
| 37 | + isValueDecl :: Declaration -> Bool |
| 38 | + isValueDecl (ValueDeclaration _ _ _ _) = True |
| 39 | + isValueDecl _ = False |
| 40 | + getIdent :: Declaration -> Ident |
| 41 | + getIdent (ValueDeclaration ident _ _ _) = ident |
| 42 | + getIdent _ = error "undefined" |
| 43 | + toBindingGroup :: SCC Declaration -> Declaration |
| 44 | + toBindingGroup (AcyclicSCC d) = d |
| 45 | + toBindingGroup (CyclicSCC [d]) = d |
| 46 | + toBindingGroup (CyclicSCC ds') = BindingGroupDeclaration ds' |
| 47 | + handleModuleDeclaration :: Declaration -> Declaration |
| 48 | + handleModuleDeclaration (ModuleDeclaration name ds') = ModuleDeclaration name $ createBindingGroups ds' |
| 49 | + handleModuleDeclaration other = other |
0 commit comments