Cleanup assignment eval

This commit is contained in:
Jason Turner
2015-01-31 14:40:26 -07:00
parent d558019bb3
commit b5188b9eda

View File

@@ -452,23 +452,20 @@ namespace chaiscript
virtual ~Equation_AST_Node() {} virtual ~Equation_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss); chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
Boxed_Value retval = this->children.back()->eval(t_ss); Boxed_Value rhs = this->children.back()->eval(t_ss);
if (this->children.size() > 1) {
Boxed_Value lhs = this->children[0]->eval(t_ss); Boxed_Value lhs = this->children[0]->eval(t_ss);
Operators::Opers oper = Operators::to_operator(this->children[1]->text); Operators::Opers oper = Operators::to_operator(this->children[1]->text);
if (oper != Operators::invalid && lhs.get_type_info().is_arithmetic() && if (oper != Operators::invalid && lhs.get_type_info().is_arithmetic() &&
retval.get_type_info().is_arithmetic()) rhs.get_type_info().is_arithmetic())
{ {
try { try {
retval = Boxed_Number::do_oper(oper, lhs, retval); return Boxed_Number::do_oper(oper, lhs, rhs);
} catch (const std::exception &) { } catch (const std::exception &) {
throw exception::eval_error("Error with unsupported arithmetic assignment operation"); throw exception::eval_error("Error with unsupported arithmetic assignment operation");
} }
} else if (this->children[1]->text == "=") { } else if (oper == Operators::assign) {
try { try {
if (lhs.is_undef()) { if (lhs.is_undef()) {
if (!this->children.empty() && if (!this->children.empty() &&
@@ -477,15 +474,15 @@ namespace chaiscript
{ {
/// \todo This does not handle the case of an unassigned reference variable /// \todo This does not handle the case of an unassigned reference variable
/// being assigned outside of its declaration /// being assigned outside of its declaration
lhs.assign(retval); lhs.assign(rhs);
return retval; return rhs;
} else { } else {
retval = t_ss.call_function("clone", retval); rhs = t_ss.call_function("clone", rhs);
} }
} }
try { try {
retval = t_ss.call_function(this->children[1]->text, std::move(lhs), retval); rhs = t_ss.call_function(this->children[1]->text, std::move(lhs), rhs);
} }
catch(const exception::dispatch_error &e){ catch(const exception::dispatch_error &e){
throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, e.functions, false, t_ss); throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, e.functions, false, t_ss);
@@ -496,21 +493,21 @@ namespace chaiscript
} }
} }
else if (this->children[1]->text == ":=") { else if (this->children[1]->text == ":=") {
if (lhs.is_undef() || Boxed_Value::type_match(lhs, retval)) { if (lhs.is_undef() || Boxed_Value::type_match(lhs, rhs)) {
lhs.assign(retval); lhs.assign(rhs);
} else { } else {
throw exception::eval_error("Mismatched types in equation"); throw exception::eval_error("Mismatched types in equation");
} }
} }
else { else {
try { try {
retval = t_ss.call_function(this->children[1]->text, std::move(lhs), retval); rhs = t_ss.call_function(this->children[1]->text, std::move(lhs), rhs);
} catch(const exception::dispatch_error &e){ } catch(const exception::dispatch_error &e){
throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, e.functions, false, t_ss); throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, e.functions, false, t_ss);
} }
} }
}
return retval; return rhs;
} }
}; };