Merge branch 'master' of ssh://git.numworks.com:2222/CalcOS

Change-Id: I5dca6556382400727625ff2e97cba8f382be3ca7
This commit is contained in:
Romain Goyet
2016-04-12 18:47:47 +02:00
29 changed files with 120 additions and 46 deletions

View File

@@ -36,8 +36,8 @@ tests += $(addprefix poincare/test/,\
fraction.cpp\
integer.cpp\
product.cpp\
simplify.cpp\
simplify_addition_integer.cpp\
simplify_addition.cpp\
simplify_product.cpp\
trigo.cpp\
subtraction.cpp\
)

View File

@@ -8,7 +8,8 @@ class Addition : public CommutativeOperation {
public:
Type type() override;
float operateApproximatevelyOn(float a, float b) override;
Expression * clone() override;
Expression * cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands = true) override;
protected:
char operatorChar() override;
};

View File

@@ -9,6 +9,7 @@ class BinaryOperation : public Expression {
~BinaryOperation();
Expression * operand(int i) override;
int numberOfOperands() override;
Expression * clone() override;
protected:
Expression * m_operands[2];
};

View File

@@ -12,6 +12,7 @@ class CommutativeOperation : public Expression {
float approximate(Context& context) override;
ExpressionLayout * createLayout() override;
bool isCommutative() override;
Expression * clone() override;
protected:
virtual float operateApproximatevelyOn(float a, float b) = 0;
virtual char operatorChar() = 0;

View File

@@ -8,7 +8,8 @@ class Cosine : public Function {
Cosine(Expression * arg, bool clone_arg=true): Function(arg, (char*) "cos", clone_arg) {}
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
Expression * cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands = true) override;
};
#endif

View File

@@ -28,6 +28,8 @@ class Expression {
virtual Expression * operand(int i) = 0;
virtual int numberOfOperands() = 0;
virtual Expression * clone() = 0;
virtual Expression * cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands = true) = 0;
// TODO: Consider std::unique_ptr - see https://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Ownership_and_Smart_Pointers

View File

@@ -9,7 +9,8 @@ class Fraction : public BinaryOperation {
ExpressionLayout * createLayout() override;
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
Expression * cloneWithDifferentOperands(Expression** newOperands,
int numnerOfOperands, bool cloneOperands = true) override;
};
#endif

View File

@@ -5,15 +5,16 @@
class Function : public Expression {
public:
Function(Expression * arg, char* function_name, bool clone_operands=true);
Function(Expression * arg, char* functionName, bool cloneOperands=true);
~Function();
ExpressionLayout * createLayout() override;
Expression * operand(int i) override;
int numberOfOperands() override;
Expression * clone() override;
protected:
Expression * m_arg;
private:
char* m_function_name;
char* m_functionName;
};
#endif

View File

@@ -7,6 +7,8 @@ class LeafExpression : public Expression {
public:
Expression * operand(int i) override;
int numberOfOperands() override;
Expression * cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands = true) override;
};
#endif

View File

@@ -9,7 +9,8 @@ class Power : public BinaryOperation {
ExpressionLayout * createLayout() override;
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
Expression * cloneWithDifferentOperands(Expression** newOperands,
int numnerOfOperands, bool cloneOperands = true) override;
};
#endif

View File

@@ -8,7 +8,8 @@ class Product : public CommutativeOperation {
public:
Type type() override;
float operateApproximatevelyOn(float a, float b) override;
Expression * clone() override;
Expression * cloneWithDifferentOperands(Expression** newOperands,
int numnerOfOperands, bool cloneOperands = true) override;
protected:
char operatorChar() override;
};

View File

@@ -8,7 +8,8 @@ class Sine : public Function {
Sine(Expression * arg, bool clone_arg=true): Function(arg, (char*) "sin", clone_arg) {}
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
Expression * cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands = true) override;
};
#endif

View File

