Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for unary minus operator #78

Merged
merged 1 commit into from
Sep 3, 2024
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
1 change: 1 addition & 0 deletions include/blocks/block_replacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class block_replacer : public block_visitor {
virtual void visit(std::shared_ptr<expr>) override;
virtual void visit(std::shared_ptr<unary_expr>) override;
virtual void visit(std::shared_ptr<binary_expr>) override;
virtual void visit(std::shared_ptr<unary_minus_expr>) override;
virtual void visit(std::shared_ptr<not_expr>) override;
virtual void visit(std::shared_ptr<bitwise_not_expr>) override;
virtual void visit(std::shared_ptr<and_expr>) override;
Expand Down
2 changes: 2 additions & 0 deletions include/blocks/block_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class expr;
class unary_expr;
class binary_expr;
class not_expr;
class unary_minus_expr;
class bitwise_not_expr;
class and_expr;
class bitwise_and_expr;
Expand Down Expand Up @@ -73,6 +74,7 @@ class block_visitor {
virtual void visit(std::shared_ptr<expr>);
virtual void visit(std::shared_ptr<unary_expr>);
virtual void visit(std::shared_ptr<binary_expr>);
virtual void visit(std::shared_ptr<unary_minus_expr>);
virtual void visit(std::shared_ptr<not_expr>);
virtual void visit(std::shared_ptr<bitwise_not_expr>);
virtual void visit(std::shared_ptr<and_expr>);
Expand Down
1 change: 1 addition & 0 deletions include/blocks/c_code_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class c_code_generator : public block_visitor {
void nextl(void);

virtual void visit(not_expr::Ptr);
virtual void visit(unary_minus_expr::Ptr);
virtual void visit(bitwise_not_expr::Ptr);
virtual void visit(and_expr::Ptr);
virtual void visit(bitwise_and_expr::Ptr);
Expand Down
13 changes: 12 additions & 1 deletion include/blocks/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,18 @@ class not_expr : public unary_expr {
return unary_is_same(self<not_expr>(), other);
}
};

// For the unary minus operator
class unary_minus_expr : public unary_expr {
public:
typedef std::shared_ptr<unary_minus_expr> Ptr;
virtual void dump(std::ostream &, int) override;
virtual void accept(block_visitor *a) override {
a->visit(self<unary_minus_expr>());
}
virtual bool is_same(block::Ptr other) override {
return unary_is_same(self<unary_minus_expr>(), other);
}
};
// For the bitwise not operator
class bitwise_not_expr : public unary_expr {
public:
Expand Down
6 changes: 6 additions & 0 deletions include/builder/operator_overload.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ typename return_type_helper<T>::type operator!(const T &a) {
return ret_type(a).template builder_unary_op<block::not_expr>();
}

template <typename T>
typename return_type_helper<T>::type operator-(const T &a) {
typedef typename return_type_helper<T>::type ret_type;
return ret_type(a).template builder_unary_op<block::unary_minus_expr>();
}

template <typename T>
typename return_type_helper<T>::type operator~(const T &a) {
typedef typename return_type_helper<T>::type ret_type;
Expand Down
3 changes: 3 additions & 0 deletions src/blocks/block_replacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ void block_replacer::visit(binary_expr::Ptr a) {
void block_replacer::visit(not_expr::Ptr a) {
unary_helper(a);
}
void block_replacer::visit(unary_minus_expr::Ptr a) {
unary_helper(a);
}
void block_replacer::visit(bitwise_not_expr::Ptr a) {
unary_helper(a);
}
Expand Down
3 changes: 3 additions & 0 deletions src/blocks/block_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ void block_visitor::visit(binary_expr::Ptr a) {
void block_visitor::visit(not_expr::Ptr a) {
a->expr1->accept(this);
}
void block_visitor::visit(unary_minus_expr::Ptr a) {
a->expr1->accept(this);
}
void block_visitor::visit(bitwise_not_expr::Ptr a) {
a->expr1->accept(this);
}
Expand Down
6 changes: 6 additions & 0 deletions src/blocks/c_code_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ void c_code_generator::visit(not_expr::Ptr a) {
oss << ")";
}

void c_code_generator::visit(unary_minus_expr::Ptr a) {
oss << "-(";
a->expr1->accept(this);
oss << ")";
}

void c_code_generator::visit(bitwise_not_expr::Ptr a) {
oss << "~(";
a->expr1->accept(this);
Expand Down
5 changes: 5 additions & 0 deletions src/blocks/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ void not_expr::dump(std::ostream &oss, int indent) {
oss << "NOT_EXPR" << std::endl;
expr1->dump(oss, indent + 1);
}
void unary_minus_expr::dump(std::ostream &oss, int indent) {
printer::indent(oss, indent);
oss << "UNARY_MINUS_EXPR" << std::endl;
expr1->dump(oss, indent + 1);
}
void bitwise_not_expr::dump(std::ostream &oss, int indent) {
printer::indent(oss, indent);
oss << "BITWISE_NOT_EXPR" << std::endl;
Expand Down
Loading