#include <stdexcept>
+#include <msp/strings/format.h>
#include "booleanevaluator.h"
using namespace std;
+using namespace Msp;
-/* I'd rather have overloads with different slots, but that creates an
-ambiguity because slots have template constructors. */
-BooleanEvaluator::BooleanEvaluator(const Function &f, bool allow_compare):
+BooleanEvaluator::BooleanEvaluator(const ValueFunction &f):
+ func([&f](const string &value, const string *){ return f(value); }),
+ ops("&|!")
+{ }
+
+BooleanEvaluator::BooleanEvaluator(const CompareFunction &f):
func(f),
- ops(allow_compare ? "&|!=^" : "&|!")
+ ops("&|!=^")
{ }
bool BooleanEvaluator::evaluate(const string &str)
else if(ops.find(*i)!=string::npos)
push_op(*i);
else
- throw runtime_error("syntax error at "+string(1, *i));
+ throw runtime_error(format("syntax error at %c", *i));
}
}
void BooleanEvaluator::push_op(char op)
{
if(last_was_op!=is_unary(op))
- throw runtime_error("syntax error at "+string(1, op));
+ throw runtime_error(format("syntax error at %c", op));
// TODO Disallow mixing of ! and =/^
if(is_logic(op) && !var_stack.empty())
value_stack.push_back(pop_value());