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.Interpreting.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.
22 { unTextI -- Inherited attributes:
25 -- Synthetised attributes:
28 type TextI_Lambda_Depth = Int
29 instance Show (TextI h) where
30 show = Text.unpack . text_from_term
33 text_from_term :: TextI h -> Text
34 text_from_term r = unTextI r precedence_Toplevel 0
38 -- ** Helpers for lambda applications
39 textI_app0 :: Text -> TextI h
40 textI_app0 name = TextI $ \_p _v -> name
45 textI_app1 name (TextI a1) =
47 let p' = precedence_App in
55 textI_app2 name (TextI a1) (TextI a2) =
57 let p' = precedence_App in
67 textI_app3 name (TextI a1) (TextI a2) (TextI a3) =
69 let p' = precedence_App in
80 textI_infix name p' (TextI a1) (TextI a2) =
82 paren p p' $ a1 p' v <> " " <> name <> " " <> a2 p' v
84 -- ** Type 'Precedence'
86 newtype Precedence = Precedence Int
87 deriving (Eq, Ord, Show)
88 precedence_pred :: Precedence -> Precedence
89 precedence_pred (Precedence p) = Precedence (pred p)
90 precedence_succ :: Precedence -> Precedence
91 precedence_succ (Precedence p) = Precedence (succ p)
92 paren :: (Monoid s, IsString s) => Precedence -> Precedence -> s -> s
95 then fromString "(" <> x <> fromString ")"
98 precedence_Toplevel :: Precedence
99 precedence_Toplevel = Precedence 0
100 precedence_App :: Precedence
101 precedence_App = Precedence 10
102 precedence_Atomic :: Precedence
103 precedence_Atomic = Precedence maxBound