From 0071e001fd933db13da530cf35819c15437b7d1a Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Tue, 10 Oct 2023 10:39:27 +0800 Subject: [PATCH] update codegen. add python code gen --- .gitignore | 1 + CodeGen/EnumsMigration/ClassidWriter.java | 8 +- CodeGen/EnumsMigration/CommonHelper.java | 6 +- CodeGen/EnumsMigration/ErrorsWriter.java | 6 +- CodeGen/EnumsMigration/GeneralWriter.java | 128 +++++++++++++++++++++- CodeGen/EnumsMigration/IndentHelper.java | 58 ++++++++-- CodeGen/EnumsMigration/MainRunner.java | 92 +++++++--------- 7 files changed, 226 insertions(+), 73 deletions(-) diff --git a/.gitignore b/.gitignore index b5cce05..5344b1f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ CodeGen/*.hpp CodeGen/*.cpp CodeGen/EnumsMigration/dest/*.hpp CodeGen/EnumsMigration/dest/*.cpp +CodeGen/EnumsMigration/dest/*.py Tools/*.bin Tools/*.obj diff --git a/CodeGen/EnumsMigration/ClassidWriter.java b/CodeGen/EnumsMigration/ClassidWriter.java index 56eff70..a632588 100644 --- a/CodeGen/EnumsMigration/ClassidWriter.java +++ b/CodeGen/EnumsMigration/ClassidWriter.java @@ -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(); } + } diff --git a/CodeGen/EnumsMigration/CommonHelper.java b/CodeGen/EnumsMigration/CommonHelper.java index 2ab62b4..34a2313 100644 --- a/CodeGen/EnumsMigration/CommonHelper.java +++ b/CodeGen/EnumsMigration/CommonHelper.java @@ -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,}", ""); } - + } diff --git a/CodeGen/EnumsMigration/ErrorsWriter.java b/CodeGen/EnumsMigration/ErrorsWriter.java index 35a6873..94b078d 100644 --- a/CodeGen/EnumsMigration/ErrorsWriter.java +++ b/CodeGen/EnumsMigration/ErrorsWriter.java @@ -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(); } } diff --git a/CodeGen/EnumsMigration/GeneralWriter.java b/CodeGen/EnumsMigration/GeneralWriter.java index 0ef5da9..df7106f 100644 --- a/CodeGen/EnumsMigration/GeneralWriter.java +++ b/CodeGen/EnumsMigration/GeneralWriter.java @@ -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 %s;", CommonHelper.getCKPartsNamespace(parts), - enum_t.mEnumName, enum_t.mEnumName); + indent.printf("extern const GeneralReflectionArray %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(); + } + } diff --git a/CodeGen/EnumsMigration/IndentHelper.java b/CodeGen/EnumsMigration/IndentHelper.java index d379128..c3aa7fc 100644 --- a/CodeGen/EnumsMigration/IndentHelper.java +++ b/CodeGen/EnumsMigration/IndentHelper.java @@ -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. *

* 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. *

* 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; + } + } } diff --git a/CodeGen/EnumsMigration/MainRunner.java b/CodeGen/EnumsMigration/MainRunner.java index f9b36aa..599bd45 100644 --- a/CodeGen/EnumsMigration/MainRunner.java +++ b/CodeGen/EnumsMigration/MainRunner.java @@ -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!"); }