-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMain.java
More file actions
83 lines (69 loc) · 2.96 KB
/
Main.java
File metadata and controls
83 lines (69 loc) · 2.96 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
package org.perlonjava.app.cli;
import org.perlonjava.app.scriptengine.PerlLanguageProvider;
import org.perlonjava.runtime.runtimetypes.ErrorMessageUtil;
import org.perlonjava.runtime.runtimetypes.GlobalVariable;
import org.perlonjava.runtime.runtimetypes.RuntimeScalar;
import java.util.Locale;
/**
* The Main class serves as the entry point for the Perl-to-Java bytecode compiler and runtime
* evaluator. It accepts the command-line arguments, parses Perl code, generates corresponding Java bytecode using ASM, and executes the
* generated bytecode.
*/
public class Main {
static {
// Set default locale to US (uses dot as decimal separator)
Locale.setDefault(Locale.US);
}
/**
* The main method initializes the compilation and execution process.
*
* @param args Command-line arguments.
*/
public static void main(String[] args) {
CompilerOptions parsedArgs = ArgumentParser.parseArguments(args);
if (parsedArgs.code == null) {
System.err.println("No code provided. Use -e <code> or specify a filename.");
System.exit(1);
}
try {
PerlLanguageProvider.executePerlCode(parsedArgs, true);
if (parsedArgs.compileOnly) {
System.out.println(parsedArgs.fileName + " syntax OK");
}
// Match system perl behavior:
// - Running external commands sets $? (usually as a wait status like 0x0200),
// but does NOT by itself make the perl interpreter exit non-zero.
// - Test frameworks (Test2/Test::More) set $? to a small integer (1..255)
// in an END block to request a non-zero exit status.
RuntimeScalar childStatus = GlobalVariable.getGlobalVariable("main::?");
int rawChildStatus = childStatus.getInt();
if (rawChildStatus > 0 && rawChildStatus <= 255) {
System.exit(rawChildStatus);
}
} catch (Throwable t) {
if (parsedArgs.debugEnabled) {
// Print full JVM stack
t.printStackTrace();
System.out.println();
}
String errorMessage = ErrorMessageUtil.stringifyException(t);
System.err.print(errorMessage);
// Match system perl behavior for unhandled die:
// Prefer $! (errno) if non-zero, else prefer ($? >> 8), else 255.
int exitCode = 255;
try {
int bang = GlobalVariable.getGlobalVariable("main::!").getInt();
int query = GlobalVariable.getGlobalVariable("main::?").getInt();
int queryExit = (query >> 8);
if (bang != 0) {
exitCode = bang;
} else if (queryExit != 0) {
exitCode = queryExit;
}
} catch (Throwable ignored) {
// Last resort below
}
System.exit(exitCode);
}
}
}