summaryrefslogtreecommitdiff
path: root/ast.hs
blob: bd845df8a5198b96ee44aa00754e6acb89bb4eb1 (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
module AST(
    Name,
    Program(..), Declaration(..), Block(..), Type(..), Literal(..),
    BinaryOperator(..), UnaryOperator(..), Expression(..), Statement(..)) where

import Data.List

import PShow


type Name = String

data Program = Program [Declaration]
  deriving (Show)

data Declaration
    = DecFunction {typeOf :: Type
                  ,nameOf :: Name
                  ,argumentsOf :: [(Type, Name)]
                  ,bodyOf :: Block}
    | DecVariable {typeOf :: Type
                  ,nameOf :: Name
                  ,valueOf :: Maybe Expression}
    | DecTypedef {typeOf :: Type
                 ,nameOf :: Name}
  deriving (Show)

data Block = Block [Statement]
  deriving (Show)

data Type = TypeInt Int
          | TypeUInt Int
          | TypeFloat
          | TypeDouble
          | TypePtr Type
          | TypeName Name
  deriving (Show, Eq)

data Literal = LitInt Integer
             | LitString String
             | LitVar Name
             | LitCall Name [Expression]
  deriving (Show)

data BinaryOperator
    = Plus | Minus | Times | Divide | Modulo
    | Equal | Unequal | Greater | Less | GEqual | LEqual
    | BoolAnd | BoolOr
  deriving (Show, Eq)

data UnaryOperator
    = Negate | Not | Invert | Dereference | Address
  deriving (Show, Eq)

data Expression
    = ExLit Literal
    | ExBinOp BinaryOperator Expression Expression
    | ExUnOp UnaryOperator Expression
  deriving (Show)

data Statement
    = StEmpty
    | StBlock Block
    | StExpr Expression
    | StVarDeclaration Type Name (Maybe Expression)
    | StAssignment Name Expression
    | StIf Expression Statement Statement
    | StWhile Expression Statement
    | StReturn Expression
  deriving (Show)


indent :: Int -> String -> String
indent sz str = intercalate "\n" $ map (prefix++) $ lines str
    where prefix = replicate sz ' '


instance PShow Program where
    pshow (Program decls) = intercalate "\n" (map pshow decls)

instance PShow Declaration where
    pshow (DecFunction t n a b) =
        concat [pshow t, " ", n, "(", intercalate ", " (map pshowArg a), ") ", pshow b]
      where pshowArg (argt, argn) = concat [pshow argt, " ", argn]
    pshow (DecVariable t n Nothing) =
        concat [pshow t, " ", n, ";"]
    pshow (DecVariable t n (Just e)) =
        concat [pshow t, " ", n, " = ", pshow e, ";"]
    pshow (DecTypedef t n) =
        concat ["type ", n, " = ", pshow t, ";"]

instance PShow Block where
    pshow (Block []) = "{}"
    pshow (Block stmts) = concat ["{\n", indent 4 $ intercalate "\n" (map pshow stmts), "\n}"]

instance PShow Type where
    pshow (TypeInt sz) = 'i' : pshow sz
    pshow (TypeUInt sz) = 'u' : pshow sz
    pshow TypeFloat = "float"
    pshow TypeDouble = "double"
    pshow (TypePtr t) = concat ["ptr(", pshow t, ")"]
    pshow (TypeName n) = n

instance PShow Literal where
    pshow (LitInt i) = pshow i
    pshow (LitString s) = pshow s
    pshow (LitVar n) = n
    pshow (LitCall n a) = concat [n, "(", intercalate ", " (map pshow a), ")"]

instance PShow BinaryOperator where
    pshow Plus = "+"
    pshow Minus = "-"
    pshow Times = "*"
    pshow Divide = "/"
    pshow Modulo = "%"
    pshow Equal = "=="
    pshow Unequal = "!="
    pshow Greater = ">"
    pshow Less = "<"
    pshow GEqual = ">="
    pshow LEqual = "<="
    pshow BoolAnd = "&&"
    pshow BoolOr = "||"

instance PShow UnaryOperator where
    pshow Negate = "-"
    pshow Not = "!"
    pshow Invert = "~"
    pshow Dereference = "*"
    pshow Address = "&"

instance PShow Expression where
    pshow (ExLit lit) = pshow lit
    pshow (ExBinOp op a b) = concat [pshow a, " ", pshow op, " ", pshow b]
    pshow (ExUnOp op a) = concat [pshow op, pshow a]

instance PShow Statement where
    pshow StEmpty = ";"
    pshow (StBlock bl) = pshow bl
    pshow (StExpr e) = pshow e ++ ";"
    pshow (StVarDeclaration t n Nothing) = concat [pshow t, " ", n, ";"]
    pshow (StVarDeclaration t n (Just e)) = concat [pshow t, " ", n, " = ", pshow e, ";"]
    pshow (StAssignment n e) = concat [n, " = ", pshow e, ";"]
    pshow (StIf c t StEmpty) = concat ["if (", pshow c, ") ", pshow t]
    pshow (StIf c t@(StBlock _) e) = concat ["if (", pshow c, ") ", pshow t, " else ", pshow e]
    pshow (StIf c t e) = concat ["if (", pshow c, ") ", pshow t, "\nelse ", pshow e]
    pshow (StWhile c s) = concat ["while (", pshow c, ") ", pshow s]
    pshow (StReturn e) = concat ["return ", pshow e, ";"]