Skip to content

Commit a7a265c

Browse files
committed
More printing
1 parent 7757f30 commit a7a265c

File tree

7 files changed

+75
-42
lines changed

7 files changed

+75
-42
lines changed

dex.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ library
114114
, megaparsec
115115
, parser-combinators
116116
-- Text output
117-
, prettyprinter
117+
-- , prettyprinter
118118
, text
119119
-- Portable system utilities
120120
, directory
@@ -232,7 +232,7 @@ executable dex
232232
, haskeline
233233
, mtl
234234
, optparse-applicative
235-
, prettyprinter
235+
-- , prettyprinter
236236
, store
237237
, text
238238
, unix

src/lib/PPrint.hs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
-- license that can be found in the LICENSE file or at
55
-- https://developers.google.com/open-source/licenses/bsd
66

7-
module PPrint (Pretty (..), Doc (..), indent, hcat, vcat, pprint, app) where
7+
module PPrint (Pretty (..), Doc (..), indent, hcat, hlist, vcat, pprint, app) where
88

99
import Data.Int
10+
import Data.Word
11+
import Data.List (intersperse)
1012
import Data.String
1113
import Control.Monad.Reader
1214
import Control.Monad.State.Strict
@@ -59,7 +61,8 @@ vcat :: [Doc] -> Doc
5961
vcat = DocItems
6062

6163
hlist :: String -> [Doc] -> Doc
62-
hlist [l,sep,r] xs = undefined
64+
hlist [l,sep,r] xs = hcat [pr l, hcat (intersperse (pr sep) xs), pr r]
65+
6366
hlist _ _ = error "expected left bracket, separator, right bracket"
6467

6568
hcat :: [Doc] -> Doc
@@ -78,7 +81,7 @@ indent = DocIndent
7881
app :: Doc -> [Doc] -> Doc
7982
app f xs = hcat [f, hlist "(,)" xs]
8083

81-
-- === instances
84+
-- === instances ===
8285

8386
instance IsString Doc where
8487
fromString = DocLine
@@ -93,7 +96,14 @@ instance Pretty a => Pretty [a] where
9396
instance (Pretty a, Pretty b) => Pretty (a, b) where
9497
pr (x, y) = hcat ["(", pr x, ", ", pr y, ")"]
9598

96-
instance Pretty Int where pr x = pr $ show x
97-
instance Pretty Int32 where pr x = pr $ show x
98-
instance Pretty Int64 where pr x = pr $ show x
99-
instance Pretty Float where pr x = pr $ show x
99+
instance Pretty a => Pretty (Maybe a) where
100+
pr = \case
101+
Nothing -> ""
102+
Just x -> pr x
103+
104+
instance Pretty Int where pr x = pr $ show x
105+
instance Pretty Int32 where pr x = pr $ show x
106+
instance Pretty Int64 where pr x = pr $ show x
107+
instance Pretty Float where pr x = pr $ show x
108+
instance Pretty Double where pr x = pr $ show x
109+
instance Pretty Word64 where pr x = pr $ show x

src/lib/Types/Complicated.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,18 @@ data DataConDef n =
176176

177177
instance GenericE CExpr where
178178
type RepE CExpr = UnitE
179-
instance Pretty (CExpr n)
179+
180+
instance Pretty (CExpr n) where
181+
pr = \case
182+
CBlock _ b -> pr b
183+
CVar v _ -> pr v
184+
CLit l -> pr l
185+
CPrimOp _ op -> pr op
186+
CTyCon _ -> undefined
187+
Lam _ -> undefined
188+
NewtypeCon _ _ -> undefined
189+
Dict _ -> undefined
190+
180191
instance SinkableE CExpr
181192
instance HoistableE CExpr
182193
instance RenameE CExpr

src/lib/Types/Primitives.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ instance Pretty ScalarBaseType where
381381
Word32Type -> "Word32"
382382
Word64Type -> "Word64"
383383

