Add a BinaryOperation class

Parent of Fraction, Power, Subtraction

Change-Id: Ibb03e96062acd4870c5f10fe21eeea896a5927ec
This commit is contained in:
Romain Goyet
2016-03-24 15:49:58 +01:00
parent aa3595ad54
commit 202d110488
10 changed files with 82 additions and 65 deletions

View File

@@ -0,0 +1,16 @@
#ifndef POINCARE_BINARY_OPERATION_H
#define POINCARE_BINARY_OPERATION_H
#include <poincare/expression.h>
class BinaryOperation : public Expression {
public:
BinaryOperation(Expression ** operands, bool cloneOperands = true);
~BinaryOperation();
Expression * operand(int i) override;
int numberOfOperands() override;
protected:
Expression * m_operands[2];
};
#endif

View File

@@ -1,19 +1,15 @@
#ifndef POINCARE_FRACTION_H
#define POINCARE_FRACTION_H
#include <poincare/expression.h>
#include <poincare/binary_operation.h>
class Fraction : public Expression {
class Fraction : public BinaryOperation {
using BinaryOperation::BinaryOperation;
public:
Fraction(Expression * numerator, Expression * denominator);
~Fraction();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
private:
Expression * m_numerator;
Expression * m_denominator;
};
#endif

View File

@@ -0,0 +1,12 @@
#ifndef POINCARE_LEAF_EXPRESSION_H
#define POINCARE_LEAF_EXPRESSION_H
#include <poincare/expression.h>
class LeafExpression : public Expression {
public:
Expression * operand(int i) override;
int numberOfOperands() override;
};
#endif

View File

@@ -1,19 +1,15 @@
#ifndef POINCARE_POWER_H
#define POINCARE_POWER_H
#include <poincare/expression.h>
#include <poincare/binary_operation.h>
class Power : public Expression {
class Power : public BinaryOperation {
using BinaryOperation::BinaryOperation;
public:
Power(Expression * base, Expression * exponent);
~Power();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
private:
Expression * m_base;
Expression * m_exponent;
};
#endif

View File

@@ -1,19 +1,15 @@
#ifndef POINCARE_SUBSTRACTION_H
#define POINCARE_SUBSTRACTION_H
#include <poincare/expression.h>
#include <poincare/binary_operation.h>
class Subtraction : public Expression {
class Subtraction : public BinaryOperation {
using BinaryOperation::BinaryOperation;
public:
Subtraction(Expression * first_operand, Expression * second_operand);
~Subtraction();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
private:
Expression * m_left;
Expression * m_right;
};
#endif

View File

@@ -0,0 +1,31 @@
#include <poincare/binary_operation.h>
extern "C" {
#include <assert.h>
}
BinaryOperation::BinaryOperation(Expression ** operands, bool cloneOperands) {
assert(operands != nullptr);
assert(operands[0] != nullptr);
assert(operands[1] != nullptr);
if (cloneOperands) {
m_operands[0] = operands[0]->clone();
m_operands[1] = operands[1]->clone();
} else {
m_operands[0] = operands[0];
m_operands[1] = operands[1];
}
}
BinaryOperation::~BinaryOperation() {
delete m_operands[1];
delete m_operands[0];
}
int BinaryOperation::numberOfOperands() {
return 2;
}
Expression * BinaryOperation::operand(int i) {
assert(i>0 && i<=2);
return m_operands[i];
}

View File

@@ -79,11 +79,11 @@ Root:
exp:
INTEGER { $$ = new Integer($1); }
| SYMBOL { $$ = new Symbol($1); }
| exp PLUS exp { Expression * terms[2] = {$1,$3}; $$ = new Addition(terms, 2, true); }
| exp MINUS exp { $$ = new Subtraction($1,$3); }
| exp MULTIPLY exp { Expression * terms[2] = {$1,$3}; $$ = new Product(terms, 2, true); }
| exp DIVIDE exp { $$ = new Fraction($1,$3); }
| exp POW exp { $$ = new Power($1,$3); }
| 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 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; }
;

View File

@@ -2,27 +2,17 @@
#include <string.h>
#include "layout/fraction_layout.h"
Fraction::Fraction(Expression * numerator, Expression * denominator) :
m_numerator(numerator),
m_denominator(denominator) {
}
Fraction::~Fraction() {
delete m_denominator;
delete m_numerator;
}
Expression * Fraction::clone() {
return new Fraction(m_numerator->clone(), m_denominator->clone());
return new Fraction(m_operands, true);
}
ExpressionLayout * Fraction::createLayout(ExpressionLayout * parent) {
return new FractionLayout(parent, m_numerator, m_denominator);
return new FractionLayout(parent, m_operands[0], m_operands[1]);
}
float Fraction::approximate(Context& context) {
// TODO: handle division by zero
return m_numerator->approximate(context)/m_denominator->approximate(context);
return m_operands[0]->approximate(context)/m_operands[1]->approximate(context);
}
Expression::Type Fraction::type() {

View File

@@ -2,22 +2,12 @@
#include <math.h>
#include "layout/exponent_layout.h"
Power::Power(Expression * base, Expression * exponent) :
m_base(base),
m_exponent(exponent) {
}
Power::~Power() {
delete m_exponent;
delete m_base;
}
Expression * Power::clone() {
return new Power(m_base->clone(), m_exponent->clone());
return new Power(m_operands, true);
}
float Power::approximate(Context& context) {
return powf(m_base->approximate(context), m_exponent->approximate(context));
return powf(m_operands[0]->approximate(context), m_operands[1]->approximate(context));
}
Expression::Type Power::type() {
@@ -25,5 +15,5 @@ Expression::Type Power::type() {
}
ExpressionLayout * Power::createLayout(ExpressionLayout * parent) {
return new ExponentLayout(parent, m_base, m_exponent);
return new ExponentLayout(parent, m_operands[0], m_operands[1]);
}

View File

@@ -1,22 +1,12 @@
#include <poincare/subtraction.h>
#include "layout/horizontal_layout.h"
Subtraction::Subtraction(Expression * first_operand, Expression * second_operand) {
m_left = first_operand;
m_right = second_operand;
}
Subtraction::~Subtraction() {
delete m_left;
delete m_right;
}
Expression * Subtraction::clone() {
return new Subtraction(m_left->clone(), m_right->clone());
return new Subtraction(m_operands, true);
}
float Subtraction::approximate(Context& context) {
return m_left->approximate(context) - m_right->approximate(context);
return m_operands[0]->approximate(context) - m_operands[1]->approximate(context);
}
Expression::Type Subtraction::type() {
@@ -24,5 +14,5 @@ Expression::Type Subtraction::type() {
}
ExpressionLayout * Subtraction::createLayout(ExpressionLayout * parent) {
return new HorizontalLayout(parent, m_left, '-', m_right);
return new HorizontalLayout(parent, m_operands[0], '-', m_operands[1]);
}