1{-# LANGUAGE OverloadedStrings #-}
2
3-- | This module implements a 'Formatter' for Scheme variable definition.
4--
5-- For example:
6--
7-- > (define x 42)
8module SchemeDoc.Format.Variable (Variable (..), mkVariable) where
9
10import qualified Data.Text as T
11import SchemeDoc.Format.Types
12import SchemeDoc.Format.Util
13import SchemeDoc.Types
14
15-- | A R7RS Scheme variable definition.
16data Variable = Variable
17 { name :: T.Text
18 -- ^ Identifier, i.e. variable name.
19 , value :: Sexp
20 -- ^ Value assigned to the variable name.
21 }
22 deriving (Eq, Show)
23
24instance Formatable Variable where
25 fmt (Variable internalId _) desc =
26 mkDeclaration internalId desc $ \n ->
27 do
28 component "constant " n
29 fromMkd desc
30 htmlSexp (Id n)
31
32-- | Parses a Scheme definition.
33--
34-- > <definition> → (define <identifier> <expression>)
35mkVariable :: Sexp -> Maybe Variable
36mkVariable (List [Id "define", Id n, expr]) =
37 Just $ Variable n expr
38mkVariable _ = Nothing