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. FScalarType -> Rep FScalarType x)
-> (forall x. Rep FScalarType x -> FScalarType)
-> Generic FScalarType
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
$cfrom :: forall x. FScalarType -> Rep FScalarType x
from :: forall x. FScalarType -> Rep FScalarType x
$cto :: forall x. Rep FScalarType x -> FScalarType
to :: forall x. Rep FScalarType x -> FScalarType
Generic, Typeable FScalarType
Typeable FScalarType =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> FScalarType -> c FScalarType)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c FScalarType)
-> (FScalarType -> Constr)
-> (FScalarType -> DataType)
-> (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))
-> ((forall b. Data b => b -> b) -> FScalarType -> FScalarType)
-> (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 u. (forall d. Data d => d -> u) -> FScalarType -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> FScalarType -> u)
-> (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 (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> FScalarType -> m FScalarType)
-> Data FScalarType
FScalarType -> Constr
FScalarType -> DataType
(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)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FScalarType -> c FScalarType
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FScalarType -> c FScalarType
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FScalarType
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FScalarType
$ctoConstr :: FScalarType -> Constr
toConstr :: FScalarType -> Constr
$cdataTypeOf :: FScalarType -> DataType
dataTypeOf :: FScalarType -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FScalarType)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FScalarType)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FScalarType)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FScalarType)
$cgmapT :: (forall b. Data b => b -> b) -> FScalarType -> FScalarType
gmapT :: (forall b. Data b => b -> b) -> FScalarType -> FScalarType
$cgmapQl :: 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
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FScalarType -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FScalarType -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> FScalarType -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> FScalarType -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FScalarType -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FScalarType -> u
$cgmapM :: forall (m :: * -> *).
Monad 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
$cgmapMp :: 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
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FScalarType -> m FScalarType
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FScalarType -> m FScalarType
Data, Int -> FScalarType -> ShowS
[FScalarType] -> ShowS
FScalarType -> String
(Int -> FScalarType -> ShowS)
-> (FScalarType -> String)
-> ([FScalarType] -> ShowS)
-> Show FScalarType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FScalarType -> ShowS
showsPrec :: Int -> FScalarType -> ShowS
$cshow :: FScalarType -> String
show :: FScalarType -> String
$cshowList :: [FScalarType] -> ShowS
showList :: [FScalarType] -> ShowS
Show, FScalarType -> FScalarType -> Bool
(FScalarType -> FScalarType -> Bool)
-> (FScalarType -> FScalarType -> Bool) -> Eq FScalarType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FScalarType -> FScalarType -> Bool
== :: FScalarType -> FScalarType -> Bool
$c/= :: FScalarType -> FScalarType -> Bool
/= :: FScalarType -> FScalarType -> Bool
Eq, Eq FScalarType
Eq FScalarType =>
(FScalarType -> FScalarType -> Ordering)
-> (FScalarType -> FScalarType -> Bool)
-> (FScalarType -> FScalarType -> Bool)
-> (FScalarType -> FScalarType -> Bool)
-> (FScalarType -> FScalarType -> Bool)
-> (FScalarType -> FScalarType -> FScalarType)
-> (FScalarType -> FScalarType -> FScalarType)
-> Ord 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
$ccompare :: FScalarType -> FScalarType -> Ordering
compare :: FScalarType -> FScalarType -> Ordering
$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
>= :: FScalarType -> FScalarType -> Bool
$cmax :: FScalarType -> FScalarType -> FScalarType
max :: FScalarType -> FScalarType -> FScalarType
$cmin :: FScalarType -> FScalarType -> FScalarType
min :: FScalarType -> FScalarType -> FScalarType
Ord)

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

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

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