Add the ability to match on values.

Change-Id: Id38aad5b64913c948966c980fb4415749b2c435d
This commit is contained in:
Felix Raimundo
2016-04-01 11:58:31 +02:00
parent b0c44dc39e
commit 6238b5a110
4 changed files with 38 additions and 3 deletions

View File

@@ -5,7 +5,7 @@
class Symbol : public LeafExpression {
public:
Symbol(char * name);
Symbol(const char * name);
~Symbol();
ExpressionLayout * createLayout() override;
float approximate(Context& context) override;

View File

@@ -2,6 +2,7 @@
#include <poincare/addition.h>
#include <poincare/integer.h>
#include <poincare/product.h>
#include <poincare/symbol.h>
extern "C" {
#include <assert.h>
}
@@ -38,6 +39,9 @@ Expression * ExpressionBuilder::build(ExpressionMatch matches[]) {
case Expression::Type::Integer:
result = new Integer(m_integerValue);
break;
case Expression::Type::Symbol:
result = new Symbol(m_symbolName);
break;
default:
assert(false);
break;

View File

@@ -1,8 +1,11 @@
#include "expression_selector.h"
extern "C" {
#include <assert.h>
}
#include <poincare/integer.h>
#include <poincare/symbol.h>
#include "expression_selector.h"
int ExpressionSelector::match(Expression * e, ExpressionMatch * matches) {
int numberOfMatches = 0;
@@ -16,6 +19,34 @@ int ExpressionSelector::match(Expression * e, ExpressionMatch * matches) {
return 0;
}
break;
case ExpressionSelector::Match::TypeAndValue:
if (e->type() != m_expressionType) {
return 0;
}
switch(e->type()) {
case Expression::Type::Integer:
{
Integer integer = Integer(m_integerValue);
if (!e->valueEquals(&integer)) {
return 0;
}
}
break;
case Expression::Type::Symbol:
{
Symbol symbol = Symbol(m_symbolName);
if (!e->valueEquals(&symbol)) {
return 0;
}
}
break;
default:
// Symbol and Integer are the only expressions which should be matched
// with a value.
assert(false);
break;
}
break;
case ExpressionSelector::Match::Wildcard:
/* This should not happen as a wildcard should be matched _before_ */
assert(false);

View File

@@ -6,7 +6,7 @@ extern "C" {
#include <assert.h>
}
Symbol::Symbol(char * name) {
Symbol::Symbol(const char * name) {
size_t length = strlen(name);
m_name = (char *)malloc(sizeof(char)*length+1);
memcpy(m_name, name, length);