aboutsummaryrefslogtreecommitdiff
path: root/AST.hs
blob: 3e8083011abc81ab855a4d277d5227550173378c (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
module AST where

import Data.List

import Defs
import Pretty


data Program = Program [DVar] [DFunc]
  deriving (Show, Eq)

data DVar = DVar Type Name Expression
  deriving (Show, Eq)

data DFunc = DFunc (Maybe Type) Name [(Type, Name)] Block
  deriving (Show, Eq)

data Type
    = TInt | TChar | TArr Type (Maybe Size)
  deriving (Show, Eq)

data Block = Block [Statement]
  deriving (Show, Eq)

data Statement
    = SDecl Type Name Expression
    | SAs AsExpression Expression
    | SIf Expression Block Block
    | SWhile Expression Block
    | SReturn (Maybe Expression)
    | SExpr Expression
  deriving (Show, Eq)

data AsExpression
    = AEVar Name (Maybe Type)
    | AESubscript AsExpression Expression (Maybe Type)
  deriving (Show, Eq)

data Expression
    = EBin BinaryOp Expression Expression (Maybe Type)
    | EUn UnaryOp Expression (Maybe Type)
    | ELit Literal (Maybe Type)
    | ESubscript Expression Expression (Maybe Type)
    | ECast Type Expression
    | ENew Type Expression
  deriving (Show, Eq)

data BinaryOp
    = BOAdd | BOSub | BOMul | BODiv | BOMod | BOPow
    | BOAnd | BOOr
    | BOEq | BONeq | BOGt | BOLt | BOGeq | BOLeq
  deriving (Show, Eq)

data UnaryOp
    = UONot | UONeg
  deriving (Show, Eq)

data Literal
    = LInt Integer
    | LChar Char
    | LVar Name
    | LCall Name [Expression]
  deriving (Show, Eq)


sizeof :: Type -> Size
sizeof TInt = 8
sizeof TChar = 1
sizeof (TArr _ _) = 8


instance Pretty Program where
    prettyI i (Program vars funcs) =
        concatMap (++ ("\n" ++ indent i)) $
            map (prettyI i) vars ++ map (prettyI i) funcs
      where
        indent n = replicate (2*n) ' '

instance Pretty DVar where
    prettyI i (DVar t n e) =
        prettyI i t ++ " " ++ n ++ " := " ++ prettyI i e ++ ";"

instance Pretty DFunc where
    prettyI i (DFunc mt n al b) =
        "func" ++ maybe "" ((' ' :) . prettyI i) mt ++ " " ++ n ++ "(" ++
            intercalate ","
                (map (\(at,an) -> prettyI i at ++ " " ++ an) al) ++
            ") " ++ prettyI i b
 
instance Pretty Type where
    prettyI _ TInt = "int"
    prettyI _ TChar = "char"
    prettyI _ (TArr t Nothing) = pretty t ++ "[]"
    prettyI _ (TArr t (Just sz)) = pretty t ++ "[" ++ show sz ++ "]"

instance Pretty Block where
    prettyI _ (Block []) = "{}"
    prettyI i (Block l) =
        "{" ++
            concatMap (("\n" ++ indent (i+1)) ++) (map (prettyI (i+1)) l) ++
            "\n" ++ indent i ++ "}"
      where
        indent n = replicate (2*n) ' '

instance Pretty Statement where
    prettyI i (SDecl t n e) =
        prettyI i t ++ " " ++ n ++ " := " ++ prettyI i e ++ ";"
    prettyI i (SAs target e) =
        prettyI i target ++ " = " ++ prettyI i e ++ ";"
    prettyI i (SIf c b1 b2) =
        "if " ++ prettyI i c ++ " " ++ prettyI i b1 ++ " else " ++ prettyI i b2
    prettyI i (SWhile c b) =
        "while " ++ prettyI i c ++ " " ++ prettyI i b
    prettyI _ (SReturn Nothing) =
        "return;"
    prettyI i (SReturn (Just e)) =
        "return " ++ prettyI i e ++ ";"
    prettyI i (SExpr e) = prettyI i e ++ ";"

instance Pretty Expression where
    prettyI i (EBin bo a b (Just t)) =
        "(" ++ prettyI i (EBin bo a b Nothing) ++ " :: " ++ prettyI i t ++ ")"
    prettyI i (EBin bo a b Nothing) =
        "(" ++ prettyI i a ++ ") " ++ prettyI i bo ++
            " (" ++ prettyI i b ++ ")"
    prettyI i (EUn uo e (Just t)) =
        "(" ++ prettyI i (EUn uo e Nothing) ++ " :: " ++ prettyI i t ++ ")"
    prettyI i (EUn uo e Nothing) =
        prettyI i uo ++ "(" ++ prettyI i e ++ ")"
    prettyI i (ELit l (Just t)) =
        "(" ++ prettyI i (ELit l Nothing) ++ " :: " ++ prettyI i t ++ ")"
    prettyI i (ELit l Nothing) = prettyI i l
    prettyI i (ESubscript a b (Just t)) =
        "(" ++ prettyI i (ESubscript a b Nothing) ++ " :: " ++ prettyI i t ++ ")"
    prettyI i (ESubscript a b Nothing) = "(" ++ prettyI i a ++ ")[" ++ prettyI i b ++ "]"
    prettyI i (ECast t e) = prettyI i t ++ "(" ++ prettyI i e ++ ")"
    prettyI i (ENew t e) = "new " ++ prettyI i t ++ "[" ++ prettyI i e ++ "]"

instance Pretty AsExpression where
    prettyI i (AEVar n (Just t)) =
        "(" ++ prettyI i (AEVar n Nothing) ++ " :: " ++ prettyI i t ++ ")"
    prettyI _ (AEVar n Nothing) = n
    prettyI i (AESubscript ae e (Just t)) =
        "(" ++ prettyI i (AESubscript ae e Nothing) ++ " :: " ++ prettyI i t ++ ")"
    prettyI i (AESubscript ae e Nothing) = prettyI i ae ++ "[" ++ prettyI i e ++ "]"

instance Pretty BinaryOp where
    prettyI _ BOAdd = "+"
    prettyI _ BOSub = "-"
    prettyI _ BOMul = "*"
    prettyI _ BODiv = "/"
    prettyI _ BOPow = "**"
    prettyI _ BOMod = "%"
    prettyI _ BOAnd = "&&"
    prettyI _ BOOr = "||"
    prettyI _ BOEq = "=="
    prettyI _ BONeq = "!="
    prettyI _ BOGt = ">"
    prettyI _ BOLt = "<"
    prettyI _ BOGeq = ">="
    prettyI _ BOLeq = "<="


instance Pretty UnaryOp where
    prettyI _ UONot = "!"
    prettyI _ UONeg = "-"

instance Pretty Literal where
    prettyI _ (LInt n) = show n
    prettyI _ (LChar c) = show c
    prettyI _ (LVar n) = n
    prettyI i (LCall n al) =
        n ++ "(" ++ intercalate ", " (map (prettyI i) al) ++ ")"