amath  1.8.5
Simple command line calculator
FunctionList Class Reference

A list of user defined functions. More...

#include <functionlist.h>

Collaboration diagram for FunctionList:

Public Member Functions

 FunctionList ()
 
 ~FunctionList ()
 
void Clear ()
 
bool Delete (const char *name, const char *argument)
 
UserFunctionGetFirstFunction () const
 
UserFunctionGetFunctionDef (const char *name)
 
UserFunctionGetFunctionDef (const char *name, const char *argument) const
 
FunctionNodeGetFunctionCall (const char *function, ExpressionNode *value) const
 
char * List () const
 
char * ListDefinitions () const
 

Static Public Member Functions

static bool IsSystemFunction (const char *name)
 

Private Member Functions

char * ListContent (bool cmdFormat) const
 

Static Private Member Functions

static functiondefGetSystemFunction (const char *ident)
 
static char * FindAlias (const char *ident)
 

Private Attributes

CharBufferbuf
 
UserFunctionfirst
 

Detailed Description

A list of user defined functions.

Definition at line 42 of file functionlist.h.

Constructor & Destructor Documentation

◆ FunctionList()

FunctionList::FunctionList ( )

Definition at line 39 of file functionlist.cpp.

References buf, CharBuffer::CharBuffer(), and first.

Referenced by Program::Program().

40 {
41  first = nullptr;
42  buf = new CharBuffer();
43 }
CharBuffer * buf
Definition: functionlist.h:61
UserFunction * first
Definition: functionlist.h:62
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:44
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~FunctionList()

FunctionList::~FunctionList ( )

Definition at line 45 of file functionlist.cpp.

References buf, and first.

46 {
47  if (first != nullptr)
48  {
49  delete first;
50  }
51 
52  delete buf;
53 }
CharBuffer * buf
Definition: functionlist.h:61
UserFunction * first
Definition: functionlist.h:62

Member Function Documentation

◆ Clear()

void FunctionList::Clear ( )

Definition at line 55 of file functionlist.cpp.

References buf, CharBuffer::ClearBuffer(), and first.

Referenced by DeleteStatement::Execute().

56 {
57  if (first != nullptr)
58  {
59  delete first;
60  first = nullptr;
61  }
62 
63  buf->ClearBuffer();
64 }
CharBuffer * buf
Definition: functionlist.h:61
void ClearBuffer()
Release memory in buffer.
Definition: charbuf.cpp:65
UserFunction * first
Definition: functionlist.h:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Delete()

bool FunctionList::Delete ( const char *  name,
const char *  argument 
)

Definition at line 66 of file functionlist.cpp.

References UserFunction::chainDelete, first, UserFunction::GetName(), Variable::GetName(), UserFunction::GetVariable(), UserFunction::Next, and StrIsEqual().

Referenced by DeleteStatement::Execute().

67 {
68  if (first == nullptr)
69  {
70  return false;
71  }
72 
73  if (first != nullptr && StrIsEqual(first->GetName(), name) && StrIsEqual(first->GetVariable()->GetName(), argument))
74  {
75  UserFunction* func = first;
76  first = first->Next;
77  func->chainDelete = false;
78  delete func;
79  return true;
80  }
81 
82  UserFunction* current = first->Next;
83  UserFunction* last = first;
84 
85  while (current != nullptr && !StrIsEqual(current->GetName(), name))
86  {
87  current = current->Next;
88  last = last->Next;
89  }
90 
91  if (current == nullptr)
92  {
93  return false;
94  }
95  else if (!StrIsEqual(current->GetVariable()->GetName(), argument))
96  {
97  return false;
98  }
99 
100  last->Next = current->Next;
101 
102  // Only delete this variable. Not the whole chain.
103  current->chainDelete = false;
104  delete current;
105 
106  return true;
107 }
UserFunction * Next
Definition: userfunction.h:51
char * GetName() const
Definition: values.cpp:69
Variable * GetVariable() const
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
char * GetName() const
A user defined function.
Definition: userfunction.h:44
UserFunction * first
Definition: functionlist.h:62
bool chainDelete
Definition: userfunction.h:65
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FindAlias()

char * FunctionList::FindAlias ( const char *  ident)
staticprivate

Definition at line 258 of file functionlist.cpp.

References identalias::alias, identalias::ident, identaliases, and StrIsEqual().

