update codegen. add python code gen

This commit is contained in:
yyc12345 2023-10-10 10:39:27 +08:00
parent d003b28b2e
commit 0071e001fd
7 changed files with 226 additions and 73 deletions

1
.gitignore vendored
View File

@ -11,6 +11,7 @@ CodeGen/*.hpp
CodeGen/*.cpp CodeGen/*.cpp
CodeGen/EnumsMigration/dest/*.hpp CodeGen/EnumsMigration/dest/*.hpp
CodeGen/EnumsMigration/dest/*.cpp CodeGen/EnumsMigration/dest/*.cpp
CodeGen/EnumsMigration/dest/*.py
Tools/*.bin Tools/*.bin
Tools/*.obj Tools/*.obj

View File

@ -5,8 +5,10 @@ import java.util.stream.Collectors;
* The nameof values writer for CK_CLASSID. * The nameof values writer for CK_CLASSID.
*/ */
public class ClassidWriter { public class ClassidWriter {
public static void writeAccVals(OutputStreamWriter writer, EnumsHelper.Enum_t classids) throws Exception {
IndentHelper indent = new IndentHelper(writer); public static void writeAccVal(String filename, EnumsHelper.Enum_t classids) throws Exception {
OutputStreamWriter writer = CommonHelper.openOutputFile(filename);
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.CPP);
indent.puts("const CkClassidReflectionArray CK_CLASSID {"); indent.puts("const CkClassidReflectionArray CK_CLASSID {");
indent.inc(); indent.inc();
@ -20,5 +22,7 @@ public class ClassidWriter {
indent.dec(); indent.dec();
indent.puts("};"); indent.puts("};");
writer.close();
} }
} }

View File

