From df3d9fd68ceb3bba5d5e54285e78d1abb59d4771 Mon Sep 17 00:00:00 2001 From: RadhikaG Date: Tue, 3 Sep 2024 16:46:11 -0400 Subject: [PATCH] added support for unary minus operator --- include/blocks/block_replacer.h | 1 + include/blocks/block_visitor.h | 2 ++ include/blocks/c_code_generator.h | 1 + include/blocks/expr.h | 13 ++++++++++++- include/builder/operator_overload.h | 6 ++++++ src/blocks/block_replacer.cpp | 3 +++ src/blocks/block_visitor.cpp | 3 +++ src/blocks/c_code_generator.cpp | 6 ++++++ src/blocks/expr.cpp | 5 +++++ 9 files changed, 39 insertions(+), 1 deletion(-) diff --git a/include/blocks/block_replacer.h b/include/blocks/block_replacer.h index 2b7ff49..3be4ffd 100644 --- a/include/blocks/block_replacer.h +++ b/include/blocks/block_replacer.h @@ -24,6 +24,7 @@ class block_replacer : public block_visitor { virtual void visit(std::shared_ptr) override; virtual void visit(std::shared_ptr) override; virtual void visit(std::shared_ptr) override; + virtual void visit(std::shared_ptr) override; virtual void visit(std::shared_ptr) override; virtual void visit(std::shared_ptr) override; virtual void visit(std::shared_ptr) override; diff --git a/include/blocks/block_visitor.h b/include/blocks/block_visitor.h index ac27d69..1317462 100644 --- a/include/blocks/block_visitor.h +++ b/include/blocks/block_visitor.h @@ -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; @@ -73,6 +74,7 @@ class block_visitor { virtual void visit(std::shared_ptr); virtual void visit(std::shared_ptr); virtual void visit(std::shared_ptr); + virtual void visit(std::shared_ptr); virtual void visit(std::shared_ptr); virtual void visit(std::shared_ptr); virtual void visit(std::shared_ptr); diff --git a/include/blocks/c_code_generator.h b/include/blocks/c_code_generator.h index c3b6147..a820840 100644 --- a/include/blocks/c_code_generator.h +++ b/include/blocks/c_code_generator.h @@ -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); diff --git a/include/blocks/expr.h b/include/blocks/expr.h index 7a59e2d..655d773 100644 --- a/include/blocks/expr.h +++ b/include/blocks/expr.h @@ -82,7 +82,18 @@ class not_expr : public unary_expr { return unary_is_same(self(), other); } }; - +// For the unary minus operator +class unary_minus_expr : public unary_expr { +public: + typedef std::shared_ptr Ptr; + virtual void dump(std::ostream &, int) override; + virtual void accept(block_visitor *a) override { + a->visit(self()); + } + virtual bool is_same(block::Ptr other) override { + return unary_is_same(self(), other); + } +}; // For the bitwise not operator class bitwise_not_expr : public unary_expr { public: diff --git a/include/builder/operator_overload.h b/include/builder/operator_overload.h index 8726202..7c2d09c 100644 --- a/include/builder/operator_overload.h +++ b/include/builder/operator_overload.h @@ -151,6 +151,12 @@ typename return_type_helper::type operator!(const T &a) { return ret_type(a).template builder_unary_op(); } +template +typename return_type_helper::type operator-(const T &a) { + typedef typename return_type_helper::type ret_type; + return ret_type(a).template builder_unary_op(); +} + template typename return_type_helper::type operator~(const T &a) { typedef typename return_type_helper::type ret_type; diff --git a/src/blocks/block_replacer.cpp b/src/blocks/block_replacer.cpp index 6a000df..5378a29 100644 --- a/src/blocks/block_replacer.cpp +++ b/src/blocks/block_replacer.cpp @@ -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); } diff --git a/src/blocks/block_visitor.cpp b/src/blocks/block_visitor.cpp index 5a19c68..7fcf318 100644 --- a/src/blocks/block_visitor.cpp +++ b/src/blocks/block_visitor.cpp @@ -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); } diff --git a/src/blocks/c_code_generator.cpp b/src/blocks/c_code_generator.cpp index b155501..45dfd0c 100644 --- a/src/blocks/c_code_generator.cpp +++ b/src/blocks/c_code_generator.cpp @@ -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); diff --git a/src/blocks/expr.cpp b/src/blocks/expr.cpp index 8025d64..6768a2f 100644 --- a/src/blocks/expr.cpp +++ b/src/blocks/expr.cpp @@ -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;