amath  1.8.5
Simple command line calculator
values.h
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 #ifndef AMATH_VALUES_H
31 #define AMATH_VALUES_H
32 
33 /**
34  * @file values.h
35  * @brief Application logic tied to numbers and values.
36  *
37  */
38 
39 #include "nodes.h"
40 #include "lib/numb.h"
41 #include "statement/node.h"
42 
43 class Variable;
44 
45 /**
46  * @brief A list of user defined variables.
47  *
48  */
50 {
51 public:
52  VariableList();
53  ~VariableList();
54 
55  void Clear();
56  bool Delete(const char* name);
57  Variable* GetFirstVariable() const;
58  Variable* GetVariable(const char* name) const;
59  Variable* CreateVariable(const char* name);
62 
63  char* List() const;
64  char* ListDefinitions() const;
65 
66 private:
67  char* ListContent(bool cmdFormat) const;
70 };
71 
72 /**
73  * @brief A user defined variable.
74  *
75  */
76 class Variable
77 {
78 public:
79  explicit Variable(const char* name);
80  ~Variable();
81 
82  char* GetName() const;
83  Number* GetValue() const;
84  void AssignValue(Number* value);
85 
87 
88 private:
89  char* name;
92  friend bool VariableList::Delete(const char* name);
93 };
94 
95 /**
96  * @brief Statement to list all user defined variables.
97  *
98  */
99 class ListVariablesStatement : public virtual StatementNode
100 {
101 public:
103  char* Execute();
104 };
105 
106 /**
107  * @brief Use of a variable in a syntax tree.
108  *
109  */
111 {
112 public:
113  explicit VariableNode(Variable* variable);
114  char* GetText();
115  Number* Evaluate();
116  void AssignValue(Number* value) const;
117  int GetPrecedence();
118  SyntaxNode* GetNext();
119  void Attach(SyntaxNode* node);
120  void Detach(SyntaxNode* node);
121  void Replace(SyntaxNode* n, SyntaxNode* x);
122 
123 protected:
124  char* GetNodeText();
125 
126 private:
128 };
129 
130 
131 /**
132  * @brief Use of last result in a syntax tree.
133  *
134  */
136 {
137 public:
138  InsVariableNode();
139  char* GetText();
140  Number* Evaluate();
141 
142 protected:
143  char* GetNodeText();
144 };
145 
146 /**
147  * @brief Use of a numeric value in a syntax tree.
148  *
149  */
151 {
152 public:
154  explicit NumericValueNode(Number* value);
155 
156  virtual ReductionType GetReductionType();
157  int GetPrecedence();
158  char* GetText();
159  SyntaxNode* GetNext();
160  Number* Evaluate();
161  void Attach(SyntaxNode* node);
162  void Detach(SyntaxNode* node);
163  void Replace(SyntaxNode* n, SyntaxNode* x);
164  void ReplaceWith(Number* value);
165 
166 protected:
167  char* GetNodeText();
168 };
169 
170 /**
171  * @brief Use of Euler's number in a syntax tree.
172  *
173  */
175 {
176 public:
178  ReductionType GetReductionType();
179 
180 protected:
181  char* GetNodeText();
182 };
183 
184 /**
185  * @brief Use of PI in a syntax tree.
186  *
187  */
188 class PiNode : public NumericValueNode
189 {
190 public:
191  PiNode();
192  ReductionType GetReductionType();
193 
194 protected:
195  char* GetNodeText();
196 };
197 
198 /**
199  * @brief Use of the square root of -2 in a syntax tree.
200  *
201  */
203 {
204 public:
205  ComplexiNode();
206  ReductionType GetReductionType();
207 
208 protected:
209  char* GetNodeText();
210 };
211 
212 #endif
PiNode()
Definition: values.cpp:457
Use of the square root of -2 in a syntax tree.
Definition: values.h:202
void Attach(SyntaxNode *node)
Definition: values.cpp:415
virtual ReductionType GetReductionType()
Definition: values.cpp:381
bool chainDelete
Definition: values.h:91
ReductionType GetReductionType()
Definition: values.cpp:482
CharBuffer * buf
Definition: values.h:68
Number * GetValue() const
Definition: values.cpp:74
A user defined variable.
Definition: values.h:76
void Detach(SyntaxNode *node)
Definition: values.cpp:333
~Variable()
Definition: values.cpp:54
Base class for all statements in a syntax tree.
Definition: node.h:40
char * GetText()
Definition: values.cpp:398
Variable * variable
Definition: values.h:127
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
NumericValueNode(Number *value)
Definition: values.cpp:376
char * GetNodeText()
Definition: values.cpp:319
void Replace(SyntaxNode *n, SyntaxNode *x)
Definition: values.cpp:337
Variable(const char *name)
Definition: values.cpp:46
void AssignValue(Number *value)
Definition: values.cpp:79
Use of PI in a syntax tree.
Definition: values.h:188
Variable * InsertTemporaryVariable(Variable *variable)
Definition: values.cpp:200
char * GetText()
Definition: values.cpp:314
Definition: numb.h:66
Variable * GetVariable(const char *name) const
Definition: values.cpp:160
A list of user defined variables.
Definition: values.h:49
char * ListDefinitions() const
Definition: values.cpp:223
void AssignValue(Number *value) const
Definition: values.cpp:309
Number * value
Definition: values.h:90
void Detach(SyntaxNode *node)
Definition: values.cpp:419
VariableNode(Variable *variable)
Definition: values.cpp:293
char * GetName() const
Definition: values.cpp:69
ReductionType GetReductionType()
Definition: values.cpp:442
char * GetNodeText()
Definition: values.cpp:467
Variable * CreateVariable(const char *name)
Definition: values.cpp:172
char * GetNodeText()
Definition: values.cpp:403
char * GetNodeText()
Definition: values.cpp:447
void Clear()
Definition: values.cpp:109
Variable * GetFirstVariable() const
Definition: values.cpp:155
int GetPrecedence()
Definition: values.cpp:386
char * GetNodeText()
Definition: values.cpp:361
void Attach(SyntaxNode *node)
Definition: values.cpp:329
void ReplaceWith(Number *value)
Definition: values.cpp:427
Number * Evaluate()
Definition: values.cpp:393
Statement to list all user defined variables.
Definition: values.h:99
SyntaxNode * GetNext()
Definition: values.cpp:410
int GetPrecedence()
Definition: values.cpp:298
Use of a variable in a syntax tree.
Definition: values.h:110
Use of a numeric value in a syntax tree.
Definition: values.h:150
char * name
Definition: values.h:89
Number * Evaluate()
Definition: values.cpp:303
Number * Evaluate()
Definition: values.cpp:350
SyntaxNode * GetNext()
Definition: values.cpp:324
~VariableList()
Definition: values.cpp:99
void RemoveTemporaryVariable()
Definition: values.cpp:210
Variable * first
Definition: values.h:69
char * List() const
Definition: values.cpp:218
char * GetText()
Definition: values.cpp:355
void Replace(SyntaxNode *n, SyntaxNode *x)
Definition: values.cpp:423
char * GetNodeText()
Definition: values.cpp:487
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
char * ListContent(bool cmdFormat) const
Definition: values.cpp:228
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:44
ReductionType GetReductionType()
Definition: values.cpp:462
friend bool VariableList::Delete(const char *name)
VariableList()
Definition: values.cpp:93
Variable * Next
Definition: values.h:86
Use of Euler&#39;s number in a syntax tree.
Definition: values.h:174
Use of last result in a syntax tree.
Definition: values.h:135