@ -113,6 +113,10 @@ public class CommonHelper {
enum CKParts { enum CKParts {
CK2, VxMath CK2, VxMath
} }
enum LangType {
CPP, Python
}
public static String getCKPartsNamespace(CKParts parts) { public static String getCKPartsNamespace(CKParts parts) {
switch (parts) { switch (parts) {
@ -187,5 +191,5 @@ public class CommonHelper {
// only remove at least 5 continuous star chars. // only remove at least 5 continuous star chars.
return cmt.replaceAll("\\*{5,}", ""); return cmt.replaceAll("\\*{5,}", "");
} }
} }

View File

@ -4,8 +4,9 @@ import java.io.OutputStreamWriter;
* The nameof values writer for CKERROR * The nameof values writer for CKERROR
*/ */
public class ErrorsWriter { public class ErrorsWriter {
public static void writeAccVals(OutputStreamWriter writer, EnumsHelper.Enum_t errors) throws Exception { public static void writeAccVal(String filename, EnumsHelper.Enum_t errors) throws Exception {
IndentHelper indent = new IndentHelper(writer); OutputStreamWriter writer = CommonHelper.openOutputFile(filename);
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.CPP);
indent.puts("const CkErrorReflectionArray CKERROR {"); indent.puts("const CkErrorReflectionArray CKERROR {");
indent.inc(); indent.inc();
@ -20,5 +21,6 @@ public class ErrorsWriter {
indent.dec(); indent.dec();
indent.puts("};"); indent.puts("};");
writer.close();
} }
} }

View File

@ -6,7 +6,7 @@ import java.io.OutputStreamWriter;
public class GeneralWriter { public class GeneralWriter {
public static void writeEnums(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog) throws Exception { public static void writeEnums(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog) throws Exception {
IndentHelper indent = new IndentHelper(writer); IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.CPP);
for (EnumsHelper.Enum_t enum_t : prog.mEnums) { for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
// write enum comment // write enum comment
indent.briefComment(enum_t.mEnumComment); indent.briefComment(enum_t.mEnumComment);
@ -35,25 +35,37 @@ public class GeneralWriter {
} }
} }
public static void writeEnums(String filename, EnumsHelper.EnumCollection_t prog) throws Exception {
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
writeEnums(fs, prog);
fs.close();
}
public static void writeEnum(OutputStreamWriter writer, EnumsHelper.Enum_t _enum) throws Exception { public static void writeEnum(OutputStreamWriter writer, EnumsHelper.Enum_t _enum) throws Exception {
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t(); EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
col.mEnums.add(_enum); col.mEnums.add(_enum);
writeEnums(writer, col); writeEnums(writer, col);
} }
public static void writeEnum(String filename, EnumsHelper.Enum_t _enum) throws Exception {
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
writeEnum(fs, _enum);
fs.close();
}
public static void writeAccVals(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog, public static void writeAccVals(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog,
CommonHelper.CKParts parts) throws Exception { CommonHelper.CKParts parts) throws Exception {
IndentHelper indent = new IndentHelper(writer); IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.CPP);
// write decls // write decls
for (EnumsHelper.Enum_t enum_t : prog.mEnums) { for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
indent.printf("extern const GeneralReflectionArray<LibCmo::%s::%s> %s;", CommonHelper.getCKPartsNamespace(parts), indent.printf("extern const GeneralReflectionArray<LibCmo::%s::%s> %s;",
enum_t.mEnumName, enum_t.mEnumName); CommonHelper.getCKPartsNamespace(parts), enum_t.mEnumName, enum_t.mEnumName);
} }
indent.puts(""); indent.puts("");
indent.puts(""); indent.puts("");
indent.puts(""); indent.puts("");
// write implements // write implements
for (EnumsHelper.Enum_t enum_t : prog.mEnums) { for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
// write enum desc header // write enum desc header
@ -73,6 +85,13 @@ public class GeneralWriter {
} }
} }
public static void writeAccVals(String filename, EnumsHelper.EnumCollection_t prog,
CommonHelper.CKParts parts) throws Exception {
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
writeAccVals(fs, prog, parts);
fs.close();
}
public static void writeAccVal(OutputStreamWriter writer, EnumsHelper.Enum_t _enum, CommonHelper.CKParts parts) public static void writeAccVal(OutputStreamWriter writer, EnumsHelper.Enum_t _enum, CommonHelper.CKParts parts)
throws Exception { throws Exception {
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t(); EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
@ -80,4 +99,101 @@ public class GeneralWriter {
writeAccVals(writer, col, parts); writeAccVals(writer, col, parts);
} }
public static void writeAccVal(String filename, EnumsHelper.Enum_t _enum, CommonHelper.CKParts parts)
throws Exception {
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
writeAccVal(fs, _enum, parts);
fs.close();
}
public static void writePyEnums(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog) throws Exception {
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Python);
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
// write enum start
indent.printf("class %s(enum.IntEnum):", enum_t.mEnumName);
indent.inc();
// write enum comment
indent.briefComment(enum_t.mEnumComment);
// write enum entries
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
// write entry self
if (enumEntry_t.mEntryValue == null) {
indent.printf("%s = auto()", enumEntry_t.mEntryName);
} else {
indent.printf("%s = %s", enumEntry_t.mEntryName, enumEntry_t.mEntryValue);
}
// write entry comment after member
indent.afterMemberComment(enumEntry_t.mEntryComment);
}
// enum tail
indent.dec();
}
}
public static void writePyEnums(String filename, EnumsHelper.EnumCollection_t prog) throws Exception {
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
writePyEnums(fs, prog);
fs.close();
}
public static void writePyEnum(OutputStreamWriter writer, EnumsHelper.Enum_t _enum) throws Exception {
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
col.mEnums.add(_enum);
writePyEnums(writer, col);
}
public static void writePyEnum(String filename, EnumsHelper.Enum_t _enum) throws Exception {
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
writePyEnum(fs, _enum);
fs.close();
}
public static void writePyAccVals(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog) throws Exception {
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Python);
// write implements
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
// write enum desc header
indent.printf("g_Annotation_%s: dict[int, str] = {", enum_t.mEnumName);
indent.inc();
// write enum desc entries
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
String comment = "";
if (enumEntry_t.mEntryComment != null) {
comment = CommonHelper.escapeString(enumEntry_t.mEntryComment);
}
indent.printf("%s.%s.value: \"%s\",", enum_t.mEnumName, enumEntry_t.mEntryName,
comment);
}
// write enum tail
indent.dec();
indent.puts("}");
}
}
public static void writePyAccVals(String filename, EnumsHelper.EnumCollection_t prog) throws Exception {
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
writePyAccVals(fs, prog);
fs.close();
}
public static void writePyAccVal(OutputStreamWriter writer, EnumsHelper.Enum_t _enum) throws Exception {
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
col.mEnums.add(_enum);
writePyAccVals(writer, col);
}
public static void writePyAccVal(String filename, EnumsHelper.Enum_t _enum) throws Exception {
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
writePyAccVal(fs, _enum);
fs.close();
}
} }

