-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantic_control.c
221 lines (205 loc) · 6.84 KB
/
semantic_control.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
/**
* @file semantic_control.c
* @author Tomáš Hobza ([email protected])
* @brief Functions for semantic control of expressions for the PSA.
* @version 0.1
* @date 2023-11-24
*
* @copyright Copyright (c) 2023
* Project: IFJ compiler
*
*/
#include "psa.h"
Expression_type getTypeCombination(PSA_Token l_operand, PSA_Token r_operand)
{
if (l_operand.expr_type == TYPE_INVALID || r_operand.expr_type == TYPE_INVALID)
{
return TYPE_INVALID;
}
switch (((char)l_operand.expr_type << 8) | r_operand.expr_type)
{
case ((char)TYPE_INT << 8) | TYPE_INT:
return TYPE_INT;
case ((char)TYPE_INT << 8) | TYPE_DOUBLE:
if (l_operand.is_literal)
{
generate_temp_pop();
generate_instruction(INT2FLOATS);
generate_temp_push();
DEBUG_PSA_CODE(printf("implicite Int2Double for left operand '%s'\n", l_operand.token_value););
return TYPE_DOUBLE;
}
else
{
throw_error(COMPATIBILITY_ERR, l_operand.line_num, "Cannot convert operand '%s' from type Int to Double.", l_operand.token_value);
return TYPE_INVALID;
}
case ((char)TYPE_DOUBLE << 8) | TYPE_INT:
if (r_operand.is_literal)
{
generate_instruction(INT2FLOATS);
DEBUG_PSA_CODE(printf("impicite Int2Double for right operand '%s'\n", r_operand.token_value););
return TYPE_DOUBLE;
}
else
{
throw_error(COMPATIBILITY_ERR, r_operand.line_num, "Cannot convert operand '%s' from type Int to Double.", r_operand.token_value);
return TYPE_INVALID;
}
case ((char)TYPE_DOUBLE << 8) | TYPE_DOUBLE:
return TYPE_DOUBLE;
case ((char)TYPE_STRING << 8) | TYPE_STRING:
return TYPE_STRING;
case ((char)TYPE_INT_NIL << 8) | TYPE_INT_NIL:
return TYPE_INT_NIL;
case ((char)TYPE_DOUBLE_NIL << 8) | TYPE_DOUBLE_NIL:
return TYPE_DOUBLE_NIL;
case ((char)TYPE_STRING_NIL << 8) | TYPE_STRING_NIL:
return TYPE_STRING_NIL;
default:
return TYPE_INVALID;
}
}
PSA_Token getHandleType(PSA_Token l_operand, Token_type operation, PSA_Token r_operand)
{
if (l_operand.expr_type == TYPE_INVALID || r_operand.expr_type == TYPE_INVALID)
{
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = TYPE_INVALID,
.preceded_by_nl = false,
.is_literal = false,
};
}
switch (operation)
{
// for: +, -, *, /
case TOKEN_PLUS:
// can be (string, string), ...
if (l_operand.expr_type == TYPE_STRING && r_operand.expr_type == TYPE_STRING && !(canTypeBeNil(l_operand.expr_type) || canTypeBeNil(r_operand.expr_type)))
{
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = TYPE_STRING,
.preceded_by_nl = false,
.is_literal = false,
};
}
__attribute__((fallthrough));
case TOKEN_MINUS:
case TOKEN_MUL:
case TOKEN_DIV:
if (canTypeBeNil(l_operand.expr_type) || canTypeBeNil(r_operand.expr_type))
{
throw_error(COMPATIBILITY_ERR, l_operand.line_num, "Invalid operand types for operation '%c'.", getOperationChar(operation));
break;
}
// can be (int, int), (int, double), (double, int), (double, double)
Expression_type type = getTypeCombination(l_operand, r_operand);
if (type != TYPE_INVALID)
{
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = type,
.preceded_by_nl = false,
.is_literal = false,
};
}
break;
// for: ==, !=
case TOKEN_EQ:
case TOKEN_NEQ:
// can be (int, int), (int, double), (double, int), (double, double), (string, string), (bool, bool)
if (l_operand.expr_type == r_operand.expr_type || getTypeCombination(l_operand, r_operand) != TYPE_INVALID)
{
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = TYPE_BOOL,
.preceded_by_nl = false,
.is_literal = false,
};
}
break;
// for: <, >, <=, >=
case TOKEN_LESS:
case TOKEN_MORE:
case TOKEN_LESS_EQ:
case TOKEN_MORE_EQ:
{
// can be (int, int), (int, double), (double, int), (double, double), (string, string)
if (getTypeCombination(l_operand, r_operand) != TYPE_INVALID || (l_operand.expr_type == TYPE_STRING && r_operand.expr_type == TYPE_STRING))
{
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = TYPE_BOOL,
.preceded_by_nl = false,
.is_literal = false,
};
}
break;
}
// for: ??, &&, ||
case TOKEN_AND:
case TOKEN_OR:
if (l_operand.expr_type == TYPE_BOOL && r_operand.expr_type == TYPE_BOOL)
{
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = TYPE_BOOL,
.preceded_by_nl = false,
.is_literal = false,
};
}
break;
// for: !
case TOKEN_BINARY_OPERATOR:
{
bool operand_types_valid = canTypeBeNil(l_operand.expr_type) && !canTypeBeNil(r_operand.expr_type);
bool operand_types_match = removeTypeNil(l_operand.expr_type) == r_operand.expr_type;
if (operand_types_valid && operand_types_match)
{
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = r_operand.expr_type,
.preceded_by_nl = false,
.is_literal = false,
};
}
break;
}
default:
break;
}
throw_error(COMPATIBILITY_ERR, l_operand.line_num, "Invalid operand types for operation '%c'.", getOperationChar(operation));
return (PSA_Token){
.type = (Token_type)TOKEN_EXPRSN,
.token_value = "E",
.expr_type = TYPE_INVALID,
.preceded_by_nl = false,
.is_literal = false,
};
}
Expression_type getIdType(PSA_Token id)
{
symtable_item *found_id = symtable_find_in_stack(id.token_value, sym_st, id.type == TOKEN_FUNC_ID);
if (found_id == NULL)
{
return TYPE_INVALID;
}
switch (found_id->type)
{
case FUNCTION:
return found_id->data.func_data->return_type;
case VARIABLE:
return found_id->data.var_data->type;
default:
return TYPE_INVALID;
}
}