-
Notifications
You must be signed in to change notification settings - Fork 0
/
TigerPretty.hs
113 lines (102 loc) · 3.23 KB
/
TigerPretty.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
module TigerPretty where
import Prelude hiding ( (<>) )
import TigerAbs
import Text.PrettyPrint
import TigerSymbol
tabWidth :: Int
tabWidth = 8
prettyVar :: Var -> Doc
prettyVar (SimpleVar s ) = text $ unpack s
prettyVar (FieldVar v s) = prettyVar v <> text "." <> (text $ unpack s)
prettyVar (SubscriptVar v e) = prettyVar v <> (brackets $ prettyExp e)
prettyOp :: Oper -> Doc
prettyOp PlusOp = text "+"
prettyOp MinusOp = text "-"
prettyOp TimesOp = text "*"
prettyOp DivideOp = text "/"
prettyOp EqOp = text "="
prettyOp NeqOp = text "<>"
prettyOp LtOp = text "<"
prettyOp GtOp = text ">"
prettyOp LeOp = text "<="
prettyOp GeOp = text ">="
-- | Completar si quieren mejorar el pp
prettyTy :: Ty -> Doc
prettyTy = text . show
-- | Completar si quieren mejorar el pp
prettyField :: [(Symbol, Escapa, Ty)] -> Doc
prettyField = text . show
prettyDec :: Dec -> Doc
prettyDec (FunctionDec f) = vcat $ map functionDec f
where
functionDec (s, f, Just r, e, _) = hang
( text "function "
<> (text $ unpack s)
<> (parens $ prettyField f)
<> text " : "
<> (text $ unpack r)
<> (text " = ")
)
tabWidth
(prettyExp e)
functionDec (s, f, Nothing, e, _) = hang
( text "function "
<> (text $ unpack s)
<> (parens $ prettyField f)
<> (text " = ")
)
tabWidth
(prettyExp e)
prettyDec (VarDec s _ (Just r) e _) =
(text $ unpack s)
<> text " : "
<> (text $ unpack r)
<> text " = "
<> prettyExp e
prettyDec (VarDec s _ Nothing e _) =
(text $ unpack s) <> text " = " <> prettyExp e
prettyDec (TypeDec f) = vcat $ map typeDec f
where
typeDec (s, ty, _) =
text "type " <> (text $ unpack s) <> text " = " <> prettyTy ty
prettyExp :: Exp -> Doc
prettyExp (VarExp v _ ) = prettyVar v
prettyExp (UnitExp _ ) = text "()"
prettyExp (NilExp _ ) = text "nil"
prettyExp (IntExp i _) = text $ show i
prettyExp (StringExp s _) = doubleQuotes $ text s
prettyExp (CallExp s args _) =
text (unpack s) <> (parens $ hcat $ punctuate comma $ map prettyExp args)
prettyExp (OpExp e1 op e2 _) = prettyExp e1 <> prettyOp op <> prettyExp e2
prettyExp (RecordExp r n _ ) = error "COMPLETAR PRINTER"
prettyExp (SeqExp e _ ) = parens $ vcat $ punctuate semi (map prettyExp e)
prettyExp (AssignExp v e _ ) = prettyVar v <> text " = " <> prettyExp e
prettyExp (IfExp e e1 (Just e2) _) =
(hang (text "if " <> prettyExp e <> text " then ") tabWidth (prettyExp e1))
$$ text "else "
<> prettyExp e2
prettyExp (IfExp e e1 Nothing _) =
hang (text "if " <> prettyExp e <> text " then ") tabWidth (prettyExp e1)
prettyExp (WhileExp e e1 _) =
hang (text "while " <> prettyExp e) tabWidth (prettyExp e1)
prettyExp (ForExp s _ e1 e2 e3 _) = hang
( text "for "
<> text (unpack s)
<> text " := "
<> prettyExp e1
<> text " to "
<> prettyExp e2
)
tabWidth
(prettyExp e3)
prettyExp (LetExp d e _) =
text "let "
<> nest tabWidth (vcat (map prettyDec d))
$$ text "in "
<> prettyExp e
$$ text "end"
prettyExp (BreakExp _) = text "break"
prettyExp (ArrayExp s e1 e2 _) =
text "array " <> brackets (prettyExp e1) <> text " of " <> prettyExp e2
renderExp :: Exp -> String
renderExp = render . prettyExp