<commit_before>-----------------------------------------------------------------------------
-- |
-- Module      : Berp.Interpreter.Repl
-- Copyright   : (c) 2010 <NAME>
-- License     : BSD-style
-- Maintainer  : <EMAIL>
-- Stability   : experimental
-- Portability : ghc
--
-- The Read Eval Print Loop (REPL) of the interpreter.
--
-----------------------------------------------------------------------------

module Berp.Interpreter.Repl (repl) where

import MonadUtils ()
import HscTypes (liftGhcT)
import Control.Monad.Trans (lift)
import GHC
   ( defaultErrorHandler, getSessionDynFlags, setSessionDynFlags
   , findModule, mkModuleName, setContext, SingleStep (RunToCompletion)
   , runStmt, gcatch, RunResult (..), LoadHowMuch (..), guessTarget, setTargets, load
   , DynFlags (..)
   )
import Linker (linkPackages, initDynLinker)
import DynFlags (PackageFlag (..))
import Control.Monad (when)
import Control.Exception.Extensible (SomeException (..))
import GHC.Paths (libdir)
import DynFlags (defaultDynFlags)
import System.IO (hSetBuffering, stdout, BufferMode (..))
import Language.Python.Version3.Parser (parseStmt)
-- import Language.Python.Common.PrettyParseError 
import Language.Python.Common.Pretty (prettyText)
import Language.Python.Common.AST (StatementSpan)
import Language.Haskell.Exts.Pretty 
   ( prettyPrintStyleMode, defaultMode, style, Style (..), PPHsMode (..)
   , Mode (..), PPLayout (PPSemiColon))
import Language.Haskell.Exts.Build (app, qualStmt) 
import Language.Haskell.Exts.Syntax (Stmt) 
import Berp.Version (versionString)
import Berp.Compile.Compile (compile)
import Berp.Compile.PrimName as Prim (interpretStmt, init)
import Berp.Compile.PySyntaxUtils (InterpreterStmt (..))
import Berp.Interpreter.Monad (Repl, runRepl)
import Berp.Interpreter.Input (getInputLines)
import Berp.Base.LiftedIO (liftIO)
 
repl :: IO ()
repl = do
    hSetBuffering stdout NoBuffering
    greeting
    defaultErrorHandler defaultDynFlags $ do
      runRepl (Just libdir) $ do
         dflags <- getSessionDynFlags
         setSessionDynFlags dflags

         -- dynFlags <- getSessionDynFlags
{-
         let oldPackageFlags = packageFlags dynFlags 
             newDynFlags = dynFlags { packageFlags = ExposePackage "berp" : oldPackageFlags }
-}
         -- packageIds <- setSessionDynFlags newDynFlags  
         -- latestDynFlags <- getSessionDynFlags
         -- liftIO $ initDynLinker newDynFlags
         -- liftIO $ linkPackages latestDynFlags packageIds 
         -- target <- guessTarget "Berp.Base" Nothing
         -- setTargets [target]
         -- prel_mod <- findModule (mkModuleName "Prelude") Nothing
         berp_base_mod <- findModule (mkModuleName "Berp.Base") Nothing
         -- setContext [] [prel_mod, berp_base_mod]
         setContext [] [berp_base_mod]
         -- load LoadAllTargets
         -- load (LoadUpTo $ mkModuleName "Berp.Base")
         replLoop

greeting :: IO ()
greeting = putStrLn $ "Berp version " ++ versionString ++ ", type control-d to exit."

replLoop :: Repl ()
replLoop = do
   maybeInput <- getInputLines
   case maybeInput of 
      Nothing -> return () 
      Just input -> do
         when (not $ null input) $ do
            pyStmts <- liftIO $ parseAndCheckErrors (input ++ "\n")
            when (not $ null pyStmts) $ do
               stmts <- liftGhcT $ lift $ compile $ InterpreterStmt pyStmts
               let finalStmt = qualStmt (app Prim.interpretStmt Prim.init)
               let stmtStrs = map oneLinePrinter (stmts ++ [finalStmt])
               -- liftIO $ mapM_ putStrLn stmtStrs
               mapM_ runAndCatch stmtStrs
         replLoop<commit_msg>Added kludge to get ghc api to link depencies before we show the first prompt of the repl.<commit_after>-----------------------------------------------------------------------------
