summaryrefslogtreecommitdiff
path: root/src/post.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2010-06-13 05:02:14 -0400
committerJohn Wiegley <johnw@newartisans.com>2010-06-13 05:02:14 -0400
commitea1642b3f969463a49e5a671478c92e4ef129665 (patch)
tree9eba47b8708123b4e3f539dd9d747f1f85d9d2fe /src/post.cc
parentad3b30a9641b8e09c38ea76e7011b614152d8faf (diff)
downloadfork-ledger-ea1642b3f969463a49e5a671478c92e4ef129665.tar.gz
fork-ledger-ea1642b3f969463a49e5a671478c92e4ef129665.tar.bz2
fork-ledger-ea1642b3f969463a49e5a671478c92e4ef129665.zip
Completely reworked argument passing in expressions
Diffstat (limited to 'src/post.cc')
-rw-r--r--src/post.cc98
1 files changed, 45 insertions, 53 deletions
diff --git a/src/post.cc b/src/post.cc
index d4a16122..bbf43227 100644
--- a/src/post.cc
+++ b/src/post.cc
@@ -35,7 +35,6 @@
#include "xact.h"
#include "account.h"
#include "journal.h"
-#include "interactive.h"
#include "format.h"
namespace ledger {
@@ -197,15 +196,13 @@ namespace {
return post.has_xdata() && post.xdata().has_flags(POST_EXT_DIRECT_AMT);
}
- value_t get_commodity(call_scope_t& scope)
+ value_t get_commodity(call_scope_t& args)
{
- in_context_t<post_t> env(scope, "&v");
- if (env.has(0)) {
- return string_value(env.value_at(0).to_amount().commodity().symbol());
+ if (args.has<amount_t>(0)) {
+ return string_value(args.get<amount_t>(0).commodity().symbol());
} else {
- post_t& post(find_scope<post_t>(scope));
- if (post.has_xdata() &&
- post.xdata().has_flags(POST_EXT_COMPOUND))
+ post_t& post(args.context<post_t>());
+ if (post.has_xdata() && post.xdata().has_flags(POST_EXT_COMPOUND))
return string_value(post.xdata().compound_value.to_amount()
.commodity().symbol());
else
@@ -254,48 +251,48 @@ namespace {
return 1L;
}
- value_t get_account(call_scope_t& scope)
+ value_t get_account(call_scope_t& args)
{
- in_context_t<post_t> env(scope, "&v");
- account_t& account(*env->reported_account());
- string name;
+ post_t& post(args.context<post_t>());
+ account_t& account(*post.reported_account());
+ string name;
- if (env.has(0)) {
- if (env.value_at(0).is_long()) {
- if (env.get<long>(0) > 2)
+ if (args.has(0)) {
+ if (args[0].is_long()) {
+ if (args.get<long>(0) > 2)
name = format_t::truncate(account.fullname(),
- env.get<long>(0) - 2,
+ args.get<long>(0) - 2,
2 /* account_abbrev_length */);
else
name = account.fullname();
} else {
- account_t * account = NULL;
- account_t * master = env->account;
+ account_t * acct = NULL;
+ account_t * master = &account;
while (master->parent)
master = master->parent;
- if (env.value_at(0).is_string()) {
- name = env.get<string>(0);
- account = master->find_account(name, false);
+ if (args[0].is_string()) {
+ name = args.get<string>(0);
+ acct = master->find_account(name, false);
}
- else if (env.value_at(0).is_mask()) {
- name = env.get<mask_t>(0).str();
- account = master->find_account_re(name);
+ else if (args[0].is_mask()) {
+ name = args.get<mask_t>(0).str();
+ acct = master->find_account_re(name);
}
else {
throw_(std::runtime_error,
_("Expected string or mask for argument 1, but received %1")
- << env.value_at(0).label());
+ << args[0].label());
}
- if (! account)
+ if (! acct)
throw_(std::runtime_error,
- _("Could not find an account matching ") << env.value_at(0));
+ _("Could not find an account matching ") << args[0]);
else
- return value_t(static_cast<scope_t *>(account));
+ return value_t(static_cast<scope_t *>(acct));
}
}
- else if (scope.type_context() == value_t::SCOPE) {
+ else if (args.type_context() == value_t::SCOPE) {
return scope_value(&account);
}
else {
@@ -304,14 +301,13 @@ namespace {
return string_value(name);
}
- value_t get_display_account(call_scope_t& scope)
+ value_t get_display_account(call_scope_t& args)
{
- in_context_t<post_t> env(scope, "&v");
-
- value_t acct = get_account(scope);
+ post_t& post(args.context<post_t>());
+ value_t acct = get_account(args);
if (acct.is_string()) {
- if (env->has_flags(POST_VIRTUAL)) {
- if (env->must_balance())
+ if (post.has_flags(POST_VIRTUAL)) {
+ if (post.must_balance())
acct = string_value(string("[") + acct.as_string() + "]");
else
acct = string_value(string("(") + acct.as_string() + ")");
@@ -349,44 +345,40 @@ namespace {
return (*Func)(find_scope<post_t>(scope));
}
- value_t fn_any(call_scope_t& scope)
+ value_t fn_any(call_scope_t& args)
{
- interactive_t args(scope, "X&X");
-
- post_t& post(find_scope<post_t>(scope));
- expr_t& expr(args.get<expr_t&>(0));
+ post_t& post(args.context<post_t>());
+ expr_t::ptr_op_t expr(args.get<expr_t::ptr_op_t>(0));
foreach (post_t * p, post.xact->posts) {
- bind_scope_t bound_scope(scope, *p);
- if (p == &post && args.has(1) &&
- ! args.get<expr_t&>(1).calc(bound_scope).to_boolean()) {
+ bind_scope_t bound_scope(args, *p);
+ if (p == &post && args.has<expr_t::ptr_op_t>(1) &&
+ ! args.get<expr_t::ptr_op_t>(1)->calc(bound_scope).to_boolean()) {
// If the user specifies any(EXPR, false), and the context is a
// posting, then that posting isn't considered by the test.
; // skip it
}
- else if (expr.calc(bound_scope).to_boolean()) {
+ else if (expr->calc(bound_scope).to_boolean()) {
return true;
}
}
return false;
}
- value_t fn_all(call_scope_t& scope)
+ value_t fn_all(call_scope_t& args)
{
- interactive_t args(scope, "X&X");
-
- post_t& post(find_scope<post_t>(scope));
- expr_t& expr(args.get<expr_t&>(0));
+ post_t& post(args.context<post_t>());
+ expr_t::ptr_op_t expr(args.get<expr_t::ptr_op_t>(0));
foreach (post_t * p, post.xact->posts) {
- bind_scope_t bound_scope(scope, *p);
- if (p == &post && args.has(1) &&
- ! args.get<expr_t&>(1).calc(bound_scope).to_boolean()) {
+ bind_scope_t bound_scope(args, *p);
+ if (p == &post && args.has<expr_t::ptr_op_t>(1) &&
+ ! args.get<expr_t::ptr_op_t>(1)->calc(bound_scope).to_boolean()) {
// If the user specifies any(EXPR, false), and the context is a
// posting, then that posting isn't considered by the test.
; // skip it
}
- else if (! expr.calc(bound_scope).to_boolean()) {
+ else if (! expr->calc(bound_scope).to_boolean()) {
return false;
}
}