Skip to content

Commit

Permalink
feat: format debugger info
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed May 14, 2024
1 parent 1bb3b2d commit cb4cc5a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 88 deletions.
176 changes: 95 additions & 81 deletions src/main/java/reincarnation/Debugger.java
Original file line number Diff line number Diff line change
Expand Up @@ -495,87 +495,6 @@ private boolean hasDominator(Node target, Node dominator, Set<Node> nodes) {
return false;
}

private static final class ColumnFormatter {

/** The tab length. */
private final int tab = 4;

private final List<List<String>> table = new ArrayList();

private List<String> row = new ArrayList();

private void write(String text) {
row.add(text);
}

private void write(String prefix, List<Node> nodes) {
write(nodes.stream().map(n -> String.valueOf(n.id)).collect(Collectors.joining(",", prefix + "[", "]")));
}

private void writeCode(String prefix, List<Operand> operands) {
StringBuilder builder = new StringBuilder(prefix);
whileDebug = true;
for (int i = 0; i < operands.size(); i++) {
Operand operand = operands.get(i);

builder.append(operand.view().strip()).append(" [").append(operand.info()).append("]");

if (i != operands.size() - 1) {
builder.append(" ");
}
}
whileDebug = false;
write(builder.toString());
}

private void writeln() {
table.add(row);
row = new ArrayList();
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
int fix = 7;
int[] maxs = new int[fix];
Arrays.fill(maxs, 0);

for (List<String> row : table) {
for (int i = 0; i < fix; i++) {
int length = row.get(i).length();
int max = Math.ceilDiv(length, tab) * tab;
if (max == length) {
max += tab;
}
maxs[i] = Math.max(maxs[i], max);
}
}

StringBuilder builder = new StringBuilder();
for (List<String> row : table) {
for (int i = 0; i < row.size(); i++) {
if (fix <= i) {
builder.append(row.get(i));
} else {
builder.append(align(row.get(i), maxs[i]));
}
}
builder.append("\n");
}

return builder.toString();
}

private String align(String text, int max) {
int remaining = max - text.length();
int num = Math.floorDiv(remaining + (remaining % tab == 0 ? 0 : tab), tab);
return text + "\t".repeat(num);

}
}

/** The compiling route. */
private Deque<DebugContext> route = new ArrayDeque();

Expand Down Expand Up @@ -689,6 +608,101 @@ private DebugContext latest() {
return route.isEmpty() ? lastAccessed : route.peekFirst();
}

/** The tab size. */
private static final int tab = 4;

/**
* Calculate the max length of the given line text.
*
* @param line
* @return
*/
static int calculateLineLength(String line) {
int length = line.length();
int calculated = Math.ceilDiv(length, tab) * tab;

return calculated == length ? calculated + tab : calculated;
}

/**
* Align the line by tab.
*
* @param text
* @param max
* @return
*/
static String alignLine(String text, int max) {
int remaining = max - text.length();
int size = Math.floorDiv(remaining + (remaining % tab == 0 ? 0 : tab), tab);
return text + "\t".repeat(size);
}

private static final class ColumnFormatter {

private final List<List<String>> table = new ArrayList();

private List<String> row = new ArrayList();

private void write(String text) {
row.add(text);
}

private void write(String prefix, List<Node> nodes) {
write(nodes.stream().map(n -> String.valueOf(n.id)).collect(Collectors.joining(",", prefix + "[", "]")));
}

private void writeCode(String prefix, List<Operand> operands) {
StringBuilder builder = new StringBuilder(prefix);
whileDebug = true;
for (int i = 0; i < operands.size(); i++) {
Operand operand = operands.get(i);

builder.append(operand.view().strip()).append(" [").append(operand.info()).append("]");

if (i != operands.size() - 1) {
builder.append(" ");
}
}
whileDebug = false;
write(builder.toString());
}

private void writeln() {
table.add(row);
row = new ArrayList();
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
int fix = 7;
int[] maxs = new int[fix];
Arrays.fill(maxs, 0);

for (List<String> row : table) {
for (int i = 0; i < fix; i++) {
maxs[i] = Math.max(maxs[i], calculateLineLength(row.get(i)));
}
}

StringBuilder builder = new StringBuilder();
for (List<String> row : table) {
for (int i = 0; i < row.size(); i++) {
if (fix <= i) {
builder.append(row.get(i));
} else {
builder.append(alignLine(row.get(i), maxs[i]));
}
}
builder.append("\n");
}

return builder.toString();
}
}

/**
*
*/
Expand Down
10 changes: 3 additions & 7 deletions src/test/java/reincarnation/CompileInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,18 @@ public void asmfier(Class typeForJavac, Class typeForECJ) {
Entry<Class, List<String>> nextE = ecj.next();

List<DiffRow> rows = generator.generateDiffRows(nextE.getValue(), nextJ.getValue());
int maxE = rows.stream().mapToInt(row -> row.getOldLine().length()).max().getAsInt() + 4;
int maxE = rows.stream().mapToInt(row -> Debugger.calculateLineLength(row.getOldLine())).max().getAsInt();

List<String> diff = new ArrayList();
diff.add(align("ECJ", maxE) + "Javac");
diff.add(Debugger.alignLine("ECJ", maxE) + "Javac");
for (DiffRow row : rows) {
diff.add(align(row.getOldLine(), maxE) + row.getNewLine());
diff.add(Debugger.alignLine(row.getOldLine(), maxE) + row.getNewLine());
}

asmfied.add(new ASMified(nextJ.getKey(), nextE.getValue(), nextJ.getValue(), diff));
}
}

private String align(String text, int max) {
return text + "\t".repeat((max - text.length()) / 4);
}

/**
* Write line text.
*
Expand Down

0 comments on commit cb4cc5a

Please sign in to comment.