#include "Singleton.hxx"
#include <algorithm>
-#include <functional>
#include <map>
#include <utility>
#include <string>
#include <sstream>
#include <boost/bind.hpp>
+#include <cstring> // for strcmp
#include <simgear/props/props.hxx>
template<typename T>
static SGExpression<T>*
-SGReadIExpression(SGPropertyNode *inputRoot, const SGPropertyNode *expression);
+SGReadExpression(SGPropertyNode *inputRoot, const SGPropertyNode *expression);
template<typename T>
static bool
{
for (int i = 0; i < expression->nChildren(); ++i) {
SGExpression<T>* inputExpression;
- inputExpression = SGReadIExpression<T>(inputRoot, expression->getChild(i));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(i));
if (!inputExpression)
return false;
nary->addOperand(inputExpression);
template<typename T>
static SGExpression<T>*
-SGReadIExpression(SGPropertyNode *inputRoot, const SGPropertyNode *expression)
+SGReadExpression(SGPropertyNode *inputRoot, const SGPropertyNode *expression)
{
if (!expression)
return 0;
std::string name = expression->getName();
+
if (name == "value") {
T value;
if (!SGReadValueFromContent(expression, value)) {
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadIExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadIExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
SGSharedPtr<SGExpression<T> > inputExpression;
for (int i = 0; !inputExpression && i < expression->nChildren(); ++i)
- inputExpression = SGReadIExpression<T>(inputRoot, expression->getChild(i));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(i));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpressions[2] = {
- SGReadIExpression<T>(inputRoot, expression->getChild(0)),
- SGReadIExpression<T>(inputRoot, expression->getChild(1))
+ SGReadExpression<T>(inputRoot, expression->getChild(0)),
+ SGReadExpression<T>(inputRoot, expression->getChild(1))
};
if (!inputExpressions[0] || !inputExpressions[1]) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpressions[2] = {
- SGReadIExpression<T>(inputRoot, expression->getChild(0)),
- SGReadIExpression<T>(inputRoot, expression->getChild(1))
+ SGReadExpression<T>(inputRoot, expression->getChild(0)),
+ SGReadExpression<T>(inputRoot, expression->getChild(1))
};
if (!inputExpressions[0] || !inputExpressions[1]) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
}
return output;
}
+
+ if (name == "difference" || name == "dif" ) {
+ if (expression->nChildren() < 1) {
+ SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
+ return 0;
+ }
+ SGDifferenceExpression<T>* output = new SGDifferenceExpression<T>;
+ if (!SGReadNaryOperands(output, inputRoot, expression)) {
+ delete output;
+ SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
+ return 0;
+ }
+ return output;
+ }
+
if (name == "prod" || name == "product") {
if (expression->nChildren() < 1) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return output;
}
- return 0;
-}
-
-
-template<typename T>
-static SGExpression<T>*
-SGReadFExpression(SGPropertyNode *inputRoot, const SGPropertyNode *expression)
-{
- SGExpression<T>* r = SGReadIExpression<T>(inputRoot, expression);
- if (r)
- return r;
-
- if (!expression)
- return 0;
+ if (name == "table") {
+ SGInterpTable* tab = new SGInterpTable(expression);
+ if (!tab) {
+ SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression: malformed table");
+ return 0;
+ }
+
+ // find input expression - i.e a child not named 'entry'
+ const SGPropertyNode* inputNode = NULL;
+ for (int i=0; (i<expression->nChildren()) && !inputNode; ++i) {
+ if (strcmp(expression->getChild(i)->getName(), "entry") == 0) {
+ continue;
+ }
+
+ inputNode = expression->getChild(i);
+ }
+
+ if (!inputNode) {
+ SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression: no input found");
+ return 0;
+ }
- std::string name = expression->getName();
+ SGSharedPtr<SGExpression<T> > inputExpression;
+ inputExpression = SGReadExpression<T>(inputRoot, inputNode);
+ if (!inputExpression) {
+ SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
+ return 0;
+ }
+
+ return new SGInterpTableExpression<T>(inputExpression, tab);
+ }
+
if (name == "acos") {
if (expression->nChildren() != 1) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpression;
- inputExpression = SGReadFExpression<T>(inputRoot, expression->getChild(0));
+ inputExpression = SGReadExpression<T>(inputRoot, expression->getChild(0));
if (!inputExpression) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
}
return new SGTanhExpression<T>(inputExpression);
}
-
-// if (name == "table") {
-// }
+
// if (name == "step") {
// }
// if (name == "condition") {
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpressions[2] = {
- SGReadFExpression<T>(inputRoot, expression->getChild(0)),
- SGReadFExpression<T>(inputRoot, expression->getChild(1))
+ SGReadExpression<T>(inputRoot, expression->getChild(0)),
+ SGReadExpression<T>(inputRoot, expression->getChild(1))
};
if (!inputExpressions[0] || !inputExpressions[1]) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpressions[2] = {
- SGReadFExpression<T>(inputRoot, expression->getChild(0)),
- SGReadFExpression<T>(inputRoot, expression->getChild(1))
+ SGReadExpression<T>(inputRoot, expression->getChild(0)),
+ SGReadExpression<T>(inputRoot, expression->getChild(1))
};
if (!inputExpressions[0] || !inputExpressions[1]) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpressions[2] = {
- SGReadFExpression<T>(inputRoot, expression->getChild(0)),
- SGReadFExpression<T>(inputRoot, expression->getChild(1))
+ SGReadExpression<T>(inputRoot, expression->getChild(0)),
+ SGReadExpression<T>(inputRoot, expression->getChild(1))
};
if (!inputExpressions[0] || !inputExpressions[1]) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
return 0;
}
SGSharedPtr<SGExpression<T> > inputExpressions[2] = {
- SGReadIExpression<T>(inputRoot, expression->getChild(0)),
- SGReadIExpression<T>(inputRoot, expression->getChild(1))
+ SGReadExpression<T>(inputRoot, expression->getChild(0)),
+ SGReadExpression<T>(inputRoot, expression->getChild(1))
};
if (!inputExpressions[0] || !inputExpressions[1]) {
SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
SGExpression<int>*
SGReadIntExpression(SGPropertyNode *inputRoot,
const SGPropertyNode *configNode)
-{ return SGReadIExpression<int>(inputRoot, configNode); }
+{ return SGReadExpression<int>(inputRoot, configNode); }
SGExpression<float>*
SGReadFloatExpression(SGPropertyNode *inputRoot,
const SGPropertyNode *configNode)
-{ return SGReadFExpression<float>(inputRoot, configNode); }
+{ return SGReadExpression<float>(inputRoot, configNode); }
SGExpression<double>*
SGReadDoubleExpression(SGPropertyNode *inputRoot,
const SGPropertyNode *configNode)
-{ return SGReadFExpression<double>(inputRoot, configNode); }
+{ return SGReadExpression<double>(inputRoot, configNode); }
// SGExpression<bool>*
// SGReadBoolExpression(SGPropertyNode *inputRoot,
parser->readChildren(exp, children);
vector<Expression*>::iterator notBool =
find_if(children.begin(), children.end(),
- bind(&Expression::getType, _1) != BOOL);
+ boost::bind(&Expression::getType, _1) != BOOL);
if (notBool != children.end())
throw("non boolean operand to logical expression");
Logicop *expr = new Logicop;
ExpParserRegistrar andRegistrar("and", logicopParser<AndExpression>);
ExpParserRegistrar orRegistrar("or", logicopParser<OrExpression>);
-int BindingLayout::addBinding(const string& name, Type type)
+size_t BindingLayout::addBinding(const string& name, Type type)
{
//XXX error checkint
vector<VariableBinding>::iterator itr
= find_if(bindings.begin(), bindings.end(),
- bind(&VariableBinding::name, _1) == name);
+ boost::bind(&VariableBinding::name, _1) == name);
if (itr != bindings.end())
return itr->location;
- int result = bindings.size();
+ size_t result = bindings.size();
bindings.push_back(VariableBinding(name, type, bindings.size()));
return result;
}
using namespace boost;
vector<VariableBinding>::const_iterator itr
= find_if(bindings.begin(), bindings.end(),
- bind(&VariableBinding::name, _1) == name);
+ boost::bind(&VariableBinding::name, _1) == name);
if (itr != bindings.end()) {
result = *itr;
return true;