1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE OverloadedStrings #-}
6 {-# LANGUAGE TypeFamilies #-}
7 {-# LANGUAGE UndecidableInstances #-}
8 -- | Interpreter to serialize an expression into a 'Text'.
9 module Language.Symantic.Repr.Text where
11 import Data.Monoid ((<>))
12 import Data.String (IsString(..))
13 import Data.Text (Text)
14 import qualified Data.Text as Text
15 import Prelude hiding (Integral(..))
19 -- | Interpreter's data.
23 -- Inherited attributes:
25 -> Repr_Text_Lambda_Depth
26 -- Synthetised attributes:
29 type Repr_Text_Lambda_Depth = Int
30 instance Show (Repr_Text h) where
31 show = Text.unpack . text_from_expr
34 text_from_expr :: Repr_Text h -> Text
35 text_from_expr r = unRepr_Text r precedence_Toplevel 0
39 -- ** Helpers for lambda applications
44 repr_text_app1 name (Repr_Text a1) =
46 let p' = precedence_App in
54 repr_text_app2 name (Repr_Text a1) (Repr_Text a2) =
56 let p' = precedence_App in
66 repr_text_app3 name (Repr_Text a1) (Repr_Text a2) (Repr_Text a3) =
68 let p' = precedence_App in
79 repr_text_infix name p' (Repr_Text a1) (Repr_Text a2) =
81 paren p p' $ a1 p' v <> " " <> name <> " " <> a2 p' v
83 -- ** Type 'Precedence'
85 newtype Precedence = Precedence Int
86 deriving (Eq, Ord, Show)
87 precedence_pred :: Precedence -> Precedence
88 precedence_pred (Precedence p) = Precedence (pred p)
89 precedence_succ :: Precedence -> Precedence
90 precedence_succ (Precedence p) = Precedence (succ p)
91 paren :: (Monoid s, IsString s) => Precedence -> Precedence -> s -> s
94 then fromString "(" <> x <> fromString ")"
97 precedence_Toplevel :: Precedence
98 precedence_Toplevel = Precedence 0
99 precedence_App :: Precedence
100 precedence_App = Precedence 10
101 precedence_Atomic :: Precedence
102 precedence_Atomic = Precedence maxBound