Skip to content

Commit

Permalink
experimental table grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed Jan 3, 2025
1 parent d5a8483 commit 088a417
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ bitField: Bit identifier (',' QuotedString ',' QuotedString)? ('(' 'comment' ':'
unionField: 'union' ENDL+ fields 'end_union';
tableAxisSpec: ('min' integer 'max' integer|'num' integer);
tableField: 'table' identifier Resizable? ENDL+
tableField: 'table' Resizable? ENDL+
'rows' tableAxisSpec scalarField ENDL+
'cols' tableAxisSpec scalarField ENDL+
'values' 'max' integer scalarField ENDL+
'values' ('max' integer)? scalarField ENDL+
'end_table';
field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,45 @@ public void exitArrayLengthSpec(RusefiConfigGrammarParser.ArrayLengthSpecContext
}
}

@Override
public void exitTableField(RusefiConfigGrammarParser.TableFieldContext ctx) {
boolean isResizable = ctx.Resizable() != null;

ScalarField valuesPrototype = (ScalarField)scope.removeLastField();
ScalarField colPrototype = (ScalarField)scope.removeLastField();
ScalarField rowPrototype = (ScalarField)scope.removeLastField();

int maxRows;
int maxCols;

if (isResizable) {
int minRows = Integer.parseInt(ctx.tableAxisSpec(0).integer(0).getText());
maxRows = Integer.parseInt(ctx.tableAxisSpec(0).integer(1).getText());
int minCols = Integer.parseInt(ctx.tableAxisSpec(1).integer(0).getText());
maxCols = Integer.parseInt(ctx.tableAxisSpec(1).integer(1).getText());

int maxValues = Integer.parseInt(ctx.integer().getText());

// Check that we can at least fit a minimum size table
assert(maxValues >= minRows * minCols);

throw new IllegalStateException("resizable table not supported yet");
} else {
int rowCount = Integer.parseInt(ctx.tableAxisSpec(0).integer(0).getText());
int colCount = Integer.parseInt(ctx.tableAxisSpec(1).integer(0).getText());

maxRows = rowCount;
maxCols = colCount;
}

// Generate bins
scope.addField(new ArrayField<>(rowPrototype, new int[] {maxRows}, false));
scope.addField(new ArrayField<>(colPrototype, new int[] {maxCols}, false));

// Generate table
scope.addField(new ArrayField<>(valuesPrototype, new int[] {maxCols, maxRows}, false));
}

@Override
public void enterUnusedField(RusefiConfigGrammarParser.UnusedFieldContext ctx) {
scope.addField(new UnusedField(Integer.parseInt(ctx.integer().getText())));
Expand Down Expand Up @@ -691,5 +730,9 @@ public void addField(Field f) {

structFields.add(f);
}

public Field removeLastField() {
return structFields.remove(structFields.size() - 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public void bitFieldsBasic() throws IOException {
}

@Test
public void bitFieldsAdvanced() throws IOException {
public void bitFieldsAdvanced() {
ParseState state = parse(
"struct_no_prefix myStruct\n" +
"bit myBit,\"a\",\"b\";comment\n" +
Expand All @@ -325,7 +325,7 @@ public void bitFieldsAdvanced() throws IOException {
}

@Test
public void bitFieldsThirtyThreeBits() throws IOException {
public void bitFieldsThirtyThreeBits() {
StringBuilder input = new StringBuilder("struct myStruct\n");
for (int i = 0; i < 33; i++) {
input.append("bit myBit").append(i).append("\n");
Expand All @@ -339,4 +339,34 @@ public void bitFieldsThirtyThreeBits() throws IOException {
Assert.assertEquals(32, ((BitGroup)state.getLastStruct().fields.get(0)).bitFields.size());
Assert.assertEquals(1, ((BitGroup)state.getLastStruct().fields.get(1)).bitFields.size());
}

@Test
public void tableFixedSize() {
ParseState state = parse(
"struct_no_prefix myStruct\n" +
"table\n" +
"rows num 4 uint8_t rowVals\n" +
"cols num 8 uint16_t colVals\n" +
"values float tableVals\n" +
"end_table\n" +
"end_struct\n"
);

List<Field> fields = state.getLastStruct().fields;
Assert.assertEquals(3, fields.size());

ArrayField<ScalarField> fieldRows = (ArrayField<ScalarField>)fields.get(0);
ArrayField<ScalarField> fieldCols = (ArrayField<ScalarField>)fields.get(1);
ArrayField<ScalarField> fieldValues = (ArrayField<ScalarField>)fields.get(2);

Assert.assertEquals(1, fieldRows.length.length);
Assert.assertEquals(4, fieldRows.length[0]);

Assert.assertEquals(1, fieldCols.length.length);
Assert.assertEquals(8, fieldCols.length[0]);

Assert.assertEquals(2, fieldValues.length.length);
Assert.assertEquals(8, fieldValues.length[0]);
Assert.assertEquals(4, fieldValues.length[1]);
}
}

0 comments on commit 088a417

Please sign in to comment.