From 8043c794133a8de6888371f8942185e5baace0a3 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 24 Feb 2006 10:14:08 +0000 Subject: (parse_value_term): Added support for general @name functions. This used to mean Python functions, now it will be used for all further value expression functions. Right now this means the new @min(x,y) and @max(x,y) functions. --- valexpr.cc | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ valexpr.h | 1 + 2 files changed, 78 insertions(+) diff --git a/valexpr.cc b/valexpr.cc index b392fa73..42e16b63 100644 --- a/valexpr.cc +++ b/valexpr.cc @@ -294,6 +294,56 @@ void value_expr_t::compute(value_t& result, const details_t& details) const result = false; break; + case F_FUNC: { + if (constant_s == "min" || constant_s == "max") { + assert(left); + if (! right) { + left->compute(result, details); + break; + } + value_t temp; + left->compute(temp, details); + assert(right->kind == O_ARG); + right->left->compute(result, details); + + if (constant_s == "min") { + if (temp < result) + result = temp; + } else { + if (temp > result) + result = temp; + } + } + else if (constant_s == "price") { + assert(left); + left->compute(result, details); + + std::time_t moment = terminus; + if (right) { + assert(right->kind == O_ARG); + switch (right->left->kind) { + case DATE: + if (details.xact && transaction_has_xdata(*details.xact) && + transaction_xdata_(*details.xact).date) + moment = transaction_xdata_(*details.xact).date; + else if (details.xact) + moment = details.xact->date(); + else if (details.entry) + moment = details.entry->date(); + break; + case CONSTANT_T: + moment = right->left->constant_t; + break; + default: + throw compute_error("Invalid date passed to @price(value,date)"); + } + } + + result = result.value(moment); + } + break; + } + case F_VALUE: { assert(left); left->compute(result, details); @@ -581,6 +631,33 @@ value_expr_t * parse_value_term(std::istream& in) break; } + case '@': { + READ_INTO(in, buf, 255, c, c != '('); + if (c != '(') + unexpected(c, '('); + + node.reset(new value_expr_t(value_expr_t::F_FUNC)); + node->constant_s = buf; + + in.get(c); + if (peek_next_nonws(in) == ')') { + in.get(c); + } else { + value_expr_t * cur = node.get(); + cur->left = parse_value_expr(in, true); + in.get(c); + while (! in.eof() && c == ',') { + cur->right = new value_expr_t(value_expr_t::O_ARG); + cur = cur->right; + cur->left = parse_value_expr(in, true); + in.get(c); + } + if (c != ')') + unexpected(c, ')'); + } + break; + } + case '(': node.reset(parse_value_expr(in, true)); in.get(c); diff --git a/valexpr.h b/valexpr.h index e79e4afd..820ae04f 100644 --- a/valexpr.h +++ b/valexpr.h @@ -64,6 +64,7 @@ struct value_expr_t F_PARENT, F_ARITH_MEAN, F_VALUE, + F_FUNC, F_NEG, F_ABS, F_STRIP, -- cgit v1.2.3