Revamped method for bootstrapping of types, using a new Module class that collects everything related to a type or group of types

This commit is contained in:
Jason Turner 2009-07-19 03:04:46 +00:00
parent 5a5b2929b0
commit a5a1e3ee1b
7 changed files with 616 additions and 486 deletions

File diff suppressed because it is too large Load Diff

View File

@ -10,8 +10,8 @@
* http://www.sgi.com/tech/stl/table_of_contents.html
*/
#ifndef __bootstrap_stl_hpp
#define __bootstrap_stl_hpp__
#ifndef __stl_hpp_type
#define __stl_hpp___type
#include "dispatchkit.hpp"
#include "register_function.hpp"
@ -74,24 +74,25 @@ namespace chaiscript
* Add Input_Range support for the given ContainerType
*/
template<typename ContainerType>
void bootstrap_input_range(Dispatch_Engine &system, const std::string &type)
ModulePtr input_range_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
system.add(user_type<Input_Range<ContainerType> >(), type + "_Range");
system.add(user_type<typename ContainerType::iterator>(), type+"_Iterator");
m->add(user_type<Input_Range<ContainerType> >(), type + "_Range");
m->add(user_type<typename ContainerType::iterator>(), type+"_Iterator");
system.add(constructor<Input_Range<ContainerType> (ContainerType &)>(), "range");
system.add(constructor<Input_Range<ContainerType> (typename ContainerType::iterator)>(), "range");
m->add(constructor<Input_Range<ContainerType> (ContainerType &)>(), "range");
m->add(constructor<Input_Range<ContainerType> (typename ContainerType::iterator)>(), "range");
typedef std::pair<typename ContainerType::iterator, typename ContainerType::iterator> ItrPair;
system.add(constructor<Input_Range<ContainerType> (const ItrPair &)>(), "range");
m->add(constructor<Input_Range<ContainerType> (const ItrPair &)>(), "range");
system.add(user_type<ItrPair>(), type+"_Iterator_Pair");
m->add(user_type<ItrPair>(), type+"_Iterator_Pair");
system.add(fun(&Input_Range<ContainerType>::empty), "empty");
system.add(fun(&Input_Range<ContainerType>::pop_front), "pop_front");
system.add(fun(&Input_Range<ContainerType>::front), "front");
system.add(constructor<Input_Range<ContainerType> (const Input_Range<ContainerType> &)>(), "clone");
m->add(fun(&Input_Range<ContainerType>::empty), "empty");
m->add(fun(&Input_Range<ContainerType>::pop_front), "pop_front");
m->add(fun(&Input_Range<ContainerType>::front), "front");
m->add(constructor<Input_Range<ContainerType> (const Input_Range<ContainerType> &)>(), "clone");
return m;
}
/**
@ -99,8 +100,9 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/ReversibleContainer.html
*/
template<typename ContainerType>
void bootstrap_reversible_container(Dispatch_Engine &/*system*/, const std::string &/*type*/)
ModulePtr reversible_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
return m;
}
/**
@ -108,18 +110,19 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/RandomAccessContainer.html
*/
template<typename ContainerType>
void bootstrap_random_access_container(Dispatch_Engine &system, const std::string &type)
ModulePtr random_access_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
bootstrap_reversible_container<ContainerType>(system, type);
reversible_container_type<ContainerType>(type, m);
typedef typename ContainerType::reference(ContainerType::*indexoper)(size_t);
//In the interest of runtime safety for the system, we prefer the at() method for [] access,
//In the interest of runtime safety for the m, we prefer the at() method for [] access,
//to throw an exception in an out of bounds condition.
system.add(
m->add(
fun(boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::at))), "[]");
system.add(
m->add(
fun(boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::operator[]))), "at");
return m;
}
/**
@ -127,10 +130,11 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/Assignable.html
*/
template<typename ContainerType>
void bootstrap_assignable(Dispatch_Engine &system, const std::string &type)
ModulePtr assignable_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
add_basic_constructors<ContainerType>(system, type);
add_oper_assign<ContainerType>(system);
add_basic_constructors<ContainerType>(type, m);
add_oper_assign<ContainerType>(m);
return m;
}
/**
@ -138,13 +142,15 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/Container.html
*/
template<typename ContainerType>
void bootstrap_container(Dispatch_Engine &system, const std::string &type)
ModulePtr container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
bootstrap_assignable<ContainerType>(system, type);
assignable_type<ContainerType>(type, m);
system.add(fun(&ContainerType::size), "size");
system.add(fun(&ContainerType::max_size), "max_size");
system.add(fun(&ContainerType::empty), "empty");
m->add(fun(&ContainerType::size), "size");
m->add(fun(&ContainerType::max_size), "max_size");
m->add(fun(&ContainerType::empty), "empty");
return m;
}
/**
@ -152,10 +158,12 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/ForwardContainer.html
*/
template<typename ContainerType>
void bootstrap_forward_container(Dispatch_Engine &system, const std::string &type)
ModulePtr forward_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
bootstrap_input_range<ContainerType>(system, type);
bootstrap_container<ContainerType>(system, type);
input_range_type<ContainerType>(type, m);
container_type<ContainerType>(type, m);
return m;
}
/**
@ -163,9 +171,10 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/DefaultConstructible.html
*/
template<typename Type>
void bootstrap_default_constructible(Dispatch_Engine &system, const std::string &type)
ModulePtr default_constructible_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
system.add(constructor<Type ()>(), type);
m->add(constructor<Type ()>(), type);
return m;
}
/**
@ -209,10 +218,10 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/Sequence.html
*/
template<typename ContainerType>
void bootstrap_sequence(Dispatch_Engine &system, const std::string &type)
ModulePtr sequence_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
bootstrap_forward_container<ContainerType>(system, type);
bootstrap_default_constructible<ContainerType>(system, type);
forward_container_type<ContainerType>(type, m);
default_constructible_type<ContainerType>(type, m);
std::string insert_name;
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value))
@ -222,8 +231,10 @@ namespace chaiscript
insert_name = "insert_at";
}
system.add(fun(&insert_at<ContainerType>), insert_name);
system.add(fun(&erase_at<ContainerType>), "erase_at");
m->add(fun(&insert_at<ContainerType>), insert_name);
m->add(fun(&erase_at<ContainerType>), "erase_at");
return m;
}
/**
@ -231,14 +242,14 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/BackInsertionSequence.html
*/
template<typename ContainerType>
void bootstrap_back_insertion_sequence(Dispatch_Engine &system, const std::string &type)
ModulePtr back_insertion_sequence_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
bootstrap_sequence<ContainerType>(system, type);
sequence_type<ContainerType>(type, m);
typedef typename ContainerType::reference (ContainerType::*backptr)();
system.add(fun(backptr(&ContainerType::back)), "back");
m->add(fun(backptr(&ContainerType::back)), "back");
std::string push_back_name;
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value))
@ -248,8 +259,9 @@ namespace chaiscript
push_back_name = "push_back";
}
system.add(fun(&ContainerType::push_back), push_back_name);
system.add(fun(&ContainerType::pop_back), "pop_back");
m->add(fun(&ContainerType::push_back), push_back_name);
m->add(fun(&ContainerType::pop_back), "pop_back");
return m;
}
/**
@ -257,11 +269,12 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/Vector.html
*/
template<typename VectorType>
void bootstrap_vector(Dispatch_Engine &system, const std::string &type)
ModulePtr vector_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
system.add(user_type<VectorType>(), type);
bootstrap_random_access_container<VectorType>(system, type);
bootstrap_back_insertion_sequence<VectorType>(system, type);
m->add(user_type<VectorType>(), type);
random_access_container_type<VectorType>(type, m);
back_insertion_sequence_type<VectorType>(type, m);
return m;
}
/**
@ -269,10 +282,11 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/Vector.html
*/
template<typename ContainerType>
void bootstrap_associative_container(Dispatch_Engine &system, const std::string &type)
ModulePtr associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
bootstrap_forward_container<ContainerType>(system, type);
bootstrap_default_constructible<ContainerType>(system, type);
forward_container_type<ContainerType>(type, m);
default_constructible_type<ContainerType>(type, m);
return m;
}
/**
@ -280,18 +294,20 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/pair.html
*/
template<typename PairType>
void bootstrap_pair(Dispatch_Engine &system, const std::string &type)
ModulePtr pair_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
system.add(user_type<PairType>(), type);
m->add(user_type<PairType>(), type);
system.add(fun(&PairType::first), "first");
system.add(fun(&PairType::second), "second");
m->add(fun(&PairType::first), "first");
m->add(fun(&PairType::second), "second");
system.add(constructor<PairType ()>(), type);
system.add(constructor<PairType (const PairType &)>(), type);
system.add(constructor<PairType (const PairType &)>(), "clone");
system.add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type);
}
m->add(constructor<PairType ()>(), type);
m->add(constructor<PairType (const PairType &)>(), type);
m->add(constructor<PairType (const PairType &)>(), "clone");
m->add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type);
return m;
}
/**
@ -299,10 +315,12 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/PairAssociativeContainer.html
*/
template<typename ContainerType>
void bootstrap_pair_associative_container(Dispatch_Engine &system, const std::string &type)
ModulePtr pair_associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
bootstrap_associative_container<ContainerType>(system, type);
bootstrap_pair<typename ContainerType::value_type>(system, type + "_Pair");
associative_container_type<ContainerType>(type, m);
pair_type<typename ContainerType::value_type>(type + "_Pair", m);
return m;
}
/**
@ -310,10 +328,12 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html
*/
template<typename ContainerType>
void bootstrap_unique_associative_container(Dispatch_Engine &system, const std::string &type)
ModulePtr unique_associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
bootstrap_associative_container<ContainerType>(system, type);
system.add(fun(&ContainerType::count), "count");
associative_container_type<ContainerType>(type, m);
m->add(fun(&ContainerType::count), "count");
return m;
}
/**
@ -321,14 +341,16 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/SortedAssociativeContainer.html
*/
template<typename ContainerType>
void bootstrap_sorted_associative_container(Dispatch_Engine &system, const std::string &type)
ModulePtr sorted_associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
typedef std::pair<typename ContainerType::iterator, typename ContainerType::iterator>
(ContainerType::*eq_range)(const typename ContainerType::key_type &);
bootstrap_reversible_container<ContainerType>(system, type);
bootstrap_associative_container<ContainerType>(system, type);
system.add(fun(eq_range(&ContainerType::equal_range)), "equal_range");
reversible_container_type<ContainerType>(type, m);
associative_container_type<ContainerType>(type, m);
m->add(fun(eq_range(&ContainerType::equal_range)), "equal_range");
return m;
}
/**
@ -336,10 +358,12 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/UniqueSortedAssociativeContainer.html
*/
template<typename ContainerType>
void bootstrap_unique_sorted_associative_container(Dispatch_Engine &system, const std::string &type)
ModulePtr unique_sorted_associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
bootstrap_sorted_associative_container<ContainerType>(system, type);
bootstrap_unique_associative_container<ContainerType>(system, type);
sorted_associative_container_type<ContainerType>(type, m);
unique_associative_container_type<ContainerType>(type, m);
return m;
}
/**
@ -347,12 +371,14 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/Map.html
*/
template<typename MapType>
void bootstrap_map(Dispatch_Engine &system, const std::string &type)
ModulePtr map_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
system.add(user_type<MapType>(), type);
system.add(fun(&MapType::operator[]), "[]");
bootstrap_unique_sorted_associative_container<MapType>(system, type);
bootstrap_pair_associative_container<MapType>(system, type);
m->add(user_type<MapType>(), type);
m->add(fun(&MapType::operator[]), "[]");
unique_sorted_associative_container_type<MapType>(type, m);
pair_associative_container_type<MapType>(type, m);
return m;
}
/**
@ -360,21 +386,23 @@ namespace chaiscript
* http://www.sgi.com/tech/stl/basic_string.html
*/
template<typename String>
void bootstrap_string(Dispatch_Engine &system, const std::string &type)
ModulePtr string_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
system.add(user_type<String>(), type);
add_oper_add<String>(system);
add_oper_add_equals<String>(system);
add_opers_comparison<String>(system);
bootstrap_random_access_container<String>(system, type);
bootstrap_sequence<String>(system, type);
m->add(user_type<String>(), type);
add_oper_add<String>(m);
add_oper_add_equals<String>(m);
add_opers_comparison<String>(m);
random_access_container_type<String>(type, m);
sequence_type<String>(type, m);
typedef typename String::size_type (String::*find_func)(const String &, typename String::size_type) const;
system.add(fun(find_func(&String::find)), "find");
system.add(fun(find_func(&String::rfind)), "rfind");
system.add(fun(find_func(&String::find_first_of)), "find_first_of");
system.add(fun(find_func(&String::find_last_of)), "find_last_of");
system.add(fun(find_func(&String::find_first_not_of)), "find_first_not_of");
system.add(fun(find_func(&String::find_last_not_of)), "find_last_not_of");
m->add(fun(find_func(&String::find)), "find");
m->add(fun(find_func(&String::rfind)), "rfind");
m->add(fun(find_func(&String::find_first_of)), "find_first_of");
m->add(fun(find_func(&String::find_last_of)), "find_last_of");
m->add(fun(find_func(&String::find_first_not_of)), "find_first_not_of");
m->add(fun(find_func(&String::find_last_not_of)), "find_last_not_of");
return m;
}
}

