-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule_handle.c
213 lines (202 loc) · 7.07 KB
/
rule_handle.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
/**
* @file rule_handle.c
* @author Anastasia Butok (xbutok00@vutbr.cz)
* @brief Functions for derivation of handles according to set rules in PSA.
* @version 0.1
* @date 2023-11-24
*
* @copyright Copyright (c) 2023
* Project: IFJ compiler
*
*/
#include "psa.h"
PSA_Token getRule(PSA_Token *handle, unsigned int len)
{
uint32_t handle_val = handleToUInt32(handle, len);
/*
E -> f
E -> i
E -> (E)
E -> !E
E -> +E
E -> -E
E -> E*E
E -> E/E
E -> E+E
E -> E-E
E -> E==E
E -> E!=E
E -> E<E
E -> E>E
E -> E<=E
E -> E>=E
E -> E??E
*/
switch (handle_val)
{
case RULE_0:
DEBUG_PSA_CODE(printf_cyan("rule: E -> f\n"););
if (strcmp(handle[0].token_value, "_") == 0)
{
throw_error(SYNTACTIC_ERR, handle[0].line_num, "Variable name cannot be '_'.");
}
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = handle[0].expr_type,
.is_literal = isTokenLiteral(handle[0].type),
};
case RULE_1a:
case RULE_1b:
case RULE_1c:
case RULE_1d:
case RULE_1e:
case RULE_1f:
case RULE_1g:
DEBUG_PSA_CODE(printf_cyan("rule: E -> i\n"););
Expression_type type = getTypeFromToken(handle[0].type);
if (handle[0].type == TOKEN_IDENTIFICATOR)
{
if (handle[0].token_value == NULL)
{
throw_error(INTERNAL_ERR, handle[0].line_num, "Token value is NULL.");
}
else if (strcmp(handle[0].token_value, "_") == 0)
{
throw_error(SYNTACTIC_ERR, handle[0].line_num, "Variable name cannot be '_'.");
}
symtable_item *found = symtable_find_in_stack(handle[0].token_value, sym_st, true);
bool found_valid_var = found != NULL && found->type == VARIABLE && found->data.var_data != NULL && found->data.var_data->type != TYPE_INVALID;
if (found_valid_var)
{
if (!found->data.var_data->is_initialized)
{
throw_error(VARIABLES_ERR, handle[0].line_num, "Variable '%s' used before inicialization!", handle[0].token_value);
}
type = found->data.var_data->type;
}
else
{
throw_error(VARIABLES_ERR, handle[0].line_num, "Variable '%s' not found!", handle[0].token_value);
}
}
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = type,
.is_literal = isTokenLiteral(handle[0].type),
};
case RULE_2:
DEBUG_PSA_CODE(printf_cyan("rule: E -> (E)\n"););
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = handle[1].expr_type,
.is_literal = handle[1].is_literal,
};
case RULE_3:
DEBUG_PSA_CODE(printf_cyan("rule: E -> !E\n"););
if (handle[1].expr_type == TYPE_BOOL)
{
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = TYPE_BOOL,
.is_literal = false,
};
}
throw_error(COMPATIBILITY_ERR, handle[1].line_num, "Invalid operand type for operation prefix '!' (not).");
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = TYPE_INVALID,
.is_literal = false,
};
case RULE_6:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E*E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_7:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E/E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_8:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E+E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_9:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E-E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_10:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E==E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_11:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E!=E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_12:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E<E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_13:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E>E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_14:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E<=E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_15:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E>=E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_16:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E&&E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_17:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E||E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_18:
DEBUG_PSA_CODE(printf_cyan("rule: E -> E??E\n"););
return getHandleType(handle[0], handle[1].type, handle[2]);
case RULE_19:
{
DEBUG_PSA_CODE(printf_cyan("rule: E -> E!\n"););
Expression_type type = removeTypeNil(handle[0].expr_type);
if (type == TYPE_INVALID)
{
throw_error(COMPATIBILITY_ERR, handle[1].line_num, "Invalid operand type for operation postfix '!' (forced unwrapping).");
}
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = type,
.is_literal = false,
};
}
default:
DEBUG_PSA_CODE(printf_red("rule: EOF\n"););
throw_error(SYNTACTIC_ERR, handle[1].line_num, "Expression '%s' is not valid.", hadleToString(handle, len));
return (PSA_Token){
.type = (Token_type)TOKEN_EOF,
.token_value = "$",
.expr_type = TYPE_INVALID,
};
}
throw_error(SYNTACTIC_ERR, handle[1].line_num, "Expression '%s' is not valid.", hadleToString(handle, len));
return (PSA_Token){
.type = (Token_type)TOKEN_EOF,
.token_value = "$",
};
}
PSA_Token *getHandleFromStack(PSA_Token_stack *s, int *i)
{
PSA_Token *handle = malloc(sizeof(PSA_Token) * s->size);
*i = 0;
while (PSA_Token_stack_top(s).type != TOKEN_SHIFT)
{
handle[*i] = ((PSA_Token)PSA_Token_stack_pop(s));
*i = *i + 1;
}
(void)PSA_Token_stack_pop(s); // pop the <
// reverse the array
for (int j = 0; j < *i / 2; j++)
{
PSA_Token tmp = handle[j];
handle[j] = handle[*i - j - 1];
handle[*i - j - 1] = tmp;
}
return handle;
}