Poincare: Clean the Expression::Type

Change-Id: I3809a8b1b040314466554866555fb634c35156a5
This commit is contained in:
Romain Goyet
2016-03-24 10:19:04 +01:00
parent 2f77d70369
commit dfdaa54928
19 changed files with 39 additions and 43 deletions

View File

@@ -5,12 +5,11 @@
class Addition : public Expression {
public:
static const expression_type_t Type = 0x00;
Addition(Expression * first_operand, Expression * second_operand);
~Addition();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
float approximate(Context& context) override;
expression_type_t type() override;
Type type() override;
private:
Expression * m_left;
Expression * m_right;

View File

@@ -5,10 +5,19 @@
#include <kandinsky.h>
class Context;
typedef uint8_t expression_type_t;
class Expression {
public:
enum class Type : uint8_t {
Addition,
Float,
Fraction,
Integer,
Power,
Product,
Subtraction,
Symbol
};
static Expression * parse(char * string);
virtual ~Expression();
@@ -22,7 +31,7 @@ class Expression {
//virtual bool identicalTo(Expression * e);
Expression * simplify();
virtual expression_type_t type() = 0;
virtual Type type() = 0;
virtual float approximate(Context& context) = 0;
/*private:

View File

@@ -5,13 +5,12 @@
class Float : public Expression {
public:
static const expression_type_t Type = 0x03;
Float(float f);
~Float();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
float approximate(Context& context) override;
expression_type_t type() override;
Type type() override;
private:
float m_float;
};

View File

@@ -5,12 +5,11 @@
class Fraction : public Expression {
public:
static const expression_type_t Type = 0x02;
Fraction(Expression * numerator, Expression * denominator);
~Fraction();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
float approximate(Context& context) override;
expression_type_t type() override;
Type type() override;
private:
Expression * m_numerator;
Expression * m_denominator;

View File

@@ -10,11 +10,10 @@ typedef uint64_t double_native_uint_t;
class Integer : public Expression {
public:
static const expression_type_t Type = 0x01;
Integer(native_int_t i);
Integer(Integer&& other); // C++11 move constructor
Integer(const char * string); // NULL-terminated
expression_type_t type() override;
Type type() override;
~Integer();

View File

@@ -5,13 +5,11 @@
class Power : public Expression {
public:
static const expression_type_t Type = 0x04;
Power(Expression * base, Expression * exponent);
~Power();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
float approximate(Context& context) override;
expression_type_t type() override;
Type type() override;
private:
Expression * m_base;
Expression * m_exponent;

View File

@@ -5,12 +5,11 @@
class Product : public Expression {
public:
static const expression_type_t Type = 0x05;
Product(Expression * first_factor, Expression * second_factor);
~Product();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
float approximate(Context& context) override;
expression_type_t type() override;
Type type() override;
Expression * factor(int i);
int numberOfFactors();

View File

@@ -5,12 +5,11 @@
class Subtraction : public Expression {
public:
static const expression_type_t Type = 0x06;
Subtraction(Expression * first_operand, Expression * second_operand);
~Subtraction();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
float approximate(Context& context) override;
expression_type_t type() override;
Type type() override;
private:
Expression * m_left;
Expression * m_right;

View File

@@ -5,12 +5,11 @@
class Symbol : public Expression {
public:
static const expression_type_t Type = 0x07;
Symbol(char * name);
~Symbol();
ExpressionLayout * createLayout(ExpressionLayout * parent) override;
float approximate(Context& context) override;
expression_type_t type() override;
Type type() override;
private:
char * m_name;
};

View File

@@ -6,8 +6,8 @@ Addition::Addition(Expression * first_operand, Expression * second_operand) {
m_right = second_operand;
}
expression_type_t Addition::type() {
return Addition::Type;
Expression::Type Addition::type() {
return Expression::Type::Addition;
}
Addition::~Addition() {

View File

@@ -11,11 +11,10 @@ float Float::approximate(Context& context) {
return m_float;
}
expression_type_t Float::type() {
return Float::Type;
Expression::Type Float::type() {
return Expression::Type::Float;
}
ExpressionLayout * Float::createLayout(ExpressionLayout * parent) {
assert(0); // Should not come here, ever...
return nullptr;

View File

@@ -21,7 +21,6 @@ float Fraction::approximate(Context& context) {
return m_numerator->approximate(context)/m_denominator->approximate(context);
}
expression_type_t Fraction::type() {
return Fraction::Type;
Expression::Type Fraction::type() {
return Expression::Type::Fraction;
}

View File

@@ -305,8 +305,8 @@ float Integer::approximate(Context& context) {
return float_result;
}
expression_type_t Integer::type() {
return Integer::Type;
Expression::Type Integer::type() {
return Expression::Type::Integer;
}
ExpressionLayout * Integer::createLayout(ExpressionLayout * parent) {

View File

@@ -16,11 +16,10 @@ float Power::approximate(Context& context) {
return powf(m_base->approximate(context), m_exponent->approximate(context));
}
expression_type_t Power::type() {
return Power::Type;
Expression::Type Power::type() {
return Expression::Type::Power;
}
ExpressionLayout * Power::createLayout(ExpressionLayout * parent) {
return new ExponentLayout(parent, m_base, m_exponent);
}

View File

@@ -33,8 +33,8 @@ Expression * Product::factor(int i) {
return m_factors[i];
}
expression_type_t Product::type() {
return Product::Type;
Expression::Type Product::type() {
return Expression::Type::Product;
}
ExpressionLayout * Product::createLayout(ExpressionLayout * parent) {

View File

@@ -3,13 +3,13 @@
#include <poincare/integer.h>
Expression * SimplifierZero(Expression * e) {
if (e->type() != Product::Type) {
if (e->type() != Expression::Type::Product) {
return nullptr;
}
Product * p = (Product *)e;
for (int i=0; i<p->numberOfFactors(); i++) {
Expression * factor = p->factor(i);
if (factor->type() == Integer::Type) {
if (factor->type() == Expression::Type::Integer) {
Integer * integer = (Integer *)factor;
if (*integer == Integer((native_int_t)0)) {
return new Integer("0");

View File

@@ -15,11 +15,10 @@ float Subtraction::approximate(Context& context) {
return m_left->approximate(context) - m_right->approximate(context);
}
expression_type_t Subtraction::type() {
return Subtraction::Type;
Expression::Type Subtraction::type() {
return Expression::Type::Subtraction;
}
ExpressionLayout * Subtraction::createLayout(ExpressionLayout * parent) {
return new HorizontalLayout(parent, m_left, '-', m_right);
}

View File

@@ -18,8 +18,8 @@ float Symbol::approximate(Context& context) {
return context[m_name]->approximate(context);
}
expression_type_t Symbol::type() {
return Symbol::Type;
Expression::Type Symbol::type() {
return Expression::Type::Symbol;
}
ExpressionLayout * Symbol::createLayout(ExpressionLayout * parent) {

View File

@@ -6,11 +6,11 @@ QUIZ_CASE(poincare_simplify) {
{
Expression * e = Expression::parse((char *)"3*0");
Expression * e2 = e->simplify();
assert(e2->type() == Integer::Type);
assert(e2->type() == Expression::Type::Integer);
}
{
Expression * e = Expression::parse((char *)"0*foo");
Expression * e2 = e->simplify();
assert(e2->type() == Integer::Type);
assert(e2->type() == Expression::Type::Integer);
}
}