Poincare: Pointer arithmetics doesn't play well with inheritance

Change-Id: I78cc57ca60bb703f00922f5257d53d6112a97f91
This commit is contained in:
Romain Goyet
2016-04-07 17:20:00 +02:00
parent b849baf0d9
commit 0ac978468a
7 changed files with 26 additions and 54 deletions

View File

@@ -9,7 +9,6 @@ $(dir)/rules.cpp: $(RULEGEN) $(dir)/rules.pr
products += $(dir)/rules.cpp
objs += $(addprefix $(dir)/,\
contiguous_tree.o\
expression_builder.o\
expression_match.o\
expression_selector.o\

View File

@@ -1,14 +0,0 @@
#include "contiguous_tree.h"
extern "C" {
#include <assert.h>
}
ContiguousTree * ContiguousTree::child(uint8_t index) {
assert(index>=0 && index<m_numberOfChildren);
if (index == 0) {
return (this+1); // Pointer arithmetics
} else {
ContiguousTree * previousChild = this->child(index-1);
return previousChild+previousChild->m_numberOfChildren+1; // Pointer arithm.
}
}

View File

@@ -1,20 +0,0 @@
#ifndef POINCARE_SIMPLIFY_CONTIGUOUS_TREE_H
#define POINCARE_SIMPLIFY_CONTIGUOUS_TREE_H
extern "C" {
#include <stdint.h>
}
class ContiguousTree {
public:
constexpr ContiguousTree(uint8_t numberOfChildren);
ContiguousTree * child(uint8_t i);
protected:
uint8_t m_numberOfChildren;
};
constexpr ContiguousTree::ContiguousTree(uint8_t numberOfChildren) :
m_numberOfChildren(numberOfChildren) {
}
#endif

View File

@@ -16,7 +16,7 @@ Expression * ExpressionBuilder::build(ExpressionMatch matches[]) {
int numberOfChildrenExpressions = 0;
for (int i=0; i<m_numberOfChildren; i++) {
ExpressionBuilder * child = (ExpressionBuilder *)this->child(i);
ExpressionBuilder * child = this->child(i);
if (child->m_action == ExpressionBuilder::Action::BringUpWildcard) {
for (int j=0; j<matches[child->m_matchIndex].numberOfExpressions(); j++) {
children_expressions[numberOfChildrenExpressions++] =
@@ -68,3 +68,13 @@ Expression * ExpressionBuilder::build(ExpressionMatch matches[]) {
}
return result;
}
ExpressionBuilder * ExpressionBuilder::child(int index) {
assert(index>=0 && index<m_numberOfChildren);
if (index == 0) {
return (this+1); // Pointer arithmetics
} else {
ExpressionBuilder * previousChild = this->child(index-1);
return previousChild+previousChild->m_numberOfChildren+1; // Pointer arithm.
}
}

View File

@@ -2,14 +2,12 @@
#define POINCARE_SIMPLIFY_EXPRESSION_BUILDER_H
#include <poincare/expression.h>
#include "contiguous_tree.h"
#include "expression_match.h"
extern "C" {
#include <stdint.h>
}
class ExpressionBuilder : public ContiguousTree {
class ExpressionBuilder {
public:
static constexpr ExpressionBuilder BuildFromType(Expression::Type type, uint8_t numberOfChildren);
static constexpr ExpressionBuilder BuildFromTypeAndValue(Expression::Type type, int32_t value, uint8_t numberOfChildren);
@@ -32,9 +30,9 @@ private:
constexpr ExpressionBuilder(Expression::Type type, int32_t integerValue, uint8_t numberOfChildren);
constexpr ExpressionBuilder(Action action, uint8_t matchIndex, uint8_t numberOfChildren);
constexpr ExpressionBuilder(ExternalGenerator * generator, uint8_t numberOfChildren);
ExpressionBuilder * child(int index);
Action m_action;
union {
// m_action == BuildFromType and BuildFromTypeAndValue
struct {
@@ -51,6 +49,7 @@ private:
// m_action == CallExternalGenerator
ExternalGenerator * m_generator;
};
uint8_t m_numberOfChildren;
};
/* Since they have to be evaluated at compile time, constexpr functions are
@@ -91,27 +90,27 @@ constexpr ExpressionBuilder::ExpressionBuilder(Expression::Type type,
int32_t integerValue,
uint8_t numberOfChildren)
:
ContiguousTree(numberOfChildren),
m_action(ExpressionBuilder::Action::BuildFromTypeAndValue),
m_expressionType(type),
m_integerValue(integerValue) {
m_integerValue(integerValue),
m_numberOfChildren(numberOfChildren) {
}
constexpr ExpressionBuilder::ExpressionBuilder(Action action,
uint8_t matchIndex,
uint8_t numberOfChildren)
:
ContiguousTree(numberOfChildren),
m_action(action),
m_matchIndex(matchIndex) {
m_matchIndex(matchIndex),
m_numberOfChildren(numberOfChildren) {
}
constexpr ExpressionBuilder::ExpressionBuilder(ExternalGenerator * generator,
uint8_t numberOfChildren)
:
ContiguousTree(numberOfChildren),
m_action(ExpressionBuilder::Action::CallExternalGenerator),
m_generator(generator) {
m_generator(generator),
m_numberOfChildren(numberOfChildren) {
}
#endif

View File

@@ -69,7 +69,7 @@ int ExpressionSelector::match(Expression * e, ExpressionMatch * matches) {
matches[numberOfMatches++] = ExpressionMatch(&e, 1);
if (this->m_numberOfChildren != 0) {
if (m_numberOfChildren != 0) {
int numberOfChildMatches = 0;
if (!e->isCommutative()) {
numberOfChildMatches = sequentialMatch(e, matches+numberOfMatches);
@@ -292,8 +292,6 @@ bool ExpressionSelector::canCommutativelyMatch(Expression * e, ExpressionMatch *
return false;
}
// Extrude in a class impossible otherwise ExpressionBuilder is not aggregate
// and cannot be initialized statically
ExpressionSelector * ExpressionSelector::child(int index) {
assert(index>=0 && index<m_numberOfChildren);
if (index == 0) {

View File

@@ -2,13 +2,12 @@
#define POINCARE_SIMPLIFY_EXPRESSION_SELECTOR_H
#include <poincare/expression.h>
#include "contiguous_tree.h"
#include "expression_match.h"
extern "C" {
#include <stdint.h>
}
class ExpressionSelector : public ContiguousTree {
class ExpressionSelector {
public:
static constexpr ExpressionSelector Any(uint8_t numberOfChildren) {
return ExpressionSelector(Match::Any, (Expression::Type)0, 0, numberOfChildren);
@@ -23,7 +22,6 @@ public:
return ExpressionSelector(Match::TypeAndValue, type, value, numberOfChildren);
}
ExpressionSelector * child(int i);
/* The match function is interesting
* - It returns 0 if the selector didn't match the expression
* - Otherwise, it returns the number of captured matches and sets *matches
@@ -44,10 +42,10 @@ private:
int32_t integerValue,
uint8_t numberOfChildren)
:
ContiguousTree(numberOfChildren),
m_match(match),
m_expressionType(type),
m_integerValue(integerValue)
m_integerValue(integerValue),
m_numberOfChildren(numberOfChildren)
{
}
@@ -56,6 +54,7 @@ private:
uint8_t * selectorMatched, int leftToMatch);
int commutativeMatch(Expression * e, ExpressionMatch * matches);
int sequentialMatch(Expression * e, ExpressionMatch * matches);
ExpressionSelector * child(int index);
Match m_match;
union {
@@ -72,6 +71,7 @@ private:
};
};
};
uint8_t m_numberOfChildren;
};
#endif