3

以下の c++ コードのように、std::wstring 型を使用して ChaiScript コードを記述したいと考えています。

#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 型のインスタンスを string 型のリテラルと比較することはできません。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: =="
4

2 に答える 2

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();
  }
}

inp == 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
于 2015-06-18T09:05:05.310 に答える