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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
module Parser(parseProgram) where
import Control.Monad
import Data.Char
import Data.Functor.Identity
import Data.Maybe
import qualified Data.Map.Strict as Map
import Text.Parsec
import qualified Text.Parsec.Expr as E
import AST
type Parser = Parsec String ()
(<<) :: (Monad m) => m a -> m b -> m a
(<<) = (<*)
parseProgram :: String -> String -> Either ParseError Program
parseProgram source fname = parse pProgram fname source
-- parse' :: Parser a -> String -> Either ParseError a
-- parse' p s = parse p "" s
pProgram :: Parser Program
pProgram = pWhiteComment >> (Program <$> many1 pDeclaration)
pDeclaration :: Parser Declaration
pDeclaration = pDecTypedef <|> do
t <- pType
n <- pName
pDecFunction' t n <|> pDecVariable' t n
pDecTypedef :: Parser Declaration
pDecTypedef = do
symbol "type"
n <- pName
symbol "="
t <- pType
symbol ";"
return $ DecTypedef t n
pDecFunction' :: Type -> Name -> Parser Declaration
pDecFunction' t n = do
symbol "("
a <- sepBy ((,) <$> pType <*> pName) (symbol ",")
symbol ")"
b <- pBlock
return $ DecFunction t n a b
pDecVariable' :: Type -> Name -> Parser Declaration
pDecVariable' t n = do
e <- (Just <$> (symbol "=" >> pExpression)) <|> return Nothing
symbol ";"
return $ DecVariable t n e
pBlock :: Parser Block
pBlock = do
symbol "{"
s <- many pStatement
symbol "}"
return $ Block s
exprTable :: (E.OperatorTable String () Identity) Expression
exprTable =
[[prefix "-" Negate,
prefix "!" Not,
prefix "~" Invert,
prefix "*" Dereference,
prefix "&" Address],
[binary "*" Times E.AssocLeft,
binary "/" Divide E.AssocLeft,
binary "%" Modulo E.AssocLeft],
[binary "+" Plus E.AssocLeft,
binary "-" Minus E.AssocLeft],
[binary ">" Greater E.AssocNone,
binary "<" Less E.AssocNone,
binary ">=" GEqual E.AssocNone,
binary "<=" LEqual E.AssocNone],
[binary "==" Equal E.AssocNone,
binary "!=" Unequal E.AssocNone],
[binary "&&" BoolAnd E.AssocLeft,
binary "||" BoolOr E.AssocLeft]]
where
binary name op assoc = E.Infix (exBinOp_ op <$ symbol name) assoc
prefix name op = E.Prefix (exUnOp_ op <$ symbol name)
pExpression :: Parser Expression
pExpression = E.buildExpressionParser exprTable pExLit
pExLit :: Parser Expression
pExLit = exLit_ <$> pLiteral
pLiteral :: Parser Literal
pLiteral = (LitInt <$> pInteger) <|> (LitString <$> pString)
<|> try pLitCall <|> (LitVar <$> pName)
pLitCall :: Parser Literal
pLitCall = do
n <- pName
symbol "("
a <- sepBy pExpression (symbol ",")
symbol ")"
return $ LitCall n a
pStatement :: Parser Statement
pStatement = pStEmpty <|> pStIf <|> pStWhile <|> pStReturn <|> pStBlock
<|> try pStAssignment <|> try pStVarDeclaration <|> pStExpr
pStEmpty :: Parser Statement
pStEmpty = symbol ";" >> return StEmpty
pStBlock :: Parser Statement
pStBlock = StBlock <$> pBlock
pStVarDeclaration :: Parser Statement
pStVarDeclaration = do
t <- pType
n <- pName
e <- optionMaybe (symbol "=" >> pExpression)
symbol ";"
return $ StVarDeclaration t n e
pStExpr :: Parser Statement
pStExpr = (StExpr <$> pExpression) << symbol ";"
pStAssignment :: Parser Statement
pStAssignment = do
n <- pName
symbol "="
e <- pExpression
symbol ";"
return $ StAssignment n e
pStIf :: Parser Statement
pStIf = do
symbol "if"
symbol "("
c <- pExpression
symbol ")"
t <- pStatement
e <- (symbol "else" >> pStatement) <|> return StEmpty
return $ StIf c t e
pStWhile :: Parser Statement
pStWhile = do
symbol "while"
symbol "("
c <- pExpression
symbol ")"
b <- pStatement
return $ StWhile c b
pStReturn :: Parser Statement
pStReturn = do
symbol "return"
e <- pExpression
symbol ";"
return $ StReturn e
primitiveTypes :: Map.Map String Type
primitiveTypes = Map.fromList
[("i1", TypeInt 1), ("i8", TypeInt 8), ("i16", TypeInt 16), ("i32", TypeInt 32), ("i64", TypeInt 64),
("u8", TypeUInt 8), ("u16", TypeUInt 16), ("u32", TypeUInt 32), ("u64", TypeUInt 64),
("float", TypeFloat), ("double", TypeDouble)]
findPrimType :: String -> Type
findPrimType s = fromJust $ Map.lookup s primitiveTypes
pType :: Parser Type
pType = pPrimType <|> pPtrType <|> pTypeName
pPrimType :: Parser Type
pPrimType = findPrimType <$> choice (map typeParser $ Map.keys primitiveTypes)
where typeParser t = try $ do
void $ string t
void $ lookAhead $ satisfy (\c -> not (isAlphaNum c) && c /= '_')
pWhiteComment
return t
pPtrType :: Parser Type
pPtrType = do
symbol "ptr"
symbol "("
t <- pType
symbol ")"
return $ TypePtr t
pTypeName :: Parser Type
pTypeName = TypeName <$> pName
pName :: Parser Name
pName = ((:) <$> pFirstChar <*> many pOtherChar) << pWhiteComment
where pFirstChar = satisfy (isAlpha .||. (=='_'))
pOtherChar = satisfy (isAlpha .||. isDigit .||. (=='_'))
pInteger :: Parser Integer
pInteger = (read <$> many1 (satisfy isDigit)) << pWhiteComment
pString :: Parser String
pString = do
void $ char '"'
s <- many (pEscape <|> anyChar)
symbol "\""
return s
where
pEscape :: Parser Char
pEscape = char '\\' >> (pEscapeN <|> pEscapeR <|> pEscapeT <|> pEscapeHex)
pEscapeN, pEscapeR, pEscapeT :: Parser Char
pEscapeN = '\n' <$ char 'n'
pEscapeR = '\r' <$ char 'r'
pEscapeT = '\t' <$ char 't'
pEscapeHex :: Parser Char
pEscapeHex = do
void $ char 'x'
c1 <- pHexChar
c2 <- pHexChar
return $ chr $ 16 * c1 + c2
pHexChar :: Parser Int
pHexChar = (liftM (\c -> ord c - ord '0') (satisfy isDigit))
<|> (liftM (\c -> ord c - ord 'a' + 10) (oneOf "abcdef"))
<|> (liftM (\c -> ord c - ord 'A' + 10) (oneOf "ABCDEF"))
symbol :: String -> Parser ()
symbol s = try (string s) >> pWhiteComment
pWhiteComment :: Parser ()
pWhiteComment = sepBy pWhitespace pComment >> return ()
pWhitespace :: Parser ()
pWhitespace = many (oneOf " \t\n") >> return ()
pComment :: Parser ()
pComment = pLineComment <|> pBlockComment
pLineComment :: Parser ()
pLineComment = try (string "//") >> manyTill anyChar (char '\n') >> return ()
pBlockComment :: Parser ()
pBlockComment = try (string "/*") >> manyTill anyChar (try (string "*/")) >> return ()
infixr 2 .||.
(.||.) :: (a -> Bool) -> (a -> Bool) -> a -> Bool
f .||. g = \x -> f x || g x
|