diff --git a/CMakeLists.txt b/CMakeLists.txt index f6183c1..1f463b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ else() endif() -option(BUILD_MODULES "Build Extra Modules (stl, reflection)" TRUE) +option(BUILD_MODULES "Build Extra Modules (stl)" TRUE) option(BUILD_SAMPLES "Build Samples Folder" FALSE) @@ -210,9 +210,7 @@ if(BUILD_MODULES) add_library(stl_extra MODULE src/stl_extra.cpp) target_link_libraries(stl_extra ${LIBS}) - add_library(reflection MODULE src/reflection.cpp) - target_link_libraries(reflection ${LIBS}) - set(MODULES stl_extra reflection) + set(MODULES stl_extra) endif() file(GLOB UNIT_TESTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/unittests/ ${CMAKE_CURRENT_SOURCE_DIR}/unittests/*.chai ${CMAKE_CURRENT_SOURCE_DIR}/unittests/3.x/*.chai) diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index 5a92948..c31b68c 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -32,6 +32,8 @@ #include "proxy_functions_detail.hpp" #include "register_function.hpp" #include "type_info.hpp" +#include "../utility/utility.hpp" +#include "../language/chaiscript_common.hpp" namespace chaiscript { @@ -357,12 +359,48 @@ namespace chaiscript return vbv; } + + static bool has_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) + { + std::shared_ptr pf + = std::dynamic_pointer_cast(t_pf); + if (pf) + { + if (pf->get_parse_tree()) + { + return true; + } else { + return false; + } + } else { + return false; + } + } + + static chaiscript::AST_NodePtr get_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) + { + std::shared_ptr pf + = std::dynamic_pointer_cast(t_pf); + if (pf) + { + if (pf->get_parse_tree()) + { + return pf->get_parse_tree(); + } else { + throw std::runtime_error("Function does not have a parse tree"); + } + } else { + throw std::runtime_error("Function does not have a parse tree"); + } + } + template static std::function (const dispatch::Proxy_Function_Base*)> return_boxed_value_vector(const Function &f) { return std::bind(&do_return_boxed_value_vector, f, std::placeholders::_1); } + public: /// \brief perform all common bootstrap functions for std::string, void and POD types /// \param[in,out] m Module to add bootstrapped functions to @@ -380,7 +418,7 @@ namespace chaiscript m->add(fun(&dispatch::Proxy_Function_Base::annotation), "get_annotation"); m->add(fun(&dispatch::Proxy_Function_Base::operator==), "=="); - + m->add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_param_types)), "get_param_types"); m->add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_contained_functions)), "get_contained_functions"); @@ -473,6 +511,68 @@ namespace chaiscript m->add(fun(&Boxed_Value::type_match), "type_match"); + + m->add(chaiscript::fun(&has_parse_tree), "has_parse_tree"); + m->add(chaiscript::fun(&get_parse_tree), "get_parse_tree"); + + m->add(chaiscript::base_class()); + +// chaiscript::bootstrap::standard_library::vector_type > >("AST_NodeVector", m); + + + chaiscript::utility::add_class(*m, + "eval_error", + { }, + { {fun(&chaiscript::exception::eval_error::reason), "reason"}, + {fun(std::function (const chaiscript::exception::eval_error &t_eval_error)>([](const chaiscript::exception::eval_error &t_eval_error) { + std::vector retval; + std::transform(t_eval_error.call_stack.begin(), t_eval_error.call_stack.end(), + std::back_inserter(retval), + &chaiscript::var>); + return retval; + })), "call_stack"} } + ); + + + chaiscript::utility::add_class(*m, + "File_Position", + { constructor(), + constructor() }, + { {fun(&File_Position::line), "line"}, + {fun(&File_Position::column), "column"} } + ); + + + chaiscript::utility::add_class(*m, + "AST_Node", + { }, + { {fun(&AST_Node::text), "text"}, + {fun(&AST_Node::identifier), "identifier"}, + {fun(&AST_Node::filename), "filename"}, + {fun(&AST_Node::start), "start"}, + {fun(&AST_Node::end), "end"}, + {fun(&AST_Node::internal_to_string), "internal_to_string"}, + {fun(std::function (const chaiscript::AST_Node &t_node)>([](const chaiscript::AST_Node &t_node) { + std::vector retval; + std::transform(t_node.children.begin(), t_node.children.end(), + std::back_inserter(retval), + &chaiscript::var>); + return retval; + })), "children"}, + {fun(&AST_Node::replace_child), "replace_child"} + } + ); + + + chaiscript::utility::add_class(*m, + "ChaiScript_Parser", + { constructor() }, + { {fun(&parser::ChaiScript_Parser::parse), "parse"}, + {fun(&parser::ChaiScript_Parser::ast), "ast"} } + ); + + + return m; } }; diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index ecf3743..6797e5d 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -185,7 +185,7 @@ namespace chaiscript copy_constructor(type + "_Range", m); - m->add(constructor(), "range"); + m->add(constructor(), "range_internal"); m->add(fun(&Bidir_Type::empty), "empty"); m->add(fun(&Bidir_Type::pop_front), "pop_front"); @@ -348,10 +348,12 @@ namespace chaiscript ModulePtr front_insertion_sequence_type(const std::string &, ModulePtr m = ModulePtr(new Module())) { typedef typename ContainerType::reference (ContainerType::*frontptr)(); + typedef typename ContainerType::const_reference (ContainerType::*constfrontptr)() const; typedef void (ContainerType::*pushptr)(typename ContainerType::const_reference); typedef void (ContainerType::*popptr)(); m->add(fun(static_cast(&ContainerType::front)), "front"); + m->add(fun(static_cast(&ContainerType::front)), "front"); std::string push_front_name; if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) @@ -475,7 +477,10 @@ namespace chaiscript m->add(user_type(), type); typedef typename VectorType::reference (VectorType::*frontptr)(); + typedef typename VectorType::const_reference (VectorType::*constfrontptr)() const; + m->add(fun(static_cast(&VectorType::front)), "front"); + m->add(fun(static_cast(&VectorType::front)), "front"); back_insertion_sequence_type(type, m); diff --git a/src/reflection.cpp b/src/reflection.cpp deleted file mode 100644 index cb369f2..0000000 --- a/src/reflection.cpp +++ /dev/null @@ -1,129 +0,0 @@ -// This file is distributed under the BSD License. -// See "license.txt" for details. -// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) -// Copyright 2009-2014, Jason Turner (jason@emptycrate.com) -// http://www.chaiscript.com - - -#include -#include -#include -#include -#include - - -// MSVC doesn't like that we are using C++ return types from our C declared module -// but this is the best way to do it for cross platform compatibility -#ifdef CHAISCRIPT_MSVC -#pragma warning(push) -#pragma warning(disable : 4190) -#endif - - -bool has_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) -{ - std::shared_ptr pf - = std::dynamic_pointer_cast(t_pf); - if (pf) - { - if (pf->get_parse_tree()) - { - return true; - } else { - return false; - } - } else { - return false; - } -} - -chaiscript::AST_NodePtr get_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) -{ - std::shared_ptr pf - = std::dynamic_pointer_cast(t_pf); - if (pf) - { - if (pf->get_parse_tree()) - { - return pf->get_parse_tree(); - } else { - throw std::runtime_error("Function does not have a parse tree"); - } - } else { - throw std::runtime_error("Function does not have a parse tree"); - } -} - - -#ifdef __llvm__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wreturn-type-c-linkage" -#endif - - -CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_reflection() -{ - chaiscript::ModulePtr m(new chaiscript::Module()); - - m->add(chaiscript::fun(&has_parse_tree), "has_parse_tree"); - m->add(chaiscript::fun(&get_parse_tree), "get_parse_tree"); - - m->add(chaiscript::base_class()); - - chaiscript::bootstrap::standard_library::vector_type > >("AST_NodeVector", m); - - using namespace chaiscript; - - chaiscript::utility::add_class(*m, - "eval_error", - { }, - { {fun(&chaiscript::exception::eval_error::reason), "reason"}, - {fun(&chaiscript::exception::eval_error::call_stack), "call_stack"} } - ); - - - chaiscript::utility::add_class(*m, - "File_Position", - { constructor(), - constructor() }, - { {fun(&File_Position::line), "line"}, - {fun(&File_Position::column), "column"} } - ); - - - chaiscript::utility::add_class(*m, - "AST_Node", - { }, - { {fun(&AST_Node::text), "text"}, - {fun(&AST_Node::identifier), "identifier"}, - {fun(&AST_Node::filename), "filename"}, - {fun(&AST_Node::start), "start"}, - {fun(&AST_Node::end), "end"}, - {fun(&AST_Node::internal_to_string), "internal_to_string"}, - {fun(&AST_Node::children), "children"}, - {fun(&AST_Node::replace_child), "replace_child"} - } - ); - - - chaiscript::utility::add_class(*m, - "ChaiScript_Parser", - { constructor() }, - { {fun(&parser::ChaiScript_Parser::parse), "parse"}, - {fun(&parser::ChaiScript_Parser::ast), "ast"} } - ); - - - - return m; -} - -#ifdef __llvm__ -#pragma clang diagnostic pop -#endif - - - -#ifdef CHAISCRIPT_MSVC -#pragma warning(pop) -#endif diff --git a/unittests/3.x/eval_error.chai b/unittests/3.x/eval_error.chai index d63ad75..39a6541 100644 --- a/unittests/3.x/eval_error.chai +++ b/unittests/3.x/eval_error.chai @@ -1,4 +1,3 @@ -load_module("reflection") def deep() { diff --git a/unittests/3.x/reflection_test.chai b/unittests/3.x/reflection_test.chai index 88b39cc..25a2416 100644 --- a/unittests/3.x/reflection_test.chai +++ b/unittests/3.x/reflection_test.chai @@ -1,4 +1,3 @@ -load_module("reflection") var parser := ChaiScript_Parser() var parse_success = parser.parse("3 + 4", "INPUT") var a := parser.ast() diff --git a/unittests/eval_error.chai b/unittests/eval_error.chai index b2737a1..fde7847 100644 --- a/unittests/eval_error.chai +++ b/unittests/eval_error.chai @@ -1,4 +1,3 @@ -load_module("reflection") def deep() { diff --git a/unittests/operator_scoping.chai b/unittests/operator_scoping.chai index 5718bd5..5621017 100644 --- a/unittests/operator_scoping.chai +++ b/unittests/operator_scoping.chai @@ -1,4 +1,3 @@ -load_module("reflection") try { eval("def `+`(x, y) \n { \n print(i); \n } \n \n var i = 10; \n \"1\" + 1;\n") diff --git a/unittests/reflection_test.chai b/unittests/reflection_test.chai index 42566bc..16ef7e9 100644 --- a/unittests/reflection_test.chai +++ b/unittests/reflection_test.chai @@ -1,4 +1,3 @@ -load_module("reflection") auto& parser = ChaiScript_Parser() auto parse_success = parser.parse("3 + 4", "INPUT") auto& a = parser.ast()