[poincare] Only some layouts are serialized with System Parentheses

wrapping their children (absoluteValueLayout, CeilingLayout...)
This commit is contained in:
Émilie Feral
2019-07-24 17:37:14 +02:00
parent b11e6e5661
commit f13dd5f9be
9 changed files with 33 additions and 19 deletions

View File

@@ -16,7 +16,7 @@ public:
// SerializationHelperInterface
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, AbsoluteValue::s_functionHelper.name());
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, AbsoluteValue::s_functionHelper.name(), true);
}
// TreeNode

View File

@@ -16,7 +16,7 @@ public:
Type type() const override { return Type::CeilingLayout; }
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Ceiling::s_functionHelper.name());
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Ceiling::s_functionHelper.name(), true);
}
// TreeNode

View File

@@ -23,7 +23,7 @@ public:
void moveCursorUp(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override { assert(false); }
void moveCursorDown(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override { assert(false); }
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Sum::s_functionHelper.name());
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Sum::s_functionHelper.name(), true);
}
LayoutNode * layoutToPointWhenInserting(Expression * correspondingExpression) override {

View File

@@ -16,7 +16,7 @@ public:
Type type() const override { return Type::FloorLayout; }
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Floor::s_functionHelper.name());
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Floor::s_functionHelper.name(), true);
}
// TreeNode

View File

@@ -24,6 +24,11 @@ namespace SerializationHelper {
int firstChildIndex = 0,
int lastChildIndex = -1);
/* needsSystemParentheses add System parentheses wrapping the layout children.
* It is used when serializing special layouts to avoid creating parsable
* string from misformed layout. For instance, we don't want to parse:
* |2)(3|, so we serialize it in "abs({2)(3})" where {} are System parentheses
* instead of "abs(2)(3)". */
int Prefix(
const TreeNode * node,
char * buffer,
@@ -31,6 +36,7 @@ namespace SerializationHelper {
Preferences::PrintFloatMode floatDisplayMode,
int numberOfDigits,
const char * operatorName,
bool needsSystemParentheses = false,
int lastChildIndex = -1);
int SerializeChild(

View File

@@ -74,7 +74,7 @@ void BinomialCoefficientLayoutNode::moveCursorDown(LayoutCursor * cursor, bool *
}
int BinomialCoefficientLayoutNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, BinomialCoefficient::s_functionHelper.name());
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, BinomialCoefficient::s_functionHelper.name(), true);
}
KDSize BinomialCoefficientLayoutNode::computeSize() {

View File

@@ -52,7 +52,7 @@ void ConjugateLayoutNode::moveCursorRight(LayoutCursor * cursor, bool * shouldRe
}
int ConjugateLayoutNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Conjugate::s_functionHelper.name());
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Conjugate::s_functionHelper.name(), true);
}
KDSize ConjugateLayoutNode::computeSize() {

View File

@@ -152,13 +152,13 @@ int NthRootLayoutNode::serialize(char * buffer, int bufferSize, Preferences::Pri
assert((const_cast<NthRootLayoutNode *>(this))->indexLayout());
if ((const_cast<NthRootLayoutNode *>(this))->indexLayout()->isEmpty()) {
// Case: root(x,empty): Write "'SquareRootSymbol'('radicandLayout')"
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, SquareRoot::s_functionHelper.name(), 0);
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, SquareRoot::s_functionHelper.name(), true, 0);
}
// Case: root(x,n)
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, NthRoot::s_functionHelper.name());
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, NthRoot::s_functionHelper.name(), true);
}
// Case: squareRoot(x)
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, SquareRoot::s_functionHelper.name());
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, SquareRoot::s_functionHelper.name(), true);
}
KDSize NthRootLayoutNode::computeSize() {

View File

@@ -68,6 +68,7 @@ int InfixPrefix(
Preferences::PrintFloatMode floatDisplayMode,
int numberOfDigits,
const char * operatorName,
bool needsSystemParentheses,
int firstChildIndex,
int lastChildIndex)
{
@@ -95,10 +96,13 @@ int InfixPrefix(
if (numberOfChar >= bufferSize-1) {
return bufferSize-1;
}
// Add the opening system parenthesis
numberOfChar += UTF8Decoder::CodePointToChars(UCodePointLeftSystemParenthesis, buffer+numberOfChar, bufferSize - numberOfChar);
if (numberOfChar >= bufferSize-1) {
return bufferSize-1;
if (needsSystemParentheses) {
// Add the opening system parenthesis
numberOfChar += UTF8Decoder::CodePointToChars(UCodePointLeftSystemParenthesis, buffer+numberOfChar, bufferSize - numberOfChar);
if (numberOfChar >= bufferSize-1) {
return bufferSize-1;
}
}
}
@@ -133,11 +137,14 @@ int InfixPrefix(
}
if (prefix) {
// Add the closing system parenthesis
numberOfChar += UTF8Decoder::CodePointToChars(UCodePointRightSystemParenthesis, buffer+numberOfChar, bufferSize - numberOfChar);
if (numberOfChar >= bufferSize-1) {
return bufferSize-1;
if (needsSystemParentheses) {
// Add the closing system parenthesis
numberOfChar += UTF8Decoder::CodePointToChars(UCodePointRightSystemParenthesis, buffer+numberOfChar, bufferSize - numberOfChar);
if (numberOfChar >= bufferSize-1) {
return bufferSize-1;
}
}
// Add the closing parenthesis
numberOfChar += UTF8Decoder::CodePointToChars(')', buffer+numberOfChar, bufferSize - numberOfChar);
if (numberOfChar >= bufferSize-1) {
@@ -159,7 +166,7 @@ int SerializationHelper::Infix(
int firstChildIndex,
int lastChildIndex)
{
return InfixPrefix(false, node, buffer, bufferSize, floatDisplayMode, numberOfDigits, operatorName, firstChildIndex, lastChildIndex);
return InfixPrefix(false, node, buffer, bufferSize, floatDisplayMode, numberOfDigits, operatorName, false, firstChildIndex, lastChildIndex);
}
int SerializationHelper::Prefix(
@@ -169,9 +176,10 @@ int SerializationHelper::Prefix(
Preferences::PrintFloatMode floatDisplayMode,
int numberOfDigits,
const char * operatorName,
bool needsSystemParentheses,
int lastChildIndex)
{
return InfixPrefix(true, node, buffer, bufferSize, floatDisplayMode, numberOfDigits, operatorName, 0, lastChildIndex);
return InfixPrefix(true, node, buffer, bufferSize, floatDisplayMode, numberOfDigits, operatorName, needsSystemParentheses, 0, lastChildIndex);
}
int SerializationHelper::CodePoint(char * buffer, int bufferSize, class CodePoint c) {