refactoring

This commit is contained in:
rusefi 2018-12-19 23:45:00 -05:00
parent 03d6876f2a
commit 176aa20908
7 changed files with 68 additions and 65 deletions

Binary file not shown.

View File

@ -31,8 +31,6 @@ public class ConfigDefinition {
private static int totalTsSize;
public static Map<String, ConfigStructure> structures = new HashMap<>();
public static Map<String, String> tsCustomLine = new HashMap<>();
public static Map<String, Integer> tsCustomSize = new HashMap<>();
public static StringBuilder settingContextHelp = new StringBuilder();
public static void main(String[] args) throws IOException {
@ -182,21 +180,7 @@ public class ConfigDefinition {
} else if (line.startsWith(END_STRUCT)) {
handleEndStruct(state, cHeader, tsHeader, javaFieldsWriter);
} else if (line.startsWith(BIT)) {
line = line.substring(BIT.length() + 1).trim();
String bitName;
String comment;
if (!line.contains(";")) {
bitName = line;
comment = "";
} else {
int index = line.indexOf(";");
bitName = line.substring(0, index);
comment = line.substring(index + 1);
}
ConfigField bitField = new ConfigField(bitName, comment, true, null, null, 0, null, false);
state.stack.peek().addBoth(bitField);
handleBitLine(state, line);
} else if (startsWithToken(line, CUSTOM)) {
line = line.substring(CUSTOM.length() + 1).trim();
@ -214,11 +198,17 @@ public class ConfigDefinition {
} catch (NumberFormatException e) {
throw new IllegalStateException("Size in " + line);
}
tsCustomSize.put(name, size);
tsCustomLine.put(name, tunerStudioLine);
state.tsCustomSize.put(name, size);
state.tsCustomLine.put(name, tunerStudioLine);
} else if (startsWithToken(line, DEFINE)) {
/**
* for example
* #define CLT_CURVE_SIZE 16
*/
processDefine(line.substring(DEFINE.length()).trim());
} else {
processLine(state, line);
processField(state, line);
}
}
cHeader.write("#endif" + EOL);
@ -226,6 +216,24 @@ public class ConfigDefinition {
cHeader.write(message);
}
private static void handleBitLine(ReaderState state, String line) {
line = line.substring(BIT.length() + 1).trim();
String bitName;
String comment;
if (!line.contains(";")) {
bitName = line;
comment = "";
} else {
int index = line.indexOf(";");
bitName = line.substring(0, index);
comment = line.substring(index + 1);
}
ConfigField bitField = new ConfigField(state, bitName, comment, true, null, null, 0, null, false);
state.stack.peek().addBoth(bitField);
}
private static boolean startsWithToken(String line, String token) {
return line.startsWith(token + " ") || line.startsWith(token + "\t");
}
@ -251,14 +259,14 @@ public class ConfigDefinition {
throw new IllegalStateException("Unexpected end_struct");
ConfigStructure structure = state.stack.pop();
System.out.println("Ending structure " + structure.getName());
structure.addAlignmentFill();
structure.addAlignmentFill(state);
ConfigDefinition.structures.put(structure.getName(), structure);
structure.headerWrite(cHeader);
if (state.stack.isEmpty()) {
totalTsSize = structure.writeTunerStudio("", tsHeader, 0);
totalTsSize = structure.writeTunerStudio(state,"", tsHeader, 0);
tsHeader.write("; total TS size = " + totalTsSize + EOL);
VariableRegistry.INSTANCE.register("TOTAL_CONFIG_SIZE", totalTsSize);
@ -266,17 +274,9 @@ public class ConfigDefinition {
}
}
private static void processLine(ReaderState state, String line) {
/**
* for example
* #define CLT_CURVE_SIZE 16
*/
if (startsWithToken(line, DEFINE)) {
processDefine(line.substring(DEFINE.length()).trim());
return;
}
private static void processField(ReaderState state, String line) {
ConfigField cf = ConfigField.parse(line);
ConfigField cf = ConfigField.parse(state, line);
if (cf == null)
throw new IllegalStateException("Cannot parse line [" + line + "]");
@ -287,7 +287,7 @@ public class ConfigDefinition {
if (cf.isIterate) {
structure.addC(cf);
for (int i = 1; i <= cf.arraySize; i++) {
ConfigField element = new ConfigField(cf.name + i, cf.comment, false, null,
ConfigField element = new ConfigField(state,cf.name + i, cf.comment, false, null,
cf.type, 1, cf.tsInfo, false);
structure.addTs(element);
}

View File

@ -17,7 +17,7 @@ import static com.rusefi.ConfigDefinition.EOL;
* 1/15/15
*/
public class ConfigField {
public static final ConfigField VOID = new ConfigField("", null, false, null, null, 1, null, false);
public static final ConfigField VOID = new ConfigField(null, "", null, false, null, null, 1, null, false);
private static final String typePattern = "([\\w\\d_]+)(\\[([\\w\\d]+)(\\s([\\w\\d]+))?\\])?";
private static final String namePattern = "[[\\w\\d\\s_]]+";
@ -49,7 +49,7 @@ public class ConfigField {
*/
public final boolean isIterate;
public ConfigField(String name, String comment, boolean isBit, String arraySizeAsText, String type,
public ConfigField(ReaderState state, String name, String comment, boolean isBit, String arraySizeAsText, String type,
int arraySize, String tsInfo, boolean isIterate) {
if (name == null)
throw new NullPointerException(comment + " " + isBit + " " + type);
@ -59,11 +59,7 @@ public class ConfigField {
this.isBit = isBit;
this.arraySizeAsText = arraySizeAsText;
this.type = type;
if (type == null) {
elementSize = 0;
} else {
elementSize = TypesHelper.getElementSize(type);
}
elementSize = TypesHelper.getElementSize(state, type);
this.arraySize = arraySize;
this.tsInfo = tsInfo;
this.isIterate = isIterate;
@ -77,7 +73,7 @@ public class ConfigField {
/**
* @see ConfigDefinitionTest#testParseLine()
*/
public static ConfigField parse(String line) {
public static ConfigField parse(ReaderState state, String line) {
Matcher matcher = FIELD.matcher(line);
if (!matcher.matches())
return null;
@ -97,7 +93,7 @@ public class ConfigField {
String tsInfo = matcher.group(10);
boolean isIterate = "iterate".equalsIgnoreCase(matcher.group(5));
ConfigField field = new ConfigField(name, comment, false, arraySizeAsText, type, arraySize,
ConfigField field = new ConfigField(state, name, comment, false, arraySizeAsText, type, arraySize,
tsInfo, isIterate);
System.out.println("type " + type);
System.out.println("name " + name);
@ -141,7 +137,7 @@ public class ConfigField {
'}';
}
public int writeTunerStudio(String prefix, Writer tsHeader, int tsPosition, ConfigField next, int bitIndex) throws IOException {
public int writeTunerStudio(ReaderState state, String prefix, Writer tsHeader, int tsPosition, ConfigField next, int bitIndex) throws IOException {
String nameWithPrefix = prefix + name;
VariableRegistry.INSTANCE.register(nameWithPrefix + "_offset", tsPosition);
@ -149,7 +145,7 @@ public class ConfigField {
ConfigStructure cs = ConfigDefinition.structures.get(type);
if (cs != null) {
String extraPrefix = cs.withPrefix ? name + "_" : "";
return cs.writeTunerStudio(prefix + extraPrefix, tsHeader, tsPosition);
return cs.writeTunerStudio(state, prefix + extraPrefix, tsHeader, tsPosition);
}
if (isBit) {
@ -164,10 +160,10 @@ public class ConfigField {
return tsPosition;
}
if (ConfigDefinition.tsCustomLine.containsKey(type)) {
String bits = ConfigDefinition.tsCustomLine.get(type);
if (state.tsCustomLine.containsKey(type)) {
String bits = state.tsCustomLine.get(type);
tsHeader.write("\t" + addTabsUpTo(nameWithPrefix, LENGTH));
int size = ConfigDefinition.tsCustomSize.get(type);
int size = state.tsCustomSize.get(type);
// tsHeader.headerWrite("\t" + size + ",");
// tsHeader.headerWrite("\t" + tsPosition + ",");
bits = bits.replaceAll("@OFFSET@", "" + tsPosition);

View File

@ -37,7 +37,7 @@ public class ConfigStructure {
return name;
}
public void addAlignmentFill() {
public void addAlignmentFill(ReaderState state) {
bitState.reset();
/**
* we make alignment decision based on C fields since we expect interation and non-iteration fields
@ -53,7 +53,7 @@ public class ConfigStructure {
int fillSize = totalSize % 4 == 0 ? 0 : 4 - (totalSize % 4);
if (fillSize != 0) {
ConfigField fill = new ConfigField("alignmentFill", "need 4 byte alignment", false,
ConfigField fill = new ConfigField(state, "alignmentFill", "need 4 byte alignment", false,
"" + fillSize,
UINT8_T, fillSize, null, false);
addBoth(fill);
@ -86,12 +86,12 @@ public class ConfigStructure {
cHeader.write("} " + name + ";" + ConfigDefinition.EOL + ConfigDefinition.EOL);
}
public int writeTunerStudio(String prefix, Writer tsHeader, int tsPosition) throws IOException {
public int writeTunerStudio(ReaderState state, String prefix, Writer tsHeader, int tsPosition) throws IOException {
FieldIterator fieldIterator = new FieldIterator();
for (int i = 0; i < tsFields.size(); i++) {
ConfigField next = i == tsFields.size() - 1 ? ConfigField.VOID : tsFields.get(i + 1);
ConfigField cf = tsFields.get(i);
tsPosition = cf.writeTunerStudio(prefix, tsHeader, tsPosition, next, fieldIterator.bitState.get());
tsPosition = cf.writeTunerStudio(state, prefix, tsHeader, tsPosition, next, fieldIterator.bitState.get());
fieldIterator.bitState.incrementBitIndex(cf, next);
}

View File

@ -1,13 +1,17 @@
package com.rusefi;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
/**
* (c) Andrey Belomutskiy
* 12/19/18
*/
class ReaderState {
public class ReaderState {
Stack<ConfigStructure> stack = new Stack<>();
public Map<String, Integer> tsCustomSize = new HashMap<>();
public Map<String, String> tsCustomLine = new HashMap<>();
public void ensureEmptyAfterProcessing() {
if (!this.stack.isEmpty())

View File

@ -9,13 +9,13 @@ import static com.rusefi.ConfigStructure.UINT_16_T;
*/
public class TypesHelper {
public static int getElementSize(String type) {
public static int getElementSize(ReaderState state, String type) {
if (type == null)
throw new NullPointerException("type");
return 0;
if (ConfigDefinition.structures.containsKey(type))
return ConfigDefinition.structures.get(type).totalSize;
if (ConfigDefinition.tsCustomSize.containsKey(type))
return ConfigDefinition.tsCustomSize.get(type);
if (state != null && state.tsCustomSize.containsKey(type))
return state.tsCustomSize.get(type);
if (type.equals(UINT8_T))
return 1;
if (type.equals(INT_16_T) || type.equals(UINT_16_T)) {

View File

@ -2,6 +2,7 @@ package com.rusefi.test;
import com.rusefi.ConfigDefinition;
import com.rusefi.ConfigField;
import com.rusefi.ReaderState;
import org.junit.Test;
import static org.junit.Assert.*;
@ -21,8 +22,9 @@ public class ConfigDefinitionTest {
@Test
public void testByteArray() {
ReaderState state = new ReaderState();
{
ConfigField cf = ConfigField.parse("uint8_t[8] field");
ConfigField cf = ConfigField.parse(state, "uint8_t[8] field");
assertEquals(cf.type, "uint8_t");
assertEquals(cf.arraySize, 8);
assertEquals(cf.getSize(null), 8);
@ -33,44 +35,45 @@ public class ConfigDefinitionTest {
@Test
public void testParseLine() {
assertNull(ConfigField.parse("int"));
ReaderState state = new ReaderState();
assertNull(ConfigField.parse(state, "int"));
{
ConfigField cf = ConfigField.parse("int field");
ConfigField cf = ConfigField.parse(state, "int field");
assertEquals(cf.type, "int");
assertEquals("Name", cf.name, "field");
}
{
ConfigField cf = ConfigField.parse("int_4 fie4_ld");
ConfigField cf = ConfigField.parse(state, "int_4 fie4_ld");
assertEquals(cf.type, "int_4");
assertEquals(cf.name, "fie4_ld");
}
{
ConfigField cf = ConfigField.parse("int_8 fi_eld;comm_;ts");
ConfigField cf = ConfigField.parse(state, "int_8 fi_eld;comm_;ts");
assertEquals(cf.type, "int_8");
assertEquals(cf.name, "fi_eld");
assertEquals("Comment", cf.comment, "comm_");
assertEquals(cf.tsInfo, "ts");
}
{
ConfigField cf = ConfigField.parse("int[3 iterate] field");
ConfigField cf = ConfigField.parse(state, "int[3 iterate] field");
assertEquals(cf.type, "int");
assertEquals(cf.arraySize, 3);
assertTrue("isIterate", cf.isIterate);
}
{
ConfigField cf = ConfigField.parse("int16_t crankingRpm;This,. value controls what RPM values we consider 'cranking' (any RPM below 'crankingRpm')\\nAnything above 'crankingRpm' would be 'running'");
ConfigField cf = ConfigField.parse(state, "int16_t crankingRpm;This,. value controls what RPM values we consider 'cranking' (any RPM below 'crankingRpm')\\nAnything above 'crankingRpm' would be 'running'");
assertEquals(cf.name, "crankingRpm");
assertEquals(cf.arraySize, 1);
assertEquals(cf.type, "int16_t");
}
{
ConfigField cf = ConfigField.parse("MAP_sensor_config_s map");
ConfigField cf = ConfigField.parse(state, "MAP_sensor_config_s map");
assertEquals(cf.name, "map");
assertEquals(cf.arraySize, 1);
assertEquals(cf.type, "MAP_sensor_config_s");
}
{
ConfigField cf = ConfigField.parse("MAP_sensor_config_s map;@see hasMapSensor\\n@see isMapAveragingEnabled");
ConfigField cf = ConfigField.parse(state, "MAP_sensor_config_s map;@see hasMapSensor\\n@see isMapAveragingEnabled");
assertEquals(cf.name, "map");
assertEquals(cf.arraySize, 1);
assertEquals(cf.type, "MAP_sensor_config_s");