1 // This file is distributed under the BSD License.
2 // See "license.txt" for details.
3 // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4 // Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
5 // http://www.chaiscript.com
6 
7 // This is an open source non-commercial project. Dear PVS-Studio, please check it.
8 // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
9 
10 
11 #ifndef CHAISCRIPT_BOOTSTRAP_HPP_
12 #define CHAISCRIPT_BOOTSTRAP_HPP_
13 
14 #include "../utility/utility.hpp"
15 #include "register_function.hpp"
16 
17 namespace chaiscript
18 {
19   /// \brief Classes and functions useful for bootstrapping of ChaiScript and adding of new types
20   namespace bootstrap
21   {
22     template<typename T, typename = typename std::enable_if<std::is_array<T>::value>::type >
array(const std::string & type,Module & m)23       void array(const std::string &type, Module& m)
24       {
25         typedef typename std::remove_extent<T>::type ReturnType;
26         const auto extent = std::extent<T>::value;
27         m.add(user_type<T>(), type);
28         m.add(fun(
29               [extent](T& t, size_t index)->ReturnType &{
30                 if (extent > 0 && index >= extent) {
31                   throw std::range_error("Array index out of range. Received: " + std::to_string(index)  + " expected < " + std::to_string(extent));
32                 } else {
33                   return t[index];
34                 }
35               }
36               ), "[]"
37             );
38 
39         m.add(fun(
40               [extent](const T &t, size_t index)->const ReturnType &{
41                 if (extent > 0 && index >= extent) {
42                   throw std::range_error("Array index out of range. Received: " + std::to_string(index)  + " expected < " + std::to_string(extent));
43                 } else {
44                   return t[index];
45                 }
46               }
47               ), "[]"
48             );
49 
50         m.add(fun(
51               [extent](const T &) {
52                 return extent;
53               }), "size");
54       }
55 
56     /// \brief Adds a copy constructor for the given type to the given Model
57     /// \param[in] type The name of the type. The copy constructor will be named "type".
58     /// \param[in,out] m The Module to add the copy constructor to
59     /// \tparam T The type to add a copy constructor for
60     /// \returns The passed in Module
61     template<typename T>
copy_constructor(const std::string & type,Module & m)62     void copy_constructor(const std::string &type, Module& m)
63     {
64       m.add(constructor<T (const T &)>(), type);
65     }
66 
67     /// \brief Add all comparison operators for the templated type. Used during bootstrap, also available to users.
68     /// \tparam T Type to create comparison operators for
69     /// \param[in,out] m module to add comparison operators to
70     /// \returns the passed in Module.
71     template<typename T>
opers_comparison(Module & m)72     void opers_comparison(Module& m)
73     {
74       operators::equal<T>(m);
75       operators::greater_than<T>(m);
76       operators::greater_than_equal<T>(m);
77       operators::less_than<T>(m);
78       operators::less_than_equal<T>(m);
79       operators::not_equal<T>(m);
80     }
81 
82 
83 
84     /// \brief Adds default and copy constructors for the given type
85     /// \param[in] type The name of the type to add the constructors for.
86     /// \param[in,out] m The Module to add the basic constructors to
87     /// \tparam T Type to generate basic constructors for
88     /// \returns The passed in Module
89     /// \sa copy_constructor
90     /// \sa constructor
91     template<typename T>
basic_constructors(const std::string & type,Module & m)92     void basic_constructors(const std::string &type, Module& m)
93     {
94       m.add(constructor<T ()>(), type);
95       copy_constructor<T>(type, m);
96     }
97 
98     /// \brief Adds a constructor for a POD type
99     /// \tparam T The type to add the constructor for
100     /// \param[in] type The name of the type
101     /// \param[in,out] m The Module to add the constructor to
102     template<typename T>
construct_pod(const std::string & type,Module & m)103     void construct_pod(const std::string &type, Module& m)
104     {
105       m.add(fun([](const Boxed_Number &bn){ return bn.get_as<T>(); }), type);
106     }
107 
108 
109     /// Internal function for converting from a string to a value
110     /// uses ostream operator >> to perform the conversion
111     template<typename Input>
parse_string(const std::string & i)112     auto parse_string(const std::string &i)
113       -> typename std::enable_if<
114              !std::is_same<Input, wchar_t>::value
115              && !std::is_same<Input, char16_t>::value
116              && !std::is_same<Input, char32_t>::value,
117       Input>::type
118     {
119       std::stringstream ss(i);
120       Input t;
121       ss >> t;
122       return t;
123     }
124 
125     template<typename Input>
parse_string(const std::string &)126     auto parse_string(const std::string &)
127       -> typename std::enable_if<
128              std::is_same<Input, wchar_t>::value
129              || std::is_same<Input, char16_t>::value
130              || std::is_same<Input, char32_t>::value,
131       Input>::type
132     {
133       throw std::runtime_error("Parsing of wide characters is not yet supported");
134     }
135 
136 
137     /// Add all common functions for a POD type. All operators, and
138     /// common conversions
139     template<typename T>
bootstrap_pod_type(const std::string & name,Module & m)140     void bootstrap_pod_type(const std::string &name, Module& m)
141     {
142       m.add(user_type<T>(), name);
143       m.add(constructor<T()>(), name);
144       construct_pod<T>(name, m);
145 
146       m.add(fun(&parse_string<T>), "to_" + name);
147       m.add(fun([](const T t){ return t; }), "to_" + name);
148     }
149 
150 
151     /// "clone" function for a shared_ptr type. This is used in the case
152     /// where you do not want to make a deep copy of an object during cloning
153     /// but want to instead maintain the shared_ptr. It is needed internally
154     /// for handling of Proxy_Function object (that is,
155     /// function variables.
156     template<typename Type>
shared_ptr_clone(const std::shared_ptr<Type> & p)157     auto shared_ptr_clone(const std::shared_ptr<Type> &p)
158     {
159       return p;
160     }
161 
162     /// Specific version of shared_ptr_clone just for Proxy_Functions
163     template<typename Type>
shared_ptr_unconst_clone(const std::shared_ptr<typename std::add_const<Type>::type> & p)164     std::shared_ptr<typename std::remove_const<Type>::type> shared_ptr_unconst_clone(const std::shared_ptr<typename std::add_const<Type>::type> &p)
165     {
166       return std::const_pointer_cast<typename std::remove_const<Type>::type>(p);
167     }
168 
169 
170 
171     /// Assignment function for shared_ptr objects, does not perform a copy of the
172     /// object pointed to, instead maintains the shared_ptr concept.
173     /// Similar to shared_ptr_clone. Used for Proxy_Function.
174     template<typename Type>
ptr_assign(Boxed_Value lhs,const std::shared_ptr<Type> & rhs)175     Boxed_Value ptr_assign(Boxed_Value lhs, const std::shared_ptr<Type> &rhs)
176     {
177       if (lhs.is_undef()
178           || (!lhs.get_type_info().is_const() && lhs.get_type_info().bare_equal(chaiscript::detail::Get_Type_Info<Type>::get())))
179       {
180         lhs.assign(Boxed_Value(rhs));
181         return lhs;
182       } else {
183         throw exception::bad_boxed_cast("type mismatch in pointer assignment");
184       }
185     }
186 
187     /// Class consisting of only static functions. All default bootstrapping occurs
188     /// from this class.
189     class Bootstrap
190     {
191     private:
192       /// Function allowing for assignment of an unknown type to any other value
unknown_assign(Boxed_Value lhs,Boxed_Value rhs)193       static Boxed_Value unknown_assign(Boxed_Value lhs, Boxed_Value rhs)
194       {
195         if (lhs.is_undef())
196         {
197           return (lhs.assign(rhs));
198         } else {
199           throw exception::bad_boxed_cast("boxed_value has a set type already");
200         }
201       }
202 
print(const std::string & s)203       static void print(const std::string &s)
204       {
205         fwrite(s.c_str(), 1, s.size(), stdout);
206       }
207 
println(const std::string & s)208       static void println(const std::string &s)
209       {
210         puts(s.c_str());
211       }
212 
213 
214       /// Add all arithmetic operators for PODs
opers_arithmetic_pod(Module & m)215       static void opers_arithmetic_pod(Module& m)
216       {
217         m.add(fun(&Boxed_Number::equals), "==");
218         m.add(fun(&Boxed_Number::less_than), "<");
219         m.add(fun(&Boxed_Number::greater_than), ">");
220         m.add(fun(&Boxed_Number::greater_than_equal), ">=");
221         m.add(fun(&Boxed_Number::less_than_equal), "<=");
222         m.add(fun(&Boxed_Number::not_equal), "!=");
223 
224         m.add(fun(&Boxed_Number::pre_decrement), "--");
225         m.add(fun(&Boxed_Number::pre_increment), "++");
226         m.add(fun(&Boxed_Number::sum), "+");
227         m.add(fun(&Boxed_Number::unary_plus), "+");
228         m.add(fun(&Boxed_Number::unary_minus), "-");
229         m.add(fun(&Boxed_Number::difference), "-");
230         m.add(fun(&Boxed_Number::assign_bitwise_and), "&=");
231         m.add(fun(&Boxed_Number::assign), "=");
232         m.add(fun(&Boxed_Number::assign_bitwise_or), "|=");
233         m.add(fun(&Boxed_Number::assign_bitwise_xor), "^=");
234         m.add(fun(&Boxed_Number::assign_remainder), "%=");
235         m.add(fun(&Boxed_Number::assign_shift_left), "<<=");
236         m.add(fun(&Boxed_Number::assign_shift_right), ">>=");
237         m.add(fun(&Boxed_Number::bitwise_and), "&");
238         m.add(fun(&Boxed_Number::bitwise_complement), "~");
239         m.add(fun(&Boxed_Number::bitwise_xor), "^");
240         m.add(fun(&Boxed_Number::bitwise_or), "|");
241         m.add(fun(&Boxed_Number::assign_product), "*=");
242         m.add(fun(&Boxed_Number::assign_quotient), "/=");
243         m.add(fun(&Boxed_Number::assign_sum), "+=");
244         m.add(fun(&Boxed_Number::assign_difference), "-=");
245         m.add(fun(&Boxed_Number::quotient), "/");
246         m.add(fun(&Boxed_Number::shift_left), "<<");
247         m.add(fun(&Boxed_Number::product), "*");
248         m.add(fun(&Boxed_Number::remainder), "%");
249         m.add(fun(&Boxed_Number::shift_right), ">>");
250      }
251 
252       /// Create a bound function object. The first param is the function to bind
253       /// the remaining parameters are the args to bind into the result
bind_function(const std::vector<Boxed_Value> & params)254       static Boxed_Value bind_function(const std::vector<Boxed_Value> &params)
255       {
256         if (params.empty()) {
257           throw exception::arity_error(0, 1);
258         }
259 
260         Const_Proxy_Function f = boxed_cast<Const_Proxy_Function>(params[0]);
261 
262         if (f->get_arity() != -1 && size_t(f->get_arity()) != params.size() - 1)
263         {
264           throw exception::arity_error(static_cast<int>(params.size()), f->get_arity());
265         }
266 
267         return Boxed_Value(Const_Proxy_Function(std::make_shared<dispatch::Bound_Function>(std::move(f),
268           std::vector<Boxed_Value>(params.begin() + 1, params.end()))));
269       }
270 
271 
has_guard(const Const_Proxy_Function & t_pf)272       static bool has_guard(const Const_Proxy_Function &t_pf)
273       {
274         auto pf = std::dynamic_pointer_cast<const dispatch::Dynamic_Proxy_Function>(t_pf);
275         return pf && pf->get_guard();
276       }
277 
get_guard(const Const_Proxy_Function & t_pf)278       static Const_Proxy_Function get_guard(const Const_Proxy_Function &t_pf)
279       {
280         const auto pf = std::dynamic_pointer_cast<const dispatch::Dynamic_Proxy_Function>(t_pf);
281         if (pf && pf->get_guard())
282         {
283           return pf->get_guard();
284         } else {
285           throw std::runtime_error("Function does not have a guard");
286         }
287       }
288 
289       template<typename FunctionType>
do_return_boxed_value_vector(FunctionType f,const dispatch::Proxy_Function_Base * b)290         static std::vector<Boxed_Value> do_return_boxed_value_vector(FunctionType f,
291             const dispatch::Proxy_Function_Base *b)
292         {
293           auto v = (b->*f)();
294 
295           std::vector<Boxed_Value> vbv;
296 
297           for (const auto &o: v)
298           {
299             vbv.push_back(const_var(o));
300           }
301 
302           return vbv;
303         }
304 
305 
has_parse_tree(const chaiscript::Const_Proxy_Function & t_pf)306       static bool has_parse_tree(const chaiscript::Const_Proxy_Function &t_pf)
307       {
308         const auto pf = std::dynamic_pointer_cast<const chaiscript::dispatch::Dynamic_Proxy_Function>(t_pf);
309         return bool(pf);
310       }
311 
get_parse_tree(const chaiscript::Const_Proxy_Function & t_pf)312       static const chaiscript::AST_Node &get_parse_tree(const chaiscript::Const_Proxy_Function &t_pf)
313       {
314         const auto pf = std::dynamic_pointer_cast<const chaiscript::dispatch::Dynamic_Proxy_Function>(t_pf);
315         if (pf)
316         {
317           return pf->get_parse_tree();
318         } else {
319           throw std::runtime_error("Function does not have a parse tree");
320         }
321       }
322 
323       template<typename Function>
return_boxed_value_vector(const Function & f)324       static auto return_boxed_value_vector(const Function &f)
325       {
326         return [f](const dispatch::Proxy_Function_Base *b) {
327           return do_return_boxed_value_vector(f, b);
328         };
329       }
330 
331 
332     public:
333       /// \brief perform all common bootstrap functions for std::string, void and POD types
334       /// \param[in,out] m Module to add bootstrapped functions to
335       /// \returns passed in Module
bootstrap(Module & m)336       static void bootstrap(Module& m)
337       {
338         m.add(user_type<void>(), "void");
339         m.add(user_type<bool>(), "bool");
340         m.add(user_type<Boxed_Value>(), "Object");
341         m.add(user_type<Boxed_Number>(), "Number");
342         m.add(user_type<Proxy_Function>(), "Function");
343         m.add(user_type<dispatch::Assignable_Proxy_Function>(), "Assignable_Function");
344         m.add(user_type<std::exception>(), "exception");
345 
346         m.add(fun(&dispatch::Proxy_Function_Base::get_arity), "get_arity");
347         m.add(fun(&dispatch::Proxy_Function_Base::operator==), "==");
348 
349 
350         m.add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_param_types)), "get_param_types");
351         m.add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_contained_functions)), "get_contained_functions");
352 
353         m.add(fun([](const std::exception &e){ return std::string(e.what()); }), "what");
354 
355         m.add(user_type<std::out_of_range>(), "out_of_range");
356         m.add(user_type<std::logic_error>(), "logic_error");
357         m.add(chaiscript::base_class<std::exception, std::logic_error>());
358         m.add(chaiscript::base_class<std::logic_error, std::out_of_range>());
359         m.add(chaiscript::base_class<std::exception, std::out_of_range>());
360 
361         m.add(user_type<std::runtime_error>(), "runtime_error");
362         m.add(chaiscript::base_class<std::exception, std::runtime_error>());
363 
364         m.add(constructor<std::runtime_error (const std::string &)>(), "runtime_error");
365 
366         m.add(user_type<dispatch::Dynamic_Object>(), "Dynamic_Object");
367         m.add(constructor<dispatch::Dynamic_Object (const std::string &)>(), "Dynamic_Object");
368         m.add(constructor<dispatch::Dynamic_Object ()>(), "Dynamic_Object");
369         m.add(fun(&dispatch::Dynamic_Object::get_type_name), "get_type_name");
370         m.add(fun(&dispatch::Dynamic_Object::get_attrs), "get_attrs");
371         m.add(fun(&dispatch::Dynamic_Object::set_explicit), "set_explicit");
372         m.add(fun(&dispatch::Dynamic_Object::is_explicit), "is_explicit");
373         m.add(fun(&dispatch::Dynamic_Object::has_attr), "has_attr");
374 
375         m.add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::get_attr)), "get_attr");
376         m.add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::get_attr)), "get_attr");
377 
378         m.add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::method_missing)), "method_missing");
379         m.add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::method_missing)), "method_missing");
380 
381         m.add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::get_attr)), "[]");
382         m.add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::get_attr)), "[]");
383 
384         m.eval(R"chaiscript(
385           def Dynamic_Object::clone() {
386             auto &new_o = Dynamic_Object(this.get_type_name());
387             for_each(this.get_attrs(), fun[new_o](x) { new_o.get_attr(x.first) = x.second; } );
388             new_o;
389           }
390 
391           def `=`(Dynamic_Object lhs, Dynamic_Object rhs) : lhs.get_type_name() == rhs.get_type_name()
392           {
393             for_each(rhs.get_attrs(), fun[lhs](x) { lhs.get_attr(x.first) = clone(x.second); } );
394           }
395 
396           def `!=`(Dynamic_Object lhs, Dynamic_Object rhs) : lhs.get_type_name() == rhs.get_type_name()
397           {
398             var rhs_attrs := rhs.get_attrs();
399             var lhs_attrs := lhs.get_attrs();
400 
401             if (rhs_attrs.size() != lhs_attrs.size()) {
402               true;
403             } else {
404               return any_of(rhs_attrs, fun[lhs](x) { !lhs.has_attr(x.first) || lhs.get_attr(x.first) != x.second; } );
405             }
406           }
407 
408           def `==`(Dynamic_Object lhs, Dynamic_Object rhs) : lhs.get_type_name() == rhs.get_type_name()
409           {
410             var rhs_attrs := rhs.get_attrs();
411             var lhs_attrs := lhs.get_attrs();
412 
413             if (rhs_attrs.size() != lhs_attrs.size()) {
414               false;
415             } else {
416               return all_of(rhs_attrs, fun[lhs](x) { lhs.has_attr(x.first) && lhs.get_attr(x.first) == x.second; } );
417             }
418           }
419         )chaiscript");
420 
421         m.add(fun(&has_guard), "has_guard");
422         m.add(fun(&get_guard), "get_guard");
423 
424         m.add(fun(&Boxed_Value::is_undef), "is_var_undef");
425         m.add(fun(&Boxed_Value::is_null), "is_var_null");
426         m.add(fun(&Boxed_Value::is_const), "is_var_const");
427         m.add(fun(&Boxed_Value::is_ref), "is_var_reference");
428         m.add(fun(&Boxed_Value::is_pointer), "is_var_pointer");
429         m.add(fun(&Boxed_Value::is_return_value), "is_var_return_value");
430         m.add(fun(&Boxed_Value::reset_return_value), "reset_var_return_value");
431         m.add(fun(&Boxed_Value::is_type), "is_type");
432         m.add(fun(&Boxed_Value::get_attr), "get_var_attr");
433         m.add(fun(&Boxed_Value::copy_attrs), "copy_var_attrs");
434         m.add(fun(&Boxed_Value::clone_attrs), "clone_var_attrs");
435 
436         m.add(fun(&Boxed_Value::get_type_info), "get_type_info");
437         m.add(user_type<Type_Info>(), "Type_Info");
438         m.add(constructor<Type_Info (const Type_Info &)>(), "Type_Info");
439 
440 
441         operators::equal<Type_Info>(m);
442 
443         m.add(fun(&Type_Info::is_const), "is_type_const");
444         m.add(fun(&Type_Info::is_reference), "is_type_reference");
445         m.add(fun(&Type_Info::is_void), "is_type_void");
446         m.add(fun(&Type_Info::is_undef), "is_type_undef");
447         m.add(fun(&Type_Info::is_pointer), "is_type_pointer");
448         m.add(fun(&Type_Info::is_arithmetic), "is_type_arithmetic");
449         m.add(fun(&Type_Info::name), "cpp_name");
450         m.add(fun(&Type_Info::bare_name), "cpp_bare_name");
451         m.add(fun(&Type_Info::bare_equal), "bare_equal");
452 
453 
454         basic_constructors<bool>("bool", m);
455         operators::assign<bool>(m);
456         operators::equal<bool>(m);
457         operators::not_equal<bool>(m);
458 
459         m.add(fun([](const std::string &s) { return s; }), "to_string");
460         m.add(fun([](const bool b) { return std::string(b?"true":"false"); }), "to_string");
461         m.add(fun(&unknown_assign), "=");
462         m.add(fun([](const Boxed_Value &bv) { throw bv; }), "throw");
463 
464         m.add(fun([](const char c) { return std::string(1, c); }), "to_string");
465         m.add(fun(&Boxed_Number::to_string), "to_string");
466 
467 
468         bootstrap_pod_type<double>("double", m);
469         bootstrap_pod_type<long double>("long_double", m);
470         bootstrap_pod_type<float>("float", m);
471         bootstrap_pod_type<int>("int", m);
472         bootstrap_pod_type<long>("long", m);
473         bootstrap_pod_type<unsigned int>("unsigned_int", m);
474         bootstrap_pod_type<unsigned long>("unsigned_long", m);
475         bootstrap_pod_type<long long>("long_long", m);
476         bootstrap_pod_type<unsigned long long>("unsigned_long_long", m);
477         bootstrap_pod_type<size_t>("size_t", m);
478         bootstrap_pod_type<char>("char", m);
479         bootstrap_pod_type<wchar_t>("wchar_t", m);
480         bootstrap_pod_type<char16_t>("char16_t", m);
481         bootstrap_pod_type<char32_t>("char32_t", m);
482         bootstrap_pod_type<std::int8_t>("int8_t", m);
483         bootstrap_pod_type<std::int16_t>("int16_t", m);
484         bootstrap_pod_type<std::int32_t>("int32_t", m);
485         bootstrap_pod_type<std::int64_t>("int64_t", m);
486         bootstrap_pod_type<std::uint8_t>("uint8_t", m);
487         bootstrap_pod_type<std::uint16_t>("uint16_t", m);
488         bootstrap_pod_type<std::uint32_t>("uint32_t", m);
489         bootstrap_pod_type<std::uint64_t>("uint64_t", m);
490 
491 
492         operators::logical_compliment<bool>(m);
493 
494         opers_arithmetic_pod(m);
495 
496 
497         m.add(fun(&Build_Info::version_major), "version_major");
498         m.add(fun(&Build_Info::version_minor), "version_minor");
499         m.add(fun(&Build_Info::version_patch), "version_patch");
500         m.add(fun(&Build_Info::version), "version");
501         m.add(fun(&Build_Info::compiler_version), "compiler_version");
502         m.add(fun(&Build_Info::compiler_name), "compiler_name");
503         m.add(fun(&Build_Info::compiler_id), "compiler_id");
504         m.add(fun(&Build_Info::debug_build), "debug_build");
505 
506 
507         m.add(fun(&print), "print_string");
508         m.add(fun(&println), "println_string");
509 
510         m.add(dispatch::make_dynamic_proxy_function(&bind_function), "bind");
511 
512         m.add(fun(&shared_ptr_unconst_clone<dispatch::Proxy_Function_Base>), "clone");
513         m.add(fun(&ptr_assign<std::remove_const<dispatch::Proxy_Function_Base>::type>), "=");
514         m.add(fun(&ptr_assign<std::add_const<dispatch::Proxy_Function_Base>::type>), "=");
515         m.add(chaiscript::base_class<dispatch::Proxy_Function_Base, dispatch::Assignable_Proxy_Function>());
516         m.add(fun(
517                   [](dispatch::Assignable_Proxy_Function &t_lhs, const std::shared_ptr<const dispatch::Proxy_Function_Base> &t_rhs) {
518                     t_lhs.assign(t_rhs);
519                   }
520                 ), "="
521               );
522 
523         m.add(fun(&Boxed_Value::type_match), "type_match");
524 
525 
526         m.add(chaiscript::fun(&has_parse_tree), "has_parse_tree");
527         m.add(chaiscript::fun(&get_parse_tree), "get_parse_tree");
528 
529         m.add(chaiscript::base_class<std::runtime_error, chaiscript::exception::eval_error>());
530         m.add(chaiscript::base_class<std::exception, chaiscript::exception::eval_error>());
531 
532         m.add(chaiscript::user_type<chaiscript::exception::arithmetic_error>(), "arithmetic_error");
533         m.add(chaiscript::base_class<std::runtime_error, chaiscript::exception::arithmetic_error>());
534         m.add(chaiscript::base_class<std::exception, chaiscript::exception::arithmetic_error>());
535 
536 
537 //        chaiscript::bootstrap::standard_library::vector_type<std::vector<std::shared_ptr<chaiscript::AST_Node> > >("AST_NodeVector", m);
538 
539 
540         chaiscript::utility::add_class<chaiscript::exception::eval_error>(m,
541             "eval_error",
542             { },
543             { {fun(&chaiscript::exception::eval_error::reason), "reason"},
544               {fun(&chaiscript::exception::eval_error::pretty_print), "pretty_print"},
545               {fun([](const chaiscript::exception::eval_error &t_eval_error) {
546                   std::vector<Boxed_Value> retval;
547                   std::transform(t_eval_error.call_stack.begin(), t_eval_error.call_stack.end(),
548                                  std::back_inserter(retval),
549                                  &chaiscript::var<const chaiscript::AST_Node_Trace &>);
550                   return retval;
551                 }), "call_stack"} }
552             );
553 
554 
555         chaiscript::utility::add_class<chaiscript::File_Position>(m,
556             "File_Position",
557             { constructor<File_Position()>(),
558               constructor<File_Position(int, int)>() },
559             { {fun(&File_Position::line), "line"},
560               {fun(&File_Position::column), "column"} }
561             );
562 
563 
564         chaiscript::utility::add_class<AST_Node>(m,
565             "AST_Node",
566             {  },
567             { {fun(&AST_Node::text), "text"},
568               {fun(&AST_Node::identifier), "identifier"},
569               {fun(&AST_Node::filename), "filename"},
570               {fun(&AST_Node::start), "start"},
571               {fun(&AST_Node::end), "end"},
572               {fun(&AST_Node::to_string), "to_string"},
573               {fun([](const chaiscript::AST_Node &t_node) -> std::vector<Boxed_Value> {
574                 std::vector<Boxed_Value> retval;
575                 const auto children = t_node.get_children();
576                 std::transform(children.begin(), children.end(),
577                                std::back_inserter(retval),
578                                &chaiscript::var<const std::reference_wrapper<chaiscript::AST_Node> &>);
579                 return retval;
580               }), "children"}
581             }
582             );
583 
584       }
585     };
586   }
587 }
588 
589 #endif
590 
591