[poincare] Transform is now just a function pointer

This commit is contained in:
Romain Goyet
2017-09-23 19:38:46 +02:00
parent d80f2f964b
commit e308fdb488
19 changed files with 49 additions and 119 deletions

View File

@@ -18,7 +18,9 @@ objs += $(addprefix $(prefix)/,\
selector/combination.o \
selector/selector.o \
selector/type_selector.o \
transform/fraction_transform.o \
transform/subtraction_transform.o \
transform/integer_addition_transform.o \
transform/merge_dynamic_hierarchy_transform.o \
transform/subtraction_transform.o \
)

View File

@@ -4,7 +4,7 @@ DemoRuleset
Subtraction.a -> SubtractionTransform(a);
// a/b -> a*b^-1
//Division.a -> ReplaceDivisionTransform(a);
Fraction.a -> FractionTransform(a);
// (a+b)+c -> a+b+c
Addition.a(Addition.b) -> MergeDynamicHierarchyTransform(a,b);

View File

@@ -2,7 +2,6 @@
#include "../expression_debug.h"
#include <iostream>
#include <poincare/static_hierarchy.h>
#include "transform/integer_addition_transform.h"
#include "demo_ruleset.h"
namespace Poincare {

View File

@@ -22,7 +22,7 @@ bool Rule::apply(Expression * e) const {
bool Rule::immediateApply(Expression * e) const {
Expression * m[5]; // En fait, 5 est un upper-bound très facilement calculable par notre compilateur de regle. C'est le max du nombre de capture de toutes les règles. Oui, on pourrait faire un truc dynamique qui n'alloue que ce dont le selecteur actuel a besoin, mais bon...
if (m_selector->match(e, m)) {
m_transform->apply(m);
m_transform(m);
return true;
}
return false;

View File

@@ -9,14 +9,14 @@ namespace Simplification {
class Rule {
public:
constexpr Rule(const Selector * s, const Transform * t) :
constexpr Rule(const Selector * s, Transform t) :
m_selector(s), m_transform(t) {
};
bool apply(Expression * e) const;
private:
bool immediateApply(Expression * e) const;
const Selector * m_selector;
const Transform * m_transform;
Transform m_transform;
};
}

View File

@@ -50,10 +50,6 @@ void Node::generateSelector(Rule * rule) {
std::cout << ");" << std::endl;
}
void Node::generateTransform() {
std::cout << "constexpr " << *m_name << " transform;" << std::endl;
}
int Node::indexOfChildrenWithIdentifier(std::string identifier) {
for (int i=0; i<m_children->size(); i++) {
if (*(m_children->at(i)->m_identifier) == identifier) {

View File

@@ -18,9 +18,9 @@ public:
void identifyAnonymousChildren(int * index);
void generateSelector(Rule * rule);
void generateTransform();
int indexOfChildrenWithIdentifier(std::string identifier);
std::string identifier();
std::string name() { return *m_name; }
private:
int selectorCaptureIndexInRule(Rule * rule);
int selectorIndexInRule(Rule * rule);

View File

@@ -6,10 +6,9 @@ void Rule::generate(int index) {
int selectorIndex = 0;
m_selector->identifyAnonymousChildren(&selectorIndex);
m_selector->generateSelector(this);
m_transform->generateTransform();
std::cout << "constexpr Rule rule(&"
<< m_selector->identifier()
<< ", &transform);" << std::endl;
<< m_selector->identifier() << ", "
<< m_transform->name() << ");" << std::endl;
std::cout << "}" << std::endl;
}

View File

@@ -108,6 +108,7 @@ int main(void) {
yyparse(&result);
std::cout << "#include \"ruleset.h\"" << std::endl << std::endl;
std::cout << "#include \"transform/transform.h\"" << std::endl << std::endl;
std::cout << "namespace Poincare {" << std::endl;
std::cout << "namespace Simplification {" << std::endl << std::endl;

View File

@@ -3,9 +3,6 @@
#include "rule.h"
#include "selector/type_selector.h"
#include "transform/subtraction_transform.h"
#include "transform/integer_addition_transform.h"
#include "transform/merge_dynamic_hierarchy_transform.h"
namespace Poincare {
namespace Simplification {

View File

@@ -0,0 +1,25 @@
#include "transform.h"
#include <assert.h>
#include <poincare/addition.h>
#include <poincare/fraction.h>
#include <poincare/integer.h>
#include <poincare/multiplication.h>
#include <poincare/power.h>
void Poincare::Simplification::FractionTransform(Expression * captures[]) {
assert(captures[0]->type() == Expression::Type::Fraction);
Fraction * d = static_cast<Fraction *>(captures[0]);
assert(d->numberOfOperands() == 2);
const Integer * minusOne = new Integer(-1);
const Expression * powerOperands[2] = {d->operand(1), minusOne};
const Power * p = new Power(powerOperands, false);
const Expression * multiplicationOperands[2] = {d->operand(0), p};
Multiplication * m = new Multiplication(multiplicationOperands, 2, false);
static_cast<Hierarchy *>(d->parent())->replaceOperand(d, m, false);
d->detachOperands();
delete d;
}

View File

@@ -1,13 +1,10 @@
#include "integer_addition_transform.h"
#include "transform.h"
#include <assert.h>
#include <poincare/addition.h>
#include <poincare/integer.h>
#include <utility>
namespace Poincare {
namespace Simplification {
void IntegerAdditionTransform::apply(Expression * captures[]) const {
void Poincare::Simplification::IntegerAdditionTransform(Expression * captures[]) {
assert(captures[0]->type() == Expression::Type::Addition);
assert(captures[1]->type() == Expression::Type::Integer);
assert(captures[2]->type() == Expression::Type::Integer);
@@ -29,6 +26,3 @@ void IntegerAdditionTransform::apply(Expression * captures[]) const {
a->removeOperand(i2);
}
}
}
}

View File

@@ -1,18 +0,0 @@
#ifndef POINCARE_SIMPLIFICATION_TRANSFORM_INTEGER_ADDITION_TRANSFORM_H
#define POINCARE_SIMPLIFICATION_TRANSFORM_INTEGER_ADDITION_TRANSFORM_H
#include "transform.h"
namespace Poincare {
namespace Simplification {
class IntegerAdditionTransform : public Transform {
public:
constexpr IntegerAdditionTransform() {};
void apply(Expression * captures[]) const override;
};
}
}
#endif

View File

@@ -1,17 +0,0 @@
#ifndef POINCARE_SIMPLIFICATION_INTEGER_PAIR_TRANSFORM_H
#define POINCARE_SIMPLIFICATION_INTEGER_PAIR_TRANSFORM_H
#include "transform.h"
namespace Poincare {
class IntegerPairTransform : public Transform {
public:
constexpr IntegerPairTransform() {};
void apply(Expression * root, Expression * captures[]) const override;
virtual Integer process(const Integer & a, const Integer & b) const = 0;
};
}
#endif

View File

@@ -1,11 +1,8 @@
#include "merge_dynamic_hierarchy_transform.h"
#include "transform.h"
#include <assert.h>
#include <poincare/dynamic_hierarchy.h>
namespace Poincare {
namespace Simplification {
void MergeDynamicHierarchyTransform::apply(Expression * captures[]) const {
void Poincare::Simplification::MergeDynamicHierarchyTransform(Expression * captures[]) {
DynamicHierarchy * h0 = static_cast<DynamicHierarchy *>(captures[0]);
DynamicHierarchy * h1 = static_cast<DynamicHierarchy *>(captures[1]);
h0->removeOperand(h1, false);
@@ -13,6 +10,3 @@ void MergeDynamicHierarchyTransform::apply(Expression * captures[]) const {
h1->detachOperands();
delete h1;
}
}
}

View File

@@ -1,21 +0,0 @@
#ifndef POINCARE_SIMPLIFICATION_TRANSFORM_MERGE_HIERARCHY_TRANSFORM_H
#define POINCARE_SIMPLIFICATION_TRANSFORM_MERGE_HIERARCHY_TRANSFORM_H
#include "transform.h"
namespace Poincare {
namespace Simplification {
// This transform expects two DynamicHierarchy in its captures.
// It will remove the second one, and put all its operands in the first.
class MergeDynamicHierarchyTransform : public Transform {
public:
constexpr MergeDynamicHierarchyTransform() {};
void apply(Expression * captures[]) const override;
};
}
}
#endif

View File

@@ -1,14 +1,11 @@
#include "subtraction_transform.h"
#include "transform.h"
#include <assert.h>
#include <poincare/addition.h>
#include <poincare/integer.h>
#include <poincare/multiplication.h>
#include <poincare/subtraction.h>
namespace Poincare {
namespace Simplification {
void SubtractionTransform::apply(Expression * captures[]) const {
void Poincare::Simplification::SubtractionTransform(Expression * captures[]) {
assert(captures[0]->type() == Expression::Type::Subtraction);
Subtraction * s = static_cast<Subtraction *>(captures[0]);
@@ -26,6 +23,3 @@ void SubtractionTransform::apply(Expression * captures[]) const {
s->detachOperands();
delete s;
}
}
}

View File

@@ -1,18 +0,0 @@
#ifndef POINCARE_SIMPLIFICATION_TRANSFORM_SUBTRACTION_TRANSFORM_H
#define POINCARE_SIMPLIFICATION_TRANSFORM_SUBTRACTION_TRANSFORM_H
#include "transform.h"
namespace Poincare {
namespace Simplification {
class SubtractionTransform : public Transform {
public:
constexpr SubtractionTransform() {};
void apply(Expression * captures[]) const override;
};
}
}
#endif

View File

@@ -6,10 +6,13 @@
namespace Poincare {
namespace Simplification {
class Transform {
public:
virtual void apply(Expression * captures[]) const = 0;
};
using Transform = void (*)(Expression * captures[]);
void SubtractionTransform(Expression * captures[]);
void FractionTransform(Expression * captures[]);
void MergeDynamicHierarchyTransform(Expression * captures[]);
void IntegerAdditionTransform(Expression * captures[]);
void PowerPowerTransform(Expression * captures[]);
}
}