Change equation to clone rhs. Add := ref equation. Failed clones will ref copy

This commit is contained in:
Jonathan Turner 2009-07-01 16:48:27 +00:00
parent 5f96b39c08
commit 27f6ec7b70
2 changed files with 59 additions and 8 deletions

View File

@ -71,6 +71,56 @@ namespace chaiscript
retval = eval_token(ss, node->children.back()); retval = eval_token(ss, node->children.back());
if (node->children.size() > 1) { if (node->children.size() > 1) {
for (i = node->children.size()-3; ((int)i) >= 0; i -= 2) { 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]);
if (lhs.is_unknown() || dispatchkit::Bootstrap::type_match(lhs, retval)) {
try {
retval = dispatch(ss.get_function("clone"), dispatchkit::Param_List_Builder() << retval);
dispatchkit::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 &e){
throw EvalError("Can not find appropriate '" + node->children[i+1]->text + "'", node->children[i+1]);
}
}
catch(const dispatchkit::dispatch_error &e){
//throw EvalError("Can not clone right hand side of equation", node->children[i+1]);
dispatchkit::Param_List_Builder plb;
plb << lhs;
plb << retval;
try {
retval = dispatch(ss.get_function("="), plb);
}
catch(const dispatchkit::dispatch_error &e){
throw EvalError("Can not find appropriate '" + node->children[i+1]->text + "'", node->children[i+1]);
}
}
}
else {
throw EvalError("Mismatched types in 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)) {
dispatchkit::Param_List_Builder plb;
plb << lhs;
plb << retval;
try {
retval = dispatch(ss.get_function("="), plb);
}
catch(const dispatchkit::dispatch_error &e){
throw EvalError("Can not find appropriate '" + node->children[i+1]->text + "'", node->children[i+1]);
}
}
else {
throw EvalError("Mismatched types in equation", node->children[i+1]);
}
}
else {
dispatchkit::Param_List_Builder plb; dispatchkit::Param_List_Builder plb;
plb << eval_token(ss, node->children[i]); plb << eval_token(ss, node->children[i]);
plb << retval; plb << retval;
@ -82,6 +132,7 @@ namespace chaiscript
} }
} }
} }
}
break; break;
case (Token_Type::Var_Decl): { case (Token_Type::Var_Decl): {
ss.add_object(node->children[0]->text, dispatchkit::Boxed_Value()); ss.add_object(node->children[0]->text, dispatchkit::Boxed_Value());

View File

@ -1180,7 +1180,7 @@ namespace chaiscript
if (Expression()) { if (Expression()) {
retval = true; retval = true;
if (Symbol("=", true) || Symbol("+=", true) || Symbol("-=", true) || Symbol("*=", true) || Symbol("/=", true)) { if (Symbol("=", true) || Symbol(":=", true) || Symbol("+=", true) || Symbol("-=", true) || Symbol("*=", true) || Symbol("/=", true)) {
if (!Equation()) { if (!Equation()) {
throw Parse_Error("Incomplete equation", match_stack.back()); throw Parse_Error("Incomplete equation", match_stack.back());
} }