mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-26 09:10:48 +01:00
[tree] SerializableNode and Ref
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
#ifndef EXPRESSION_NODE_H
|
||||
#define EXPRESSION_NODE_H
|
||||
|
||||
#include "tree_node.h"
|
||||
#include <poincare/serializable_node.h>
|
||||
#include <stdint.h>
|
||||
|
||||
class ExpressionNode : public TreeNode {
|
||||
class ExpressionNode : public SerializableNode {
|
||||
public:
|
||||
enum class Type : uint8_t {
|
||||
AllocationFailure = 0,
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
#ifndef EXPRESSION_REFERENCE_H
|
||||
#define EXPRESSION_REFERENCE_H
|
||||
#ifndef POINCARE_EXPRESSION_REFERENCE_H
|
||||
#define POINCARE_EXPRESSION_REFERENCE_H
|
||||
|
||||
#include "tree_reference.h"
|
||||
#include "expression_node.h"
|
||||
#include <poincare/expression_node.h>
|
||||
#include <poincare/serializable_reference.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
template <class T>
|
||||
class ExpressionReference : public TreeReference<T> {
|
||||
class ExpressionReference : public SerializableReference<T> {
|
||||
public:
|
||||
using TreeReference<T>::TreeReference;
|
||||
using SerializableReference<T>::SerializableReference;
|
||||
|
||||
/* Allow every ExpressionReference<T> to be transformed into an
|
||||
* ExpressionReference<ExpressionNode>, i.e. ExpressionRef */
|
||||
|
||||
@@ -2,6 +2,10 @@ SFLAGS += -Ipoincare/include
|
||||
|
||||
#include poincare/src/simplify/Makefile
|
||||
#include poincare/src/simplification/Makefile
|
||||
objs += $(addprefix poincare/src/,\
|
||||
tree_node.o\
|
||||
tree_pool.o\
|
||||
)
|
||||
|
||||
objs += $(addprefix poincare/src/,\
|
||||
char_layout_node.o\
|
||||
@@ -92,8 +96,6 @@ objs += $(addprefix poincare/src/,\
|
||||
sum.o\
|
||||
symbol.o\
|
||||
tangent.o\
|
||||
tree_node.o\
|
||||
tree_pool.o\
|
||||
trigonometry.o\
|
||||
undefined.o\
|
||||
variable_context.o\
|
||||
|
||||
@@ -72,6 +72,8 @@
|
||||
#include <poincare/rational.h>
|
||||
#include <poincare/real_part.h>
|
||||
#include <poincare/round.h>
|
||||
#include <poincare/serializable_node.h>
|
||||
#include <poincare/serializable_reference.h>
|
||||
#include <poincare/sine.h>
|
||||
#include <poincare/square_root.h>
|
||||
#include <poincare/store.h>
|
||||
|
||||
@@ -53,8 +53,8 @@ public:
|
||||
bool writeFirstChild = true);
|
||||
|
||||
/* LayoutReference to Text */
|
||||
static int writeInfixTreeRefTextInBuffer(
|
||||
const TreeRef treeRef,
|
||||
static int writeInfixSerializableRefTextInBuffer(
|
||||
const SerializableRef serializableRef,
|
||||
char * buffer,
|
||||
int bufferSize,
|
||||
int numberOfDigits,
|
||||
@@ -71,7 +71,7 @@ private:
|
||||
static int writeInfixExpressionOrExpressionLayoutTextInBuffer(const Expression * expression, const ExpressionLayout * expressionLayout, char * buffer, int bufferSize, int numberOfDigits, const char * operatorName, int firstChildIndex, int lastChildIndex, ChildNeedsParenthesis childNeedsParenthesis);
|
||||
static int writePrefixExpressionOrExpressionLayoutTextInBuffer(const Expression * expression, const ExpressionLayout * expressionLayout, char * buffer, int bufferSize, int numberOfDigits, const char * operatorName, bool writeFirstChild = true);
|
||||
|
||||
static void writeChildTreeInBuffer(TreeRef childRef, TreeRef parentRef, char * buffer, int bufferSize, int numberOfDigits, int * numberOfChar);
|
||||
static void writeChildTreeInBuffer(SerializableRef childRef, SerializableRef parentRef, char * buffer, int bufferSize, int numberOfDigits, int * numberOfChar);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef POINCARE_LAYOUT_NODE_H
|
||||
#define POINCARE_LAYOUT_NODE_H
|
||||
|
||||
#include <poincare/tree_node.h>
|
||||
#include <poincare/serializable_node.h>
|
||||
#include <kandinsky.h>
|
||||
#include <ion/charset.h>
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Poincare {
|
||||
|
||||
class LayoutCursor;
|
||||
|
||||
class LayoutNode : public TreeNode {
|
||||
class LayoutNode : public SerializableNode {
|
||||
public:
|
||||
enum class VerticalDirection {
|
||||
Up,
|
||||
@@ -22,7 +22,7 @@ public:
|
||||
|
||||
// Constructor
|
||||
LayoutNode() :
|
||||
TreeNode(),
|
||||
SerializableNode(),
|
||||
m_baseline(0),
|
||||
m_frame(KDRectZero),
|
||||
m_baselined(false),
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
#ifndef POINCARE_LAYOUT_REFERENCE_H
|
||||
#define POINCARE_LAYOUT_REFERENCE_H
|
||||
|
||||
#include "tree_reference.h"
|
||||
#include "layout_node.h"
|
||||
#include <poincare/layout_node.h>
|
||||
#include <poincare/serializable_reference.h>
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
class LayoutCursor;
|
||||
|
||||
template <typename T>
|
||||
class LayoutReference : public TreeReference<T> {
|
||||
class LayoutReference : public SerializableReference<T> {
|
||||
friend class LayoutCursor;
|
||||
public:
|
||||
using TreeReference<T>::TreeReference;
|
||||
using SerializableReference<T>::SerializableReference;
|
||||
|
||||
// Operators
|
||||
|
||||
|
||||
19
poincare/include/poincare/serializable_node.h
Normal file
19
poincare/include/poincare/serializable_node.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef POINCARE_SERIALIZABLE_NODE_H
|
||||
#define POINCARE_SERIALIZABLE_NODE_H
|
||||
|
||||
#include <poincare/tree_node.h>
|
||||
#include <poincare/print_float.h>
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
class SerializableNode : public TreeNode {
|
||||
public:
|
||||
using TreeNode::TreeNode;
|
||||
virtual bool needsParenthesisWithParent(SerializableNode * parentNode) { return false; } //TODO
|
||||
virtual int writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits = PrintFloat::k_numberOfStoredSignificantDigits) const = 0;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
35
poincare/include/poincare/serializable_reference.h
Normal file
35
poincare/include/poincare/serializable_reference.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef POINCARE_SERIALIZABLE_REFERENCE_H
|
||||
#define POINCARE_SERIALIZABLE_REFERENCE_H
|
||||
|
||||
#include <poincare/print_float.h>
|
||||
#include <poincare/tree_reference.h>
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
template <typename T>
|
||||
class SerializableReference : public TreeReference<T> {
|
||||
public:
|
||||
using TreeReference<T>::TreeReference;
|
||||
|
||||
// Serialization
|
||||
bool needsParenthesisWithParent(SerializableReference<SerializableNode> parentRef) {
|
||||
assert(this->isDefined());
|
||||
return this->typedNode()->needsParenthesisWithParent(parentRef.typedNode());
|
||||
}
|
||||
int writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits = PrintFloat::k_numberOfStoredSignificantDigits) const {
|
||||
assert(this->isDefined());
|
||||
return this->typedNode()->writeTextInBuffer(buffer, bufferSize, numberOfSignificantDigits);
|
||||
}
|
||||
|
||||
// Tree
|
||||
SerializableReference<SerializableNode> serializableChildAtIndex(int i) {
|
||||
TreeReference<T> treeRefChild = TreeReference<T>::treeChildAtIndex(i);
|
||||
return SerializableReference<SerializableNode>(treeRefChild.node());
|
||||
}
|
||||
};
|
||||
|
||||
typedef SerializableReference<SerializableNode> SerializableRef;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,8 +1,6 @@
|
||||
#ifndef POINCARE_TREE_NODE_H
|
||||
#define POINCARE_TREE_NODE_H
|
||||
|
||||
#include <poincare/print_float.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <strings.h>
|
||||
@@ -37,10 +35,6 @@ public:
|
||||
|
||||
virtual const char * description() const { return "UNKNOWN";}
|
||||
|
||||
// Serialization
|
||||
virtual bool needsParenthesisWithParent(TreeNode * parentNode) { return false; } //TODO virtual pure and override on expresionNode/layoutNode
|
||||
virtual int writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits = PrintFloat::k_numberOfStoredSignificantDigits) const { return 0; } //TODO virtual pure
|
||||
|
||||
// Allocation failure
|
||||
virtual bool isAllocationFailure() const { return false; }
|
||||
virtual TreeNode * failedAllocationStaticNode() = 0;
|
||||
|
||||
@@ -82,16 +82,6 @@ public:
|
||||
return node()->numberOfDescendants(includeSelf);
|
||||
}
|
||||
|
||||
// Serialization
|
||||
bool needsParenthesisWithParent(TreeReference<TreeNode> parentRef) {
|
||||
assert(isDefined());
|
||||
return node()->needsParenthesisWithParent(parentRef.node());
|
||||
}
|
||||
int writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits = PrintFloat::k_numberOfStoredSignificantDigits) const {
|
||||
assert(isDefined());
|
||||
return node()->writeTextInBuffer(buffer, bufferSize, numberOfSignificantDigits);
|
||||
}
|
||||
|
||||
// Hierarchy
|
||||
bool hasChild(TreeReference<TreeNode> t) const {
|
||||
assert(isDefined());
|
||||
|
||||
@@ -142,7 +142,7 @@ int HorizontalLayoutNode::writeTextInBuffer(char * buffer, int bufferSize, int n
|
||||
buffer[0] = 0;
|
||||
return 0;
|
||||
}
|
||||
return LayoutEngine::writeInfixTreeRefTextInBuffer(TreeRef(const_cast<HorizontalLayoutNode *>(this)), buffer, bufferSize, numberOfSignificantDigits, "");
|
||||
return LayoutEngine::writeInfixSerializableRefTextInBuffer(SerializableRef(const_cast<HorizontalLayoutNode *>(this)), buffer, bufferSize, numberOfSignificantDigits, "");
|
||||
}
|
||||
|
||||
void HorizontalLayoutNode::moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) {
|
||||
|
||||
@@ -213,8 +213,8 @@ int LayoutEngine::writePrefixExpressionOrExpressionLayoutTextInBuffer(const Expr
|
||||
}
|
||||
|
||||
/* LayoutReference to Text */
|
||||
int LayoutEngine::writeInfixTreeRefTextInBuffer(
|
||||
const TreeRef treeRef,
|
||||
int LayoutEngine::writeInfixSerializableRefTextInBuffer(
|
||||
const SerializableRef serializableRef,
|
||||
char * buffer,
|
||||
int bufferSize,
|
||||
int numberOfDigits,
|
||||
@@ -223,7 +223,7 @@ int LayoutEngine::writeInfixTreeRefTextInBuffer(
|
||||
int lastChildIndex)
|
||||
{
|
||||
// If buffer has size 0 or 1, put a zero if it fits and return
|
||||
assert(treeRef.isDefined());
|
||||
assert(serializableRef.isDefined());
|
||||
if (bufferSize == 0) {
|
||||
return -1;
|
||||
}
|
||||
@@ -233,13 +233,13 @@ int LayoutEngine::writeInfixTreeRefTextInBuffer(
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get some information on the TreeRef
|
||||
// Get some information on the SerializableRef
|
||||
int numberOfChar = 0;
|
||||
int numberOfOperands = treeRef.numberOfChildren();
|
||||
int numberOfOperands = serializableRef.numberOfChildren();
|
||||
assert(numberOfOperands > 0);
|
||||
|
||||
// Write the first child, with parentheses if needed
|
||||
writeChildTreeInBuffer(treeRef.treeChildAtIndex(firstChildIndex), treeRef, buffer, bufferSize, numberOfDigits, &numberOfChar);
|
||||
writeChildTreeInBuffer((const_cast<SerializableRef *>(&serializableRef))->serializableChildAtIndex(firstChildIndex), serializableRef, buffer, bufferSize, numberOfDigits, &numberOfChar);
|
||||
if (numberOfChar >= bufferSize-1) {
|
||||
return bufferSize-1;
|
||||
}
|
||||
@@ -252,7 +252,7 @@ int LayoutEngine::writeInfixTreeRefTextInBuffer(
|
||||
return bufferSize-1;
|
||||
}
|
||||
// Write the child, with parentheses if needed
|
||||
writeChildTreeInBuffer(treeRef.treeChildAtIndex(i), treeRef, buffer, bufferSize, numberOfDigits, &numberOfChar);
|
||||
writeChildTreeInBuffer((const_cast<SerializableRef *>(&serializableRef))->serializableChildAtIndex(i), serializableRef, buffer, bufferSize, numberOfDigits, &numberOfChar);
|
||||
if (numberOfChar >= bufferSize-1) {
|
||||
return bufferSize-1;
|
||||
}
|
||||
@@ -263,7 +263,7 @@ int LayoutEngine::writeInfixTreeRefTextInBuffer(
|
||||
return numberOfChar;
|
||||
}
|
||||
|
||||
void LayoutEngine::writeChildTreeInBuffer(TreeRef childRef, TreeRef parentRef, char * buffer, int bufferSize, int numberOfDigits, int * numberOfChar) {
|
||||
void LayoutEngine::writeChildTreeInBuffer(SerializableRef childRef, SerializableRef parentRef, char * buffer, int bufferSize, int numberOfDigits, int * numberOfChar) {
|
||||
// Write the child with parentheses if needed
|
||||
bool addParentheses = childRef.needsParenthesisWithParent(parentRef);
|
||||
if (addParentheses) {
|
||||
|
||||
Reference in New Issue
Block a user