Skip to content

Commit

Permalink
fix repl scoping & evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Dec 20, 2023
1 parent cc34c77 commit 2ec39b9
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 57 deletions.
2 changes: 1 addition & 1 deletion app/Commands/Dev/Termination/CallGraph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Data.HashMap.Strict qualified as HashMap
import Juvix.Compiler.Internal.Pretty qualified as Internal
import Juvix.Compiler.Internal.Translation.FromConcrete.Data.Context qualified as Internal
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.Termination qualified as Termination
import Juvix.Compiler.Store.Language qualified as Stored
import Juvix.Compiler.Store.Extra qualified as Stored
import Juvix.Prelude.Pretty

runCommand :: (Members '[Embed IO, TaggedLock, App] r) => CallGraphOptions -> Sem r ()
Expand Down
1 change: 1 addition & 0 deletions src/Juvix/Compiler/Concrete/Translation/FromParsed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Juvix.Compiler.Concrete.Translation.FromParsed.Analysis.Scoping
import Juvix.Compiler.Concrete.Translation.FromParsed.Analysis.Scoping.Data.Context
import Juvix.Compiler.Concrete.Translation.FromSource.Data.Context qualified as Parsed
import Juvix.Compiler.Pipeline.EntryPoint
import Juvix.Compiler.Store.Extra
import Juvix.Compiler.Store.Language
import Juvix.Prelude

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,16 @@ scopeCheck' importTab pr m = do
ScopeParameters
{ _scopeImportedModules = importTab ^. scopedModuleTable
}
mkResult :: (ScoperState, (Module 'Scoped 'ModuleTop, ScopedModule)) -> ScoperResult
mkResult (scoperSt, (md, sm)) =
mkResult :: (ScoperState, (Module 'Scoped 'ModuleTop, ScopedModule, Scope)) -> ScoperResult
mkResult (scoperSt, (md, sm, sc)) =
let exp = createExportsTable (sm ^. scopedModuleExportInfo)
in ScoperResult
{ _resultParserResult = pr,
_resultModule = md,
_resultScopedModule = sm,
_resultExports = exp,
_resultScoperState = scoperSt
_resultScoperState = scoperSt,
_resultScope = sc
}

-- TODO refactor to have less code duplication
Expand Down Expand Up @@ -1033,7 +1034,7 @@ checkTopModule ::
forall r.
(Members '[Error ScoperError, Reader ScopeParameters, State ScoperState, Reader InfoTable, NameIdGen, Reader EntryPoint] r) =>
Module 'Parsed 'ModuleTop ->
Sem r (Module 'Scoped 'ModuleTop, ScopedModule)
Sem r (Module 'Scoped 'ModuleTop, ScopedModule, Scope)
checkTopModule m@Module {..} = checkedModule
where
freshTopModulePath ::
Expand Down Expand Up @@ -1061,9 +1062,9 @@ checkTopModule m@Module {..} = checkedModule
iniScope :: Scope
iniScope = emptyScope (getTopModulePath m)

checkedModule :: Sem r (Module 'Scoped 'ModuleTop, ScopedModule)
checkedModule :: Sem r (Module 'Scoped 'ModuleTop, ScopedModule, Scope)
checkedModule = do
(tab, (e, body', path', doc')) <- evalState iniScope $ runInfoTableBuilder mempty $ do
(sc, (tab, (e, body', path', doc'))) <- runState iniScope $ runInfoTableBuilder mempty $ do
path' <- freshTopModulePath
withTopScope $ do
(e, body') <- topBindings (checkModuleBody _moduleBody)
Expand Down Expand Up @@ -1093,7 +1094,7 @@ checkTopModule m@Module {..} = checkedModule
_scopedModuleLocalModules = localModules,
_scopedModuleInfoTable = tab
}
return (md, smd)
return (md, smd, sc)

withTopScope :: (Members '[State Scope] r) => Sem r a -> Sem r a
withTopScope ma = do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ data ScoperResult = ScoperResult
_resultModule :: Module 'Scoped 'ModuleTop,
_resultScopedModule :: ScopedModule,
_resultExports :: HashSet NameId,
_resultScoperState :: ScoperState
_resultScoperState :: ScoperState,
_resultScope :: Scope
}

makeLenses ''ScoperResult
Expand Down
4 changes: 2 additions & 2 deletions src/Juvix/Compiler/Core/Translation/FromInternal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Juvix.Compiler.Internal.Pretty (ppTrace)
import Juvix.Compiler.Internal.Pretty qualified as Internal
import Juvix.Compiler.Internal.Translation.Extra qualified as Internal
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.TypeChecking qualified as InternalTyped
import Juvix.Compiler.Store.Extra (computeCombinedCoreInfoTable)
import Juvix.Compiler.Store.Extra qualified as Store
import Juvix.Compiler.Store.Language qualified as Store
import Juvix.Data.Loc qualified as Loc
import Juvix.Data.PPOutput
Expand Down Expand Up @@ -50,7 +50,7 @@ mkIdentIndex = show . (^. Internal.nameId)
fromInternal :: (Members '[NameIdGen, Reader Store.ModuleTable] k) => Internal.InternalTypedResult -> Sem k CoreResult
fromInternal i = do
importTab <- asks Store.getInternalModuleTable
coreImportsTab <- asks computeCombinedCoreInfoTable
coreImportsTab <- asks Store.computeCombinedCoreInfoTable
let md =
Module
{ _moduleId = i ^. InternalTyped.resultInternalModule . Internal.internalModuleId,
Expand Down
1 change: 1 addition & 0 deletions src/Juvix/Compiler/Internal/Translation/FromInternal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.Termination.Che
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.TypeChecking
import Juvix.Compiler.Pipeline.Artifacts
import Juvix.Compiler.Pipeline.EntryPoint
import Juvix.Compiler.Store.Extra
import Juvix.Compiler.Store.Language
import Juvix.Data.Effect.NameIdGen
import Juvix.Prelude hiding (fromEither)
Expand Down
11 changes: 11 additions & 0 deletions src/Juvix/Compiler/Pipeline/Artifacts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ import Juvix.Compiler.Builtins
import Juvix.Compiler.Concrete.Data.InfoTableBuilder qualified as Scoped
import Juvix.Compiler.Concrete.Data.Scope qualified as S
import Juvix.Compiler.Core.Data.InfoTableBuilder qualified as Core
import Juvix.Compiler.Core.Data.Module qualified as Core
import Juvix.Compiler.Internal.Language qualified as Internal
import Juvix.Compiler.Internal.Translation.FromConcrete qualified as Internal
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.Termination.Checker
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.TypeChecking.Data.Context
import Juvix.Compiler.Pipeline.Artifacts.Base
import Juvix.Compiler.Store.Extra
import Juvix.Compiler.Store.Language
import Juvix.Prelude

appendArtifactsModuleTable :: ModuleTable -> Artifacts -> Artifacts
appendArtifactsModuleTable mtab =
over artifactInternalTypedTable (computeCombinedInfoTable importTab <>)
. over (artifactCoreModule . Core.moduleImportsTable) (computeCombinedCoreInfoTable mtab <>)
where
importTab :: Internal.InternalModuleTable
importTab = getInternalModuleTable mtab

-- | It only reads the Artifacts. It does not modify the table in it.
extendedTableReplArtifacts :: forall r. (Members '[State Artifacts] r) => Internal.Expression -> Sem r Internal.InfoTable
extendedTableReplArtifacts e = Internal.extendWithReplExpression e <$> gets (^. artifactInternalTypedTable)
Expand Down
1 change: 1 addition & 0 deletions src/Juvix/Compiler/Pipeline/Driver.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.TypeChecking.Da
import Juvix.Compiler.Pipeline
import Juvix.Compiler.Pipeline.Loader.PathResolver
import Juvix.Compiler.Store.Core.Extra
import Juvix.Compiler.Store.Extra qualified as Store
import Juvix.Compiler.Store.Language qualified as Store
import Juvix.Compiler.Store.Options qualified as StoredModule
import Juvix.Compiler.Store.Options qualified as StoredOptions
Expand Down
6 changes: 3 additions & 3 deletions src/Juvix/Compiler/Pipeline/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Juvix.Compiler.Pipeline.Loader.PathResolver.Error
import Juvix.Compiler.Pipeline.Package.Loader.Error
import Juvix.Compiler.Pipeline.Package.Loader.EvalEff.IO
import Juvix.Compiler.Pipeline.Result
import Juvix.Compiler.Store.Language qualified as Store
import Juvix.Compiler.Store.Extra qualified as Store
import Juvix.Data.Effect.Git
import Juvix.Data.Effect.Process (runProcessIO)
import Juvix.Data.Effect.TaggedLock
Expand Down Expand Up @@ -113,8 +113,8 @@ registerImport ::
registerImport i = do
e <- ask
PipelineResult mi mtab <- Driver.processImport e i
modify' (over artifactModuleTable (Store.insertModule (i ^. importModulePath) mi))
modify' (over artifactModuleTable (mtab <>))
let mtab' = Store.insertModule (i ^. importModulePath) mi mtab
modify' (appendArtifactsModuleTable mtab')

fromInternalExpression :: (Members '[State Artifacts] r) => Internal.Expression -> Sem r Core.Node
fromInternalExpression exp = do
Expand Down
35 changes: 18 additions & 17 deletions src/Juvix/Compiler/Pipeline/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -181,33 +181,34 @@ runReplPipelineIOEither' lockMode entry = do
resultScoperTable :: InfoTable
resultScoperTable = Scoped.getCombinedInfoTable (scopedResult ^. Scoped.resultScopedModule)
in Right $
Artifacts
{ _artifactMainModuleScope = Nothing,
_artifactParsing = parserResult ^. P.resultParserState,
_artifactInternalTypedTable = typedTable,
_artifactTerminationState = typedResult ^. Typed.resultTermination,
_artifactCoreModule = coreModule,
_artifactScopeTable = resultScoperTable,
_artifactScopeExports = scopedResult ^. Scoped.resultExports,
_artifactTypes = typesTable,
_artifactFunctions = functionsTable,
_artifactScoperState = scopedResult ^. Scoped.resultScoperState,
_artifactResolver = art ^. artifactResolver,
_artifactBuiltins = art ^. artifactBuiltins,
_artifactNameIdState = art ^. artifactNameIdState,
_artifactModuleTable = _pipelineResultImports
}
appendArtifactsModuleTable _pipelineResultImports $
Artifacts
{ _artifactMainModuleScope = Just $ scopedResult ^. Scoped.resultScope,
_artifactParsing = parserResult ^. P.resultParserState,
_artifactInternalTypedTable = typedTable,
_artifactTerminationState = typedResult ^. Typed.resultTermination,
_artifactCoreModule = coreModule,
_artifactScopeTable = resultScoperTable,
_artifactScopeExports = scopedResult ^. Scoped.resultExports,
_artifactTypes = typesTable,
_artifactFunctions = functionsTable,
_artifactScoperState = scopedResult ^. Scoped.resultScoperState,
_artifactResolver = art ^. artifactResolver,
_artifactBuiltins = art ^. artifactBuiltins,
_artifactNameIdState = art ^. artifactNameIdState,
_artifactModuleTable = mempty
}
where
initialArtifacts :: Artifacts
initialArtifacts =
Artifacts
{ _artifactParsing = mempty,
_artifactMainModuleScope = Nothing,
_artifactInternalTypedTable = mempty,
_artifactTypes = mempty,
_artifactTerminationState = iniTerminationState,
_artifactResolver = iniResolverState,
_artifactNameIdState = genNameIdState defaultModuleId,
_artifactTypes = mempty,
_artifactFunctions = mempty,
_artifactCoreModule = Core.emptyModule,
_artifactScopeTable = mempty,
Expand Down
28 changes: 28 additions & 0 deletions src/Juvix/Compiler/Store/Extra.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
module Juvix.Compiler.Store.Extra where

import Data.HashMap.Strict qualified as HashMap
import Juvix.Compiler.Concrete.Data.ScopedName qualified as S
import Juvix.Compiler.Concrete.Language (TopModulePath)
import Juvix.Compiler.Core.Data.InfoTable qualified as Core
import Juvix.Compiler.Store.Core.Extra
import Juvix.Compiler.Store.Internal.Language
import Juvix.Compiler.Store.Language
import Juvix.Compiler.Store.Scoped.Language
import Juvix.Prelude

getModulePath :: ModuleInfo -> TopModulePath
getModulePath mi = mi ^. moduleInfoScopedModule . scopedModulePath . S.nameConcrete

getModuleId :: ModuleInfo -> ModuleId
getModuleId mi = mi ^. moduleInfoScopedModule . scopedModuleId

getScopedModuleTable :: ModuleTable -> ScopedModuleTable
getScopedModuleTable mtab =
ScopedModuleTable $ fmap (^. moduleInfoScopedModule) (mtab ^. moduleTable)

getInternalModuleTable :: ModuleTable -> InternalModuleTable
getInternalModuleTable mtab =
InternalModuleTable $
HashMap.fromList (map (\mi -> (mi ^. moduleInfoInternalModule . internalModuleName, mi ^. moduleInfoInternalModule)) (HashMap.elems (mtab ^. moduleTable)))

mkModuleTable :: [ModuleInfo] -> ModuleTable
mkModuleTable = ModuleTable . HashMap.fromList . map (\mi -> (getModulePath mi, mi))

lookupModule :: ModuleTable -> TopModulePath -> ModuleInfo
lookupModule mtab n = fromJust $ HashMap.lookup n (mtab ^. moduleTable)

insertModule :: TopModulePath -> ModuleInfo -> ModuleTable -> ModuleTable
insertModule p mi = over moduleTable (HashMap.insert p mi)

computeCombinedCoreInfoTable :: ModuleTable -> Core.InfoTable
computeCombinedCoreInfoTable mtab =
mconcatMap (toCore . (^. moduleInfoCoreTable)) (HashMap.elems (mtab ^. moduleTable))
26 changes: 0 additions & 26 deletions src/Juvix/Compiler/Store/Language.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Juvix.Compiler.Store.Language where

import Data.HashMap.Strict qualified as HashMap
import Juvix.Compiler.Concrete.Data.ScopedName qualified as S
import Juvix.Compiler.Concrete.Language (TopModulePath)
import Juvix.Compiler.Store.Core.Data.InfoTable qualified as Core
import Juvix.Compiler.Store.Internal.Language
Expand Down Expand Up @@ -29,27 +27,3 @@ newtype ModuleTable = ModuleTable

makeLenses ''ModuleInfo
makeLenses ''ModuleTable

getModulePath :: ModuleInfo -> TopModulePath
getModulePath mi = mi ^. moduleInfoScopedModule . scopedModulePath . S.nameConcrete

getModuleId :: ModuleInfo -> ModuleId
getModuleId mi = mi ^. moduleInfoScopedModule . scopedModuleId

getScopedModuleTable :: ModuleTable -> ScopedModuleTable
getScopedModuleTable mtab =
ScopedModuleTable $ fmap (^. moduleInfoScopedModule) (mtab ^. moduleTable)

getInternalModuleTable :: ModuleTable -> InternalModuleTable
getInternalModuleTable mtab =
InternalModuleTable $
HashMap.fromList (map (\mi -> (mi ^. moduleInfoInternalModule . internalModuleName, mi ^. moduleInfoInternalModule)) (HashMap.elems (mtab ^. moduleTable)))

mkModuleTable :: [ModuleInfo] -> ModuleTable
mkModuleTable = ModuleTable . HashMap.fromList . map (\mi -> (getModulePath mi, mi))

lookupModule :: ModuleTable -> TopModulePath -> ModuleInfo
lookupModule mtab n = fromJust $ HashMap.lookup n (mtab ^. moduleTable)

insertModule :: TopModulePath -> ModuleInfo -> ModuleTable -> ModuleTable
insertModule p mi = over moduleTable (HashMap.insert p mi)

0 comments on commit 2ec39b9

Please sign in to comment.