Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive Ord instance for all core AST types #283

Merged
merged 29 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f1b7bbd
derive Ord instance for all core AST types
raehik Jun 15, 2024
4c72831
topological sort of dependency graph
dorchard Aug 15, 2024
21f5f54
add command line argument for showing the dependency list
dorchard Aug 15, 2024
ad0bae8
move some useful helper functions from Main to Utils so easier to reu…
dorchard Aug 15, 2024
9a65b86
add test for module graph
dorchard Aug 15, 2024
3b0b45c
better error handling when there are parsing issues when building mak…
dorchard Aug 15, 2024
7a120ed
make test platform agnostic
dorchard Aug 15, 2024
55ac84d
make test platform agnostic (missed a case)
dorchard Aug 15, 2024
093d7ff
remove any duplicates
dorchard Aug 23, 2024
b5068b7
keep first occurence when removing duplicates
dorchard Aug 23, 2024
94eb5b0
filepath consistency
dorchard Sep 2, 2024
9926607
remove duplicates after processing to a list
dorchard Sep 2, 2024
39a7c76
some helpers for working with constant expression evaluator
dorchard Sep 3, 2024
69b1911
remoev comment
dorchard Sep 3, 2024
59e1bc4
add missing fall through case
dorchard Sep 4, 2024
4157d22
more robust test
dorchard Sep 3, 2024
5c7737c
feature for tagging in module maps whether names are defined loal or …
dorchard Sep 3, 2024
19f25a1
improve error reporting if syntax error with mod file
dorchard Sep 3, 2024
341021c
mod file spec test
dorchard Sep 3, 2024
193dbb2
add modfilespec to cabal
dorchard Sep 3, 2024
49d9ff1
os agnostic test
dorchard Sep 3, 2024
b47e91d
--version functionality
dorchard Sep 4, 2024
d098d00
package update
dorchard Sep 4, 2024
eac05b1
comment
dorchard Sep 4, 2024
61cba3a
Update README.md
dorchard Sep 4, 2024
0d3d95d
add date to release
dorchard Sep 4, 2024
306ec46
more detail in changelog
dorchard Sep 4, 2024
c2c3067
update changelog
dorchard Sep 4, 2024
86ba2d6
Merge branch 'master' into ast-ord-instance
dorchard Sep 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
### 0.16.0 (Sept 4, 2024)
### 0.16.1 (Sep 04, 2024)
* Minor fix to `fromConstReal` which was partial.
* Added `Ord` instance for `AST` and all its sub data types, allowing, for example, ASTs to be in containers like Data.Set

### 0.16.0 (Sep 04, 2024)
* Added `--show-make-list` option to give a topological sort on the dependency graph for a source tree
* Added `--version` option
* Some robustness improvements around mod files [#286](https://github.com/camfort/fortran-src/pull/286)
Expand Down
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
Loading