Hi Rich, everyone, The attached patch combines Repl, Script, and the lib compiler that Stephen G. and I have worked on.
$ java -jar clojure.jar -help Usage: java -jar clojure.jar [options] [file] Options: -help This help message -eval expr Evaluate an expression (may be repeated) -compile libs... Compile the named libs; terminates options -repl Run an interactive Read-Eval-Print Loop (default) file Run a script; extra args in *command-line-args* - Run a script read from standard input -Stuart Sierra --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---
Index: src/clj/precompile.clj =================================================================== --- src/clj/precompile.clj (revision 1121) +++ src/clj/precompile.clj (working copy) @@ -1,11 +0,0 @@ -;; This script is run by the Ant build task to precompile the core -;; Clojure source files. - -(println "Compiling Clojure core sources...") - -(binding [*compile-path* (System/getProperty "clojure.compile.path")] - (compile 'clojure.core) - (compile 'clojure.set) - (compile 'clojure.xml) - (compile 'clojure.zip) - (compile 'clojure.inspector)) Index: src/jvm/clojure/lang/Main.java =================================================================== --- src/jvm/clojure/lang/Main.java (revision 0) +++ src/jvm/clojure/lang/Main.java (revision 0) @@ -0,0 +1,198 @@ +/** + * Copyright (c) Rich Hickey. All rights reserved. + * The use and distribution terms for this software are covered by the + * Common Public License 1.0 (http://opensource.org/licenses/cpl.php) + * which can be found in the file CPL.TXT at the root of this distribution. + * By using this software in any fashion, you are agreeing to be bound by + * the terms of this license. + * You must not remove this notice, or any other, from this software. + **/ + +package clojure.lang; + +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.StringReader; +import java.util.Arrays; +import java.util.List; + +public class Main { + + static final String COMPILE_PATH_PROP = "clojure.compile.path"; + + static final Symbol USER = Symbol.create("user"); + static final Symbol CLOJURE = Symbol.create("clojure.core"); + + static final Var in_ns = RT.var("clojure.core", "in-ns"); + static final Var refer = RT.var("clojure.core", "refer"); + static final Var ns = RT.var("clojure.core", "*ns*"); + static final Var compile_path = RT.var("clojure.core", "*compile-path*"); + static final Var warn_on_reflection = RT.var("clojure.core", "*warn-on-reflection*"); + static final Var print_meta = RT.var("clojure.core", "*print-meta*"); + static final Var print_length = RT.var("clojure.core", "*print-length*"); + static final Var print_level = RT.var("clojure.core", "*print-level*"); + static final Var star1 = RT.var("clojure.core", "*1"); + static final Var star2 = RT.var("clojure.core", "*2"); + static final Var star3 = RT.var("clojure.core", "*3"); + static final Var stare = RT.var("clojure.core", "*e"); + static final Var cmd_line_args = RT.var("clojure.core", "*command-line-args*"); + static final Var compile = RT.var("clojure.core", "compile"); + + public static void repl(String[] args) throws Exception { + commandLineArgs(args); + + //repl IO support + LineNumberingPushbackReader rdr = new LineNumberingPushbackReader(new InputStreamReader(System.in, RT.UTF8)); + OutputStreamWriter w = (OutputStreamWriter) RT.OUT.get(); + Object EOF = new Object(); + + //start the loop + w.write("Clojure\n"); + for(; ;) { + try { + w.write(Compiler.currentNS().name + "=> "); + w.flush(); + Object r = LispReader.read(rdr, false, EOF, false); + if(r == EOF) { + w.write("\n"); + w.flush(); + break; + } + Object ret = Compiler.eval(r); + RT.print(ret, w); + w.write('\n'); + star3.set(star2.get()); + star2.set(star1.get()); + star1.set(ret); + } catch(Throwable e) { + Throwable c = e; + while(c.getCause() != null) + c = c.getCause(); + ((PrintWriter) RT.ERR.get()).println(e instanceof Compiler.CompilerException ? e : c); + stare.set(e); + } + } + } + + public static void runScript(String[] args) throws Exception { + String file = args[0]; + commandLineArgs(Arrays.copyOfRange(args, 1, args.length)); + + if (file.equals("-")) { // read script frod STDIN + LineNumberingPushbackReader rdr = new LineNumberingPushbackReader(new InputStreamReader(System.in, RT.UTF8)); + OutputStreamWriter w = (OutputStreamWriter) RT.OUT.get(); + Object EOF = new Object(); + for (;;) { + Object r = LispReader.read(rdr, false, EOF, false); + if(r == EOF) { + w.flush(); + break; + } + Compiler.eval(r); + } + } else if (file.startsWith("@")) { + // trim leading slash if it's there -- loadResourceScript + // prepends its own slash to every name it's given + RT.loadResourceScript(file.substring(file.startsWith("@/") ? 2 : 1)); + } else { + try { + Compiler.loadFile(file); + } catch (java.io.FileNotFoundException e) { + System.err.println(e.toString()); + System.err.println("Run with -help for command-line options."); + } + } + } + + public static void commandLineArgs(String[] args) throws Exception { + // Set up *command-line-args* + List<String> arglist = Arrays.asList(args); + cmd_line_args.bindRoot(RT.seq(arglist.subList(0, args.length))); + } + + public static void compile(String[] args) throws Exception { + if (compile_path.get() == null) { + System.err.println("ERROR: Must set system property " + COMPILE_PATH_PROP + + "\nto the location for compiled .class files." + + "\nThis directory must also be on your CLASSPATH."); + System.exit(1); + } + System.out.println("Compiling " + args.length + " " + + ((args.length == 1) ? "lib" : "libs") + + " to " + compile_path.get()); + System.out.flush(); + for (String lib : args) { + compile.invoke(Symbol.intern(lib)); + } + } + + public static void eval(String expr) throws Exception { + LineNumberingPushbackReader rdr = new LineNumberingPushbackReader(new StringReader(expr)); + Object EOF = new Object(); + Object r = LispReader.read(rdr, true, null, false); + Compiler.eval(r); + } + + public static void printHelpAndExit() { + System.out.print("Usage: java -jar clojure.jar [options]\n\n" + + "Options:\n" + + " -help This help message\n" + + " -eval expr Evaluate an expression (may be repeated)\n" + + " -compile libs... Compile the named libs; terminates options\n" + + " -repl Run an interactive Read-Eval-Print Loop (default)\n" + + " file Run a script; extra args in *command-line-args*\n" + + " - Run a script read from standard input\n"); + System.exit(0); + } + + public static void main(String[] args) throws Exception { + try { + // Setup default bindings. + Var.pushThreadBindings(RT.map(ns, ns.get(), + warn_on_reflection, warn_on_reflection.get(), + print_meta, print_meta.get(), + print_length, print_length.get(), + print_level, print_level.get(), + compile_path, System.getProperty(COMPILE_PATH_PROP), + star1, null, + star2, null, + star3, null, + stare, null)); + + // Create and move into the user namespace. + in_ns.invoke(USER); + refer.invoke(CLOJURE); + + // Default action: run a REPL. + if (args.length == 0) { + repl(args); + } + + // Process command line. + for (int i = 0; i < args.length; ++i) { + if (args[i].equals("-help")) { + printHelpAndExit(); + } else if (args[i].equals("-repl")) { + args = Arrays.copyOfRange(args, i + 1, args.length); + repl(args); + return; + } else if (args[i].equals("-compile")) { + args = Arrays.copyOfRange(args, i + 1, args.length); + compile(args); + return; + } else if (args[i].equals("-eval")) { + eval(args[++i]); + } else { + runScript(args); + return; + } + } + } catch(Exception e) { + e.printStackTrace((PrintWriter) RT.ERR.get()); + System.exit(-1); + } finally { + Var.popThreadBindings(); + } + } +} Index: build.xml =================================================================== --- build.xml (revision 1121) +++ build.xml (working copy) @@ -23,21 +23,25 @@ <javac srcdir="${jsrc}" destdir="${build}" includeJavaRuntime="yes" debug="true" target="1.5"/> </target> - <target name="core" depends="compile" + <target name="compile_clojure" depends="compile" description="Precompile Clojure core sources."> - <java classname="clojure.lang.Script" + <java classname="clojure.lang.Main" classpath="${build}:${cljsrc}"> <sysproperty key="clojure.compile.path" value="${build}"/> - <arg value="${precompile}"/> + <arg value="-compile"/> + <arg value="clojure.core"/> + <arg value="clojure.set"/> + <arg value="clojure.xml"/> + <arg value="clojure.zip"/> + <arg value="clojure.inspector"/> </java> </target> - <target name="jar" depends="core" + <target name="jar" depends="compile_clojure" description="Create jar file."> <jar jarfile="${clojure_jar}" basedir="${build}"> - <!-- <fileset dir="${cljsrc}" includes="**/*.clj"/> --> <manifest> - <attribute name="Main-Class" value="clojure.lang.Repl"/> + <attribute name="Main-Class" value="clojure.lang.Main"/> <attribute name="Class-Path" value="."/> </manifest> </jar>