View File

@ -682,6 +682,15 @@ namespace chaiscript
{
return Boxed_Value(t);
}
/**
* Return true if the two Boxed_Value's share the same internal type
*/
static bool type_match(Boxed_Value l, Boxed_Value r)
{
return l.get_type_info() == r.get_type_info();
}
}
#endif

View File

@ -25,6 +25,45 @@
namespace chaiscript
{
class Module
{
public:
Module &add(const Type_Info &ti, const std::string &name)
{
m_typeinfos.push_back(std::make_pair(ti, name));
return *this;
}
Module &add(const Proxy_Function &f, const std::string &name)
{
m_funcs.push_back(std::make_pair(f, name));
return *this;
}
template<typename T>
void apply(T &t) const
{
apply(m_typeinfos.begin(), m_typeinfos.end(), t);
apply(m_funcs.begin(), m_funcs.end(), t);
}
private:
std::vector<std::pair<Type_Info, std::string> > m_typeinfos;
std::vector<std::pair<Proxy_Function, std::string> > m_funcs;
template<typename T, typename InItr>
void apply(InItr begin, InItr end, T &t) const
{
while (begin != end)
{
t.add(begin->first, begin->second);
++begin;
}
}
};
typedef boost::shared_ptr<Module> ModulePtr;
/**
* A Proxy_Function implementation that is able to take
* a vector of Proxy_Functions and perform a dispatch on them. It is
@ -110,6 +149,14 @@ namespace chaiscript
return add_function(f, name);
}
/**
* Add a module's worth of registrations to the system
*/
void add(const ModulePtr &m)
{
m->apply(*this);
}
/**
* Set the value of an object, by name. If the object
* is not available in the current scope it is created
@ -411,6 +458,19 @@ namespace chaiscript
}
std::cout << std::endl;
}
/**
* return true if the Boxed_Value matches the registered type by name
*/
static bool is_type(const Dispatch_Engine &e, const std::string &user_typename, Boxed_Value r)
{
try {
return e.get_type(user_typename) == r.get_type_info();
} catch (const std::range_error &) {
return false;
}
}
}
#endif

