Added "source" command, for executing valexpr files

This commit is contained in:
John Wiegley 2010-06-24 20:29:20 -04:00
parent 024fb4f3e0
commit 35da9ad466
3 changed files with 30 additions and 0 deletions

View file

@ -33,6 +33,7 @@
#include "expr.h"
#include "parser.h"
#include "scope.h"
namespace ledger {
@ -162,4 +163,27 @@ void expr_t::dump(std::ostream& out) const
if (ptr) ptr->dump(out, 0);
}
value_t source_command(call_scope_t& args)
{
std::istream * in = NULL;
scoped_ptr<ifstream> stream;
if (args.has(0)) {
stream.reset(new ifstream(path(args.get<string>(0))));
in = stream.get();
}
symbol_scope_t file_locals(args);
while (in->good() && ! in->eof()) {
char buf[4096];
in->getline(buf, 4095);
if (buf[0] != ';')
expr_t(buf).calc(file_locals);
}
return true;
}
} // namespace ledger

View file

@ -160,6 +160,10 @@ inline value_t expr_value(expr_t::ptr_op_t op) {
return temp;
}
class call_scope_t;
value_t source_command(call_scope_t& scope);
} // namespace ledger
#endif // _EXPR_H

View file

@ -1449,6 +1449,8 @@ expr_t::ptr_op_t report_t::lookup(const symbol_t::kind_t kind,
case 's':
if (is_eq(p, "stats") || is_eq(p, "stat"))
return WRAP_FUNCTOR(report_statistics);
else if (is_eq(p, "source"))
return WRAP_FUNCTOR(source_command);
break;
case 'x':