diff options
Diffstat (limited to 'host/lib/rfnoc/nocscript/expression.cpp')
-rw-r--r-- | host/lib/rfnoc/nocscript/expression.cpp | 189 |
1 files changed, 84 insertions, 105 deletions
diff --git a/host/lib/rfnoc/nocscript/expression.cpp b/host/lib/rfnoc/nocscript/expression.cpp index 1a7f3ee94..32065bda1 100644 --- a/host/lib/rfnoc/nocscript/expression.cpp +++ b/host/lib/rfnoc/nocscript/expression.cpp @@ -8,121 +8,110 @@ #include "expression.hpp" #include "function_table.hpp" #include <uhd/utils/cast.hpp> -#include <boost/format.hpp> #include <boost/algorithm/string.hpp> +#include <boost/format.hpp> using namespace uhd::rfnoc::nocscript; -std::map<expression::type_t, std::string> expression::type_repr{ - {TYPE_INT, "INT"}, +std::map<expression::type_t, std::string> expression::type_repr{{TYPE_INT, "INT"}, {TYPE_DOUBLE, "DOUBLE"}, {TYPE_STRING, "STRING"}, {TYPE_BOOL, "BOOL"}, - {TYPE_INT_VECTOR, "INT_VECTOR"} -}; + {TYPE_INT_VECTOR, "INT_VECTOR"}}; /******************************************************************** * Literal expressions (constants) *******************************************************************/ expression_literal::expression_literal( - const std::string token_val, - expression::type_t type -) : _bool_val(false) - , _int_val(0) - , _double_val(0.0) - , _val(token_val) - , _type(type) + const std::string token_val, expression::type_t type) + : _bool_val(false), _int_val(0), _double_val(0.0), _val(token_val), _type(type) { switch (_type) { - case expression::TYPE_STRING: - // Remove the leading and trailing quotes: - _val = _val.substr(1, _val.size()-2); - break; - - case expression::TYPE_INT: - if (_val.substr(0, 2) == "0x") { - _int_val = uhd::cast::hexstr_cast<int>(_val); - } else { - _int_val = std::stoi(_val); - } - break; - - case expression::TYPE_DOUBLE: - _double_val = std::stod(_val); - break; - - case expression::TYPE_BOOL: - if (boost::to_upper_copy(_val) == "TRUE") { - _bool_val = true; - } else { - // lexical cast to bool is too picky - _bool_val = bool(std::stoi(_val)); - } - break; - - case expression::TYPE_INT_VECTOR: - { - std::string str_vec = _val.substr(1, _val.size()-2); - std::vector<std::string> subtoken_list; - boost::split(subtoken_list, str_vec, boost::is_any_of(", "), boost::token_compress_on); - for(const std::string &t: subtoken_list) { - _int_vector_val.push_back(std::stoi(t)); - } - break; + case expression::TYPE_STRING: + // Remove the leading and trailing quotes: + _val = _val.substr(1, _val.size() - 2); + break; + + case expression::TYPE_INT: + if (_val.substr(0, 2) == "0x") { + _int_val = uhd::cast::hexstr_cast<int>(_val); + } else { + _int_val = std::stoi(_val); + } + break; + + case expression::TYPE_DOUBLE: + _double_val = std::stod(_val); + break; + + case expression::TYPE_BOOL: + if (boost::to_upper_copy(_val) == "TRUE") { + _bool_val = true; + } else { + // lexical cast to bool is too picky + _bool_val = bool(std::stoi(_val)); + } + break; + + case expression::TYPE_INT_VECTOR: { + std::string str_vec = _val.substr(1, _val.size() - 2); + std::vector<std::string> subtoken_list; + boost::split( + subtoken_list, str_vec, boost::is_any_of(", "), boost::token_compress_on); + for (const std::string& t : subtoken_list) { + _int_vector_val.push_back(std::stoi(t)); + } + break; } - default: - UHD_THROW_INVALID_CODE_PATH(); + default: + UHD_THROW_INVALID_CODE_PATH(); } } expression_literal::expression_literal(bool b) - : _bool_val(b) - , _int_val(0) - , _double_val(0.0) - , _val("") - , _type(expression::TYPE_BOOL) + : _bool_val(b), _int_val(0), _double_val(0.0), _val(""), _type(expression::TYPE_BOOL) { // nop } expression_literal::expression_literal(int i) - : _bool_val(false) - , _int_val(i) - , _double_val(0.0) - , _val("") - , _type(expression::TYPE_INT) + : _bool_val(false) + , _int_val(i) + , _double_val(0.0) + , _val("") + , _type(expression::TYPE_INT) { // nop } expression_literal::expression_literal(double d) - : _bool_val(false) - , _int_val(0) - , _double_val(d) - , _val("") - , _type(expression::TYPE_DOUBLE) + : _bool_val(false) + , _int_val(0) + , _double_val(d) + , _val("") + , _type(expression::TYPE_DOUBLE) { // nop } -expression_literal::expression_literal(const std::string &s) - : _bool_val(false) - , _int_val(0) - , _double_val(0.0) - , _val(s) - , _type(expression::TYPE_STRING) +expression_literal::expression_literal(const std::string& s) + : _bool_val(false) + , _int_val(0) + , _double_val(0.0) + , _val(s) + , _type(expression::TYPE_STRING) { // nop } expression_literal::expression_literal(const std::vector<int> v) - : _bool_val(false) - , _int_val(0) - , _double_val(0.0) - , _int_vector_val(v) - , _val("") - , _type(expression::TYPE_INT_VECTOR) + : _bool_val(false) + , _int_val(0) + , _double_val(0.0) + , _int_vector_val(v) + , _val("") + , _type(expression::TYPE_INT_VECTOR) { // nop } @@ -201,8 +190,7 @@ std::string expression_literal::repr() const return std::to_string(_double_val); case TYPE_BOOL: return _bool_val ? "TRUE" : "FALSE"; - case TYPE_INT_VECTOR: - { + case TYPE_INT_VECTOR: { std::stringstream sstr; sstr << "["; for (size_t i = 0; i < _int_vector_val.size(); i++) { @@ -213,13 +201,13 @@ std::string expression_literal::repr() const } sstr << "]"; return sstr.str(); - } + } default: UHD_THROW_INVALID_CODE_PATH(); } } -bool expression_literal::operator==(const expression_literal &rhs) const +bool expression_literal::operator==(const expression_literal& rhs) const { if (rhs.infer_type() != _type) { return false; @@ -287,7 +275,7 @@ expression_literal expression_container::eval() } expression_literal ret_val; - for(const expression::sptr &sub_expr: _sub_exprs) { + for (const expression::sptr& sub_expr : _sub_exprs) { ret_val = sub_expr->eval(); if (_combiner == COMBINE_AND and ret_val.to_bool() == false) { return ret_val; @@ -303,11 +291,12 @@ expression_literal expression_container::eval() /******************************************************************** * Functions *******************************************************************/ -std::string expression_function::to_string(const std::string &name, const argtype_list_type &types) +std::string expression_function::to_string( + const std::string& name, const argtype_list_type& types) { std::string s = name; int arg_count = 0; - for(const expression::type_t type: types) { + for (const expression::type_t type : types) { if (arg_count == 0) { s += "("; } else { @@ -322,17 +311,12 @@ std::string expression_function::to_string(const std::string &name, const argtyp } expression_function::expression_function( - const std::string &name, - const function_table::sptr func_table -) : _name(name) - , _func_table(func_table) + const std::string& name, const function_table::sptr func_table) + : _name(name), _func_table(func_table) { _combiner = COMBINE_ALL; if (not _func_table->function_exists(_name)) { - throw uhd::syntax_error(str( - boost::format("Unknown function: %s") - % _name - )); + throw uhd::syntax_error(str(boost::format("Unknown function: %s") % _name)); } } @@ -359,21 +343,18 @@ std::string expression_function::repr() const } expression_function::sptr expression_function::make( - const std::string &name, - const function_table::sptr func_table -) { + const std::string& name, const function_table::sptr func_table) +{ return sptr(new expression_function(name, func_table)); } /******************************************************************** * Variables *******************************************************************/ -expression_variable::expression_variable( - const std::string &token_val, +expression_variable::expression_variable(const std::string& token_val, type_getter_type type_getter, - value_getter_type value_getter -) : _type_getter(type_getter) - , _value_getter(value_getter) + value_getter_type value_getter) + : _type_getter(type_getter), _value_getter(value_getter) { // We can assume this is true because otherwise, it's not a valid token: UHD_ASSERT_THROW(not token_val.empty() and token_val[0] == '$'); @@ -391,11 +372,9 @@ expression_literal expression_variable::eval() return _value_getter(_varname); } -expression_variable::sptr expression_variable::make( - const std::string &token_val, - type_getter_type type_getter, - value_getter_type value_getter -) { +expression_variable::sptr expression_variable::make(const std::string& token_val, + type_getter_type type_getter, + value_getter_type value_getter) +{ return sptr(new expression_variable(token_val, type_getter, value_getter)); } - |