amath  1.8.5
Simple command line calculator
nodes.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 "amath.h"
31 #include "amathc.h"
32 #include "nodes.h"
33 #include "lib/numb.h"
34 #include "lib/charbuf.h"
35 #include "loc/text.h"
36 #include "system/program.h"
37 
38 // -----------------------------------------------------
39 // ------------------- SyntaxNode ----------------------
40 // -----------------------------------------------------
41 
43 {
44  output = new CharBuffer();
45  parent = nullptr;
46  iterator = nullptr;
47  leftBottom = false;
48 }
49 
51 {
52  delete output;
53 }
54 
55 ReductionType SyntaxNode::GetReductionType()
56 {
57  return nonereduc;
58 }
59 
60 bool SyntaxNode::GetFirstNode() const
61 {
62  return leftBottom;
63 }
64 
66 {
67  leftBottom = true;
68 }
69 
71 {
72  return parent;
73 }
74 
76 {
77  parent = node;
78 }
79 
81 {
82  iterator = nullptr;
83 }
84 
85 // -----------------------------------------------------
86 // ------------------ ExpressionNode -------------------
87 // -----------------------------------------------------
88 
91 {
92  result = nullptr;
93 }
94 
97 {
98  result = value;
99 }
100 
102 {
103  if (result != nullptr)
104  {
105  delete result;
106  }
107 }
108 
110 {
111  return expression;
112 }
113 
115 {
116  return false;
117 }
118 
120 {
121  return GetNodeText();
122 }
123 
125 {
126  return nullptr;
127 }
128 
129 // -----------------------------------------------------
130 // -------------------- ErrorNode ----------------------
131 // -----------------------------------------------------
132 
133 ErrorNode::ErrorNode(const char* input, const char* message, const char* parameter, int pos) :
134  SyntaxNode(), pos(pos)
135 {
136  this->input = new char[StrLen(input) + 1];
137  StrCopyVisible(this->input, input);
138 
139  CharBuffer* temp = new CharBuffer();
140  temp->EnsureSize(StrLen(message) + StrLen(parameter) + 1);
141  temp->Append(message);
142  temp->Append(parameter);
144  delete temp;
145 }
146 
147 ErrorNode::ErrorNode(const char* input, int pos) :
148  SyntaxNode(), pos(pos)
149 {
150  char* temp = HELPSYNTAX;
151  this->input = new char[StrLen(input) + 1];
152  StrCopyVisible(this->input, input);
153  AllocAndCopy(&this->message, temp);
154 }
155 
157 {
158  delete [] input;
159  delete [] message;
160 }
161 
163 {
164  return othernodetype;
165 }
166 
168 {
169  static char* ret = (char*)"ERRND";
170  return ret;
171 }
172 
174 {
182  output->Append('^');
184 
185  return output->GetString();
186 }
187 
188 void ErrorNode::StrCopyVisible(char* destination, const char* source)
189 {
190  while (*source != '\0' && *source >= 32 && *source <= 126)
191  {
192  *destination++ = *source++;
193  }
194 
195  *destination = '\0';
196 }
197 
199 {
200  return nullptr;
201 }
202 
204 {
205 }
206 
208 {
209 }
210 
212 {
213 }
214 
215 // -----------------------------------------------------
216 // ----------------- StatementBlockNode ----------------
217 // -----------------------------------------------------
218 
221 {
222  first = nullptr;
223  iterator = nullptr;
224 }
225 
227 {
229 
230  while (e != nullptr)
231  {
232  StatementBlockElement* next = e->next;
233  delete e->statement;
234  delete e;
235  e = next;
236  }
237 }
238 
240 {
241  return statement;
242 }
243 
245 {
246  if (first == nullptr)
247  {
249  first->statement = node;
250  first->next = nullptr;
251  return;
252  }
253 
256 
257  while (current != nullptr)
258  {
259  current = current->next;
260  last = last->next;
261  }
262 
263  current = new StatementBlockElement();
264  current->statement = node;
265  current->next = nullptr;
266  last->next = current;
267 }
268 
270 {
271  static char* ret = (char*)"SBLCK";
272  return ret;
273 }
274 
276 {
279 
281  while (e != nullptr)
282  {
283  const char* out = e->statement->Execute();
285  output->Append(out);
286  e = e->next;
287  }
288 
289  return output->GetString();
290 }
291 
293 {
294  // TODO: Implement GetNext in StatementBlockNode
295  return nullptr;
296 }
297 
299 {
300 }
301 
303 {
304  // TODO: Implement Detach in StatementBlockNode
305 }
306 
308 {
309  // TODO: Implement Replace in StatementBlockNode
310 }
ErrorNode(const char *input, const char *message, const char *parameter, int pos)
Definition: nodes.cpp:133
void Append(const char c)
Definition: charbuf.cpp:245
#define NEWLINE
Definition: amath.h:222
void Add(SyntaxNode *node)
Definition: nodes.cpp:244
StatementBlockElement * first
Definition: nodes.h:167
char * GetTextCode()
Definition: nodes.cpp:269
void Empty()
Definition: charbuf.cpp:218
virtual ~ErrorNode()
Definition: nodes.cpp:156
virtual ReductionType GetReductionType()
Definition: nodes.cpp:55
void Attach(SyntaxNode *node)
Definition: nodes.cpp:203
Used to create a linked list of statements.
Definition: nodes.h:174
char * GetString() const
Definition: charbuf.cpp:306
void Replace(SyntaxNode *n, SyntaxNode *x)
Definition: nodes.cpp:211
A sequence of statements in a syntax tree.
Definition: nodes.h:151
SyntaxNode()
Definition: nodes.cpp:42
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
ExpressionNode()
Definition: nodes.cpp:89
NodeType GetNodeType()
Definition: nodes.cpp:239
void Attach(SyntaxNode *node)
Definition: nodes.cpp:298
virtual char * GetNodeText()=0
void ClearBuffer()
Release memory in buffer.
Definition: charbuf.cpp:65
char * input
Definition: nodes.h:141
void Append(const char *source)
Definition: charbuf.cpp:262
Definition: numb.h:66
bool leftBottom
Definition: nodes.h:88
char * Execute()
Definition: nodes.cpp:124
CharBuffer()
Initialize without allocating memory.
Definition: charbuf.cpp:38
bool GetFirstNode() const
Definition: nodes.cpp:60
ExpressionNode(Number *value)
Definition: nodes.cpp:95
virtual ~ExpressionNode()
Definition: nodes.cpp:101
SyntaxNode * parent
Definition: nodes.h:86
virtual char * Execute()=0
SyntaxNode * statement
Definition: nodes.h:176
NodeType GetNodeType()
Definition: nodes.cpp:162
NodeType GetNodeType()
Definition: nodes.cpp:109
char * GetTextCode()
Definition: nodes.cpp:119
void SetFirstNode()
Definition: nodes.cpp:65
void SetParent(SyntaxNode *node)
Definition: nodes.cpp:75
virtual void ResetIterator()
Definition: nodes.cpp:80
void Detach(SyntaxNode *node)
Definition: nodes.cpp:207
SyntaxNode * GetNext()
Definition: nodes.cpp:292
ErrorNode(const char *input, int pos)
Definition: nodes.cpp:147
SyntaxNode * GetParent() const
Definition: nodes.cpp:70
Number * result
Definition: nodes.h:116
SyntaxNode * GetNext()
Definition: nodes.cpp:198
void EnsureGrowth(unsigned int size)
Definition: charbuf.cpp:169
virtual ~SyntaxNode()
Definition: nodes.cpp:50
Represents an error message encapsulated in a syntax tree.
Definition: nodes.h:123
#define HELPSYNTAX
Definition: text.h:78
int pos
Definition: nodes.h:142
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
static void StrCopyVisible(char *destination, const char *source)
Definition: nodes.cpp:188
void Replace(SyntaxNode *n, SyntaxNode *x)
Definition: nodes.cpp:307
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
char * GetTextCode()
Definition: nodes.cpp:167
void Append(const char c, unsigned int count)
Definition: charbuf.cpp:250
void Detach(SyntaxNode *node)
Definition: nodes.cpp:302
SyntaxNode * iterator
Definition: nodes.h:87
char * Execute()
Definition: nodes.cpp:275
char * Execute()
Definition: nodes.cpp:173
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:44
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
StatementBlockElement * next
Definition: nodes.h:177
char * message
Definition: nodes.h:140
void ClearAndAlloc(unsigned int size)
Release memory and allocate new size.
Definition: charbuf.cpp:92
virtual bool IsSilent()
Definition: nodes.cpp:114