Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REPL option to apply Core transformations #1796

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions app/Commands/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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
import Juvix.Compiler.Core.Pretty qualified as Core
import Juvix.Compiler.Core.Transformation qualified as Core
import Juvix.Compiler.Core.Translation.FromInternal.Data qualified as Core
import Juvix.Compiler.Internal.Language qualified as Internal
import Juvix.Compiler.Internal.Pretty qualified as Internal
Expand Down Expand Up @@ -170,14 +171,20 @@ runCommand opts = do
defaultLoc = singletonInterval (mkInitialLoc replPath)

compileThenEval :: ReplContext -> String -> Repl (Either JuvixError Core.Node)
compileThenEval ctx s = bindEither compileString eval
compileThenEval ctx s = bindEither (fmap transformNode' <$> compileString) eval
where
eval :: Core.Node -> Repl (Either JuvixError Core.Node)
eval n =
liftIO $
mapLeft
(JuvixError @Core.CoreError)
<$> doEvalIO False defaultLoc (ctx ^. replContextExpContext . contextCoreResult . Core.coreResultTable) n
<$> doEvalIO False defaultLoc infoTable n

infoTable :: Core.InfoTable
infoTable = ctx ^. replContextExpContext . contextCoreResult . Core.coreResultTable

transformNode' :: Core.Node -> Core.Node
transformNode' = transformNode infoTable (opts ^. replTransformations)

compileString :: Repl (Either JuvixError Core.Node)
compileString = liftIO $ compileExpressionIO' ctx (strip (pack s))
Expand Down
5 changes: 4 additions & 1 deletion app/Commands/Repl/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ module Commands.Repl.Options where

import CommonOptions
import Juvix.Compiler.Core.Pretty.Options qualified as Core
import Juvix.Compiler.Core.Transformation

data ReplOptions = ReplOptions
{ _replInputFile :: Maybe (AppPath File),
_replShowDeBruijn :: Bool,
_replNoPrelude :: Bool
_replNoPrelude :: Bool,
_replTransformations :: [TransformationId]
}
deriving stock (Data)

Expand All @@ -20,6 +22,7 @@ instance CanonicalProjection ReplOptions Core.Options where

parseRepl :: Parser ReplOptions
parseRepl = do
_replTransformations <- optTransformationIds
_replInputFile <- optional parseInputJuvixFile
_replShowDeBruijn <-
switch
Expand Down
14 changes: 13 additions & 1 deletion src/Juvix/Compiler/Pipeline/ExpressionContext.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
module Juvix.Compiler.Pipeline.ExpressionContext where

import Data.HashMap.Strict
import Juvix.Compiler.Abstract.Translation qualified as Abstract
import Juvix.Compiler.Concrete.Data.InfoTable qualified as Scoper
import Juvix.Compiler.Concrete.Data.Scope qualified as S
import Juvix.Compiler.Concrete.Data.ScopedName qualified as S
import Juvix.Compiler.Concrete.Language qualified as C
import Juvix.Compiler.Concrete.Translation.FromParsed qualified as Scoper
import Juvix.Compiler.Core qualified as Core
import Juvix.Compiler.Core.Data.InfoTableBuilder
import Juvix.Compiler.Core.Language
import Juvix.Compiler.Core.Transformation
import Juvix.Compiler.Internal qualified as Internal
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.ArityChecking.Data.Context qualified as InternalArity
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.TypeChecking.Data.Context qualified as InternalTyped
import Juvix.Prelude

data ExpressionContext = ExpressionContext
{ _contextInternalTypedResult :: InternalTyped.InternalTypedResult,
Expand Down Expand Up @@ -53,3 +56,12 @@ mainModuleScope e = fromJust (moduleScope e (mainModuleTopPath e))

mainModuleTopPath :: ExpressionContext -> C.TopModulePath
mainModuleTopPath = (^. contextScoperResult . Scoper.mainModule . C.modulePath . S.nameConcrete)

transformNode :: InfoTable -> [TransformationId] -> Node -> Node
transformNode tab ts n = snd (run (runInfoTableBuilder tab transformNode'))
where
transformNode' :: Member InfoTableBuilder r => Sem r Node
transformNode' = do
sym <- freshSymbol
registerIdentNode sym n
lookupDefault impossible sym . (^. identContext) . applyTransformations ts <$> getInfoTable
13 changes: 12 additions & 1 deletion tests/smoke/Commands/repl.smoke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,15 @@ tests:
stderr:
contains: |
evaluation error: failure: Enough
exit-status: 0
exit-status: 0

- name: repl-nat-to-int-transform
command:
- juvix
- repl
- -t nat-to-int
stdin: "1 + 2"
stdout:
contains: |
3
exit-status: 0