-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemCheck.cpp
99 lines (91 loc) · 2.93 KB
/
semCheck.cpp
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
#include "semCheck.h"
bool noError = true;
extern int linenum;
void semanticError( std::string msg )
{
printf("<Error> found in Line %d: %s\n", linenum, msg.c_str() );
noError = false;
}
bool verifyAndOrOp(Type a, std::string op, Type b)
{
char buf[300];
bool result = true;
if (a.typeID == T_ERROR || b.typeID == T_ERROR)
result = false;
else if (a.dimensions.size()>0 || b.dimensions.size()>0){
sprintf(buf, "one of the operands of operator '%s' is array type", op.c_str());
semanticError(buf);
result = false;
} else if (a.typeID != T_BOOLEAN || b.typeID != T_BOOLEAN) {
sprintf(buf, "one of the operands of operator '%s' is not boolean", op.c_str());
semanticError(buf);
result = false;
}
return result;
}
bool verifyNotOp(Type a)
{
char buf[300];
bool result = true;
if (a.typeID == T_ERROR)
result = false;
else if (a.dimensions.size()>0){
sprintf(buf, "operand of operator 'not' is array type");
semanticError(buf);
result = false;
} else if (a.typeID != T_BOOLEAN) {
semanticError("operand of operator 'not' is not boolean");
result = false;
}
return result;
}
bool verifyRelOp(Type a, std::string op, Type b)
{
char buf[300];
bool result = true;
if (a.typeID == T_ERROR || b.typeID == T_ERROR)
result = false;
else if (a.dimensions.size()>0 || b.dimensions.size()>0){
sprintf(buf, "one of the operands of operator '%s' is array type", op.c_str());
semanticError(buf);
result = false;
} else if ((a.typeID != T_INTEGER && a.typeID != T_REAL) || (a.typeID != b.typeID)) {
sprintf(buf, "operands of operator '%s' are not both integer or both real", op.c_str());
semanticError(buf);
result = false;
}
return result;
}
bool verifyArithOp(Type a, std::string op, Type b)
{
char buf[300];
bool result = true;
if (a.typeID == T_ERROR) {
sprintf(buf, "error in left operand of '%s' operator", op.c_str());
semanticError(buf);
result = false;
} else if (b.typeID == T_ERROR) {
sprintf(buf, "error in right operand of '%s' operator", op.c_str());
semanticError(buf);
result = false;
} else if (a.dimensions.size()>0 || b.dimensions.size()>0){
sprintf(buf, "one of the operands of operator '%s' is array type", op.c_str());
semanticError(buf);
result = false;
}
return result;
}
bool verifyModOp(Type a, Type b)
{
char buf[300];
bool result = true;
if (a.typeID != T_INTEGER || b.typeID != T_INTEGER) {
semanticError("one of the operands of operator 'mod' is not integer");
result = false;
} else if (a.dimensions.size()>0 || b.dimensions.size()>0){
sprintf(buf, "one of the operands of operator 'mod' is array type");
semanticError(buf);
result = false;
}
return result;
}