amath  1.8.5
Simple command line calculator
node.cpp
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  */
29 
30 #include "node.h"
31 #include "amathc.h"
32 #include "system/program.h"
33 
34 FunctionNode::FunctionNode(ExpressionNode* expression, char* text, char* sys) :
35  ExpressionNode(), expression(expression)
36 {
37  AllocAndCopy(&this->name, text);
38  AllocAndCopy(&this->sysname, sys);
39 }
40 
42 {
43  if (expression != nullptr)
44  {
45  delete expression;
46  }
47 
48  if (name != nullptr)
49  {
50  delete name;
51  }
52 
53  if (sysname != nullptr)
54  {
55  delete sysname;
56  }
57 }
58 
60 {
61  return 5;
62 }
63 
65 {
66  const char* functionText = GetNodeText();
67  const char* expText = expression->GetText();
68 
69  output->EnsureSize(StrLen(functionText) + StrLen(expText) + 2 + 1);
71  output->Append(functionText);
73  output->Append(expText);
75 
76  return output->GetString();
77 }
78 
80 {
82  ? sysname
83  : name;
84 }
85 
87 {
88  if (iterator == nullptr)
89  {
91  return expression;
92  }
93 
94  return nullptr;
95 }
96 
98 {
99  if (expression == nullptr)
100  {
101  expression = static_cast<ExpressionNode*>(node);
102  node->SetParent(this);
103  }
104 }
105 
107 {
108  if (expression == node)
109  {
110  expression = nullptr;
111  }
112 }
113 
115 {
116  if (expression == n)
117  {
118  delete expression;
119  expression = static_cast<ExpressionNode*>(x);
120  }
121 }
virtual void Detach(SyntaxNode *node)
Definition: node.cpp:106
Represents a mathematical function in a syntax tree.
Definition: node.h:50
void Empty()
Definition: charbuf.cpp:218
char * GetString() const
Definition: charbuf.cpp:306
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
ExpressionNode()
Definition: nodes.cpp:89
char * sysname
Definition: node.h:68
virtual void Replace(SyntaxNode *n, SyntaxNode *x)
Definition: node.cpp:114
void Append(const char *source)
Definition: charbuf.cpp:262
virtual char * GetNodeText()
Definition: node.cpp:79
char * GetText()
Definition: node.cpp:64
~FunctionNode()
Definition: node.cpp:41
void SetParent(SyntaxNode *node)
Definition: nodes.cpp:75
char * name
Definition: node.h:67
virtual void Attach(SyntaxNode *node)
Definition: node.cpp:97
virtual SyntaxNode * GetNext()
Definition: node.cpp:86
bool GetRefactorNames()
FunctionNode(ExpressionNode *expression, char *text, char *sys)
Definition: node.cpp:34
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
virtual char * GetText()=0
int GetPrecedence()
Definition: node.cpp:59
CharBuffer * output
Definition: nodes.h:85
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:40
SyntaxNode * iterator
Definition: nodes.h:87
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
ExpressionNode * expression
Definition: node.h:66
class PreferencesBase * Preferences
Definition: program.h:73