1{-# LANGUAGE OverloadedStrings #-}
2
3-- | This module implement a 'Formatter' for Scheme procedure definitions.
4--
5-- For example:
6--
7-- > (define (my-proc x1 x2)
8-- > (* x1 x2))
9module SchemeDoc.Format.Procedure (Procedure (..), mkProcedure) where
10
11import qualified Data.Text as T
12
13import SchemeDoc.Format.Types
14import SchemeDoc.Format.Util
15import SchemeDoc.Types
16
17-- | A R7RS Scheme procedure definition.
18data Procedure = Procedure
19 { name :: T.Text
20 -- ^ Identifier, i.e. procedure name.
21 , params :: [T.Text]
22 -- ^ Procedure parameters.
23 , body :: [Sexp]
24 -- ^ Procedure body.
25 }
26 deriving (Eq, Show)
27
28instance Formatable Procedure where
29 fmt (Procedure{name = internalId, params = p}) desc =
30 mkDeclaration internalId desc $ \n ->
31 do
32 component "procedure" n
33 fromMkd desc
34 htmlSexp $ List (Id n : map Id p)
35
36-- | Parses a Scheme procedure definition.
37--
38-- > <procedure> → (define (<identifier> <def formals>) <body>)
39--
40-- where
41--
42-- > <def formals> → <identifier>* | <identifier>* . <identifier>
43mkProcedure :: Sexp -> Maybe Procedure
44mkProcedure (List ((Id "define") : (List ((Id defid) : arglst)) : bodylst)) =
45 (flip $ Procedure defid) bodylst
46 <$> mapM (onId id) arglst
47mkProcedure _ = Nothing