amath  1.8.5
Simple command line calculator
NumericOperator Class Reference

#include <operators.h>

Inheritance diagram for NumericOperator:
Collaboration diagram for NumericOperator:

Public Member Functions

 NumericOperator (ExpressionNode *left, ExpressionNode *right)
 
 ~NumericOperator ()
 
SyntaxNodeGetNext ()
 
char * GetText ()
 
void Attach (SyntaxNode *node)
 
void Detach (SyntaxNode *node)
 
void Replace (SyntaxNode *n, SyntaxNode *x)
 
- Public Member Functions inherited from ExpressionNode
 ExpressionNode ()
 
 ExpressionNode (Number *value)
 
virtual ~ExpressionNode ()
 
NodeType GetNodeType ()
 
virtual bool IsSilent ()
 
virtual NumberEvaluate ()=0
 
virtual int GetPrecedence ()=0
 
char * GetTextCode ()
 
char * Execute ()
 
- Public Member Functions inherited from SyntaxNode
 SyntaxNode ()
 
virtual ~SyntaxNode ()
 
void SetFirstNode ()
 
bool GetFirstNode () const
 
SyntaxNodeGetParent () const
 
void SetParent (SyntaxNode *node)
 
virtual ReductionType GetReductionType ()
 
virtual void ResetIterator ()
 

Protected Attributes

ExpressionNodeleft
 
ExpressionNoderight
 
- Protected Attributes inherited from ExpressionNode
Numberresult
 
- Protected Attributes inherited from SyntaxNode
CharBufferoutput
 
SyntaxNodeparent
 
SyntaxNodeiterator
 
bool leftBottom
 

Additional Inherited Members

- Protected Member Functions inherited from ExpressionNode
virtual char * GetNodeText ()=0
 

Detailed Description

Definition at line 106 of file operators.h.

Constructor & Destructor Documentation

◆ NumericOperator()

NumericOperator::NumericOperator ( ExpressionNode left,
ExpressionNode right 
)

Definition at line 308 of file operators.cpp.

References ExpressionNode::ExpressionNode(), left, and right.

Referenced by AdditionNode::AdditionNode(), AssignmentNode::AssignmentNode(), DivisionNode::DivisionNode(), MultiplicationNode::MultiplicationNode(), PowerNode::PowerNode(), and SubtractionNode::SubtractionNode().

308  :
309  ExpressionNode(), left(left), right(right)
310 {
311 }
ExpressionNode()
Definition: nodes.cpp:89
ExpressionNode * left
Definition: operators.h:119
ExpressionNode * right
Definition: operators.h:120
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~NumericOperator()

NumericOperator::~NumericOperator ( )

Definition at line 313 of file operators.cpp.

References left, and right.

314 {
315  if (left != nullptr)
316  {
317  delete left;
318  }
319 
320  if (right != nullptr)
321  {
322  delete right;
323  }
324 }
ExpressionNode * left
Definition: operators.h:119
ExpressionNode * right
Definition: operators.h:120

Member Function Documentation

◆ Attach()

void NumericOperator::Attach ( SyntaxNode node)
virtual

Implements SyntaxNode.

Definition at line 379 of file operators.cpp.

References left, right, and SyntaxNode::SetParent().

380 {
381  if (left == nullptr)
382  {
383  left = static_cast<ExpressionNode*>(node);
384  node->SetParent(this);
385  }
386  else if (right == nullptr)
387  {
388  right = static_cast<ExpressionNode*>(node);
389  node->SetParent(this);
390  }
391 }
ExpressionNode * left
Definition: operators.h:119
void SetParent(SyntaxNode *node)
Definition: nodes.cpp:75
ExpressionNode * right
Definition: operators.h:120
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
Here is the call graph for this function:

◆ Detach()

void NumericOperator::Detach ( SyntaxNode node)
virtual

Implements SyntaxNode.

Definition at line 393 of file operators.cpp.

References left, and right.

394 {
395  if (left == node)
396  {
397  left = nullptr;
398  }
399  else if (right == node)
400  {
401  right = nullptr;
402  }
403 }
ExpressionNode * left
Definition: operators.h:119
ExpressionNode * right
Definition: operators.h:120

◆ GetNext()

SyntaxNode * NumericOperator::GetNext ( )
virtual

Implements SyntaxNode.

Definition at line 362 of file operators.cpp.

References SyntaxNode::iterator, left, and right.

363 {
364  if (iterator == nullptr)
365  {
366  iterator = left;
367  return left;
368  }
369 
370  if (iterator == left)
371  {
372  iterator = right;
373  return right;
374  }
375 
376  return nullptr;
377 }
ExpressionNode * left
Definition: operators.h:119
ExpressionNode * right
Definition: operators.h:120
SyntaxNode * iterator
Definition: nodes.h:87

◆ GetText()

char * NumericOperator::GetText ( )
virtual

Implements ExpressionNode.

Definition at line 326 of file operators.cpp.

References CharBuffer::Append(), CharBuffer::Empty(), CharBuffer::EnsureSize(), ExpressionNode::GetNodeText(), ExpressionNode::GetPrecedence(), CharBuffer::GetString(), ExpressionNode::GetText(), left, SyntaxNode::output, right, and StrLen().

327 {
328  const char* leftText = left->GetText();
329  const char* nodeText = GetNodeText();
330  const char* rightText = right->GetText();
331 
332  output->Empty();
333  output->EnsureSize(StrLen(leftText) + 2 + StrLen(nodeText) + StrLen(rightText) + 2 + 1);
334 
335  if (left->GetPrecedence() != 0 && left->GetPrecedence() < GetPrecedence())
336  {
337  output->Append("(");
338  output->Append(leftText);
339  output->Append(")");
340  }
341  else
342  {
343  output->Append(leftText);
344  }
345 
346  output->Append(nodeText);
347 
348  if (right->GetPrecedence() != 0 && GetPrecedence() > right->GetPrecedence())
349  {
350  output->Append("(");
351  output->Append(rightText);
352  output->Append(")");
353  }
354  else
355  {
356  output->Append(rightText);
357  }
358 
359  return output->GetString();
360 }
void Empty()
Definition: charbuf.cpp:218
char * GetString() const
Definition: charbuf.cpp:306
virtual char * GetNodeText()=0
void Append(const char *source)
Definition: charbuf.cpp:262
ExpressionNode * left
Definition: operators.h:119
ExpressionNode * right
Definition: operators.h:120
virtual int GetPrecedence()=0
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
virtual char * GetText()=0
CharBuffer * output
Definition: nodes.h:85
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
Here is the call graph for this function:

◆ Replace()

void NumericOperator::Replace ( SyntaxNode n,
SyntaxNode x 
)
virtual

Implements SyntaxNode.

Definition at line 405 of file operators.cpp.

References SyntaxNode::iterator, left, and right.

406 {
407  if (left == n)
408  {
409  if (iterator == left)
410  {
411  iterator = static_cast<ExpressionNode*>(x);
412  }
413  delete left;
414  left = static_cast<ExpressionNode*>(x);
415  }
416  else if (right == n)
417  {
418  if (iterator == right)
419  {
420  iterator = static_cast<ExpressionNode*>(x);
421  }
422  delete right;
423  right = static_cast<ExpressionNode*>(x);
424  }
425 }
ExpressionNode * left
Definition: operators.h:119
ExpressionNode * right
Definition: operators.h:120
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
SyntaxNode * iterator
Definition: nodes.h:87

Member Data Documentation

◆ left

◆ right


The documentation for this class was generated from the following files: