-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandRunner.java
More file actions
293 lines (253 loc) · 10.7 KB
/
Copy pathCommandRunner.java
File metadata and controls
293 lines (253 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// CommandRunner.java
package cod.runner;
import cod.ast.node.*;
import cod.debug.DebugSystem;
import cod.interpreter.Interpreter;
import cod.interpreter.Index;
import cod.ir.IRManager;
import cod.ptac.Artifact;
import cod.ptac.Executor;
import cod.ptac.Options;
public class CommandRunner extends BaseRunner {
private final Interpreter interpreter;
private IRManager irManager;
private final Options ptacOptions;
private static final String NAME = "COMMAND";
public CommandRunner() {
this.interpreter = new Interpreter();
this.ptacOptions = Options.current();
}
@Override
public void run(String[] args) throws Exception {
// Check for compile command first
if (args.length > 0 && "compile".equals(args[0])) {
handleCompileCommand(args);
return;
}
String outputFilename = null;
RunnerConfig config =
processArgs(
args,
null,
new Configuration() {
@Override
public void configure(RunnerConfig config) {
config.withDebugLevel(DebugSystem.Level.INFO);
}
});
// Parse command-line arguments
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("--interpret".equals(arg) || "-i".equals(arg)) {
// Default mode, do nothing
} else if ("-o".equals(arg)) {
if (i + 1 < args.length) {
outputFilename = args[i + 1];
i++;
} else {
outE("Error: -o option requires an output filename.");
}
} else if ("--debug".equals(arg)) {
config.debugLevel = DebugSystem.Level.DEBUG;
} else if ("--trace".equals(arg)) {
config.debugLevel = DebugSystem.Level.TRACE;
} else if ("--quiet".equals(arg)) {
config.debugLevel = DebugSystem.Level.ERROR;
} else if ("--help".equals(arg) || "-h".equals(arg)) {
printHelp();
return;
}
}
if (outputFilename != null) {
config.withOutputFilename(outputFilename);
}
configureDebugSystem(config.debugLevel);
DebugSystem.info(NAME + LOG_TAG, "Starting CommandRunner execution");
DebugSystem.info(NAME + LOG_TAG, "Input file: " + config.inputFilename);
if (config.inputFilename == null || config.inputFilename.isEmpty()) {
throw new RuntimeException(
"No input file specified. Usage: CommandRunner <filename> [options]");
}
DebugSystem.startTimer("exec");
try {
// Set file path on interpreter BEFORE parsing
interpreter.setFilePath(config.inputFilename);
DebugSystem.startTimer("parsing");
Program ast = parse(config.inputFilename, interpreter);
if (ast == null) {
throw new RuntimeException("Parsing failed, AST is null.");
}
DebugSystem.stopTimer("parsing");
DebugSystem.info(NAME + LOG_TAG, "AST built successfully");
// Initialize IR manager
initializeIRManager();
executeInterpretation(ast);
DebugSystem.info(NAME + LOG_TAG, "CommandRunner execution completed");
} finally {
System.out.println("\n-----------------------------");
System.out.println("Execution completed! Duration: " + DebugSystem.stopTimer("exec") + "ms");
}
}
/**
* Handle the "compile" command
*/
private void handleCompileCommand(String[] args) throws Exception {
if (args.length < 2) {
outE("Error: No source file specified for compilation");
outE("Usage: CommandRunner compile <filename>");
return;
}
String sourceFile = args[1];
DebugSystem.info(NAME + LOG_TAG, "Compiling: " + sourceFile);
Interpreter tempInterpreter = new Interpreter();
tempInterpreter.setFilePath(sourceFile);
Program ast = parse(sourceFile, tempInterpreter);
if (ast == null) {
outE("Error: Failed to parse source file");
return;
}
// Initialize IR manager for compilation
String srcMainRoot = tempInterpreter.getImportResolver().getSrcMainRoot();
if (srcMainRoot != null) {
String projectRoot = Index.getProjectRoot();
if (projectRoot != null) {
IRManager bm = new IRManager(projectRoot);
int compiled = 0;
for (Type type : ast.unit.types) {
bm.save(ast.unit.name, type);
String unitPath = IRManager.toUnitPath(ast.unit.name);
System.out.println("Compiled (CodP-TAC artifact): " + type.name + " → <project>.codc/" + unitPath + "/" + type.name + ".codb");
compiled++;
}
System.out.println("Compilation complete: " + compiled + " class(es) compiled");
} else {
outE("Error: Could not determine project root");
}
} else {
outE("Error: Could not find src/main/ structure");
}
}
/**
* Initialize IR manager after project root is known
*/
private void initializeIRManager() {
String srcMainRoot = interpreter.getImportResolver().getSrcMainRoot();
if (srcMainRoot != null) {
String projectRoot = Index.getProjectRoot();
if (projectRoot != null) {
this.irManager = new IRManager(projectRoot);
DebugSystem.debug(NAME + LOG_TAG, "IR manager initialized with root: " + projectRoot);
}
}
}
private void executeInterpretation(Program ast) {
DebugSystem.info(NAME + LOG_TAG, "Starting program interpretation");
boolean hasImports =
ast != null
&& ast.unit != null
&& ast.unit.imports != null
&& ast.unit.imports.imports != null
&& !ast.unit.imports.imports.isEmpty();
if (hasImports) {
DebugSystem.info(NAME + LOG_TAG, "Generating indexes...");
generateIndexes(ast, interpreter);
if (irManager != null) {
DebugSystem.info(NAME + LOG_TAG, "Generating IR...");
compileToBytecode(ast);
}
} else {
DebugSystem.debug(NAME + LOG_TAG, "Skipping index/IR generation (no imports)");
}
if (ptacOptions.isCompileExecuteEnabled() && irManager != null && ast != null && ast.unit != null) {
Type entryType = findMainType(ast);
if (entryType != null) {
Artifact artifact = irManager.loadArtifact(ast.unit.name, entryType.name);
if (artifact == null) {
irManager.save(ast.unit.name, entryType);
artifact = irManager.loadArtifact(ast.unit.name, entryType.name);
}
if (artifact != null) {
DebugSystem.info(NAME + LOG_TAG, "Executing using CodP-TAC executor");
new Executor(ptacOptions).execute(artifact, interpreter);
DebugSystem.info(NAME + LOG_TAG, "Program interpretation completed");
return;
}
}
}
interpreter.run(ast);
DebugSystem.info(NAME + LOG_TAG, "Program interpretation completed");
}
/**
* Compile all classes in the program to .codc IR container entries
*/
private void compileToBytecode(Program ast) {
if (ast == null || ast.unit == null || irManager == null) {
return;
}
String unitName = ast.unit.name;
if (unitName == null || unitName.equals("default")) {
return;
}
int compiled = 0;
for (Type type : ast.unit.types) {
try {
irManager.save(unitName, type);
compiled++;
String unitPath = IRManager.toUnitPath(unitName);
DebugSystem.debug(NAME + LOG_TAG, "Compiled CodP-TAC artifact: " + type.name + " → <project>.codc/" + unitPath + "/" + type.name + ".codb");
} catch (Exception e) {
DebugSystem.warn(NAME + LOG_TAG, "Failed to compile " + type.name + ": " + e.getMessage());
}
}
if (compiled > 0) {
DebugSystem.info(NAME + LOG_TAG, "Compiled " + compiled + " class(es) to IR");
}
}
private void printHelp() {
out("Coderive CommandRunner - Execute Coderive programs");
out("Usage: CommandRunner <filename> [options]");
out(" CommandRunner compile <filename>");
out();
out("Options:");
out(" -i, --interpret Interpret the program (default)");
out(" -o <file> Write output to file");
out(" --debug Enable debug output");
out(" --trace Enable trace-level debugging");
out(" --quiet Only show errors");
out(" -h, --help Show this help message");
out();
out("Commands:");
out(" compile <file> Compile source to bytecode container (.codc with .codb entries)");
out("Environment flags:");
out(" COD_PTAC_MODE=interpreter|compile-only|compile-execute");
out(" COD_PTAC_FALLBACK=true|false");
out();
out("Examples:");
out(" CommandRunner program.cod");
out(" CommandRunner program.cod -o output.txt");
out(" CommandRunner compile program.cod");
}
public static void main(String[] args) {
try {
CommandRunner runner = new CommandRunner();
runner.run(args);
} catch (Exception e) {
DebugSystem.error(NAME + LOG_TAG, "Execution failed: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
private Type findMainType(Program ast) {
if (ast == null || ast.unit == null || ast.unit.types == null) return null;
for (Type type : ast.unit.types) {
if (type == null || type.methods == null) continue;
for (Method method : type.methods) {
if (method != null && "main".equals(method.methodName)
&& (method.parameters == null || method.parameters.isEmpty())) {
return type;
}
}
}
return !ast.unit.types.isEmpty() ? ast.unit.types.get(0) : null;
}
}