Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Nov 6, 2023
1 parent 2575404 commit 654f60b
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 207 deletions.
4 changes: 2 additions & 2 deletions src/Juvix/Compiler/Concrete/Data/ParsedInfoTable.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Juvix.Compiler.Concrete.Data.ParsedInfoTable where

import Juvix.Compiler.Concrete.Language
import Juvix.Compiler.Store.Scoped.Language
import Juvix.Prelude

data InfoTable = InfoTable
{ _infoParsedComments :: Comments,
_infoParsedModules :: HashMap TopModulePath (Module 'Parsed 'ModuleTop)
_infoParsedModules :: HashMap TopModulePath ScopedModule
}
deriving stock (Eq, Show)

makeLenses ''InfoTable
6 changes: 2 additions & 4 deletions src/Juvix/Compiler/Concrete/Data/Scope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ import Juvix.Compiler.Concrete.Data.NameSpace
import Juvix.Compiler.Concrete.Data.Scope.Base
import Juvix.Compiler.Concrete.Data.ScopedName qualified as S
import Juvix.Compiler.Concrete.Language
import Juvix.Compiler.Store.Scoped.Language
import Juvix.Prelude

nsEntry :: forall ns. (SingI ns) => Lens' (NameSpaceEntryType ns) (S.Name' ())
nsEntry :: forall ns. (SingI ns) => Lens' (NameSpaceEntryType ns) ScopedName
nsEntry = case sing :: SNameSpace ns of
SNameSpaceModules -> moduleEntry
SNameSpaceSymbols -> preSymbolName
SNameSpaceFixities -> fixityEntry

mkModuleRef' :: (SingI t) => ModuleRef'' 'S.NotConcrete t -> ModuleRef' 'S.NotConcrete
mkModuleRef' m = ModuleRef' (sing :&: m)

scopeNameSpace :: forall (ns :: NameSpace). (SingI ns) => Lens' Scope (HashMap Symbol (SymbolInfo ns))
scopeNameSpace = case sing :: SNameSpace ns of
SNameSpaceSymbols -> scopeSymbols
Expand Down
9 changes: 5 additions & 4 deletions src/Juvix/Compiler/Concrete/Data/Scope/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Juvix.Compiler.Concrete.Data.Scope.Base where
import Juvix.Compiler.Concrete.Data.NameSpace
import Juvix.Compiler.Concrete.Data.ScopedName qualified as S
import Juvix.Compiler.Concrete.Language
import Juvix.Compiler.Store.Scoped.Language
import Juvix.Prelude

newtype SymbolInfo (n :: NameSpace) = SymbolInfo
Expand All @@ -28,7 +29,7 @@ data Scope = Scope
-- several imports under the same name. E.g.
-- import A as X;
-- import B as X;
_scopeTopModules :: HashMap TopModulePath (HashMap S.NameId (ModuleRef'' 'S.NotConcrete 'ModuleTop)),
_scopeTopModules :: HashMap TopModulePath (HashMap S.NameId ScopedModule),
-- | Symbols that have been defined in the current scope level. Every symbol
-- should map to itself. This is needed because we may query it with a
-- symbol with a different location but we may want the location of the
Expand All @@ -39,13 +40,13 @@ data Scope = Scope
}

