2023-03-14 18:24:07 +03:00
|
|
|
module Commands.Eval where
|
|
|
|
|
|
|
|
import Commands.Base
|
|
|
|
import Commands.Eval.Options
|
|
|
|
import Evaluator qualified as Eval
|
|
|
|
import Juvix.Compiler.Core qualified as Core
|
2023-06-06 14:26:43 +03:00
|
|
|
import Juvix.Extra.Strings qualified as Str
|
2023-03-14 18:24:07 +03:00
|
|
|
|
2023-12-06 20:24:59 +03:00
|
|
|
runCommand :: (Members '[Embed IO, TaggedLock, App] r) => EvalOptions -> Sem r ()
|
2023-03-14 18:24:07 +03:00
|
|
|
runCommand opts@EvalOptions {..} = do
|
2023-03-27 11:42:27 +03:00
|
|
|
gopts <- askGlobalOptions
|
2023-03-14 18:24:07 +03:00
|
|
|
Core.CoreResult {..} <- runPipeline _evalInputFile upToCore
|
2023-03-27 11:42:27 +03:00
|
|
|
let r =
|
|
|
|
run $
|
|
|
|
runReader (project gopts) $
|
|
|
|
runError @JuvixError $
|
|
|
|
(Core.toEval' _coreResultTable :: Sem '[Error JuvixError, Reader Core.CoreOptions] Core.InfoTable)
|
2023-03-20 12:13:07 +03:00
|
|
|
tab <- getRight r
|
2023-06-06 14:26:43 +03:00
|
|
|
let mevalNode =
|
2023-03-14 18:24:07 +03:00
|
|
|
if
|
2023-06-06 14:26:43 +03:00
|
|
|
| isJust _evalSymbolName -> getNode tab (selInfo tab)
|
|
|
|
| otherwise -> getNode tab (mainInfo tab)
|
|
|
|
case mevalNode of
|
|
|
|
Just evalNode ->
|
|
|
|
Eval.evalAndPrint opts tab evalNode
|
|
|
|
Nothing -> do
|
|
|
|
let name = fromMaybe Str.main _evalSymbolName
|
|
|
|
printFailureExit ("function not found: " <> name)
|
2023-03-14 18:24:07 +03:00
|
|
|
where
|
|
|
|
mainInfo :: Core.InfoTable -> Maybe Core.IdentifierInfo
|
|
|
|
mainInfo tab = do
|
|
|
|
s <- tab ^. Core.infoMain
|
|
|
|
tab ^. Core.infoIdentifiers . at s
|
|
|
|
|
|
|
|
selInfo :: Core.InfoTable -> Maybe Core.IdentifierInfo
|
|
|
|
selInfo tab = do
|
|
|
|
s <- _evalSymbolName
|
|
|
|
find (^. Core.identifierName . to (== s)) (tab ^. Core.infoIdentifiers)
|
|
|
|
|
|
|
|
getNode :: Core.InfoTable -> Maybe Core.IdentifierInfo -> Maybe Core.Node
|
|
|
|
getNode tab m = m >>= \i -> tab ^. Core.identContext . at (i ^. Core.identifierSymbol)
|