<commit_before>module Eval where

import qualified Data.Map as Map
import Data.List (foldl', null)
import Data.List.Split (splitWhen)
import Data.Maybe (fromJust, mapMaybe, isJust)
import Control.Monad.State
import Control.Monad.State.Lazy (StateT(..), runStateT, liftIO, modify, get, put)
import System.Exit (exitSuccess, exitFailure, exitWith, ExitCode(..))
import System.IO (hPutStr)
import System.Directory (doesPathExist)
import Control.Concurrent (forkIO)
import qualified Data.Map as Map
import Data.Maybe (fromJust, mapMaybe, isJust)
import Control.Monad
import Control.Exception
import Debug.Trace

import Parsing
import Emit
import Obj
import Types
import Infer
import Deftype
import ColorText
import Template
import Util
import Commands
import Expand
import Lookup
import Qualify

-- | Dynamic (REPL) evaluation of XObj:s (s-expressions)
eval :: Env -> XObj -> StateT Context IO (Either EvalError XObj)
eval env xobj =
  case obj xobj of
  --case obj (trace ("\nEval " ++ pretty xobj ++ ", obj: " ++ show (obj xobj)) xobj) of
    Lst _   -> evalList xobj
    Arr _   -> evalArray xobj
    Sym _ _ -> evalSymbol xobj
    _       -> return (Right xobj)

  where
    evalList :: XObj -> StateT Context IO (Either EvalError XObj)
    evalList listXObj@(XObj (Lst xobjs) i t) =
      case xobjs of
        [] ->
          return (Right xobj)

        [XObj (Sym (SymPath [] "quote") _) _ _, target] ->
          return (Right target)

        [XObj (Sym (SymPath [] "source-location") _) _ _] ->
          return (Right (XObj (Str (prettyInfoFromXObj listXObj)) i t))

        XObj Do _ _ : rest ->
          do evaledList <- fmap sequence (mapM (eval env) rest)
             case evaledList of
               Left e -> return (Left e)
               Right ok ->
                 case ok of
                   [] -> return (Left (EvalError "No forms in 'do' statement."))
                   _ -> return (Right (last ok))

        XObj (Sym (SymPath [] "list") _) _ _ : rest ->
          do evaledList <- fmap sequence (mapM (eval env) rest)
             return $ do okList <- evaledList
                         Right (XObj (Lst okList) i t)

        XObj (Sym (SymPath [] "array") _) _ _ : rest ->
          do evaledArray <- fmap sequence (mapM (eval env) rest)
             return $ do okEvaledArray <- evaledArray
                         Right (XObj (Arr okEvaledArray) i t)

        -- and, or, and not are defined here because they are expected to
        -- shortcircuit and because they would otherwise clash with the regular
        -- functions
        [XObj (Sym (SymPath [] "and") _) _ _, a, b] ->
          do evaledA <- eval env a
             evaledB <- eval env b
             return $ do okA <- evaledA
                         case okA of
                           XObj (Bol ab) _ _ ->
                             if ab
                               then do okB <- evaledB
                                       case okB of
                                         XObj (Bol bb) _ _ ->
                                           if bb then Right trueXObj else Right falseXObj
                                         _ ->
                                           Left (EvalError ("Can't perform logical operation (and) on " ++ pretty okB))
                               else Right falseXObj
                           _ ->
                             Left (EvalError ("Can't perform logical operation (and) on " ++ pretty okA))

        [XObj (Sym (SymPath [] "or") _) _ _, a, b] ->
          do evaledA <- eval env a
             evaledB <- eval env b
             return $ do okA <- evaledA
                         case okA of
                           XObj (Bol ab) _ _ ->
                             if ab
                               then Right trueXObj
                               else do okB <- evaledB
                                       case okB of
                                         XObj (Bol bb) _ _ ->
                                           if bb then Right trueXObj else Right falseXObj
                                         _ ->
                                           Left (EvalError ("Can't perform logical operation (and) on " ++ pretty okB))
                           _ ->
                             Left (EvalError ("Can't perform logical operation (and) on " ++ pretty okA))<commit_msg>Typo in dynamic `or` evaluation error<commit_after>module Eval where

import qualified Data.Map as Map
import Data.List (foldl', null)
import Data.List.Split (splitWhen)
import Data.Maybe (fromJust, mapMaybe, isJust)
import Control.Monad.State
import Control.Monad.State.Lazy (StateT(..), runStateT, liftIO, modify, get, put)
import System.Exit (exitSuccess, exitFailure, exitWith, ExitCode(..))
import System.IO (hPutStr)
import System.Directory (doesPathExist)
import Control.Concurrent (forkIO)
import qualified Data.Map as Map
import Data.Maybe (fromJust, mapMaybe, isJust)
import Control.Monad
import Control.Exception
import Debug.Trace

import Parsing
import Emit
import Obj
import Types
import Infer
import Deftype
import ColorText
import Template
import Util
import Commands
import Expand
import Lookup
import Qualify

-- | Dynamic (REPL) evaluation of XObj:s (s-expressions)
eval :: Env -> XObj -> StateT Context IO (Either EvalError XObj)
eval env xobj =
  case obj xobj of
  --case obj (trace ("\nEval " ++ pretty xobj ++ ", obj: " ++ show (obj xobj)) xobj) of
    Lst _   -> evalList xobj
    Arr _   -> evalArray xobj
    Sym _ _ -> evalSymbol xobj
    _       -> return (Right xobj)

  where
    evalList :: XObj -> StateT Context IO (Either EvalError XObj)
    evalList listXObj@(XObj (Lst xobjs) i t) =
      case xobjs of
        [] ->
          return (Right xobj)

        [XObj (Sym (SymPath [] "quote") _) _ _, target] ->
          return (Right target)

        [XObj (Sym (SymPath [] "source-location") _) _ _] ->
          return (Right (XObj (Str (prettyInfoFromXObj listXObj)) i t))

        XObj Do _ _ : rest ->
          do evaledList <- fmap sequence (mapM (eval env) rest)
             case evaledList of
               Left e -> return (Left e)
               Right ok ->
                 case ok of
                   [] -> return (Left (EvalError "No forms in 'do' statement."))
                   _ -> return (Right (last ok))

        XObj (Sym (SymPath [] "list") _) _ _ : rest ->
          do evaledList <- fmap sequence (mapM (eval env) rest)
             return $ do okList <- evaledList
                         Right (XObj (Lst okList) i t)

        XObj (Sym (SymPath [] "array") _) _ _ : rest ->
          do evaledArray <- fmap sequence (mapM (eval env) rest)
             return $ do okEvaledArray <- evaledArray
                         Right (XObj (Arr okEvaledArray) i t)

        -- and, or, and not are defined here because they are expected to
        -- shortcircuit and because they would otherwise clash with the regular
        -- functions
        [XObj (Sym (SymPath [] "and") _) _ _, a, b] ->
          do evaledA <- eval env a
             evaledB <- eval env b
             return $ do okA <- evaledA
                         case okA of
                           XObj (Bol ab) _ _ ->
                             if ab
                               then do okB <- evaledB
                                       case okB of
                                         XObj (Bol bb) _ _ ->
                                           if bb then Right trueXObj else Right falseXObj
                                         _ ->
                                           Left (EvalError ("Can't perform logical operation (and) on " ++ pretty okB))
                               else Right falseXObj
                           _ ->
                             Left (EvalError ("Can't perform logical operation (and) on " ++ pretty okA))

        [XObj (Sym (SymPath [] "or") _) _ _, a, b] ->
          do evaledA <- eval env a
             evaledB <- eval env b
             return $ do okA <- evaledA
                         case okA of
                           XObj (Bol ab) _ _ ->
                             if ab
                               then Right trueXObj
                               else do okB <- evaledB
                                       case okB of
                                         XObj (Bol bb) _ _ ->
                                           if bb then Right trueXObj else Right falseXObj
                                         _ ->
                                           Left (EvalError ("Can't perform logical operation (or) on " ++ pretty okB))
                           _ ->
                             Left (EvalError ("Can't perform logical operation (or) on " ++ pretty okA))
{'fraction_non_alphanumeric': 0.08449417675268327, 'fraction_numerical': 0.0, 'mean_word_length': 1.338227442605446, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6736060', 'n_tokens_mistral': 2323, 'n_tokens_neox': 2230, 'n_words': 834}