Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (45 sloc) 1.34 KB
import "ast.pil"
import gel
import gel::util
import gelly
String stripEndSpaces(String str, Int n) {
var m = new MutableString(str);
for(Int i = str.length-1; i > 0 && n > 0; i--) {
if(str[i] != ' ') {
return m.as<String>;
}
m.remove(i);
n--;
}
return m.as<String>;
}
String gelly::pretty(Term t, Int indentLevel) {
if(t instanceof GroupTerm) {
var gt = t.as<GroupTerm>;
if(gt.type == '(') {
return "(" + pretty(gt.group, indentLevel+1) + ")";
} else if(gt.type == '[') {
return "[" + pretty(gt.group, indentLevel+1) + "]";
} else if(gt.type == '{') {
return "{\n" + spaces(indentLevel+1) + stripEndSpaces(pretty(gt.group, indentLevel+1), 2) + "}";
}
} else if(t instanceof OpTerm) {
var ot = t.as<OpTerm>;
if(ot.op == ";") {
return pretty(ot.left, indentLevel) + ";\n" + spaces(indentLevel) + pretty(ot.right, indentLevel);
}
if(ot.op == "_") {
return pretty(ot.left, indentLevel) + " " + pretty(ot.right, indentLevel);
}
if(ot.isPrefix()) {
return ot.op + pretty(ot.right, indentLevel);
}
if(ot.isPostfix()) {
return pretty(ot.left, indentLevel) + ot.op;
}
return pretty(ot.left, indentLevel) + " " + ot.op + " " + pretty(ot.right, indentLevel);
} else if(t instanceof EmptyTerm) {
return "";
}
return t.as<String>;
}