Merge changes I44c1bed2,Ibcf1dd17,Id02daf67

* changes:
  Use proper english name for sine.
  Add parsing test for trigo functions.
  Horizontal layout finished.
This commit is contained in:
Félix Raimundo
2016-03-25 11:09:20 +01:00
committed by Gerrit
20 changed files with 119 additions and 98 deletions

View File

@@ -4,7 +4,7 @@ objs += $(addprefix poincare/src/,\
binary_operation.o\
commutative_operation.o\
context.o\
cosinus.o\
cosine.o\
expression.o\
expression_lexer.o\
expression_parser.o\
@@ -15,7 +15,7 @@ objs += $(addprefix poincare/src/,\
leaf_expression.o\
power.o\
product.o\
sinus.o\
sine.o\
subtraction.o\
symbol.o\
tangent.o\
@@ -40,6 +40,7 @@ tests += $(addprefix poincare/test/,\
product.cpp\
simplify.cpp\
simplify_addition_integer.cpp\
trigo.cpp\
subtraction.cpp\
)

View File

@@ -3,7 +3,7 @@
#include <poincare/addition.h>
#include <poincare/context.h>
#include <poincare/cosinus.h>
#include <poincare/cosine.h>
#include <poincare/expression.h>
#include <poincare/float.h>
#include <poincare/fraction.h>
@@ -11,7 +11,7 @@
#include <poincare/integer.h>
#include <poincare/power.h>
#include <poincare/product.h>
#include <poincare/sinus.h>
#include <poincare/sine.h>
#include <poincare/subtraction.h>
#include <poincare/symbol.h>
#include <poincare/tangent.h>

View File

@@ -0,0 +1,14 @@
#ifndef POINCARE_COSINE_H
#define POINCARE_COSINE_H
#include <poincare/function.h>
class Cosine : public Function {
public:
Cosine(Expression * arg, bool clone_arg=true): Function(arg, (char*) "cos", clone_arg) {}
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
};
#endif

View File

@@ -1,14 +0,0 @@
#ifndef POINCARE_COSINUS_H
#define POINCARE_COSINUS_H
#include <poincare/function.h>
class Cosinus : public Function {
public:
Cosinus(Expression * arg, bool clone_arg=true): Function(arg, (char*) "cos", clone_arg) {}
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
};
#endif

View File

@@ -10,13 +10,13 @@ class Expression {
public:
enum class Type : uint8_t {
Addition,
Cosinus,
Cosine,
Float,
Fraction,
Integer,
Power,
Product,
Sinus,
Sine,
Subtraction,
Symbol,
Tangent,

View File

@@ -0,0 +1,14 @@
#ifndef POINCARE_SINE_H
#define POINCARE_SINE_H
#include <poincare/function.h>
class Sine : public Function {
public:
Sine(Expression * arg, bool clone_arg=true): Function(arg, (char*) "sin", clone_arg) {}
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
};
#endif

View File

@@ -1,14 +0,0 @@
#ifndef POINCARE_SINUS_H
#define POINCARE_SINUS_H
#include <poincare/function.h>
class Sinus : public Function {
public:
Sinus(Expression * arg, bool clone_arg=true): Function(arg, (char*) "sin", clone_arg) {}
float approximate(Context& context) override;
Type type() override;
Expression * clone() override;
};
#endif

View File

@@ -14,6 +14,5 @@ float Addition::operateApproximatevelyOn(float a, float b) {
}
ExpressionLayout * Addition::createLayout(ExpressionLayout * parent) {
//FIXME: There can be more than two operands now! :-)
return new HorizontalLayout(parent, operand(0), '+', operand(1));
return new HorizontalLayout(parent, m_operands, m_numberOfOperands, '+');
}

View File

@@ -30,6 +30,7 @@ int CommutativeOperation::numberOfOperands() {
}
Expression * CommutativeOperation::operand(int i) {
assert(i < m_numberOfOperands);
return m_operands[i];
}

15
poincare/src/cosine.cpp Normal file
View File

@@ -0,0 +1,15 @@
#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;
}
float Cosine::approximate(Context& context) {
// FIXME: use cosine obviously.
return m_arg->approximate(context);
}

View File

@@ -1,15 +0,0 @@
#include <poincare/cosinus.h>
#include "layout/horizontal_layout.h"
Expression * Cosinus::clone() {
return new Cosinus(m_arg, true);
}
Expression::Type Cosinus::type() {
return Expression::Type::Cosinus;
}
float Cosinus::approximate(Context& context) {
// FIXME: use cosinus obviously.
return m_arg->approximate(context);
}

View File

@@ -42,10 +42,10 @@ class Expression;
%%
[0-9]+ { yylval->string = yytext; return(INTEGER); }
[A-Za-z]+ { yylval->string = yytext; return(SYMBOL); }
sin {return(SINUS);}
cos {return(COSINUS);}
sin {return(SINE);}
cos {return(COSINE);}
tan {return(TANGENT);}
[A-Za-z]+ { yylval->string = yytext; return(SYMBOL); }
\+ { return(PLUS); }
\- { return(MINUS); }
\* { return(MULTIPLY); }

View File

@@ -54,8 +54,8 @@ void poincare_expression_yyerror(void * scanner, Expression ** expressionOutput,
%token MULTIPLY
%token DIVIDE
%token POW
%token SINUS
%token COSINUS
%token SINE
%token COSINE
%token TANGENT
%token LEFT_PARENTHESIS
%token RIGHT_PARENTHESIS
@@ -88,8 +88,8 @@ exp:
| 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; }
| SINUS LEFT_PARENTHESIS exp RIGHT_PARENTHESIS { $$ = new Sinus($3); }
| COSINUS LEFT_PARENTHESIS exp RIGHT_PARENTHESIS { $$ = new Cosinus($3); }
| SINE LEFT_PARENTHESIS exp RIGHT_PARENTHESIS { $$ = new Sine($3); }
| COSINE LEFT_PARENTHESIS exp RIGHT_PARENTHESIS { $$ = new Cosine($3); }
| TANGENT LEFT_PARENTHESIS exp RIGHT_PARENTHESIS { $$ = new Tangent($3); }
;

View File

@@ -1,29 +1,28 @@
extern "C" {
#include <kandinsky.h>
#include <assert.h>
#include <kandinsky.h>
#include <stdlib.h>
}
#include "horizontal_layout.h"
#include "string_layout.h"
HorizontalLayout::HorizontalLayout(ExpressionLayout * parent, int number_of_operands, Expression ** operands, char symbol) {
m_number_of_operands = number_of_operands;
// FIXME: This implementation is not optimal as the operator layout is created and stored a lot of times.
// The reason for this is how the layouts are drawn.
m_children_layouts = (ExpressionLayout **)malloc((2*m_number_of_operands-)*sizeof(ExpressionLayout *));
m_operator_layout = new StringLayout(this, string, 1);
HorizontalLayout::HorizontalLayout(ExpressionLayout * parent, Expression ** operands,int number_of_operands, char symbol) : ExpressionLayout(parent) {
assert(number_of_operands > 0);
m_number_of_children = 2*number_of_operands-1;
m_children_layouts = (ExpressionLayout **)malloc(m_number_of_children*sizeof(ExpressionLayout *));
char string[2] = {symbol, '\0'};
for (int i=1; i<m_number_of_operands; i++) {
m_operands[2*i-1] = m_operator_layout;
m_operands[2*i] = operands[i]->createLayout();
m_children_layouts[0] = operands[0]->createLayout(this);
for (int i=1; i<number_of_operands; i++) {
m_children_layouts[2*i-1] = new StringLayout(this, string, 1);
m_children_layouts[2*i] = operands[i]->createLayout(this);
}
}
HorizontalLayout::~HorizontalLayout() {
for (int i(0); i<m_numberOfOperands; i++) {
delete m_operands[2*i];
for (int i=0; i<m_number_of_children; i++) {
delete m_children_layouts[i];
}
free(m_operands);
delete m_operator_layout;
free(m_children_layouts);
}
void HorizontalLayout::render(KDPoint point) { }
@@ -42,23 +41,25 @@ KDSize HorizontalLayout::computeSize() {
}
ExpressionLayout * HorizontalLayout::child(uint16_t index) {
if (index >= 2*m_number_of_operands) {
assert(index <= m_number_of_children);
if (index < m_number_of_children) {
return m_children_layouts[index];
} else {
return nullptr;
}
return m_children_layouts[index];
}
KDPoint HorizontalLayout::positionOfChild(ExpressionLayout * child) {
KDPoint position = (KDPoint){.x = 0, .y = 0};
uint16_t index = 0;
for (int i=0;i<3;i++) {
if (m_children[i] == child) {
for (int i=0;i<m_number_of_children;i++) {
if (m_children_layouts[i] == child) {
index = i;
break;
}
}
if (index > 0) {
ExpressionLayout * previousChild = m_children[index-1];
ExpressionLayout * previousChild = m_children_layouts[index-1];
assert(previousChild != nullptr);
position.x = previousChild->origin().x + previousChild->size().width;
}

View File

@@ -6,7 +6,7 @@
class HorizontalLayout : public ExpressionLayout {
public:
HorizontalLayout(ExpressionLayout * parent, int number_of_operands, Expression ** operands, char symbol);
HorizontalLayout(ExpressionLayout * parent, Expression ** operands, int number_of_children, char symbol);
~HorizontalLayout();
protected:
void render(KDPoint point) override;
@@ -14,9 +14,8 @@ class HorizontalLayout : public ExpressionLayout {
ExpressionLayout * child(uint16_t index) override;
KDPoint positionOfChild(ExpressionLayout * child) override;
private:
int m_number_of_operands;
int m_number_of_children;
ExpressionLayout ** m_children_layouts;
ExpressionLayout * m_operator_layout;
};
#endif

View File

@@ -17,6 +17,5 @@ Expression::Type Product::type() {
}
ExpressionLayout * Product::createLayout(ExpressionLayout * parent) {
//FIXME: There can be more than two factors now! :-)
return new HorizontalLayout(parent, operand(0), '*', operand(1));
return new HorizontalLayout(parent, m_operands, m_numberOfOperands, '*');
}

15
poincare/src/sine.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include <poincare/sine.h>
#include "layout/horizontal_layout.h"
Expression * Sine::clone() {
return new Sine(m_arg, true);
}
Expression::Type Sine::type() {
return Expression::Type::Sine;
}
float Sine::approximate(Context& context) {
// FIXME: use sine obviously.
return m_arg->approximate(context);
}

View File

@@ -1,15 +0,0 @@
#include <poincare/sinus.h>
#include "layout/horizontal_layout.h"
Expression * Sinus::clone() {
return new Sinus(m_arg, true);
}
Expression::Type Sinus::type() {
return Expression::Type::Sinus;
}
float Sinus::approximate(Context& context) {
// FIXME: use sinus obviously.
return m_arg->approximate(context);
}

View File

@@ -14,5 +14,5 @@ Expression::Type Subtraction::type() {
}
ExpressionLayout * Subtraction::createLayout(ExpressionLayout * parent) {
return new HorizontalLayout(parent, m_operands[0], '-', m_operands[1]);
return new HorizontalLayout(parent, m_operands, 2, '-');
}

21
poincare/test/trigo.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include <quiz.h>
#include <poincare.h>
#include <assert.h>
QUIZ_CASE(poincare_parse_trigo) {
{
Expression * e = Expression::parse("sin(0)");
Expression * e2 = e->simplify();
assert(e2->type() == Expression::Type::Sine);
}
{
Expression * e = Expression::parse("cos(0)");
Expression * e2 = e->simplify();
assert(e2->type() == Expression::Type::Cosine);
}
{
Expression * e = Expression::parse("tan(0)");
Expression * e2 = e->simplify();
assert(e2->type() == Expression::Type::Tangent);
}
}