-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexeme.hs
267 lines (250 loc) · 4.7 KB
/
Lexeme.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
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
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
module Lexer.Lexeme where
import Data.Char (chr)
import Data.Text hiding (any)
import Data.Text.Prettyprint.Doc
import Data.Void
import Prelude hiding (any)
data Lexeme
= LitInt Int
| LitDouble Double
| LitString String
| LitChar Char
| LitNull
| Ident String
| Type BuiltinType
| Struct
| Return
| Assign
| Comma
| Semi
| LParen
| RParen
| LBrace
| RBrace
| LBrack
| RBrack
| For
| While
| If
| Else
| Plus
| Minus
| Asterisk
| Div
| Mod
| Equal
| Neq
| Less
| Leq
| Greater
| Geq
| And
| Or
| Not
| Ampers
| Bar
| Caret
| Dot
| Arrow
| Sizeof
| Eof
| Include
| Increment
| Decrement
| Error
deriving (Show, Eq, Ord)
data BuiltinType = Int | Double | Char | Void deriving (Show, Eq, Ord)
isType :: Lexeme -> Bool
isType (Type _) = True
isType _ = False
isLitInt :: Lexeme -> Bool
isLitInt (LitInt _) = True
isLitInt _ = False
isLitDouble :: Lexeme -> Bool
isLitDouble (LitDouble _) = True
isLitDouble _ = False
isLitChar :: Lexeme -> Bool
isLitChar (LitChar _) = True
isLitChar _ = False
isLitString :: Lexeme -> Bool
isLitString (LitString _) = True
isLitString _ = False
isLit :: Lexeme -> Bool
isLit lexeme =
or
[ isLitInt lexeme,
isLitDouble lexeme,
isLitChar lexeme,
isLitString lexeme
]
isIdent :: Lexeme -> Bool
isIdent (Ident _) = True
isIdent _ = False
any :: [Lexeme] -> Lexeme -> Bool
any opts = (`elem` opts)
is :: Lexeme -> Lexeme -> Bool
is lexeme = any [lexeme]
instance Pretty BuiltinType where
pretty Int = pretty "int"
pretty Double = pretty "double"
pretty Char = pretty "char"
pretty Void = pretty "void"
instance Pretty Lexeme where
pretty = \case
(LitInt x) -> pretty x
(LitDouble x) -> pretty x
(LitChar c) -> squotes $ pretty c
(LitString s) -> dquotes $ pretty s
LitNull -> pretty "NULL"
(Ident x) -> pretty x
(Type Int) -> pretty "int"
(Type Double) -> pretty "double"
(Type Char) -> pretty "char"
(Type Void) -> pretty "void"
Struct -> pretty "struct"
Return -> pretty "return"
Assign -> pretty "="
Comma -> pretty ","
Semi -> pretty ";"
LParen -> pretty "("
RParen -> pretty ")"
LBrace -> pretty "{"
RBrace -> pretty "}"
LBrack -> pretty "["
RBrack -> pretty "]"
For -> pretty "for"
While -> pretty "while"
If -> pretty "if"
Else -> pretty "else"
Plus -> pretty "+"
Minus -> pretty "-"
Asterisk -> pretty "*"
Div -> pretty "/"
Mod -> pretty "%"
Equal -> pretty "=="
Neq -> pretty "!="
Less -> pretty "<"
Leq -> pretty "<="
Greater -> pretty ">"
Geq -> pretty ">="
And -> pretty "&&"
Or -> pretty "||"
Not -> pretty "!"
Ampers -> pretty "&"
Bar -> pretty "|"
Caret -> pretty "^"
Dot -> pretty "."
Arrow -> pretty "->"
Sizeof -> pretty "sizeof"
Include -> pretty "#include"
Error -> pretty "error"
Increment -> pretty "++"
Decrement -> pretty "--"
Eof -> emptyDoc
display :: Lexeme -> String
display = \case
(LitInt x) -> "integer literal"
(LitDouble x) -> "double literal"
(LitChar c) -> "char literal"
(LitString s) -> "string literal"
LitNull -> "NULL"
(Ident x) -> "identifier"
(Type Int) -> "int"
(Type Double) -> "double"
(Type Char) -> "char"
(Type Void) -> "void"
Struct -> "struct"
Return -> "return"
Assign -> "\'=\'"
Comma -> "\',\'"
Semi -> "\';\'"
LParen -> "\'(\'"
RParen -> "\')\'"
LBrace -> "\'{\'"
RBrace -> "\'}\'"
LBrack -> "\'[\'"
RBrack -> "\']\'"
For -> "for"
While -> "while"
If -> "if"
Else -> "else"
Plus -> "+"
Minus -> "\'-\'"
Asterisk -> "\'*\'"
Div -> "\'/\'"
Mod -> "\'%\'"
Equal -> "\'==\'"
Neq -> "\'!=\'"
Less -> "\'<\'"
Leq -> "\'<=\'"
Greater -> "\'>\'"
Geq -> "\'>=\'"
And -> "\'&&\'"
Or -> "\'||\'"
Not -> "\'!\'"
Ampers -> "\'&\'"
Bar -> "\'|\'"
Caret -> "\'^\'"
Dot -> "\'.\'"
Arrow -> "\'->\'"
Sizeof -> "sizeof"
Include -> "#include"
Error -> "error"
Increment -> "++"
Decrement -> "--"
Eof -> "EOF"
defaultLexemes :: [Lexeme]
defaultLexemes =
[ LitInt 0,
LitDouble 0,
LitString "",
LitChar ' ',
LitNull,
Ident "",
Type Int,
Type Double,
Type Char,
Type Void,
Struct,
Return,
Assign,
Comma,
Semi,
LParen,
RParen,
LBrace,
RBrace,
LBrack,
RBrack,
For,
While,
If,
Else,
Plus,
Minus,
Asterisk,
Div,
Mod,
Equal,
Neq,
Less,
Leq,
Greater,
Geq,
And,
Or,
Not,
Ampers,
Bar,
Caret,
Dot,
Arrow,
Sizeof,
Eof,
Include,
Increment,
Decrement,
Error
]