Referenced by GetFunctionCall(), and GetSystemFunction().

259 {
260  static const unsigned int count = sizeof(identaliases) / sizeof(identalias);
261  for (unsigned int i = 0; i < count; i++)
262  {
263  if (StrIsEqual(identaliases[i].ident, ident))
264  {
265  return const_cast<char*>(identaliases[i].alias);
266  }
267  }
268 
269  return const_cast<char*>(ident);
270 }
static const identalias identaliases[]
Definition: functionalias.h:35
const char * alias
Definition: amatht.h:42
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFirstFunction()

UserFunction * FunctionList::GetFirstFunction ( ) const

Definition at line 109 of file functionlist.cpp.

References first.

110 {
111  return first;
112 }
UserFunction * first
Definition: functionlist.h:62

◆ GetFunctionCall()

FunctionNode * FunctionList::GetFunctionCall ( const char *  function,
ExpressionNode value 
) const

Definition at line 160 of file functionlist.cpp.

References functiondef::create, FindAlias(), first, UserFunction::GetName(), GetSystemFunction(), UserFunction::Next, StrIsEqual(), and UserFunctionNode::UserFunctionNode().

Referenced by Parser::ParseIdent().

161 {
162  const functiondef* systemFunction = GetSystemFunction(function);
163  if (systemFunction != nullptr)
164  {
165  char* sysname = FindAlias(function);
166  return (systemFunction->create)(parameter, const_cast<char*>(function), sysname);
167  }
168 
169  UserFunction* current = first;
170  while (current != nullptr && !StrIsEqual(current->GetName(), function))
171  {
172  current = current->Next;
173  }
174 
175  if (current != nullptr)
176  {
177  return new UserFunctionNode(current, parameter);
178  }
179 
180  return nullptr;
181 }
UserFunction * Next
Definition: userfunction.h:51
An expression node able to compute a function value.
Definition: user.h:40
Function definitions of known mathematical functions.
Definition: defs.h:103
CreateFunctionNode create
Definition: defs.h:106
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
char * GetName() const
A user defined function.
Definition: userfunction.h:44
static functiondef * GetSystemFunction(const char *ident)
static char * FindAlias(const char *ident)
UserFunction * first
Definition: functionlist.h:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFunctionDef() [1/2]

UserFunction * FunctionList::GetFunctionDef ( const char *  name)

Definition at line 114 of file functionlist.cpp.

References first, UserFunction::GetName(), UserFunction::Next, StrIsEqual(), and UserFunction::UserFunction().

Referenced by Parser::ParseFunctionDef().

115 {
116  // Search the list for function definition. If function not found then create a new definition.
117  if (first == nullptr)
118  {
119  first = new UserFunction(name);
120  return first;
121  }
122 
123  UserFunction* current = first;
124  UserFunction* last = nullptr;
125 
126  while (current != nullptr)
127  {
128  if (StrIsEqual(current->GetName(), name))
129  {
130  return current;
131  }
132 
133  last = current;
134  current = current->Next;
135  }
136 
137  current = new UserFunction(name);
138  last->Next = current;
139 
140  return current;
141 }
UserFunction * Next
Definition: userfunction.h:51
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
char * GetName() const
A user defined function.
Definition: userfunction.h:44
UserFunction * first
Definition: functionlist.h:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFunctionDef() [2/2]

UserFunction * FunctionList::GetFunctionDef ( const char *  name,
const char *  argument 
) const

Definition at line 143 of file functionlist.cpp.

References first, UserFunction::GetName(), Variable::GetName(), UserFunction::GetVariable(), UserFunction::Next, and StrIsEqual().

Referenced by PlotStatement::Execute().

144 {
145  UserFunction* current = first;
146 
147  while (current != nullptr)
148  {
149  if (StrIsEqual(current->GetName(), name) && StrIsEqual(current->GetVariable()->GetName(), argument))
150  {
151  return current;
152  }
153 
154  current = current->Next;
155  }
156 
157  return nullptr;
158 }
UserFunction * Next
Definition: userfunction.h:51
char * GetName() const
Definition: values.cpp:69
Variable * GetVariable() const
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
char * GetName() const
A user defined function.
Definition: userfunction.h:44
UserFunction * first
Definition: functionlist.h:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetSystemFunction()

functiondef * FunctionList::GetSystemFunction ( const char *  ident)
staticprivate

