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/EnumsMigration/dest/*.hpp
CodeGen/EnumsMigration/dest/*.cpp
CodeGen/EnumsMigration/dest/*.py
Tools/*.bin
Tools/*.obj

View File

@ -5,8 +5,10 @@ import java.util.stream.Collectors;
* The nameof values writer for CK_CLASSID.
*/
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.inc();
@ -20,5 +22,7 @@ public class ClassidWriter {
indent.dec();
indent.puts("};");
writer.close();
}
}

View File

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

View File

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

View File

@ -6,7 +6,7 @@ import java.io.OutputStreamWriter;
public class GeneralWriter {
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) {
// write enum comment
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 {
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
col.mEnums.add(_enum);
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,
CommonHelper.CKParts parts) throws Exception {
IndentHelper indent = new IndentHelper(writer);
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.CPP);
// write decls
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
indent.printf("extern const GeneralReflectionArray<LibCmo::%s::%s> %s;", CommonHelper.getCKPartsNamespace(parts),
enum_t.mEnumName, enum_t.mEnumName);
indent.printf("extern const GeneralReflectionArray<LibCmo::%s::%s> %s;",
CommonHelper.getCKPartsNamespace(parts), enum_t.mEnumName, enum_t.mEnumName);
}
indent.puts("");
indent.puts("");
indent.puts("");
// write implements
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
// 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)
throws Exception {
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
@ -80,4 +99,101 @@ public class GeneralWriter {
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;
public class IndentHelper {
public IndentHelper(OutputStreamWriter writer) {
public IndentHelper(OutputStreamWriter writer, CommonHelper.LangType lang_type) {
mIndent = 0;
mLangType = lang_type;
mWriter = writer;
}
private int mIndent;
private CommonHelper.LangType mLangType;
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() {
++mIndent;
}
@ -19,9 +34,7 @@ public class IndentHelper {
public void indent() throws Exception {
mWriter.write("\n");
for (int i = 0; i < mIndent; ++i) {
mWriter.write("\t");
}
rawIndent();
}
public void puts(String data) throws Exception {
@ -38,28 +51,57 @@ public class IndentHelper {
* Write standard Doxygen document comment.
* <p>
* Usually be called before writing content.
*
* @param comment
* @throws Exception
*/
public void briefComment(String comment) throws Exception {
if (comment == null)
return;
puts("/**");
puts(comment);
puts(" */");
switch (mLangType) {
case CPP:
rawIndent();
puts("/**");
puts(comment);
rawIndent();
puts(" */");
break;
case Python:
rawIndent();
puts("\"\"\"!");
puts(comment);
rawIndent();
puts("\"\"\"");
break;
}
}
/**
* Write suffix style Doxygen document comment.
* <p>
* Usually be called after writing content.
*
* @param comment
* @throws Exception
*/
public void afterMemberComment(String comment) throws Exception {
if (comment == null)
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 {
OutputStreamWriter fs = null;
// =========== CKERROR ===========
EnumsHelper.Enum_t ckerror = organiseDefines("src/CKError.txt", "CKERROR");
fs = CommonHelper.openOutputFile("dest/CKError.hpp");
GeneralWriter.writeEnum(fs, ckerror);
fs.close();
fs = CommonHelper.openOutputFile("dest/CKError.AccVal.hpp");
ErrorsWriter.writeAccVals(fs, ckerror);
fs.close();
GeneralWriter.writeEnum("dest/CKError.hpp", ckerror);
GeneralWriter.writePyEnum("dest/CKError.py", ckerror);
ErrorsWriter.writeAccVal("dest/CKError.AccVal.hpp", ckerror);
GeneralWriter.writePyAccVal("dest/CKError.AccVal.py", ckerror);
// =========== CK_CLASSID ===========
EnumsHelper.Enum_t classid = organiseClassid("src/CK_CLASSID.txt");
fs = CommonHelper.openOutputFile("dest/CK_CLASSID.hpp");
GeneralWriter.writeEnum(fs, classid);
fs.close();
fs = CommonHelper.openOutputFile("dest/CK_CLASSID.AccVal.hpp");
ClassidWriter.writeAccVals(fs, classid);
fs.close();
GeneralWriter.writeEnum("dest/CK_CLASSID.hpp", classid);
GeneralWriter.writePyEnum("dest/CK_CLASSID.py", classid);
ClassidWriter.writeAccVal("dest/CK_CLASSID.AccVal.hpp", classid);
GeneralWriter.writePyAccVal("dest/CK_CLASSID.AccVal.py", classid);
// =========== Define2 ===========
// Define2 do not need values.
EnumsHelper.EnumCollection_t def2 = getEnumsCollection("src/Defines2.txt");
fs = CommonHelper.openOutputFile("dest/CK_CLASSID.hpp");
GeneralWriter.writeEnums(fs, def2);
fs.close();
GeneralWriter.writeEnums("dest/CK_CLASSID.hpp", def2);
GeneralWriter.writePyEnums("dest/CK_CLASSID.py", def2);
// =========== Combined enums ===========
EnumsHelper.EnumCollection_t ck2Enums = getEnumsCollection("src/CKEnums.txt"),
vxEnums = getEnumsCollection("src/VxEnums.txt");
fs = CommonHelper.openOutputFile("dest/CKEnums.hpp");
GeneralWriter.writeEnums(fs, ck2Enums);
fs.close();
fs = CommonHelper.openOutputFile("dest/CKEnums.AccVal.hpp");
GeneralWriter.writeAccVals(fs, ck2Enums, CommonHelper.CKParts.CK2);
fs.close();
fs = CommonHelper.openOutputFile("dest/VxEnums.hpp");
GeneralWriter.writeEnums(fs, vxEnums);
fs.close();
fs = CommonHelper.openOutputFile("dest/VxEnums.AccVal.hpp");
GeneralWriter.writeAccVals(fs, vxEnums, CommonHelper.CKParts.VxMath);
fs.close();
GeneralWriter.writeEnums("dest/CKEnums.hpp", ck2Enums);
GeneralWriter.writePyEnums("dest/CKEnums.py", ck2Enums);
GeneralWriter.writeAccVals("dest/CKEnums.AccVal.hpp", ck2Enums, CommonHelper.CKParts.CK2);
GeneralWriter.writePyAccVals("dest/CKEnums.AccVal.py", ck2Enums);
GeneralWriter.writeEnums("dest/VxEnums.hpp", vxEnums);
GeneralWriter.writePyEnums("dest/VxEnums.py", vxEnums);
GeneralWriter.writeAccVals("dest/VxEnums.AccVal.hpp", vxEnums, CommonHelper.CKParts.VxMath);
GeneralWriter.writePyAccVals("dest/VxEnums.AccVal.py", vxEnums);
// =========== Single enums ===========
EnumsHelper.Enum_t single;
single = organiseDefines("src/CK_STATECHUNK_CHUNKVERSION.txt", "CK_STATECHUNK_CHUNKVERSION");
fs = CommonHelper.openOutputFile("dest/CK_STATECHUNK_CHUNKVERSION.hpp");
GeneralWriter.writeEnum(fs, single);
fs.close();
fs = CommonHelper.openOutputFile("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.hpp");
GeneralWriter.writeAccVal(fs, single, CommonHelper.CKParts.CK2);
fs.close();
GeneralWriter.writeEnum("dest/CK_STATECHUNK_CHUNKVERSION.hpp", single);
GeneralWriter.writePyEnum("dest/CK_STATECHUNK_CHUNKVERSION.py", single);
GeneralWriter.writeAccVal("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.hpp", single, CommonHelper.CKParts.CK2);
GeneralWriter.writePyAccVal("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.py", single);
single = organiseDefines("src/CK_STATECHUNK_DATAVERSION.txt", "CK_STATECHUNK_DATAVERSION");
fs = CommonHelper.openOutputFile("dest/CK_STATECHUNK_DATAVERSION.hpp");
GeneralWriter.writeEnum(fs, single);
fs.close();
fs = CommonHelper.openOutputFile("dest/CK_STATECHUNK_DATAVERSION.AccVal.hpp");
GeneralWriter.writeAccVal(fs, single, CommonHelper.CKParts.CK2);
fs.close();
GeneralWriter.writeEnum("dest/CK_STATECHUNK_DATAVERSION.hpp", single);
GeneralWriter.writePyEnum("dest/CK_STATECHUNK_DATAVERSION.py", single);
GeneralWriter.writeAccVal("dest/CK_STATECHUNK_DATAVERSION.AccVal.hpp", single, CommonHelper.CKParts.CK2);
GeneralWriter.writePyAccVal("dest/CK_STATECHUNK_DATAVERSION.AccVal.py", single);
single = organiseDefines("src/CK_BITMAPDATA_FLAGS.txt", "CK_BITMAPDATA_FLAGS");
fs = CommonHelper.openOutputFile("dest/CK_BITMAPDATA_FLAGS.hpp");
GeneralWriter.writeEnum(fs, single);
fs.close();
fs = CommonHelper.openOutputFile("dest/CK_BITMAPDATA_FLAGS.AccVal.hpp");
GeneralWriter.writeAccVal(fs, single, CommonHelper.CKParts.CK2);
fs.close();
GeneralWriter.writeEnum("dest/CK_BITMAPDATA_FLAGS.hpp", single);
GeneralWriter.writePyEnum("dest/CK_BITMAPDATA_FLAGS.py", single);
GeneralWriter.writeAccVal("dest/CK_BITMAPDATA_FLAGS.AccVal.hpp", single, CommonHelper.CKParts.CK2);
GeneralWriter.writePyAccVal("dest/CK_BITMAPDATA_FLAGS.AccVal.py", single);
// print message.
System.out.println("DONE!");
}