From cbc26a8508a1abcffc3ffe0e329f59e1b64c72b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Mon, 13 Aug 2018 17:57:53 +0200 Subject: [PATCH] [poincare] ExceptionNode and ExceptionExpressionNode --- poincare/Makefile | 1 + .../poincare/allocation_failure_node.h | 7 ++- .../poincare/exception_expression_node.h | 51 +++++++++++++++++++ poincare/include/poincare/exception_node.h | 29 +++++++++++ poincare/include/poincare/expression_node.h | 7 +-- poincare/include/poincare/tree_node.h | 3 ++ poincare/src/exception_expression_node.cpp | 11 ++++ 7 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 poincare/include/poincare/exception_expression_node.h create mode 100644 poincare/include/poincare/exception_node.h create mode 100644 poincare/src/exception_expression_node.cpp diff --git a/poincare/Makefile b/poincare/Makefile index f4e193741..d485433ae 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -36,6 +36,7 @@ objs += $(addprefix poincare/src/,\ objs += $(addprefix poincare/src/,\ addition.o\ arithmetic.o\ + exception_expression_node.o\ expression_lexer.o\ expression_parser.o\ layout_helper.o\ diff --git a/poincare/include/poincare/allocation_failure_node.h b/poincare/include/poincare/allocation_failure_node.h index 44d7c2d7d..77d861f07 100644 --- a/poincare/include/poincare/allocation_failure_node.h +++ b/poincare/include/poincare/allocation_failure_node.h @@ -2,18 +2,17 @@ #define POINCARE_ALLOCATION_FAILURE_NODE_H #include +#include namespace Poincare { template -class AllocationFailureNode : public T { +class AllocationFailureNode : public ExceptionNode { public: - AllocationFailureNode() : T() {} - AllocationFailureNode(T node) : T(node) {} // TreeNode size_t size() const override { return sizeof(AllocationFailureNode); } - int numberOfChildren() const override { return 0; } + bool isException() const override { return false; } //TODO ? bool isAllocationFailure() const override { return true; } #if POINCARE_TREE_LOG virtual void logNodeName(std::ostream & stream) const override { diff --git a/poincare/include/poincare/exception_expression_node.h b/poincare/include/poincare/exception_expression_node.h new file mode 100644 index 000000000..d0d932803 --- /dev/null +++ b/poincare/include/poincare/exception_expression_node.h @@ -0,0 +1,51 @@ +#ifndef POINCARE_EXCEPTION_EXPRESSION_NODE_H +#define POINCARE_EXCEPTION_EXPRESSION_NODE_H + +#include +#include +#include +#include +#include +#include + +namespace Poincare { + +/* All exceptions should be caught so ExceptionExpressionNode's methods are + * asserted false. */ +class ExceptionExpressionNode : public ExceptionNode { +public: + // ExpressionNode + ExpressionNode::Sign sign() const override { assert(false); return ExpressionNode::Sign::Unknown; } + ExpressionNode::Type type() const override { assert(false); return ExpressionNode::Type::Exception; } // TODO assert(false) ? + Evaluation approximate(ExpressionNode::SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { assert(false); return Complex::Undefined(); } + Evaluation approximate(ExpressionNode::DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { assert(false); return Complex::Undefined(); } + int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode = Preferences::PrintFloatMode::Decimal, int numberOfSignificantDigits = 0) const override { + assert(false); + if (bufferSize == 0) { + return -1; + } + return PrintFloat::convertFloatToText(NAN, buffer, bufferSize, numberOfSignificantDigits, floatDisplayMode); + } + LayoutRef createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override { assert(false); return LayoutRef(CharLayoutNode::FailedAllocationStaticNode()); } + // FIXME: Use EmptyLayoutNode here above, once EmptyLayout has been cleaned up + + // TreeNode + TreeNode * failedAllocationStaticNode() override { /*TODO*/ assert(false); return nullptr; } + size_t size() const override { return sizeof(ExceptionExpressionNode); } +#if POINCARE_TREE_LOG + virtual void logNodeName(std::ostream & stream) const override { + stream << "ExceptionExpression"; + } +#endif +}; + +class ExceptionExpression : public Expression { +public: + ExceptionExpression() : Expression(ExceptionExpressionStaticNode()) {} +private: + static ExceptionExpressionNode * ExceptionExpressionStaticNode(); +}; + +} + +#endif diff --git a/poincare/include/poincare/exception_node.h b/poincare/include/poincare/exception_node.h new file mode 100644 index 000000000..37fd9764b --- /dev/null +++ b/poincare/include/poincare/exception_node.h @@ -0,0 +1,29 @@ +#ifndef POINCARE_EXCEPTION_NODE_H +#define POINCARE_EXCEPTION_NODE_H + +#include + +namespace Poincare { + +template +class ExceptionNode : public T { +public: + ExceptionNode() : T() {} + ExceptionNode(T node) : T(node) {} + + // TreeNode + size_t size() const override { return sizeof(ExceptionNode); } + int numberOfChildren() const override { return 0; } + bool isException() const override { return true; } +#if POINCARE_TREE_LOG + virtual void logNodeName(std::ostream & stream) const override { + stream << "Exception["; + T::logNodeName(stream); + stream << "]"; + } +#endif +}; + +} + +#endif diff --git a/poincare/include/poincare/expression_node.h b/poincare/include/poincare/expression_node.h index ac6e34c6d..40be99d0a 100644 --- a/poincare/include/poincare/expression_node.h +++ b/poincare/include/poincare/expression_node.h @@ -21,9 +21,10 @@ class ExpressionNode : public TreeNode, public SerializationHelperInterface { friend class SymbolNode; public: enum class Type : uint8_t { - AllocationFailure = 0, - Undefined = 1, - Integer = 2, + Exception = 0, + AllocationFailure = 1, + Undefined = 2, + Integer = 3, Rational, Decimal, Float, diff --git a/poincare/include/poincare/tree_node.h b/poincare/include/poincare/tree_node.h index 6aab65bb9..fb6c31377 100644 --- a/poincare/include/poincare/tree_node.h +++ b/poincare/include/poincare/tree_node.h @@ -36,6 +36,9 @@ public: m_referenceCounter = refCount; } + // Exception + virtual bool isException() const { return false; } + // Ghost virtual bool isGhost() const { return false; } diff --git a/poincare/src/exception_expression_node.cpp b/poincare/src/exception_expression_node.cpp new file mode 100644 index 000000000..31b52354a --- /dev/null +++ b/poincare/src/exception_expression_node.cpp @@ -0,0 +1,11 @@ +#include + +namespace Poincare { + +ExceptionExpressionNode * ExceptionExpression::ExceptionExpressionStaticNode() { + static ExceptionExpressionNode exception; + TreePool::sharedPool()->registerStaticNodeIfRequired(&exception); + return &exception; +} + +}