Skip to content
Snippets Groups Projects
Commit 276f03b2 authored by sof's avatar sof
Browse files

[project @ 1999-07-05 17:06:21 by sof]

Tidied up the handling of the code that emits the precedence level
predicates that gets used in applications of showParen and readParen.
parent ee731a02
No related branches found
No related tags found
No related merge requests found
...@@ -34,7 +34,9 @@ import HsSyn ( InPat(..), HsExpr(..), MonoBinds(..), ...@@ -34,7 +34,9 @@ import HsSyn ( InPat(..), HsExpr(..), MonoBinds(..),
import RdrHsSyn ( mkOpApp, RdrNameMonoBinds, RdrNameHsExpr, RdrNamePat ) import RdrHsSyn ( mkOpApp, RdrNameMonoBinds, RdrNameHsExpr, RdrNamePat )
import RdrName ( RdrName, mkSrcUnqual ) import RdrName ( RdrName, mkSrcUnqual )
import RnMonad ( Fixities ) import RnMonad ( Fixities )
import BasicTypes ( RecFlag(..), Fixity(..), FixityDirection(..) ) import BasicTypes ( RecFlag(..), Fixity(..), FixityDirection(..)
, maxPrecedence, defaultFixity
)
import FieldLabel ( fieldLabelName ) import FieldLabel ( fieldLabelName )
import DataCon ( isNullaryDataCon, dataConTag, import DataCon ( isNullaryDataCon, dataConTag,
dataConRawArgTys, fIRST_TAG, dataConRawArgTys, fIRST_TAG,
...@@ -57,7 +59,7 @@ import TysPrim ( charPrimTy, intPrimTy, wordPrimTy, addrPrimTy, ...@@ -57,7 +59,7 @@ import TysPrim ( charPrimTy, intPrimTy, wordPrimTy, addrPrimTy,
floatPrimTy, doublePrimTy floatPrimTy, doublePrimTy
) )
import Util ( mapAccumL, zipEqual, zipWithEqual, import Util ( mapAccumL, zipEqual, zipWithEqual,
zipWith3Equal, nOfThem ) zipWith3Equal, nOfThem, assocDefault )
import Panic ( panic, assertPanic ) import Panic ( panic, assertPanic )
import Maybes ( maybeToBool, assocMaybe ) import Maybes ( maybeToBool, assocMaybe )
import Constants import Constants
...@@ -897,7 +899,7 @@ gen_Read_binds fixities tycon ...@@ -897,7 +899,7 @@ gen_Read_binds fixities tycon
then d_Expr then d_Expr
else HsVar (last bs_needed)] True else HsVar (last bs_needed)] True
[lp,rp] = getLRPrecs fixities dc_nm [lp,rp] = getLRPrecs is_infix fixities dc_nm
quals quals
| is_infix = let (h:t) = field_quals in (h:con_qual:t) | is_infix = let (h:t) = field_quals in (h:con_qual:t)
...@@ -905,8 +907,11 @@ gen_Read_binds fixities tycon ...@@ -905,8 +907,11 @@ gen_Read_binds fixities tycon
stmts = quals ++ [ReturnStmt result_expr] stmts = quals ++ [ReturnStmt result_expr]
{-
c.f. Figure 18 in Haskell 1.1 report.
-}
paren_prec_limit paren_prec_limit
| not is_infix = 9 | not is_infix = fromInt maxPrecedence
| otherwise = getFixity fixities dc_nm | otherwise = getFixity fixities dc_nm
read_paren_arg -- parens depend on precedence... read_paren_arg -- parens depend on precedence...
...@@ -939,7 +944,7 @@ gen_Show_binds fixs_assoc tycon ...@@ -939,7 +944,7 @@ gen_Show_binds fixs_assoc tycon
([wildPat, con_pat], show_con) ([wildPat, con_pat], show_con)
| otherwise = | otherwise =
([a_Pat, con_pat], ([a_Pat, con_pat],
showParen_Expr (HsPar (genOpApp a_Expr gt_RDR (HsLit (HsInt paren_prec_limit)))) showParen_Expr (HsPar (genOpApp a_Expr ge_RDR (HsLit (HsInt paren_prec_limit))))
(HsPar (nested_compose_Expr show_thingies))) (HsPar (nested_compose_Expr show_thingies)))
where where
data_con_RDR = qual_orig_name data_con data_con_RDR = qual_orig_name data_con
...@@ -994,7 +999,7 @@ gen_Show_binds fixs_assoc tycon ...@@ -994,7 +999,7 @@ gen_Show_binds fixs_assoc tycon
mk_showString_app str = HsApp (HsVar showString_RDR) mk_showString_app str = HsApp (HsVar showString_RDR)
(HsLit (mkHsString str)) (HsLit (mkHsString str))
prec_cons = getLRPrecs fixs_assoc dc_nm prec_cons = getLRPrecs is_infix fixs_assoc dc_nm
real_show_thingies real_show_thingies
| is_infix = | is_infix =
...@@ -1017,41 +1022,51 @@ gen_Show_binds fixs_assoc tycon ...@@ -1017,41 +1022,51 @@ gen_Show_binds fixs_assoc tycon
(con_left_assoc, con_right_assoc) = isLRAssoc fixs_assoc dc_nm (con_left_assoc, con_right_assoc) = isLRAssoc fixs_assoc dc_nm
{-
c.f. Figure 16 and 17 in Haskell 1.1 report
-}
paren_prec_limit paren_prec_limit
| not is_infix = 9 | not is_infix = fromInt maxPrecedence + 1
| otherwise = getFixity fixs_assoc dc_nm | otherwise = getFixity fixs_assoc dc_nm + 1
\end{code} \end{code}
\begin{code} \begin{code}
getLRPrecs :: Fixities -> Name -> [Integer] getLRPrecs :: Bool -> Fixities -> Name -> [Integer]
getLRPrecs fixs_assoc nm = [lp, rp] getLRPrecs is_infix fixs_assoc nm = [lp, rp]
where where
( con_left_assoc, con_right_assoc) = isLRAssoc fixs_assoc nm {-
paren_prec_limit = 9 Figuring out the fixities of the arguments to a constructor,
cf. Figures 16-18 in Haskell 1.1 report.
-}
(con_left_assoc, con_right_assoc) = isLRAssoc fixs_assoc nm
paren_con_prec = getFixity fixs_assoc nm
maxPrec = fromInt maxPrecedence
lp lp
| con_left_assoc = paren_prec_limit | not is_infix = maxPrec + 1
| otherwise = paren_prec_limit + 1 | con_left_assoc = paren_con_prec
| otherwise = paren_con_prec + 1
rp rp
| con_right_assoc = paren_prec_limit | not is_infix = maxPrec + 1
| otherwise = paren_prec_limit + 1 | con_right_assoc = paren_con_prec
| otherwise = paren_con_prec + 1
getFixity :: Fixities -> Name -> Integer getFixity :: Fixities -> Name -> Integer
getFixity fixs_assoc nm = getFixity fixs_assoc nm =
case assocMaybe fixs_assoc nm of case lookupFixity fixs_assoc nm of
Nothing -> 9 Fixity x _ -> fromInt x
Just (Fixity x _) -> fromInt x + 1
isLRAssoc :: Fixities -> Name -> (Bool, Bool) isLRAssoc :: Fixities -> Name -> (Bool, Bool)
isLRAssoc fixs_assoc nm = isLRAssoc fixs_assoc nm =
case assocMaybe fixs_assoc nm of case lookupFixity fixs_assoc nm of
Just (Fixity _ InfixL) -> (True, False) Fixity _ InfixN -> (False, False)
Just (Fixity _ InfixR) -> (False, True) Fixity _ InfixR -> (False, True)
Just (Fixity _ _) -> (False, False) Fixity _ InfixL -> (True, False)
_ -> (True, False)
lookupFixity :: Fixities -> Name -> Fixity
lookupFixity fixs_assoc nm = assocDefault defaultFixity fixs_assoc nm
\end{code} \end{code}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment