-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
256 lines (249 loc) · 6.34 KB
/
utils.c
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
/**
* @file utils.c
* @author Tomáš Hobza (xhobza03@vutbr.cz)
* @brief Utility functions for the whole project.
* @version 0.1
* @date 2023-11-24
*
* @copyright Copyright (c) 2023
* Project: IFJ compiler
*
*/
#include "utils.h"
void fprint_token_type(Token_type type, FILE *out)
{
switch (type)
{
case TOKEN_IF:
fprintf_cyan(out, "TOKEN_IF");
break;
case TOKEN_ELSE:
fprintf_cyan(out, "TOKEN_ELSE");
break;
case TOKEN_WHILE:
fprintf_cyan(out, "TOKEN_WHILE");
break;
case TOKEN_RETURN:
fprintf_cyan(out, "TOKEN_RETURN");
break;
case TOKEN_VAR:
fprintf_cyan(out, "TOKEN_VAR");
break;
case TOKEN_LET:
fprintf_cyan(out, "TOKEN_LET");
break;
case TOKEN_BREAK:
fprintf_cyan(out, "TOKEN_BREAK");
break;
case TOKEN_CONTINUE:
fprintf_cyan(out, "TOKEN_CONTINUE");
break;
case TOKEN_TYPE_STRING:
fprintf_cyan(out, "TOKEN_TYPE_STRING");
break;
case TOKEN_TYPE_INT:
fprintf_cyan(out, "TOKEN_TYPE_INT");
break;
case TOKEN_TYPE_DOUBLE:
fprintf_cyan(out, "TOKEN_TYPE_DOUBLE");
break;
case TOKEN_TYPE_BOOL:
fprintf_cyan(out, "TOKEN_TYPE_BOOL");
break;
case TOKEN_TYPE_STRING_NIL:
fprintf_cyan(out, "TOKEN_TYPE_STRING_NIL");
break;
case TOKEN_TYPE_INT_NIL:
fprintf_cyan(out, "TOKEN_TYPE_INT_NIL");
break;
case TOKEN_TYPE_DOUBLE_NIL:
fprintf_cyan(out, "TOKEN_TYPE_DOUBLE_NIL");
break;
case TOKEN_TYPE_BOOL_NIL:
fprintf_cyan(out, "TOKEN_TYPE_BOOL_NIL");
break;
case TOKEN_FUNC:
fprintf_cyan(out, "TOKEN_FUNC");
break;
case TOKEN_IDENTIFICATOR:
fprintf_cyan(out, "TOKEN_IDENTIFICATOR");
break;
case TOKEN_EOF:
fprintf_cyan(out, "TOKEN_EOF");
break;
case TOKEN_INT:
fprintf_cyan(out, "TOKEN_INT");
break;
case TOKEN_DOUBLE:
fprintf_cyan(out, "TOKEN_DOUBLE");
break;
case TOKEN_BOOL:
fprintf_cyan(out, "TOKEN_BOOL");
break;
case TOKEN_EXP:
fprintf_cyan(out, "TOKEN_EXP");
break;
case TOKEN_STRING:
fprintf_cyan(out, "TOKEN_STRING");
break;
case TOKEN_EQ:
fprintf_cyan(out, "TOKEN_EQ");
break;
case TOKEN_NEQ:
fprintf_cyan(out, "TOKEN_NEQ");
break;
case TOKEN_LESS:
fprintf_cyan(out, "TOKEN_LESS");
break;
case TOKEN_MORE:
fprintf_cyan(out, "TOKEN_MORE");
break;
case TOKEN_MORE_EQ:
fprintf_cyan(out, "TOKEN_MORE_EQ");
break;
case TOKEN_LESS_EQ:
fprintf_cyan(out, "TOKEN_LESS_EQ");
break;
case TOKEN_PLUS:
fprintf_cyan(out, "TOKEN_PLUS");
break;
case TOKEN_MINUS:
fprintf_cyan(out, "TOKEN_MINUS");
break;
case TOKEN_MUL:
fprintf_cyan(out, "TOKEN_MUL");
break;
case TOKEN_DIV:
fprintf_cyan(out, "TOKEN_DIV");
break;
case TOKEN_BINARY_OPERATOR:
fprintf_cyan(out, "TOKEN_BINARY_OPERATOR");
break;
case TOKEN_ASSIGN:
fprintf_cyan(out, "TOKEN_ASSIGN");
break;
case TOKEN_L_BRACKET:
fprintf_cyan(out, "TOKEN_L_BRACKET");
break;
case TOKEN_R_BRACKET:
fprintf_cyan(out, "TOKEN_R_BRACKET");
break;
case TOKEN_R_CURLY:
fprintf_cyan(out, "TOKEN_R_CURLY");
break;
case TOKEN_L_CURLY:
fprintf_cyan(out, "TOKEN_L_CURLY");
break;
case TOKEN_COMMA:
fprintf_cyan(out, "TOKEN_COMMA");
break;
case TOKEN_ARROW:
fprintf_cyan(out, "TOKEN_ARROW");
break;
case TOKEN_NIL:
fprintf_cyan(out, "TOKEN_NIL");
break;
case TOKEN_DOUBLE_DOT:
fprintf_cyan(out, "TOKEN_DOUBLE_DOT");
break;
case TOKEN_NOT:
fprintf_cyan(out, "TOKEN_NOT");
break;
case TOKEN_AND:
fprintf_cyan(out, "TOKEN_AND");
break;
case TOKEN_OR:
fprintf_cyan(out, "TOKEN_OR");
break;
case TOKEN_UNDERSCORE:
fprintf_cyan(out, "TOKEN_UNDERSCORE");
break;
case TOKEN_FUNC_ID:
fprintf_cyan(out, "TOKEN_FUNC_ID");
break;
case TOKEN_EXPRSN:
fprintf_yellow(out, "TOKEN_EXPRSN");
break;
case TOKEN_SHIFT:
fprintf_yellow(out, "TOKEN_SHIFT");
break;
case TOKEN_UNSHIFT:
fprintf_yellow(out, "TOKEN_UNSHIFT");
break;
default:
fprintf_red(out, "[invalid token]");
break;
}
fprintf_cyan(out, "\n");
}
void print_token_type(Token_type type)
{
fprint_token_type(type, stdout);
}
void fprint_expression_type(Expression_type type, FILE *out)
{
switch (type)
{
case TYPE_INVALID:
fprintf_red(out, "TYPE_INVALID");
break;
case TYPE_INT:
fprintf_cyan(out, "TYPE_INT");
break;
case TYPE_DOUBLE:
fprintf_cyan(out, "TYPE_DOUBLE");
break;
case TYPE_STRING:
fprintf_cyan(out, "TYPE_STRING");
break;
case TYPE_BOOL:
fprintf_cyan(out, "TYPE_BOOL");
break;
case TYPE_NIL:
fprintf_cyan(out, "TYPE_NIL");
break;
case TYPE_EMPTY:
fprintf_cyan(out, "TYPE_EMPTY");
break;
case TYPE_INT_NIL:
fprintf_cyan(out, "TYPE_INT_NIL");
break;
case TYPE_DOUBLE_NIL:
fprintf_cyan(out, "TYPE_DOUBLE_NIL");
break;
case TYPE_STRING_NIL:
fprintf_cyan(out, "TYPE_STRING_NIL");
break;
case TYPE_BOOL_NIL:
fprintf_cyan(out, "TYPE_BOOL_NIL");
break;
default:
fprintf_red(out, "[invalid type]");
break;
}
fprintf(out, "\n");
}
void print_expression_type(Expression_type type)
{
fprint_expression_type(type, stdout);
}
Token convertPSATokenToToken(PSA_Token psa_tkn)
{
return (Token){
.type = psa_tkn.type,
.token_value = psa_tkn.token_value,
.preceded_by_nl = psa_tkn.preceded_by_nl,
.line_num = psa_tkn.line_num,
};
}
PSA_Token convertTokenToPSAToken(Token tkn)
{
return (PSA_Token){
.type = tkn.type,
.token_value = tkn.token_value,
.preceded_by_nl = tkn.preceded_by_nl,
.expr_type = getTypeFromToken(tkn.type),
.is_literal = isTokenLiteral(tkn.type),
.line_num = tkn.line_num,
};
}