From ae4ef7a88ddd39ed544383e65d3c55ba97f4f8c1 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 7 Mar 2012 21:22:07 -0600 Subject: More work done on proper evaluation of lambdas --- src/op.cc | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'src/op.cc') diff --git a/src/op.cc b/src/op.cc index a5db1690..6a1a8f54 100644 --- a/src/op.cc +++ b/src/op.cc @@ -155,23 +155,7 @@ expr_t::ptr_op_t expr_t::op_t::compile(scope_t& scope, const int depth, node->set_left(left()->right()); node->set_right(right()); - symbol_scope_t params(param_scope ? - *param_scope : *scope_t::empty_scope); - for (ptr_op_t sym = node->left(); - sym; - sym = sym->has_right() ? sym->right() : NULL) { - ptr_op_t varname = sym->kind == O_CONS ? sym->left() : sym; - if (! varname->is_ident()) { - throw_(calc_error, _("Invalid function definition")); - } else { - DEBUG("expr.compile", - "Defining function parameter " << varname->as_ident()); - params.define(symbol_t::FUNCTION, varname->as_ident(), - new op_t(op_t::PLUG)); - } - } - - node = node->compile(*scope_ptr, depth + 1, ¶ms); + node = node->compile(*scope_ptr, depth + 1, param_scope); DEBUG("expr.compile", "Defining " << left()->left()->as_ident() << " in " << scope_ptr); @@ -185,6 +169,30 @@ expr_t::ptr_op_t expr_t::op_t::compile(scope_t& scope, const int depth, } result = wrap_value(NULL_VALUE); } + else if (kind == O_LAMBDA) { + symbol_scope_t params(param_scope ? *param_scope : *scope_t::empty_scope); + + for (ptr_op_t sym = left(); + sym; + sym = sym->has_right() ? sym->right() : NULL) { + ptr_op_t varname = sym->kind == O_CONS ? sym->left() : sym; + + if (! varname->is_ident()) { + throw_(calc_error, _("Invalid function or lambda parameter")); + } else { + DEBUG("expr.compile", + "Defining function parameter " << varname->as_ident()); + params.define(symbol_t::FUNCTION, varname->as_ident(), + new op_t(PLUG)); + } + } + + ptr_op_t rhs(right()->compile(*scope_ptr, depth + 1, ¶ms)); + if (rhs == right()) + result = this; + else + result = copy(left(), rhs); + } if (! result) { ptr_op_t lhs(left()->compile(*scope_ptr, depth + 1, param_scope)); -- cgit v1.2.3