Skip to content

Commit

Permalink
add Package cache
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Dec 5, 2023
1 parent 7fbc895 commit 1aec7e7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
32 changes: 24 additions & 8 deletions app/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Data.ByteString qualified as ByteString
import GlobalOptions
import Juvix.Compiler.Concrete.Translation.FromParsed.Analysis.PathResolver
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.Termination.Checker
import Juvix.Compiler.Pipeline.Package
import Juvix.Compiler.Pipeline.Run
import Juvix.Data.Error qualified as Error
import Juvix.Extra.Paths.Base hiding (rootBuildDir)
Expand Down Expand Up @@ -41,14 +42,26 @@ data RunAppIOArgs = RunAppIOArgs
_runAppIOArgsRoot :: Root
}

makeLenses ''RunAppIOArgs

runAppIO ::
forall r a.
(Members '[Embed IO, TaggedLock] r) =>
RunAppIOArgs ->
Sem (App ': r) a ->
Sem r a
runAppIO args@RunAppIOArgs {..} =
interpret $ \case
runAppIO args = evalSingletonCache (readPackageRootIO root) . reAppIO args
where
root = args ^. runAppIOArgsRoot

reAppIO ::
forall r a.
(Members '[Embed IO, TaggedLock] r) =>
RunAppIOArgs ->
Sem (App ': r) a ->
Sem (SCache Package ': r) a
reAppIO args@RunAppIOArgs {..} =
reinterpret $ \case
AskPackageGlobal -> return (_runAppIOArgsRoot ^. rootPackageType `elem` [GlobalStdlib, GlobalPackageDescription, GlobalPackageBase])
FromAppPathFile p -> embed (prepathToAbsFile invDir (p ^. pathPath))
GetMainFile m -> getMainFile' m
Expand Down Expand Up @@ -84,11 +97,13 @@ runAppIO args@RunAppIOArgs {..} =
ExitMsg exitCode t -> exitMsg' exitCode t
SayRaw b -> embed (ByteString.putStr b)
where
getPkg :: Sem r Package
getPkg = undefined
exitMsg' :: ExitCode -> Text -> Sem r x
exitMsg' exitCode t = embed (putStrLn t >> hFlush stdout >> exitWith exitCode)
getMainFile' :: Maybe (AppPath File) -> Sem r (Path Abs File)
getPkg :: (Members '[SCache Package] r') => Sem r' Package
getPkg = cacheSingletonGet

exitMsg' :: (Members '[Embed IO] r') => ExitCode -> Text -> Sem r' x
exitMsg' exitCode t = liftIO (putStrLn t >> hFlush stdout >> exitWith exitCode)

getMainFile' :: (Members '[SCache Package, Embed IO] r') => Maybe (AppPath File) -> Sem r' (Path Abs File)
getMainFile' = \case
Just p -> embed (prepathToAbsFile invDir (p ^. pathPath))
-- Nothing -> case pkg ^. packageMain of
Expand All @@ -97,7 +112,8 @@ runAppIO args@RunAppIOArgs {..} =
case pkg ^. packageMain of
Just p -> embed (prepathToAbsFile invDir p)
Nothing -> missingMainErr
missingMainErr :: Sem r x

missingMainErr :: (Members '[Embed IO] r') => Sem r' x
missingMainErr =
exitMsg'
(ExitFailure 1)
Expand Down
11 changes: 4 additions & 7 deletions src/Juvix/Compiler/Pipeline/Root.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import Control.Exception (SomeException)
import Control.Exception qualified as IO
import Juvix.Compiler.Pipeline.Package
import Juvix.Compiler.Pipeline.Root.Base
import Juvix.Data.Effect.TaggedLock
import Juvix.Extra.Paths qualified as Paths
import Juvix.Prelude

findRootAndChangeDir ::
forall r.
(Members '[Embed IO, TaggedLock, Final IO] r) =>
(Members '[Embed IO, Final IO] r) =>
Maybe (Path Abs Dir) ->
Maybe (Path Abs Dir) ->
Path Abs Dir ->
Expand Down Expand Up @@ -46,14 +45,13 @@ findRootAndChangeDir minputFileDir mbuildDir _rootInvokeDir = do
Nothing -> do
let cwd = fromMaybe _rootInvokeDir minputFileDir
packageBaseRootDir <- runFilesIO globalPackageBaseRoot
(_rootPackage, _rootRootDir, _rootPackageType) <-
(_rootRootDir, _rootPackageType) <-
if
| isPathPrefix packageBaseRootDir cwd ->
return (packageBasePackage, packageBaseRootDir, GlobalPackageBase)
return (packageBaseRootDir, GlobalPackageBase)
| otherwise -> do
globalPkg <- readGlobalPackageIO
r <- runFilesIO globalRoot
return (globalPkg, r, GlobalStdlib)
return (r, GlobalStdlib)
let _rootBuildDir = getBuildDir mbuildDir
return Root {..}
Just pkgPath -> do
Expand All @@ -63,7 +61,6 @@ findRootAndChangeDir minputFileDir mbuildDir _rootInvokeDir = do
| isPathPrefix packageDescriptionRootDir _rootRootDir = GlobalPackageDescription
| otherwise = LocalPackage
_rootBuildDir = getBuildDir mbuildDir
_rootPackage <- readPackageIO _rootRootDir _rootBuildDir
return Root {..}

getBuildDir :: Maybe (Path Abs Dir) -> BuildDir
Expand Down
16 changes: 16 additions & 0 deletions src/Juvix/Data/Effect/Cache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ module Juvix.Data.Effect.Cache
runCacheEmpty,
cacheGet,
cacheLookup,
evalSingletonCache,
cacheSingletonGet,
Cache,
SCache,
)
where

Expand All @@ -15,6 +18,9 @@ data Cache k v m a where
CacheGet :: k -> Cache k v m v
CacheLookup :: k -> Cache k v m (Maybe v)

-- | Singleton cache
type SCache = Cache ()

makeSem ''Cache

-- | Run a 'Cache' effect purely.
Expand Down Expand Up @@ -52,6 +58,16 @@ runCacheEmpty ::
runCacheEmpty f = runCache f mempty
{-# INLINE runCacheEmpty #-}

cacheSingletonGet :: (Members '[SCache v] r) => Sem r v
cacheSingletonGet = cacheGet ()

evalSingletonCache ::
Sem (SCache v ': r) v ->
Sem (SCache v ': r) a ->
Sem r a
evalSingletonCache f c = evalCacheEmpty @() (const f) c
{-# INLINE evalSingletonCache #-}

re ::
forall k v r a.
(Hashable k) =>
Expand Down

0 comments on commit 1aec7e7

Please sign in to comment.