summaryrefslogtreecommitdiff
path: root/src/report.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/report.cc')
-rw-r--r--src/report.cc183
1 files changed, 173 insertions, 10 deletions
diff --git a/src/report.cc b/src/report.cc
index 4b45577b..2c34fc5d 100644
--- a/src/report.cc
+++ b/src/report.cc
@@ -441,7 +441,7 @@ namespace {
template <class Type = xact_t,
class handler_ptr = xact_handler_ptr,
void (report_t::*report_method)(handler_ptr) =
- &report_t::xacts_report>
+ &report_t::xacts_report>
class reporter
{
shared_ptr<item_handler<Type> > handler;
@@ -464,6 +464,143 @@ namespace {
return true;
}
};
+
+ value_t parse_command(call_scope_t& args)
+ {
+ var_t<string> arg(args, 0);
+
+ if (! arg) {
+ throw std::logic_error("Usage: parse TEXT");
+ return 1L;
+ }
+
+ report_t& report(find_scope<report_t>(args));
+ std::ostream& out(report.output_stream);
+
+ out << "--- Input text ---" << std::endl;
+ out << *arg << std::endl;
+
+ out << std::endl << "--- Text as parsed ---" << std::endl;
+ expr_t expr(*arg);
+ expr.print(out);
+ out << std::endl;
+
+ out << std::endl << "--- Expression tree ---" << std::endl;
+ expr.dump(out);
+
+ out << std::endl << "--- Calculated value ---" << std::endl;
+ expr.calc(args).print(out);
+ out << std::endl;
+
+ return 0L;
+ }
+
+ value_t compile_command(call_scope_t& args)
+ {
+ var_t<string> arg(args, 0);
+
+ if (! arg) {
+ throw std::logic_error("Usage: compile TEXT");
+ return 1L;
+ }
+
+ report_t& report(find_scope<report_t>(args));
+ std::ostream& out(report.output_stream);
+
+ out << "--- Input text ---" << std::endl;
+ out << *arg << std::endl;
+
+ out << std::endl << "--- Text as parsed ---" << std::endl;
+ expr_t expr(*arg);
+ expr.print(out);
+ out << std::endl;
+
+ out << std::endl << "--- Expression tree ---" << std::endl;
+ expr.dump(out);
+
+ expr.compile(args);
+
+ out << std::endl << "--- Compiled tree ---" << std::endl;
+ expr.dump(out);
+
+ out << std::endl << "--- Calculated value ---" << std::endl;
+ expr.calc(args).print(out);
+ out << std::endl;
+
+ return 0L;
+ }
+
+ value_t eval_command(call_scope_t& args)
+ {
+ var_t<string> arg(args, 0);
+
+ if (! arg) {
+ throw std::logic_error("Usage: eval TEXT");
+ return 1L;
+ }
+
+ report_t& report(find_scope<report_t>(args));
+ std::ostream& out(report.output_stream);
+
+ expr_t expr(*arg);
+ out << expr.calc(args).strip_annotations() << std::endl;
+ return 0L;
+ }
+
+ value_t format_command(call_scope_t& args)
+ {
+ var_t<string> arg(args, 0);
+
+ if (! arg) {
+ throw std::logic_error("Usage: format TEXT");
+ return 1L;
+ }
+
+ report_t& report(find_scope<report_t>(args));
+ std::ostream& out(report.output_stream);
+
+ format_t fmt(*arg);
+ fmt.dump(out);
+
+ return 0L;
+ }
+
+ value_t period_command(call_scope_t& args)
+ {
+ var_t<string> arg(args, 0);
+
+ if (! arg) {
+ throw std::logic_error("Usage: period TEXT");
+ return 1L;
+ }
+
+ report_t& report(find_scope<report_t>(args));
+ std::ostream& out(report.output_stream);
+
+ interval_t interval(*arg);
+
+ if (! is_valid(interval.begin)) {
+ out << "Time period has no beginning." << std::endl;
+ } else {
+ out << "begin: " << format_date(interval.begin) << std::endl;
+ out << " end: " << format_date(interval.end) << std::endl;
+ out << std::endl;
+
+ date_t date = interval.first();
+
+ for (int i = 0; i < 20; i++) {
+ out << std::right;
+ out.width(2);
+
+ out << i << ": " << format_date(date) << std::endl;
+
+ date = interval.increment(date);
+ if (is_valid(interval.end) && date >= interval.end)
+ break;
+ }
+ }
+ return 0L;
+ }
}
expr_t::ptr_op_t report_t::lookup(const string& name)
@@ -475,8 +612,13 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
return MAKE_FUNCTOR(report_t::get_amount_expr);
break;
- case 'c':
- if (std::strncmp(p, "cmd_", 4) == 0) {
+ case 'd':
+ if (std::strcmp(p, "display_total") == 0)
+ return MAKE_FUNCTOR(report_t::get_display_total);
+ break;
+
+ case 'l':
+ if (std::strncmp(p, "ledger_cmd_", 11) == 0) {
#define FORMAT(str) \
(format_string.empty() ? session. str : format_string)
@@ -494,7 +636,7 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
// xml
#endif
- p = p + 4;
+ p = p + 11;
switch (*p) {
case 'b':
if (*(p + 1) == '\0' ||
@@ -528,11 +670,6 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
}
break;
- case 'd':
- if (std::strcmp(p, "display_total") == 0)
- return MAKE_FUNCTOR(report_t::get_display_total);
- break;
-
case 'm':
if (std::strcmp(p, "market_value") == 0)
return MAKE_FUNCTOR(report_t::f_market_value);
@@ -782,7 +919,33 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
break;
case 'p':
- if (std::strcmp(p, "print_balance") == 0)
+ if (std::strncmp(p, "ledger_precmd_", 14) == 0) {
+ p = p + 14;
+ switch (*p) {
+ case 'p':
+ if (std::strcmp(p, "parse") == 0)
+ return WRAP_FUNCTOR(parse_command);
+ else if (std::strcmp(p, "period") == 0)
+ return WRAP_FUNCTOR(period_command);
+ break;
+
+ case 'c':
+ if (std::strcmp(p, "compile") == 0)
+ return WRAP_FUNCTOR(compile_command);
+ break;
+
+ case 'e':
+ if (std::strcmp(p, "eval") == 0)
+ return WRAP_FUNCTOR(eval_command);
+ break;
+
+ case 'f':
+ if (std::strcmp(p, "format") == 0)
+ return WRAP_FUNCTOR(format_command);
+ break;
+ }
+ }
+ else if (std::strcmp(p, "print_balance") == 0)
return WRAP_FUNCTOR(print_balance);
break;