newtype ModulesCache = ModulesCache
{ _cachedModules :: HashMap TopModulePath (ModuleRef'' 'S.NotConcrete 'ModuleTop)
{ _cachedModules :: HashMap TopModulePath ScopedModule
}

data ScopeParameters = ScopeParameters
{ -- | Used for import cycle detection.
_scopeTopParents :: [Import 'Parsed],
_scopeParsedModules :: HashMap TopModulePath (Module 'Parsed 'ModuleTop)
_scopeParsedModules :: HashMap TopModulePath ScopedModule
}

data RecordInfo = RecordInfo
Expand All @@ -56,7 +57,7 @@ data RecordInfo = RecordInfo
data ScoperState = ScoperState
{ _scoperModulesCache :: ModulesCache,
-- | Local and top modules
_scoperModules :: HashMap S.ModuleNameId (ModuleRef' 'S.NotConcrete),
_scoperModules :: HashMap S.ModuleNameId ScopedModuleRef,
_scoperScope :: HashMap TopModulePath Scope,
_scoperAlias :: HashMap S.NameId PreSymbolEntry,
_scoperSignatures :: HashMap S.NameId (NameSignature 'Parsed),
Expand Down
4 changes: 2 additions & 2 deletions src/Juvix/Compiler/Concrete/Data/ScopedName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ topModulePathSymbol = over nameConcrete (^. C.modulePathName)
topModulePathName :: TopModulePath -> Name
topModulePathName = over nameConcrete C.topModulePathToName

unConcrete :: Name' a -> Name' ()
unConcrete = set nameConcrete ()
unConcrete :: Name' a -> Name' (Maybe C.Name)
unConcrete = set nameConcrete Nothing

symbolText :: Symbol -> Text
symbolText s = s ^. nameConcrete . C.symbolText
Expand Down
42 changes: 2 additions & 40 deletions src/Juvix/Compiler/Concrete/Extra.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Juvix.Compiler.Concrete.Extra
( module Juvix.Prelude.Parsing,
mkScopedModule,
getAllModules,
getModuleFilePath,
unfoldApplication,
groupStatements,
Expand All @@ -14,49 +12,14 @@ module Juvix.Compiler.Concrete.Extra
)
where

import Data.HashMap.Strict qualified as HashMap
import Data.IntMap.Strict qualified as IntMap
import Data.List.NonEmpty qualified as NonEmpty
import Juvix.Compiler.Concrete.Data.ScopedName qualified as S
import Juvix.Compiler.Concrete.Language
import Juvix.Compiler.Store.Scoped.Language
import Juvix.Prelude hiding (some)
import Juvix.Prelude.Parsing

data ScopedModule = forall t. MkScopedModule (SModuleIsTop t) (Module 'Scoped t)

mkScopedModule :: forall t. (SingI t) => Module 'Scoped t -> ScopedModule
mkScopedModule = MkScopedModule sing

getAllModules :: Module 'Scoped 'ModuleTop -> HashMap S.NameId (Module 'Scoped 'ModuleTop)
getAllModules m = HashMap.fromList (fst (run (runOutputList (getAllModules' m))))

getAllModules' ::
forall r.
(Member (Output (S.NameId, Module 'Scoped 'ModuleTop)) r) =>
Module 'Scoped 'ModuleTop ->
Sem r ()
getAllModules' m = recordModule m
where
recordModule :: Module 'Scoped 'ModuleTop -> Sem r ()
recordModule n = do
output (n ^. modulePath . S.nameId, n)
processModule (mkScopedModule n)

processModule :: ScopedModule -> Sem r ()
processModule (MkScopedModule _ w) = forM_ (w ^. moduleBody) processStatement

processStatement :: Statement 'Scoped -> Sem r ()
processStatement = \case
StatementImport i -> recordModule (i ^. importModule . moduleRefModule)
StatementModule n -> processModule (mkScopedModule n)
StatementOpenModule n -> forM_ (getModuleRefTopModule (n ^. openModuleName)) recordModule
_ -> return ()

getModuleRefTopModule :: ModuleRef' c -> Maybe (Module 'Scoped 'ModuleTop)
getModuleRefTopModule (ModuleRef' (isTop :&: ModuleRef'' {..})) = case isTop of
SModuleLocal -> Nothing
SModuleTop -> Just _moduleRefModule

getModuleFilePath :: Module s 'ModuleTop -> Path Abs File
getModuleFilePath m = getLoc (m ^. moduleKw) ^. intervalFile

Expand Down Expand Up @@ -95,8 +58,7 @@ groupStatements = \case
SScoped ->
i
^. importModule
. moduleRefModule
. modulePath
. scopedModulePath
. S.nameId
== getModuleRefNameId (o ^. openModuleName)
(StatementImport _, _) -> False
Expand Down
Loading

0 comments on commit 654f60b

Please sign in to comment.