From 93d8f60619e1468d0ef2a2cb41cad12cb4fa014e Mon Sep 17 00:00:00 2001 From: Ruben Dashyan Date: Fri, 2 Nov 2018 14:30:55 +0100 Subject: [PATCH] [poincare] Derivative, Integral, Product, Sum constructors check their second parameter is of Symbol type. --- poincare/include/poincare/derivative.h | 12 ++++++++++-- poincare/include/poincare/expression.h | 2 ++ poincare/include/poincare/integral.h | 12 ++++++++++-- poincare/include/poincare/product.h | 11 +++++++++-- poincare/include/poincare/sequence.h | 1 + poincare/include/poincare/sum.h | 11 +++++++++-- poincare/src/sequence.cpp | 1 - 7 files changed, 41 insertions(+), 9 deletions(-) diff --git a/poincare/include/poincare/derivative.h b/poincare/include/poincare/derivative.h index ea25e500a..88f239bcc 100644 --- a/poincare/include/poincare/derivative.h +++ b/poincare/include/poincare/derivative.h @@ -2,6 +2,7 @@ #define POINCARE_DERIVATIVE_H #include +#include #include namespace Poincare { @@ -46,13 +47,20 @@ private: class Derivative final : public Expression { public: Derivative(const DerivativeNode * n) : Expression(n) {} - static Derivative Builder(Expression child0, Expression child1, Expression child2) { return Derivative(child0, child1, child2); } - static Expression UntypedBuilder(Expression children) { return Builder(children.childAtIndex(0), children.childAtIndex(1), children.childAtIndex(2)); } + static Derivative Builder(Expression child0, Symbol child1, Expression child2) { return Derivative(child0, child1, child2); } + static Expression UntypedBuilder(Expression children) { + if (children.childAtIndex(1).type() != ExpressionNode::Type::Symbol) { + // Second parameter must be a Symbol. + return Expression(); + } + return Builder(children.childAtIndex(0), children.childAtIndex(1).convert(), children.childAtIndex(2)); + } static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("diff", 3, &UntypedBuilder); Expression shallowReduce(Context & context, Preferences::AngleUnit angleUnit, bool replaceSymbols = true); private: Derivative(Expression child0, Expression child1, Expression child2) : Expression(TreePool::sharedPool()->createTreeNode()) { + assert(child1.type() == ExpressionNode::Type::Symbol); replaceChildAtIndexInPlace(0, child0); replaceChildAtIndexInPlace(1, child1); replaceChildAtIndexInPlace(2, child2); diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index aeafa1d87..6bd0e22ee 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -62,12 +62,14 @@ class Expression : public TreeHandle { friend class PermuteCoefficient; friend class Power; friend class PredictionInterval; + friend class Product; friend class RealPart; friend class Round; friend class Sine; friend class SquareRoot; friend class Store; friend class Subtraction; + friend class Sum; friend class Symbol; friend class Tangent; friend class Trigonometry; diff --git a/poincare/include/poincare/integral.h b/poincare/include/poincare/integral.h index 6c14256f8..621065698 100644 --- a/poincare/include/poincare/integral.h +++ b/poincare/include/poincare/integral.h @@ -2,6 +2,7 @@ #define POINCARE_INTEGRAL_H #include +#include namespace Poincare { @@ -49,14 +50,21 @@ private: class Integral final : public Expression { public: Integral(const IntegralNode * n) : Expression(n) {} - static Integral Builder(Expression child0, Expression child1, Expression child2, Expression child3) { return Integral(child0, child1, child2, child3); } - static Expression UntypedBuilder(Expression children) { return Builder(children.childAtIndex(0), children.childAtIndex(1), children.childAtIndex(2), children.childAtIndex(3)); } + static Integral Builder(Expression child0, Symbol child1, Expression child2, Expression child3) { return Integral(child0, child1, child2, child3); } + static Expression UntypedBuilder(Expression children) { + if (children.childAtIndex(1).type() != ExpressionNode::Type::Symbol) { + // Second parameter must be a Symbol. + return Expression(); + } + return Builder(children.childAtIndex(0), children.childAtIndex(1).convert(), children.childAtIndex(2), children.childAtIndex(3)); + } static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("int", 4, &UntypedBuilder); // Expression Expression shallowReduce(Context & context, Preferences::AngleUnit angleUnit, bool replaceSymbols = true); private: Integral(Expression child0, Expression child1, Expression child2, Expression child3) : Expression(TreePool::sharedPool()->createTreeNode()) { + assert(child1.type() == ExpressionNode::Type::Symbol); replaceChildAtIndexInPlace(0, child0); replaceChildAtIndexInPlace(1, child1); replaceChildAtIndexInPlace(2, child2); diff --git a/poincare/include/poincare/product.h b/poincare/include/poincare/product.h index 014060097..76e2fe4d2 100644 --- a/poincare/include/poincare/product.h +++ b/poincare/include/poincare/product.h @@ -33,11 +33,18 @@ class Product final : public Expression { friend class ProductNode; public: Product(const ProductNode * n) : Expression(n) {} - static Product Builder(Expression child0, Expression child1, Expression child2, Expression child3) { return Product(child0, child1, child2, child3); } - static Expression UntypedBuilder(Expression children) { return Builder(children.childAtIndex(0), children.childAtIndex(1), children.childAtIndex(2), children.childAtIndex(3)); } + static Product Builder(Expression child0, Symbol child1, Expression child2, Expression child3) { return Product(child0, child1, child2, child3); } + static Expression UntypedBuilder(Expression children) { + if (children.childAtIndex(1).type() != ExpressionNode::Type::Symbol) { + // Second parameter must be a Symbol. + return Expression(); + } + return Builder(children.childAtIndex(0), children.childAtIndex(1).convert(), children.childAtIndex(2), children.childAtIndex(3)); + } static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("product", 4, &UntypedBuilder); private: Product(Expression child0, Expression child1, Expression child2, Expression child3) : Expression(TreePool::sharedPool()->createTreeNode()) { + assert(child1.type() == ExpressionNode::Type::Symbol); replaceChildAtIndexInPlace(0, child0); replaceChildAtIndexInPlace(1, child1); replaceChildAtIndexInPlace(2, child2); diff --git a/poincare/include/poincare/sequence.h b/poincare/include/poincare/sequence.h index adcb454a8..36fe8eb7c 100644 --- a/poincare/include/poincare/sequence.h +++ b/poincare/include/poincare/sequence.h @@ -2,6 +2,7 @@ #define POINCARE_SEQUENCE_H #include +#include #include namespace Poincare { diff --git a/poincare/include/poincare/sum.h b/poincare/include/poincare/sum.h index fe10e1ab6..e39cffcd3 100644 --- a/poincare/include/poincare/sum.h +++ b/poincare/include/poincare/sum.h @@ -33,11 +33,18 @@ class Sum final : public Expression { friend class SumNode; public: Sum(const SumNode * n) : Expression(n) {} - static Sum Builder(Expression child0, Expression child1, Expression child2, Expression child3) { return Sum(child0, child1, child2, child3); } - static Expression UntypedBuilder(Expression children) { return Builder(children.childAtIndex(0), children.childAtIndex(1), children.childAtIndex(2), children.childAtIndex(3)); } + static Sum Builder(Expression child0, Symbol child1, Expression child2, Expression child3) { return Sum(child0, child1, child2, child3); } + static Expression UntypedBuilder(Expression children) { + if (children.childAtIndex(1).type() != ExpressionNode::Type::Symbol) { + // Second parameter must be a Symbol. + return Expression(); + } + return Builder(children.childAtIndex(0), children.childAtIndex(1).convert(), children.childAtIndex(2), children.childAtIndex(3)); + } static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("sum", 4, &UntypedBuilder); private: Sum(Expression child0, Expression child1, Expression child2, Expression child3) : Expression(TreePool::sharedPool()->createTreeNode()) { + assert(child1.type() == ExpressionNode::Type::Symbol); replaceChildAtIndexInPlace(0, child0); replaceChildAtIndexInPlace(1, child1); replaceChildAtIndexInPlace(2, child2); diff --git a/poincare/src/sequence.cpp b/poincare/src/sequence.cpp index 8603e26a5..5e3748ac6 100644 --- a/poincare/src/sequence.cpp +++ b/poincare/src/sequence.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include extern "C" {