amath  1.8.5
Simple command line calculator
nodes.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_NODES_H
31 #define AMATH_NODES_H
32 
33 /**
34  * @file nodes.h
35  * @brief Top nodes in syntax trees.
36  *
37  */
38 
39 #include "lib/numb.h"
40 #include "lib/charbuf.h"
41 
42 typedef enum
43 {
47 } NodeType;
48 
49 typedef enum
50 {
56 } ReductionType;
57 
58 /**
59 * @brief Base class for all nodes in a syntax tree.
60 * @details
61 * More info is available at Wikipedia:
62 * https://wikipedia.org/wiki/Abstract_syntax_tree
63 *
64 */
66 {
67 public:
68  SyntaxNode();
69  virtual ~SyntaxNode();
70  void SetFirstNode();
71  bool GetFirstNode() const;
72  SyntaxNode* GetParent() const;
73  void SetParent(SyntaxNode* node);
74  virtual NodeType GetNodeType() = 0;
75  virtual ReductionType GetReductionType();
76  virtual void ResetIterator();
77  virtual SyntaxNode* GetNext() = 0;
78  virtual char* GetTextCode() = 0;
79  virtual char* Execute() = 0;
80  virtual void Attach(SyntaxNode* node) = 0;
81  virtual void Detach(SyntaxNode* node) = 0;
82  virtual void Replace(SyntaxNode* n, SyntaxNode* x) = 0;
83 
84 protected:
88  bool leftBottom;
89 };
90 
91 /**
92 * @brief Base class for all nodes related to mathematical expressions.
93 * @details
94 * Nodes of this type is used to build trees representing an mathematical
95 * expressions. Each node in the tree consist of a number, an operator or
96 * any other element in an expression.
97 *
98 */
99 class ExpressionNode : public SyntaxNode
100 {
101 public:
102  ExpressionNode();
103  explicit ExpressionNode(Number* value);
104  virtual ~ExpressionNode();
105 
106  NodeType GetNodeType();
107  virtual bool IsSilent();
108  virtual char* GetText() = 0;
109  virtual Number* Evaluate() = 0;
110  virtual int GetPrecedence() = 0;
111  char* GetTextCode();
112  char* Execute();
113 
114 protected:
115  virtual char* GetNodeText() = 0;
117 };
118 
119 /**
120 * @brief Represents an error message encapsulated in a syntax tree.
121 *
122 */
123 class ErrorNode : public virtual SyntaxNode
124 {
125 public:
126  ErrorNode(const char* input, int pos);
127  ErrorNode(const char* input, const char* message, const char* parameter, int pos);
128  virtual ~ErrorNode();
129 
130  NodeType GetNodeType();
131  SyntaxNode* GetNext();
132  char* GetTextCode();
133  char* Execute();
134  void Attach(SyntaxNode* node);
135  void Detach(SyntaxNode* node);
136  void Replace(SyntaxNode* n, SyntaxNode* x);
137 
138 private:
139  static void StrCopyVisible(char* destination, const char* source);
140  char* message;
141  char* input;
142  int pos;
143 };
144 
145 struct StatementBlockElement;
146 
147 /**
148 * @brief A sequence of statements in a syntax tree.
149 *
150 */
151 class StatementBlockNode : public virtual SyntaxNode
152 {
153 public:
156 
157  NodeType GetNodeType();
158  SyntaxNode* GetNext();
159  void Add(SyntaxNode* node);
160  char* GetTextCode();
161  char* Execute();
162  void Attach(SyntaxNode* node);
163  void Detach(SyntaxNode* node);
164  void Replace(SyntaxNode* n, SyntaxNode* x);
165 
166 private:
168 };
169 
170 /**
171 * @brief Used to create a linked list of statements.
172 *
173 */
175 {
178 };
179 
180 #endif
ErrorNode(const char *input, const char *message, const char *parameter, int pos)
Definition: nodes.cpp:133
void Add(SyntaxNode *node)
Definition: nodes.cpp:244
StatementBlockElement * first
Definition: nodes.h:167
char * GetTextCode()
Definition: nodes.cpp:269
virtual ~ErrorNode()
Definition: nodes.cpp:156
virtual ReductionType GetReductionType()
Definition: nodes.cpp:55
void Attach(SyntaxNode *node)
Definition: nodes.cpp:203
virtual void Detach(SyntaxNode *node)=0
Used to create a linked list of statements.
Definition: nodes.h:174
void Replace(SyntaxNode *n, SyntaxNode *x)
Definition: nodes.cpp:211
A sequence of statements in a syntax tree.
Definition: nodes.h:151
virtual void Attach(SyntaxNode *node)=0
SyntaxNode()
Definition: nodes.cpp:42
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
ExpressionNode()
Definition: nodes.cpp:89
virtual char * GetTextCode()=0
NodeType GetNodeType()
Definition: nodes.cpp:239
virtual void Replace(SyntaxNode *n, SyntaxNode *x)=0
void Attach(SyntaxNode *node)
Definition: nodes.cpp:298
virtual char * GetNodeText()=0
char * input
Definition: nodes.h:141
Definition: numb.h:66
bool leftBottom
Definition: nodes.h:88
char * Execute()
Definition: nodes.cpp:124
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
virtual NodeType GetNodeType()=0
virtual Number * Evaluate()=0
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
virtual int GetPrecedence()=0
virtual ~SyntaxNode()
Definition: nodes.cpp:50
Represents an error message encapsulated in a syntax tree.
Definition: nodes.h:123
int pos
Definition: nodes.h:142
virtual char * GetText()=0
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
virtual SyntaxNode * GetNext()=0
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
char * GetTextCode()
Definition: nodes.cpp:167
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
StatementBlockElement * next
Definition: nodes.h:177
char * message
Definition: nodes.h:140
virtual bool IsSilent()
Definition: nodes.cpp:114