From 7e5b7cbd7a9f72e9a41a3cec465bee78fb4d7804 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Fri, 21 Aug 2009 20:05:05 +0000 Subject: [PATCH] Switch get_function to default to not doing object lookup. Correct method eval to maintain method lambda syntax. Add unit test for method lambda --- .../chaiscript/dispatchkit/dispatchkit.hpp | 2 +- .../chaiscript/language/chaiscript_eval.hpp | 31 ++++++++++++------- unittests/method_lambda.chai | 3 ++ unittests/method_lambda.txt | 1 + 4 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 unittests/method_lambda.chai create mode 100644 unittests/method_lambda.txt diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 35613be9..6fcab0be 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -324,7 +324,7 @@ namespace chaiscript std::vector::mapped_type> > get_function(const std::string &t_name) const { - return get_function_impl(t_name, true); + return get_function_impl(t_name, false); } /** diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 4d3f9813..1e82d707 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -471,23 +471,32 @@ namespace chaiscript } } - std::string fun_name; - if (node->children[i]->identifier == Token_Type::Fun_Call) { - fun_name = node->children[i]->children[0]->text; - } - else { - fun_name = node->children[i]->text; - } - + //std::string fun_name; + Boxed_Value fn; try { - fn = ss.get_function(fun_name); + if (node->children[i]->identifier == Token_Type::Fun_Call) { + //fun_name = node->children[i]->children[0]->text; + fn = eval_token(ss, node->children[i]->children[0]); + } + else { + //fun_name = node->children[i]->text; + fn = eval_token(ss, node->children[i]); + } + } + catch(Eval_Error &ee) { + ss.set_stack(prev_stack); + throw Eval_Error(ee.reason, node->children[i]); + } + try { + //fn = ss.get_function(fun_name); ss.set_stack(new_stack); - retval = dispatch(fn, plb); + //retval = dispatch(fn, plb); + retval = (*boxed_cast(fn))(plb); ss.set_stack(prev_stack); } catch(const dispatch_error &e){ ss.set_stack(prev_stack); - throw Eval_Error(std::string(e.what()) + " with function '" + fun_name + "'", node->children[i]); + throw Eval_Error(std::string(e.what()), node->children[i]); } catch(Return_Value &rv) { ss.set_stack(prev_stack); diff --git a/unittests/method_lambda.chai b/unittests/method_lambda.chai new file mode 100644 index 00000000..bf7805e2 --- /dev/null +++ b/unittests/method_lambda.chai @@ -0,0 +1,3 @@ +var addit = fun(x, y) { return x+y } + +print(3.addit(4)) diff --git a/unittests/method_lambda.txt b/unittests/method_lambda.txt new file mode 100644 index 00000000..7f8f011e --- /dev/null +++ b/unittests/method_lambda.txt @@ -0,0 +1 @@ +7