summaryrefslogtreecommitdiff
path: root/parser.hs
blob: 40da1ca0b5651f1310dae89e0c198b3dafd32e4e (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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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 <|> pDecExtern <|> do
    t <- pTypeVoid <|> pType
    n <- pName
    if t == TypeVoid
      then pDecFunction' t n
      else pDecFunction' t n <|> pDecVariable' t n

pDecTypedef :: Parser Declaration
pDecTypedef = do
    symbol "type"
    n <- pName
    symbol "="
    t <- pType
    symbol ";"
    return $ DecTypedef t n

pDecExtern :: Parser Declaration
pDecExtern = do
    symbol "extern"
    t <- pType
    n <- pName
    symbol ";"
    return $ DecExtern 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 ">=" GEqual E.AssocNone,
      binary "<=" LEqual E.AssocNone,
      binary ">" Greater E.AssocNone,
      binary "<" Less 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 (pExCast <|> pExLit)

pExCast :: Parser Expression
pExCast = do
    symbol "cast"
    symbol "("
    t <- pType
    symbol ")"
    e <- pParenExpr
    return $ ExCast t e

pExLit :: Parser Expression
pExLit = do
    litex <- (exLit_ <$> pLiteral) <|> pParenExpr
    option litex $ do
        symbol "["
        arg <- pExpression
        symbol "]"
        return $ exBinOp_ Index litex arg

pParenExpr :: Parser Expression
pParenExpr = do
    symbol "("
    e <- pExpression
    symbol ")"
    return e

pLiteral :: Parser Literal
pLiteral = (LitFloat <$> pFloat) <|> pLitInt <|> (LitInt <$> pCharStr)
           <|> (LitString <$> pString) <|> try pLitCall <|> (LitVar <$> pName)

pLitInt :: Parser Literal
pLitInt = do
    i <- pInteger
    liftM (maybe (LitInt i) (const $ LitUInt i)) $ optionMaybe (symbol "U")

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"
    (symbol ";" >> return (StReturn Nothing)) <|> do
        e <- pExpression
        symbol ";"
        return $ StReturn (Just e)


primitiveTypes :: Map.Map String Type
primitiveTypes = Map.fromList
    [("i8", TypeInt 8), ("i16", TypeInt 16), ("i32", TypeInt 32), ("i64", TypeInt 64),
     ("u1", TypeUInt 1), ("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 <|> pTypePtr <|> pTypeFunc <|> 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

pTypeVoid :: Parser Type
pTypeVoid = symbol "void" >> return TypeVoid

pTypePtr :: Parser Type
pTypePtr = do
    symbol "ptr"
    symbol "("
    t <- pType
    symbol ")"
    return $ TypePtr t

pTypeFunc :: Parser Type
pTypeFunc = do
    symbol "func"
    r <- pTypeVoid <|> pType
    symbol "("
    a <- sepBy pType (symbol ",")
    symbol ")"
    return $ TypeFunc r a

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 digit) << pWhiteComment

pFloat :: Parser Double
pFloat = try $ do
    pre <- many1 digit
    post <- choice [pExponent,
                    (do
                        void $ char '.'
                        s <- many1 digit
                        ex <- option "" pExponent
                        return $ '.' : s ++ ex)]
    pWhiteComment
    return $ read $ pre ++ post
  where
    pExponent = do
        c <- choice [char 'e', char 'E']
        pm <- option "" $ choice [string "+", string "-"]
        val <- many1 digit
        return $ c : pm ++ val

pString :: Parser String
pString = do
    void $ char '"'
    s <- many (pEscape <|> satisfy (/='"'))
    symbol "\""
    return s

pCharStr :: Parser Integer
pCharStr = do
    void $ char '\''
    c <- pEscape <|> satisfy (/='\'')
    symbol "'"
    return $ fromIntegral (ord c)

pEscape :: Parser Char
pEscape = char '\\' >> (pEscapeQuote <|> pEscapeN <|> pEscapeR <|> pEscapeT <|> pEscapeHex)
  where
    pEscapeQuote, pEscapeN, pEscapeR, pEscapeT :: Parser Char
    pEscapeQuote = ('"' <$ char '"') <|> ('\'' <$ 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
  where
    pHexChar :: Parser Int
    pHexChar = (liftM (\c -> ord c - ord '0') digit)
               <|> (liftM (\c -> ord c - ord 'a' + 10) (oneOf "abcdef"))
               <|> (liftM (\c -> ord c - ord 'A' + 10) (oneOf "ABCDEF"))

symbol :: String -> Parser ()
symbol s = do
    void $ try (string s)
    when (isAlphaNum (last s)) $ notFollowedBy alphaNum
    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