Definition at line 243 of file functionlist.cpp.

References FindAlias(), functiondefs, functiondef::name, and StrIsEqual().

Referenced by GetFunctionCall(), and IsSystemFunction().

244 {
245  char* s = FindAlias(ident);
246  static const unsigned int count = sizeof(functiondefs) / sizeof(functiondef);
247  for (unsigned int i = 0; i < count; i++)
248  {
249  if (StrIsEqual(functiondefs[i].name, s))
250  {
251  return const_cast<functiondef*>(&functiondefs[i]);
252  }
253  }
254 
255  return nullptr;
256 }
Function definitions of known mathematical functions.
Definition: defs.h:103
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
static const functiondef functiondefs[]
Definition: defs.h:109
static char * FindAlias(const char *ident)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsSystemFunction()

bool FunctionList::IsSystemFunction ( const char *  name)
static

Definition at line 238 of file functionlist.cpp.

References GetSystemFunction().

Referenced by Parser::ParseFunctionDef().

239 {
240  return (GetSystemFunction(name) != nullptr);
241 }
static functiondef * GetSystemFunction(const char *ident)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ List()

char * FunctionList::List ( ) const

Definition at line 183 of file functionlist.cpp.

References ListContent().

Referenced by ListFunctionsStatement::Execute().

184 {
185  return ListContent(false);
186 }
char * ListContent(bool cmdFormat) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ListContent()

char * FunctionList::ListContent ( bool  cmdFormat) const
private

Definition at line 193 of file functionlist.cpp.

References CharBuffer::Append(), buf, CharBuffer::Empty(), CharBuffer::EnsureSize(), first, UserFunction::GetExpression(), UserFunction::GetName(), Variable::GetName(), CharBuffer::GetString(), ExpressionNode::GetText(), UserFunction::GetVariable(), UserFunction::Next, and StrLen().

Referenced by List(), and ListDefinitions().

194 {
195  if (first == nullptr)
196  {
197  return static_cast<char*>(cmdFormat ? nullptr : HELPFUNCNDEF);
198  }
199 
200  int len = 0;
201  UserFunction* current = first;
202 
203  while (current != nullptr)
204  {
205  len += StrLen(current->GetName());
206  len += 1;
207  len += StrLen(current->GetVariable()->GetName());
208  len += 2;
209  len += StrLen(current->GetExpression()->GetText());
210  len += cmdFormat ? 2 : 1;
211  current = current->Next;
212  }
213 
214  buf->Empty();
215  buf->EnsureSize(len + StrLen(NEWLINE) + 1);
216 
217  current = first;
218  while (current != nullptr)
219  {
220  buf->Append(current->GetName());
221  buf->Append("(");
222  buf->Append(current->GetVariable()->GetName());
223  buf->Append(")=");
224  buf->Append(current->GetExpression()->GetText());
225 
226  if (cmdFormat)
227  {
228  buf->Append(';');
229  }
230 
231  buf->Append(NEWLINE);
232  current = current->Next;
233  }
234 
235  return buf->GetString();
236 }
#define NEWLINE
Definition: amath.h:222
UserFunction * Next
Definition: userfunction.h:51
void Empty()
Definition: charbuf.cpp:218
ExpressionNode * GetExpression() const
char * GetString() const
Definition: charbuf.cpp:306
CharBuffer * buf
Definition: functionlist.h:61
#define HELPFUNCNDEF
Definition: text.h:92
void Append(const char *source)
Definition: charbuf.cpp:262
#define nullptr
Definition: amath.h:116
char * GetName() const
Definition: values.cpp:69
Variable * GetVariable() const
char * GetName() const
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
virtual char * GetText()=0
A user defined function.
Definition: userfunction.h:44
UserFunction * first
Definition: functionlist.h:62
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:
Here is the caller graph for this function:

◆ ListDefinitions()

char * FunctionList::ListDefinitions ( ) const

Definition at line 188 of file functionlist.cpp.

References ListContent().

Referenced by SaveStatement::Execute().

189 {
190  return ListContent(true);
191 }
char * ListContent(bool cmdFormat) const
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ buf

CharBuffer* FunctionList::buf
private

Definition at line 61 of file functionlist.h.

Referenced by Clear(), FunctionList(), ListContent(), and ~FunctionList().

◆ first

UserFunction* FunctionList::first
private

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