diff --git a/poincare/include/poincare/product.h b/poincare/include/poincare/product.h index ce1872272..bf5fa64be 100644 --- a/poincare/include/poincare/product.h +++ b/poincare/include/poincare/product.h @@ -1,17 +1,16 @@ #ifndef POINCARE_PRODUCT_H #define POINCARE_PRODUCT_H -#include +#include -class Product : public CommutativeOperation { - using CommutativeOperation::CommutativeOperation; +class Product : public BinaryOperation { + using BinaryOperation::BinaryOperation; public: Type type() const override; - float operateApproximatevelyOn(float a, float b) const override; + ExpressionLayout * createLayout() const override; + float approximate(Context& context) const override; Expression * cloneWithDifferentOperands(Expression** newOperands, int numnerOfOperands, bool cloneOperands = true) const override; - protected: - char operatorChar() const override; }; #endif diff --git a/poincare/src/expression_parser.y b/poincare/src/expression_parser.y index 48860d0de..74a9ab177 100644 --- a/poincare/src/expression_parser.y +++ b/poincare/src/expression_parser.y @@ -105,7 +105,7 @@ exp: | SYMBOL { $$ = new Symbol($1); } | exp PLUS exp { Expression * terms[2] = {$1,$3}; $$ = new Addition(terms, 2, false); } | exp MINUS exp { Expression * terms[2] = {$1,$3}; $$ = new Subtraction(terms, false); } - | exp MULTIPLY exp { Expression * terms[2] = {$1,$3}; $$ = new Product(terms, 2, false); } + | exp MULTIPLY exp { Expression * terms[2] = {$1,$3}; $$ = new Product(terms, false); } | exp DIVIDE exp { Expression * terms[2] = {$1,$3}; $$ = new Fraction(terms, false); } | exp POW exp { Expression * terms[2] = {$1,$3}; $$ = new Power(terms, false); } | LEFT_PARENTHESIS exp RIGHT_PARENTHESIS { $$ = new Parenthesis($2, false); } diff --git a/poincare/src/product.cpp b/poincare/src/product.cpp index 418721619..528f55557 100644 --- a/poincare/src/product.cpp +++ b/poincare/src/product.cpp @@ -1,22 +1,31 @@ extern "C" { #include +#include } #include - -Expression * Product::cloneWithDifferentOperands(Expression** newOperands, - int numberOfOperands, bool cloneOperands) const { - return new Product(newOperands, numberOfOperands, cloneOperands); -} - -float Product::operateApproximatevelyOn(float a, float b) const { - return a*b; -} +#include "layout/string_layout.h" +#include "layout/horizontal_layout.h" Expression::Type Product::type() const { return Expression::Type::Product; } -char Product::operatorChar() const { - return '*'; +ExpressionLayout * Product::createLayout() const { + ExpressionLayout** children_layouts = (ExpressionLayout **)malloc(3*sizeof(ExpressionLayout *)); + children_layouts[0] = m_operands[0]->createLayout(); + children_layouts[1] = new StringLayout("*", 1); + children_layouts[2] = m_operands[1]->createLayout(); + return new HorizontalLayout(children_layouts, 3); +} + +float Product::approximate(Context& context) const { + return m_operands[0]->approximate(context)*m_operands[1]->approximate(context);; +} + +Expression * Product::cloneWithDifferentOperands(Expression** newOperands, + int numberOfOperands, bool cloneOperands) const { + assert(numberOfOperands == 2); + assert(newOperands != nullptr); + return new Product(newOperands, cloneOperands); } diff --git a/poincare/src/simplify/expression_builder.cpp b/poincare/src/simplify/expression_builder.cpp index 7222a4f49..123136f16 100644 --- a/poincare/src/simplify/expression_builder.cpp +++ b/poincare/src/simplify/expression_builder.cpp @@ -39,7 +39,7 @@ Expression * ExpressionBuilder::build(ExpressionMatch matches[]) { case Expression::Type::Product: /* The children do not need to be cloned as they already have been * before. */ - result = new Product(children_expressions, numberOfChildrenExpressions, false); + result = new Product(children_expressions, false); break; default: assert(false);