@@ -9,7 +9,8 @@ class Subtraction : public BinaryOperation {
ExpressionLayout * createLayout() override;
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
Expression * cloneWithDifferentOperands(Expression** newOperands,
int numnerOfOperands, bool cloneOperands = true) override;
};
#endif

View File

@@ -8,7 +8,8 @@ class Tangent : public Function {
Tangent(Expression * arg, bool clone_arg=true): Function(arg, (char*) "tan", clone_arg) {}
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
Expression * cloneWithDifferentOperands(Expression** newOperands,
int numnerOfOperands, bool cloneOperands = true) override;
};
#endif

View File

@@ -5,8 +5,9 @@ Expression::Type Addition::type() {
return Expression::Type::Addition;
}
Expression * Addition::clone() {
return new Addition(m_operands, m_numberOfOperands, true);
Expression * Addition::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) {
return new Addition(newOperands, numberOfOperands, cloneOperands);
}
float Addition::operateApproximatevelyOn(float a, float b) {

View File

@@ -29,3 +29,7 @@ Expression * BinaryOperation::operand(int i) {
assert(i>0 && i<=2);
return m_operands[i];
}
Expression * BinaryOperation::clone() {
return this->cloneWithDifferentOperands(m_operands, 2, true);
}

View File

@@ -8,6 +8,7 @@ extern "C" {
#include "layout/string_layout.h"
CommutativeOperation::CommutativeOperation(Expression ** operands, int numberOfOperands, bool cloneOperands) {
assert(operands != nullptr);
assert(numberOfOperands >= 2);
m_numberOfOperands = numberOfOperands;
m_operands = (Expression **)malloc(numberOfOperands*sizeof(Expression *));
@@ -37,6 +38,10 @@ Expression * CommutativeOperation::operand(int i) {
return m_operands[i];
}
Expression * CommutativeOperation::clone() {
return this->cloneWithDifferentOperands(m_operands, m_numberOfOperands, true);
}
float CommutativeOperation::approximate(Context& context) {
float result = m_operands[0]->approximate(context);
for (size_t i=1; i<m_numberOfOperands; i++) {
@@ -61,4 +66,3 @@ ExpressionLayout * CommutativeOperation::createLayout() {
bool CommutativeOperation::isCommutative() {
return true;
}

View File

@@ -1,14 +1,21 @@
extern "C" {
#include <assert.h>
}
#include <poincare/cosine.h>
#include "layout/horizontal_layout.h"
Expression * Cosine::clone() {
return new Cosine(m_arg, true);
}
Expression::Type Cosine::type() {
return Expression::Type::Cosine;
}
Expression * Cosine::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) {
assert(numberOfOperands == 1);
assert(newOperands != nullptr);
return new Cosine(*newOperands, cloneOperands);
}
float Cosine::approximate(Context& context) {
// FIXME: use cosine obviously.
return m_arg->approximate(context);

View File

@@ -1,7 +1,7 @@
#include <stdlib.h>
#include <string.h>
extern "C" {
#include <assert.h>
#include <stdlib.h>
#include <string.h>
}
#include <poincare/float.h>

View File

@@ -1,9 +1,16 @@
#include <poincare/fraction.h>
extern "C" {
#include <assert.h>
#include <string.h>
}
#include <poincare/fraction.h>
#include "layout/fraction_layout.h"
Expression * Fraction::clone() {
return new Fraction(m_operands, true);
Expression * Fraction::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) {
assert(numberOfOperands == 2);
assert(newOperands != nullptr);
return new Fraction(newOperands, cloneOperands);
}
ExpressionLayout * Fraction::createLayout() {

View File

@@ -1,14 +1,16 @@
extern "C" {
#include <assert.h>
#include <stdlib.h>
}
#include <poincare/function.h>
#include "layout/horizontal_layout.h"
#include "layout/string_layout.h"
Function::Function(Expression * arg, char* function_name, bool clone_operands) {
Function::Function(Expression * arg, char* functionName, bool cloneOperands) {
assert(arg != nullptr);
m_arg = (Expression *)malloc(sizeof(Expression));
m_function_name = function_name;
if (clone_operands) {
m_functionName = functionName;
if (cloneOperands) {
m_arg = arg->clone();
} else {
m_arg = arg;
@@ -19,9 +21,13 @@ Function::~Function() {
delete m_arg;
}
Expression * Function::clone() {
return this->cloneWithDifferentOperands(&m_arg, 1, true);
}
ExpressionLayout * Function::createLayout() {
ExpressionLayout** children_layouts = (ExpressionLayout **)malloc(4*sizeof(ExpressionLayout *));
children_layouts[0] = new StringLayout(m_function_name, strlen(m_function_name));
children_layouts[0] = new StringLayout(m_functionName, strlen(m_functionName));
char string[2] = {'(', '\0'};
children_layouts[1] = new StringLayout(string, 1);
children_layouts[2] = m_arg->createLayout();

View File

@@ -12,3 +12,9 @@ Expression * LeafExpression::operand(int i) {
assert(false);
return nullptr;
}
Expression * LeafExpression::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) {
assert(false);
return nullptr;
}

View File

@@ -1,11 +1,11 @@
#include <poincare/power.h>
extern "C" {
#include <assert.h>
#include <math.h>
#include "layout/exponent_layout.h"
Expression * Power::clone() {
return new Power(m_operands, true);
}
#include <poincare/power.h>
#include "layout/exponent_layout.h"
float Power::approximate(Context& context) {
return powf(m_operands[0]->approximate(context), m_operands[1]->approximate(context));
}
@@ -14,6 +14,12 @@ Expression::Type Power::type() {
return Expression::Type::Power;
}
Expression * Power::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) {
assert(numberOfOperands == 2);
return new Power(newOperands, cloneOperands);
}
ExpressionLayout * Power::createLayout() {
return new ExponentLayout(m_operands[0]->createLayout(), m_operands[1]->createLayout());
}

View File

@@ -1,11 +1,12 @@
#include <poincare/product.h>
#include "layout/horizontal_layout.h"
extern "C" {
#include <stdlib.h>
#include <assert.h>
}
Expression * Product::clone() {
return new Product(m_operands, m_numberOfOperands, true);
#include <poincare/product.h>
Expression * Product::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) {
return new Product(newOperands, numberOfOperands, cloneOperands);
}
float Product::operateApproximatevelyOn(float a, float b) {

View File

@@ -1,8 +1,14 @@
#include <poincare/sine.h>
#include "layout/horizontal_layout.h"
Expression * Sine::clone() {
return new Sine(m_arg, true);
extern "C" {
#include <assert.h>
}
Expression * Sine::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) {
assert(newOperands != nullptr);
assert(numberOfOperands == 1);
return new Sine(*newOperands, cloneOperands);
}
Expression::Type Sine::type() {

View File

@@ -1,12 +1,17 @@
extern "C" {
#include <assert.h>
#include <stdlib.h>
}
#include <poincare/subtraction.h>
#include "layout/horizontal_layout.h"
#include "layout/string_layout.h"
Expression * Subtraction::clone() {
return new Subtraction(m_operands, true);
Expression * Subtraction::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) {
assert(newOperands != nullptr);
assert(numberOfOperands == 2);
return new Subtraction(newOperands, cloneOperands);
}
float Subtraction::approximate(Context& context) {

View File

@@ -1,8 +1,14 @@
#include <poincare/tangent.h>
#include "layout/horizontal_layout.h"
Expression * Tangent::clone() {
return new Tangent(m_arg, true);
extern "C" {
#include <assert.h>
}
Expression * Tangent::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) {
assert(newOperands != nullptr);
assert(numberOfOperands == 1);
return new Tangent(*newOperands, cloneOperands);
}
Expression::Type Tangent::type() {