<commit_before>{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE TupleSections #-}
module Main where

import System.Console.Haskeline

import Text.Parsec.Error (ParseError)
import Text.Parsec 

import Text.Parsec.Indent

import SIL.Parser
import SIL.RunTime
import SIL

import Control.Monad.IO.Class
import Data.List

import qualified Data.Map         as Map
import qualified System.IO.Strict as Strict

-- REPL parsing

addReplBound :: String -> Term1 -> ParserState -> ParserState
addReplBound name expr (ParserState bound) = ParserState (Map.insert name expr bound)

parseReplAssignment :: SILParser ()
parseReplAssignment = do
  var <- identifier
  annotation <- optionMaybe parseRefinementCheck
  reservedOp "=" <?> "assignment ="
  expr <- parseLongExpr
  let annoExp = case annotation of
        Just f -> f expr
        _ -> expr
      assign ps = addReplBound var annoExp ps 
  modifyState assign

parseReplExpr :: SILParser ()
parseReplExpr = do
  expr <- parseLongExpr
  let assign ps = addReplBound "_tmp_" expr ps 
  modifyState assign

data ReplStep = ReplAssignment
              | ReplExpr

parseReplStep :: SILParser (ReplStep, Bindings)
parseReplStep =  step >>= (\x -> (x,) <$> (bound <$> getState)) 
    where step = try (parseReplAssignment *> return ReplAssignment)
               <|>   (parseReplExpr       *> return ReplExpr)

runReplParser :: Bindings -> String -> Either ParseError (ReplStep, Bindings)
runReplParser prelude = let startState = ParserState prelude
                        in runIndentParser parseReplStep startState "sil"

-- Repl loop

data ReplState = ReplState Bindings

replStep :: Bindings -> String -> InputT IO Bindings
replStep bindings s = do
    let e_new_bindings = runReplParser bindings s
    case e_new_bindings of
        Left err -> do 
            outputStrLn $ "Parse error: " ++ show err
            return bindings
        Right (ReplExpr,new_bindings) -> do  
            case Map.lookup "_tmp_" new_bindings of
                Nothing -> outputStrLn "Could not find _tmp_ in bindings"
                Just e  -> do
                    iexpr <- convertPT <$> debruijinize [] e 
                    e_iexpr <- liftIO $ simpleEval iexpr
                    outputStrLn $ (show.PrettyIExpr) e_iexpr
            return bindings
        Right (ReplAssignment, new_bindings) -> do
            return new_bindings
            

replMultiline :: [String] -> InputT IO String
replMultiline buffer = do
    minput <- getInputLine ""
    case minput of
        Nothing -> return ""
        Just ":}" -> return $ concat $ intersperse "\n" $ reverse buffer
        Just s    -> replMultiline (s : buffer) 


replLoop :: ReplState -> InputT IO ()
replLoop (ReplState bs) = do 
    minput <- getInputLine "sil> "
    case minput of
        Nothing   -> return ()
        Just ":{" -> replLoop =<< (ReplState <$> (replStep bs =<< replMultiline [])) 
        Just s    -> replLoop =<< (ReplState <$> replStep bs s)

main = do
    e_prelude <- parsePrelude <$> Strict.readFile "Prelude.sil" 
    let bindings = case e_prelude of
            Left  _   -> Map.empty
            Right bds -> bds
    runInputT defaultSettings $ replLoop (ReplState bindings)
<commit_msg>Catching exceptions related to missing identifiers.<commit_after>{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE TupleSections #-}
module Main where

import System.Console.Haskeline

import Text.Parsec.Error (ParseError)
import Text.Parsec 

import Text.Parsec.Indent

import SIL.Parser
import SIL.RunTime
import SIL

import Control.Monad.IO.Class
import Data.List

import qualified Data.Map         as Map
import qualified System.IO.Strict as Strict

-- REPL parsing

addReplBound :: String -> Term1 -> ParserState -> ParserState
addReplBound name expr (ParserState bound) = ParserState (Map.insert name expr bound)

parseReplAssignment :: SILParser ()
parseReplAssignment = do
  var <- identifier
  annotation <- optionMaybe parseRefinementCheck
  reservedOp "=" <?> "assignment ="
  expr <- parseLongExpr
  let annoExp = case annotation of
        Just f -> f expr
        _ -> expr
      assign ps = addReplBound var annoExp ps 
  modifyState assign

parseReplExpr :: SILParser ()
parseReplExpr = do
  expr <- parseLongExpr
  let assign ps = addReplBound "_tmp_" expr ps 
  modifyState assign

data ReplStep = ReplAssignment
              | ReplExpr

parseReplStep :: SILParser (ReplStep, Bindings)
parseReplStep =  step >>= (\x -> (x,) <$> (bound <$> getState)) 
    where step = try (parseReplAssignment *> return ReplAssignment)
               <|>   (parseReplExpr       *> return ReplExpr)

runReplParser :: Bindings -> String -> Either ParseError (ReplStep, Bindings)
runReplParser prelude = let startState = ParserState prelude
                        in runIndentParser parseReplStep startState "sil"

-- Repl loop

data ReplState = ReplState Bindings

replStep :: Bindings -> String -> InputT IO Bindings
replStep bindings s = do
    let e_new_bindings = runReplParser bindings s
    case e_new_bindings of
        Left err -> do 
            outputStrLn $ "Parse error: " ++ show err
            return bindings
        Right (ReplExpr,new_bindings) -> do  
            case Map.lookup "_tmp_" new_bindings of
                Nothing -> outputStrLn "Could not find _tmp_ in bindings"
                Just e  -> do
                    let m_iexpr = convertPT <$> debruijinize [] e 
                    case m_iexpr of
                        Nothing     -> outputStrLn "conversion error"
                        Just iexpr' -> do
                            iexpr <- liftIO $ simpleEval iexpr' 
                            outputStrLn $ (show.PrettyIExpr) iexpr
            return bindings
        Right (ReplAssignment, new_bindings) -> do
            return new_bindings
            

replMultiline :: [String] -> InputT IO String
replMultiline buffer = do
    minput <- getInputLine ""
    case minput of
        Nothing -> return ""
        Just ":}" -> return $ concat $ intersperse "\n" $ reverse buffer
        Just s    -> replMultiline (s : buffer) 


replLoop :: ReplState -> InputT IO ()
replLoop (ReplState bs) = do 
    minput <- getInputLine "sil> "
    case minput of
        Nothing   -> return ()
        Just ":{" -> replLoop =<< (ReplState <$> (replStep bs =<< replMultiline [])) 
        Just s    -> replLoop =<< (ReplState <$> replStep bs s)

main = do
    e_prelude <- parsePrelude <$> Strict.readFile "Prelude.sil" 
    let bindings = case e_prelude of
            Left  _   -> Map.empty
            Right bds -> bds
    runInputT defaultSettings $ replLoop (ReplState bindings)

{'fraction_non_alphanumeric': 0.08834614804764059, 'fraction_numerical': 0.00030152268958239106, 'mean_word_length': 2.416065911431514, 'pattern_counts': {'":': 4, '<': 48, '<?xml version=': 0, '>': 81, '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': '1947978', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1995, 'n_words': 596}