384+
instance Pretty BinOp where pr x = pr $ show x
385+
instance Pretty UnOp where pr x = pr $ show x
386+
384387
instance Pretty a => Pretty (PrimOp a) where
385388
pr = \case
386389
MemOp op -> pr op
@@ -390,8 +393,8 @@ instance Pretty a => Pretty (PrimOp a) where
390393
MPut x -> app "(:=)" [pr ref, pr x]
391394
IndexRef i -> app "(!)" [pr ref, pr i]
392395
ProjRef i -> app "proj_ref" [pr ref, pr i]
393-
UnOp op x -> undefined
394-
BinOp op x y -> undefined
396+
UnOp op x -> app (pr op) [pr x]
397+
BinOp op x y -> app (pr op) [pr x, pr y]
395398
MiscOp op -> undefined
396399

397400
instance Pretty Projection where

src/lib/Types/Source.hs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -905,9 +905,18 @@ instance Pretty CSBlock where
905905
pr (ExprBlock g) = pr g
906906

907907
instance Pretty Group where
908-
pr = undefined
909-
-- prettyPrec (CIdentifier n) = atPrec ArgPrec $ fromString n
910-
-- prettyPrec (CPrim prim args) = prettyOpDefault prim args
908+
pr = \case
909+
CLeaf leaf -> pr leaf
910+
CPrim prim args -> app (pr prim) (map pr args)
911+
912+
913+
-- prettyOpDefault :: PrettyPrec a => PrimName -> [a] -> DocPrec ann
914+
-- prettyOpDefault name args =
915+
-- case length args of
916+
-- 0 -> atPrec ArgPrec primName
917+
-- _ -> atPrec AppPrec $ pAppArg primName args
918+
-- where primName = pretty name
919+
911920
-- prettyPrec (CParens blk) =
912921
-- atPrec ArgPrec $ "(" <> p blk <> ")"
913922
-- prettyPrec (CBrackets g) = atPrec ArgPrec $ pretty g
@@ -919,6 +928,16 @@ instance Pretty Group where
919928
-- atPrec LowestPrec $ "case " <> p scrut <> " of " <> prettyLines alts
920929
-- prettyPrec g = atPrec ArgPrec $ fromString $ show g
921930

931+
instance Pretty CLeaf where
932+
pr = \case
933+
CIdentifier s -> pr s
934+
CNat n -> pr n
935+
CInt n -> pr n
936+
CString s -> pr $ show s
937+
CChar c -> pr $ show c
938+
CFloat f -> pr f
939+
CHole -> "_"
940+
922941
instance Pretty Bin where
923942
pr = \case
924943
EvalBinOp name -> pr name
@@ -945,8 +964,7 @@ instance Pretty CTopDecl where
945964
pr d = fromString $ show d
946965

947966
instance Pretty CSDecl where
948-
pr = undefined
949-
-- pr (CLet pat blk) = pArg pat <+> "=" <+> p blk
967+
pr (CLet pat blk) = hcat [pr pat, "=", pr blk]
950968
-- pr (CDefDecl (CDef name args maybeAnn blk)) =
951969
-- "def " <> fromString name <> " " <> prParamGroups args <+> annDoc
952970
-- <> nest 2 (hardline <> p blk)
@@ -990,8 +1008,9 @@ instance Pretty (UAlt n) where
9901008
pr (UAlt pat body) = undefined -- pr pat <+> "->" <+> pr body
9911009

