{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings #-}

-- |
-- This module provides some helpful functions to assist in an interface for a significant figures calculator. There isn't much to see here.
module Data.SigFig.Interface where

import Data.SigFig.Evaluate
import Data.SigFig.Parse
import Data.SigFig.Types
import Data.SigFig.Util
import Data.Text (Text)
import Data.Text qualified as T

-- | Takes an expression in text and returns either an error message or an evaluated term.
parseEval :: Text -> Either Text Term
parseEval :: Text -> Either Text Term
parseEval Text
e = Text -> Either Text Expr
parse Text
e forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Expr -> Either Text Term
evaluate

-- | A convenience function for use in REPLs (used in the CLI). Returns text that can either signify a
-- result or error.
processExpression :: Text -> Text
processExpression :: Text -> Text
processExpression Text
e = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Text
"Error: " forall a. Semigroup a => a -> a -> a
<>) Term -> Text
displayFull forall a b. (a -> b) -> a -> b
$ Text -> Either Text Term
parseEval Text
e