diff options
author | dante <dante19031999@gmail.com> | 2021-01-07 18:46:50 +0100 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-01-31 15:22:36 +0100 |
commit | ad57c26d4f16ac5b477a29106f4933ed1e1ca911 (patch) | |
tree | a859fbecf6d89ddfa9cbc333375c6dc4bec66827 /starmath | |
parent | 56d8010963d8f8f53e9a7e5baf26f2e1f2f2363a (diff) |
Increase node parser mathematical abstraction
The purpose is go to a simple parser in wich nodes are loaded by priorities.
For that purpose we shall first increase the parser abstraction and simplify to maximum node releted operations.
For that purpose:
- Can set as subnodes unique_ptr and pointers alike.
- Binary operators node->setsubnodes() will automatically adjust
data order in base to data type.
Change-Id: I050360cad34616709549e7461add0811dc26edc6
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/108944
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'starmath')
-rw-r--r-- | starmath/inc/node.hxx | 37 | ||||
-rw-r--r-- | starmath/source/node.cxx | 80 | ||||
-rw-r--r-- | starmath/source/parse.cxx | 15 |
3 files changed, 119 insertions, 13 deletions
diff --git a/starmath/inc/node.hxx b/starmath/inc/node.hxx index 3bdb6bac6c9c..988a1024332a 100644 --- a/starmath/inc/node.hxx +++ b/starmath/inc/node.hxx @@ -500,6 +500,13 @@ public: virtual SmNode * GetSubNode(size_t nIndex) override; /** + * Gets the subnode of index nIndex, used for operators. + * @param nIndex + * @return subnode of index nIndex + */ + SmNode * GetSubNodeBinMo(size_t nIndex) const; + + /** * Does the cleaning of the subnodes. * @return */ @@ -517,6 +524,36 @@ public: /** * Sets subnodes. + * @param pFirst + * @param pSecond + * @param pThird + * @return + */ + void SetSubNodes(SmNode* pFirst, SmNode* pSecond, SmNode* pThird); + + /** + * Sets subnodes, used for operators. + * The data is reordered so the items are correctly ordered. + * @param pFirst + * @param pSecond + * @param pThird + * @return + */ + void SetSubNodesBinMo(std::unique_ptr<SmNode> pFirst, std::unique_ptr<SmNode> pSecond, + std::unique_ptr<SmNode> pThird = nullptr); + + /** + * Sets subnodes, used for operators. + * The data is reordered so the items are correctly ordered. + * @param pFirst + * @param pSecond + * @param pThird + * @return + */ + void SetSubNodesBinMo(SmNode* pFirst, SmNode* pSecond, SmNode* pThird); + + /** + * Sets subnodes. * @param rNodeArray * @return */ diff --git a/starmath/source/node.cxx b/starmath/source/node.cxx index f6fc8f0aed10..4de696dcabfa 100644 --- a/starmath/source/node.cxx +++ b/starmath/source/node.cxx @@ -387,6 +387,74 @@ void SmStructureNode::SetSubNodes(std::unique_ptr<SmNode> pFirst, std::unique_pt ClaimPaternity(); } +void SmStructureNode::SetSubNodes(SmNode* pFirst, SmNode* pSecond, SmNode* pThird) +{ + size_t nSize = pThird ? 3 : (pSecond ? 2 : (pFirst ? 1 : 0)); + maSubNodes.resize( nSize ); + if (pFirst) + maSubNodes[0] = pFirst; + if (pSecond) + maSubNodes[1] = pSecond; + if (pThird) + maSubNodes[2] = pThird; + + ClaimPaternity(); +} + +void SmStructureNode::SetSubNodesBinMo(std::unique_ptr<SmNode> pFirst, std::unique_ptr<SmNode> pSecond, std::unique_ptr<SmNode> pThird) +{ + if(GetType()==SmNodeType::BinDiagonal) + { + size_t nSize = pSecond ? 3 : (pThird ? 2 : (pFirst ? 1 : 0)); + maSubNodes.resize( nSize ); + if (pFirst) + maSubNodes[0] = pFirst.release(); + if (pSecond) + maSubNodes[2] = pSecond.release(); + if (pThird) + maSubNodes[1] = pThird.release(); + } + else + { + size_t nSize = pThird ? 3 : (pSecond ? 2 : (pFirst ? 1 : 0)); + maSubNodes.resize( nSize ); + if (pFirst) + maSubNodes[0] = pFirst.release(); + if (pSecond) + maSubNodes[1] = pSecond.release(); + if (pThird) + maSubNodes[2] = pThird.release(); + } + ClaimPaternity(); +} + +void SmStructureNode::SetSubNodesBinMo(SmNode* pFirst, SmNode* pSecond, SmNode* pThird) +{ + if(GetType()==SmNodeType::BinDiagonal) + { + size_t nSize = pSecond ? 3 : (pThird ? 2 : (pFirst ? 1 : 0)); + maSubNodes.resize( nSize ); + if (pFirst) + maSubNodes[0] = pFirst; + if (pSecond) + maSubNodes[1] = pSecond; + if (pThird) + maSubNodes[2] = pThird; + } + else + { + size_t nSize = pThird ? 3 : (pSecond ? 2 : (pFirst ? 1 : 0)); + maSubNodes.resize( nSize ); + if (pFirst) + maSubNodes[0] = pFirst; + if (pSecond) + maSubNodes[1] = pSecond; + if (pThird) + maSubNodes[2] = pThird; + } + ClaimPaternity(); +} + void SmStructureNode::SetSubNodes(SmNodeArray&& rNodeArray) { maSubNodes = std::move(rNodeArray); @@ -408,6 +476,18 @@ SmNode* SmStructureNode::GetSubNode(size_t nIndex) return maSubNodes[nIndex]; } +SmNode* SmStructureNode::GetSubNodeBinMo(size_t nIndex) const +{ + if(GetType()==SmNodeType::BinDiagonal) + { + if (nIndex==1) + nIndex = 2; + else if (nIndex==2) + nIndex = 1; + } + return maSubNodes[nIndex]; +} + void SmStructureNode::GetAccessibleText( OUStringBuffer &rText ) const { ForEachNonNull(const_cast<SmStructureNode *>(this), diff --git a/starmath/source/parse.cxx b/starmath/source/parse.cxx index ef09bf24d9fa..1e0846eb7667 100644 --- a/starmath/source/parse.cxx +++ b/starmath/source/parse.cxx @@ -1320,12 +1320,11 @@ std::unique_ptr<SmNode> SmParser::DoProduct() //this linear loop builds a recursive structure, if it gets //too deep then later processing, e.g. releasing the tree, //can exhaust stack - if (nDepthLimit > DEPTH_LIMIT) + if (m_nParseDepth + nDepthLimit > DEPTH_LIMIT) throw std::range_error("parser depth limit"); std::unique_ptr<SmStructureNode> xSNode; std::unique_ptr<SmNode> xOper; - bool bSwitchArgs = false; SmTokenType eType = m_aCurToken.eType; switch (eType) @@ -1365,7 +1364,6 @@ std::unique_ptr<SmNode> SmParser::DoProduct() xOper.reset(new SmPolyLineNode(m_aCurToken)); NextToken(); - bSwitchArgs = true; break; } @@ -1376,16 +1374,7 @@ std::unique_ptr<SmNode> SmParser::DoProduct() } auto xArg = DoPower(); - - if (bSwitchArgs) - { - //! vgl siehe SmBinDiagonalNode::Arrange - xSNode->SetSubNodes(std::move(xFirst), std::move(xArg), std::move(xOper)); - } - else - { - xSNode->SetSubNodes(std::move(xFirst), std::move(xOper), std::move(xArg)); - } + xSNode->SetSubNodesBinMo(std::move(xFirst), std::move(xOper), std::move(xArg)); xFirst = std::move(xSNode); ++nDepthLimit; } |