Skip to content

Commit

Permalink
fix some issues with prettyprinting
Browse files Browse the repository at this point in the history
  • Loading branch information
psybers committed Nov 20, 2023
1 parent 5eaa832 commit 09963b4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/java/boa/datagen/util/PythonVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Map;
import java.util.Stack;

import org.apache.commons.lang.StringEscapeUtils;
import org.eclipse.dltk.ast.ASTNode;
import org.eclipse.dltk.ast.ASTVisitor;
import org.eclipse.dltk.ast.declarations.Decorator;
Expand Down Expand Up @@ -1945,7 +1944,7 @@ public boolean visit(org.eclipse.dltk.ast.expressions.StringLiteral md) {
b.setChange(status);
}

b.setLiteral(StringEscapeUtils.escapeJava(md.getValue()));
b.setLiteral(md.getValue());
expressions.push(b.build());

return true;
Expand Down
40 changes: 31 additions & 9 deletions src/java/boa/functions/langmode/PythonLangMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ public String prettyprint(final Statement stmt) {

case ASSERT:
s += indent() + "assert ";
s += prettyprint(stmt.getConditions(0));
if (stmt.getConditionsCount() > 0)
s += prettyprint(stmt.getConditions(0));
if (stmt.getExpressionsCount() > 0)
s += ", " + prettyprint(stmt.getExpressions(0));
s += "\n";
Expand Down Expand Up @@ -405,7 +406,10 @@ public String prettyprint(final Statement stmt) {
s += ", ";
s += prettyprint(stmt.getVariableDeclarations(i));
}
s += " in " + prettyprint(stmt.getConditions(0)) + ":\n";
if (stmt.getConditionsCount() == 1)
s += " in " + prettyprint(stmt.getConditions(0)) + ":\n";
else
s += ":\n";
indent++;
s += prettyprint(stmt.getStatements(0));
indent--;
Expand All @@ -418,7 +422,10 @@ public String prettyprint(final Statement stmt) {
return s;

case WHILE:
s += indent() + "while " + prettyprint(stmt.getConditions(0)) + ":\n";
if (stmt.getConditionsCount() > 0)
s += indent() + "while " + prettyprint(stmt.getConditions(0)) + ":\n";
else
s += indent() + "while:\n";
indent++;
s += prettyprint(stmt.getStatements(0));
indent--;
Expand All @@ -431,7 +438,10 @@ public String prettyprint(final Statement stmt) {
return s;

case IF:
s += indent() + "if " + prettyprint(stmt.getConditions(0)) + ":\n";
if (stmt.getConditionsCount() == 1)
s += indent() + "if " + prettyprint(stmt.getConditions(0)) + ":\n";
else
s += indent() + "if:\n";
indent++;
s += prettyprint(stmt.getStatements(0));
indent--;
Expand Down Expand Up @@ -519,7 +529,10 @@ public String prettyprint(final Expression e) {
case LOGICAL_NOT: return ppPrefix("not ", e);
case BIT_NOT: return ppPrefix("~", e);

case UNARY: return prettyprint(e.getExpressions(0)) + " " + prettyprint(e.getExpressions(1));
case UNARY:
if (e.getExpressionsCount() == 2)
return prettyprint(e.getExpressions(0)) + " " + prettyprint(e.getExpressions(1));
return prettyprint(e.getExpressions(0));
case PAREN: return "(" + prettyprint(e.getExpressions(0)) + ")";

case LITERAL: return e.getLiteral();
Expand All @@ -529,13 +542,17 @@ public String prettyprint(final Expression e) {
s += e.getVariable();
return s;
case CONDITIONAL:
s += prettyprint(e.getExpressions(0)) + " if " + prettyprint(e.getExpressions(1));
s += prettyprint(e.getExpressions(0)) + " if";
if (e.getExpressionsCount() > 1)
s += " " + prettyprint(e.getExpressions(1));
if (e.getExpressionsCount() > 2)
s += " else " + prettyprint(e.getExpressions(2));
return s;

case YIELD:
return indent() + "yield " + prettyprint(e.getExpressions(0)) + "\n";
if (e.getExpressionsCount() == 0)
return indent() + "yield";
return indent() + "yield " + prettyprint(e.getExpressions(0));

case METHODCALL:
for (int i = 0; i < e.getExpressionsCount(); i++)
Expand Down Expand Up @@ -581,7 +598,10 @@ public String prettyprint(final Expression e) {
if (i > 0)
s += ", ";
final Expression node = e.getExpressions(i);
s += prettyprint(node.getExpressions(0)) + ": " + prettyprint(node.getExpressions(1));
if (node.getExpressionsCount() == 2)
s += prettyprint(node.getExpressions(0)) + ": " + prettyprint(node.getExpressions(1));
else
s += prettyprint(node);
}
s += " }";
return s;
Expand Down Expand Up @@ -610,7 +630,7 @@ public String prettyprint(final Expression e) {
s += prettyprint(e.getExpressions(0));
s += ":";
s += prettyprint(e.getExpressions(1));
} else {
} else if (e.getExpressionsCount() == 1) {
s += prettyprint(e.getExpressions(0));
}
}
Expand Down Expand Up @@ -659,6 +679,8 @@ private String ppPostfix(final String op, final Expression e) {
}

private String ppInfix(final String op, final List<Expression> exps) {
if (exps.size() == 0) return "";

StringBuilder s = new StringBuilder();

s.append(prettyprint(exps.get(0)));
Expand Down

0 comments on commit 09963b4

Please sign in to comment.