Isis 3 Programmer Reference
InlineInfixToPostfix.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include "InlineInfixToPostfix.h"
9 
10 // Qt library
11 #include <QString>
12 #include <QVector>
13 
14 // other ISIS
15 #include "IException.h"
16 #include "IString.h"
17 
18 namespace Isis {
25  initialize();
26  }
27 
28 
33  }
34 
35 
44  bool InlineInfixToPostfix::isKnownSymbol(QString representation) {
45 
46  if ( representation.isEmpty() ) return (false);
47 
48  // not an empty string
49  if ( InfixToPostfix::isKnownSymbol(representation) ) {
50  return (true);
51  }
52 
53  // not known symbol from parent class
54  if ( isScalar(representation) ) {
55  return (false);
56  }
57 
58  // not scalar, the only thing left is a variable...
59  return isVariable(representation);
60  }
61 
62 
82  try {
83  return InfixToPostfix::findOperator(token);
84  }
85  catch(IException &e) {
86  if ( isVariable(token) ) {
87  // If the token is not a scalar, the isVariable() method will assume it's a variable.
88  // Then the token is added it to the variables list and an InfixFunction from this token
89  // is added to the operators.
90  // Now that is has been added to the operators list, we can call findOperator() again and
91  // should find this representation in the list.
92  return (findOperator(token));
93  }
94  else {
95  QString msg = "The token '" + token
96  + "' is not recognized as an operator, function or variable.";
97  throw IException(e, IException::User, msg, _FILEINFO_);
98  }
99  }
100 
101  }
102 
103 
109  p_operators.push_back(new InfixOperator(1, "%"));
110  p_operators.push_back(new InfixFunction("mod", 2));
111  p_operators.push_back(new InfixFunction("fmod", 2));
112 
113  p_operators.push_back(new InfixOperator(1, "&"));
114  p_operators.push_back(new InfixOperator(1, "and"));
115 
116  p_operators.push_back(new InfixOperator(1, "&&"));
117 
118  p_operators.push_back(new InfixOperator(1, "|"));
119  p_operators.push_back(new InfixOperator(1, "or"));
120 
121  p_operators.push_back(new InfixOperator(1, "||"));
122  return;
123  }
124 
125 
136  bool InlineInfixToPostfix::exists(const QString &token) {
137  return (m_variables.contains(token, Qt::CaseInsensitive));
138  }
139 
140 
149  bool InlineInfixToPostfix::isScalar(const QString &token) {
150 
151  // NOTE: The following checks are commented out due to redundancy
152  // This is a private method only called from isKnownSymbol()
153  // and all of these conditions have already been checked.
154  // if (token.isEmpty()) return (false);
155  // if (InfixToPostfix::isKnownSymbol(token)) return (false);
156 
157  try {
158  Isis::toDouble(token);
159  return (true);
160  }
161  catch (IException &) {
162  return (false);
163  }
164  }
165 
166 
178  bool InlineInfixToPostfix::isVariable(const QString &token) {
179 
180  // NOTE: The following checks are commented out due to redundancy
181  // This is a private method only called from isKnownSymbol()
182  // and findOperator().
183  // All of these conditions have already been checked for
184  // isKnownSymbol().
185  // if (isScalar(token)) return (false);
186  // if (InfixToPostfix::isKnownSymbol(token)) return (false);
187 
188  if (token.isEmpty()) return (false);
189 
190  m_variables.push_back(token);
191  p_operators.push_back(new InfixFunction(token, 0));
192  return (true);
193  }
194 
195 } // Namespace Isis
196 
Isis::InlineInfixToPostfix::isVariable
bool isVariable(const QString &str)
Determines whether the given token is a variable and, if so, appends it to the list of variables.
Definition: InlineInfixToPostfix.cpp:178
Isis::InlineInfixToPostfix::InlineInfixToPostfix
InlineInfixToPostfix()
Constructs an InlineInfixToPostfix object.
Definition: InlineInfixToPostfix.cpp:24
Isis::InfixToPostfix::findOperator
virtual InfixOperator * findOperator(QString representation)
This method will return a pointer to the operator represented by 'representation.
Definition: InfixToPostfix.cpp:347
Isis::InfixFunction
InfixOperator and InfixFunction are helper classes for InfixToPostfix.
Definition: InfixToPostfix.h:130
Isis::InlineInfixToPostfix::m_variables
QStringList m_variables
The list of variables (represented as strings).
Definition: InlineInfixToPostfix.h:50
Isis::InlineInfixToPostfix::exists
bool exists(const QString &str)
Determines whether the given string exists as a recognized variable.
Definition: InlineInfixToPostfix.cpp:136
Isis::InlineInfixToPostfix::~InlineInfixToPostfix
virtual ~InlineInfixToPostfix()
Destroys the InlineInfixToPostfix object.
Definition: InlineInfixToPostfix.cpp:32
Isis::InlineInfixToPostfix::isKnownSymbol
virtual bool isKnownSymbol(QString representation)
This method attempts to verify that the given argument is recognized as a valid function,...
Definition: InlineInfixToPostfix.cpp:44
Isis::InfixOperator
InfixOperator and InfixFunction are helper classes for InfixToPostfix.
Definition: InfixToPostfix.h:81
Isis::InlineInfixToPostfix::initialize
void initialize()
Adds several infix operators and functions to the operator list that are not already recognized by th...
Definition: InlineInfixToPostfix.cpp:108
Isis::InfixToPostfix
Converter for math equations.
Definition: InfixToPostfix.h:46
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::toDouble
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:149
Isis::InlineInfixToPostfix::isScalar
bool isScalar(const QString &scalar)
Determines whether the given token represents a scalar value (i.e.
Definition: InlineInfixToPostfix.cpp:149
Isis::InfixToPostfix::isKnownSymbol
virtual bool isKnownSymbol(QString representation)
This method will return true if it believes the argument represents a valid function or operator.
Definition: InfixToPostfix.cpp:253
Isis::InlineInfixToPostfix::findOperator
virtual InfixOperator * findOperator(QString element)
This method will first search the recognized list of operators and functions for the given token.
Definition: InlineInfixToPostfix.cpp:81
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::IException::User
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition: IException.h:126