-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestRunner.java
More file actions
391 lines (336 loc) · 15.3 KB
/
Copy pathTestRunner.java
File metadata and controls
391 lines (336 loc) · 15.3 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package cod.runner;
import cod.ast.node.*;
import cod.debug.DebugSystem;
import cod.interpreter.Interpreter;
import cod.debug.Linter;
import cod.interpreter.Index;
import cod.ir.IRManager;
import cod.ptac.Artifact;
import cod.ptac.Executor;
import cod.ptac.Options;
import java.io.File;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;
/*
This class is used by the language developer for testing. Rigorously tested in an Android phone.
For production use CommandRunner.
*/
public class TestRunner extends BaseRunner {
private final String TEST_FILE = "helloworld/HelloWorldModule";
private final String androidPath = "/storage/emulated/0";
private final String definedFilePath =
"/JavaNIDE/Programming-Language/Coderive/app/src/main/cod/demo/src/main/test/" + TEST_FILE + ".cod";
private final String consoleRelativePath =
"src/main/cod/demo/src/main/test/" + TEST_FILE + ".cod";
private final String NAME = "TEST";
private final DebugSystem.Level level = DebugSystem.Level.OFF;
private final Interpreter interpreter;
private IRManager irManager;
private final Options ptacOptions;
public TestRunner() {
this.interpreter = new Interpreter();
this.ptacOptions = Options.current();
}
@Override
public void run(String[] args) throws Exception {
try (Scanner scanner = newSystemInScanner()) {
String inputFilename = getInputFilename(args, scanner);
// Validate file path is under src/main/
validateSourceFilePath(inputFilename);
RunnerConfig config =
processArgs(
args,
inputFilename,
new Configuration() {
@Override
public void configure(RunnerConfig config) {
// Enable DEBUG to see index generation logs
config.withDebugLevel(level);
}
});
configureDebugSystem(config.debugLevel);
DebugSystem.info(
NAME + LOG_TAG,
"Starting interpreter execution...\nInput file: " + config.inputFilename);
DebugSystem.startTimer("exec");
// Set file path on interpreter BEFORE parsing
interpreter.setFilePath(config.inputFilename);
// Parse with interpreter for unit validation
Program ast = parse(config.inputFilename, interpreter);
// Initialize IR manager after parsing (project root is now known)
initializeIRManager();
// Perform linting and check completion status
boolean lintingCompleted = performLinting(ast);
// Ensure all output streams are fully flushed before proceeding
flushStreams();
// Only run interpreter if linting completed successfully
if (lintingCompleted) {
executeWithManualInterpreter(ast);
} else {
DebugSystem.error(
NAME + LOG_TAG,
"Linting did not complete successfully. Interpreter execution aborted.");
throw new RuntimeException("Linting phase failed to complete");
}
System.out.println("\n" + "-----------------------------");
System.out.println("Execution completed! Duration: " + DebugSystem.stopTimer("exec") + "ms");
}
}
private Scanner newSystemInScanner() {
InputStream nonClosingIn =
new FilterInputStream(System.in) {
@Override
public void close() throws IOException {
// Keep System.in available for interpreter execution.
}
};
return new Scanner(nonClosingIn);
}
/**
* 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);
}
}
}
// Validate source file is under src/main/
private void validateSourceFilePath(String filePath) {
String normalized = filePath.replace('\\', '/');
if (!normalized.contains("/src/main/")) {
DebugSystem.warn(
NAME + LOG_TAG,
"Source file not under src/main/: "
+ filePath
+ "\nUnit declaration validation may fail."
+ "\nExpected structure: src/main/<unit>/<file>.cod");
}
}
private String getInputFilename(String[] args, Scanner scanner) {
// First, check for input file in args
String inputFileFromArgs = null;
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (!arg.startsWith("-") && !arg.equals("-o")) {
inputFileFromArgs = arg;
break;
}
}
if (inputFileFromArgs != null) {
return inputFileFromArgs;
}
String defaultFilename = androidPath + definedFilePath;
String consoleFilename =
new File(System.getProperty("user.dir"), consoleRelativePath).getAbsolutePath();
File consoleFile = new File(consoleFilename);
// Prefer local console default if it exists
if (consoleFile.exists() && consoleFile.isFile()) {
out("Using console default file: " + consoleFilename);
return consoleFilename;
}
out("Enter file path or press Enter for default [" + defaultFilename + "]");
out("Or type 'console' to use local default [" + consoleFilename + "]");
System.out.print("> ");
String userInput = scanner.nextLine().trim();
if (userInput.isEmpty()) {
System.out.println("Using default file: " + defaultFilename);
return defaultFilename;
} else if ("console".equalsIgnoreCase(userInput)) {
System.out.println("Using console file: " + consoleFilename);
return consoleFilename;
} else {
out("Using user provided file: " + userInput);
return userInput;
}
}
private void executeWithManualInterpreter(Program ast) {
DebugSystem.info(NAME + LOG_TAG, "Running Interpreter");
// Generate indexes before execution for O(1) import resolution
DebugSystem.info(NAME + LOG_TAG, "Generating indexes...");
generateIndexes(ast, interpreter);
// Compile to IR after index generation
if (irManager != null && ast != null && ast.unit != null) {
DebugSystem.info(NAME + LOG_TAG, "Generating IR...");
compileToBytecode(ast);
}
// ========== DEBUG: Check if index was generated ==========
DebugSystem.info(NAME + LOG_TAG, "Checking for generated indexes...");
// Get the unit name from the AST
String unitName = null;
if (ast != null && ast.unit != null) {
unitName = ast.unit.name;
DebugSystem.info(NAME + LOG_TAG, "Main unit name: " + unitName);
}
// Try to load index for the main unit
if (unitName != null && !unitName.equals("default")) {
Index idx = Index.load(unitName);
if (idx != null) {
DebugSystem.info(NAME + LOG_TAG, "✓ Index found for unit '" + unitName +
"' with " + idx.size() + " classes");
if (DebugSystem.getLevel().compareTo(DebugSystem.Level.TRACE) >= 0) {
DebugSystem.trace(NAME + LOG_TAG, " Classes: " + idx.getClassNames());
}
} else {
DebugSystem.warn(NAME + LOG_TAG, "✗ No index found for unit '" + unitName + "'");
}
}
// Also check for imported units
if (ast != null && ast.unit != null && ast.unit.imports != null) {
for (String importName : ast.unit.imports.imports) {
int lastDot = importName.lastIndexOf('.');
if (lastDot > 0) {
String importedUnit = importName.substring(0, lastDot);
Index importedIdx = Index.load(importedUnit);
if (importedIdx != null) {
DebugSystem.info(NAME + LOG_TAG, "✓ Index found for imported unit '" +
importedUnit + "' with " + importedIdx.size() + " classes");
} else {
DebugSystem.warn(NAME + LOG_TAG, "✗ No index found for imported unit '" +
importedUnit + "'");
}
}
}
}
// Check src/idx directory using project root
String projectRoot = Index.getProjectRoot();
if (projectRoot != null) {
File idxDir = new File(projectRoot + File.separator + "src" + File.separator + "idx");
if (idxDir.exists() && idxDir.isDirectory()) {
DebugSystem.info(NAME + LOG_TAG, "Index directory exists at: " + idxDir.getAbsolutePath());
String[] files = idxDir.list();
if (files != null && files.length > 0) {
DebugSystem.info(NAME + LOG_TAG, " Contents: " + java.util.Arrays.toString(files));
} else {
DebugSystem.warn(NAME + LOG_TAG, " Index directory is empty");
}
} else {
DebugSystem.info(NAME + LOG_TAG, "Index directory will be created at: " + idxDir.getAbsolutePath());
}
// Check bytecode directory
File binDir = new File(projectRoot + File.separator + "src" + File.separator + "bin");
if (binDir.exists() && binDir.isDirectory()) {
DebugSystem.info(NAME + LOG_TAG, "Bytecode directory exists at: " + binDir.getAbsolutePath());
String[] files = binDir.list();
if (files != null && files.length > 0) {
DebugSystem.info(NAME + LOG_TAG, " Contents: " + java.util.Arrays.toString(files));
}
} else {
DebugSystem.info(NAME + LOG_TAG, "Bytecode directory will be created at: " + binDir.getAbsolutePath());
}
} else {
DebugSystem.warn(NAME + LOG_TAG, "Project root not set, cannot verify directories");
DebugSystem.warn(NAME + LOG_TAG, " Current working directory: " + new File(".").getAbsolutePath());
}
DebugSystem.startTimer("interpretation");
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);
double duration = DebugSystem.stopTimer("interpretation");
DebugSystem.info(NAME + LOG_TAG, String.format("Interpretation completed in %.3f ms", duration));
return;
}
}
}
interpreter.run(ast);
double duration = DebugSystem.stopTimer("interpretation");
DebugSystem.info(NAME + LOG_TAG, String.format("Interpretation completed in %.3f ms", duration));
}
/**
* 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 boolean performLinting(Program ast) {
DebugSystem.startTimer("linting");
Linter linter = new Linter();
List<String> warnings = linter.lint(ast);
DebugSystem.stopTimer("linting");
// Print warnings immediately and synchronously
Linter.WarningUtils.printWarnings(warnings);
boolean lintingCompleted = linter.isCompleted();
int warningCount = linter.getWarningCount();
DebugSystem.info(
NAME + LOG_TAG,
"Linting completed: " + lintingCompleted + " with " + warningCount + " warning(s)");
// Force flush all streams after linting output
flushStreams();
return lintingCompleted;
}
/** Forces all output streams to flush completely to ensure proper output ordering */
private void flushStreams() {
try {
System.out.flush();
System.err.flush();
} catch (Exception e) {
// Ignore interruption, just continue
}
}
public static void main(String[] args) {
try {
new TestRunner().run(args);
} catch (Exception e) {
e.printStackTrace();
outE();
outE("Usage: TestRunner [filename] [options]");
outE("Options:");
outE(" -o <file> Output file");
outE("Environment flags:");
outE(" COD_PTAC_MODE=interpreter|compile-only|compile-execute");
outE(" COD_PTAC_FALLBACK=true|false");
outE();
outE("Example:");
outE(" TestRunner myprogram.cod");
outE();
}
}
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;
}
}