Improve formatter usage. Fix bug in object printer.

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-08-17 11:29:43 -07:00
parent 0f3af23778
commit a6f36ba546
8 changed files with 47 additions and 23 deletions

View file

@ -9,7 +9,7 @@ Author: Leonardo de Moura
#include "printer.h"
namespace lean {
class simple_formatter : public formatter {
class simple_formatter_cell : public formatter_cell {
public:
virtual format operator()(expr const & e) {
std::ostringstream s; s << e; return format(s.str());
@ -31,7 +31,7 @@ public:
std::ostringstream s; s << env; return format(s.str());
}
};
std::shared_ptr<formatter> mk_simple_formatter() {
return std::shared_ptr<formatter>(new simple_formatter());
formatter mk_simple_formatter() {
return formatter(new simple_formatter_cell());
}
}

View file

@ -14,9 +14,9 @@ class object;
/**
\brief API for formatting expressions, contexts and environments.
*/
class formatter {
class formatter_cell {
public:
virtual ~formatter() {}
virtual ~formatter_cell() {}
/** \brief Format the given expression. */
virtual format operator()(expr const & e) = 0;
/** \brief Format the given context. */
@ -34,8 +34,18 @@ public:
/** \brief Format the given environment */
virtual format operator()(environment const & env) = 0;
};
/**
\brief Return simple expression formatter that just uses printer module.
*/
std::shared_ptr<formatter> mk_simple_formatter();
class formatter {
std::shared_ptr<formatter_cell> m_cell;
public:
formatter(formatter_cell * c):m_cell(c) {}
formatter(std::shared_ptr<formatter_cell> const & c):m_cell(c) {}
format operator()(expr const & e) { return (*m_cell)(e); }
format operator()(context const & c) { return (*m_cell)(c); }
format operator()(context const & c, expr const & e, bool format_ctx = false) { return (*m_cell)(c, e, format_ctx); }
format operator()(object const & obj) { return (*m_cell)(obj); }
format operator()(environment const & env) { return (*m_cell)(env); }
};
formatter mk_simple_formatter();
}

View file

@ -163,6 +163,7 @@ std::ostream & operator<<(std::ostream & out, object const & obj) {
case object_kind::Neutral:
break;
}
return out;
}
std::ostream & operator<<(std::ostream & out, environment const & env) {

View file

@ -573,7 +573,7 @@ public:
}
};
class pp_formatter : public formatter {
class pp_formatter_cell : public formatter_cell {
frontend const & m_frontend;
options m_options;
unsigned m_indent;
@ -652,13 +652,13 @@ class pp_formatter : public formatter {
}
public:
pp_formatter(frontend const & fe, options const & opts):
pp_formatter_cell(frontend const & fe, options const & opts):
m_frontend(fe),
m_options(opts) {
m_indent = get_pp_indent(opts);
}
virtual ~pp_formatter() {
virtual ~pp_formatter_cell() {
}
virtual format operator()(expr const & e) {
@ -714,13 +714,13 @@ public:
}
};
std::shared_ptr<formatter> mk_pp_formatter(frontend const & fe, options const & opts) {
return std::shared_ptr<formatter>(new pp_formatter(fe, opts));
formatter mk_pp_formatter(frontend const & fe, options const & opts) {
return formatter(new pp_formatter_cell(fe, opts));
}
std::ostream & operator<<(std::ostream & out, frontend const & fe) {
auto pp = mk_pp_formatter(fe, options());
out << (*pp)(fe.get_environment());
formatter fmt = mk_pp_formatter(fe, options());
out << fmt(fe.get_environment());
return out;
}
}

View file

@ -11,6 +11,6 @@ Author: Leonardo de Moura
namespace lean {
class frontend;
std::shared_ptr<formatter> mk_pp_formatter(frontend const & fe, options const & opts = options());
formatter mk_pp_formatter(frontend const & fe, options const & opts = options());
std::ostream & operator<<(std::ostream & out, frontend const & fe);
}

View file

@ -134,6 +134,7 @@ public:
bool operator==(object_iterator const & s) const { lean_assert(&m_env == &(s.m_env)); return m_idx == s.m_idx; }
bool operator!=(object_iterator const & s) const { return !operator==(s); }
object const & operator*() { return m_env.get_object(m_idx, m_local); }
object const * operator->() { return &(m_env.get_object(m_idx, m_local)); }
};
/**

View file

@ -56,8 +56,7 @@ static void tst3() {
static void tst4() {
frontend f;
std::shared_ptr<formatter> fmt_ptr = mk_pp_formatter(f);
formatter & fmt = *fmt_ptr;
formatter fmt = mk_pp_formatter(f);
expr Bool = Const("Bool");
context c;
c = extend(c, "x", Bool);
@ -72,8 +71,7 @@ static void tst4() {
static void tst5() {
std::cout << "=================\n";
frontend f;
std::shared_ptr<formatter> fmt_ptr = mk_pp_formatter(f);
formatter & fmt = *fmt_ptr;
formatter fmt = mk_pp_formatter(f);
f.add_var("A", Type());
f.add_var("x", Const("A"));
object const & obj = f.find_object("x");
@ -103,8 +101,7 @@ static void tst6() {
frontend f;
environment env;
env.add_neutral_object(new alien_cell());
std::shared_ptr<formatter> fmt_ptr = mk_pp_formatter(f);
formatter & fmt = *fmt_ptr;
formatter fmt = mk_pp_formatter(f);
std::cout << fmt(env) << "\n";
}

View file

@ -11,6 +11,20 @@ Author: Leonardo de Moura
#include "test.h"
using namespace lean;
static void tst0() {
environment env;
lean_assert(env.begin_objects() == env.end_objects());
std::for_each(env.begin_objects(), env.end_objects(), [&](object const & obj) {
std::cout << obj.keyword() << "\n";
});
level l1 = env.add_uvar(name(name("l1"), "suffix"), level());
lean_assert(env.begin_objects() != env.end_objects());
std::for_each(env.begin_objects(), env.end_objects(), [&](object const & obj) {
std::cout << obj.keyword() << "\n";
});
std::cout << env;
}
static void tst1() {
environment env;
level l1 = env.add_uvar(name(name("l1"), "suffix"), level());
@ -94,6 +108,7 @@ static void tst5() {
}
int main() {
tst0();
tst1();
tst2();
tst3();