1
1
mirror of https://github.com/github/semantic.git synced 2024-12-19 12:51:52 +03:00

The Reader module table holds Modules.

This commit is contained in:
Rob Rix 2018-03-21 19:30:51 -04:00
parent 9d1cf41d63
commit 1002395074
3 changed files with 12 additions and 8 deletions

View File

@ -13,6 +13,7 @@ import Control.Monad.Effect.State
import Data.Abstract.Configuration
import qualified Data.Abstract.Environment as Env
import Data.Abstract.Evaluatable
import Data.Abstract.Module
import Data.Abstract.ModuleTable
import Data.Abstract.Value
import Data.Blob
@ -56,10 +57,11 @@ evaluates pairs (b, t) = runAnalysis @(Evaluating term value) (withModules b pai
-- | Run an action with the passed ('Blob', @term@) pairs available for imports.
withModules :: MonadAnalysis term value m => Blob -> [(Blob, term)] -> m a -> m a
withModules Blob{..} pairs = localModuleTable (const moduleTable)
withModules blob pairs = localModuleTable (const moduleTable)
where
moduleTable = ModuleTable (Map.fromListWith (<>) (map (bimap moduleName pure) pairs))
rootDir = dropFileName blobPath
moduleTable = ModuleTable (Map.fromListWith (<>) (map toModulePair pairs))
rootDir = dropFileName (blobPath blob)
toModulePair (blob, term) = let name = moduleName blob in (name, [Module name (blobPath blob) term])
moduleName Blob{..} = let path = dropExtensions (makeRelative rootDir blobPath)
in case blobLanguage of
-- TODO: Need a better way to handle module registration and resolution
@ -81,7 +83,7 @@ type EvaluatingEffects term value
= '[ Fail -- Failure with an error message
, State (EnvironmentFor value) -- Environments (both local and global)
, State (HeapFor value) -- The heap
, Reader (ModuleTable [term]) -- Cache of unevaluated modules
, Reader (ModuleTable [Module term]) -- Cache of unevaluated modules
, State (ModuleTable (EnvironmentFor value)) -- Cache of evaluated modules
, State (ExportsFor value) -- Exports (used to filter environments when they are imported)
, State (IntMap.IntMap term) -- For jumps
@ -114,7 +116,7 @@ instance Member (State (HeapFor value)) effects => MonadHeap value (Evaluating t
getHeap = raise get
putHeap = raise . put
instance Members '[Reader (ModuleTable [term]), State (ModuleTable (EnvironmentFor value))] effects => MonadModuleTable term value (Evaluating term value effects) where
instance Members '[Reader (ModuleTable [Module term]), State (ModuleTable (EnvironmentFor value))] effects => MonadModuleTable term value (Evaluating term value effects) where
getModuleTable = raise get
putModuleTable = raise . put

View File

@ -24,6 +24,7 @@ import Data.Abstract.Environment (Environment)
import qualified Data.Abstract.Environment as Env
import Data.Abstract.Exports (Exports)
import qualified Data.Abstract.Exports as Export
import Data.Abstract.Module
import Data.Abstract.ModuleTable
import Data.Abstract.Value
import Data.Coerce
@ -73,7 +74,7 @@ load :: ( MonadAnalysis term value m
)
=> ModuleName
-> m (EnvironmentFor value)
load name = askModuleTable >>= maybe notFound evalAndCache . moduleTableLookup name
load name = askModuleTable >>= maybe notFound (evalAndCache . map moduleBody) . moduleTableLookup name
where
notFound = fail ("cannot load module: " <> show name)
evalAndCache :: (MonadAnalysis term value m, Ord (LocationFor value)) => [term] -> m (EnvironmentFor value)

View File

@ -21,6 +21,7 @@ import qualified Data.Abstract.Environment as Env
import qualified Data.Abstract.Exports as Export
import Data.Abstract.FreeVariables
import Data.Abstract.Heap
import Data.Abstract.Module
import Data.Abstract.ModuleTable
import Data.Abstract.Value
import Data.Semigroup.Reducer
@ -128,9 +129,9 @@ class Monad m => MonadModuleTable term value m | m -> term, m -> value where
putModuleTable :: ModuleTable (EnvironmentFor value) -> m ()
-- | Retrieve the table of unevaluated modules.
askModuleTable :: m (ModuleTable [term])
askModuleTable :: m (ModuleTable [Module term])
-- | Run an action with a locally-modified table of unevaluated modules.
localModuleTable :: (ModuleTable [term] -> ModuleTable [term]) -> m a -> m a
localModuleTable :: (ModuleTable [Module term] -> ModuleTable [Module term]) -> m a -> m a
-- | Update the evaluated module table.
modifyModuleTable :: MonadModuleTable term value m => (ModuleTable (EnvironmentFor value) -> ModuleTable (EnvironmentFor value)) -> m ()