[poincare] Expression deletion

This commit is contained in:
Romain Goyet
2015-09-29 21:59:58 +02:00
parent 2d97d2e4c3
commit b6aded4d8f
6 changed files with 24 additions and 56 deletions

View File

@@ -6,8 +6,9 @@
class Addition : public Expression {
public:
Addition(Expression * first_operand, Expression * second_operand);
~Addition();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
virtual float approximate();
float approximate() override;
private:
Expression * m_children[2];
};

View File

@@ -7,6 +7,7 @@
class Expression {
public:
static Expression * parse(char * string);
virtual ~Expression();
//virtual Expression ** children() = 0; // NULL-terminated

View File

@@ -6,12 +6,12 @@
class Fraction : public Expression {
public:
Fraction(Expression * numerator, Expression * denominator);
// virtual Expression ** children();
// protected:
virtual ExpressionLayout * createLayout(ExpressionLayout * parent);
virtual float approximate();
~Fraction();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
float approximate() override;
private:
Expression * m_children[3];
Expression * m_numerator;
Expression * m_denominator;
};
#endif

View File

@@ -6,6 +6,11 @@ Addition::Addition(Expression * first_operand, Expression * second_operand) {
m_children[1] = second_operand;
}
Addition::~Addition() {
delete m_children[1];
delete m_children[0];
}
float Addition::approximate() {
return m_children[0]->approximate() + m_children[1]->approximate();
}

View File

@@ -12,13 +12,15 @@ Expression * Expression::parse(char * string) {
Expression * expression = 0;
poincare_expression_yyparse(scanner, &expression);
poincare_expression_yy_delete_buffer(buf, scanner);
poincare_expression_yylex_destroy(scanner);
return expression;
}
Expression::~Expression() {
}
/*
bool Expression::identicalTo(Expression * e) {
// By default, two expression are not identical.

View File

@@ -2,62 +2,21 @@
#include <string.h>
#include "layout/fraction_layout.h"
/*
static inline KDCoordinate max(KDCoordinate a, KDCoordinate b) {
return (a > b ? a : b);
Fraction::Fraction(Expression * numerator, Expression * denominator) :
m_numerator(numerator),
m_denominator(denominator) {
}
*/
#define NUMERATOR m_children[0]
#define DENOMINATOR m_children[1]
#define FRACTION_BORDER_LENGTH 2
#define FRACTION_LINE_MARGIN 2
#define FRACTION_LINE_HEIGHT 1
Fraction::Fraction(Expression * numerator, Expression * denominator) {
m_children[0] = numerator;
m_children[1] = denominator;
m_children[2] = NULL;
Fraction::~Fraction() {
delete m_denominator;
delete m_numerator;
}
/*
Expression ** Fraction::children() {
return m_children;
}
*/
ExpressionLayout * Fraction::createLayout(ExpressionLayout * parent) {
return new FractionLayout(parent, NUMERATOR, DENOMINATOR);
return new FractionLayout(parent, m_numerator, m_denominator);
}
/*
void Fraction::layout() {
KDRect numFrame = NUMERATOR->m_frame;
KDRect denFrame = DENOMINATOR->m_frame;
m_frame.width = max(numFrame.width, denFrame.width) + 2*FRACTION_BORDER_LENGTH;
m_frame.height = numFrame.height + FRACTION_LINE_MARGIN + FRACTION_LINE_HEIGHT + FRACTION_LINE_MARGIN + denFrame.height;
NUMERATOR->m_frame.origin = {
.x = (KDCoordinate)((m_frame.width - numFrame.width)/2),
.y = 0
};
DENOMINATOR->m_frame.origin = {
.x = (KDCoordinate)((m_frame.width - denFrame.width)/2),
.y = (KDCoordinate)(numFrame.height + 2*FRACTION_LINE_MARGIN + FRACTION_LINE_HEIGHT)
};
}
void Fraction::draw() {
KDCoordinate fractionLineY = NUMERATOR->m_frame.height + FRACTION_LINE_MARGIN;
KDDrawLine((KDPoint){.x = 0, .y = fractionLineY},
(KDPoint){.x = m_frame.width, .y = fractionLineY});
}
*/
float Fraction::approximate() {
// TODO: handle division by zero
return m_children[0]->approximate()/m_children[1]->approximate();
return m_numerator->approximate()/m_denominator->approximate();
}