9921010
instance Pretty UTopDecl where
993-
pr = undefined
994-
-- pretty = \case
1011+
pr = \case
1012+
UTopLet b _ expr -> hcat [pr b, " = ", pr expr]
1013+
-- (Maybe (UType VoidS)) (UExpr VoidS)
9951014
-- UDataDefDecl (UDataDef nm bs dataCons) bTyCon bDataCons ->
9961015
-- "enum" <+> p bTyCon <+> p nm <+> spaced (unsafeFromNest bs) <+> "where" <> nest 2
9971016
-- (prettyLines (zip (toList $ unsafeFromNest bDataCons) dataCons))
@@ -1056,11 +1075,9 @@ instance Pretty (UBlock' n) where
10561075
-- prettyLines (unsafeFromNest decls) <> hardline <> pLowest result
10571076

10581077
instance Pretty (UExpr' n) where
1059-
pr = undefined
1060-
-- instance PrettyPrec (UExpr' n) where
1061-
-- prettyPrec expr = case expr of
1062-
-- ULit l -> prettyPrec l
1063-
-- UVar v -> atPrec ArgPrec $ p v
1078+
pr = \case
1079+
ULit l -> pr l
1080+
UVar v -> pr v
10641081
-- ULam lam -> prettyPrec lam
10651082
-- UApp f xs named -> atPrec AppPrec $ pAppArg (pApp f) xs <+> p named
10661083
-- UTabApp f x -> atPrec AppPrec $ pArg f <> "." <> pArg x
@@ -1078,20 +1095,20 @@ instance Pretty (UExpr' n) where
10781095
-- UTypeAnn v ty -> atPrec LowestPrec $
10791096
-- group $ pApp v <> line <> ":" <+> pApp ty
10801097
-- UTabCon xs -> atPrec ArgPrec $ p xs
1081-
-- UPrim prim xs -> atPrec AppPrec $ p (show prim) <+> p xs
1098+
UPrim prim xs -> app (pr prim) (map pr xs)
10821099
-- UCase e alts -> atPrec LowestPrec $ "case" <+> p e <>
10831100
-- nest 2 (prettyLines alts)
10841101
-- UFieldAccess x (WithSrc _ f) -> atPrec AppPrec $ p x <> "~" <> p f
1085-
-- UNatLit v -> atPrec ArgPrec $ p v
1086-
-- UIntLit v -> atPrec ArgPrec $ p v
1087-
-- UFloatLit v -> atPrec ArgPrec $ p v
1102+
UNatLit v -> pr v
1103+
UIntLit v -> pr v
1104+
UFloatLit v -> pr v
10881105
-- UDo block -> atPrec LowestPrec $ p block
10891106
-- where
10901107
-- p :: Pretty a => a -> Doc ann
10911108
-- p = pretty
10921109

10931110
instance Pretty SourceBlock where
1094-
pr block = undefined
1111+
pr block = pr $ sbContents block
10951112
-- pr $ ensureNewline (sbText block) where
10961113
-- -- Force the SourceBlock to end in a newline for echoing, even if
10971114
-- -- it was terminated with EOF in the original program.
@@ -1105,7 +1122,7 @@ instance Pretty Output where
11051122
TextOut s -> pr s
11061123
HtmlOut _ -> "<html output>"
11071124
SourceInfo _ -> ""
1108-
PassResult _ s -> undefined -- pr s
1125+
PassResult _ s -> pr s
11091126
MiscLog s -> pr s
11101127
Error e -> pr e
11111128

@@ -1122,10 +1139,3 @@ instance Pretty FieldName' where
11221139
pr = \case
11231140
FieldName s -> pr s
11241141
FieldNum n -> pr n
1125-
1126-
-- prettyOpDefault :: PrettyPrec a => PrimName -> [a] -> DocPrec ann
1127-
-- prettyOpDefault name args =
1128-
-- case length args of
1129-
-- 0 -> atPrec ArgPrec primName
1130-
-- _ -> atPrec AppPrec $ pAppArg primName args
1131-
-- where primName = pretty name

src/lib/Types/Top2.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module Types.Top2 where
1313

1414
import Data.Functor ((<&>))
1515
import Data.Hashable
16-
import Data.Text.Prettyprint.Doc
1716
import qualified Data.Map.Strict as M
1817
import qualified Data.Set as S
1918

src/lib/Util.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import qualified Data.Text.Encoding as T
2828
import qualified Data.List.NonEmpty as NE
2929
import qualified Data.ByteString as BS
3030
import Data.Foldable
31-
import Data.Text.Prettyprint.Doc (Pretty (..), pretty)
3231
import Data.List.NonEmpty (NonEmpty (..))
3332
import GHC.Generics (Generic)
3433

3534
import Err
35+
import PPrint
3636

3737
class IsBool a where
3838
toBool :: a -> Bool
@@ -365,9 +365,9 @@ zipTrees (Branch xs) (Branch ys) | length xs == length ys = Branch $ zipWith zip
365365
zipTrees _ _ = error "zip error"
366366

367367
instance Pretty a => Pretty (Tree a) where
368-
pretty = \case
369-
Leaf x -> pretty x
370-
Branch xs -> pretty xs
368+
pr = \case
369+
Leaf x -> pr x
370+
Branch xs -> pr xs
371371

372372
readFileText :: MonadIO m => FilePath -> m T.Text
373373
readFileText fname = liftIO $ T.decodeUtf8 <$> BS.readFile fname

0 commit comments

Comments
 (0)