Skip to content

Commit a0c78d2

Browse files
authored
Merge pull request #283 from camfort/ast-ord-instance
Derive `Ord` instance for all core AST types
2 parents aac5b87 + 86ba2d6 commit a0c78d2

File tree

7 files changed

+59
-49
lines changed

7 files changed

+59
-49
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
### 0.16.0 (Sept 4, 2024)
1+
### 0.16.1 (Sep 04, 2024)
2+
* Minor fix to `fromConstReal` which was partial.
3+
* Added `Ord` instance for `AST` and all its sub data types, allowing, for example, ASTs to be in containers like Data.Set
4+
5+
### 0.16.0 (Sep 04, 2024)
26
* Added `--show-make-list` option to give a topological sort on the dependency graph for a source tree
37
* Added `--version` option
48
* Some robustness improvements around mod files [#286](https://github.com/camfort/fortran-src/pull/286)

src/Language/Fortran/AST.hs

+42-39
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Useful Fortran standard references:
1414
* Fortran 90 standard: ANSI X3.198-1992 (also ISO/IEC 1539:1991)
1515
* Fortran 90 Handbook (J. Adams)
1616
* Fortran 77 standard: ANSI X3.9-1978
17+
18+
Note that the 'Ord' instances provided here do not guarantee any specific
19+
behaviour, other than being valid instances (they are largely for convenience).
1720
-}
1821

1922
module Language.Fortran.AST
@@ -172,7 +175,7 @@ data TypeSpec a = TypeSpec
172175
, typeSpecSpan :: SrcSpan
173176
, typeSpecBaseType :: BaseType
174177
, typeSpecSelector :: Maybe (Selector a)
175-
} deriving stock (Eq, Show, Data, Generic, Functor)
178+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
176179

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

200203
data MetaInfo = MetaInfo { miVersion :: FortranVersion, miFilename :: String }
201-
deriving stock (Eq, Show, Data, Generic)
204+
deriving stock (Eq, Ord, Show, Data, Generic)
202205

203206
-- Program structure definition
204207
data ProgramFile a = ProgramFile
205208
{ programFileMeta :: MetaInfo
206209
, programFileProgramUnits :: [ ProgramUnit a ]
207-
} deriving stock (Eq, Show, Data, Generic, Functor)
210+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
208211

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

264267
type Prefixes a = Maybe (AList Prefix a)
265268
type Suffixes a = Maybe (AList Suffix a)
@@ -277,7 +280,7 @@ emptyPrefixSuffix = (emptyPrefixes, emptySuffixes)
277280
data Prefix a = PfxRecursive a SrcSpan
278281
| PfxElemental a SrcSpan
279282
| PfxPure a SrcSpan
280-
deriving stock (Eq, Show, Data, Generic, Functor)
283+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
281284

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

293296
data Suffix a = SfxBind a SrcSpan (Maybe (Expression a))
294-
deriving stock (Eq, Show, Data, Generic, Functor)
297+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
295298

296299
programUnitBody :: ProgramUnit a -> [Block a]
297300
programUnitBody (PUMain _ _ _ bs _) = bs
@@ -323,7 +326,7 @@ programUnitSubprograms PUBlockData{} = Nothing
323326
programUnitSubprograms PUComment{} = Nothing
324327

325328
newtype Comment a = Comment String
326-
deriving stock (Eq, Show, Data, Generic, Functor)
329+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
327330

328331
data Block a =
329332
BlStatement -- ^ Statement
@@ -391,7 +394,7 @@ data Block a =
391394
| BlComment -- ^ Block-level comment
392395
a SrcSpan
393396
(Comment a)
394-
deriving stock (Eq, Show, Data, Generic, Functor)
397+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
395398

396399
data Statement a =
397400
StDeclaration
@@ -615,28 +618,28 @@ data Statement a =
615618
-- Following is a temporary solution to a complicated FORMAT statement
616619
-- parsing problem.
617620
| StFormatBogus a SrcSpan String
618-
deriving stock (Eq, Show, Data, Generic, Functor)
621+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
619622

620623
-- R1214 proc-decl is procedure-entity-name [=> null-init]
621624
data ProcDecl a = ProcDecl
622625
{ procDeclAnno :: a
623626
, procDeclSpan :: SrcSpan
624627
, procDeclEntityName :: Expression a
625628
, procDeclInitName :: Maybe (Expression a)
626-
} deriving stock (Eq, Show, Data, Generic, Functor)
629+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
627630

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

