module Main (main) where import Text.Parsec import Lib (exprparser, evaluate, replaceVars) import Data.Map (Map) import qualified Data.Map as Map import Data.Ratio import System.IO main :: IO () main = ioLoop precision = 5 :: Int showRatio :: Int -> Rational -> String showRatio p r = (if (r < 0) then "-" else "") ++ prepoint_digits ++ "." ++ postpoint_digits where prepoint_digits = init . show . round $ (r * 10) postpoint_digits = (take p) . (drop (length prepoint_digits)) . show . round $ (r * 10^p) useResult (Right e) = (showRatio precision) . evaluate $ replaceVars e vars useResult (Left e) = show e vars :: Map String Rational vars = Map.fromList [("pi", 245850922 % 78256779), ("e", 271801 % 99990)] ioLoop :: IO () ioLoop = do done <- isEOF if done then putStrLn "Quit!" else do inp <- getLine let expr = parse exprparser "" inp putStrLn . useResult $ expr ioLoop