Improved the output from some debugging commands.

There are now three commands one can use to interact with value expressions
directly:

  ledger parse EXPR       # shows the parse tree resulting from EXPR
  ledger compile EXPR     # shows what the compiled tree looks like
  ledger eval EXPR        # print the resulting value, useful in scripts
This commit is contained in:
John Wiegley 2008-08-01 02:53:32 -04:00
parent d213b32ffc
commit af344b1ab1
4 changed files with 49 additions and 24 deletions

55
main.cc
View file

@ -170,30 +170,51 @@ static int read_and_report(ledger::report_t& report, int argc, char * argv[],
if (verb == "parse") {
expr_t expr(*arg);
IF_INFO() {
std::cout << "Value expression tree:" << std::endl;
expr.dump(std::cout);
std::cout << std::endl;
*out << "Value expression as input: " << *arg << std::endl;
std::cout << "Value expression parsed was:" << std::endl;
expr.print(std::cout, report);
std::cout << std::endl << std::endl;
*out << "Value expression as parsed: ";
expr.print(*out, report);
*out << std::endl;
expr.compile(report);
*out << std::endl;
*out << "--- Parsed tree ---" << std::endl;
expr.dump(*out);
std::cout << "Value expression after compiling:" << std::endl;
expr.dump(std::cout);
std::cout << std::endl;
*out << std::endl;
*out << "--- Calculated value ---" << std::endl;
expr.calc(report).print(*out);
*out << std::endl;
std::cout << "Value expression is now:" << std::endl;
expr.print(std::cout, report);
std::cout << std::endl << std::endl;
return 0;
}
else if (verb == "compile") {
expr_t expr(*arg);
std::cout << "Result of calculation: ";
}
*out << "Value expression as input: " << *arg << std::endl;
*out << "Value expression as parsed: ";
expr.print(*out, report);
*out << std::endl;
std::cout << expr.calc(report).strip_annotations() << std::endl;
*out << std::endl;
*out << "--- Parsed tree ---" << std::endl;
expr.dump(*out);
expr.compile(report);
*out << std::endl;
*out << "--- Compiled tree ---" << std::endl;
expr.dump(*out);
*out << std::endl;
*out << "--- Calculated value ---" << std::endl;
expr.calc(report).print(*out);
*out << std::endl;
return 0;
}
else if (verb == "eval") {
expr_t expr(*arg);
*out << expr.calc(report).strip_annotations() << std::endl;
return 0;
}

9
op.cc
View file

@ -754,9 +754,12 @@ value_t expr_t::op_t::calc(scope_t& scope)
return ! left()->calc(scope);
case O_AND:
return left()->calc(scope) && right()->calc(scope);
return ! left()->calc(scope) ? value_t(false) : right()->calc(scope);
case O_OR:
return left()->calc(scope) || right()->calc(scope);
if (value_t temp = left()->calc(scope))
return temp;
else
return right()->calc(scope);
case O_COMMA: {
value_t result(left()->calc(scope));
@ -976,7 +979,7 @@ void expr_t::op_t::dump(std::ostream& out, const int depth) const
{
out.setf(std::ios::left);
out.width(10);
out << this << " ";
out << this;
for (int i = 0; i < depth; i++)
out << " ";

7
op.h
View file

@ -275,11 +275,12 @@ public:
unsigned long * start_pos;
unsigned long * end_pos;
print_context_t(scope_t& _scope,
const bool _relaxed = false,
// jww (2008-08-01): Is a scope needed here?
print_context_t(scope_t& _scope,
const bool _relaxed = false,
const ptr_op_t& _op_to_find = ptr_op_t(),
unsigned long * _start_pos = NULL,
unsigned long * _end_pos = NULL)
unsigned long * _end_pos = NULL)
: scope(_scope), relaxed(_relaxed), op_to_find(_op_to_find),
start_pos(_start_pos), end_pos(_end_pos) {}
};

View file

@ -240,7 +240,7 @@ public:
}
value_t option_bar(call_scope_t& args) {
std::cout << "This is bar: " << args[0] << std::endl;
return NULL_VALUE;
return args[0];
}
//