633636
-- | Part of a FORALL statement. Introduced in Fortran 95.
634637
data ForallHeader a = ForallHeader
635638
{ forallHeaderAnno :: a
636639
, forallHeaderSpan :: SrcSpan
637640
, forallHeaderHeaders :: [ForallHeaderPart a]
638641
, forallHeaderScaling :: Maybe (Expression a)
639-
} deriving stock (Eq, Show, Data, Generic, Functor)
642+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
640643

641644
data ForallHeaderPart a = ForallHeaderPart
642645
{ forallHeaderPartAnno :: a
@@ -645,13 +648,13 @@ data ForallHeaderPart a = ForallHeaderPart
645648
, forallHeaderPartStart :: Expression a
646649
, forallHeaderPartEnd :: Expression a
647650
, forallHeaderPartStride :: Maybe (Expression a)
648-
} deriving stock (Eq, Show, Data, Generic, Functor)
651+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
649652

650653
data Only = Exclusive | Permissive
651-
deriving stock (Eq, Show, Data, Generic)
654+
deriving stock (Eq, Ord, Show, Data, Generic)
652655

653656
data ModuleNature = ModIntrinsic | ModNonIntrinsic
654-
deriving stock (Eq, Show, Data, Generic)
657+
deriving stock (Eq, Ord, Show, Data, Generic)
655658

656659
-- | Part of USE statement. /(F2018 14.2.2)/
657660
--
@@ -664,15 +667,15 @@ data Use a =
664667
| UseID
665668
a SrcSpan
666669
(Expression a) -- ^ name
667-
deriving stock (Eq, Show, Data, Generic, Functor)
670+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
668671

669672
-- TODO potentially should throw Maybe String into ArgumentExpression too?
670673
data Argument a = Argument
671674
{ argumentAnno :: a
672675
, argumentSpan :: SrcSpan
673676
, argumentName :: Maybe String
674677
, argumentExpr :: ArgumentExpression a
675-
} deriving stock (Eq, Show, Data, Generic, Functor)
678+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
676679

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

688691
instance Annotated ArgumentExpression where
689692
getAnnotation = \case
@@ -729,17 +732,17 @@ data Attribute a =
729732
| AttrTarget a SrcSpan
730733
| AttrValue a SrcSpan
731734
| AttrVolatile a SrcSpan
732-
deriving stock (Eq, Show, Data, Generic, Functor)
735+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
733736

734737
data Intent = In | Out | InOut
735-
deriving stock (Eq, Show, Data, Generic)
738+
deriving stock (Eq, Ord, Show, Data, Generic)
736739

737740
data ControlPair a = ControlPair
738741
{ controlPairAnno :: a
739742
, controlPairSpan :: SrcSpan
740743
, controlPairName :: Maybe String
741744
, controlPairExpr :: Expression a
742-
} deriving stock (Eq, Show, Data, Generic, Functor)
745+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
743746

744747
-- | Part of ALLOCATE statement.
745748
--
@@ -754,22 +757,22 @@ data AllocOpt a =
754757
a SrcSpan
755758
(Expression a) -- ^ scalar character variable
756759
| AOSource a SrcSpan (Expression a)
757-
deriving stock (Eq, Show, Data, Generic, Functor)
760+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
758761

759762
-- | List of names for an IMPLICIT statement.
760763
data ImpList a = ImpList
761764
{ impListAnno :: a
762765
, impListSpan :: SrcSpan
763766
, impListType :: TypeSpec a
764767
, impListElements :: AList ImpElement a
765-
} deriving stock (Eq, Show, Data, Generic, Functor)
768+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
766769

767770
data ImpElement a = ImpElement
768771
{ impElementAnno :: a
769772
, impElementSpan :: SrcSpan
770773
, impElementFrom :: Char
771774
, impElementTo :: Maybe Char
772-
} deriving stock (Eq, Show, Data, Generic, Functor)
775+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
773776

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

784787
data Namelist a = Namelist
785788
{ namelistAnno :: a
786789
, namelistSpan :: SrcSpan
787790
, namelistName :: Expression a
788791
, namelistVars :: AList Expression a
789-
} deriving stock (Eq, Show, Data, Generic, Functor)
792+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
790793

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

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

826829
data UnionMap a = UnionMap
827830
{ unionMapAnno :: a
828831
, unionMapSpan :: SrcSpan
829832
, unionMapFields :: AList StructureItem a
830-
} deriving stock (Eq, Show, Data, Generic, Functor)
833+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
831834

832835
data FormatItem a =
833836
FIFormatList a SrcSpan (Maybe String) (AList FormatItem a)
@@ -838,7 +841,7 @@ data FormatItem a =
838841
| FIFieldDescriptorAIL a SrcSpan (Maybe Integer) Char Integer
839842
| FIBlankDescriptor a SrcSpan Integer
840843
| FIScaleFactor a SrcSpan Integer
841-
deriving stock (Eq, Show, Data, Generic, Functor)
844+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
842845

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