View File

@ -59,11 +59,20 @@ namespace chaiscript
* Adds an object to the system: type, function, object
*/
template<typename T>
void add(const T &t, const std::string &name)
ChaiScript_System &add(const T &t, const std::string &name)
{
engine.add(t, name);
return *this;
}
/**
* Adds a module object to the system
*/
ChaiScript_System &add(const ModulePtr &p)
{
engine.add(p);
}
/**
* Helper for calling script code as if it were native C++ code
* example:
@ -129,11 +138,18 @@ namespace chaiscript
* Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
*/
void build_eval_system() {
Bootstrap::bootstrap(engine);
bootstrap_vector<std::vector<Boxed_Value> >(engine, "Vector");
bootstrap_string<std::string>(engine, "string");
bootstrap_map<std::map<std::string, Boxed_Value> >(engine, "Map");
bootstrap_pair<std::pair<Boxed_Value, Boxed_Value > >(engine, "Pair");
engine.add(Bootstrap::bootstrap());
engine.add(fun(boost::function<void ()>(boost::bind(&dump_system, boost::ref(engine)))), "dump_system");
engine.add(fun(boost::function<void (Boxed_Value)>(boost::bind(&dump_object, _1, boost::ref(engine)))), "dump_object");
engine.add(fun(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(engine), _2, _1))),
"is_type");
engine.add(vector_type<std::vector<Boxed_Value> >("Vector"));
engine.add(string_type<std::string>("string"));
engine.add(map_type<std::map<std::string, Boxed_Value> >("Map"));
engine.add(pair_type<std::pair<Boxed_Value, Boxed_Value > >("Pair"));
engine.add(Proxy_Function(
new Dynamic_Proxy_Function(boost::bind(&ChaiScript_System<Eval_Engine>::internal_eval, boost::ref(*this), _1), 1)), "eval");

View File

@ -138,7 +138,7 @@ namespace chaiscript
}
else if (node->children[i+1]->text == ":=") {
Boxed_Value lhs = eval_token(ss, node->children[i]);
if (lhs.is_unknown() || Bootstrap::type_match(lhs, retval)) {
if (lhs.is_unknown() || type_match(lhs, retval)) {
lhs.assign(retval);
}
else {

View File

@ -115,11 +115,17 @@ int main(int argc, char *argv[]) {
// Add usage model for mixed use:
// chai.eval("call(?, ?)", 5, "hello world"); or something
// add examples for and clean up usage of bootstrap stuffs
//Creating a functor on the stack and using it immediatly
int x = chai.functor<int (int, int)>("fun (x, y) { return x + y; }")(5, 6);
log("Functor test output", boost::lexical_cast<std::string>(x));
//Ability to create our own container types when needed. std::vector and std::map are
//mostly supported currently
chai.add(vector_type<std::vector<int> >("IntVector"));
}