MD5 校验值:195005882709ac21163d7a1b97aeec73
repl.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package kawa; import gnu.bytecode.ClassType; import gnu.expr.ApplicationMainSupport; import gnu.expr.Compilation; import gnu.expr.Language; import gnu.expr.ModuleBody; import gnu.expr.ModuleExp; import gnu.expr.ModuleInfo; import gnu.expr.ModuleManager; import gnu.kawa.servlet.HttpRequestContext; import gnu.lists.FString; import gnu.mapping.CharArrayInPort; import gnu.mapping.Environment; import gnu.mapping.InPort; import gnu.mapping.OutPort; import gnu.mapping.Procedure0or1; import gnu.mapping.Values; import gnu.text.Options; import gnu.text.SourceMessages; import gnu.text.SyntaxException; import gnu.text.WriterManager; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import java.io.Serializable; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Vector; import org.apache.commons.lang3.StringUtils; public class repl extends Procedure0or1 { public static String homeDirectory; public static boolean noConsole; static Language previousLanguage; Language language; public static String compilationTopname = null; static int defaultParseOptions = 72; static boolean shutdownRegistered = WriterManager.instance.registerShutdownHook(); public repl(Language language) { this.language = language; } static void bad_option(String str) { System.err.println("kawa: bad option '" + str + "'"); printOptions(System.err); System.exit(-1); } public static void checkInitFile() { Serializable serializable; if (homeDirectory == null) { File file = null; homeDirectory = System.getProperty("user.home"); if (homeDirectory != null) { serializable = new FString(homeDirectory); file = new File(homeDirectory, "/".equals(System.getProperty("file.separator")) ? ".kawarc.scm" : "kawarc.scm"); } else { serializable = Boolean.FALSE; } Environment.getCurrent().put("home-directory", (Object) serializable); if (file == null || !file.exists() || Shell.runFileOrClass(file.getPath(), true, 0)) { return; } System.exit(-1); } } public static void compileFiles(String[] strArr, int i, int i2) { ModuleManager moduleManager = ModuleManager.getInstance(); Compilation[] compilationArr = new Compilation[i2 - i]; ModuleInfo[] moduleInfoArr = new ModuleInfo[i2 - i]; SourceMessages sourceMessages = new SourceMessages(); for (int i3 = i; i3 < i2; i3++) { String str = strArr[i3]; getLanguageFromFilenameExtension(str); try { try { Compilation parse = Language.getDefaultLanguage().parse(InPort.openFile(str), sourceMessages, defaultParseOptions); if (compilationTopname != null) { ClassType classType = new ClassType(Compilation.mangleNameIfNeeded(compilationTopname)); ModuleExp module = parse.getModule(); module.setType(classType); module.setName(compilationTopname); parse.mainClass = classType; } moduleInfoArr[i3 - i] = moduleManager.find(parse); compilationArr[i3 - i] = parse; } catch (FileNotFoundException e) { System.err.println(e); System.exit(-1); break; } } catch (Throwable th) { if (!(th instanceof SyntaxException) || ((SyntaxException) th).getMessages() != sourceMessages) { internalError(th, null, str); } } if (sourceMessages.seenErrorsOrWarnings()) { System.err.println("(compiling " + str + ')'); if (sourceMessages.checkErrors(System.err, 20)) { System.exit(1); } } } for (int i4 = i; i4 < i2; i4++) { String str2 = strArr[i4]; Compilation compilation = compilationArr[i4 - i]; try { System.err.println("(compiling " + str2 + " to " + compilation.mainClass.getName() + ')'); moduleInfoArr[i4 - i].loadByStages(14); boolean seenErrors = sourceMessages.seenErrors(); sourceMessages.checkErrors(System.err, 50); if (seenErrors) { System.exit(-1); } compilationArr[i4 - i] = compilation; boolean seenErrors2 = sourceMessages.seenErrors(); sourceMessages.checkErrors(System.err, 50); if (seenErrors2) { System.exit(-1); } } catch (Throwable th2) { internalError(th2, compilation, str2); } } } public static void getLanguage() { if (previousLanguage == null) { previousLanguage = Language.getInstance(null); Language.setDefaults(previousLanguage); } } public static void getLanguageFromFilenameExtension(String str) { if (previousLanguage == null) { previousLanguage = Language.getInstanceFromFilenameExtension(str); if (previousLanguage != null) { Language.setDefaults(previousLanguage); return; } } getLanguage(); } static void internalError(Throwable th, Compilation compilation, Object obj) { StringBuffer stringBuffer = new StringBuffer(); if (compilation != null) { String fileName = compilation.getFileName(); int lineNumber = compilation.getLineNumber(); if (fileName != null && lineNumber > 0) { stringBuffer.append(fileName); stringBuffer.append(':'); stringBuffer.append(lineNumber); stringBuffer.append(": "); } } stringBuffer.append("internal error while compiling "); stringBuffer.append(obj); System.err.println(stringBuffer.toString()); th.printStackTrace(System.err); System.exit(-1); } public static void main(String[] strArr) { try { int processArgs = processArgs(strArr, 0, strArr.length); if (processArgs < 0) { return; } if (processArgs < strArr.length) { String str = strArr[processArgs]; getLanguageFromFilenameExtension(str); setArgs(strArr, processArgs + 1); checkInitFile(); Shell.runFileOrClass(str, false, 0); } else { getLanguage(); setArgs(strArr, processArgs); checkInitFile(); if (shouldUseGuiConsole()) { startGuiConsole(); } else if (!Shell.run(Language.getDefaultLanguage(), Environment.getCurrent())) { System.exit(-1); } } if (!shutdownRegistered) { OutPort.runCleanups(); } ModuleBody.exitDecrement(); } finally { if (!shutdownRegistered) { OutPort.runCleanups(); } ModuleBody.exitDecrement(); } } public static void printOption(PrintStream printStream, String str, String str2) { printStream.print(StringUtils.SPACE); printStream.print(str); int length = str.length() + 1; for (int i = 0; i < 30 - length; i++) { printStream.print(StringUtils.SPACE); } printStream.print(StringUtils.SPACE); printStream.println(str2); } public static void printOptions(PrintStream printStream) { printStream.println("Usage: [java kawa.repl | kawa] [options ...]"); printStream.println(); printStream.println(" Generic options:"); printOption(printStream, "--help", "Show help about options"); printOption(printStream, "--author", "Show author information"); printOption(printStream, "--version", "Show version information"); printStream.println(); printStream.println(" Options"); printOption(printStream, "-e <expr>", "Evaluate expression <expr>"); printOption(printStream, "-c <expr>", "Same as -e, but make sure ~/.kawarc.scm is run first"); printOption(printStream, "-f <filename>", "File to interpret"); printOption(printStream, "-s| --", "Start reading commands interactively from console"); printOption(printStream, "-w", "Launch the interpreter in a GUI window"); printOption(printStream, "--server <port>", "Start a server accepting telnet connections on <port>"); printOption(printStream, "--debug-dump-zip", "Compiled interactive expressions to a zip archive"); printOption(printStream, "--debug-print-expr", "Print generated internal expressions"); printOption(printStream, "--debug-print-final-expr", "Print expression after any optimizations"); printOption(printStream, "--debug-error-prints-stack-trace", "Print stack trace with errors"); printOption(printStream, "--debug-warning-prints-stack-trace", "Print stack trace with warnings"); printOption(printStream, "--[no-]full-tailcalls", "(Don't) use full tail-calls"); printOption(printStream, "-C <filename> ...", "Compile named files to Java class files"); printOption(printStream, "--output-format <format>", "Use <format> when printing top-level output"); printOption(printStream, "--<language>", "Select source language, one of:"); String[][] languages = Language.getLanguages(); for (int i = 0; i < languages.length; i++) { printStream.print(" "); String[] strArr = languages[i]; int length = strArr.length - 1; for (int i2 = 0; i2 < length; i2++) { printStream.print(strArr[i2] + StringUtils.SPACE); } if (i == 0) { printStream.print("[default]"); } printStream.println(); } printStream.println(" Compilation options, must be specified before -C"); printOption(printStream, "-d <dirname>", "Directory to place .class files in"); printOption(printStream, "-P <prefix>", "Prefix to prepand to class names"); printOption(printStream, "-T <topname>", "name to give to top-level class"); printOption(printStream, "--main", "Generate an application, with a main method"); printOption(printStream, "--applet", "Generate an applet"); printOption(printStream, "--servlet", "Generate a servlet"); printOption(printStream, "--module-static", "Top-level definitions are by default static"); ArrayList<String> keys = Compilation.options.keys(); for (int i3 = 0; i3 < keys.size(); i3++) { String str = keys.get(i3); printOption(printStream, "--" + str, Compilation.options.getDoc(str)); } printStream.println(); printStream.println("For more information go to: http://www.gnu.org/software/kawa/"); } public static int processArgs(String[] strArr, int i, int i2) { int i3; String substring; int i4; int i5 = i; boolean z = false; while (i5 < i2) { String str = strArr[i5]; if (str.equals("-c") || str.equals("-e")) { i5++; if (i5 == i2) { bad_option(str); } getLanguage(); setArgs(strArr, i5 + 1); if (str.equals("-c")) { checkInitFile(); } Language defaultLanguage = Language.getDefaultLanguage(); SourceMessages sourceMessages = new SourceMessages(); Throwable run = Shell.run(defaultLanguage, Environment.getCurrent(), new CharArrayInPort(strArr[i5]), OutPort.outDefault(), (OutPort) null, sourceMessages); if (run != null) { Shell.printError(run, sourceMessages, OutPort.errDefault()); System.exit(-1); } z = true; } else if (str.equals("-f")) { i5++; if (i5 == i2) { bad_option(str); } String str2 = strArr[i5]; getLanguageFromFilenameExtension(str2); setArgs(strArr, i5 + 1); checkInitFile(); if (!Shell.runFileOrClass(str2, true, 0)) { System.exit(-1); } z = true; } else { if (str.startsWith("--script")) { String substring2 = str.substring(8); int i6 = i5 + 1; int i7 = 0; if (substring2.length() > 0) { try { i7 = Integer.parseInt(substring2); } catch (Throwable th) { i6 = i2; } } if (i6 == i2) { bad_option(str); } String str3 = strArr[i6]; getLanguageFromFilenameExtension(str3); setArgs(strArr, i6 + 1); checkInitFile(); if (!Shell.runFileOrClass(str3, true, i7)) { System.exit(-1); } return -1; } if (str.equals("\\")) { int i8 = i5 + 1; if (i8 == i2) { bad_option(str); } String str4 = strArr[i8]; SourceMessages sourceMessages2 = new SourceMessages(); try { BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(str4)); int read = bufferedInputStream.read(); if (read == 35) { StringBuffer stringBuffer = new StringBuffer(100); Vector vector = new Vector(10); int i9 = 0; while (read != 10 && read != 13 && read >= 0) { read = bufferedInputStream.read(); } while (true) { int read2 = bufferedInputStream.read(); if (read2 < 0) { System.err.println("unexpected end-of-file processing argument line for: '" + str4 + '\''); System.exit(-1); } if (i9 != 0) { if (i9 == 92) { i9 = 0; } else if (read2 == i9) { i9 = 0; } stringBuffer.append((char) read2); } else if (read2 == 92 || read2 == 39 || read2 == 34) { i9 = read2; } else { if (read2 == 10 || read2 == 13) { break; } if (read2 != 32 && read2 != 9) { stringBuffer.append((char) read2); } else if (stringBuffer.length() > 0) { vector.addElement(stringBuffer.toString()); stringBuffer.setLength(0); } } } if (stringBuffer.length() > 0) { vector.addElement(stringBuffer.toString()); } int size = vector.size(); if (size > 0) { String[] strArr2 = new String[size]; vector.copyInto(strArr2); int processArgs = processArgs(strArr2, 0, size); if (processArgs >= 0 && processArgs < size) { System.err.println("" + (size - processArgs) + " unused meta args"); } } } getLanguageFromFilenameExtension(str4); InPort openFile = InPort.openFile(bufferedInputStream, str4); setArgs(strArr, i8 + 1); checkInitFile(); OutPort errDefault = OutPort.errDefault(); Throwable run2 = Shell.run(Language.getDefaultLanguage(), Environment.getCurrent(), openFile, OutPort.outDefault(), (OutPort) null, sourceMessages2); sourceMessages2.printAll(errDefault, 20); if (run2 != null) { if ((run2 instanceof SyntaxException) && ((SyntaxException) run2).getMessages() == sourceMessages2) { System.exit(1); } throw run2; } } catch (Throwable th2) { Shell.printError(th2, sourceMessages2, OutPort.errDefault()); System.exit(1); } return -1; } if (str.equals("-s") || str.equals("--")) { getLanguage(); setArgs(strArr, i5 + 1); checkInitFile(); Shell.run(Language.getDefaultLanguage(), Environment.getCurrent()); return -1; } if (str.equals("-w")) { i5++; getLanguage(); setArgs(strArr, i5); checkInitFile(); startGuiConsole(); z = true; } else if (str.equals("-d")) { i5++; if (i5 == i2) { bad_option(str); } ModuleManager.getInstance().setCompilationDirectory(strArr[i5]); } else if (str.equals("--target") || str.equals("target")) { i5++; if (i5 == i2) { bad_option(str); } String str5 = strArr[i5]; if (str5.equals("7")) { Compilation.defaultClassFileVersion = ClassType.JDK_1_7_VERSION; } if (str5.equals("6") || str5.equals("1.6")) { Compilation.defaultClassFileVersion = ClassType.JDK_1_6_VERSION; } else if (str5.equals("5") || str5.equals("1.5")) { Compilation.defaultClassFileVersion = ClassType.JDK_1_5_VERSION; } else if (str5.equals("1.4")) { Compilation.defaultClassFileVersion = ClassType.JDK_1_4_VERSION; } else if (str5.equals("1.3")) { Compilation.defaultClassFileVersion = ClassType.JDK_1_3_VERSION; } else if (str5.equals("1.2")) { Compilation.defaultClassFileVersion = ClassType.JDK_1_2_VERSION; } else if (str5.equals("1.1")) { Compilation.defaultClassFileVersion = ClassType.JDK_1_1_VERSION; } else { bad_option(str5); } } else if (str.equals("-P")) { i5++; if (i5 == i2) { bad_option(str); } Compilation.classPrefixDefault = strArr[i5]; } else if (str.equals("-T")) { i5++; if (i5 == i2) { bad_option(str); } compilationTopname = strArr[i5]; } else { if (str.equals("-C")) { int i10 = i5 + 1; if (i10 == i2) { bad_option(str); } compileFiles(strArr, i10, i2); return -1; } if (str.equals("--output-format") || str.equals("--format")) { i5++; if (i5 == i2) { bad_option(str); } Shell.setDefaultFormat(strArr[i5]); } else if (str.equals("--connect")) { i5++; if (i5 == i2) { bad_option(str); } if (strArr[i5].equals("-")) { i4 = 0; } else { try { i4 = Integer.parseInt(strArr[i5]); } catch (NumberFormatException e) { bad_option("--connect port#"); i4 = -1; } } try { Telnet telnet = new Telnet(new Socket(InetAddress.getByName(null), i4), true); TelnetInputStream inputStream = telnet.getInputStream(); PrintStream printStream = new PrintStream((OutputStream) telnet.getOutputStream(), true); System.setIn(inputStream); System.setOut(printStream); System.setErr(printStream); } catch (IOException e2) { e2.printStackTrace(System.err); throw new Error(e2.toString()); } } else if (str.equals("--server")) { getLanguage(); int i11 = i5 + 1; if (i11 == i2) { bad_option(str); } if (strArr[i11].equals("-")) { i3 = 0; } else { try { i3 = Integer.parseInt(strArr[i11]); } catch (NumberFormatException e3) { bad_option("--server port#"); i3 = -1; } } try { ServerSocket serverSocket = new ServerSocket(i3); System.err.println("Listening on port " + serverSocket.getLocalPort()); while (true) { System.err.print("waiting ... "); System.err.flush(); Socket accept = serverSocket.accept(); System.err.println("got connection from " + accept.getInetAddress() + " port:" + accept.getPort()); TelnetRepl.serve(Language.getDefaultLanguage(), accept); } } catch (IOException e4) { throw new Error(e4.toString()); } } else if (str.equals("--http-auto-handler")) { i5 += 2; if (i5 >= i2) { bad_option(str); } System.err.println("kawa: HttpServer classes not found"); System.exit(-1); } else if (str.equals("--http-start")) { i5++; if (i5 >= i2) { bad_option("missing httpd port argument"); } System.err.println("kawa: HttpServer classes not found"); System.exit(-1); } else if (str.equals("--main")) { Compilation.generateMainDefault = true; } else if (str.equals("--applet")) { defaultParseOptions |= 16; } else if (str.equals("--servlet")) { defaultParseOptions |= 32; HttpRequestContext.importServletDefinitions = 2; } else if (str.equals("--debug-dump-zip")) { ModuleExp.dumpZipPrefix = "kawa-zip-dump-"; } else if (str.equals("--debug-print-expr")) { Compilation.debugPrintExpr = true; } else if (str.equals("--debug-print-final-expr")) { Compilation.debugPrintFinalExpr = true; } else if (str.equals("--debug-error-prints-stack-trace")) { SourceMessages.debugStackTraceOnError = true; } else if (str.equals("--debug-warning-prints-stack-trace")) { SourceMessages.debugStackTraceOnWarning = true; } else if (str.equals("--module-nonstatic") || str.equals("--no-module-static")) { Compilation.moduleStatic = -1; } else if (str.equals("--module-static")) { Compilation.moduleStatic = 1; } else if (str.equals("--module-static-run")) { Compilation.moduleStatic = 2; } else if (str.equals("--no-inline") || str.equals("--inline=none")) { Compilation.inlineOk = false; } else if (str.equals("--no-console")) { noConsole = true; } else if (str.equals("--inline")) { Compilation.inlineOk = true; } else if (str.equals("--cps")) { Compilation.defaultCallConvention = 4; } else if (str.equals("--full-tailcalls")) { Compilation.defaultCallConvention = 3; } else if (str.equals("--no-full-tailcalls")) { Compilation.defaultCallConvention = 1; } else if (str.equals("--pedantic")) { Language.requirePedantic = true; } else if (str.equals("--help")) { printOptions(System.out); System.exit(0); } else if (str.equals("--author")) { System.out.println("Per Bothner <per@bothner.com>"); System.exit(0); } else if (str.equals("--version")) { System.out.print("Kawa "); System.out.print(Version.getVersion()); System.out.println(); System.out.println("Copyright (C) 2009 Per Bothner"); z = true; } else if (str.length() > 0 && str.charAt(0) == '-') { String str6 = str; if (str6.length() > 2 && str6.charAt(0) == '-') { str6 = str6.substring(str6.charAt(1) == '-' ? 2 : 1); } Language language = Language.getInstance(str6); if (language != null) { if (previousLanguage == null) { Language.setDefaults(language); } else { Language.setCurrentLanguage(language); } previousLanguage = language; } else { int indexOf = str6.indexOf("="); if (indexOf < 0) { substring = null; } else { substring = str6.substring(indexOf + 1); str6 = str6.substring(0, indexOf); } boolean z2 = str6.startsWith("no-") && str6.length() > 3; if (substring == null && z2) { substring = "no"; str6 = str6.substring(3); } String str7 = Compilation.options.set(str6, substring); if (str7 != null) { if (z2 && str7 == Options.UNKNOWN) { str7 = "both '--no-' prefix and '=" + substring + "' specified"; } if (str7 == Options.UNKNOWN) { bad_option(str); } else { System.err.println("kawa: bad option '" + str + "': " + str7); System.exit(-1); } } } } else if (!ApplicationMainSupport.processSetProperty(str)) { break; } } } i5++; } return z ? -1 : i5; } public static void setArgs(String[] strArr, int i) { ApplicationMainSupport.setArgs(strArr, i); } public static boolean shouldUseGuiConsole() { if (noConsole) { return true; } try { if (Class.forName("java.lang.System").getMethod("console", new Class[0]).invoke(new Object[0], new Object[0]) == null) { return true; } } catch (Throwable th) { } return false; } private static void startGuiConsole() { try { Class.forName("kawa.GuiConsole").newInstance(); } catch (Exception e) { System.err.println("failed to create Kawa window: " + e); System.exit(-1); } } @Override public Object apply0() { Shell.run(this.language, Environment.getCurrent()); return Values.empty; } @Override public Object apply1(Object obj) { Shell.run(this.language, (Environment) obj); return Values.empty; } }