Skip to content

Commit

Permalink
add operator== for IrLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
geo committed Jan 27, 2025
1 parent 450d1be commit 40b1ca3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
Binary file added .DS_Store
Binary file not shown.
88 changes: 47 additions & 41 deletions include/Ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class IrExpr : public virtual Ir {

class IrBinaryExpr : public IrExpr {
private:
string operation;
const string operation;
IrExpr* leftOperand;
IrExpr* rightOperand;

Expand All @@ -80,15 +80,15 @@ class IrBinaryExpr : public IrExpr {
delete leftOperand;
delete rightOperand;
}
IrExpr* getLeftOperand() {
const IrExpr* getLeftOperand() const{
return this->leftOperand;
}

IrExpr* getRightOperand() {
const IrExpr* getRightOperand() const{
return this->rightOperand;
}

string& getOperation() {
const string& getOperation() const{
return this->operation;
}

Expand Down Expand Up @@ -155,13 +155,7 @@ class IrTypeBool : public IrType {
}

bool operator==(const Ir& that) const override{
if (&that == this) {
return true;
}
if (dynamic_cast<const IrTypeBool*>(&that) == nullptr) {
return false;
}
return true;
return dynamic_cast<const IrTypeBool*>(&that) != nullptr;
}

string toString() const override{
Expand All @@ -188,14 +182,7 @@ class IrTypeVoid : public IrType {
}

bool operator==(const Ir& that) const override{
if (&that == this) {
return true;
}
if (dynamic_cast<const IrTypeVoid*>(&that) == nullptr) {
return false;
}

return true;
return dynamic_cast<const IrTypeVoid*>(&that) != nullptr;
}

string toString() const override{
Expand All @@ -222,14 +209,10 @@ class IrTypeInt : public IrType {
}

bool operator==(const Ir& that) const override{
if (&that == this) {
return true;
if (auto thatTypeInt = dynamic_cast<const IrTypeInt*>(&that)) {
return this->width == thatTypeInt->width;
}
if (dynamic_cast<const IrTypeInt*>(&that) == nullptr) {
return false;
}

return true;
return false;
}

string toString() const override{
Expand All @@ -255,13 +238,7 @@ class IrTypeChar : public IrType {
}

bool operator==(const Ir& that) const override{
if (&that == this) {
return true;
}
if (dynamic_cast<const IrTypeChar*>(&that) == nullptr) {
return false;
}
return true;
return dynamic_cast<const IrTypeChar*>(&that) != nullptr;
}

string prettyPrint(string indentSpace) const override{
Expand All @@ -288,13 +265,7 @@ class IrTypeString : public IrType {
}

bool operator==(const Ir& that) const override{
if (&that == this) {
return true;
}
if (dynamic_cast<const IrTypeString*>(&that) == nullptr) {
return false;
}
return true;
return dynamic_cast<const IrTypeString*>(&that) != nullptr;
}

string toString() const override{
Expand Down Expand Up @@ -348,7 +319,7 @@ class IrTypeArray : public IrType {
if (this->dimension.size() != thatTypeArray->dimension.size()) {
return false;
}
for (int i = 0; i < this->dimension.size(); i++) {
for (size_t i = 0; i < this->dimension.size(); i++) {
if (this->dimension[i] != thatTypeArray->dimension[i]) {
return false;
}
Expand Down Expand Up @@ -380,6 +351,13 @@ class IrLiteralBool : public IrLiteral {
return new IrTypeBool(getNode());
}

bool operator==(const Ir& that) const override{
if (auto thatLiteralBool = dynamic_cast<const IrLiteralBool*>(&that)) {
return this->value == thatLiteralBool->value;
}
return false;
}

string prettyPrint(string indentSpace) {
string prettyPrint = indentSpace + "|--boolLiteral\n";
prettyPrint += addIndent(indentSpace)+ "|--value: " + (this->value ? "true" : "false") + "\n";
Expand Down Expand Up @@ -411,6 +389,13 @@ class IrLiteralChar : public IrLiteral {
return new IrTypeVoid(getNode());
}

bool operator==(const Ir& that) const override{
if (auto thatLiteralChar = dynamic_cast<const IrLiteralChar*>(&that)) {
return this->value == thatLiteralChar->value;
}
return false;
}

string prettyPrint(string indentSpace) {
string prettyPrint = indentSpace + "|--charLiteral\n";
prettyPrint += addIndent(indentSpace) + "|--value: " + this->value + "\n";
Expand Down Expand Up @@ -446,6 +431,13 @@ class IrLiteralNumber : public IrLiteral {
return new IrTypeInt(getNode());
}

bool operator==(const Ir& that) const override{
if (auto thatLiteralNumber = dynamic_cast<const IrLiteralNumber*>(&that)) {
return this->value == thatLiteralNumber->value;
}
return false;
}

string prettyPrint(string indentSpace) const override {
string prettyPrint = indentSpace + "|--NumberLiteral\n";
prettyPrint += addIndent(indentSpace) + "|--value: " + to_string(this->value) + "\n";
Expand Down Expand Up @@ -479,6 +471,13 @@ class IrLiteralStringContent : public IrLiteral {
return value;
}

bool operator==(const Ir& that) const override{
if (auto thatLiteralStringContent = dynamic_cast<const IrLiteralStringContent*>(&that)) {
return this->value == thatLiteralStringContent->value;
}
return false;
}

string prettyPrint(string indentSpace) const override {
return indentSpace + "|--stringContent: " + value + "\n";
}
Expand All @@ -505,6 +504,13 @@ class IrLiteralString : public IrLiteral {
return stringContent->getValue();
}

bool operator==(const Ir& that) const override{
if (auto thatLiteralString = dynamic_cast<const IrLiteralString*>(&that)) {
return *this->stringContent == *thatLiteralString->stringContent;
}
return false;
}

string prettyPrint(string indentSpace) const override {
string prettyPrint = indentSpace + "|--StringLiteral\n";
prettyPrint += stringContent->prettyPrint(addIndent(indentSpace));
Expand Down

0 comments on commit 40b1ca3

Please sign in to comment.