Commit 9e811d95 authored by Skylot's avatar Skylot

core: add method for print line numbers

parent 957d5394
...@@ -22,6 +22,8 @@ public class CodeWriter { ...@@ -22,6 +22,8 @@ public class CodeWriter {
public static final String NL = System.getProperty("line.separator"); public static final String NL = System.getProperty("line.separator");
public static final String INDENT = " "; public static final String INDENT = " ";
private static final boolean ADD_LINE_NUMBERS = false;
private static final String[] INDENT_CACHE = { private static final String[] INDENT_CACHE = {
"", "",
INDENT, INDENT,
...@@ -43,6 +45,9 @@ public class CodeWriter { ...@@ -43,6 +45,9 @@ public class CodeWriter {
public CodeWriter() { public CodeWriter() {
this.indent = 0; this.indent = 0;
this.indentStr = ""; this.indentStr = "";
if (ADD_LINE_NUMBERS) {
incIndent(2);
}
} }
public CodeWriter startLine() { public CodeWriter startLine() {
...@@ -65,6 +70,26 @@ public class CodeWriter { ...@@ -65,6 +70,26 @@ public class CodeWriter {
return this; return this;
} }
public CodeWriter startLineWithNum(int sourceLine) {
if (sourceLine == 0) {
startLine();
return this;
}
if (ADD_LINE_NUMBERS) {
newLine();
attachSourceLine(sourceLine);
String ln = "/* " + sourceLine + " */ ";
add(ln);
if (indentStr.length() > ln.length()) {
add(indentStr.substring(ln.length()));
}
} else {
startLine();
attachSourceLine(sourceLine);
}
return this;
}
public CodeWriter add(String str) { public CodeWriter add(String str) {
buf.append(str); buf.append(str);
offset += str.length(); offset += str.length();
......
...@@ -196,10 +196,7 @@ public class InsnGen { ...@@ -196,10 +196,7 @@ public class InsnGen {
state.add(flag); state.add(flag);
makeInsnBody(code, insn, state); makeInsnBody(code, insn, state);
} else { } else {
code.startLine(); code.startLineWithNum(insn.getSourceLine());
if (insn.getSourceLine() != 0) {
code.attachSourceLine(insn.getSourceLine());
}
if (insn.getResult() != null && insn.getType() != InsnType.ARITH_ONEARG) { if (insn.getResult() != null && insn.getType() != InsnType.ARITH_ONEARG) {
assignVar(code, insn); assignVar(code, insn);
code.add(" = "); code.add(" = ");
...@@ -304,7 +301,7 @@ public class InsnGen { ...@@ -304,7 +301,7 @@ public class InsnGen {
addArg(code, insn.getArg(0)); addArg(code, insn.getArg(0));
code.add(" == "); code.add(" == ");
addArg(code, insn.getArg(1)); addArg(code, insn.getArg(1));
code.add("? 0 : -1))"); code.add(" ? 0 : -1))");
break; break;
case INSTANCE_OF: { case INSTANCE_OF: {
......
...@@ -78,8 +78,8 @@ public class MethodGen { ...@@ -78,8 +78,8 @@ public class MethodGen {
if (clsAccFlags.isAnnotation()) { if (clsAccFlags.isAnnotation()) {
ai = ai.remove(AccessFlags.ACC_PUBLIC); ai = ai.remove(AccessFlags.ACC_PUBLIC);
} }
code.startLine(ai.makeString()); code.startLineWithNum(mth.getSourceLine());
code.attachSourceLine(mth.getSourceLine()); code.add(ai.makeString());
if (classGen.addGenericMap(code, mth.getGenericMap())) { if (classGen.addGenericMap(code, mth.getGenericMap())) {
code.add(' '); code.add(' ');
......
...@@ -101,14 +101,11 @@ public class RegionGen extends InsnGen { ...@@ -101,14 +101,11 @@ public class RegionGen extends InsnGen {
} }
private void makeIf(IfRegion region, CodeWriter code, boolean newLine) throws CodegenException { private void makeIf(IfRegion region, CodeWriter code, boolean newLine) throws CodegenException {
if (region.getTernRegion() != null) {
makeSimpleBlock(region.getTernRegion().getBlock(), code);
return;
}
if (newLine) { if (newLine) {
code.startLine(); code.startLineWithNum(region.getSourceLine());
} else {
code.attachSourceLine(region.getSourceLine());
} }
code.attachSourceLine(region.getSourceLine());
code.add("if ("); code.add("if (");
new ConditionGen(this).add(code, region.getCondition()); new ConditionGen(this).add(code, region.getCondition());
code.add(") {"); code.add(") {");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment