lean2/src/util/format.cpp

97 lines
2.3 KiB
C++
Raw Normal View History

2013-07-23 01:30:25 +00:00
/*
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Soonho Kong
*/
#include <cstring>
#include "sexpr.h"
#include "format.h"
namespace lean {
std::ostream & layout(std::ostream & out, sexpr const & s) {
if(is_cons(s)) {
sexpr v = cdr(s);
2013-07-23 01:30:25 +00:00
switch (to_int(car(s))) {
case format::format_kind::NIL:
out << "NIL";
break;
case format::format_kind::NEST:
out << "NEST("
<< car(v)
<< ", ";
layout(out, cdr(v));
out << ")";
break;
case format::format_kind::COMPOSE:
out << "COMPOSE(";
layout(out, car(v));
out << ", ";
layout(out, cdr(v));
out << ")";
break;
case format::format_kind::CHOICE:
out << "CHOICE(";
layout(out, car(v));
out << ", ";
layout(out, cdr(v));
out << ")";
break;
case format::format_kind::LINE:
out << "LINE()";
break;
}
} else {
out << s;
2013-07-23 01:30:25 +00:00
}
return out;
2013-07-23 01:30:25 +00:00
}
std::ostream & operator<<(std::ostream & out, format const & f) {
return layout(out, f.m_value);
2013-07-23 01:30:25 +00:00
}
sexpr flatten(sexpr const & s) {
if(is_cons(s)) {
sexpr v = cdr(s);
switch (to_int(car(s))) {
case format::format_kind::NIL:
/* flatten NIL = NIL */
return s;
case format::format_kind::NEST:
/* flatten (NEST i x) = flatten x */
return flatten(cdr(v));
case format::format_kind::COMPOSE:
/* flatten (x <> y) = flatten x <> flatten y */
return sexpr(format::format_kind::COMPOSE,
sexpr(flatten(car(v)),
flatten(cdr(v))));
case format::format_kind::CHOICE:
/* flatten (x <|> y) = flatten x */
return flatten(car(v));
2013-07-23 01:30:25 +00:00
case format::format_kind::LINE:
return sexpr(" ");
2013-07-23 01:30:25 +00:00
}
}
return s;
2013-07-23 01:30:25 +00:00
}
format flatten(format const & f){
return format(flatten(f.m_value));
2013-07-23 01:30:25 +00:00
}
format group(format const & f) {
return choice(flatten(f), f);
2013-07-23 01:30:25 +00:00
}
}
void pp(lean::format const & n) { std::cout << "pp" << "\n"; }