From 27f6ec7b70fbbf2c1e26ef4a5b3f2a6bfb884078 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Wed, 1 Jul 2009 16:48:27 +0000 Subject: [PATCH] Change equation to clone rhs. Add := ref equation. Failed clones will ref copy --- chaiscript/chaiscript_eval.hpp | 65 ++++++++++++++++++++++++++++---- chaiscript/chaiscript_parser.hpp | 2 +- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/chaiscript/chaiscript_eval.hpp b/chaiscript/chaiscript_eval.hpp index e68e1654..f571b390 100644 --- a/chaiscript/chaiscript_eval.hpp +++ b/chaiscript/chaiscript_eval.hpp @@ -71,14 +71,65 @@ namespace chaiscript retval = eval_token(ss, node->children.back()); if (node->children.size() > 1) { for (i = node->children.size()-3; ((int)i) >= 0; i -= 2) { - dispatchkit::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); + 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]); + } } - catch(const dispatchkit::dispatch_error &e){ - throw EvalError("Can not find appropriate '" + node->children[i+1]->text + "'", 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; + 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 &e){ + throw EvalError("Can not find appropriate '" + node->children[i+1]->text + "'", node->children[i+1]); + } } } } diff --git a/chaiscript/chaiscript_parser.hpp b/chaiscript/chaiscript_parser.hpp index 284cdee6..95a80d58 100644 --- a/chaiscript/chaiscript_parser.hpp +++ b/chaiscript/chaiscript_parser.hpp @@ -1180,7 +1180,7 @@ namespace chaiscript if (Expression()) { 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()) { throw Parse_Error("Incomplete equation", match_stack.back()); }