This repository has been archived by the owner on Aug 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Pretty.hs
140 lines (124 loc) · 4.27 KB
/
Pretty.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
-- | Pretty printer for Textual AST.
{-# LANGUAGE FlexibleInstances #-}
module Language.Wasm.Pretty (
pretty,
display,
) where
import Prelude hiding ((<$>))
import Language.Wasm.Syntax
import qualified Data.Text as T
import Text.PrettyPrint.ANSI.Leijen
instance Pretty Name where
pretty (Name t) = text (T.unpack t)
pretty (UnName n) = pretty (fromIntegral n :: Int)
instance Pretty Expr where
prettyList = nest 4 . vsep . map (parens . pretty)
pretty expr = case expr of
Nop -> text "nop"
Unreachable -> text "unreachable"
Block name e -> text "block" <+> maybe empty pretty name <$> pretty e
If cond true -> text "if" <+> pretty [cond,true]
IfElse cond true false -> text "if_else" <+> pretty [cond,true,false]
BrIf cond name e -> text "todo"
Loop{} -> text "todo"
Br name e -> pretty [text "br" <+> pretty name]
Return e -> text "return" <+> parens (pretty e)
Call name _ -> text "todo"
Const typ value -> pretty typ <> dot <> text "const" <+> pretty value
Lit value -> pretty value
Load memop e -> text "todo"
Store memop e -> text "todo"
GetLocal name -> text "get_local" <+> pretty name
SetLocal name e -> text "set_local" <+> pretty name <+> parens (pretty e)
LoadExtend extop e -> text "todo"
StoreWrap wrapop e1 e2 -> text "todo"
Bin binop typ e1 e2 -> pretty typ <> dot <> pretty binop <+> parens (pretty e1) <+> parens( pretty e2)
Un unop typ e -> pretty typ <> dot <> pretty unop <+> parens (pretty e)
Rel relop typ e1 e2 -> pretty typ <> dot <> pretty relop <+> parens (pretty e1) <+> parens( pretty e2)
Sel selop e1 e2 e3 -> text "todo"
Convert cvtop ty e -> text "todo"
Host hostop _ -> text "todo"
instance Pretty BinOp where
pretty binop = text $ case binop of
Add -> "add"
Sub -> "sub"
Mul -> "mul"
DivS -> "divs"
DivU -> "divu"
RemS -> "rems"
RemU -> "remu"
And -> "and"
Or -> "or"
Xor -> "xor"
Shl -> "shl"
ShrU -> "shr_u"
ShrS -> "shr_s"
RotL -> "rotl"
RotR -> "Rotr"
Div -> "div"
CopySign -> "copysign"
Min -> "min"
Max -> "max"
instance Pretty UnOp where
pretty unop = text $ case unop of
Clz -> "clz"
Ctz -> "ctz"
Popcnt -> "popcnt"
Neg -> "neg"
Abs -> "abs"
Ceil -> "ceil"
Floor -> "floor"
Trunc -> "trunc"
Nearest -> "nearest"
Sqrt -> "sqrt"
instance Pretty RelOp where
pretty relop = text $ case relop of
Eqz -> "eqz"
Eq -> "eq"
Ne -> "ne"
LtS -> "lts"
LtU -> "ltu"
LeS -> "les"
LeU -> "leu"
GtS -> "gts"
GtU -> "gtu"
GeS -> "ges"
GeU -> "geu"
Lt -> "lt"
Le -> "le"
Gt -> "gt"
Ge -> "ge"
instance Pretty Value where
pretty value = text $ case value of
VI32 v -> show v
VI64 v -> show v
VF32 v -> show v
VF64 v -> show v
instance Pretty Func where
prettyList = vsep . map (parens . pretty)
pretty f = case f of
Export name value -> text "export" <+> pretty name <+> pretty value
Import name value -> text "Import" <+> pretty name <+> pretty value
Func name params body -> nest 4 (text "func" <+> maybe empty pretty name <+> pretty params <$> pretty body )
instance Pretty Decl where
pretty d = case d of
ModDecl m -> pretty m
ExprDecl expr -> pretty expr
instance Pretty Module where
pretty Module { _funcs=funcs } = parens (nest 4 (text "module" <$> pretty funcs))
instance Pretty Param where
prettyList = fillSep . map (parens . pretty)
pretty param = case param of
Param (Just name) typ -> text "param" <+> pretty name <+> pretty typ
Param Nothing typ -> text "param" <+> pretty typ
Result typ -> text "result" <+> pretty typ
Body expr -> pretty expr
instance Pretty Type where
pretty ty = text $ case ty of
I32 -> "i32"
I64 -> "i64"
F32 -> "f32"
F64 -> "f64"
FuncType -> "func"
display :: Pretty a => a -> String
display x = displayS (renderPretty 0.4 180 (pretty x)) ""