[poincare] ExceptionNode and ExceptionExpressionNode

This commit is contained in:
Léa Saviot
2018-08-13 17:57:53 +02:00
parent fcaa32c753
commit cbc26a8508
7 changed files with 102 additions and 7 deletions

View File

@@ -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\

View File

@@ -2,18 +2,17 @@
#define POINCARE_ALLOCATION_FAILURE_NODE_H
#include <stddef.h>
#include <poincare/exception_node.h>
namespace Poincare {
template <typename T>
class AllocationFailureNode : public T {
class AllocationFailureNode : public ExceptionNode<T> {
public:
AllocationFailureNode() : T() {}
AllocationFailureNode(T node) : T(node) {}
// TreeNode
size_t size() const override { return sizeof(AllocationFailureNode<T>); }
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 {

View File

@@ -0,0 +1,51 @@
#ifndef POINCARE_EXCEPTION_EXPRESSION_NODE_H
#define POINCARE_EXCEPTION_EXPRESSION_NODE_H
#include <poincare/exception_node.h>
#include <poincare/expression_node.h>
#include <poincare/expression.h>
#include <poincare/complex.h>
#include <poincare/char_layout_node.h>
#include <stdio.h>
namespace Poincare {
/* All exceptions should be caught so ExceptionExpressionNode's methods are
* asserted false. */
class ExceptionExpressionNode : public ExceptionNode<ExpressionNode> {
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<float> approximate(ExpressionNode::SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { assert(false); return Complex<float>::Undefined(); }
Evaluation<double> approximate(ExpressionNode::DoublePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { assert(false); return Complex<double>::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<float>(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

View File

@@ -0,0 +1,29 @@
#ifndef POINCARE_EXCEPTION_NODE_H
#define POINCARE_EXCEPTION_NODE_H
#include <stddef.h>
namespace Poincare {
template <typename T>
class ExceptionNode : public T {
public:
ExceptionNode() : T() {}
ExceptionNode(T node) : T(node) {}
// TreeNode
size_t size() const override { return sizeof(ExceptionNode<T>); }
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

View File

@@ -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,

View File

@@ -36,6 +36,9 @@ public:
m_referenceCounter = refCount;
}
// Exception
virtual bool isException() const { return false; }
// Ghost
virtual bool isGhost() const { return false; }

View File

@@ -0,0 +1,11 @@
#include <poincare/exception_expression_node.h>
namespace Poincare {
ExceptionExpressionNode * ExceptionExpression::ExceptionExpressionStaticNode() {
static ExceptionExpressionNode exception;
TreePool::sharedPool()->registerStaticNodeIfRequired(&exception);
return &exception;
}
}