Fixed sections formatting

master
Marco Maccaferri 2020-01-02 16:28:14 +01:00
rodzic c29fbe0401
commit e2e08d9592
4 zmienionych plików z 95 dodań i 4 usunięć

Wyświetl plik

@ -111,6 +111,58 @@ public class SourceFormatterTest {
assertEquals("test1 db 'string'\r\ntest2 db \"string\"\r\n", formatter.format());
}
@Test
public void testFormatIfSections() {
mnemonicColumn = 16;
argumentColumn = mnemonicColumn + 6;
commentColumn = mnemonicColumn + 16;
String text = "" +
"start .if 1\n" +
" di\n" +
" .else\n" +
" xor a\n" +
" .endif\n" +
" ret\n";
String expected = "" +
"start .if 1\r\n" +
" di\r\n" +
" .else\r\n" +
" xor a\r\n" +
" .endif\r\n" +
" ret\r\n" +
"\r\n";
assertEquals(expected, format(text));
}
@Test
public void testFormatProcSections() {
mnemonicColumn = 16;
argumentColumn = mnemonicColumn + 6;
commentColumn = mnemonicColumn + 16;
String text = "" +
"start di\n" +
" call clear\n" +
" ret\n" +
"\n" +
"clear proc\n" +
" xor a\n" +
" ret\n" +
" endp\n";
String expected = "" +
"start di\r\n" +
" call clear\r\n" +
" ret\r\n" +
"\r\n" +
"clear proc\r\n" +
" xor a\r\n" +
" ret\r\n" +
" endp\r\n" +
"\r\n";
assertEquals(expected, format(text));
}
public String format(String... sourceLines) {
StringBuilder builder = new StringBuilder();
for (String lineText : sourceLines) {

Wyświetl plik

@ -17,6 +17,8 @@ import nl.grauw.glass.AssemblyException;
import nl.grauw.glass.Line;
import nl.grauw.glass.Source;
import nl.grauw.glass.directives.If;
import nl.grauw.glass.directives.Proc;
import nl.grauw.glass.directives.Section;
import nl.grauw.glass.expressions.Annotation;
import nl.grauw.glass.expressions.BinaryOperator;
import nl.grauw.glass.expressions.Expression;
@ -233,10 +235,39 @@ public class SourceFormatter {
sb.append("\r\n");
if (line.getDirective() instanceof If) {
If ins = (If) line.getDirective();
format(ins.getThenSource());
if (ins.getElseSource() != null) {
format(ins.getElseSource());
if (line.getInstructionObject() != null) {
nl.grauw.glass.instructions.If ins = (nl.grauw.glass.instructions.If) line.getInstruction();
format(ins.getThenSource());
if (ins.getElseSource() != null) {
format(ins.getElseSource());
}
}
else {
If ins = (If) line.getDirective();
format(ins.getThenSource());
if (ins.getElseSource() != null) {
format(ins.getElseSource());
}
}
}
else if (line.getDirective() instanceof Section) {
if (line.getInstructionObject() != null) {
nl.grauw.glass.instructions.Section ins = (nl.grauw.glass.instructions.Section) line.getInstruction();
format(ins.getSource());
}
else {
Section ins = (Section) line.getDirective();
format(ins.getSource());
}
}
else if (line.getDirective() instanceof Proc) {
if (line.getInstructionObject() != null) {
nl.grauw.glass.instructions.Proc ins = (nl.grauw.glass.instructions.Proc) line.getInstruction();
format(ins.getSource());
}
else {
Proc ins = (Proc) line.getDirective();
format(ins.getSource());
}
}
} catch (AssemblyException e) {

Wyświetl plik

@ -18,4 +18,8 @@ public class Proc extends Directive {
super.register(scope, line);
}
public Source getSource() {
return source;
}
}

Wyświetl plik

@ -41,4 +41,8 @@ public class Proc extends InstructionFactory {
return new Empty.EmptyObject(context);
}
public Source getSource() {
return source;
}
}