Changeset 13216 for src/Pugs/Val

Show
Ignore:
Timestamp:
09/12/06 12:46:26 (2 years ago)
Author:
gaal
Message:

* Pretty printing of Signature objects.

  • Should work ok (see t/syntax/signature.t): invocant, positional, named, optional/required, parameter traits.
  • Partially supported: :(MyType? $x), :($x = "default") (lossy/messy output)
  • Still not perfect. Please add more tests!
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Val/Code.hs

    r13087 r13216  
    120120    deriving (Typeable) 
    121121 
    122 data ParamDefault 
    123     = DNil | DExp Exp 
     122newtype ParamDefault = MkParamDefault { unDefault :: Maybe Exp } 
    124123    deriving (Typeable) 
    125124 
    126125instance Eq ParamDefault where _ == _ = True 
    127126instance Ord ParamDefault where compare _ _ = EQ 
    128 instance Show ParamDefault where show _ = "<Param.Default>" 
     127instance Show ParamDefault where 
     128    show MkParamDefault{ unDefault = Nothing } = "<ParamDefault:Nothing>" 
     129    show _    = "<ParamDefault:Just<Exp>>" 
    129130 
    130131instance Eq CodeBody where _ == _ = True 
     
    139140 
    140141instance ICoercible P Sig where 
    141         asStr _ = return (cast "<sig>")  -- XXX 
    142  
    143 instance Pure Sig where {} 
    144          
    145  
     142        asStr = return . cast . render . purePretty 
     143 
     144instance Pure Sig where 
     145    purePretty s = colon <> (parens $ prettySig s) 
     146     
     147prettySig :: Sig -> Doc 
     148prettySig s@(SigMethSingle {}) = (prettyParam (s_invocant s) True True) <> colon `invSpace` (prettySubSig s) 
     149    where 
     150    invSpace :: Doc -> Doc -> Doc 
     151    invSpace = if (isEmpty $ prettySubSig s) then (<>) else (<+>) 
     152prettySig s = prettySubSig s 
     153 
     154prettySubSig :: Sig -> Doc 
     155prettySubSig s = sep $ punctuate comma $ concat [posParams, namedParams] 
     156    where 
     157    posParams = [prettyParam p r True | p <- (s_positionalList s) | r <- (replicate (s_requiredPositionalCount s) True) ++ repeat False] 
     158    namedParams = [prettyParam p (isReqNamed n) False | (n, p) <- Map.toList $ s_namedSet s] 
     159    isReqNamed n = Set.member n $ s_requiredNames s 
     160 
     161prettyParam :: Param -> Bool -> Bool -> Doc 
     162prettyParam p isReq isPos = staticTypes <+> varName <> defaultHint <+> 
     163    (if haveDefault then equals <+> text "..." else empty) <+> acc <+> ref <+> lazy <+> 
     164    slots <+> constraints <+> debugDump 
     165    where 
     166    varName 
     167        | isPos = text (cast $ p_variable p) 
     168        | Buf.tail (cast $ p_variable p) == (cast $ p_label p) = text $ ":" ++ (cast $ p_variable p) 
     169        | otherwise = text ":" <> text (cast p_label p) <> (parens $ text (cast p_variable p)) 
     170    -- staticTypes = hsep $ map (text . (cast :: Types.Type -> String)) $ p_types p XXX: why is this wrong? 
     171    staticTypes = hsep $ map (text . show) $ p_types p 
     172    defaultHint = if not isReq && not haveDefault then text "?" else empty 
     173    haveDefault = isJust $ unDefault $ p_default p 
     174    acc = case p_hasAccess p of 
     175        AccessRO   -> empty 
     176        AccessRW   -> text "is rw" 
     177        AccessCopy -> text "is copy" 
     178    ref   = if p_isRef  p then text "is ref"  else empty 
     179    lazy  = if p_isLazy p then text "is lazy" else empty 
     180    -- slots = hsep [text ("is " ++ (cast aux)) <+> text "..." | (aux, val) <- Map.toList $ p_slots p] XXX: for when traits have args 
     181    slots = hsep [text ("is " ++ (cast $ fst trait)) | trait <- Map.toList $ p_slots p] 
     182    constraints = hsep $ replicate (length $ p_constraints p) (text "where {...}") 
     183    debugDump = if True then empty else braces $ text $ show p -- XXX delme 
    146184-------------------------------------------------------------------------------------- 
    147185