Skip to content

Commit 81d24cb

Browse files
authored
Merge pull request #95 from DanexCodr/copilot/add-full-compile-flag
Add `-f/--full` option to force full project compile from `CommandRunner compile`
2 parents 726d0ad + fc242ef commit 81d24cb

1 file changed

Lines changed: 93 additions & 5 deletions

File tree

src/main/java/cod/runner/CommandRunner.java

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
import cod.ptac.Executor;
1111
import cod.ptac.Options;
1212

13+
import java.io.File;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
1317
public class CommandRunner extends BaseRunner {
1418

1519
private final Interpreter interpreter;
@@ -113,11 +117,31 @@ public void configure(RunnerConfig config) {
113117
private void handleCompileCommand(String[] args) throws Exception {
114118
if (args.length < 2) {
115119
outE("Error: No source file specified for compilation");
116-
outE("Usage: CommandRunner compile <filename>");
120+
outE("Usage: CommandRunner compile <filename> [-f|--full]");
117121
return;
118122
}
119-
120-
String sourceFile = args[1];
123+
124+
String sourceFile = null;
125+
boolean fullCompile = false;
126+
for (int i = 1; i < args.length; i++) {
127+
String arg = args[i];
128+
if ("-f".equals(arg) || "--full".equals(arg)) {
129+
fullCompile = true;
130+
} else if (sourceFile == null) {
131+
sourceFile = arg;
132+
} else {
133+
outE("Error: Unexpected argument: " + arg);
134+
outE("Usage: CommandRunner compile <filename> [-f|--full]");
135+
return;
136+
}
137+
}
138+
139+
if (sourceFile == null || sourceFile.trim().isEmpty()) {
140+
outE("Error: No source file specified for compilation");
141+
outE("Usage: CommandRunner compile <filename> [-f|--full]");
142+
return;
143+
}
144+
121145
DebugSystem.info(NAME + LOG_TAG, "Compiling: " + sourceFile);
122146

123147
Interpreter tempInterpreter = new Interpreter();
@@ -135,7 +159,12 @@ private void handleCompileCommand(String[] args) throws Exception {
135159
String projectRoot = Index.getProjectRoot();
136160
if (projectRoot != null) {
137161
IRManager bm = new IRManager(projectRoot);
138-
162+
163+
if (fullCompile) {
164+
compileAllUnits(bm, srcMainRoot);
165+
return;
166+
}
167+
139168
int compiled = 0;
140169
for (Type type : ast.unit.types) {
141170
bm.save(ast.unit.name, type);
@@ -153,6 +182,63 @@ private void handleCompileCommand(String[] args) throws Exception {
153182
}
154183
}
155184

185+
private void compileAllUnits(IRManager bm, String srcMainRoot) {
186+
List<File> codFiles = new ArrayList<>();
187+
collectCodFiles(new File(srcMainRoot), codFiles);
188+
189+
int compiled = 0;
190+
int scanned = codFiles.size();
191+
int failed = 0;
192+
193+
for (File file : codFiles) {
194+
try {
195+
Interpreter fileInterpreter = new Interpreter();
196+
fileInterpreter.setFilePath(file.getAbsolutePath());
197+
Program fileAst = parse(file.getAbsolutePath(), fileInterpreter);
198+
if (fileAst == null || fileAst.unit == null || fileAst.unit.types == null) {
199+
continue;
200+
}
201+
String unitName = fileAst.unit.name;
202+
// Files parsed as the synthetic "default" unit have no stable entry path in project.codc,
203+
// so they are intentionally skipped during full-compilation artifact emission.
204+
if (unitName == null || "default".equals(unitName)) {
205+
continue;
206+
}
207+
for (Type type : fileAst.unit.types) {
208+
bm.save(unitName, type);
209+
compiled++;
210+
}
211+
} catch (Exception e) {
212+
failed++;
213+
DebugSystem.warn(NAME + LOG_TAG, "Failed to compile file " + file.getAbsolutePath() + ": " + e.getMessage());
214+
}
215+
}
216+
217+
out(String.format("Full compilation complete: %d type(s) compiled from %d file(s)", compiled, scanned));
218+
if (failed > 0) {
219+
outE("Warning: " + failed + " file(s) failed during full compilation");
220+
}
221+
}
222+
223+
private void collectCodFiles(File root, List<File> sink) {
224+
if (root == null || sink == null || !root.exists()) {
225+
return;
226+
}
227+
if (root.isFile()) {
228+
if (root.getName().endsWith(".cod")) {
229+
sink.add(root);
230+
}
231+
return;
232+
}
233+
File[] children = root.listFiles();
234+
if (children == null) {
235+
return;
236+
}
237+
for (File child : children) {
238+
collectCodFiles(child, sink);
239+
}
240+
}
241+
156242
/**
157243
* Initialize IR manager after project root is known
158244
*/
@@ -243,7 +329,7 @@ private void compileToBytecode(Program ast) {
243329
private void printHelp() {
244330
out("Coderive CommandRunner - Execute Coderive programs");
245331
out("Usage: CommandRunner <filename> [options]");
246-
out(" CommandRunner compile <filename>");
332+
out(" CommandRunner compile <filename> [-f|--full]");
247333
out();
248334
out("Options:");
249335
out(" -i, --interpret Interpret the program (default)");
@@ -255,6 +341,7 @@ private void printHelp() {
255341
out();
256342
out("Commands:");
257343
out(" compile <file> Compile source to bytecode container (.codc with .codb entries)");
344+
out(" -f, --full Full compile all .cod files under src/main");
258345
out("Environment flags:");
259346
out(" COD_PTAC_MODE=interpreter|compile-only|compile-execute");
260347
out(" COD_PTAC_FALLBACK=true|false");
@@ -263,6 +350,7 @@ private void printHelp() {
263350
out(" CommandRunner program.cod");
264351
out(" CommandRunner program.cod -o output.txt");
265352
out(" CommandRunner compile program.cod");
353+
out(" CommandRunner compile program.cod -f");
266354
}
267355

268356
public static void main(String[] args) {

0 commit comments

Comments
 (0)