{- |
Module      : Language.Egison
Licence     : MIT

This is the top module of Egison.
-}

module Language.Egison
       ( module Language.Egison.AST
       , module Language.Egison.Data
       , module Language.Egison.Eval
       , module Language.Egison.Parser
       , module Language.Egison.Primitives
       -- * Modules needed to execute Egison
       , module Language.Egison.CmdOptions
       , module Language.Egison.RState
       -- * Environment
       , initialEnv
       -- * Information
       , version
      ) where

import           Control.Monad.Reader       (asks, local)
import           Control.Monad.State

import           Data.Version
import qualified Paths_egison               as P

import           Language.Egison.AST
import           Language.Egison.CmdOptions
import           Language.Egison.Data
import           Language.Egison.Eval
import           Language.Egison.Parser
import           Language.Egison.Primitives
import           Language.Egison.RState

-- |Version number
version :: Version
version :: Version
version = Version
P.version

-- |Environment that contains core libraries
initialEnv :: RuntimeM Env
initialEnv :: RuntimeM Env
initialEnv = do
  Bool
isNoIO <- (EgisonOpts -> Bool) -> ReaderT EgisonOpts (StateT RState IO) Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks EgisonOpts -> Bool
optNoIO
  Bool
useMathNormalize <- (EgisonOpts -> Bool) -> ReaderT EgisonOpts (StateT RState IO) Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks EgisonOpts -> Bool
optMathNormalize
  Env
env <- IO Env -> RuntimeM Env
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Env -> RuntimeM Env) -> IO Env -> RuntimeM Env
forall a b. (a -> b) -> a -> b
$ if Bool
isNoIO then IO Env
primitiveEnvNoIO else IO Env
primitiveEnv
  let normalizeLib :: [Char]
normalizeLib = if Bool
useMathNormalize then [Char]
"lib/math/normalize.egi" else [Char]
"lib/math/no-normalize.egi"
  Either EgisonError Env
ret <- (EgisonOpts -> EgisonOpts)
-> ReaderT EgisonOpts (StateT RState IO) (Either EgisonError Env)
-> ReaderT EgisonOpts (StateT RState IO) (Either EgisonError Env)
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (EgisonOpts -> EgisonOpts -> EgisonOpts
forall a b. a -> b -> a
const EgisonOpts
defaultOption)
               (EvalM Env
-> ReaderT EgisonOpts (StateT RState IO) (Either EgisonError Env)
forall a. EvalM a -> RuntimeM (Either EgisonError a)
fromEvalT (Env -> [TopExpr] -> EvalM Env
evalTopExprs Env
env ([TopExpr] -> EvalM Env) -> [TopExpr] -> EvalM Env
forall a b. (a -> b) -> a -> b
$ ([Char] -> TopExpr) -> [[Char]] -> [TopExpr]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> TopExpr
Load ([[Char]]
coreLibraries [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]
normalizeLib])))
  case Either EgisonError Env
ret of
    Left EgisonError
err -> do
      IO () -> ReaderT EgisonOpts (StateT RState IO) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ReaderT EgisonOpts (StateT RState IO) ())
-> IO () -> ReaderT EgisonOpts (StateT RState IO) ()
forall a b. (a -> b) -> a -> b
$ [Char] -> IO ()
forall a. Show a => a -> IO ()
print (EgisonError -> [Char]
forall a. Show a => a -> [Char]
show EgisonError
err)
      Env -> RuntimeM Env
forall (m :: * -> *) a. Monad m => a -> m a
return Env
env
    Right Env
env' -> Env -> RuntimeM Env
forall (m :: * -> *) a. Monad m => a -> m a
return Env
env'

coreLibraries :: [String]
coreLibraries :: [[Char]]
coreLibraries =
  -- Libs that defines user-defined infixes comes first
  [ [Char]
"lib/core/base.egi"              -- Defines (&&) (||)
  , [Char]
"lib/math/common/arithmetic.egi" -- Defines (+) (-) (*) (/) (+') (-') (*') (/')
  , [Char]
"lib/math/algebra/tensor.egi"    -- Defines (.) (.')
  , [Char]
"lib/core/collection.egi"        -- Defines (++) for patterns
  , [Char]
"lib/math/expression.egi"        -- Defines (+) (*) (/) (^) for patterns

  , [Char]
"lib/core/assoc.egi"
  , [Char]
"lib/core/io.egi"
  , [Char]
"lib/core/maybe.egi"
  , [Char]
"lib/core/number.egi"
  , [Char]
"lib/core/order.egi"
  , [Char]
"lib/core/random.egi"
  , [Char]
"lib/core/string.egi"
  , [Char]
"lib/core/sort.egi"
  , [Char]
"lib/math/common/constants.egi"
  , [Char]
"lib/math/common/functions.egi"
  , [Char]
"lib/math/algebra/root.egi"
  , [Char]
"lib/math/algebra/equations.egi"
  , [Char]
"lib/math/algebra/inverse.egi"
  , [Char]
"lib/math/analysis/derivative.egi"
  , [Char]
"lib/math/analysis/integral.egi"
  , [Char]
"lib/math/algebra/vector.egi"
  , [Char]
"lib/math/algebra/matrix.egi"
  , [Char]
"lib/math/geometry/differential-form.egi"
  ]