module Language.Fortran.Repr.Type.Scalar where

import Language.Fortran.Repr.Type.Scalar.Common
import Language.Fortran.Repr.Type.Scalar.Int
import Language.Fortran.Repr.Type.Scalar.Real
import Language.Fortran.Repr.Type.Scalar.Complex
import Language.Fortran.Repr.Type.Scalar.String

import Language.Fortran.Repr.Compat.Natural

import GHC.Generics ( Generic )
import Data.Data ( Data )

-- | A Fortran scalar type.
data FScalarType
  = FSTInt FTInt
  | FSTReal FTReal
  | FSTComplex FTReal
  | FSTLogical FTInt
  | FSTString Natural
  | FSTCustom String     -- ^ F77 structure, F90 DDT (non-intrinsic scalar)
    deriving stock (forall x. Rep FScalarType x -> FScalarType
forall x. FScalarType -> Rep FScalarType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep FScalarType x -> FScalarType
$cfrom :: forall x. FScalarType -> Rep FScalarType x
Generic, Typeable FScalarType
FScalarType -> DataType
FScalarType -> Constr
(forall b. Data b => b -> b) -> FScalarType -> FScalarType
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> FScalarType -> u
forall u. (forall d. Data d => d -> u) -> FScalarType -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FScalarType -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FScalarType -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FScalarType -> m FScalarType
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FScalarType -> m FScalarType
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FScalarType
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FScalarType -> c FScalarType
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FScalarType)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FScalarType)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FScalarType -> m FScalarType
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FScalarType -> m FScalarType
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FScalarType -> m FScalarType
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FScalarType -> m FScalarType
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FScalarType -> m FScalarType
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FScalarType -> m FScalarType
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FScalarType -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FScalarType -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> FScalarType -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> FScalarType -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FScalarType -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FScalarType -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FScalarType -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FScalarType -> r
gmapT :: (forall b. Data b => b -> b) -> FScalarType -> FScalarType
$cgmapT :: (forall b. Data b => b -> b) -> FScalarType -> FScalarType
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FScalarType)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FScalarType)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FScalarType)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FScalarType)
dataTypeOf :: FScalarType -> DataType
$cdataTypeOf :: FScalarType -> DataType
toConstr :: FScalarType -> Constr
$ctoConstr :: FScalarType -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FScalarType
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FScalarType
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FScalarType -> c FScalarType
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FScalarType -> c FScalarType
Data, Int -> FScalarType -> ShowS
[FScalarType] -> ShowS
FScalarType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FScalarType] -> ShowS
$cshowList :: [FScalarType] -> ShowS
show :: FScalarType -> String
$cshow :: FScalarType -> String
showsPrec :: Int -> FScalarType -> ShowS
$cshowsPrec :: Int -> FScalarType -> ShowS
Show, FScalarType -> FScalarType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FScalarType -> FScalarType -> Bool
$c/= :: FScalarType -> FScalarType -> Bool
== :: FScalarType -> FScalarType -> Bool
$c== :: FScalarType -> FScalarType -> Bool
Eq, Eq FScalarType
FScalarType -> FScalarType -> Bool
FScalarType -> FScalarType -> Ordering
FScalarType -> FScalarType -> FScalarType
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: FScalarType -> FScalarType -> FScalarType
$cmin :: FScalarType -> FScalarType -> FScalarType
max :: FScalarType -> FScalarType -> FScalarType
$cmax :: FScalarType -> FScalarType -> FScalarType
>= :: FScalarType -> FScalarType -> Bool
$c>= :: FScalarType -> FScalarType -> Bool
> :: FScalarType -> FScalarType -> Bool
$c> :: FScalarType -> FScalarType -> Bool
<= :: FScalarType -> FScalarType -> Bool
$c<= :: FScalarType -> FScalarType -> Bool
< :: FScalarType -> FScalarType -> Bool
$c< :: FScalarType -> FScalarType -> Bool
compare :: FScalarType -> FScalarType -> Ordering
$ccompare :: FScalarType -> FScalarType -> Ordering
Ord)

prettyScalarType :: FScalarType -> String
prettyScalarType :: FScalarType -> String
prettyScalarType = \case
  FSTInt     FTInt
k -> forall a. FKind a => a -> ShowS
prettyKinded FTInt
k String
"INTEGER"
  FSTReal    FTReal
k -> forall a. FKind a => a -> ShowS
prettyKinded FTReal
k String
"REAL"
  FSTComplex FTReal
k -> forall a. FKind a => a -> ShowS
prettyKinded (FTReal -> FTComplexWrapper
FTComplexWrapper FTReal
k) String
"COMPLEX"
  FSTLogical FTInt
k -> forall a. FKind a => a -> ShowS
prettyKinded FTInt
k String
"LOGICAL"
  FSTString  Natural
l -> String
"CHARACTER("forall a. Semigroup a => a -> a -> a
<>Natural -> String
prettyCharLen Natural
lforall a. Semigroup a => a -> a -> a
<>String
")"
  FSTCustom  String
t -> String
"TYPE("forall a. Semigroup a => a -> a -> a
<>String
tforall a. Semigroup a => a -> a -> a
<>String
")"

fScalarTypeKind :: FScalarType -> Maybe FKindLit
fScalarTypeKind :: FScalarType -> Maybe FKindLit
fScalarTypeKind = \case
  FSTInt     FTInt
k -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. FKind a => a -> FKindLit
printFKind FTInt
k
  FSTReal    FTReal
k -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. FKind a => a -> FKindLit
printFKind FTReal
k
  FSTComplex FTReal
k -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. FKind a => a -> FKindLit
printFKind (FTReal -> FTComplexWrapper
FTComplexWrapper FTReal
k)
  FSTLogical FTInt
k -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. FKind a => a -> FKindLit
printFKind FTInt
k
  FSTString  Natural
l -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Natural
l
  FSTCustom  String
t -> forall a. Maybe a
Nothing

prettyKinded :: FKind a => a -> String -> String
prettyKinded :: forall a. FKind a => a -> ShowS
prettyKinded a
k String
name = String
nameforall a. Semigroup a => a -> a -> a
<>String
"("forall a. Semigroup a => a -> a -> a
<>forall a. Show a => a -> String
show (forall a. FKind a => a -> FKindLit
printFKind a
k)forall a. Semigroup a => a -> a -> a
<>String
")"