861864
data DoSpecification a = DoSpecification
862865
{ doSpecAnno :: a
863866
, doSpecSpan :: SrcSpan
864867
, doSpecInitial :: Statement a -- ^ Guaranteed to be 'StExpressionAssign'
865868
, doSpecLimit :: Expression a
866869
, doSpecIncrement :: Maybe (Expression a)
867-
} deriving stock (Eq, Show, Data, Generic, Functor)
870+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
868871

869872
data Expression a =
870873
ExpValue a SrcSpan (Value a)
@@ -885,15 +888,15 @@ data Expression a =
885888
-- ^ Array initialisation
886889
| ExpReturnSpec a SrcSpan (Expression a)
887890
-- ^ Function return value specification
888-
deriving stock (Eq, Show, Data, Generic, Functor)
891+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
889892

890893
data Index a =
891894
IxSingle a SrcSpan (Maybe String) (Expression a)
892895
| IxRange a SrcSpan
893896
(Maybe (Expression a)) -- ^ Lower index
894897
(Maybe (Expression a)) -- ^ Upper index
895898
(Maybe (Expression a)) -- ^ Stride
896-
deriving stock (Eq, Show, Data, Generic, Functor)
899+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
897900

898901
-- | Values and literals.
899902
--
@@ -925,7 +928,7 @@ data Value a
925928
| ValType String
926929
| ValStar
927930
| ValColon -- see R402 / C403 in Fortran2003 spec.
928-
deriving stock (Eq, Show, Data, Generic, Functor)
931+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
929932
deriving anyclass (NFData, Out)
930933

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

954957
data DeclaratorType a
955958
= ScalarDecl
956959
| ArrayDecl (AList DimensionDeclarator a)
957-
deriving stock (Eq, Show, Data, Generic, Functor)
960+
deriving stock (Eq, Ord, Show, Data, Generic, Functor)
958961

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

973976
data UnaryOp =
974977
Plus

src/Language/Fortran/AST/AList.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ data AList t a = AList
2121
{ alistAnno :: a
2222
, alistSpan :: SrcSpan
2323
, alistList :: [t a]
24-
} deriving stock (Eq, Show, Data, Generic)
24+
} deriving stock (Eq, Ord, Show, Data, Generic)
2525

2626
instance Functor t => Functor (AList t) where
2727
fmap f (AList a s xs) = AList (f a) s (map (fmap f) xs)
@@ -76,7 +76,7 @@ data ATuple t1 t2 a = ATuple
7676
, atupleSpan :: SrcSpan
7777
, atupleFst :: t1 a
7878
, atupleSnd :: t2 a
79-
} deriving stock (Eq, Show, Data, Generic, Functor)
79+
} deriving stock (Eq, Ord, Show, Data, Generic, Functor)
8080

8181
instance FirstParameter (ATuple t1 t2 a) a
8282
instance SecondParameter (ATuple t1 t2 a) SrcSpan

src/Language/Fortran/AST/Literal.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Text.PrettyPrint.GenericPretty ( Out )
1414
data KindParam a
1515
= KindParamInt a SrcSpan String -- ^ @[0-9]+@
1616
| KindParamVar a SrcSpan Name -- ^ @[a-z][a-z0-9]+@ (case insensitive)
17-
deriving stock (Eq, Show, Data, Typeable, Generic, Functor)
17+
deriving stock (Eq, Ord, Show, Data, Typeable, Generic, Functor)
1818
deriving anyclass (NFData, Out)
1919

2020
instance FirstParameter (KindParam a) a

src/Language/Fortran/AST/Literal/Boz.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ instance Eq BozPrefix where
7171
_ -> False
7272

7373
data Conforming = Conforming | Nonconforming
74-
deriving stock (Eq, Show, Generic, Data, Typeable, Ord)
74+
deriving stock (Eq, Ord, Show, Generic, Data, Typeable)
7575
deriving anyclass (NFData, Out)
7676

7777
-- | UNSAFE. Parses a BOZ literal constant string.

src/Language/Fortran/AST/Literal/Complex.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ data ComplexLit a = ComplexLit
2727
, complexLitPos :: SrcSpan
2828
, complexLitRealPart :: ComplexPart a
2929
, complexLitImagPart :: ComplexPart a
30-
} deriving stock (Eq, Show, Data, Typeable, Generic, Functor)
30+
} deriving stock (Eq, Ord, Show, Data, Typeable, Generic, Functor)
3131
deriving anyclass (NFData, Out)
3232

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

5757
instance FirstParameter (ComplexPart a) a

0 commit comments

Comments
 (0)