More usage cleansups
This commit is contained in:
@@ -489,7 +489,7 @@ namespace chaiscript
|
||||
template<typename T>
|
||||
void bootstrap_pod_type(Dispatch_Engine &s, const std::string &name)
|
||||
{
|
||||
s.add(type_<T>(), name);
|
||||
s.add(user_type<T>(), name);
|
||||
add_basic_constructors<T>(s, name);
|
||||
add_oper_assign<T>(s);
|
||||
add_oper_assign_pod<T>(s);
|
||||
@@ -590,10 +590,10 @@ namespace chaiscript
|
||||
/**
|
||||
* return true if the Boxed_Value matches the registered type by name
|
||||
*/
|
||||
static bool is_type(const Dispatch_Engine &e, const std::string &type_name, Boxed_Value r)
|
||||
static bool is_type(const Dispatch_Engine &e, const std::string &user_typename, Boxed_Value r)
|
||||
{
|
||||
try {
|
||||
return e.get_type(type_name) == r.get_type_info();
|
||||
return e.get_type(user_typename) == r.get_type_info();
|
||||
} catch (const std::range_error &) {
|
||||
return false;
|
||||
}
|
||||
@@ -638,11 +638,11 @@ namespace chaiscript
|
||||
*/
|
||||
static void bootstrap(Dispatch_Engine &s)
|
||||
{
|
||||
s.add(type_<void>(), "void");
|
||||
s.add(type_<bool>(), "bool");
|
||||
s.add(type_<Boxed_Value>(), "Object");
|
||||
s.add(type_<Boxed_POD_Value>(), "PODObject");
|
||||
s.add(type_<Proxy_Function>(), "function");
|
||||
s.add(user_type<void>(), "void");
|
||||
s.add(user_type<bool>(), "bool");
|
||||
s.add(user_type<Boxed_Value>(), "Object");
|
||||
s.add(user_type<Boxed_POD_Value>(), "PODObject");
|
||||
s.add(user_type<Proxy_Function>(), "function");
|
||||
|
||||
add_basic_constructors<bool>(s, "bool");
|
||||
add_oper_assign<std::string>(s);
|
||||
|
@@ -22,7 +22,7 @@ namespace chaiscript
|
||||
/**
|
||||
* Input_Range, based on the D concept of ranges.
|
||||
* \todo Update the Range code to base its capabilities on
|
||||
* the type_traits of the iterator passed in
|
||||
* the user_typetraits of the iterator passed in
|
||||
*/
|
||||
template<typename Container>
|
||||
struct Input_Range
|
||||
@@ -76,8 +76,8 @@ namespace chaiscript
|
||||
template<typename ContainerType>
|
||||
void bootstrap_input_range(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.add(type_<Input_Range<ContainerType> >(), type + "_Range");
|
||||
system.add(type_<typename ContainerType::iterator>(), type+"_Iterator");
|
||||
system.add(user_type<Input_Range<ContainerType> >(), type + "_Range");
|
||||
system.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");
|
||||
@@ -86,7 +86,7 @@ namespace chaiscript
|
||||
|
||||
system.add(constructor<Input_Range<ContainerType> (const ItrPair &)>(), "range");
|
||||
|
||||
system.add(type_<ItrPair>(), type+"_Iterator_Pair");
|
||||
system.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");
|
||||
@@ -259,7 +259,7 @@ namespace chaiscript
|
||||
template<typename VectorType>
|
||||
void bootstrap_vector(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.add(type_<VectorType>(), type);
|
||||
system.add(user_type<VectorType>(), type);
|
||||
bootstrap_random_access_container<VectorType>(system, type);
|
||||
bootstrap_back_insertion_sequence<VectorType>(system, type);
|
||||
}
|
||||
@@ -282,7 +282,7 @@ namespace chaiscript
|
||||
template<typename PairType>
|
||||
void bootstrap_pair(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.add(type_<PairType>(), type);
|
||||
system.add(user_type<PairType>(), type);
|
||||
|
||||
system.add(fun(&PairType::first), "first");
|
||||
system.add(fun(&PairType::second), "second");
|
||||
@@ -349,7 +349,7 @@ namespace chaiscript
|
||||
template<typename MapType>
|
||||
void bootstrap_map(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.add(type_<MapType>(), type);
|
||||
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);
|
||||
@@ -362,7 +362,7 @@ namespace chaiscript
|
||||
template<typename String>
|
||||
void bootstrap_string(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.add(type_<String>(), type);
|
||||
system.add(user_type<String>(), type);
|
||||
add_oper_add<String>(system);
|
||||
add_oper_add_equals<String>(system);
|
||||
add_opers_comparison<String>(system);
|
||||
|
@@ -118,14 +118,14 @@ namespace chaiscript
|
||||
|
||||
|
||||
template<typename T>
|
||||
Type_Info type_(T)
|
||||
Type_Info user_type(T)
|
||||
{
|
||||
return Get_Type_Info<T>::get();
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
Type_Info type_()
|
||||
Type_Info user_type()
|
||||
{
|
||||
return Get_Type_Info<T>::get();
|
||||
}
|
||||
|
@@ -21,6 +21,35 @@ namespace chaiscript
|
||||
Eval_Engine engine;
|
||||
ChaiScript_Parser parser;
|
||||
|
||||
/**
|
||||
* Evaluates the given string in by parsing it and running the results through the evaluator
|
||||
*/
|
||||
Boxed_Value do_eval(const std::string &input, const char *filename = "__EVAL__") {
|
||||
//debug_print(tokens);
|
||||
Boxed_Value value;
|
||||
parser.clear_match_stack();
|
||||
|
||||
try {
|
||||
if (parser.parse(input, filename)) {
|
||||
//parser.show_match_stack();
|
||||
value = eval_token<Eval_Engine>(engine, parser.ast());
|
||||
}
|
||||
}
|
||||
catch (const Return_Value &rv) {
|
||||
value = rv.retval;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the given boxed string, used during eval() inside of a script
|
||||
*/
|
||||
const Boxed_Value internal_eval(const std::vector<Boxed_Value> &vals) {
|
||||
return do_eval(boxed_cast<std::string>(vals.at(0)));
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
ChaiScript_System() {
|
||||
build_eval_system();
|
||||
@@ -34,6 +63,7 @@ namespace chaiscript
|
||||
{
|
||||
engine.add(t, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for calling script code as if it were native C++ code
|
||||
* example:
|
||||
@@ -44,9 +74,18 @@ namespace chaiscript
|
||||
template<typename FunctionType>
|
||||
boost::function<FunctionType> functor(const std::string &script)
|
||||
{
|
||||
return chaiscript::functor<FunctionType>(evaluate_string(script));
|
||||
return chaiscript::functor<FunctionType>(eval(script));
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate a string via eval method
|
||||
*/
|
||||
Boxed_Value operator()(const std::string &script)
|
||||
{
|
||||
return do_eval(script);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the current evaluation engine
|
||||
*/
|
||||
@@ -64,16 +103,6 @@ namespace chaiscript
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the given boxed string, used during eval() inside of a script
|
||||
*/
|
||||
const Boxed_Value eval(const std::vector<Boxed_Value> &vals) {
|
||||
std::string val;
|
||||
val = boxed_cast<std::string &>(vals[0]);
|
||||
|
||||
return evaluate_string(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for loading a file
|
||||
*/
|
||||
@@ -107,41 +136,39 @@ namespace chaiscript
|
||||
bootstrap_pair<std::pair<Boxed_Value, Boxed_Value > >(engine, "Pair");
|
||||
|
||||
engine.add(Proxy_Function(
|
||||
new Dynamic_Proxy_Function(boost::bind(&ChaiScript_System<Eval_Engine>::eval, boost::ref(*this), _1), 1)), "eval");
|
||||
new Dynamic_Proxy_Function(boost::bind(&ChaiScript_System<Eval_Engine>::internal_eval, boost::ref(*this), _1), 1)), "eval");
|
||||
|
||||
evaluate_string(chaiscript_prelude, "standard prelude");
|
||||
do_eval(chaiscript_prelude, "standard prelude");
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the given string in by parsing it and running the results through the evaluator
|
||||
*/
|
||||
Boxed_Value evaluate_string(const std::string &input, const char *filename = "__EVAL__") {
|
||||
//debug_print(tokens);
|
||||
Boxed_Value value;
|
||||
parser.clear_match_stack();
|
||||
template<typename T>
|
||||
T eval(const std::string &input)
|
||||
{
|
||||
return boxed_cast<T>(do_eval(input));
|
||||
}
|
||||
|
||||
try {
|
||||
if (parser.parse(input, filename)) {
|
||||
//parser.show_match_stack();
|
||||
value = eval_token<Eval_Engine>(engine, parser.ast());
|
||||
}
|
||||
}
|
||||
catch (const Return_Value &rv) {
|
||||
value = rv.retval;
|
||||
}
|
||||
|
||||
return value;
|
||||
Boxed_Value eval(const std::string &input)
|
||||
{
|
||||
return do_eval(input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the file specified by filename, evaluates it, and returns the result
|
||||
*/
|
||||
Boxed_Value evaluate_file(const char *filename) {
|
||||
return evaluate_string(load_file(filename), filename);
|
||||
Boxed_Value eval_file(const char *filename) {
|
||||
return do_eval(load_file(filename), filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the file specified by filename, evaluates it, and returns the as the specified type
|
||||
*/
|
||||
template<typename T>
|
||||
T eval_file(const char *filename) {
|
||||
return boxed_cast<T>(do_eval(load_file(filename), filename));
|
||||
}
|
||||
};
|
||||
|
||||
typedef ChaiScript_System<Dispatch_Engine> ChaiScript_Engine;
|
||||
typedef ChaiScript_System<Dispatch_Engine> ChaiScript;
|
||||
}
|
||||
#endif /* CHAISCRIPT_ENGINE_HPP_ */
|
||||
|
||||
|
Reference in New Issue
Block a user