-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayAccessNode.cpp
33 lines (29 loc) · 1.04 KB
/
ArrayAccessNode.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
#include "ArrayAccessNode.hpp"
#include "Tac.hpp"
std::string ArrayAccessNode::checkTypes(SymbolTable &st) const {
const auto arrayType = array->checkTypes(st);
const auto indexType = index->checkTypes(st);
if (indexType != "int") {
std::cerr << "Error: (line " << lineno << ") ";
std::cerr << "Invalid array index type ";
std::cerr << "'" << indexType << "', ";
std::cerr << "expected type 'int'.\n";
return "";
}
if (arrayType != "int[]") {
std::cerr << "Error: (line " << lineno << ") ";
std::cerr << "Invalid array type ";
std::cerr << "'" << indexType << "', ";
std::cerr << "expected type 'int[]'.\n";
return "";
}
return "int";
}
Operand ArrayAccessNode::generateIR(CFG &graph, SymbolTable &st) {
auto indexName = index->generateIR(graph, st);
auto arrayName = array->value;
auto name = graph.getTemporaryName();
st.addIntegerVariable(name);
graph.addInstruction(new ArrayAccessTac(name, arrayName, indexName));
return name;
}