packages feed

calculator-0.2.0.2: src/Main.hs

module Main where

--------------------------------------------------------------------------------

import           Paths_calculator            (version)

--------------------------------------------------------------------------------

import           Calculator.Color            (Color (..), bold, color)
import           Calculator.Evaluator.Base   (evaluate)
import           Calculator.Prim.Bindings    (Bindings)
import           Calculator.Prim.Definitions (defBinds)

--------------------------------------------------------------------------------

import           Data.Version                (showVersion)
import           System.Console.Haskeline

--------------------------------------------------------------------------------

type Repl a = InputT IO a

--------------------------------------------------------------------------------

repl :: Bindings -> Repl ()
repl b = do
  input <- getInputLine $ color Red True "calc> "
  case input of
    Nothing  -> return ()
    Just inp -> case evaluate b inp of
                  Left s   -> outputStrLn s >> repl b
                  Right b' -> repl b'

--------------------------------------------------------------------------------

main :: IO ()
main = do
  putStrLn $ "calculator, version "
               ++ showVersion version
               ++ ". Use :? for help."
  runInputT defaultSettings (repl defBinds)
  putStrLn $ "Please report issues at: " ++
       bold "https://github.com/sumitsahrawat/calculator/issues"

--------------------------------------------------------------------------------