-- |
-- Module      : Berp.Interpreter.Repl
-- Copyright   : (c) 2010 <NAME>
-- License     : BSD-style
-- Maintainer  : <EMAIL>
-- Stability   : experimental
-- Portability : ghc
--
-- The Read Eval Print Loop (REPL) of the interpreter.
--
-----------------------------------------------------------------------------

module Berp.Interpreter.Repl (repl) where

import MonadUtils ()
import HscTypes (liftGhcT)
import Control.Monad.Trans (lift)
import GHC
   ( defaultErrorHandler, getSessionDynFlags, setSessionDynFlags
   , findModule, mkModuleName, setContext, SingleStep (RunToCompletion)
   , runStmt, gcatch, RunResult (..)
   )
import Control.Monad (when)
import Control.Exception.Extensible (SomeException (..))
import GHC.Paths (libdir)
import DynFlags (defaultDynFlags)
import System.IO (hSetBuffering, stdout, BufferMode (..))
import Language.Python.Version3.Parser (parseStmt)
-- import Language.Python.Common.PrettyParseError 
import Language.Python.Common.Pretty (prettyText)
import Language.Python.Common.AST (StatementSpan)
import Language.Haskell.Exts.Pretty 
   ( prettyPrintStyleMode, defaultMode, style, Style (..), PPHsMode (..)
   , Mode (..), PPLayout (PPSemiColon))
import Language.Haskell.Exts.Build (app, qualStmt) 
import Language.Haskell.Exts.Syntax (Stmt) 
import Berp.Version (versionString)
import Berp.Compile.Compile (compile)
import Berp.Compile.PrimName as Prim (interpretStmt, init)
import Berp.Compile.PySyntaxUtils (InterpreterStmt (..))
import Berp.Interpreter.Monad (Repl, runRepl)
import Berp.Interpreter.Input (getInputLines)
import Berp.Base.LiftedIO (liftIO)
 
repl :: IO ()
repl = do
    hSetBuffering stdout NoBuffering
    greeting
    defaultErrorHandler defaultDynFlags $ do
      runRepl (Just libdir) $ do
         dflags <- getSessionDynFlags
         setSessionDynFlags dflags
         berp_base_mod <- findModule (mkModuleName "Berp.Base") Nothing
         setContext [] [berp_base_mod]
         -- kludge to get ghc to link depencies at the start, rather
         -- than when the user types the first command.
         evalInput "None"
         replLoop

greeting :: IO ()
greeting = putStrLn $ "Berp version " ++ versionString ++ ", type control-d to exit."

replLoop :: Repl ()
replLoop = do
   maybeInput <- getInputLines
   case maybeInput of 
      Nothing -> return () 
      Just input -> evalInput input >> replLoop

evalInput :: String -> Repl ()
evalInput input =
   when (not $ null input) $ do
      pyStmts <- liftIO $ parseAndCheckErrors (input ++ "\n")
      when (not $ null pyStmts) $ do
         stmts <- liftGhcT $ lift $ compile $ InterpreterStmt pyStmts
         let finalStmt = qualStmt (app Prim.interpretStmt Prim.init)
         let stmtStrs = map oneLinePrinter (stmts ++ [finalStmt])
         -- liftIO $ mapM_ putStrLn stmtStrs
         mapM_ runAndCatch stmtStrs
{'fraction_non_alphanumeric': 0.14446276037764125, 'fraction_numerical': 0.0014985763524651581, 'mean_word_length': 2.631120783460283, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '942554', 'n_tokens_mistral': 2041, 'n_tokens_neox': 2000, 'n_words': 523}