1 {-# LANGUAGE UndecidableInstances #-}
2 -- | Interpreter to serialize an expression into a 'Text'.
3 module Language.Symantic.Interpreting.Text where
5 import Data.Monoid ((<>))
6 import Data.String (IsString(..))
7 import Data.Text (Text)
8 import qualified Data.Text as Text
9 import Prelude hiding (Integral(..))
13 -- | Interpreter's data.
16 { unTextI -- Inherited attributes:
19 -- Synthetised attributes:
22 type TextI_Lambda_Depth = Int
23 instance Show (TextI h) where
24 show = Text.unpack . text_from_term
27 text_from_term :: TextI h -> Text
28 text_from_term r = unTextI r precedence_Toplevel 0
32 -- ** Helpers for lambda applications
33 textI_app0 :: Text -> TextI h
34 textI_app0 name = TextI $ \_p _v -> name
39 textI_app1 name (TextI a1) =
41 let p' = precedence_App in
49 textI_app2 name (TextI a1) (TextI a2) =
51 let p' = precedence_App in
61 textI_app3 name (TextI a1) (TextI a2) (TextI a3) =
63 let p' = precedence_App in
74 textI_infix name p' (TextI a1) (TextI a2) =
76 paren p p' $ a1 p' v <> " " <> name <> " " <> a2 p' v
78 -- ** Type 'Precedence'
80 newtype Precedence = Precedence Int
81 deriving (Eq, Ord, Show)
82 precedence_pred :: Precedence -> Precedence
83 precedence_pred (Precedence p) = Precedence (pred p)
84 precedence_succ :: Precedence -> Precedence
85 precedence_succ (Precedence p) = Precedence (succ p)
86 paren :: (Monoid s, IsString s) => Precedence -> Precedence -> s -> s
89 then fromString "(" <> x <> fromString ")"
92 precedence_Toplevel :: Precedence
93 precedence_Toplevel = Precedence 0
94 precedence_App :: Precedence
95 precedence_App = Precedence 10
96 precedence_Atomic :: Precedence
97 precedence_Atomic = Precedence maxBound