比较std :: wstring类型并在ChaiScript中表示wstring文字

时间:2015-06-15 06:40:20

标签: wstring chaiscript

我想使用std :: wstring类型编写ChaiScript代码,如下面的c ++代码。

#include <iostream>

int testfunc(std::wstring s, std::wstring t)
{
    if(s==t)
    {
        std::cout << "1" << std::endl;
    }

    if(s[1]==t[1])
    {
        std::cout << "2" << std::endl;
    }

    if(s==L"aaaa")
    {
        std::cout << "3" << std::endl;
    }

    if(s[1]==L'b')
    {
        std::cout << "4" << std::endl;
    }

    return 5;
}

int main()
{
    std::cout << testfunc(std::wstring(L"abcd"), std::wstring(L"abbb"));

    return 0;
}

D:\TestWork\test_chaiscript>t6
2
4
5

比较std :: wstring类型的实例是好的。

#include <iostream>
#include <chaiscript/chaiscript.hpp>
#include <chaiscript/chaiscript_stdlib.hpp>
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>

int main()
{
    chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());

    chai.add(chaiscript::bootstrap::standard_library::string_type<std::wstring>("wstring"));

    std::cout << chai.eval<std::function<int (std::wstring, std::wstring)> >(
        "fun(s, t){"
        "   if(s==t){"
        "       print(\"1\");"
        "   }"
        "   return 3;"
        "}"
    )(std::wstring(L"abcd"), std::wstring(L"abaa"));

    return 0;
}

D:\TestWork\test_chaiscript>t5
3

比较wchar_t类型的实例不起作用。

是否必须添加比较运算符方法?

        "   if(s[1]==t[1]){"
        "       print(\"2\");"
        "   }"

D:\TestWork\test_chaiscript>t5
terminate called after throwing an instance of 'chaiscript::exception::eval_error'
  what():  Error: "Error with numeric operator calling: =="

将std :: wstring类型的实例与字符串类型的文字进行比较不起作用。 我无法输入wstring类型的文字。

是否可以在ChaiScript中输入wstring类型的文字?

        "   if(s==\"aaaa\"){"
        "       print(\"2\");"
        "   }"

D:\TestWork\test_chaiscript>t5
terminate called after throwing an instance of 'chaiscript::exception::eval_error'
  what():  Error: "Can not find appropriate '==' operator." With parameters: (wstring, const string)

将wchar_t类型的实例与wchar_t类型的文字进行比较不起作用。 我无法输入wchat_t类型的文字。

是否可以在ChaiScript中输入wchat_t类型的文字?

        "   if(s[1]=='b'){"
        "       print(\"2\");"
        "   }"

D:\TestWork\test_chaiscript>t5
terminate called after throwing an instance of 'chaiscript::exception::eval_error'
  what():  Error: "Error with numeric operator calling: =="

2 个答案:

答案 0 :(得分:0)

是的,您需要告诉ChaiScript wchar_t的某些内容,例如==运算符。例如:

chai.add(
  chaiscript::fun<bool (wchar_t, wchar_t)>(
    [](wchar_t lhs, wchar_t rhs) { return lhs == rhs; }
  ), "=="
);

答案 1 :(得分:0)

我认为ChaiScript中需要本机支持wchar_t。

“数字运算符调用出错:”在chaiscript_eval.hpp中生成异常。

如果变量的类型是算术,则调用Boxed_Number :: do_oper()。

// chaiscript_eval.hpp, line 98
if (t_oper != Operators::invalid && t_lhs.get_type_info().is_arithmetic() && t_rhs.get_type_info().is_arithmetic())
{
  // If it's an arithmetic operation we want to short circuit dispatch
  try{
    return Boxed_Number::do_oper(t_oper, t_lhs, t_rhs);
  } catch (const chaiscript::exception::arithmetic_error &) {
    throw;
  } catch (...) {
    throw exception::eval_error("Error with numeric operator calling: " + t_oper_string);
  }
} else {

似乎使用std :: is_arithmetic来确定是否为算术类型。

// type_info.hpp, 133 line
struct Get_Type_Info
{
    typedef T type;

     static Type_Info get()
    {
      return Type_Info(std::is_const<typename std::remove_pointer<typename std::remove_reference<T>::type>::type>::value, std::is_reference<T>::value, std::is_pointer<T>::value, 
          std::is_void<T>::value,
          std::is_arithmetic<T>::value && !std::is_same<typename std::remove_const<T>::type, bool>::value,
          &typeid(T), 
          &typeid(typename Bare_Type<T>::type));
    }
};

wchat_t是算术类型。

std::cout << std::is_arithmetic<wchar_t>::value << std::endl;
std::cout << std::is_arithmetic<std::string>::value << std::endl;

D:\TestWork\test_chaiscript>t5.exe
1
0

如果类型是算术,则调用Boxed_Value的oper()方法。 但是,oper()方法不支持wchar_t类型。

// boxed_number.hpp, 295 line
inline static Boxed_Value oper(Operators::Opers t_oper, const Boxed_Value &t_lhs, const Boxed_Value &t_rhs)
{
  const Type_Info &inp_ = t_lhs.get_type_info();

  if (inp_ == typeid(int)) {
    return oper_rhs<int>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(double)) {
    return oper_rhs<double>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(long double)) {
    return oper_rhs<long double>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(float)) {
    return oper_rhs<float>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(char)) {
    return oper_rhs<char>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(unsigned int)) {
    return oper_rhs<unsigned int>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(long)) {
    return oper_rhs<long>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(unsigned long)) {
    return oper_rhs<unsigned long>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::int8_t)) {
    return oper_rhs<std::int8_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::int16_t)) {
    return oper_rhs<std::int16_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::int32_t)) {
    return oper_rhs<std::int32_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::int64_t)) {
    return oper_rhs<std::int64_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::uint8_t)) {
    return oper_rhs<std::uint8_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::uint16_t)) {
    return oper_rhs<std::uint16_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::uint32_t)) {
    return oper_rhs<std::uint32_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::uint64_t)) {
    return oper_rhs<std::uint64_t>(t_oper, t_lhs, t_rhs);
  } else  {
    throw chaiscript::detail::exception::bad_any_cast();
  }
}

我在p == typeid(char)中使用的所有地方添加代码以支持wchar_t类型。

} else if (inp_ == typeid(char)) {
    return go<LHS, char>(t_oper, t_lhs, t_rhs);
} else if (inp_ == typeid(wchar_t)) {
    return go<LHS, wchar_t>(t_oper, t_lhs, t_rhs);
} else if (inp_ == typeid(unsigned int)) {

我的测试代码效果很好。

std::cout << chai.eval<std::function<int (wchar_t a, wchar_t b)> >(
    "fun(a, b){"
    "   if(a==b){"
    "       print(\"2\");"
    "   }"
    "   return 3;"
    "}"
)(wchar_t(L'a'), wchar_t(L'a'));

D:\TestWork\test_chaiscript>t5.exe
2
3