Init
This commit is contained in:
commit
950e7b7e2d
350 files changed
+38599
No files matched your search
Executable
+94
@@ -0,0 +1,94 @@
|
||||
#include "Value.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include <boost/spirit/include/support_utree.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
namespace bts {
|
||||
namespace expression {
|
||||
|
||||
namespace sp = boost::spirit;
|
||||
|
||||
bool value_to_bool(const Value& val)
|
||||
{
|
||||
switch(val.which())
|
||||
{
|
||||
case ValueType::boolean:
|
||||
return boost::get<bool>(val);
|
||||
case ValueType::integer:
|
||||
return boost::get<int>(val) != 0;
|
||||
case ValueType::real:
|
||||
return boost::get<double>(val) != 0;
|
||||
case ValueType::string:
|
||||
return !boost::get<std::string>(val).empty();
|
||||
case ValueType::list:
|
||||
return boost::get<std::vector<Value> >(val).size() > 0;
|
||||
default:
|
||||
throw std::logic_error("Invalid type in variant. Please update this switch statement");
|
||||
}
|
||||
}
|
||||
std::string value_to_string(const Value& val)
|
||||
{
|
||||
switch(val.which())
|
||||
{
|
||||
case ValueType::boolean:
|
||||
return boost::get<bool>(val) ? "true" : "false";
|
||||
case ValueType::integer:
|
||||
return boost::lexical_cast<std::string>(
|
||||
boost::get<int>(val));
|
||||
case ValueType::real:
|
||||
return boost::lexical_cast<std::string>(
|
||||
boost::get<double>(val));
|
||||
case ValueType::string:
|
||||
return boost::get<std::string>(val);
|
||||
case ValueType::list: {
|
||||
auto vals = boost::get<std::vector<Value>>(val);
|
||||
std::string result;
|
||||
for (const auto& v : vals) {
|
||||
result += value_to_string(v);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
default:
|
||||
throw std::logic_error("Invalid type in variant. Please update this switch statement");
|
||||
}
|
||||
}
|
||||
|
||||
const std::string get_symbol(const sp::utree& node)
|
||||
{
|
||||
sp::utf8_symbol_range_type rt = node.get<sp::utf8_symbol_range_type>();
|
||||
return std::string(rt.begin (),rt.end ());
|
||||
}
|
||||
|
||||
const std::string get_string(const boost::spirit::utree& node)
|
||||
{
|
||||
sp::utf8_symbol_range_type string_range = node.get<sp::utf8_string_range_type>();
|
||||
return std::string(string_range.begin (), string_range.end ());
|
||||
}
|
||||
|
||||
Value value_from_utree(const boost::spirit::utree& node)
|
||||
{
|
||||
switch(node.which())
|
||||
{
|
||||
case sp::utree_type::bool_type:
|
||||
return Value(node.get<bool>());
|
||||
case sp::utree_type::int_type:
|
||||
return Value(node.get<int>());
|
||||
case sp::utree_type::double_type:
|
||||
return Value(node.get<double>());
|
||||
case sp::utree_type::string_type:
|
||||
return Value(get_string(node));
|
||||
case sp::utree_type::list_type:
|
||||
if (node.size() == 1)
|
||||
{
|
||||
return value_from_utree(node.front());
|
||||
}
|
||||
// else fall through
|
||||
default:
|
||||
// Switch may need updating
|
||||
throw std::logic_error("Invalid type node primitive type");
|
||||
}
|
||||
}
|
||||
|
||||
}}//namespace
|
||||
Reference in new issue
Block a user