Add array type support #167
This commit is contained in:
@@ -52,6 +52,43 @@ namespace chaiscript
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T, typename = typename std::enable_if<std::is_array<T>::value>::type >
|
||||
ModulePtr array(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
typedef typename std::remove_extent<T>::type ReturnType;
|
||||
const auto extent = std::extent<T>::value;
|
||||
m->add(user_type<T>(), type);
|
||||
m->add(fun<ReturnType& (T &, size_t)>(
|
||||
[extent](T& t, size_t index)->ReturnType &{
|
||||
if (extent > 0 && index >= extent) {
|
||||
throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent));
|
||||
} else {
|
||||
return t[index];
|
||||
}
|
||||
}
|
||||
), "[]"
|
||||
);
|
||||
|
||||
m->add(fun<const ReturnType& (const T &, size_t)>(
|
||||
[extent](const T &t, size_t index)->const ReturnType &{
|
||||
if (extent > 0 && index >= extent) {
|
||||
throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent));
|
||||
} else {
|
||||
return t[index];
|
||||
}
|
||||
}
|
||||
), "[]"
|
||||
);
|
||||
|
||||
m->add(fun<size_t (const T &)>(
|
||||
[extent](const T &) {
|
||||
return extent;
|
||||
}), "size");
|
||||
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/// \brief Adds a copy constructor for the given type to the given Model
|
||||
/// \param[in] type The name of the type. The copy constructor will be named "type".
|
||||
/// \param[in,out] m The Module to add the copy constructor to
|
||||
|
@@ -40,7 +40,8 @@ namespace chaiscript
|
||||
{
|
||||
if (ob.get_type_info().bare_equal_type_info(typeid(Result)))
|
||||
{
|
||||
return *(static_cast<const Result *>(throw_if_null(ob.get_const_ptr())));
|
||||
auto p = throw_if_null(ob.get_const_ptr());
|
||||
return std::cref(*static_cast<const Result *>(p));
|
||||
} else {
|
||||
throw chaiscript::detail::exception::bad_any_cast();
|
||||
}
|
||||
|
@@ -712,7 +712,7 @@ namespace chaiscript
|
||||
|
||||
fpp.save_params(params);
|
||||
|
||||
std::string fun_name = [&](){
|
||||
std::string fun_name = [&]()->std::string{
|
||||
if ((this->children[i]->identifier == AST_Node_Type::Fun_Call) || (this->children[i]->identifier == AST_Node_Type::Array_Call)) {
|
||||
return this->children[i]->children[0]->text;
|
||||
}
|
||||
|
@@ -617,7 +617,7 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
|
||||
const size_t size = [&](){
|
||||
const size_t size = [&]()->size_t{
|
||||
if (longlong_)
|
||||
{
|
||||
return sizeof(int64_t) * 8;
|
||||
@@ -800,7 +800,7 @@ namespace chaiscript
|
||||
const auto prev_line = m_line;
|
||||
if (Id_()) {
|
||||
m_match_stack.push_back(std::make_shared<eval::Id_AST_Node>(
|
||||
[&](){
|
||||
[&]()->std::string{
|
||||
if (*start == '`') {
|
||||
//Id Literal
|
||||
return std::string(start+1, m_input_pos-1);
|
||||
|
Reference in New Issue
Block a user