View File

@ -1,14 +1,29 @@
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
public class IndentHelper { public class IndentHelper {
public IndentHelper(OutputStreamWriter writer) { public IndentHelper(OutputStreamWriter writer, CommonHelper.LangType lang_type) {
mIndent = 0; mIndent = 0;
mLangType = lang_type;
mWriter = writer; mWriter = writer;
} }
private int mIndent; private int mIndent;
private CommonHelper.LangType mLangType;
private OutputStreamWriter mWriter; private OutputStreamWriter mWriter;
private void rawIndent() throws Exception {
for (int i = 0; i < mIndent; ++i) {
switch (mLangType) {
case CPP:
mWriter.write("\t");
break;
case Python:
mWriter.write(" ");
break;
}
}
}
public void inc() { public void inc() {
++mIndent; ++mIndent;
} }
@ -19,9 +34,7 @@ public class IndentHelper {
public void indent() throws Exception { public void indent() throws Exception {
mWriter.write("\n"); mWriter.write("\n");
for (int i = 0; i < mIndent; ++i) { rawIndent();
mWriter.write("\t");
}
} }
public void puts(String data) throws Exception { public void puts(String data) throws Exception {
@ -38,28 +51,57 @@ public class IndentHelper {
* Write standard Doxygen document comment. * Write standard Doxygen document comment.
* <p> * <p>
* Usually be called before writing content. * Usually be called before writing content.
*
* @param comment * @param comment
* @throws Exception * @throws Exception
*/ */
public void briefComment(String comment) throws Exception { public void briefComment(String comment) throws Exception {
if (comment == null) if (comment == null)
return; return;
puts("/**");
puts(comment); switch (mLangType) {
puts(" */"); case CPP:
rawIndent();
puts("/**");
puts(comment);
rawIndent();
puts(" */");
break;
case Python:
rawIndent();
puts("\"\"\"!");
puts(comment);
rawIndent();
puts("\"\"\"");
break;
}
} }
/** /**
* Write suffix style Doxygen document comment. * Write suffix style Doxygen document comment.
* <p> * <p>
* Usually be called after writing content. * Usually be called after writing content.
*
* @param comment * @param comment
* @throws Exception * @throws Exception
*/ */
public void afterMemberComment(String comment) throws Exception { public void afterMemberComment(String comment) throws Exception {
if (comment == null) if (comment == null)
return; return;
mWriter.write(String.format("\t/**< %s */", CommonHelper.removeEol(comment)));
switch (mLangType) {
case CPP:
mWriter.write(String.format("\t/**< %s */", CommonHelper.removeEol(comment)));
break;
case Python:
mWriter.write(String.format(" ##< %s", CommonHelper.removeEol(comment)));
break;
}
} }
} }

View File

@ -57,76 +57,60 @@ public class MainRunner {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
OutputStreamWriter fs = null;
// =========== CKERROR =========== // =========== CKERROR ===========
EnumsHelper.Enum_t ckerror = organiseDefines("src/CKError.txt", "CKERROR"); EnumsHelper.Enum_t ckerror = organiseDefines("src/CKError.txt", "CKERROR");
fs = CommonHelper.openOutputFile("dest/CKError.hpp"); GeneralWriter.writeEnum("dest/CKError.hpp", ckerror);
GeneralWriter.writeEnum(fs, ckerror); GeneralWriter.writePyEnum("dest/CKError.py", ckerror);
fs.close(); ErrorsWriter.writeAccVal("dest/CKError.AccVal.hpp", ckerror);
fs = CommonHelper.openOutputFile("dest/CKError.AccVal.hpp"); GeneralWriter.writePyAccVal("dest/CKError.AccVal.py", ckerror);
ErrorsWriter.writeAccVals(fs, ckerror);
fs.close();
// =========== CK_CLASSID =========== // =========== CK_CLASSID ===========
EnumsHelper.Enum_t classid = organiseClassid("src/CK_CLASSID.txt"); EnumsHelper.Enum_t classid = organiseClassid("src/CK_CLASSID.txt");
fs = CommonHelper.openOutputFile("dest/CK_CLASSID.hpp"); GeneralWriter.writeEnum("dest/CK_CLASSID.hpp", classid);
GeneralWriter.writeEnum(fs, classid); GeneralWriter.writePyEnum("dest/CK_CLASSID.py", classid);
fs.close(); ClassidWriter.writeAccVal("dest/CK_CLASSID.AccVal.hpp", classid);
fs = CommonHelper.openOutputFile("dest/CK_CLASSID.AccVal.hpp"); GeneralWriter.writePyAccVal("dest/CK_CLASSID.AccVal.py", classid);
ClassidWriter.writeAccVals(fs, classid);
fs.close();
// =========== Define2 =========== // =========== Define2 ===========
// Define2 do not need values. // Define2 do not need values.
EnumsHelper.EnumCollection_t def2 = getEnumsCollection("src/Defines2.txt"); EnumsHelper.EnumCollection_t def2 = getEnumsCollection("src/Defines2.txt");
fs = CommonHelper.openOutputFile("dest/CK_CLASSID.hpp"); GeneralWriter.writeEnums("dest/CK_CLASSID.hpp", def2);
GeneralWriter.writeEnums(fs, def2); GeneralWriter.writePyEnums("dest/CK_CLASSID.py", def2);
fs.close();
// =========== Combined enums =========== // =========== Combined enums ===========
EnumsHelper.EnumCollection_t ck2Enums = getEnumsCollection("src/CKEnums.txt"), EnumsHelper.EnumCollection_t ck2Enums = getEnumsCollection("src/CKEnums.txt"),
vxEnums = getEnumsCollection("src/VxEnums.txt"); vxEnums = getEnumsCollection("src/VxEnums.txt");
fs = CommonHelper.openOutputFile("dest/CKEnums.hpp"); GeneralWriter.writeEnums("dest/CKEnums.hpp", ck2Enums);
GeneralWriter.writeEnums(fs, ck2Enums); GeneralWriter.writePyEnums("dest/CKEnums.py", ck2Enums);
fs.close(); GeneralWriter.writeAccVals("dest/CKEnums.AccVal.hpp", ck2Enums, CommonHelper.CKParts.CK2);
fs = CommonHelper.openOutputFile("dest/CKEnums.AccVal.hpp"); GeneralWriter.writePyAccVals("dest/CKEnums.AccVal.py", ck2Enums);
GeneralWriter.writeAccVals(fs, ck2Enums, CommonHelper.CKParts.CK2); GeneralWriter.writeEnums("dest/VxEnums.hpp", vxEnums);
fs.close(); GeneralWriter.writePyEnums("dest/VxEnums.py", vxEnums);
fs = CommonHelper.openOutputFile("dest/VxEnums.hpp"); GeneralWriter.writeAccVals("dest/VxEnums.AccVal.hpp", vxEnums, CommonHelper.CKParts.VxMath);
GeneralWriter.writeEnums(fs, vxEnums); GeneralWriter.writePyAccVals("dest/VxEnums.AccVal.py", vxEnums);
fs.close();
fs = CommonHelper.openOutputFile("dest/VxEnums.AccVal.hpp");
GeneralWriter.writeAccVals(fs, vxEnums, CommonHelper.CKParts.VxMath);
fs.close();
// =========== Single enums =========== // =========== Single enums ===========
EnumsHelper.Enum_t single; EnumsHelper.Enum_t single;
single = organiseDefines("src/CK_STATECHUNK_CHUNKVERSION.txt", "CK_STATECHUNK_CHUNKVERSION"); single = organiseDefines("src/CK_STATECHUNK_CHUNKVERSION.txt", "CK_STATECHUNK_CHUNKVERSION");
fs = CommonHelper.openOutputFile("dest/CK_STATECHUNK_CHUNKVERSION.hpp"); GeneralWriter.writeEnum("dest/CK_STATECHUNK_CHUNKVERSION.hpp", single);
GeneralWriter.writeEnum(fs, single); GeneralWriter.writePyEnum("dest/CK_STATECHUNK_CHUNKVERSION.py", single);
fs.close(); GeneralWriter.writeAccVal("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.hpp", single, CommonHelper.CKParts.CK2);
fs = CommonHelper.openOutputFile("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.hpp"); GeneralWriter.writePyAccVal("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.py", single);
GeneralWriter.writeAccVal(fs, single, CommonHelper.CKParts.CK2);
fs.close();
single = organiseDefines("src/CK_STATECHUNK_DATAVERSION.txt", "CK_STATECHUNK_DATAVERSION"); single = organiseDefines("src/CK_STATECHUNK_DATAVERSION.txt", "CK_STATECHUNK_DATAVERSION");
fs = CommonHelper.openOutputFile("dest/CK_STATECHUNK_DATAVERSION.hpp"); GeneralWriter.writeEnum("dest/CK_STATECHUNK_DATAVERSION.hpp", single);
GeneralWriter.writeEnum(fs, single); GeneralWriter.writePyEnum("dest/CK_STATECHUNK_DATAVERSION.py", single);
fs.close(); GeneralWriter.writeAccVal("dest/CK_STATECHUNK_DATAVERSION.AccVal.hpp", single, CommonHelper.CKParts.CK2);
fs = CommonHelper.openOutputFile("dest/CK_STATECHUNK_DATAVERSION.AccVal.hpp"); GeneralWriter.writePyAccVal("dest/CK_STATECHUNK_DATAVERSION.AccVal.py", single);
GeneralWriter.writeAccVal(fs, single, CommonHelper.CKParts.CK2);
fs.close();
single = organiseDefines("src/CK_BITMAPDATA_FLAGS.txt", "CK_BITMAPDATA_FLAGS"); single = organiseDefines("src/CK_BITMAPDATA_FLAGS.txt", "CK_BITMAPDATA_FLAGS");
fs = CommonHelper.openOutputFile("dest/CK_BITMAPDATA_FLAGS.hpp"); GeneralWriter.writeEnum("dest/CK_BITMAPDATA_FLAGS.hpp", single);
GeneralWriter.writeEnum(fs, single); GeneralWriter.writePyEnum("dest/CK_BITMAPDATA_FLAGS.py", single);
fs.close(); GeneralWriter.writeAccVal("dest/CK_BITMAPDATA_FLAGS.AccVal.hpp", single, CommonHelper.CKParts.CK2);
fs = CommonHelper.openOutputFile("dest/CK_BITMAPDATA_FLAGS.AccVal.hpp"); GeneralWriter.writePyAccVal("dest/CK_BITMAPDATA_FLAGS.AccVal.py", single);
GeneralWriter.writeAccVal(fs, single, CommonHelper.CKParts.CK2);
fs.close();
// print message. // print message.
System.out.println("DONE!"); System.out.println("DONE!");
} }