|
11 | 11 | */ |
12 | 12 | public class CppCLI { |
13 | 13 |
|
14 | | - private static final Set<String> LIFECYCLE_METHOD_NAMES = Set.of( |
| 14 | + private static final Set<String> LIFECYCLE = Set.of( |
15 | 15 | "setup","draw","mousePressed","mouseReleased","mouseClicked", |
16 | 16 | "mouseMoved","mouseDragged","mouseWheel", |
17 | 17 | "keyPressed","keyReleased","keyTyped","settings" |
18 | 18 | ); |
19 | 19 |
|
20 | 20 | public static void main(String[] args) throws Exception { |
21 | 21 | String input = new String(System.in.readAllBytes(), StandardCharsets.UTF_8); |
22 | | - CppBuild build = allocate(); |
| 22 | + try { |
| 23 | + System.out.print(translate(input)); |
| 24 | + } catch (Exception e) { |
| 25 | + System.err.println(e.getMessage()); |
| 26 | + System.exit(1); |
| 27 | + } |
| 28 | + } |
23 | 29 |
|
24 | | - // String-level pre-processing (mirrors writeSketchImpl) |
| 30 | + private static String translate(String input) throws Exception { |
| 31 | + CppBuild b = allocate(); |
25 | 32 | String code = input; |
26 | | - code = invoke(build, "sanitize", code); |
27 | | - code = invoke(build, "removeUserIncludes", code); |
| 33 | + code = invoke(b, "sanitize", code); |
| 34 | + code = invoke(b, "removeUserIncludes", code); |
28 | 35 | code = code.replaceAll("(\\bfinal_suspend\\s*\\([^)]*\\))(\\s*\\{)", "$1 noexcept$2"); |
29 | 36 | code = code.replaceAll("(\\binitial_suspend\\s*\\([^)]*\\))(\\s*\\{)", "$1 noexcept$2"); |
30 | 37 | code = code.replaceAll("\\btranslate\\s*\\(\\s*0+\\.?0*f?\\s*,\\s*0+\\.?0*f?\\s*\\)\\s*;", ""); |
31 | 38 | code = code.replaceAll("\\btranslate\\s*\\(\\s*0+\\.?0*f?\\s*,\\s*0+\\.?0*f?\\s*,\\s*0+\\.?0*f?\\s*\\)\\s*;", ""); |
32 | | - code = invoke(build, "stripRawStringLiterals", code); |
| 39 | + code = invoke(b, "stripRawStringLiterals", code); |
33 | 40 | code = code.replaceAll("(?<=[0-9a-fA-FxXbB])'(?=[0-9a-fA-F])", ""); |
34 | | - code = invoke(build, "javaToC", code); |
35 | | - code = invoke(build, "stripNamespaceProcessing", code); |
36 | | - code = invoke(build, "preprocessMacros", code); |
| 41 | + code = invoke(b, "javaToC", code); |
| 42 | + code = invoke(b, "stripNamespaceProcessing", code); |
| 43 | + code = invoke(b, "preprocessMacros", code); |
37 | 44 |
|
38 | 45 | boolean hasSetup = code.contains("void setup("); |
39 | 46 | boolean hasDraw = code.contains("void draw("); |
40 | 47 |
|
| 48 | + StringBuilder out = new StringBuilder(); |
41 | 49 | StringBuilder preNs = new StringBuilder(); |
42 | | - StringBuilder header = new StringBuilder(); |
43 | | - appendHeader(header); |
| 50 | + appendHeader(out); |
44 | 51 |
|
45 | 52 | if (hasSetup || hasDraw) { |
46 | | - try { |
47 | | - String result = runFullPipeline(code, preNs); |
48 | | - System.out.print(preNs.toString()); |
49 | | - System.out.print(header.toString()); |
50 | | - System.out.print(result); |
51 | | - System.out.print("\nint main() { Processing::_PSketch sketch; sketch.run(); return 0; }\n"); |
52 | | - } catch (Exception e) { |
53 | | - System.err.println(e.getMessage()); |
54 | | - System.exit(1); |
55 | | - } |
| 53 | + String result = runPipeline(code, preNs); |
| 54 | + System.out.print(preNs); |
| 55 | + System.out.print(out); |
| 56 | + System.out.print(result); |
| 57 | + System.out.print("\nint main() { Processing::_PSketch sketch; sketch.run(); return 0; }\n"); |
| 58 | + return ""; |
56 | 59 | } else { |
57 | | - System.out.print(header.toString()); |
58 | | - System.out.print("\nnamespace Processing {\n\n"); |
59 | | - System.out.print(code); |
60 | | - System.out.print("\n} // namespace Processing\n\n"); |
61 | | - System.out.print("int main() { Processing::_PSketch sketch; sketch.run(); return 0; }\n"); |
| 60 | + out.append("\nnamespace Processing {\n\n"); |
| 61 | + out.append(code); |
| 62 | + out.append("\n} // namespace Processing\n\n"); |
| 63 | + out.append("int main() { Processing::_PSketch sketch; sketch.run(); return 0; }\n"); |
| 64 | + return out.toString(); |
62 | 65 | } |
63 | 66 | } |
64 | 67 |
|
65 | | - private static String runFullPipeline(String code, StringBuilder preNs) throws Exception { |
66 | | - // Step 0: parse |
| 68 | + private static String runPipeline(String code, StringBuilder preNs) throws Exception { |
67 | 69 | CompilationUnit cu = Parser.parse(code); |
68 | | - |
69 | | - // Step 1: enum extraction |
70 | 70 | EnumScopeExtractor.Result enumResult = EnumScopeExtractor.extract(cu.items()); |
71 | | - |
72 | | - // Step 2: lifecycle rewriting |
73 | | - List<TopLevelItem> afterLifecycle = LifecycleRewriter.rewrite(enumResult.rest, LIFECYCLE_METHOD_NAMES); |
74 | | - |
75 | | - // Step 3: class hoisting |
| 71 | + List<TopLevelItem> afterLifecycle = LifecycleRewriter.rewrite(enumResult.rest, LIFECYCLE); |
76 | 72 | ClassHoister.Result classResult = ClassHoister.hoist(afterLifecycle); |
77 | | - |
78 | | - // Step 4: _PSketch injection |
79 | | - List<PSketchInjector.Result> injectedClasses = PSketchInjector.injectAll(classResult.hoistedClasses); |
80 | | - List<TypeDef> finalClasses = injectedClasses.stream().map(PSketchInjector.Result::typeDef).toList(); |
81 | | - |
82 | | - // Step 5: array hoisting |
| 73 | + List<PSketchInjector.Result> injected = PSketchInjector.injectAll(classResult.hoistedClasses); |
| 74 | + List<TypeDef> finalClasses = injected.stream().map(PSketchInjector.Result::typeDef).toList(); |
83 | 75 | ArrayHoister.Result arrayResult = ArrayHoister.hoist(classResult.rest); |
84 | | - |
85 | | - // Step 6: dependency hoisting |
86 | | - DependencyHoister.Result depResult = DependencyHoister.hoist(arrayResult.rest, finalClasses, LIFECYCLE_METHOD_NAMES); |
87 | | - |
88 | | - // Step 7: forward declarations |
| 76 | + DependencyHoister.Result depResult = DependencyHoister.hoist(arrayResult.rest, finalClasses, LIFECYCLE); |
89 | 77 | List<FunctionDecl> forwardDecls = ForwardDeclGenerator.generate(depResult.hoistedFunctions); |
90 | 78 |
|
91 | | - // Build output |
92 | 79 | StringBuilder sb = new StringBuilder(); |
93 | 80 | sb.append("\nnamespace Processing {\n\n"); |
94 | 81 |
|
95 | | - // Hoist preprocessor directives and namespaces to file scope |
96 | 82 | List<TopLevelItem> filteredRest = new ArrayList<>(); |
97 | 83 | for (TopLevelItem item : depResult.rest) { |
98 | 84 | if (item instanceof PreprocessorLine pl) { |
99 | | - if (pl.rawText().startsWith("#include")) { |
100 | | - preNs.append(CodeGen.generateNode(item, 0)); |
101 | | - } else { |
102 | | - sb.append(CodeGen.generateNode(item, 0)); |
103 | | - } |
| 85 | + if (pl.rawText().startsWith("#include")) preNs.append(CodeGen.generateNode(item, 0)); |
| 86 | + else sb.append(CodeGen.generateNode(item, 0)); |
104 | 87 | continue; |
105 | 88 | } |
106 | 89 | if (item instanceof NamespaceDecl || item instanceof UsingNamespaceDecl) { |
107 | | - sb.append(CodeGen.generateNode(item, 0)); |
108 | | - continue; |
| 90 | + sb.append(CodeGen.generateNode(item, 0)); continue; |
109 | 91 | } |
110 | 92 | filteredRest.add(item); |
111 | 93 | } |
112 | 94 |
|
113 | | - // Forward decls |
114 | | - for (FunctionDecl fd : forwardDecls) { |
115 | | - sb.append(CodeGen.generateNode(fd, 0)); |
116 | | - } |
117 | | - |
118 | | - // Enum extracted items |
119 | | - for (TopLevelItem item : enumResult.enums) { |
120 | | - sb.append(CodeGen.generateNode(item, 0)); |
121 | | - } |
| 95 | + for (FunctionDecl fd : forwardDecls) sb.append(CodeGen.generateNode(fd, 0)); |
| 96 | + for (TopLevelItem e : enumResult.enums) sb.append(CodeGen.generateNode(e, 0)); |
122 | 97 |
|
123 | | - // Hoisted functions and variables from dependency hoister |
| 98 | + // Collect lifecycle bodies from hoisted functions |
| 99 | + List<FunctionDecl> lifecycleBodies = new ArrayList<>(); |
124 | 100 | for (TopLevelItem item : depResult.hoistedFunctions) { |
125 | | - sb.append(CodeGen.generateNode(item, 0)); |
126 | | - } |
127 | | - for (TopLevelItem item : depResult.hoistedVariables) { |
128 | | - sb.append(CodeGen.generateNode(item, 0)); |
129 | | - } |
130 | | - |
131 | | - // Hoisted arrays |
132 | | - for (TopLevelItem item : arrayResult.hoistedArrays) { |
133 | | - sb.append(CodeGen.generateNode(item, 0)); |
134 | | - } |
135 | | - |
136 | | - // Final classes (_PSketch injected) |
137 | | - for (TypeDef td : finalClasses) { |
138 | | - sb.append(CodeGen.generateNode(td, 0)); |
| 101 | + if (item instanceof FunctionDecl fd && LIFECYCLE.contains(fd.name()) && fd.body() != null) |
| 102 | + lifecycleBodies.add(fd); |
| 103 | + else sb.append(CodeGen.generateNode(item, 0)); |
139 | 104 | } |
| 105 | + for (TopLevelItem item : depResult.hoistedVariables) sb.append(CodeGen.generateNode(item, 0)); |
| 106 | + for (TopLevelItem item : arrayResult.hoistedArrays) sb.append(CodeGen.generateNode(item, 0)); |
| 107 | + for (TypeDef td : finalClasses) sb.append(CodeGen.generateNode(td, 0)); |
140 | 108 |
|
141 | | - // Remaining items |
| 109 | + // Also collect from filteredRest |
| 110 | + List<TopLevelItem> nonLifecycle = new ArrayList<>(); |
142 | 111 | for (TopLevelItem item : filteredRest) { |
143 | | - sb.append(CodeGen.generateNode(item, 0)); |
| 112 | + if (item instanceof FunctionDecl fd && LIFECYCLE.contains(fd.name()) && fd.body() != null) |
| 113 | + lifecycleBodies.add(fd); |
| 114 | + else nonLifecycle.add(item); |
144 | 115 | } |
145 | | - |
| 116 | + for (TopLevelItem item : nonLifecycle) sb.append(CodeGen.generateNode(item, 0)); |
| 117 | + |
| 118 | + // Build _PSketch with inline lifecycle bodies |
| 119 | + sb.append("class _PSketch : public PApplet {\npublic:\n"); |
| 120 | + Map<String,FunctionDecl> bodyMap = new LinkedHashMap<>(); |
| 121 | + for (FunctionDecl fd : lifecycleBodies) bodyMap.put(fd.name(), fd); |
| 122 | + for (String name : LIFECYCLE) { |
| 123 | + FunctionDecl fd = bodyMap.get(name); |
| 124 | + if (fd == null) continue; |
| 125 | + String rendered = CodeGen.generateNode(fd, 0).strip(); |
| 126 | + if (!rendered.contains("override")) |
| 127 | + rendered = rendered.replaceFirst("(\\b" + name + "\\s*\\([^)]*\\)\\s*)", "$1 override "); |
| 128 | + for (String line : rendered.split("\n", -1)) |
| 129 | + sb.append(" ").append(line).append("\n"); |
| 130 | + } |
| 131 | + sb.append("};\n"); |
146 | 132 | sb.append("\n} // namespace Processing\n"); |
147 | 133 | return sb.toString(); |
148 | 134 | } |
149 | 135 |
|
150 | 136 | private static void appendHeader(StringBuilder out) { |
151 | 137 | out.append("#include \"Processing.h\"\n"); |
152 | | - out.append("using std::vector; using std::string; using std::wstring;\n"); |
153 | | - out.append("using std::pair; using std::make_pair; using std::tuple;\n"); |
154 | | - out.append("using std::deque; using std::list; using std::stack; using std::queue;\n"); |
155 | | - out.append("using std::unordered_map; using std::unordered_set;\n"); |
156 | | - out.append("using std::sort; using std::shuffle; using std::reverse;\n"); |
157 | | - out.append("using std::unique_ptr; using std::shared_ptr;\n"); |
158 | | - out.append("using std::make_unique; using std::make_shared;\n"); |
159 | | - out.append("using std::to_string; using std::stoi; using std::stof; using std::stod;\n"); |
160 | | - out.append("using std::function;\n"); |
161 | | - out.append("using std::map; using std::set;\n"); |
162 | | - out.append("using std::array; using std::optional;\n"); |
163 | | - out.append("using std::runtime_error; using std::logic_error; using std::exception;\n"); |
164 | | - out.append("using std::numeric_limits;\n"); |
| 138 | + out.append("using namespace std;\n"); |
165 | 139 | } |
166 | 140 |
|
167 | | - private static String invoke(CppBuild build, String method, String code) throws Exception { |
| 141 | + private static String invoke(CppBuild b, String method, String code) throws Exception { |
168 | 142 | Method m = getMethod(CppBuild.class, method, String.class); |
169 | 143 | m.setAccessible(true); |
170 | | - return (String) m.invoke(build, code); |
| 144 | + return (String) m.invoke(b, code); |
171 | 145 | } |
172 | 146 |
|
173 | 147 | private static Method getMethod(Class<?> cls, String name, Class<?>... params) throws NoSuchMethodException { |
|
0 commit comments