Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/ast/ArithmeticExpressionNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,6 @@

#include "ir/ArithmeticTac.hpp"

std::string ArithmeticExpressionNode::checkTypes(SymbolTable &st) const {
const auto lhsType = left->checkTypes(st);
const auto rhsType = right->checkTypes(st);

if (!lhsType.empty() && !rhsType.empty()) {
if (lhsType == "int" && rhsType == "int") {
return "int";
}
}
std::cerr << "Error: ";
std::cerr << "(line " << lineno << ") ";
std::cerr << type << " operation does not support operands of types ";
std::cerr << "'" << lhsType << "' and '" << rhsType << "'.\n";
return "";
}

Operand PlusNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhs_name = left->generateIR(graph, st);
auto rhs_name = right->generateIR(graph, st);
Expand Down
5 changes: 1 addition & 4 deletions src/ast/ArithmeticExpressionNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ class ArithmeticExpressionNode : public Node {
Node *left, *right;

public:
ArithmeticExpressionNode(const std::string &t,
std::unique_ptr<Node> left_,
ArithmeticExpressionNode(const std::string &t, std::unique_ptr<Node> left_,
std::unique_ptr<Node> right_, int l)
: Node(t, l) {
left = append_child(std::move(left_));
right = append_child(std::move(right_));
}

std::string checkTypes(SymbolTable &st) const override;
};

class PlusNode : public ArithmeticExpressionNode {
Expand Down
22 changes: 0 additions & 22 deletions src/ast/ArrayAccessNode.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
#include "ast/ArrayAccessNode.hpp"
#include "ir/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;
Expand Down
1 change: 0 additions & 1 deletion src/ast/ArrayAccessNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ArrayAccessNode : public Node {
array = append_child(std::move(array_));
index = append_child(std::move(index_));
}
std::string checkTypes(SymbolTable &st) const override;
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

Expand Down
12 changes: 0 additions & 12 deletions src/ast/ArrayLengthNode.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
#include "ast/ArrayLengthNode.hpp"
#include "ir/Tac.hpp"

std::string ArrayLengthNode::checkTypes(SymbolTable &st) const {
const auto arrayType = array->checkTypes(st);
if (arrayType != "int[]") {
std::cerr << "Error: (line " << lineno << ") Invalid type '"
<< arrayType
<< "' for array length, "
"expected type 'int[]'.\n";
return "";
}
return "int";
}

Operand ArrayLengthNode::generateIR(CFG &graph, SymbolTable &st) {
auto name = graph.getTemporaryName();
st.addIntegerVariable(name);
Expand Down
2 changes: 1 addition & 1 deletion src/ast/ArrayLengthNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ArrayLengthNode : public Node {
public:
ArrayLengthNode(std::unique_ptr<Node> array_, int l)
: Node("Array length", l), array(std::move(array_)) {}
std::string checkTypes(SymbolTable &st) const override;
[[nodiscard]] const Node &getArrayNode() const { return *array; }
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

Expand Down
15 changes: 0 additions & 15 deletions src/ast/BooleanExpressionNode.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
#include "ast/BooleanExpressionNode.hpp"
#include "ir/BooleanTac.hpp"

std::string BooleanExpressionNode::checkTypes(SymbolTable &st) const {
const auto lhsType = left->checkTypes(st);
const auto rhsType = right->checkTypes(st);

if (lhsType == "boolean" && rhsType == "boolean") {
return "boolean";
}

std::cerr << "Error: ";
std::cerr << "(line " << lineno << ") ";
std::cerr << type << " operation does not support operands of types ";
std::cerr << lhsType << " and " << rhsType << ".\n";
return "";
}

Operand AndNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhs_name = left->generateIR(graph, st);
auto rhs_name = right->generateIR(graph, st);
Expand Down
1 change: 0 additions & 1 deletion src/ast/BooleanExpressionNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class BooleanExpressionNode : public Node {
left = append_child(std::move(left_));
right = append_child(std::move(right_));
}
std::string checkTypes(SymbolTable &st) const override;
};

class AndNode : public BooleanExpressionNode {
Expand Down
3 changes: 0 additions & 3 deletions src/ast/BooleanNode.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include "ast/BooleanNode.hpp"
#include "ir/Tac.hpp"

std::string TrueNode::checkTypes(SymbolTable &st) const { return "boolean"; }
std::string FalseNode::checkTypes(SymbolTable &st) const { return "boolean"; }
Operand TrueNode::generateIR(CFG &graph, SymbolTable &st) { return true; }
Operand FalseNode::generateIR(CFG &graph, SymbolTable &st) { return false; }
2 changes: 0 additions & 2 deletions src/ast/BooleanNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
class TrueNode : public Node {
public:
TrueNode(int l) : Node("TRUE", l) {};
std::string checkTypes(SymbolTable &st) const override;
Operand generateIR(CFG &graph, SymbolTable &st) override;
};
class FalseNode : public Node {
public:
FalseNode(int l) : Node("FALSE", l) {};
std::string checkTypes(SymbolTable &st) const override;
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

Expand Down
4 changes: 0 additions & 4 deletions src/ast/ClassAllocationNode.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include "ast/ClassAllocationNode.hpp"
#include "ir/Tac.hpp"

std::string ClassAllocationNode::checkTypes(SymbolTable &st) const {
return id;
}

Operand ClassAllocationNode::generateIR(CFG &graph, SymbolTable &st) {
auto name = graph.getTemporaryName();
st.addVariable(id, name);
Expand Down
1 change: 0 additions & 1 deletion src/ast/ClassAllocationNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class ClassAllocationNode : public Node {
public:
ClassAllocationNode(std::unique_ptr<Node> object, int l)
: Node("Class allocation", object->value, l), id{object->value} {}
std::string checkTypes(SymbolTable &st) const override;
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

Expand Down
10 changes: 0 additions & 10 deletions src/ast/ClassNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@

void ClassNode::accept(AstVisitor &visitor) const { visitor.visit(*this); }

std::string ClassNode::checkTypes(SymbolTable &st) const {
st.enterClassScope(className);
auto type = body->checkTypes(st);
st.exitScope();
if (type.empty()) {
return "";
}
return "void";
}

Operand ClassNode::generateIR(CFG &graph, SymbolTable &st) {
auto *currentClass = st.lookupClass(className);
st.enterClassScope(currentClass);
Expand Down
1 change: 0 additions & 1 deletion src/ast/ClassNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ClassNode : public Node {
[[nodiscard]] const std::string &getClassName() const { return className; }
[[nodiscard]] const Node &getBodyNode() const { return *body; }

std::string checkTypes(SymbolTable &st) const override;
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

Expand Down
12 changes: 0 additions & 12 deletions src/ast/ControlStatementNode.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
#include "ast/ControlStatementNode.hpp"
#include "ir/Tac.hpp"

std::string ControlStatementNode::checkTypes(SymbolTable &st) const {
const auto condType = cond->checkTypes(st);
if (!condType.empty() && condType != "boolean") {
std::cerr << "Error: ";
std::cerr << "(line " << lineno << ") ";
std::cerr << "Condition for " << type << "-statement of invalid type "
<< condType << ".\n";
return "";
}
return "void";
}

Operand IfNode::generateIR(CFG &graph, SymbolTable &st) {
auto *trueBlock = graph.newBlock();
auto *joinBlock = graph.newBlock();
Expand Down
1 change: 0 additions & 1 deletion src/ast/ControlStatementNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ControlStatementNode : public Node {
cond = append_child(std::move(cond_));
stmts = append_child(std::move(stmts_));
}
std::string checkTypes(SymbolTable &st) const override;
};
class IfNode : public ControlStatementNode {
Node *cond, *stmt;
Expand Down
20 changes: 0 additions & 20 deletions src/ast/IdentifierNode.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
#include "ast/IdentifierNode.hpp"

std::string IdentifierNode::checkTypes(SymbolTable &st) const {
auto variableLookup = st.lookupVariable(value);
if (variableLookup) {
return variableLookup->getType();
}
auto classLookup = st.lookupClass(value);
if (classLookup) {
return classLookup->getType();
}
auto methodLookup = st.lookupMethod(value);
if (methodLookup) {
return methodLookup->getType();
}

std::cerr << "Error: ";
std::cerr << "(line " << lineno << ") ";
std::cerr << "Undeclared identifier " << value << ".\n";
return "";
}

Operand IdentifierNode::generateIR(CFG &graph, SymbolTable &st) {
return value;
}
2 changes: 0 additions & 2 deletions src/ast/IdentifierNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class IdentifierNode : public Node {
IdentifierNode(const std::string &value_, int l)
: Node("Identifier", value_, l), value{value_} {}

std::string checkTypes(SymbolTable &st) const override;

Operand generateIR(CFG &graph, SymbolTable &st) override;
};

Expand Down
12 changes: 0 additions & 12 deletions src/ast/IntegerArrayAllocationNode.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
#include "ast/IntegerArrayAllocationNode.hpp"
#include "ir/Tac.hpp"

std::string IntegerArrayAllocationNode::checkTypes(SymbolTable &st) const {
const auto lengthType = length->checkTypes(st);
if (lengthType != "int") {
std::cerr << "Error: (line " << lineno << ") Invalid type '"
<< lengthType
<< "' for array length"
", expected type 'int'.\n";
return "";
}
return "int[]";
}

Operand IntegerArrayAllocationNode::generateIR(CFG &graph, SymbolTable &st) {
auto name = graph.getTemporaryName();
st.addVariable("int[]", name);
Expand Down
2 changes: 1 addition & 1 deletion src/ast/IntegerArrayAllocationNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class IntegerArrayAllocationNode : public Node {
public:
IntegerArrayAllocationNode(std::unique_ptr<Node> length_, int l)
: Node("Integer array allocation", l), length(std::move(length_)) {}
std::string checkTypes(SymbolTable &st) const override;
[[nodiscard]] const Node &getLengthNode() const { return *length; }
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

Expand Down
2 changes: 0 additions & 2 deletions src/ast/IntegerNode.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "ast/IntegerNode.hpp"

std::string IntegerNode::checkTypes(SymbolTable &st) const { return "int"; }

Operand IntegerNode::generateIR(CFG &graph, SymbolTable &st) { return value; }
2 changes: 0 additions & 2 deletions src/ast/IntegerNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class IntegerNode : public Node {
IntegerNode(const std::string &value_, int l)
: Node("Integer", value_, l), value{std::stoi(value_)} {}

std::string checkTypes(SymbolTable &st) const override;

Operand generateIR(CFG &graph, SymbolTable &st) override;
};

Expand Down
35 changes: 0 additions & 35 deletions src/ast/LogicalExpressionNode.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
#include "ast/LogicalExpressionNode.hpp"
#include "ir/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);
Expand All @@ -37,21 +17,6 @@ Operand GreaterThanNode::generateIR(CFG &graph, SymbolTable &st) {
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);
Expand Down
7 changes: 2 additions & 5 deletions src/ast/LogicalExpressionNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ class LogicalExpressionNode : public Node {
left = append_child(std::move(left_));
right = append_child(std::move(right_));
}

std::string checkTypes(SymbolTable &st) const override;
};

class LessThanNode : public LogicalExpressionNode {
public:
LessThanNode(std::unique_ptr<Node> left, std::unique_ptr<Node> right, int l)
: LogicalExpressionNode("Less-than", std::move(left),
std::move(right), l) {}
: LogicalExpressionNode("Less-than", std::move(left), std::move(right),
l) {}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

Expand All @@ -45,7 +43,6 @@ class EqualToNode : public Node {
left = append_child(std::move(left_));
right = append_child(std::move(right_));
}
std::string checkTypes(SymbolTable &st) const override;
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

Expand Down
7 changes: 0 additions & 7 deletions src/ast/MainClassNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

void MainClassNode::accept(AstVisitor &visitor) const { visitor.visit(*this); }

std::string MainClassNode::checkTypes(SymbolTable &st) const {
st.enterClassScope(mainClassName);
body->checkTypes(st);
st.exitScope();
return "void";
}

Operand MainClassNode::generateIR(CFG &graph, SymbolTable &st) {
st.enterClassScope(mainClassName);
st.enterMethodScope("main");
Expand Down
Loading