2022-11-07 16:47:56 +03:00
|
|
|
module Evaluator where
|
|
|
|
|
|
|
|
import App
|
|
|
|
import CommonOptions
|
2023-12-30 22:15:35 +03:00
|
|
|
import Juvix.Compiler.Core 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
|
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
|
|
|
|
|
|
|
|
evalAndPrint ::
|
|
|
|
forall r a.
|
2024-02-13 21:00:01 +03:00
|
|
|
(Members '[EmbedIO, App] r, CanonicalProjection a EvalOptions, CanonicalProjection a Core.Options) =>
|
2022-11-07 16:47:56 +03:00
|
|
|
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
|
2023-12-30 22:15:35 +03:00
|
|
|
node'' = if project opts ^. evalNoDisambiguate then node' else Core.disambiguateNodeNames (Core.moduleFromInfoTable 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.
|
2024-02-13 21:00:01 +03:00
|
|
|
(Members '[EmbedIO, App] r, CanonicalProjection a EvalOptions, CanonicalProjection a Core.Options) =>
|
2023-05-15 19:01:40 +03:00
|
|
|
a ->
|
|
|
|
Core.InfoTable ->
|
|
|
|
Core.Node ->
|
|
|
|
Sem r ()
|
|
|
|
normalizeAndPrint opts tab node =
|
2023-12-30 22:15:35 +03:00
|
|
|
let node' = normalize (Core.moduleFromInfoTable tab) node
|
2023-05-15 19:01:40 +03:00
|
|
|
in if
|
|
|
|
| Info.member Info.kNoDisplayInfo (Core.getInfo node') ->
|
|
|
|
return ()
|
|
|
|
| otherwise -> do
|
2023-12-30 22:15:35 +03:00
|
|
|
let node'' = if project opts ^. evalNoDisambiguate then node' else Core.disambiguateNodeNames (Core.moduleFromInfoTable tab) node'
|
2023-05-15 19:01:40 +03:00
|
|
|
renderStdOut (Core.ppOut opts node'')
|
2024-01-16 19:22:10 +03:00
|
|
|
putStrLn ""
|