Major updates to the C++ API. Please see trunk/src/example.cpp to follow along
This commit is contained in:
@@ -15,14 +15,14 @@ namespace chaiscript
|
||||
* Helper function that will set up the scope around a function call, including handling the named function parameters
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
const dispatchkit::Boxed_Value eval_function (Eval_System &ss, TokenPtr node, const std::vector<std::string> ¶m_names, const std::vector<dispatchkit::Boxed_Value> &vals) {
|
||||
const Boxed_Value eval_function (Eval_System &ss, TokenPtr node, const std::vector<std::string> ¶m_names, const std::vector<Boxed_Value> &vals) {
|
||||
ss.new_scope();
|
||||
|
||||
for (unsigned int i = 0; i < param_names.size(); ++i) {
|
||||
ss.add_object(param_names[i], vals[i]);
|
||||
}
|
||||
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value retval;
|
||||
|
||||
try {
|
||||
retval = eval_token(ss, node);
|
||||
@@ -42,8 +42,8 @@ namespace chaiscript
|
||||
* Evaluates the top-level file node
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_file(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_file(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i;
|
||||
for (i = 0; i < node->children.size(); ++i) {
|
||||
retval = eval_token(ss, node->children[i]);
|
||||
@@ -55,13 +55,13 @@ namespace chaiscript
|
||||
* Evaluates a variable or function name identifier
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_id(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value eval_id(Eval_System &ss, TokenPtr node) {
|
||||
|
||||
if (node->text == "true") {
|
||||
return dispatchkit::Boxed_Value(true);
|
||||
return Boxed_Value(true);
|
||||
}
|
||||
else if (node->text == "false") {
|
||||
return dispatchkit::Boxed_Value(false);
|
||||
return Boxed_Value(false);
|
||||
}
|
||||
else {
|
||||
try {
|
||||
@@ -77,68 +77,68 @@ namespace chaiscript
|
||||
* Evaluates a floating point number
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_float(Eval_System &, TokenPtr node) {
|
||||
return dispatchkit::Boxed_Value(double(atof(node->text.c_str())));
|
||||
Boxed_Value eval_float(Eval_System &, TokenPtr node) {
|
||||
return Boxed_Value(double(atof(node->text.c_str())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates an integer
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_int(Eval_System &, TokenPtr node) {
|
||||
return dispatchkit::Boxed_Value(atoi(node->text.c_str()));
|
||||
Boxed_Value eval_int(Eval_System &, TokenPtr node) {
|
||||
return Boxed_Value(atoi(node->text.c_str()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates a quoted string
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_quoted_string(Eval_System &, TokenPtr node) {
|
||||
return dispatchkit::Boxed_Value(node->text);
|
||||
Boxed_Value eval_quoted_string(Eval_System &, TokenPtr node) {
|
||||
return Boxed_Value(node->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates a char group
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_single_quoted_string(Eval_System &, TokenPtr node) {
|
||||
return dispatchkit::Boxed_Value(node->text);
|
||||
Boxed_Value eval_single_quoted_string(Eval_System &, TokenPtr node) {
|
||||
return Boxed_Value(node->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates a string of equations in reverse order so that the right-most side has precedence
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_equation(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_equation(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i;
|
||||
retval = eval_token(ss, node->children.back());
|
||||
if (node->children.size() > 1) {
|
||||
for (i = node->children.size()-3; ((int)i) >= 0; i -= 2) {
|
||||
if (node->children[i+1]->text == "=") {
|
||||
dispatchkit::Boxed_Value lhs = eval_token(ss, node->children[i]);
|
||||
Boxed_Value lhs = eval_token(ss, node->children[i]);
|
||||
try {
|
||||
if (lhs.is_unknown())
|
||||
{
|
||||
retval = dispatch(ss.get_function("clone"), dispatchkit::Param_List_Builder() << retval);
|
||||
retval = dispatch(ss.get_function("clone"), Param_List_Builder() << retval);
|
||||
}
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
Param_List_Builder plb;
|
||||
plb << lhs;
|
||||
plb << retval;
|
||||
try {
|
||||
retval = dispatch(ss.get_function(node->children[i+1]->text), plb);
|
||||
}
|
||||
catch(const dispatchkit::dispatch_error &){
|
||||
catch(const dispatch_error &){
|
||||
throw Eval_Error("Mismatched types in equation", node->children[i+1]);
|
||||
}
|
||||
}
|
||||
catch(const dispatchkit::dispatch_error &){
|
||||
catch(const dispatch_error &){
|
||||
throw Eval_Error("Can not clone right hand side of equation", node->children[i+1]);
|
||||
}
|
||||
}
|
||||
else if (node->children[i+1]->text == ":=") {
|
||||
dispatchkit::Boxed_Value lhs = eval_token(ss, node->children[i]);
|
||||
if (lhs.is_unknown() || dispatchkit::Bootstrap::type_match(lhs, retval)) {
|
||||
Boxed_Value lhs = eval_token(ss, node->children[i]);
|
||||
if (lhs.is_unknown() || Bootstrap::type_match(lhs, retval)) {
|
||||
lhs.assign(retval);
|
||||
}
|
||||
else {
|
||||
@@ -146,13 +146,13 @@ namespace chaiscript
|
||||
}
|
||||
}
|
||||
else {
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
Param_List_Builder plb;
|
||||
plb << eval_token(ss, node->children[i]);
|
||||
plb << retval;
|
||||
try {
|
||||
retval = dispatch(ss.get_function(node->children[i+1]->text), plb);
|
||||
}
|
||||
catch(const dispatchkit::dispatch_error &){
|
||||
catch(const dispatch_error &){
|
||||
throw Eval_Error("Can not find appropriate '" + node->children[i+1]->text + "'", node->children[i+1]);
|
||||
}
|
||||
}
|
||||
@@ -165,8 +165,8 @@ namespace chaiscript
|
||||
* Evaluates a variable declaration
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_var_decl(Eval_System &ss, TokenPtr node) {
|
||||
ss.add_object(node->children[0]->text, dispatchkit::Boxed_Value());
|
||||
Boxed_Value eval_var_decl(Eval_System &ss, TokenPtr node) {
|
||||
ss.add_object(node->children[0]->text, Boxed_Value());
|
||||
return ss.get_object(node->children[0]->text);
|
||||
}
|
||||
|
||||
@@ -174,8 +174,8 @@ namespace chaiscript
|
||||
* Evaluates binary boolean operators. Respects short-circuiting rules.
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_expression(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_expression(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i;
|
||||
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
@@ -183,9 +183,9 @@ namespace chaiscript
|
||||
for (i = 1; i < node->children.size(); i += 2) {
|
||||
bool lhs;
|
||||
try {
|
||||
lhs = dispatchkit::boxed_cast<bool &>(retval);
|
||||
lhs = boxed_cast<bool &>(retval);
|
||||
}
|
||||
catch (const dispatchkit::bad_boxed_cast &) {
|
||||
catch (const bad_boxed_cast &) {
|
||||
throw Eval_Error("Condition not boolean", node);
|
||||
}
|
||||
if (node->children[i]->text == "&&") {
|
||||
@@ -193,12 +193,12 @@ namespace chaiscript
|
||||
retval = eval_token(ss, node->children[i+1]);
|
||||
}
|
||||
else {
|
||||
retval = dispatchkit::Boxed_Value(false);
|
||||
retval = Boxed_Value(false);
|
||||
}
|
||||
}
|
||||
else if (node->children[i]->text == "||") {
|
||||
if (lhs) {
|
||||
retval = dispatchkit::Boxed_Value(true);
|
||||
retval = Boxed_Value(true);
|
||||
}
|
||||
else {
|
||||
retval = eval_token(ss, node->children[i+1]);
|
||||
@@ -213,21 +213,21 @@ namespace chaiscript
|
||||
* Evaluates comparison, additions, and multiplications and their relatives
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_comp_add_mul(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_comp_add_mul(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i;
|
||||
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
if (node->children.size() > 1) {
|
||||
for (i = 1; i < node->children.size(); i += 2) {
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
Param_List_Builder plb;
|
||||
plb << retval;
|
||||
plb << eval_token(ss, node->children[i + 1]);
|
||||
|
||||
try {
|
||||
retval = dispatch(ss.get_function(node->children[i]->text), plb);
|
||||
}
|
||||
catch(const dispatchkit::dispatch_error &){
|
||||
catch(const dispatch_error &){
|
||||
throw Eval_Error("Can not find appropriate '" + node->children[i]->text + "'", node->children[i]);
|
||||
}
|
||||
}
|
||||
@@ -240,13 +240,13 @@ namespace chaiscript
|
||||
* Evaluates an array lookup
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_array_call(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_array_call(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i;
|
||||
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
for (i = 1; i < node->children.size(); ++i) {
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
Param_List_Builder plb;
|
||||
plb << retval;
|
||||
plb << eval_token(ss, node->children[i]);
|
||||
try {
|
||||
@@ -255,7 +255,7 @@ namespace chaiscript
|
||||
catch(std::out_of_range &) {
|
||||
throw Eval_Error("Out of bounds exception", node);
|
||||
}
|
||||
catch(const dispatchkit::dispatch_error &){
|
||||
catch(const dispatch_error &){
|
||||
throw Eval_Error("Can not find appropriate array lookup '[]' " + node->children[i]->text, node->children[i]);
|
||||
}
|
||||
}
|
||||
@@ -267,13 +267,13 @@ namespace chaiscript
|
||||
* Evaluates a unary negation
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_negate(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_negate(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
Param_List_Builder plb;
|
||||
plb << retval;
|
||||
plb << dispatchkit::Boxed_Value(-1.0);
|
||||
plb << Boxed_Value(-1.0);
|
||||
|
||||
try {
|
||||
return dispatch(ss.get_function("*"), plb);
|
||||
@@ -287,29 +287,29 @@ namespace chaiscript
|
||||
* Evaluates a unary boolean not
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_not(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_not(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
|
||||
bool cond;
|
||||
try {
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
cond = dispatchkit::boxed_cast<bool &>(retval);
|
||||
cond = boxed_cast<bool &>(retval);
|
||||
}
|
||||
catch (const dispatchkit::bad_boxed_cast &) {
|
||||
catch (const bad_boxed_cast &) {
|
||||
throw Eval_Error("Boolean not('!') condition not boolean", node->children[0]);
|
||||
}
|
||||
return dispatchkit::Boxed_Value(!cond);
|
||||
return Boxed_Value(!cond);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates any unary prefix
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_prefix(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_prefix(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
|
||||
retval = eval_token(ss, node->children[1]);
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
Param_List_Builder plb;
|
||||
plb << retval;
|
||||
|
||||
try {
|
||||
@@ -324,25 +324,25 @@ namespace chaiscript
|
||||
* Evaluates (and generates) an inline array initialization
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_inline_array(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_inline_array(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i;
|
||||
|
||||
try {
|
||||
retval = dispatch(ss.get_function("Vector"), dispatchkit::Param_List_Builder());
|
||||
retval = dispatch(ss.get_function("Vector"), Param_List_Builder());
|
||||
if (node->children.size() > 0) {
|
||||
for (i = 0; i < node->children[0]->children.size(); ++i) {
|
||||
try {
|
||||
dispatchkit::Boxed_Value tmp = eval_token(ss, node->children[0]->children[i]);
|
||||
dispatch(ss.get_function("push_back"), dispatchkit::Param_List_Builder() << retval << tmp);
|
||||
Boxed_Value tmp = eval_token(ss, node->children[0]->children[i]);
|
||||
dispatch(ss.get_function("push_back"), Param_List_Builder() << retval << tmp);
|
||||
}
|
||||
catch (const dispatchkit::dispatch_error &) {
|
||||
catch (const dispatch_error &) {
|
||||
throw Eval_Error("Can not find appropriate 'push_back'", node->children[0]->children[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const dispatchkit::dispatch_error &) {
|
||||
catch (const dispatch_error &) {
|
||||
throw Eval_Error("Can not find appropriate 'Vector()'", node);
|
||||
}
|
||||
|
||||
@@ -353,13 +353,13 @@ namespace chaiscript
|
||||
* Evaluates (and generates) an inline range initialization
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_inline_range(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value eval_inline_range(Eval_System &ss, TokenPtr node) {
|
||||
try {
|
||||
return dispatch(ss.get_function("generate_range"), dispatchkit::Param_List_Builder()
|
||||
return dispatch(ss.get_function("generate_range"), Param_List_Builder()
|
||||
<< eval_token(ss, node->children[0]->children[0]->children[0])
|
||||
<< eval_token(ss, node->children[0]->children[0]->children[1]));
|
||||
}
|
||||
catch (const dispatchkit::dispatch_error &) {
|
||||
catch (const dispatch_error &) {
|
||||
throw Eval_Error("Unable to generate range vector", node);
|
||||
}
|
||||
}
|
||||
@@ -368,24 +368,24 @@ namespace chaiscript
|
||||
* Evaluates (and generates) an inline map initialization
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_inline_map(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_inline_map(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i;
|
||||
|
||||
try {
|
||||
retval = dispatch(ss.get_function("Map"), dispatchkit::Param_List_Builder());
|
||||
retval = dispatch(ss.get_function("Map"), Param_List_Builder());
|
||||
for (i = 0; i < node->children[0]->children.size(); ++i) {
|
||||
try {
|
||||
dispatchkit::Boxed_Value key = eval_token(ss, node->children[0]->children[i]->children[0]);
|
||||
dispatchkit::Boxed_Value slot = dispatch(ss.get_function("[]"), dispatchkit::Param_List_Builder() << retval << key);
|
||||
dispatch(ss.get_function("="), dispatchkit::Param_List_Builder() << slot << eval_token(ss, node->children[0]->children[i]->children[1]));
|
||||
Boxed_Value key = eval_token(ss, node->children[0]->children[i]->children[0]);
|
||||
Boxed_Value slot = dispatch(ss.get_function("[]"), Param_List_Builder() << retval << key);
|
||||
dispatch(ss.get_function("="), Param_List_Builder() << slot << eval_token(ss, node->children[0]->children[i]->children[1]));
|
||||
}
|
||||
catch (const dispatchkit::dispatch_error &) {
|
||||
catch (const dispatch_error &) {
|
||||
throw Eval_Error("Can not find appropriate '=' for map init", node->children[0]->children[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const dispatchkit::dispatch_error &) {
|
||||
catch (const dispatch_error &) {
|
||||
throw Eval_Error("Can not find appropriate 'Map()'", node);
|
||||
}
|
||||
|
||||
@@ -396,21 +396,21 @@ namespace chaiscript
|
||||
* Evaluates a function call, starting with its arguments. Handles resetting the scope to the previous one after the call.
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_fun_call(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
dispatchkit::Dispatch_Engine::Stack prev_stack = ss.get_stack();
|
||||
dispatchkit::Dispatch_Engine::Stack new_stack;
|
||||
Boxed_Value eval_fun_call(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
Param_List_Builder plb;
|
||||
Dispatch_Engine::Stack prev_stack = ss.get_stack();
|
||||
Dispatch_Engine::Stack new_stack;
|
||||
unsigned int i;
|
||||
|
||||
new_stack.push_back(dispatchkit::Dispatch_Engine::Scope());
|
||||
new_stack.push_back(Dispatch_Engine::Scope());
|
||||
|
||||
if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) {
|
||||
for (i = 0; i < node->children[1]->children.size(); ++i) {
|
||||
plb << eval_token(ss, node->children[1]->children[i]);
|
||||
}
|
||||
}
|
||||
dispatchkit::Boxed_Value fn;
|
||||
Boxed_Value fn;
|
||||
try {
|
||||
fn = eval_token(ss, node->children[0]);
|
||||
}
|
||||
@@ -420,10 +420,10 @@ namespace chaiscript
|
||||
}
|
||||
try {
|
||||
ss.set_stack(new_stack);
|
||||
retval = (*dispatchkit::boxed_cast<boost::shared_ptr<dispatchkit::Proxy_Function> >(fn))(plb);
|
||||
retval = (*boxed_cast<Proxy_Function >(fn))(plb);
|
||||
ss.set_stack(prev_stack);
|
||||
}
|
||||
catch(const dispatchkit::dispatch_error &e){
|
||||
catch(const dispatch_error &e){
|
||||
ss.set_stack(prev_stack);
|
||||
throw Eval_Error(std::string(e.what()) + " with function '" + node->children[0]->text + "'", node->children[0]);
|
||||
}
|
||||
@@ -443,21 +443,21 @@ namespace chaiscript
|
||||
* Evaluates a method/attributes invocation
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_dot_access(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
std::vector<std::pair<std::string, boost::shared_ptr<dispatchkit::Proxy_Function> > > fn;
|
||||
dispatchkit::Dispatch_Engine::Stack prev_stack = ss.get_stack();
|
||||
dispatchkit::Dispatch_Engine::Stack new_stack;
|
||||
Boxed_Value eval_dot_access(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
std::vector<std::pair<std::string, Proxy_Function > > fn;
|
||||
Dispatch_Engine::Stack prev_stack = ss.get_stack();
|
||||
Dispatch_Engine::Stack new_stack;
|
||||
unsigned int i, j;
|
||||
|
||||
new_stack.push_back(dispatchkit::Dispatch_Engine::Scope());
|
||||
new_stack.push_back(Dispatch_Engine::Scope());
|
||||
|
||||
//todo: Please extract a single way of doing function calls between this and eval_fun_call
|
||||
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
if (node->children.size() > 1) {
|
||||
for (i = 1; i < node->children.size(); ++i) {
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
Param_List_Builder plb;
|
||||
plb << retval;
|
||||
|
||||
if (node->children[i]->children.size() > 1) {
|
||||
@@ -480,7 +480,7 @@ namespace chaiscript
|
||||
retval = dispatch(fn, plb);
|
||||
ss.set_stack(prev_stack);
|
||||
}
|
||||
catch(const dispatchkit::dispatch_error &e){
|
||||
catch(const dispatch_error &e){
|
||||
ss.set_stack(prev_stack);
|
||||
throw Eval_Error(std::string(e.what()) + " with function '" + fun_name + "'", node->children[i]);
|
||||
}
|
||||
@@ -502,16 +502,16 @@ namespace chaiscript
|
||||
* Evaluates an if/elseif/else block
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_if(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_if(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i;
|
||||
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
bool cond;
|
||||
try {
|
||||
cond = dispatchkit::boxed_cast<bool &>(retval);
|
||||
cond = boxed_cast<bool &>(retval);
|
||||
}
|
||||
catch (const dispatchkit::bad_boxed_cast &) {
|
||||
catch (const bad_boxed_cast &) {
|
||||
throw Eval_Error("If condition not boolean", node->children[0]);
|
||||
}
|
||||
if (cond) {
|
||||
@@ -528,9 +528,9 @@ namespace chaiscript
|
||||
else if (node->children[i]->text == "else if") {
|
||||
retval = eval_token(ss, node->children[i+1]);
|
||||
try {
|
||||
cond = dispatchkit::boxed_cast<bool &>(retval);
|
||||
cond = boxed_cast<bool &>(retval);
|
||||
}
|
||||
catch (const dispatchkit::bad_boxed_cast &) {
|
||||
catch (const bad_boxed_cast &) {
|
||||
throw Eval_Error("'else if' condition not boolean", node->children[i+1]);
|
||||
}
|
||||
if (cond) {
|
||||
@@ -549,21 +549,21 @@ namespace chaiscript
|
||||
* Evaluates a while block
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_while(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value eval_while(Eval_System &ss, TokenPtr node) {
|
||||
bool cond;
|
||||
try {
|
||||
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[0]));
|
||||
cond = boxed_cast<bool &>(eval_token(ss, node->children[0]));
|
||||
}
|
||||
catch (const dispatchkit::bad_boxed_cast &) {
|
||||
catch (const bad_boxed_cast &) {
|
||||
throw Eval_Error("While condition not boolean", node->children[0]);
|
||||
}
|
||||
while (cond) {
|
||||
try {
|
||||
eval_token(ss, node->children[1]);
|
||||
try {
|
||||
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[0]));
|
||||
cond = boxed_cast<bool &>(eval_token(ss, node->children[0]));
|
||||
}
|
||||
catch (const dispatchkit::bad_boxed_cast &) {
|
||||
catch (const bad_boxed_cast &) {
|
||||
throw Eval_Error("While condition not boolean", node->children[0]);
|
||||
}
|
||||
}
|
||||
@@ -571,26 +571,26 @@ namespace chaiscript
|
||||
cond = false;
|
||||
}
|
||||
}
|
||||
return dispatchkit::Boxed_Value();
|
||||
return Boxed_Value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates a for block, including the for's conditions, from left to right
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_for(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value eval_for(Eval_System &ss, TokenPtr node) {
|
||||
bool cond;
|
||||
|
||||
try {
|
||||
if (node->children.size() == 4) {
|
||||
eval_token(ss, node->children[0]);
|
||||
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[1]));
|
||||
cond = boxed_cast<bool &>(eval_token(ss, node->children[1]));
|
||||
}
|
||||
else {
|
||||
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[0]));
|
||||
cond = boxed_cast<bool &>(eval_token(ss, node->children[0]));
|
||||
}
|
||||
}
|
||||
catch (const dispatchkit::bad_boxed_cast &) {
|
||||
catch (const bad_boxed_cast &) {
|
||||
throw Eval_Error("For condition not boolean", node);
|
||||
}
|
||||
while (cond) {
|
||||
@@ -598,35 +598,35 @@ namespace chaiscript
|
||||
if (node->children.size() == 4) {
|
||||
eval_token(ss, node->children[3]);
|
||||
eval_token(ss, node->children[2]);
|
||||
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[1]));
|
||||
cond = boxed_cast<bool &>(eval_token(ss, node->children[1]));
|
||||
}
|
||||
else {
|
||||
eval_token(ss, node->children[2]);
|
||||
eval_token(ss, node->children[1]);
|
||||
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[0]));
|
||||
cond = boxed_cast<bool &>(eval_token(ss, node->children[0]));
|
||||
}
|
||||
}
|
||||
catch (const dispatchkit::bad_boxed_cast &) {
|
||||
catch (const bad_boxed_cast &) {
|
||||
throw Eval_Error("For condition not boolean", node);
|
||||
}
|
||||
catch (Break_Loop &) {
|
||||
cond = false;
|
||||
}
|
||||
}
|
||||
return dispatchkit::Boxed_Value();
|
||||
return Boxed_Value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates a function definition
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_def(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_def(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i;
|
||||
|
||||
std::vector<std::string> param_names;
|
||||
std::string annotation = node->annotation?node->annotation->text:"";
|
||||
boost::shared_ptr<dispatchkit::Dynamic_Proxy_Function> guard;
|
||||
boost::shared_ptr<Dynamic_Proxy_Function> guard;
|
||||
size_t numparams = 0;
|
||||
std::string function_name = node->children[0]->text;
|
||||
TokenPtr guardnode;
|
||||
@@ -651,14 +651,14 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
if (guardnode) {
|
||||
guard = boost::shared_ptr<dispatchkit::Dynamic_Proxy_Function>
|
||||
(new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>,
|
||||
guard = boost::shared_ptr<Dynamic_Proxy_Function>
|
||||
(new Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>,
|
||||
boost::ref(ss), guardnode,
|
||||
param_names, _1), numparams));
|
||||
}
|
||||
|
||||
ss.register_function(boost::shared_ptr<dispatchkit::Proxy_Function>
|
||||
(new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>,
|
||||
ss.add(Proxy_Function
|
||||
(new Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>,
|
||||
boost::ref(ss), node->children.back(),
|
||||
param_names, _1), numparams,
|
||||
annotation, guard)), function_name);
|
||||
@@ -670,8 +670,8 @@ namespace chaiscript
|
||||
* Evaluates a lambda (anonymous function)
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_lambda(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_lambda(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i;
|
||||
|
||||
std::vector<std::string> param_names;
|
||||
@@ -689,8 +689,8 @@ namespace chaiscript
|
||||
numparams = 0;
|
||||
}
|
||||
|
||||
return dispatchkit::Boxed_Value(boost::shared_ptr<dispatchkit::Proxy_Function>(
|
||||
new dispatchkit::Dynamic_Proxy_Function(
|
||||
return Boxed_Value(Proxy_Function(
|
||||
new Dynamic_Proxy_Function(
|
||||
boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), numparams)));
|
||||
}
|
||||
|
||||
@@ -698,8 +698,8 @@ namespace chaiscript
|
||||
* Evaluates a scoped block. Handles resetting the scope after the block has completed.
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_block(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_block(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i;
|
||||
|
||||
ss.new_scope();
|
||||
@@ -726,13 +726,13 @@ namespace chaiscript
|
||||
* Evaluates a return statement
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_return(Eval_System &ss, TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
Boxed_Value eval_return(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
if (node->children.size() > 0) {
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
}
|
||||
else {
|
||||
retval = dispatchkit::Boxed_Value();
|
||||
retval = Boxed_Value();
|
||||
}
|
||||
throw Return_Value(retval, node);
|
||||
}
|
||||
@@ -741,7 +741,7 @@ namespace chaiscript
|
||||
* Evaluates a break statement
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_break(Eval_System &, TokenPtr node) {
|
||||
Boxed_Value eval_break(Eval_System &, TokenPtr node) {
|
||||
throw Break_Loop(node);
|
||||
}
|
||||
|
||||
@@ -749,7 +749,7 @@ namespace chaiscript
|
||||
* Top-level evaluation dispatch for all AST node types
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_token(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value eval_token(Eval_System &ss, TokenPtr node) {
|
||||
switch (node->identifier) {
|
||||
case (Token_Type::File) :
|
||||
return eval_file(ss, node);
|
||||
@@ -862,7 +862,7 @@ namespace chaiscript
|
||||
break;
|
||||
|
||||
default :
|
||||
return dispatchkit::Boxed_Value();
|
||||
return Boxed_Value();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user