Skip to content

Commit 689150e

Browse files
committed
manage associate assignments another way
Copy the AList approach for tuples. Nice and easy approach that seems sensible to me.
1 parent 6f0ef88 commit 689150e

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

src/Language/Fortran/AST.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ data Block a =
347347
| BlAssociate a SrcSpan
348348
(Maybe (Expression a)) -- Label
349349
(Maybe String) -- Construct name
350-
[(Expression a, Expression a)] -- Expression abbreviations
350+
(AList (ATuple Expression Expression) a) -- Expression abbreviations
351351
[ Block a ] -- Body
352352
(Maybe (Expression a)) -- Label to END IF
353353
-- ^ The first 'Expression' in the abbreviation tuple is always an

src/Language/Fortran/AST/AList.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE FlexibleInstances #-}
33
{-# LANGUAGE DeriveDataTypeable #-}
44
{-# LANGUAGE DeriveGeneric #-}
5+
{-# LANGUAGE DeriveFunctor #-}
56

67
module Language.Fortran.AST.AList where
78

@@ -63,3 +64,14 @@ aStrip' (Just a) = aStrip a
6364

6465
aMap :: (t a -> r a) -> AList t a -> AList r a
6566
aMap f (AList a s xs) = AList a s (map f xs)
67+
68+
--------------------------------------------------------------------------------
69+
70+
data ATuple t1 t2 a = ATuple a SrcSpan (t1 a) (t2 a)
71+
deriving (Eq, Show, Data, Typeable, Generic, Functor)
72+
73+
instance FirstParameter (ATuple t1 t2 a) a
74+
instance SecondParameter (ATuple t1 t2 a) SrcSpan
75+
instance Spanned (ATuple t1 t2 a)
76+
instance (Out a, Out (t1 a), Out (t2 a)) => Out (ATuple t1 t2 a)
77+
instance (NFData a, NFData (t1 a), NFData (t2 a)) => NFData (ATuple t1 t2 a)

src/Language/Fortran/Parser/Fortran90.y

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -411,17 +411,17 @@ ASSOCIATE_BLOCK :: { Block A0 }
411411
mLabel = Just $1;
412412
TId _ name = $2;
413413
mName = Just name;
414-
abbrevs = $6;
415-
body = $10;
414+
abbrevs = fromReverseList $6;
415+
body = reverse $10;
416416
(endSpan, mEndLabel) = $11;
417417
span = getTransSpan startSpan endSpan }
418418
in BlAssociate () span mLabel mName abbrevs body mEndLabel }
419419
| INTEGER_LITERAL associate '(' ABBREVIATIONS ')' MAYBE_COMMENT NEWLINE BLOCKS END_ASSOCIATE
420420
{ let { startSpan = getSpan $1;
421421
mLabel = Just $1;
422422
mName = Nothing;
423-
abbrevs = $4;
424-
body = $8;
423+
abbrevs = fromReverseList $4;
424+
body = reverse $8;
425425
(endSpan, mEndLabel) = $9;
426426
span = getTransSpan startSpan endSpan }
427427
in BlAssociate () span mLabel mName abbrevs body mEndLabel }
@@ -430,17 +430,17 @@ ASSOCIATE_BLOCK :: { Block A0 }
430430
TId _ name = $1;
431431
mLabel = Nothing;
432432
mName = Just name;
433-
abbrevs = $5;
434-
body = $9;
433+
abbrevs = fromReverseList $5;
434+
body = reverse $9;
435435
(endSpan, mEndLabel) = $10;
436436
span = getTransSpan startSpan endSpan }
437437
in BlAssociate () span mLabel mName abbrevs body mEndLabel }
438438
| associate '(' ABBREVIATIONS ')' MAYBE_COMMENT NEWLINE BLOCKS END_ASSOCIATE
439439
{ let { startSpan = getSpan $1;
440440
mLabel = Nothing;
441441
mName = Nothing;
442-
abbrevs = $3;
443-
body = $7;
442+
abbrevs = fromReverseList $3;
443+
body = reverse $7;
444444
(endSpan, mEndLabel) = $8;
445445
span = getTransSpan startSpan endSpan }
446446
in BlAssociate () span mLabel mName abbrevs body mEndLabel }
@@ -453,11 +453,11 @@ END_ASSOCIATE :: { (SrcSpan, Maybe (Expression A0)) }
453453
| INTEGER_LITERAL endassociate id { (getSpan $3, Just $1) }
454454

