[Nice-commit] Nice/src/nice/tools/compiler/console main.nice,NONE,1.1 listener.nice,NONE,1.1
Brought to you by:
bonniot
From: <bo...@us...> - 2003-06-11 22:26:59
|
Update of /cvsroot/nice/Nice/src/nice/tools/compiler/console In directory sc8-pr-cvs1:/tmp/cvs-serv22581/src/nice/tools/compiler/console Added Files: main.nice listener.nice Log Message: Added the compilation listener interface. Console output is factored out in package nice.tools.compiler.console --- NEW FILE: main.nice --- /**************************************************************************/ /* N I C E */ /* A high-level object-oriented research language */ /* (c) Daniel Bonniot 2003 */ /* */ /* This program is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* */ /**************************************************************************/ package nice.tools.compiler.console; /** Nice compiler main program. Exit code table: 0 : Normal exit (compilation sucessful, version message) 1 : Abnormal termination (bug in the compiler) 2 : Error reported (file missing, type error, ...) 3 : Warning reported Changes to this table should be reflected in function <code>nice-compilation-exit</code> in emacs mode file <tt>nice-mode.el</tt>. @author Daniel Bonniot (bo...@us...) */ import nice.tools.compiler; import bossa.modules; import nice.getopt; import nice.doc; /** @return a status code describing if the compilation was successful. The codes are found in OutputMessages (OK, WARNING, ERROR, BUG). */ int compile(String[] args) { nice.tools.util.Chronometer chrono = notNull(nice.tools.util.Chronometer.make("Total")); chrono.start(); boolean memInfo = false, timeInfo = false; String mainPackage; ?String nativeProgram = null; ?String nativeCompiler = null; ?String classpath = null; boolean editorMode = false; ConsoleOutput output = new ConsoleOutput(); Compilation compilation = new Compilation(listener: output); Program prg = new Program (name: "nicec", longName: "The Nice Compiler", shortDescription: "compiler for the Nice programming language", author: "Daniel Bonniot <bo...@us...>, <Dan...@in...>", seeAlso: "http://nice.sourceforge.net the Nice Home Page\n", manualSection: "1", arguments: "package", options: cast(null)); prg.options = [ option("sourcepath", "Search path for source packages\nPATH is a list of directories and .jar archives", "path", String path => { compilation.sourcePath = path; }), option(letter: 'd', "destination", "Destination directory for compiled packages", "dir", String dir => { compilation.destinationDir = dir; }), option("classpath", "Search path for compiled packages and libraries\nPATH is a list of directories and .jar archives", "path", String path => { compilation.packagePath = path; }), option(letter: 'a', "jar", "Compile to archive\nYou can then run the program with 'java -jar FILE'", "file", String output => { compilation.output = output; }), option(letter: 'o', "output", "Generate native executable", "file", String output => { compilation.output = output + ".jar"; nativeProgram = output; }), option(letter: 'r', "recompile", "Force recompilation of package", () => { compilation.recompileCommandLine = true; }), option(letter: 'R', "recompile-all", "Force recompilation of all dependant packages", () => { compilation.recompileAll = true; }), option(letter: 'c', "compile", "Compile packages but do not link them", () => { compilation.skipLink = true; }), option("strict", "Type-check uses of java classes more stricly", () => { compilation.strictJavaTypes = true; }), // PRIVATE OPTIONS // This options are only usefull for compiling the compiler itself, // so they should not be advertised. option("exclude-runtime", visible: false, "Avoid inclusion of the runtime in the archive", () => { compilation.excludeRuntime = true; }), option("runtime", visible: false, "Location of nice.jar", "file", String file => { compilation.runtimeFile = file; }), option("native-compiler", visible: false, "Location of the native compiler binary (gcj)", "file", String file => { nativeCompiler = file; }), option(letter: 'h', "help", "Print help message and exit", () => help(prg)), option("editor", "Tell nicec that it is called by an editor.", () => { editorMode = true; }), option("man", "Print man page to stdout", () => man(prg)), option("version", "Print version info and exit", () => version()), option("usage", "Print usage information and exit", () => usage(prg)), option("memory", "Print memory usage information after compilation", () => { memInfo = true; }), option("benchmark", "Print time usage information after compilation", () => { timeInfo = true; }) ]; List<String> rest = parse(prg, args); if (rest.size() != 1 || rest[0].length() == 0) { if (rest.size() > 1) println("Supply only one package on the command line.\n" + "nicec will automatically find dependancies."); usage(prg, ERROR); } mainPackage = rest[0]; /* For convenience of command-line tools (shells) with completions, we treat a directory name as the corresponding package name. That is, we replace '/' (which is illegal in package names) by '.'. */ mainPackage = mainPackage.deleteAtEnd("/").replace('/', "."); compile(compilation, mainPackage, nativeProgram, nativeCompiler, editorMode); int res = output.statusCode; if (memInfo) printMemoryUsage(); if (timeInfo) { chrono.stop(); println("Time mesurements:"); nice.tools.util.Chronometer.printAll(); } return res; } void main(String[] args) { int status = compile(args); // A non-zero code is interpreted as error, e.g. by make. // We do not want that if there are just warnings. // When called from an editor it does not matter, as it should be able to interpret warnings. if (! bossa.util.Location.editorMode && status == WARNING) status = OK; System.exit(status); } void version() { println("Nice compiler version " + versionNumber + " (build " + buildDate + ")"); println("Copyright (C) 2003 Daniel Bonniot"); println("Visit the Nice homepage: http://nice.sourceforge.net"); System.exit(0); } String prettyBytes(long bytes) { long K = 1024; if (bytes < 10 * K) return ""+bytes+" B"; else if (bytes < 100 * K) return ""+(bytes/K) + "." + ((bytes % K)/100) + " KB"; else if (bytes < 10*K*K) return ""+(bytes/K) + " KB"; else if (bytes < 100*K*K) return ""+(bytes/(K*K)) + "." + ((bytes % (K*K))/(100*K)) + " MB"; else if (bytes < 10*K*K*K) return ""+(bytes/(K*K)) + " MB"; else return ""+(bytes/(K*K*K)) + " GB"; } void printMemoryUsage() { Runtime r = Runtime.getRuntime(); r.gc(); long total = r.totalMemory(); long free = r.freeMemory(); println("Memory allocated: " + prettyBytes(total) + ", used: " + prettyBytes(total - free)); } // Local Variables: // nice-xprogram: "nicec -d \"$HOME/Nice/classes\" --sourcepath=\"$HOME/Nice/src\" --classpath=\"$HOME/Nice/classes\"" // End: --- NEW FILE: listener.nice --- /**************************************************************************/ /* N I C E */ /* A high-level object-oriented research language */ /* (c) Daniel Bonniot 2003 */ /* */ /* This program is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* */ /**************************************************************************/ package nice.tools.compiler.console; /** Keep track of messages reported to the user. @author Daniel Bonniot (bo...@us...) */ public ConsoleOutput consoleOutput() = new ConsoleOutput(); let int OK = 0; // Normal exit (compilation sucessful, version message) let int BUG = 1; // Abnormal termination (bug in the compiler) let int ERROR = 2; // Error reported (file missing, type error, ...) let int WARNING = 3; // Warning reported public final class ConsoleOutput implements CompilationListener { public int statusCode = OK; private void setStatusCode(int status) { if (worse(status, statusCode)) statusCode = status; } progress(packageName, phase) { if (packageName != null) println(packageName + ": " + phase); else println(phase); } warning(location, message) { this.setStatusCode(WARNING); if (location != null) System.out.println("\n" + location + ":\n" + message); else System.out.println("\n" + message); } error(location, message) { this.setStatusCode(ERROR); if (location != null) System.err.println("\n" + location + ":\n" + message); else System.err.println("\n" + message); } /** A bug has occured inside the compiler. */ bug(stackTrace, url) { this.setStatusCode(BUG); System.err.println ("\nAn exception has occured in the compiler\n" + "Please fill-in a bug report at the following webpage:\n" + url + // "\n\nException: " + exn.toString() + "\n\nStack trace:\n" + stackTrace); } } boolean worse(int status, int than) { if (than == OK) return true; if (than == BUG) return false; if (than == ERROR) return false; if (than == WARNING) return status == BUG || status == ERROR; return false; } |