[poincare] add a parenthesis object in expression

Change-Id: I050cdf061058ddaad37cc1d59f223946fcfdd6c0
This commit is contained in:
Émilie Feral
2016-11-15 18:03:24 +01:00
parent 3d96c4694d
commit afb7620217
7 changed files with 86 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ objs += $(addprefix poincare/src/,\
integer.o\
leaf_expression.o\
logarithm.o\
parenthesis.o\
power.o\
product.o\
sine.o\

View File

@@ -10,6 +10,7 @@
#include <poincare/function.h>
#include <poincare/integer.h>
#include <poincare/logarithm.h>
#include <poincare/parenthesis.h>
#include <poincare/power.h>
#include <poincare/product.h>
#include <poincare/sine.h>

View File

@@ -15,6 +15,7 @@ class Expression {
Integer,
Logarithm,
Fraction,
Parenthesis,
Power,
Product,
Sine,

View File

@@ -0,0 +1,22 @@
#ifndef POINCARE_PARENTHESIS_H
#define POINCARE_PARENTHESIS_H
#include <poincare/expression.h>
class Parenthesis : public Expression {
public:
Parenthesis(Expression * operand, bool cloneOperands = true);
~Parenthesis();
const Expression * operand(int i) const override;
int numberOfOperands() const override;
Expression * clone() const override;
ExpressionLayout * createLayout() const override;
float approximate(Context& context) const override;
Type type() const override;
Expression * cloneWithDifferentOperands(Expression** newOperands,
int numnerOfOperands, bool cloneOperands = true) const override;
protected:
Expression * m_operand;
};
#endif

View File

@@ -29,6 +29,9 @@ void print_expression(const Expression * e, int indentationLevel) {
case Expression::Type::Fraction:
std::cout << "Fraction";
break;
case Expression::Type::Parenthesis:
std::cout << "Parenthesis";
break;
case Expression::Type::Power:
std::cout << "Power";
break;

View File

@@ -88,7 +88,7 @@ exp:
| exp MULTIPLY exp { Expression * terms[2] = {$1,$3}; $$ = new Product(terms, 2, 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 { $$ = $2; }
| LEFT_PARENTHESIS exp RIGHT_PARENTHESIS { $$ = new Parenthesis($2, false); }
| FUNCTION LEFT_PARENTHESIS exp RIGHT_PARENTHESIS { $$ = $1; $1->setArgument($3, false); }
;

View File

@@ -0,0 +1,57 @@
extern "C" {
#include <assert.h>
#include <stdlib.h>
}
#include <poincare/parenthesis.h>
#include "layout/horizontal_layout.h"
#include "layout/string_layout.h"
Parenthesis::Parenthesis(Expression * operand, bool cloneOperands) {
assert(operand != nullptr);
if (cloneOperands) {
m_operand = operand->clone();
} else {
m_operand = operand;
}
}
Parenthesis::~Parenthesis() {
delete m_operand;
}
int Parenthesis::numberOfOperands() const {
return 1;
}
const Expression * Parenthesis::operand(int i) const {
assert(i == 0);
return m_operand;
}
Expression * Parenthesis::clone() const {
return this->cloneWithDifferentOperands((Expression**) &m_operand, 1, true);
}
ExpressionLayout * Parenthesis::createLayout() const {
// TODO: create a parenthesis layout to adjust parenthesis sizes to the operand
ExpressionLayout ** childrenLayouts = (ExpressionLayout **)malloc(3*sizeof(ExpressionLayout *));
childrenLayouts[0] = new StringLayout("(", 1);
childrenLayouts[1] = m_operand->createLayout();
childrenLayouts[2] = new StringLayout(")", 1);
return new HorizontalLayout(childrenLayouts, 3);
}
float Parenthesis::approximate(Context& context) const {
return m_operand->approximate(context);
}
Expression::Type Parenthesis::type() const {
return Expression::Type::Parenthesis;
}
Expression * Parenthesis::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
assert(numberOfOperands == 1);
assert(newOperands != nullptr);
return new Parenthesis(newOperands[0], cloneOperands);
}