455455
-- (var (ExpValue (ValVariable)), assoc. expr)
456-
ABBREVIATIONS :: { [(Expression A0, Expression A0)] }
456+
ABBREVIATIONS :: { [(ATuple Expression Expression A0)] }
457457
: ABBREVIATIONS ',' ABBREVIATION { $3 : $1 }
458458
| ABBREVIATION { [ $1 ] }
459-
ABBREVIATION :: { (Expression A0, Expression A0) }
460-
: VARIABLE '=>' EXPRESSION { ($1, $3) }
459+
ABBREVIATION :: { ATuple Expression Expression A0 }
460+
: VARIABLE '=>' EXPRESSION { ATuple () (getTransSpan $1 $3) $1 $3 }
461461

462462
MAYBE_EXPRESSION :: { Maybe (Expression A0) }
463463
: EXPRESSION { Just $1 }

src/Language/Fortran/PrettyPrint.hs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,46 @@ instance IndentablePretty (Block a) where
327327
then indent i (pprint' v label <+> stDoc)
328328
else pprint' v mLabel `overlay` indent i stDoc
329329

330+
-- TODO associate
330331
pprint v (BlAssociate _ _ mLabel mName abbrevs bodies mEndLabel) i
331332
| v >= Fortran90 =
332-
indent i ("associate" <+> "ABBREVS" <> newline) <>
333-
pprint v bodies nextI <>
334-
indent i ("end associate" <> newline)
333+
labeledIndent mLabel
334+
$ pprint' v mName <?> colon
335+
<+> ("associate" <+> "(" <> pprint' v abbrevs <> ")" <> newline)
336+
<> pprint v bodies nextI
337+
<> labeledIndent mEndLabel ("end associate" <+> pprint' v mName <> newline)
335338
| otherwise = tooOld v "Associate block" Fortran90
336339
where
337340
nextI = incIndentation i
341+
labeledIndent label stDoc =
342+
if v >= Fortran90
343+
then indent i (pprint' v label <+> stDoc)
344+
else pprint' v mLabel `overlay` indent i stDoc
345+
{-
346+
pprint v (BlIf _ _ mLabel mName conds bodies el) i
347+
| v >= Fortran77 =
348+
labeledIndent mLabel
349+
$ (pprint' v mName <?> colon
350+
<+> "if" <+> parens (pprint' v firstCond) <+> "then" <> newline)
351+
<> pprint v firstBody nextI
352+
<> foldl' (<>) empty (map displayCondBlock restCondsBodies)
353+
<> labeledIndent el ("end if" <+> pprint' v mName <> newline)
354+
| otherwise = tooOld v "Structured if" Fortran77
355+
where
356+
((firstCond, firstBody): restCondsBodies) = zip conds bodies
357+
displayCondBlock (mCond, block) =
358+
indent i
359+
(case mCond of {
360+
Just cond -> "else if" <+> parens (pprint' v cond) <+> "then";
361+
Nothing -> "else"
362+
} <> newline) <>
363+
pprint v block nextI
364+
nextI = incIndentation i
365+
labeledIndent label stDoc =
366+
if v >= Fortran90
367+
then indent i (pprint' v label <+> stDoc)
368+
else pprint' v mLabel `overlay` indent i stDoc
369+
-}
338370

339371
pprint v (BlComment _ _ (Comment comment)) i
340372
| v >= Fortran90 = indent i (char '!' <> text comment <> newline)
@@ -353,6 +385,10 @@ instance Pretty String where
353385
instance Pretty (e a) => Pretty (AList e a) where
354386
pprint' v es = commaSep (map (pprint' v) (aStrip es))
355387

388+
-- TODO associate
389+
instance (Pretty (t1 a), Pretty (t2 a)) => Pretty (ATuple t1 t2 a) where
390+
pprint' v (ATuple _ _ t1 t2) = pprint' v t1 <+> "=>" <+> pprint' v t2
391+
356392
instance Pretty BaseType where
357393
pprint' _ TypeInteger = "integer"
358394
pprint' _ TypeReal = "real"

0 commit comments

Comments
 (0)