mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
Add the ability to match on values.
Change-Id: Id38aad5b64913c948966c980fb4415749b2c435d
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user