aboutsummaryrefslogtreecommitdiffstats
path: root/src/pprint.cpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2010-12-09 21:39:58 +0000
committerDavid Robillard <d@drobilla.net>2010-12-09 21:39:58 +0000
commit815ab7a5ea07e96ae532f6c4aac8e0354a215b74 (patch)
tree2be2397fa6d9d2dfa4df06fa555b62628d7b815f /src/pprint.cpp
parent240950c9df43688b59585920a6688bbe8a5807dd (diff)
downloadresp-815ab7a5ea07e96ae532f6c4aac8e0354a215b74.tar.gz
resp-815ab7a5ea07e96ae532f6c4aac8e0354a215b74.tar.bz2
resp-815ab7a5ea07e96ae532f6c4aac8e0354a215b74.zip
Decent pretty printing for all forms.
git-svn-id: http://svn.drobilla.net/resp/resp@331 ad02d1e2-f140-0410-9f75-f8b11f17cedd
Diffstat (limited to 'src/pprint.cpp')
-rw-r--r--src/pprint.cpp131
1 files changed, 71 insertions, 60 deletions
diff --git a/src/pprint.cpp b/src/pprint.cpp
index c948865..a2178a5 100644
--- a/src/pprint.cpp
+++ b/src/pprint.cpp
@@ -35,30 +35,61 @@ newline(ostream& out, unsigned indent)
}
ostream&
-print_tuple(ostream& out, const ATuple* tup, ATuple::const_iterator i,
- unsigned indent, bool newlines, CEnv* cenv, bool types, bool elem_types)
+print_list_one_line(ostream& out, const ATuple* tup, ATuple::const_iterator i,
+ unsigned indent, CEnv* cenv, bool types, bool elem_types)
{
for (; i != tup->end(); ) {
ATuple::const_iterator next = i;
++next;
- print_to(out, *i, newlines ? indent + 2 : indent, cenv, types);
+ print_to(out, *i, indent, cenv, types);
if (elem_types)
out << " :" << cenv->tsubst.apply(cenv->tenv.var(*i));
- if (next != tup->end()) {
- if (newlines)
- newline(out, indent + 2);
- else
- out << " ";
- }
+ if (next != tup->end())
+ out << " ";
i = next;
}
return (out << ")");
}
-
+
+/** Print list arguments on separate lines, possibly in separated pairs, e.g.:
+ * (elem one)
+ * (elem two)
+ * (elem three)
+ *
+ * or, if split_pairs is true:
+ *
+ * (elem one)
+ * (elem two)
+ *
+ * (elem three)
+ * (elem four)
+ */
+ostream&
+print_list(ostream& out, const ATuple* tup, ATuple::const_iterator i,
+ unsigned indent, CEnv* cenv, bool types, bool split_pairs)
+{
+ for (; i != tup->end(); ) {
+ ATuple::const_iterator next = i;
+ ++next;
+
+ print_to(out, *i, indent, cenv, types);
+ if (next != tup->end()) {
+ newline(out, indent);
+ print_to(out, *next++, indent, cenv, types);
+ if (split_pairs)
+ newline(out, 0);
+ newline(out, indent);
+ }
+
+ i = next;
+ }
+ return out;
+}
+
ostream&
print_to(ostream& out, const AST* ast, unsigned indent, CEnv* cenv, bool types)
{
@@ -81,74 +112,57 @@ print_to(ostream& out, const AST* ast, unsigned indent, CEnv* cenv, bool types)
const ATuple* tup = ast->as_tuple();
out << "(";
ATuple::const_iterator i = tup->begin();
+ if (i == tup->end())
+ return out << ")";
- std::string form = "";
- if (i != tup->end()) {
- const ASymbol* sym = (*i)->to_symbol();
- if (sym) {
- form = sym->sym();
- }
- }
-
- if (form == "def") {
- out << (*i++) << " ";
+ std::string form = "";
+ const ASymbol* sym = (*i)->to_symbol();
+ if (sym)
+ form = sym->sym();
- // Print symbol (possibly with type annotation)
- const AST* sym = *i++;
- out << sym;
+ out << (*i++);
+ if (i != tup->end())
+ out << " ";
+
+ if (form == "def") {
+ out << (*i++); // Print symbol
if (types)
out << " :" << cenv->type(tup->list_ref(2));
- // Print value on following lines, indented
+ // Print value indented on the next line
newline(out, indent + 2);
- print_tuple(out, tup, i, indent, true, cenv, types, false);
+ print_list(out, tup, i, indent + 2, cenv, types, false);
newline(out, 0);
- return out;
} else if (form == "fn") {
- out << (*i++) << " ";
- const ATuple* pat = (*i++)->as_tuple();
-
// Print prototype (possibly with parameter type annotations)
+ const ATuple* pat = (*i++)->as_tuple();
out << "(";
- print_tuple(out, pat, pat->begin(), indent, false, cenv, types, types);
+ print_list_one_line(out, pat, pat->begin(), indent, cenv, types, types);
- // Print body expression(s) on following lines, indented
+ // Print body expression(s) indented on the following lines
newline(out, indent + 2);
- return print_tuple(out, tup, i, indent + 2, true, cenv, types, false);
+ print_list(out, tup, i, indent + 2, cenv, types, false);
} else if (form == "if") {
- out << (*i++) << " ";
+ print_list(out, tup, i, indent + 4, cenv, types, true);
- // Print each condition and consequent pair separated by blank lines
- for (; i != tup->end(); ) {
- ATuple::const_iterator next = i;
- ++next;
-
- print_to(out, *i, indent + 2, cenv, types);
- if (next != tup->end()) {
- newline(out, indent + 2);
- print_to(out, *next++, indent + 2, cenv, types);
- newline(out, 0);
- newline(out, indent + 2);
- }
-
- i = next;
- }
-
- return out;
+ } else if (form == "match") {
+ out << (*i++);
+ newline(out, indent + 2);
+ print_list(out, tup, i, indent + 2, cenv, types, true);
} else if (form == "def-type") {
- print_tuple(out, tup, i, indent + 1, false, cenv, types, false);
+ out << (*i++);
+ newline(out, indent + 2);
+ print_list(out, tup, i, indent + 2, cenv, types, false);
newline(out, 0);
- return out;
} else if (form == "let") {
- out << (*i++) << " (";
+ out << "(";
const ATuple* vars = (*i)->as_tuple();
for (ATuple::const_iterator v = vars->begin(); v != vars->end();) {
out << (*v);
-
if (types)
out << " :" << cenv->tsubst.apply(cenv->tenv.var(*v));
@@ -160,18 +174,15 @@ print_to(ostream& out, const AST* ast, unsigned indent, CEnv* cenv, bool types)
out << ")";
}
newline(out, indent + 2);
- for (ATuple::const_iterator i = tup->iter_at(2); i != tup->end(); ++i)
- print_to(out, *i, indent + 2, cenv, types);
+ print_list(out, tup, tup->iter_at(2), indent + 2, cenv, types, false);
out << ")";
if (types)
out << " :" << cenv->tsubst.apply(cenv->tenv.var(tup->list_last()));
- return out;
-
} else {
- return print_tuple(out, tup, i, indent + 1, false, cenv, types, false);
+ print_list_one_line(out, tup, i, indent + 1, cenv, types, false);
}
- break;
+ return out;
}
case T_BOOL:
return out << ((((const ALiteral<bool>*)ast)->val) ? "#t" : "#f");