2022-11-07 16:47:56 +03:00
|
|
|
module Evaluator where
|
|
|
|
|
|
|
|
import App
|
|
|
|
import CommonOptions
|
|
|
|
import Juvix.Compiler.Core.Data.InfoTable qualified as Core
|
|
|
|
import Juvix.Compiler.Core.Error qualified as Core
|
|
|
|
import Juvix.Compiler.Core.Evaluator qualified as Core
|
|
|
|
import Juvix.Compiler.Core.Extra.Base qualified as Core
|
2023-06-06 13:35:01 +03:00
|
|
|
import Juvix.Compiler.Core.Extra.Value qualified as Core
|
2022-11-07 16:47:56 +03:00
|
|
|
import Juvix.Compiler.Core.Info qualified as Info
|
|
|
|
import Juvix.Compiler.Core.Info.NoDisplayInfo qualified as Info
|
|
|
|
import Juvix.Compiler.Core.Language qualified as Core
|
2023-05-15 19:01:40 +03:00
|
|
|
import Juvix.Compiler.Core.Normalizer
|
2022-11-07 16:47:56 +03:00
|
|
|
import Juvix.Compiler.Core.Pretty qualified as Core
|
2023-03-15 18:41:39 +03:00
|
|
|
import Juvix.Compiler.Core.Transformation.DisambiguateNames qualified as Core
|
2022-11-07 16:47:56 +03:00
|
|
|
|
|
|
|
data EvalOptions = EvalOptions
|
2022-12-20 15:05:40 +03:00
|
|
|
{ _evalInputFile :: AppPath File,
|
2023-03-15 18:41:39 +03:00
|
|
|
_evalNoIO :: Bool,
|
2023-06-06 13:35:01 +03:00
|
|
|
_evalNoDisambiguate :: Bool,
|
|
|
|
_evalPrintValues :: Bool
|
2022-11-07 16:47:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
makeLenses ''EvalOptions
|
|
|
|
|
|
|
|
doEvalIO ::
|
|
|
|
Bool ->
|
|
|
|
Interval ->
|
|
|
|
Core.InfoTable ->
|
|
|
|
Core.Node ->
|
|
|
|
IO (Either Core.CoreError Core.Node)
|
2023-10-27 14:35:20 +03:00
|
|
|
doEvalIO noIO i tab node = runM (Core.doEval noIO i tab node)
|
2022-11-07 16:47:56 +03:00
|
|
|
|
|
|
|
evalAndPrint ::
|
|
|
|
forall r a.
|
|
|
|
(Members '[Embed IO, App] r, CanonicalProjection a EvalOptions, CanonicalProjection a Core.Options) =>
|
|
|
|
a ->
|
|
|
|
Core.InfoTable ->
|
|
|
|
Core.Node ->
|
|
|
|
Sem r ()
|
|
|
|
evalAndPrint opts tab node = do
|
2023-01-26 12:14:06 +03:00
|
|
|
loc <- defaultLoc
|
2023-10-27 14:35:20 +03:00
|
|
|
r <- Core.doEval (project opts ^. evalNoIO) loc tab node
|
2022-11-07 16:47:56 +03:00
|
|
|
case r of
|
|
|
|
Left err -> exitJuvixError (JuvixError err)
|
|
|
|
Right node'
|
|
|
|
| Info.member Info.kNoDisplayInfo (Core.getInfo node') ->
|
|
|
|
return ()
|
2023-06-06 13:35:01 +03:00
|
|
|
Right node'
|
|
|
|
| project opts ^. evalPrintValues -> do
|
|
|
|
renderStdOut (Core.ppOut opts (Core.toValue tab node'))
|
|
|
|
newline
|
|
|
|
| otherwise -> do
|
|
|
|
renderStdOut (Core.ppOut opts node'')
|
|
|
|
newline
|
2023-03-15 18:41:39 +03:00
|
|
|
where
|
|
|
|
node'' = if project opts ^. evalNoDisambiguate then node' else Core.disambiguateNodeNames tab node'
|
2022-11-07 16:47:56 +03:00
|
|
|
where
|
2023-01-26 12:14:06 +03:00
|
|
|
defaultLoc :: Sem r Interval
|
2023-04-19 17:56:48 +03:00
|
|
|
defaultLoc = singletonInterval . mkInitialLoc <$> fromAppPathFile f
|
|
|
|
f :: AppPath File
|
|
|
|
f = project opts ^. evalInputFile
|
2023-05-15 19:01:40 +03:00
|
|
|
|
|
|
|
normalizeAndPrint ::
|
|
|
|
forall r a.
|
|
|
|
(Members '[Embed IO, App] r, CanonicalProjection a EvalOptions, CanonicalProjection a Core.Options) =>
|
|
|
|
a ->
|
|
|
|
Core.InfoTable ->
|
|
|
|
Core.Node ->
|
|
|
|
Sem r ()
|
|
|
|
normalizeAndPrint opts tab node =
|
|
|
|
let node' = normalize tab node
|
|
|
|
in if
|
|
|
|
| Info.member Info.kNoDisplayInfo (Core.getInfo node') ->
|
|
|
|
return ()
|
|
|
|
| otherwise -> do
|
|
|
|
let node'' = if project opts ^. evalNoDisambiguate then node' else Core.disambiguateNodeNames tab node'
|
|
|
|
renderStdOut (Core.ppOut opts node'')
|
|
|
|
embed (putStrLn "")
|