Skip to content

Commit

Permalink
derive Ord instance for all core AST types
Browse files Browse the repository at this point in the history
Requested in #282 . Note that `Eq` was already derived.
  • Loading branch information
raehik committed Jun 15, 2024
1 parent 9424823 commit f1b7bbd
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 48 deletions.
81 changes: 42 additions & 39 deletions src/Language/Fortran/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Useful Fortran standard references:
* Fortran 90 standard: ANSI X3.198-1992 (also ISO/IEC 1539:1991)
* Fortran 90 Handbook (J. Adams)
* Fortran 77 standard: ANSI X3.9-1978
Note that the 'Ord' instances provided here do not guarantee any specific
behaviour, other than being valid instances (they are largely for convenience).
-}

module Language.Fortran.AST
Expand Down Expand Up @@ -172,7 +175,7 @@ data TypeSpec a = TypeSpec
, typeSpecSpan :: SrcSpan
, typeSpecBaseType :: BaseType
, typeSpecSelector :: Maybe (Selector a)
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- | The "kind selector" of a declaration statement. Tightly bound to
-- 'TypeSpec'.
Expand All @@ -195,16 +198,16 @@ data Selector a = Selector
, selectorSpan :: SrcSpan
, selectorLength :: Maybe (Expression a)
, selectorKind :: Maybe (Expression a)
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data MetaInfo = MetaInfo { miVersion :: FortranVersion, miFilename :: String }
deriving stock (Eq, Show, Data, Generic)
deriving stock (Eq, Ord, Show, Data, Generic)

