module Language.Drasil.Printing.Import.Literal (literal) where


import Language.Drasil hiding (neg, sec, symbol, isIn)
import Language.Drasil.Literal.Development (Literal(..))

import qualified Language.Drasil.Printing.AST as P
import Language.Drasil.Printing.PrintingInformation (HasPrintingOptions(..),
  PrintingInformation, Notation(Scientific, Engineering))

import Control.Lens ((^.))
import Numeric (floatToDigits)

import Language.Drasil.Printing.Import.Helpers
    (digitsProcess, processExpo)

literal :: Literal -> PrintingInformation -> P.Expr
literal :: Literal -> PrintingInformation -> Expr
literal (Dbl Double
d)                  PrintingInformation
sm = case PrintingInformation
sm forall s a. s -> Getting a s a -> a
^. forall c. HasPrintingOptions c => Lens' c Notation
getSetting of
  Notation
Engineering -> [Expr] -> Expr
P.Row forall a b. (a -> b) -> a -> b
$ [Integer] -> Int -> Int -> Integer -> [Expr]
digitsProcess (forall a b. (a -> b) -> [a] -> [b]
map forall a. Integral a => a -> Integer
toInteger forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall a. RealFloat a => Integer -> a -> ([Int], Int)
floatToDigits Integer
10 Double
d)
     (forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ Int -> (Int, Int)
processExpo forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. RealFloat a => Integer -> a -> ([Int], Int)
floatToDigits Integer
10 Double
d) Int
0
     (forall a. Integral a => a -> Integer
toInteger forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ Int -> (Int, Int)
processExpo forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. RealFloat a => Integer -> a -> ([Int], Int)
floatToDigits Integer
10 Double
d)
  Notation
Scientific  ->  Double -> Expr
P.Dbl Double
d
literal (Int Integer
i)                   PrintingInformation
_ = Integer -> Expr
P.Int Integer
i
literal (ExactDbl Integer
d)              PrintingInformation
_ = Integer -> Expr
P.Int Integer
d
literal (Str String
s)                   PrintingInformation
_ = String -> Expr
P.Str String
s
literal (Perc Integer
a Integer
b)               PrintingInformation
sm = [Expr] -> Expr
P.Row [Literal -> PrintingInformation -> Expr
literal (forall r. LiteralC r => Double -> r
dbl Double
val) PrintingInformation
sm, Ops -> Expr
P.MO Ops
P.Perc]
  where
    val :: Double
val = forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
a forall a. Fractional a => a -> a -> a
/ (Double
10 forall a. Floating a => a -> a -> a
** forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer
b forall a. Num a => a -> a -> a
- Integer
2))