-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpr.cpp
More file actions
48 lines (38 loc) · 1.12 KB
/
expr.cpp
File metadata and controls
48 lines (38 loc) · 1.12 KB
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
#include "expr.hpp"
Type Expr::getType()
{
/* Assumption: sem() or type_check() was called before this is called */
return expr_type;
}
void Expr::type_check(Type expected_type)
{
sem();
if(!equalType(expr_type, expected_type))
{
semError("Type mismatch");
}
}
void Expr::type_check_param(Type expected_type)
{
sem();
if(!equalTypeAutocomplete(expr_type, expected_type))
{
semError("Type mismatch");
}
}
bool check_assignable_operands(Expr *left, Expr *right)
{
/* Either both operands will be integers or both operands will be chars. */
/* Semantic analysis for both operands */
left->sem();
right->sem();
/* Grab type of both operands */
Type left_type = left->getType();
Type right_type = right->getType();
/* Check if both operands are of type int or if both operands are of type char */
bool int_operands = equalType(left_type, typeInteger) &&
equalType(right_type, typeInteger);
bool char_operands = equalType(left_type, typeChar) &&
equalType(right_type, typeChar);
return int_operands || char_operands;
}