-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogicalExpressionNode.cpp
62 lines (56 loc) · 2.13 KB
/
LogicalExpressionNode.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
#include "LogicalExpressionNode.hpp"
#include "LogicalTac.hpp"
std::string LogicalExpressionNode::checkTypes(SymbolTable &st) const {
const auto lhsType = left->checkTypes(st);
const auto rhsType = right->checkTypes(st);
if (lhsType == "int" && rhsType == "int") {
return "boolean";
}
if (!lhsType.empty() && !rhsType.empty()) {
std::cerr << "Error: (line " << lineno << ") " << type << " "
<< "operation does not "
"support operands of types '"
<< lhsType
<< "' and "
"'"
<< rhsType << "'.\n";
}
return "";
}
Operand LessThanNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhs_name = left->generateIR(graph, st);
auto rhs_name = right->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addBooleanVariable(name);
graph.addInstruction(new LessThanTac(name, lhs_name, rhs_name));
return name;
}
Operand GreaterThanNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhs_name = left->generateIR(graph, st);
auto rhs_name = right->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addBooleanVariable(name);
graph.addInstruction(new GreaterThanTac(name, lhs_name, rhs_name));
return name;
}
std::string EqualToNode::checkTypes(SymbolTable &st) const {
const auto lhsType = left->checkTypes(st);
const auto rhsType = right->checkTypes(st);
if ((lhsType == "boolean" && rhsType == "boolean") ||
(lhsType == "int" && rhsType == "int")) {
return "boolean";
}
std::cerr << "Error: ";
std::cerr << "(line " << lineno << ") ";
std::cerr << "Operator '==' does not support operands of types ";
std::cerr << "'" << lhsType << "' and '" << rhsType << "'.\n";
return "";
}
Operand EqualToNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhs_name = left->generateIR(graph, st);
auto rhs_name = right->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addBooleanVariable(name);
graph.addInstruction(new EqualToTac(name, lhs_name, rhs_name));
return name;
}