pretty printer
uses ISeq, as described in 'Implementing Functional Languages'. going to try using a style similar to ShowS instead.
This commit is contained in:
@@ -22,6 +22,7 @@ library
|
|||||||
, TI
|
, TI
|
||||||
other-modules: Data.Heap
|
other-modules: Data.Heap
|
||||||
, Control.DFA
|
, Control.DFA
|
||||||
|
, Data.Pretty
|
||||||
|
|
||||||
-- other-extensions:
|
-- other-extensions:
|
||||||
build-depends: base ^>=4.18.0.0
|
build-depends: base ^>=4.18.0.0
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
module Core where
|
module Core where
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
import Data.Coerce
|
import Data.Coerce
|
||||||
|
import Data.Pretty
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
|
|
||||||
data Expr = Var Name
|
data Expr = Var Name
|
||||||
@@ -39,6 +40,11 @@ newtype Program = Program [ScDef]
|
|||||||
|
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
instance Pretty Expr where
|
||||||
|
prettyPrec _ (Var k) = iStr k
|
||||||
|
|
||||||
|
----------------------------------------------------------------------------------
|
||||||
|
|
||||||
instance Semigroup Program where
|
instance Semigroup Program where
|
||||||
(<>) = coerce $ (++) @ScDef
|
(<>) = coerce $ (++) @ScDef
|
||||||
|
|
||||||
|
|||||||
54
src/Data/Pretty.hs
Normal file
54
src/Data/Pretty.hs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
module Data.Pretty
|
||||||
|
( Pretty(..)
|
||||||
|
, ISeq
|
||||||
|
, iNil
|
||||||
|
, iStr
|
||||||
|
, iAppend
|
||||||
|
)
|
||||||
|
where
|
||||||
|
----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class Pretty a where
|
||||||
|
pretty :: a -> String
|
||||||
|
prettyPrec :: Int -> a -> ISeq
|
||||||
|
|
||||||
|
pretty = squash . prettyPrec 0
|
||||||
|
prettyPrec _ a = iBracket (iStr $ pretty a)
|
||||||
|
|
||||||
|
data ISeq where
|
||||||
|
INil :: ISeq
|
||||||
|
IStr :: String -> ISeq
|
||||||
|
IAppend :: ISeq -> ISeq -> ISeq
|
||||||
|
|
||||||
|
instance Semigroup ISeq where
|
||||||
|
(<>) = IAppend
|
||||||
|
|
||||||
|
instance Monoid ISeq where
|
||||||
|
mempty = INil
|
||||||
|
|
||||||
|
squash :: ISeq -> String
|
||||||
|
squash = flatten . pure
|
||||||
|
|
||||||
|
flatten :: [ISeq] -> String
|
||||||
|
flatten (INil : ss) = flatten ss
|
||||||
|
flatten (IStr s : ss) = s ++ flatten ss
|
||||||
|
flatten (IAppend r s : ss) = flatten (r : s : ss)
|
||||||
|
|
||||||
|
iNil :: ISeq
|
||||||
|
iNil = INil
|
||||||
|
|
||||||
|
iStr :: String -> ISeq
|
||||||
|
iStr = IStr
|
||||||
|
|
||||||
|
iAppend :: ISeq -> ISeq -> ISeq
|
||||||
|
iAppend = IAppend
|
||||||
|
|
||||||
|
iIndent :: ISeq -> ISeq
|
||||||
|
iIndent = id
|
||||||
|
|
||||||
|
iBreak :: ISeq
|
||||||
|
iBreak = iStr "\n"
|
||||||
|
|
||||||
|
iBracket :: ISeq -> ISeq
|
||||||
|
iBracket s = iStr "(" `iAppend` s `iAppend` iStr ")"
|
||||||
|
|
||||||
Reference in New Issue
Block a user