-- Program structure definition
data ProgramFile a = ProgramFile
{ programFileMeta :: MetaInfo
, programFileProgramUnits :: [ ProgramUnit a ]
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

pfSetFilename :: String -> ProgramFile a -> ProgramFile a
pfSetFilename fn (ProgramFile mi pus) = ProgramFile (mi { miFilename = fn }) pus
Expand Down Expand Up @@ -259,7 +262,7 @@ data ProgramUnit a =
| PUComment -- ^ Program unit-level comment
a SrcSpan
(Comment a)
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

type Prefixes a = Maybe (AList Prefix a)
type Suffixes a = Maybe (AList Suffix a)
Expand All @@ -277,7 +280,7 @@ emptyPrefixSuffix = (emptyPrefixes, emptySuffixes)
data Prefix a = PfxRecursive a SrcSpan
| PfxElemental a SrcSpan
| PfxPure a SrcSpan
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- see C1241 & C1242 (Fortran2003)
validPrefixSuffix :: PrefixSuffix a -> Bool
Expand All @@ -291,7 +294,7 @@ validPrefixSuffix (mpfxs, msfxs) =
sfxs = aStrip' msfxs

data Suffix a = SfxBind a SrcSpan (Maybe (Expression a))
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

programUnitBody :: ProgramUnit a -> [Block a]
programUnitBody (PUMain _ _ _ bs _) = bs
Expand Down Expand Up @@ -323,7 +326,7 @@ programUnitSubprograms PUBlockData{} = Nothing
programUnitSubprograms PUComment{} = Nothing

newtype Comment a = Comment String
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data Block a =
BlStatement -- ^ Statement
Expand Down Expand Up @@ -391,7 +394,7 @@ data Block a =
| BlComment -- ^ Block-level comment
a SrcSpan
(Comment a)
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data Statement a =
StDeclaration
Expand Down Expand Up @@ -615,28 +618,28 @@ data Statement a =
-- Following is a temporary solution to a complicated FORMAT statement
-- parsing problem.
| StFormatBogus a SrcSpan String
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- R1214 proc-decl is procedure-entity-name [=> null-init]
data ProcDecl a = ProcDecl
{ procDeclAnno :: a
, procDeclSpan :: SrcSpan
, procDeclEntityName :: Expression a
, procDeclInitName :: Maybe (Expression a)
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- R1212 proc-interface is interface-name or declaration-type-spec
data ProcInterface a = ProcInterfaceName a SrcSpan (Expression a)
| ProcInterfaceType a SrcSpan (TypeSpec a)
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- | Part of a FORALL statement. Introduced in Fortran 95.
data ForallHeader a = ForallHeader
{ forallHeaderAnno :: a
, forallHeaderSpan :: SrcSpan
, forallHeaderHeaders :: [ForallHeaderPart a]
, forallHeaderScaling :: Maybe (Expression a)
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data ForallHeaderPart a = ForallHeaderPart
{ forallHeaderPartAnno :: a
Expand All @@ -645,13 +648,13 @@ data ForallHeaderPart a = ForallHeaderPart
, forallHeaderPartStart :: Expression a
, forallHeaderPartEnd :: Expression a
, forallHeaderPartStride :: Maybe (Expression a)
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data Only = Exclusive | Permissive
deriving stock (Eq, Show, Data, Generic)
deriving stock (Eq, Ord, Show, Data, Generic)

data ModuleNature = ModIntrinsic | ModNonIntrinsic
deriving stock (Eq, Show, Data, Generic)
deriving stock (Eq, Ord, Show, Data, Generic)

-- | Part of USE statement. /(F2018 14.2.2)/
--
Expand All @@ -664,15 +667,15 @@ data Use a =
| UseID
a SrcSpan
(Expression a) -- ^ name
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- TODO potentially should throw Maybe String into ArgumentExpression too?
data Argument a = Argument
{ argumentAnno :: a
, argumentSpan :: SrcSpan
, argumentName :: Maybe String
, argumentExpr :: ArgumentExpression a
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- | Extra data type to disambiguate between plain variable arguments and
-- expression arguments (due to apparent behaviour of some Fortran compilers
Expand All @@ -683,7 +686,7 @@ data Argument a = Argument
data ArgumentExpression a
= ArgExpr (Expression a)
| ArgExprVar a SrcSpan Name
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

instance Annotated ArgumentExpression where
getAnnotation = \case
Expand Down Expand Up @@ -729,17 +732,17 @@ data Attribute a =
| AttrTarget a SrcSpan
| AttrValue a SrcSpan
| AttrVolatile a SrcSpan
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data Intent = In | Out | InOut
deriving stock (Eq, Show, Data, Generic)
deriving stock (Eq, Ord, Show, Data, Generic)

data ControlPair a = ControlPair
{ controlPairAnno :: a
, controlPairSpan :: SrcSpan
, controlPairName :: Maybe String
, controlPairExpr :: Expression a
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- | Part of ALLOCATE statement.
--
Expand All @@ -754,22 +757,22 @@ data AllocOpt a =
a SrcSpan
(Expression a) -- ^ scalar character variable
| AOSource a SrcSpan (Expression a)
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- | List of names for an IMPLICIT statement.
data ImpList a = ImpList
{ impListAnno :: a
, impListSpan :: SrcSpan
, impListType :: TypeSpec a
, impListElements :: AList ImpElement a
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data ImpElement a = ImpElement
{ impElementAnno :: a
, impElementSpan :: SrcSpan
, impElementFrom :: Char
, impElementTo :: Maybe Char
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- | A single COMMON block definition.
--
Expand All @@ -779,14 +782,14 @@ data CommonGroup a = CommonGroup
, commonGroupSpan :: SrcSpan
, commonGroupName :: Maybe (Expression a)
, commonGroupVars :: AList Declarator a
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data Namelist a = Namelist
{ namelistAnno :: a
, namelistSpan :: SrcSpan
, namelistName :: Expression a
, namelistVars :: AList Expression a
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- | The part of a DATA statement describing a single set of initializations.
--
Expand All @@ -799,7 +802,7 @@ data DataGroup a = DataGroup
, dataGroupSpan :: SrcSpan
, dataGroupNames :: AList Expression a
, dataGroupInitializers :: AList Expression a
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- | Field types in pre-Fortran 90 non-standard structure/record/union
-- extension.
Expand All @@ -821,13 +824,13 @@ data StructureItem a =
(Maybe String) -- ^ Substructure name
String -- ^ Field name
(AList StructureItem a) -- ^ Substructure fields
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data UnionMap a = UnionMap
{ unionMapAnno :: a
, unionMapSpan :: SrcSpan
, unionMapFields :: AList StructureItem a
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data FormatItem a =
FIFormatList a SrcSpan (Maybe String) (AList FormatItem a)
Expand All @@ -838,7 +841,7 @@ data FormatItem a =
| FIFieldDescriptorAIL a SrcSpan (Maybe Integer) Char Integer
| FIBlankDescriptor a SrcSpan Integer
| FIScaleFactor a SrcSpan Integer
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- | Part of the newer (Fortran 2003?) FLUSH statement.
--
Expand All @@ -856,15 +859,15 @@ data FlushSpec a
| FSErr
a SrcSpan
(Expression a) -- ^ statement label
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data DoSpecification a = DoSpecification
{ doSpecAnno :: a
, doSpecSpan :: SrcSpan
, doSpecInitial :: Statement a -- ^ Guaranteed to be 'StExpressionAssign'
, doSpecLimit :: Expression a
, doSpecIncrement :: Maybe (Expression a)
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data Expression a =
ExpValue a SrcSpan (Value a)
Expand All @@ -885,15 +888,15 @@ data Expression a =
-- ^ Array initialisation
| ExpReturnSpec a SrcSpan (Expression a)
-- ^ Function return value specification
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data Index a =
IxSingle a SrcSpan (Maybe String) (Expression a)
| IxRange a SrcSpan
(Maybe (Expression a)) -- ^ Lower index
(Maybe (Expression a)) -- ^ Upper index
(Maybe (Expression a)) -- ^ Stride
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- | Values and literals.
--
Expand Down Expand Up @@ -925,7 +928,7 @@ data Value a
| ValType String
| ValStar
| ValColon -- see R402 / C403 in Fortran2003 spec.
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
deriving anyclass (NFData, Out)

-- | Declarators. R505 entity-decl from F90 ISO spec.
Expand All @@ -949,12 +952,12 @@ data Declarator a = Declarator
, declaratorType :: DeclaratorType a
, declaratorLength :: Maybe (Expression a)
, declaratorInitial :: Maybe (Expression a)
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data DeclaratorType a
= ScalarDecl
| ArrayDecl (AList DimensionDeclarator a)
deriving stock (Eq, Show, Data, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Generic, Functor)

-- | Set a 'Declarator''s initializing expression only if it has none already.
setInitialisation :: Declarator a -> Expression a -> Declarator a
Expand All @@ -968,7 +971,7 @@ data DimensionDeclarator a = DimensionDeclarator
, dimDeclSpan :: SrcSpan
, dimDeclLower :: Maybe (Expression a)
, dimDeclUpper :: Maybe (Expression a)
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

data UnaryOp =
Plus
Expand Down
4 changes: 2 additions & 2 deletions src/Language/Fortran/AST/AList.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ data AList t a = AList
{ alistAnno :: a
, alistSpan :: SrcSpan
, alistList :: [t a]
} deriving stock (Eq, Show, Data, Generic)
} deriving stock (Eq, Ord, Show, Data, Generic)

instance Functor t => Functor (AList t) where
fmap f (AList a s xs) = AList (f a) s (map (fmap f) xs)
Expand Down Expand Up @@ -76,7 +76,7 @@ data ATuple t1 t2 a = ATuple
, atupleSpan :: SrcSpan
, atupleFst :: t1 a
, atupleSnd :: t2 a
} deriving stock (Eq, Show, Data, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)

instance FirstParameter (ATuple t1 t2 a) a
instance SecondParameter (ATuple t1 t2 a) SrcSpan
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Fortran/AST/Literal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Text.PrettyPrint.GenericPretty ( Out )
data KindParam a
= KindParamInt a SrcSpan String -- ^ @[0-9]+@
| KindParamVar a SrcSpan Name -- ^ @[a-z][a-z0-9]+@ (case insensitive)
deriving stock (Eq, Show, Data, Typeable, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Typeable, Generic, Functor)
deriving anyclass (NFData, Out)

instance FirstParameter (KindParam a) a
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Fortran/AST/Literal/Boz.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ instance Eq BozPrefix where
_ -> False

data Conforming = Conforming | Nonconforming
deriving stock (Eq, Show, Generic, Data, Typeable, Ord)
deriving stock (Eq, Ord, Show, Generic, Data, Typeable)
deriving anyclass (NFData, Out)

-- | UNSAFE. Parses a BOZ literal constant string.
Expand Down
4 changes: 2 additions & 2 deletions src/Language/Fortran/AST/Literal/Complex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ data ComplexLit a = ComplexLit
, complexLitPos :: SrcSpan
, complexLitRealPart :: ComplexPart a
, complexLitImagPart :: ComplexPart a
} deriving stock (Eq, Show, Data, Typeable, Generic, Functor)
} deriving stock (Eq, Ord, Show, Data, Typeable, Generic, Functor)
deriving anyclass (NFData, Out)

instance FirstParameter (ComplexLit a) a
Expand All @@ -51,7 +51,7 @@ data ComplexPart a
= ComplexPartReal a SrcSpan RealLit (Maybe (KindParam a)) -- ^ signed real lit
| ComplexPartInt a SrcSpan String (Maybe (KindParam a)) -- ^ signed int lit
| ComplexPartNamed a SrcSpan Name -- ^ named constant
deriving stock (Eq, Show, Data, Typeable, Generic, Functor)
deriving stock (Eq, Ord, Show, Data, Typeable, Generic, Functor)
deriving anyclass (NFData, Out)

instance FirstParameter (ComplexPart a) a
Expand Down
Loading

0 comments on commit f1b7bbd

Please sign in to comment.