Skip to content

Commit

Permalink
SD logs write with visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed May 2, 2024
1 parent 190f526 commit aedba43
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public void writeCOffsetCheck(PrintStream ps, String parentTypeName) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,6 @@ public void writeCOffsetHeader(PrintStream ps, String comment, String units) {

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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,53 +63,6 @@ public void writeCOffsetCheck(PrintStream ps, String parentTypeName) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,35 +155,6 @@ public String toString() {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand Down Expand Up @@ -31,6 +32,10 @@ public void writeSdLogs(ParseState parser, String sourceName) {
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]);
}
}

0 comments on commit aedba43

Please sign in to comment.