More usage cleansups

This commit is contained in:
Jason Turner
2009-07-18 23:34:08 +00:00
parent 4d4c26bf73
commit 82bd46bb1a
6 changed files with 113 additions and 61 deletions

View File

@@ -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_ */