SD logs write with visitor

This commit is contained in:
Matthew Kennedy 2024-05-01 23:48:45 -07:00
parent 190f526a85
commit aedba437de
6 changed files with 82 additions and 101 deletions

View File

@ -66,11 +66,6 @@ public class ArrayLayout extends Layout {
this.prototypeLayout.writeCOffsetCheck(ps, parentTypeName);
}
@Override
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) {
this.prototypeLayout.writeSdLogLayout(ps, prefixer, sourceName, this.length);
}
@Override
protected void doVisit(ILayoutVisitor v, PrintStream ps, StructNamePrefixer pfx, int offsetAdd, int[] arrayDims) {
if (arrayDims.length != 0) {

View File

@ -51,25 +51,6 @@ public abstract class Layout {
public void writeCOffsetCheck(PrintStream ps, String parentTypeName) { }
public void writeSdLogLayout(PrintStream ps, String sourceName) {
// TODO
final String prefix = null;
StructNamePrefixer prefixer = new StructNamePrefixer('.');
if (prefix != null) {
prefixer.push(prefix);
}
writeSdLogLayout(ps, prefixer, sourceName);
}
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) { }
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, int[] arrayLength) {
throw new IllegalStateException("This type can't be in an array!");
}
protected void doVisit(ILayoutVisitor v, PrintStream ps, StructNamePrefixer pfx, int offsetAdd, int[] arrayDims)
{
throw new IllegalStateException("This type is missing its visitor");

View File

@ -63,53 +63,6 @@ public class ScalarLayout extends Layout {
ps.println(");");
}
@Override
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) {
writeSdLogLayout(ps, prefixer, "", "", sourceName);
}
@Override
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, int[] arrayLength) {
if (arrayLength.length != 1) {
throw new IllegalStateException("Output channels don't support multi dimension arrays");
}
for (int i = 0; i < arrayLength[0]; i++) {
writeSdLogLayout(ps, prefixer, "[" + i + "]", " " + (i + 1), sourceName);
}
}
private void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String arraySub, String commentSuffix, String sourceName) {
if (this.name.startsWith("unused")) {
return;
}
// {engine->outputChannels.mafMeasured, "MAF", "kg/h", 1},
final String prefixedName = prefixer.get(this.name);
ps.print("\t{");
ps.print(sourceName);
ps.print(prefixedName);
ps.print(arraySub);
ps.print(", \"");
String comment = this.options.comment;
// default to name in case of no comment
if (comment == null || comment.length() == 0) {
comment = prefixedName;
}
ps.print(comment);
ps.print(commentSuffix);
ps.print("\", ");
ps.print(this.options.units);
ps.print(", ");
ps.print(this.options.digits);
ps.println("},");
}
@Override
protected void doVisit(ILayoutVisitor v, PrintStream ps, StructNamePrefixer pfx, int offsetAdd, int[] arrayDims) {
v.visit(this, ps, pfx, offsetAdd, arrayDims);

View File

@ -155,35 +155,6 @@ public class StructLayout extends Layout {
return "Struct " + this.typeName + " " + super.toString();
}
private void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, String name) {
if (!this.noPrefix) {
prefixer.push(name);
}
this.children.forEach(c -> c.writeSdLogLayout(ps, prefixer, sourceName));
if (!this.noPrefix) {
prefixer.pop();
}
}
@Override
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) {
writeSdLogLayout(ps, prefixer, sourceName, this.name);
}
@Override
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, int[] arrayLength) {
if (arrayLength.length != 1) {
throw new IllegalStateException("Output channels don't support multi dimension arrays");
}
// TODO: This doesn't quite work, as it's unclear how to make automatic naming work properly
// for (int i = 0; i < arrayLength[0]; i++) {
// writeSdLogLayout(ps, prefixer, sourceName, this.name + "[" + i + "]");
// }
}
@Override
protected void doVisit(ILayoutVisitor v, PrintStream ps, StructNamePrefixer pfx, int offsetAdd, int[] arrayDims) {
v.visit(this, ps, pfx, offsetAdd, arrayDims);

View File

@ -0,0 +1,76 @@
package com.rusefi.newparse.outputs;
import com.rusefi.newparse.layout.*;
import java.io.PrintStream;
public class SdLogVisitor extends ILayoutVisitor {
private final String mSourceName;
public SdLogVisitor(String sourceName) {
mSourceName = sourceName;
}
@Override
public void visit(StructLayout struct, PrintStream ps, StructNamePrefixer prefixer, int offsetAdd, int[] arrayDims) {
if (arrayDims.length == 0) {
visit(struct, ps, prefixer, offsetAdd, struct.name);
} else if (arrayDims.length == 1) {
int elementOffset = offsetAdd;
for (int i = 0; i < arrayDims[0]; i++) {
visit(struct, ps, prefixer, elementOffset, struct.name + "[" + i + "]");
elementOffset += struct.size;
}
} else {
throw new IllegalStateException("Output channels don't support multi dimension arrays");
}
}
private void visitScalar(ScalarLayout scalar, PrintStream ps, StructNamePrefixer prefixer, String arraySub, String commentSuffix) {
final String prefixedName = prefixer.get(scalar.name);
ps.print("\t{");
ps.print(mSourceName);
ps.print(prefixedName);
ps.print(arraySub);
ps.print(", \"");
String comment = scalar.options.comment;
// default to name in case of no comment
if (comment == null || comment.isEmpty()) {
comment = prefixedName;
}
ps.print(comment.split("\\n")[0]);
ps.print(commentSuffix);
ps.print("\", ");
ps.print(scalar.options.units);
ps.print(", ");
ps.print(scalar.options.digits);
ps.println("},");
}
@Override
public void visit(ScalarLayout scalar, PrintStream ps, StructNamePrefixer prefixer, int offsetAdd, int[] arrayDims) {
if (scalar.name.startsWith("unused")) {
return;
}
if (arrayDims.length == 0) {
visitScalar(scalar, ps, prefixer, "", "");
} else if (arrayDims.length == 1) {
for (int i = 0; i < arrayDims[0]; i++) {
visitScalar(scalar, ps, prefixer, "[" + i + "]", " " + (i + 1));
}
} else {
throw new IllegalStateException("SD log doesn't support multi dimension arrays");
}
}
@Override
public void visit(BitGroupLayout bitGroup, PrintStream ps, StructNamePrefixer prefixer, int offsetAdd, int[] arrayDims) {
}
}

View File

@ -2,6 +2,7 @@ package com.rusefi.newparse.outputs;
import com.rusefi.newparse.ParseState;
import com.rusefi.newparse.layout.StructLayout;
import com.rusefi.newparse.layout.StructNamePrefixer;
import com.rusefi.newparse.parsing.Struct;
import java.io.FileNotFoundException;
@ -31,6 +32,10 @@ public class SdLogWriter {
Struct s = parser.getStructs().get(parser.getStructs().size() - 1);
StructLayout sl = new StructLayout(0, "root", s);
sl.writeSdLogLayout(ps, sourceName);
SdLogVisitor v = new SdLogVisitor(sourceName);
StructNamePrefixer prefixer = new StructNamePrefixer('.');
v.visit(sl, ps, prefixer, 0, new int[0]);
}
}