Mercurial > lasercutter
diff src/clojure/lang/RT.java @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/lang/RT.java Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,1735 @@ 1.4 +/** 1.5 + * Copyright (c) Rich Hickey. All rights reserved. 1.6 + * The use and distribution terms for this software are covered by the 1.7 + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.8 + * which can be found in the file epl-v10.html at the root of this distribution. 1.9 + * By using this software in any fashion, you are agreeing to be bound by 1.10 + * the terms of this license. 1.11 + * You must not remove this notice, or any other, from this software. 1.12 + **/ 1.13 + 1.14 +/* rich Mar 25, 2006 4:28:27 PM */ 1.15 + 1.16 +package clojure.lang; 1.17 + 1.18 +import java.util.concurrent.atomic.AtomicInteger; 1.19 +import java.util.concurrent.Callable; 1.20 +import java.util.*; 1.21 +import java.util.regex.Matcher; 1.22 +import java.util.regex.Pattern; 1.23 +import java.io.*; 1.24 +import java.lang.reflect.Array; 1.25 +import java.math.BigDecimal; 1.26 +import java.math.BigInteger; 1.27 +import java.security.AccessController; 1.28 +import java.security.PrivilegedAction; 1.29 +import java.net.URL; 1.30 +import java.net.JarURLConnection; 1.31 +import java.nio.charset.Charset; 1.32 + 1.33 +public class RT{ 1.34 + 1.35 +static final public Boolean T = Boolean.TRUE;//Keyword.intern(Symbol.create(null, "t")); 1.36 +static final public Boolean F = Boolean.FALSE;//Keyword.intern(Symbol.create(null, "t")); 1.37 +static final public String LOADER_SUFFIX = "__init"; 1.38 + 1.39 +//simple-symbol->class 1.40 +final static IPersistentMap DEFAULT_IMPORTS = map( 1.41 +// Symbol.create("RT"), "clojure.lang.RT", 1.42 +// Symbol.create("Num"), "clojure.lang.Num", 1.43 +// Symbol.create("Symbol"), "clojure.lang.Symbol", 1.44 +// Symbol.create("Keyword"), "clojure.lang.Keyword", 1.45 +// Symbol.create("Var"), "clojure.lang.Var", 1.46 +// Symbol.create("Ref"), "clojure.lang.Ref", 1.47 +// Symbol.create("IFn"), "clojure.lang.IFn", 1.48 +// Symbol.create("IObj"), "clojure.lang.IObj", 1.49 +// Symbol.create("ISeq"), "clojure.lang.ISeq", 1.50 +// Symbol.create("IPersistentCollection"), 1.51 +// "clojure.lang.IPersistentCollection", 1.52 +// Symbol.create("IPersistentMap"), "clojure.lang.IPersistentMap", 1.53 +// Symbol.create("IPersistentList"), "clojure.lang.IPersistentList", 1.54 +// Symbol.create("IPersistentVector"), "clojure.lang.IPersistentVector", 1.55 +Symbol.create("Boolean"), Boolean.class, 1.56 +Symbol.create("Byte"), Byte.class, 1.57 +Symbol.create("Character"), Character.class, 1.58 +Symbol.create("Class"), Class.class, 1.59 +Symbol.create("ClassLoader"), ClassLoader.class, 1.60 +Symbol.create("Compiler"), Compiler.class, 1.61 +Symbol.create("Double"), Double.class, 1.62 +Symbol.create("Enum"), Enum.class, 1.63 +Symbol.create("Float"), Float.class, 1.64 +Symbol.create("InheritableThreadLocal"), InheritableThreadLocal.class, 1.65 +Symbol.create("Integer"), Integer.class, 1.66 +Symbol.create("Long"), Long.class, 1.67 +Symbol.create("Math"), Math.class, 1.68 +Symbol.create("Number"), Number.class, 1.69 +Symbol.create("Object"), Object.class, 1.70 +Symbol.create("Package"), Package.class, 1.71 +Symbol.create("Process"), Process.class, 1.72 +Symbol.create("ProcessBuilder"), ProcessBuilder.class, 1.73 +Symbol.create("Runtime"), Runtime.class, 1.74 +Symbol.create("RuntimePermission"), RuntimePermission.class, 1.75 +Symbol.create("SecurityManager"), SecurityManager.class, 1.76 +Symbol.create("Short"), Short.class, 1.77 +Symbol.create("StackTraceElement"), StackTraceElement.class, 1.78 +Symbol.create("StrictMath"), StrictMath.class, 1.79 +Symbol.create("String"), String.class, 1.80 +Symbol.create("StringBuffer"), StringBuffer.class, 1.81 +Symbol.create("StringBuilder"), StringBuilder.class, 1.82 +Symbol.create("System"), System.class, 1.83 +Symbol.create("Thread"), Thread.class, 1.84 +Symbol.create("ThreadGroup"), ThreadGroup.class, 1.85 +Symbol.create("ThreadLocal"), ThreadLocal.class, 1.86 +Symbol.create("Throwable"), Throwable.class, 1.87 +Symbol.create("Void"), Void.class, 1.88 +Symbol.create("Appendable"), Appendable.class, 1.89 +Symbol.create("CharSequence"), CharSequence.class, 1.90 +Symbol.create("Cloneable"), Cloneable.class, 1.91 +Symbol.create("Comparable"), Comparable.class, 1.92 +Symbol.create("Iterable"), Iterable.class, 1.93 +Symbol.create("Readable"), Readable.class, 1.94 +Symbol.create("Runnable"), Runnable.class, 1.95 +Symbol.create("Callable"), Callable.class, 1.96 +Symbol.create("BigInteger"), BigInteger.class, 1.97 +Symbol.create("BigDecimal"), BigDecimal.class, 1.98 +Symbol.create("ArithmeticException"), ArithmeticException.class, 1.99 +Symbol.create("ArrayIndexOutOfBoundsException"), ArrayIndexOutOfBoundsException.class, 1.100 +Symbol.create("ArrayStoreException"), ArrayStoreException.class, 1.101 +Symbol.create("ClassCastException"), ClassCastException.class, 1.102 +Symbol.create("ClassNotFoundException"), ClassNotFoundException.class, 1.103 +Symbol.create("CloneNotSupportedException"), CloneNotSupportedException.class, 1.104 +Symbol.create("EnumConstantNotPresentException"), EnumConstantNotPresentException.class, 1.105 +Symbol.create("Exception"), Exception.class, 1.106 +Symbol.create("IllegalAccessException"), IllegalAccessException.class, 1.107 +Symbol.create("IllegalArgumentException"), IllegalArgumentException.class, 1.108 +Symbol.create("IllegalMonitorStateException"), IllegalMonitorStateException.class, 1.109 +Symbol.create("IllegalStateException"), IllegalStateException.class, 1.110 +Symbol.create("IllegalThreadStateException"), IllegalThreadStateException.class, 1.111 +Symbol.create("IndexOutOfBoundsException"), IndexOutOfBoundsException.class, 1.112 +Symbol.create("InstantiationException"), InstantiationException.class, 1.113 +Symbol.create("InterruptedException"), InterruptedException.class, 1.114 +Symbol.create("NegativeArraySizeException"), NegativeArraySizeException.class, 1.115 +Symbol.create("NoSuchFieldException"), NoSuchFieldException.class, 1.116 +Symbol.create("NoSuchMethodException"), NoSuchMethodException.class, 1.117 +Symbol.create("NullPointerException"), NullPointerException.class, 1.118 +Symbol.create("NumberFormatException"), NumberFormatException.class, 1.119 +Symbol.create("RuntimeException"), RuntimeException.class, 1.120 +Symbol.create("SecurityException"), SecurityException.class, 1.121 +Symbol.create("StringIndexOutOfBoundsException"), StringIndexOutOfBoundsException.class, 1.122 +Symbol.create("TypeNotPresentException"), TypeNotPresentException.class, 1.123 +Symbol.create("UnsupportedOperationException"), UnsupportedOperationException.class, 1.124 +Symbol.create("AbstractMethodError"), AbstractMethodError.class, 1.125 +Symbol.create("AssertionError"), AssertionError.class, 1.126 +Symbol.create("ClassCircularityError"), ClassCircularityError.class, 1.127 +Symbol.create("ClassFormatError"), ClassFormatError.class, 1.128 +Symbol.create("Error"), Error.class, 1.129 +Symbol.create("ExceptionInInitializerError"), ExceptionInInitializerError.class, 1.130 +Symbol.create("IllegalAccessError"), IllegalAccessError.class, 1.131 +Symbol.create("IncompatibleClassChangeError"), IncompatibleClassChangeError.class, 1.132 +Symbol.create("InstantiationError"), InstantiationError.class, 1.133 +Symbol.create("InternalError"), InternalError.class, 1.134 +Symbol.create("LinkageError"), LinkageError.class, 1.135 +Symbol.create("NoClassDefFoundError"), NoClassDefFoundError.class, 1.136 +Symbol.create("NoSuchFieldError"), NoSuchFieldError.class, 1.137 +Symbol.create("NoSuchMethodError"), NoSuchMethodError.class, 1.138 +Symbol.create("OutOfMemoryError"), OutOfMemoryError.class, 1.139 +Symbol.create("StackOverflowError"), StackOverflowError.class, 1.140 +Symbol.create("ThreadDeath"), ThreadDeath.class, 1.141 +Symbol.create("UnknownError"), UnknownError.class, 1.142 +Symbol.create("UnsatisfiedLinkError"), UnsatisfiedLinkError.class, 1.143 +Symbol.create("UnsupportedClassVersionError"), UnsupportedClassVersionError.class, 1.144 +Symbol.create("VerifyError"), VerifyError.class, 1.145 +Symbol.create("VirtualMachineError"), VirtualMachineError.class, 1.146 +Symbol.create("Thread$UncaughtExceptionHandler"), Thread.UncaughtExceptionHandler.class, 1.147 +Symbol.create("Thread$State"), Thread.State.class, 1.148 +Symbol.create("Deprecated"), Deprecated.class, 1.149 +Symbol.create("Override"), Override.class, 1.150 +Symbol.create("SuppressWarnings"), SuppressWarnings.class 1.151 + 1.152 +// Symbol.create("Collection"), "java.util.Collection", 1.153 +// Symbol.create("Comparator"), "java.util.Comparator", 1.154 +// Symbol.create("Enumeration"), "java.util.Enumeration", 1.155 +// Symbol.create("EventListener"), "java.util.EventListener", 1.156 +// Symbol.create("Formattable"), "java.util.Formattable", 1.157 +// Symbol.create("Iterator"), "java.util.Iterator", 1.158 +// Symbol.create("List"), "java.util.List", 1.159 +// Symbol.create("ListIterator"), "java.util.ListIterator", 1.160 +// Symbol.create("Map"), "java.util.Map", 1.161 +// Symbol.create("Map$Entry"), "java.util.Map$Entry", 1.162 +// Symbol.create("Observer"), "java.util.Observer", 1.163 +// Symbol.create("Queue"), "java.util.Queue", 1.164 +// Symbol.create("RandomAccess"), "java.util.RandomAccess", 1.165 +// Symbol.create("Set"), "java.util.Set", 1.166 +// Symbol.create("SortedMap"), "java.util.SortedMap", 1.167 +// Symbol.create("SortedSet"), "java.util.SortedSet" 1.168 +); 1.169 + 1.170 +// single instance of UTF-8 Charset, so as to avoid catching UnsupportedCharsetExceptions everywhere 1.171 +static public Charset UTF8 = Charset.forName("UTF-8"); 1.172 + 1.173 +static public final Namespace CLOJURE_NS = Namespace.findOrCreate(Symbol.create("clojure.core")); 1.174 +//static final Namespace USER_NS = Namespace.findOrCreate(Symbol.create("user")); 1.175 +final static public Var OUT = 1.176 + Var.intern(CLOJURE_NS, Symbol.create("*out*"), new OutputStreamWriter(System.out)); 1.177 +final static public Var IN = 1.178 + Var.intern(CLOJURE_NS, Symbol.create("*in*"), 1.179 + new LineNumberingPushbackReader(new InputStreamReader(System.in))); 1.180 +final static public Var ERR = 1.181 + Var.intern(CLOJURE_NS, Symbol.create("*err*"), 1.182 + new PrintWriter(new OutputStreamWriter(System.err), true)); 1.183 +final static Keyword TAG_KEY = Keyword.intern(null, "tag"); 1.184 +final static public Var AGENT = Var.intern(CLOJURE_NS, Symbol.create("*agent*"), null); 1.185 +final static public Var READEVAL = Var.intern(CLOJURE_NS, Symbol.create("*read-eval*"), T); 1.186 +final static public Var ASSERT = Var.intern(CLOJURE_NS, Symbol.create("*assert*"), T); 1.187 +final static public Var MATH_CONTEXT = Var.intern(CLOJURE_NS, Symbol.create("*math-context*"), null); 1.188 +static Keyword LINE_KEY = Keyword.intern(null, "line"); 1.189 +static Keyword FILE_KEY = Keyword.intern(null, "file"); 1.190 +static Keyword DECLARED_KEY = Keyword.intern(null, "declared"); 1.191 +final static public Var USE_CONTEXT_CLASSLOADER = 1.192 + Var.intern(CLOJURE_NS, Symbol.create("*use-context-classloader*"), T); 1.193 +//final static public Var CURRENT_MODULE = Var.intern(Symbol.create("clojure.core", "current-module"), 1.194 +// Module.findOrCreateModule("clojure/user")); 1.195 + 1.196 +final static Symbol LOAD_FILE = Symbol.create("load-file"); 1.197 +final static Symbol IN_NAMESPACE = Symbol.create("in-ns"); 1.198 +final static Symbol NAMESPACE = Symbol.create("ns"); 1.199 +static final Symbol IDENTICAL = Symbol.create("identical?"); 1.200 +final static Var CMD_LINE_ARGS = Var.intern(CLOJURE_NS, Symbol.create("*command-line-args*"), null); 1.201 +//symbol 1.202 +final public static Var CURRENT_NS = Var.intern(CLOJURE_NS, Symbol.create("*ns*"), 1.203 + CLOJURE_NS); 1.204 + 1.205 +final static Var FLUSH_ON_NEWLINE = Var.intern(CLOJURE_NS, Symbol.create("*flush-on-newline*"), T); 1.206 +final static Var PRINT_META = Var.intern(CLOJURE_NS, Symbol.create("*print-meta*"), F); 1.207 +final static Var PRINT_READABLY = Var.intern(CLOJURE_NS, Symbol.create("*print-readably*"), T); 1.208 +final static Var PRINT_DUP = Var.intern(CLOJURE_NS, Symbol.create("*print-dup*"), F); 1.209 +final static Var WARN_ON_REFLECTION = Var.intern(CLOJURE_NS, Symbol.create("*warn-on-reflection*"), F); 1.210 +final static Var ALLOW_UNRESOLVED_VARS = Var.intern(CLOJURE_NS, Symbol.create("*allow-unresolved-vars*"), F); 1.211 + 1.212 +final static Var IN_NS_VAR = Var.intern(CLOJURE_NS, Symbol.create("in-ns"), F); 1.213 +final static Var NS_VAR = Var.intern(CLOJURE_NS, Symbol.create("ns"), F); 1.214 +static final Var PRINT_INITIALIZED = Var.intern(CLOJURE_NS, Symbol.create("print-initialized")); 1.215 +static final Var PR_ON = Var.intern(CLOJURE_NS, Symbol.create("pr-on")); 1.216 +//final static Var IMPORTS = Var.intern(CLOJURE_NS, Symbol.create("*imports*"), DEFAULT_IMPORTS); 1.217 +final static IFn inNamespace = new AFn(){ 1.218 + public Object invoke(Object arg1) throws Exception{ 1.219 + Symbol nsname = (Symbol) arg1; 1.220 + Namespace ns = Namespace.findOrCreate(nsname); 1.221 + CURRENT_NS.set(ns); 1.222 + return ns; 1.223 + } 1.224 +}; 1.225 + 1.226 +final static IFn bootNamespace = new AFn(){ 1.227 + public Object invoke(Object __form, Object __env,Object arg1) throws Exception{ 1.228 + Symbol nsname = (Symbol) arg1; 1.229 + Namespace ns = Namespace.findOrCreate(nsname); 1.230 + CURRENT_NS.set(ns); 1.231 + return ns; 1.232 + } 1.233 +}; 1.234 + 1.235 +public static List<String> processCommandLine(String[] args){ 1.236 + List<String> arglist = Arrays.asList(args); 1.237 + int split = arglist.indexOf("--"); 1.238 + if(split >= 0) { 1.239 + CMD_LINE_ARGS.bindRoot(RT.seq(arglist.subList(split + 1, args.length))); 1.240 + return arglist.subList(0, split); 1.241 + } 1.242 + return arglist; 1.243 +} 1.244 + 1.245 +// duck typing stderr plays nice with e.g. swank 1.246 +public static PrintWriter errPrintWriter(){ 1.247 + Writer w = (Writer) ERR.deref(); 1.248 + if (w instanceof PrintWriter) { 1.249 + return (PrintWriter) w; 1.250 + } else { 1.251 + return new PrintWriter(w); 1.252 + } 1.253 +} 1.254 + 1.255 +static public final Object[] EMPTY_ARRAY = new Object[]{}; 1.256 +static public final Comparator DEFAULT_COMPARATOR = new DefaultComparator(); 1.257 + 1.258 +private static final class DefaultComparator implements Comparator, Serializable { 1.259 + public int compare(Object o1, Object o2){ 1.260 + return Util.compare(o1, o2); 1.261 + } 1.262 + 1.263 + private Object readResolve() throws ObjectStreamException { 1.264 + // ensures that we aren't hanging onto a new default comparator for every 1.265 + // sorted set, etc., we deserialize 1.266 + return DEFAULT_COMPARATOR; 1.267 + } 1.268 +} 1.269 + 1.270 +static AtomicInteger id = new AtomicInteger(1); 1.271 + 1.272 +static public void addURL(Object url) throws Exception{ 1.273 + URL u = (url instanceof String) ? (new URL((String) url)) : (URL) url; 1.274 + ClassLoader ccl = Thread.currentThread().getContextClassLoader(); 1.275 + if(ccl instanceof DynamicClassLoader) 1.276 + ((DynamicClassLoader)ccl).addURL(u); 1.277 + else 1.278 + throw new IllegalAccessError("Context classloader is not a DynamicClassLoader"); 1.279 +} 1.280 + 1.281 +static{ 1.282 + Keyword dockw = Keyword.intern(null, "doc"); 1.283 + Keyword arglistskw = Keyword.intern(null, "arglists"); 1.284 + Symbol namesym = Symbol.create("name"); 1.285 + OUT.setTag(Symbol.create("java.io.Writer")); 1.286 + CURRENT_NS.setTag(Symbol.create("clojure.lang.Namespace")); 1.287 + AGENT.setMeta(map(dockw, "The agent currently running an action on this thread, else nil")); 1.288 + AGENT.setTag(Symbol.create("clojure.lang.Agent")); 1.289 + MATH_CONTEXT.setTag(Symbol.create("java.math.MathContext")); 1.290 + Var nv = Var.intern(CLOJURE_NS, NAMESPACE, bootNamespace); 1.291 + nv.setMacro(); 1.292 + Var v; 1.293 + v = Var.intern(CLOJURE_NS, IN_NAMESPACE, inNamespace); 1.294 + v.setMeta(map(dockw, "Sets *ns* to the namespace named by the symbol, creating it if needed.", 1.295 + arglistskw, list(vector(namesym)))); 1.296 + v = Var.intern(CLOJURE_NS, LOAD_FILE, 1.297 + new AFn(){ 1.298 + public Object invoke(Object arg1) throws Exception{ 1.299 + return Compiler.loadFile((String) arg1); 1.300 + } 1.301 + }); 1.302 + v.setMeta(map(dockw, "Sequentially read and evaluate the set of forms contained in the file.", 1.303 + arglistskw, list(vector(namesym)))); 1.304 + try { 1.305 + doInit(); 1.306 + } 1.307 + catch(Exception e) { 1.308 + throw new RuntimeException(e); 1.309 + } 1.310 +} 1.311 + 1.312 + 1.313 +static public Var var(String ns, String name){ 1.314 + return Var.intern(Namespace.findOrCreate(Symbol.intern(null, ns)), Symbol.intern(null, name)); 1.315 +} 1.316 + 1.317 +static public Var var(String ns, String name, Object init){ 1.318 + return Var.intern(Namespace.findOrCreate(Symbol.intern(null, ns)), Symbol.intern(null, name), init); 1.319 +} 1.320 + 1.321 +public static void loadResourceScript(String name) throws Exception{ 1.322 + loadResourceScript(name, true); 1.323 +} 1.324 + 1.325 +public static void maybeLoadResourceScript(String name) throws Exception{ 1.326 + loadResourceScript(name, false); 1.327 +} 1.328 + 1.329 +public static void loadResourceScript(String name, boolean failIfNotFound) throws Exception{ 1.330 + loadResourceScript(RT.class, name, failIfNotFound); 1.331 +} 1.332 + 1.333 +public static void loadResourceScript(Class c, String name) throws Exception{ 1.334 + loadResourceScript(c, name, true); 1.335 +} 1.336 + 1.337 +public static void loadResourceScript(Class c, String name, boolean failIfNotFound) throws Exception{ 1.338 + int slash = name.lastIndexOf('/'); 1.339 + String file = slash >= 0 ? name.substring(slash + 1) : name; 1.340 + InputStream ins = baseLoader().getResourceAsStream(name); 1.341 + if(ins != null) { 1.342 + try { 1.343 + Compiler.load(new InputStreamReader(ins, UTF8), name, file); 1.344 + } 1.345 + finally { 1.346 + ins.close(); 1.347 + } 1.348 + } 1.349 + else if(failIfNotFound) { 1.350 + throw new FileNotFoundException("Could not locate Clojure resource on classpath: " + name); 1.351 + } 1.352 +} 1.353 + 1.354 +static public void init() throws Exception{ 1.355 + RT.errPrintWriter().println("No need to call RT.init() anymore"); 1.356 +} 1.357 + 1.358 +static public long lastModified(URL url, String libfile) throws Exception{ 1.359 + if(url.getProtocol().equals("jar")) { 1.360 + return ((JarURLConnection) url.openConnection()).getJarFile().getEntry(libfile).getTime(); 1.361 + } 1.362 + else { 1.363 + return url.openConnection().getLastModified(); 1.364 + } 1.365 +} 1.366 + 1.367 +static void compile(String cljfile) throws Exception{ 1.368 + InputStream ins = baseLoader().getResourceAsStream(cljfile); 1.369 + if(ins != null) { 1.370 + try { 1.371 + Compiler.compile(new InputStreamReader(ins, UTF8), cljfile, 1.372 + cljfile.substring(1 + cljfile.lastIndexOf("/"))); 1.373 + } 1.374 + finally { 1.375 + ins.close(); 1.376 + } 1.377 + 1.378 + } 1.379 + else 1.380 + throw new FileNotFoundException("Could not locate Clojure resource on classpath: " + cljfile); 1.381 +} 1.382 + 1.383 +static public void load(String scriptbase) throws Exception{ 1.384 + load(scriptbase, true); 1.385 +} 1.386 + 1.387 +static public void load(String scriptbase, boolean failIfNotFound) throws Exception{ 1.388 + String classfile = scriptbase + LOADER_SUFFIX + ".class"; 1.389 + String cljfile = scriptbase + ".clj"; 1.390 + URL classURL = baseLoader().getResource(classfile); 1.391 + URL cljURL = baseLoader().getResource(cljfile); 1.392 + boolean loaded = false; 1.393 + 1.394 + if((classURL != null && 1.395 + (cljURL == null 1.396 + || lastModified(classURL, classfile) > lastModified(cljURL, cljfile))) 1.397 + || classURL == null) { 1.398 + try { 1.399 + Var.pushThreadBindings( 1.400 + RT.map(CURRENT_NS, CURRENT_NS.deref(), 1.401 + WARN_ON_REFLECTION, WARN_ON_REFLECTION.deref())); 1.402 + loaded = (loadClassForName(scriptbase.replace('/', '.') + LOADER_SUFFIX) != null); 1.403 + } 1.404 + finally { 1.405 + Var.popThreadBindings(); 1.406 + } 1.407 + } 1.408 + if(!loaded && cljURL != null) { 1.409 + if(booleanCast(Compiler.COMPILE_FILES.deref())) 1.410 + compile(cljfile); 1.411 + else 1.412 + loadResourceScript(RT.class, cljfile); 1.413 + } 1.414 + else if(!loaded && failIfNotFound) 1.415 + throw new FileNotFoundException(String.format("Could not locate %s or %s on classpath: ", classfile, cljfile)); 1.416 +} 1.417 + 1.418 +static void doInit() throws Exception{ 1.419 + load("clojure/core"); 1.420 + load("clojure/zip", false); 1.421 + load("clojure/xml", false); 1.422 + load("clojure/set", false); 1.423 + 1.424 + Var.pushThreadBindings( 1.425 + RT.map(CURRENT_NS, CURRENT_NS.deref(), 1.426 + WARN_ON_REFLECTION, WARN_ON_REFLECTION.deref())); 1.427 + try { 1.428 + Symbol USER = Symbol.create("user"); 1.429 + Symbol CLOJURE = Symbol.create("clojure.core"); 1.430 + 1.431 + Var in_ns = var("clojure.core", "in-ns"); 1.432 + Var refer = var("clojure.core", "refer"); 1.433 + in_ns.invoke(USER); 1.434 + refer.invoke(CLOJURE); 1.435 + maybeLoadResourceScript("user.clj"); 1.436 + } 1.437 + finally { 1.438 + Var.popThreadBindings(); 1.439 + } 1.440 +} 1.441 + 1.442 +static public int nextID(){ 1.443 + return id.getAndIncrement(); 1.444 +} 1.445 + 1.446 + 1.447 +////////////// Collections support ///////////////////////////////// 1.448 + 1.449 +static public ISeq seq(Object coll){ 1.450 + if(coll instanceof ASeq) 1.451 + return (ASeq) coll; 1.452 + else if(coll instanceof LazySeq) 1.453 + return ((LazySeq) coll).seq(); 1.454 + else 1.455 + return seqFrom(coll); 1.456 +} 1.457 + 1.458 +static ISeq seqFrom(Object coll){ 1.459 + if(coll instanceof Seqable) 1.460 + return ((Seqable) coll).seq(); 1.461 + else if(coll == null) 1.462 + return null; 1.463 + else if(coll instanceof Iterable) 1.464 + return IteratorSeq.create(((Iterable) coll).iterator()); 1.465 + else if(coll.getClass().isArray()) 1.466 + return ArraySeq.createFromObject(coll); 1.467 + else if(coll instanceof CharSequence) 1.468 + return StringSeq.create((CharSequence) coll); 1.469 + else if(coll instanceof Map) 1.470 + return seq(((Map) coll).entrySet()); 1.471 + else { 1.472 + Class c = coll.getClass(); 1.473 + Class sc = c.getSuperclass(); 1.474 + throw new IllegalArgumentException("Don't know how to create ISeq from: " + c.getName()); 1.475 + } 1.476 +} 1.477 + 1.478 +static public ISeq keys(Object coll){ 1.479 + return APersistentMap.KeySeq.create(seq(coll)); 1.480 +} 1.481 + 1.482 +static public ISeq vals(Object coll){ 1.483 + return APersistentMap.ValSeq.create(seq(coll)); 1.484 +} 1.485 + 1.486 +static public IPersistentMap meta(Object x){ 1.487 + if(x instanceof IMeta) 1.488 + return ((IMeta) x).meta(); 1.489 + return null; 1.490 +} 1.491 + 1.492 +public static int count(Object o){ 1.493 + if(o instanceof Counted) 1.494 + return ((Counted) o).count(); 1.495 + return countFrom(Util.ret1(o, o = null)); 1.496 +} 1.497 + 1.498 +static int countFrom(Object o){ 1.499 + if(o == null) 1.500 + return 0; 1.501 + else if(o instanceof IPersistentCollection) { 1.502 + ISeq s = seq(o); 1.503 + o = null; 1.504 + int i = 0; 1.505 + for(; s != null; s = s.next()) { 1.506 + if(s instanceof Counted) 1.507 + return i + s.count(); 1.508 + i++; 1.509 + } 1.510 + return i; 1.511 + } 1.512 + else if(o instanceof CharSequence) 1.513 + return ((CharSequence) o).length(); 1.514 + else if(o instanceof Collection) 1.515 + return ((Collection) o).size(); 1.516 + else if(o instanceof Map) 1.517 + return ((Map) o).size(); 1.518 + else if(o.getClass().isArray()) 1.519 + return Array.getLength(o); 1.520 + 1.521 + throw new UnsupportedOperationException("count not supported on this type: " + o.getClass().getSimpleName()); 1.522 +} 1.523 + 1.524 +static public IPersistentCollection conj(IPersistentCollection coll, Object x){ 1.525 + if(coll == null) 1.526 + return new PersistentList(x); 1.527 + return coll.cons(x); 1.528 +} 1.529 + 1.530 +static public ISeq cons(Object x, Object coll){ 1.531 + //ISeq y = seq(coll); 1.532 + if(coll == null) 1.533 + return new PersistentList(x); 1.534 + else if(coll instanceof ISeq) 1.535 + return new Cons(x, (ISeq) coll); 1.536 + else 1.537 + return new Cons(x, seq(coll)); 1.538 +} 1.539 + 1.540 +static public Object first(Object x){ 1.541 + if(x instanceof ISeq) 1.542 + return ((ISeq) x).first(); 1.543 + ISeq seq = seq(x); 1.544 + if(seq == null) 1.545 + return null; 1.546 + return seq.first(); 1.547 +} 1.548 + 1.549 +static public Object second(Object x){ 1.550 + return first(next(x)); 1.551 +} 1.552 + 1.553 +static public Object third(Object x){ 1.554 + return first(next(next(x))); 1.555 +} 1.556 + 1.557 +static public Object fourth(Object x){ 1.558 + return first(next(next(next(x)))); 1.559 +} 1.560 + 1.561 +static public ISeq next(Object x){ 1.562 + if(x instanceof ISeq) 1.563 + return ((ISeq) x).next(); 1.564 + ISeq seq = seq(x); 1.565 + if(seq == null) 1.566 + return null; 1.567 + return seq.next(); 1.568 +} 1.569 + 1.570 +static public ISeq more(Object x){ 1.571 + if(x instanceof ISeq) 1.572 + return ((ISeq) x).more(); 1.573 + ISeq seq = seq(x); 1.574 + if(seq == null) 1.575 + return PersistentList.EMPTY; 1.576 + return seq.more(); 1.577 +} 1.578 + 1.579 +//static public Seqable more(Object x){ 1.580 +// Seqable ret = null; 1.581 +// if(x instanceof ISeq) 1.582 +// ret = ((ISeq) x).more(); 1.583 +// else 1.584 +// { 1.585 +// ISeq seq = seq(x); 1.586 +// if(seq == null) 1.587 +// ret = PersistentList.EMPTY; 1.588 +// else 1.589 +// ret = seq.more(); 1.590 +// } 1.591 +// if(ret == null) 1.592 +// ret = PersistentList.EMPTY; 1.593 +// return ret; 1.594 +//} 1.595 + 1.596 +static public Object peek(Object x){ 1.597 + if(x == null) 1.598 + return null; 1.599 + return ((IPersistentStack) x).peek(); 1.600 +} 1.601 + 1.602 +static public Object pop(Object x){ 1.603 + if(x == null) 1.604 + return null; 1.605 + return ((IPersistentStack) x).pop(); 1.606 +} 1.607 + 1.608 +static public Object get(Object coll, Object key){ 1.609 + if(coll instanceof ILookup) 1.610 + return ((ILookup) coll).valAt(key); 1.611 + return getFrom(coll, key); 1.612 +} 1.613 + 1.614 +static Object getFrom(Object coll, Object key){ 1.615 + if(coll == null) 1.616 + return null; 1.617 + else if(coll instanceof Map) { 1.618 + Map m = (Map) coll; 1.619 + return m.get(key); 1.620 + } 1.621 + else if(coll instanceof IPersistentSet) { 1.622 + IPersistentSet set = (IPersistentSet) coll; 1.623 + return set.get(key); 1.624 + } 1.625 + else if(key instanceof Number && (coll instanceof String || coll.getClass().isArray())) { 1.626 + int n = ((Number) key).intValue(); 1.627 + if(n >= 0 && n < count(coll)) 1.628 + return nth(coll, n); 1.629 + return null; 1.630 + } 1.631 + 1.632 + return null; 1.633 +} 1.634 + 1.635 +static public Object get(Object coll, Object key, Object notFound){ 1.636 + if(coll instanceof ILookup) 1.637 + return ((ILookup) coll).valAt(key, notFound); 1.638 + return getFrom(coll, key, notFound); 1.639 +} 1.640 + 1.641 +static Object getFrom(Object coll, Object key, Object notFound){ 1.642 + if(coll == null) 1.643 + return notFound; 1.644 + else if(coll instanceof Map) { 1.645 + Map m = (Map) coll; 1.646 + if(m.containsKey(key)) 1.647 + return m.get(key); 1.648 + return notFound; 1.649 + } 1.650 + else if(coll instanceof IPersistentSet) { 1.651 + IPersistentSet set = (IPersistentSet) coll; 1.652 + if(set.contains(key)) 1.653 + return set.get(key); 1.654 + return notFound; 1.655 + } 1.656 + else if(key instanceof Number && (coll instanceof String || coll.getClass().isArray())) { 1.657 + int n = ((Number) key).intValue(); 1.658 + return n >= 0 && n < count(coll) ? nth(coll, n) : notFound; 1.659 + } 1.660 + return notFound; 1.661 + 1.662 +} 1.663 + 1.664 +static public Associative assoc(Object coll, Object key, Object val){ 1.665 + if(coll == null) 1.666 + return new PersistentArrayMap(new Object[]{key, val}); 1.667 + return ((Associative) coll).assoc(key, val); 1.668 +} 1.669 + 1.670 +static public Object contains(Object coll, Object key){ 1.671 + if(coll == null) 1.672 + return F; 1.673 + else if(coll instanceof Associative) 1.674 + return ((Associative) coll).containsKey(key) ? T : F; 1.675 + else if(coll instanceof IPersistentSet) 1.676 + return ((IPersistentSet) coll).contains(key) ? T : F; 1.677 + else if(coll instanceof Map) { 1.678 + Map m = (Map) coll; 1.679 + return m.containsKey(key) ? T : F; 1.680 + } 1.681 + else if(key instanceof Number && (coll instanceof String || coll.getClass().isArray())) { 1.682 + int n = ((Number) key).intValue(); 1.683 + return n >= 0 && n < count(coll); 1.684 + } 1.685 + return F; 1.686 +} 1.687 + 1.688 +static public Object find(Object coll, Object key){ 1.689 + if(coll == null) 1.690 + return null; 1.691 + else if(coll instanceof Associative) 1.692 + return ((Associative) coll).entryAt(key); 1.693 + else { 1.694 + Map m = (Map) coll; 1.695 + if(m.containsKey(key)) 1.696 + return new MapEntry(key, m.get(key)); 1.697 + return null; 1.698 + } 1.699 +} 1.700 + 1.701 +//takes a seq of key,val,key,val 1.702 + 1.703 +//returns tail starting at val of matching key if found, else null 1.704 +static public ISeq findKey(Keyword key, ISeq keyvals) throws Exception{ 1.705 + while(keyvals != null) { 1.706 + ISeq r = keyvals.next(); 1.707 + if(r == null) 1.708 + throw new Exception("Malformed keyword argslist"); 1.709 + if(keyvals.first() == key) 1.710 + return r; 1.711 + keyvals = r.next(); 1.712 + } 1.713 + return null; 1.714 +} 1.715 + 1.716 +static public Object dissoc(Object coll, Object key) throws Exception{ 1.717 + if(coll == null) 1.718 + return null; 1.719 + return ((IPersistentMap) coll).without(key); 1.720 +} 1.721 + 1.722 +static public Object nth(Object coll, int n){ 1.723 + if(coll instanceof Indexed) 1.724 + return ((Indexed) coll).nth(n); 1.725 + return nthFrom(Util.ret1(coll, coll = null), n); 1.726 +} 1.727 + 1.728 +static Object nthFrom(Object coll, int n){ 1.729 + if(coll == null) 1.730 + return null; 1.731 + else if(coll instanceof CharSequence) 1.732 + return Character.valueOf(((CharSequence) coll).charAt(n)); 1.733 + else if(coll.getClass().isArray()) 1.734 + return Reflector.prepRet(Array.get(coll, n)); 1.735 + else if(coll instanceof RandomAccess) 1.736 + return ((List) coll).get(n); 1.737 + else if(coll instanceof Matcher) 1.738 + return ((Matcher) coll).group(n); 1.739 + 1.740 + else if(coll instanceof Map.Entry) { 1.741 + Map.Entry e = (Map.Entry) coll; 1.742 + if(n == 0) 1.743 + return e.getKey(); 1.744 + else if(n == 1) 1.745 + return e.getValue(); 1.746 + throw new IndexOutOfBoundsException(); 1.747 + } 1.748 + 1.749 + else if(coll instanceof Sequential) { 1.750 + ISeq seq = RT.seq(coll); 1.751 + coll = null; 1.752 + for(int i = 0; i <= n && seq != null; ++i, seq = seq.next()) { 1.753 + if(i == n) 1.754 + return seq.first(); 1.755 + } 1.756 + throw new IndexOutOfBoundsException(); 1.757 + } 1.758 + else 1.759 + throw new UnsupportedOperationException( 1.760 + "nth not supported on this type: " + coll.getClass().getSimpleName()); 1.761 +} 1.762 + 1.763 +static public Object nth(Object coll, int n, Object notFound){ 1.764 + if(coll instanceof Indexed) { 1.765 + Indexed v = (Indexed) coll; 1.766 + return v.nth(n, notFound); 1.767 + } 1.768 + return nthFrom(coll, n, notFound); 1.769 +} 1.770 + 1.771 +static Object nthFrom(Object coll, int n, Object notFound){ 1.772 + if(coll == null) 1.773 + return notFound; 1.774 + else if(n < 0) 1.775 + return notFound; 1.776 + 1.777 + else if(coll instanceof CharSequence) { 1.778 + CharSequence s = (CharSequence) coll; 1.779 + if(n < s.length()) 1.780 + return Character.valueOf(s.charAt(n)); 1.781 + return notFound; 1.782 + } 1.783 + else if(coll.getClass().isArray()) { 1.784 + if(n < Array.getLength(coll)) 1.785 + return Reflector.prepRet(Array.get(coll, n)); 1.786 + return notFound; 1.787 + } 1.788 + else if(coll instanceof RandomAccess) { 1.789 + List list = (List) coll; 1.790 + if(n < list.size()) 1.791 + return list.get(n); 1.792 + return notFound; 1.793 + } 1.794 + else if(coll instanceof Matcher) { 1.795 + Matcher m = (Matcher) coll; 1.796 + if(n < m.groupCount()) 1.797 + return m.group(n); 1.798 + return notFound; 1.799 + } 1.800 + else if(coll instanceof Map.Entry) { 1.801 + Map.Entry e = (Map.Entry) coll; 1.802 + if(n == 0) 1.803 + return e.getKey(); 1.804 + else if(n == 1) 1.805 + return e.getValue(); 1.806 + return notFound; 1.807 + } 1.808 + else if(coll instanceof Sequential) { 1.809 + ISeq seq = RT.seq(coll); 1.810 + coll = null; 1.811 + for(int i = 0; i <= n && seq != null; ++i, seq = seq.next()) { 1.812 + if(i == n) 1.813 + return seq.first(); 1.814 + } 1.815 + return notFound; 1.816 + } 1.817 + else 1.818 + throw new UnsupportedOperationException( 1.819 + "nth not supported on this type: " + coll.getClass().getSimpleName()); 1.820 +} 1.821 + 1.822 +static public Object assocN(int n, Object val, Object coll){ 1.823 + if(coll == null) 1.824 + return null; 1.825 + else if(coll instanceof IPersistentVector) 1.826 + return ((IPersistentVector) coll).assocN(n, val); 1.827 + else if(coll instanceof Object[]) { 1.828 + //hmm... this is not persistent 1.829 + Object[] array = ((Object[]) coll); 1.830 + array[n] = val; 1.831 + return array; 1.832 + } 1.833 + else 1.834 + return null; 1.835 +} 1.836 + 1.837 +static boolean hasTag(Object o, Object tag){ 1.838 + return Util.equals(tag, RT.get(RT.meta(o), TAG_KEY)); 1.839 +} 1.840 + 1.841 +/** 1.842 + * ********************* Boxing/casts ****************************** 1.843 + */ 1.844 +static public Object box(Object x){ 1.845 + return x; 1.846 +} 1.847 + 1.848 +static public Character box(char x){ 1.849 + return Character.valueOf(x); 1.850 +} 1.851 + 1.852 +static public Object box(boolean x){ 1.853 + return x ? T : F; 1.854 +} 1.855 + 1.856 +static public Object box(Boolean x){ 1.857 + return x;// ? T : null; 1.858 +} 1.859 + 1.860 +static public Number box(byte x){ 1.861 + return x;//Num.from(x); 1.862 +} 1.863 + 1.864 +static public Number box(short x){ 1.865 + return x;//Num.from(x); 1.866 +} 1.867 + 1.868 +static public Number box(int x){ 1.869 + return x;//Num.from(x); 1.870 +} 1.871 + 1.872 +static public Number box(long x){ 1.873 + return x;//Num.from(x); 1.874 +} 1.875 + 1.876 +static public Number box(float x){ 1.877 + return x;//Num.from(x); 1.878 +} 1.879 + 1.880 +static public Number box(double x){ 1.881 + return x;//Num.from(x); 1.882 +} 1.883 + 1.884 +static public char charCast(Object x){ 1.885 + if(x instanceof Character) 1.886 + return ((Character) x).charValue(); 1.887 + 1.888 + long n = ((Number) x).longValue(); 1.889 + if(n < Character.MIN_VALUE || n > Character.MAX_VALUE) 1.890 + throw new IllegalArgumentException("Value out of range for char: " + x); 1.891 + 1.892 + return (char) n; 1.893 +} 1.894 + 1.895 +static public boolean booleanCast(Object x){ 1.896 + if(x instanceof Boolean) 1.897 + return ((Boolean) x).booleanValue(); 1.898 + return x != null; 1.899 +} 1.900 + 1.901 +static public boolean booleanCast(boolean x){ 1.902 + return x; 1.903 +} 1.904 + 1.905 +static public byte byteCast(Object x){ 1.906 + long n = ((Number) x).longValue(); 1.907 + if(n < Byte.MIN_VALUE || n > Byte.MAX_VALUE) 1.908 + throw new IllegalArgumentException("Value out of range for byte: " + x); 1.909 + 1.910 + return (byte) n; 1.911 +} 1.912 + 1.913 +static public short shortCast(Object x){ 1.914 + long n = ((Number) x).longValue(); 1.915 + if(n < Short.MIN_VALUE || n > Short.MAX_VALUE) 1.916 + throw new IllegalArgumentException("Value out of range for short: " + x); 1.917 + 1.918 + return (short) n; 1.919 +} 1.920 + 1.921 +static public int intCast(Object x){ 1.922 + if(x instanceof Integer) 1.923 + return ((Integer)x).intValue(); 1.924 + if(x instanceof Number) 1.925 + return intCast(((Number) x).longValue()); 1.926 + return ((Character) x).charValue(); 1.927 +} 1.928 + 1.929 +static public int intCast(char x){ 1.930 + return x; 1.931 +} 1.932 + 1.933 +static public int intCast(byte x){ 1.934 + return x; 1.935 +} 1.936 + 1.937 +static public int intCast(short x){ 1.938 + return x; 1.939 +} 1.940 + 1.941 +static public int intCast(int x){ 1.942 + return x; 1.943 +} 1.944 + 1.945 +static public int intCast(float x){ 1.946 + if(x < Integer.MIN_VALUE || x > Integer.MAX_VALUE) 1.947 + throw new IllegalArgumentException("Value out of range for int: " + x); 1.948 + return (int) x; 1.949 +} 1.950 + 1.951 +static public int intCast(long x){ 1.952 + if(x < Integer.MIN_VALUE || x > Integer.MAX_VALUE) 1.953 + throw new IllegalArgumentException("Value out of range for int: " + x); 1.954 + return (int) x; 1.955 +} 1.956 + 1.957 +static public int intCast(double x){ 1.958 + if(x < Integer.MIN_VALUE || x > Integer.MAX_VALUE) 1.959 + throw new IllegalArgumentException("Value out of range for int: " + x); 1.960 + return (int) x; 1.961 +} 1.962 + 1.963 +static public long longCast(Object x){ 1.964 + return ((Number) x).longValue(); 1.965 +} 1.966 + 1.967 +static public long longCast(int x){ 1.968 + return x; 1.969 +} 1.970 + 1.971 +static public long longCast(float x){ 1.972 + if(x < Long.MIN_VALUE || x > Long.MAX_VALUE) 1.973 + throw new IllegalArgumentException("Value out of range for long: " + x); 1.974 + return (long) x; 1.975 +} 1.976 + 1.977 +static public long longCast(long x){ 1.978 + return x; 1.979 +} 1.980 + 1.981 +static public long longCast(double x){ 1.982 + if(x < Long.MIN_VALUE || x > Long.MAX_VALUE) 1.983 + throw new IllegalArgumentException("Value out of range for long: " + x); 1.984 + return (long) x; 1.985 +} 1.986 + 1.987 +static public float floatCast(Object x){ 1.988 + if(x instanceof Float) 1.989 + return ((Float) x).floatValue(); 1.990 + 1.991 + double n = ((Number) x).doubleValue(); 1.992 + if(n < -Float.MAX_VALUE || n > Float.MAX_VALUE) 1.993 + throw new IllegalArgumentException("Value out of range for float: " + x); 1.994 + 1.995 + return (float) n; 1.996 + 1.997 +} 1.998 + 1.999 +static public float floatCast(int x){ 1.1000 + return x; 1.1001 +} 1.1002 + 1.1003 +static public float floatCast(float x){ 1.1004 + return x; 1.1005 +} 1.1006 + 1.1007 +static public float floatCast(long x){ 1.1008 + return x; 1.1009 +} 1.1010 + 1.1011 +static public float floatCast(double x){ 1.1012 + if(x < -Float.MAX_VALUE || x > Float.MAX_VALUE) 1.1013 + throw new IllegalArgumentException("Value out of range for float: " + x); 1.1014 + 1.1015 + return (float) x; 1.1016 +} 1.1017 + 1.1018 +static public double doubleCast(Object x){ 1.1019 + return ((Number) x).doubleValue(); 1.1020 +} 1.1021 + 1.1022 +static public double doubleCast(int x){ 1.1023 + return x; 1.1024 +} 1.1025 + 1.1026 +static public double doubleCast(float x){ 1.1027 + return x; 1.1028 +} 1.1029 + 1.1030 +static public double doubleCast(long x){ 1.1031 + return x; 1.1032 +} 1.1033 + 1.1034 +static public double doubleCast(double x){ 1.1035 + return x; 1.1036 +} 1.1037 + 1.1038 +static public IPersistentMap map(Object... init){ 1.1039 + if(init == null) 1.1040 + return PersistentArrayMap.EMPTY; 1.1041 + else if(init.length <= PersistentArrayMap.HASHTABLE_THRESHOLD) 1.1042 + return PersistentArrayMap.createWithCheck(init); 1.1043 + return PersistentHashMap.createWithCheck(init); 1.1044 +} 1.1045 + 1.1046 +static public IPersistentSet set(Object... init){ 1.1047 + return PersistentHashSet.createWithCheck(init); 1.1048 +} 1.1049 + 1.1050 +static public IPersistentVector vector(Object... init){ 1.1051 + return LazilyPersistentVector.createOwning(init); 1.1052 +} 1.1053 + 1.1054 +static public IPersistentVector subvec(IPersistentVector v, int start, int end){ 1.1055 + if(end < start || start < 0 || end > v.count()) 1.1056 + throw new IndexOutOfBoundsException(); 1.1057 + if(start == end) 1.1058 + return PersistentVector.EMPTY; 1.1059 + return new APersistentVector.SubVector(null, v, start, end); 1.1060 +} 1.1061 + 1.1062 +/** 1.1063 + * **************************************** list support ******************************* 1.1064 + */ 1.1065 + 1.1066 + 1.1067 +static public ISeq list(){ 1.1068 + return null; 1.1069 +} 1.1070 + 1.1071 +static public ISeq list(Object arg1){ 1.1072 + return new PersistentList(arg1); 1.1073 +} 1.1074 + 1.1075 +static public ISeq list(Object arg1, Object arg2){ 1.1076 + return listStar(arg1, arg2, null); 1.1077 +} 1.1078 + 1.1079 +static public ISeq list(Object arg1, Object arg2, Object arg3){ 1.1080 + return listStar(arg1, arg2, arg3, null); 1.1081 +} 1.1082 + 1.1083 +static public ISeq list(Object arg1, Object arg2, Object arg3, Object arg4){ 1.1084 + return listStar(arg1, arg2, arg3, arg4, null); 1.1085 +} 1.1086 + 1.1087 +static public ISeq list(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5){ 1.1088 + return listStar(arg1, arg2, arg3, arg4, arg5, null); 1.1089 +} 1.1090 + 1.1091 +static public ISeq listStar(Object arg1, ISeq rest){ 1.1092 + return (ISeq) cons(arg1, rest); 1.1093 +} 1.1094 + 1.1095 +static public ISeq listStar(Object arg1, Object arg2, ISeq rest){ 1.1096 + return (ISeq) cons(arg1, cons(arg2, rest)); 1.1097 +} 1.1098 + 1.1099 +static public ISeq listStar(Object arg1, Object arg2, Object arg3, ISeq rest){ 1.1100 + return (ISeq) cons(arg1, cons(arg2, cons(arg3, rest))); 1.1101 +} 1.1102 + 1.1103 +static public ISeq listStar(Object arg1, Object arg2, Object arg3, Object arg4, ISeq rest){ 1.1104 + return (ISeq) cons(arg1, cons(arg2, cons(arg3, cons(arg4, rest)))); 1.1105 +} 1.1106 + 1.1107 +static public ISeq listStar(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, ISeq rest){ 1.1108 + return (ISeq) cons(arg1, cons(arg2, cons(arg3, cons(arg4, cons(arg5, rest))))); 1.1109 +} 1.1110 + 1.1111 +static public ISeq arrayToList(Object[] a) throws Exception{ 1.1112 + ISeq ret = null; 1.1113 + for(int i = a.length - 1; i >= 0; --i) 1.1114 + ret = (ISeq) cons(a[i], ret); 1.1115 + return ret; 1.1116 +} 1.1117 + 1.1118 +static public Object[] object_array(Object sizeOrSeq){ 1.1119 + if(sizeOrSeq instanceof Number) 1.1120 + return new Object[((Number) sizeOrSeq).intValue()]; 1.1121 + else 1.1122 + { 1.1123 + ISeq s = RT.seq(sizeOrSeq); 1.1124 + int size = RT.count(s); 1.1125 + Object[] ret = new Object[size]; 1.1126 + for(int i = 0; i < size && s != null; i++, s = s.next()) 1.1127 + ret[i] = s.first(); 1.1128 + return ret; 1.1129 + } 1.1130 +} 1.1131 + 1.1132 +static public Object[] toArray(Object coll) throws Exception{ 1.1133 + if(coll == null) 1.1134 + return EMPTY_ARRAY; 1.1135 + else if(coll instanceof Object[]) 1.1136 + return (Object[]) coll; 1.1137 + else if(coll instanceof Collection) 1.1138 + return ((Collection) coll).toArray(); 1.1139 + else if(coll instanceof Map) 1.1140 + return ((Map) coll).entrySet().toArray(); 1.1141 + else if(coll instanceof String) { 1.1142 + char[] chars = ((String) coll).toCharArray(); 1.1143 + Object[] ret = new Object[chars.length]; 1.1144 + for(int i = 0; i < chars.length; i++) 1.1145 + ret[i] = chars[i]; 1.1146 + return ret; 1.1147 + } 1.1148 + else if(coll.getClass().isArray()) { 1.1149 + ISeq s = (seq(coll)); 1.1150 + Object[] ret = new Object[count(s)]; 1.1151 + for(int i = 0; i < ret.length; i++, s = s.next()) 1.1152 + ret[i] = s.first(); 1.1153 + return ret; 1.1154 + } 1.1155 + else 1.1156 + throw new Exception("Unable to convert: " + coll.getClass() + " to Object[]"); 1.1157 +} 1.1158 + 1.1159 +static public Object[] seqToArray(ISeq seq){ 1.1160 + int len = length(seq); 1.1161 + Object[] ret = new Object[len]; 1.1162 + for(int i = 0; seq != null; ++i, seq = seq.next()) 1.1163 + ret[i] = seq.first(); 1.1164 + return ret; 1.1165 +} 1.1166 + 1.1167 +static public Object seqToTypedArray(ISeq seq) throws Exception{ 1.1168 + Class type = (seq != null) ? seq.first().getClass() : Object.class; 1.1169 + return seqToTypedArray(type, seq); 1.1170 +} 1.1171 + 1.1172 +static public Object seqToTypedArray(Class type, ISeq seq) throws Exception{ 1.1173 + Object ret = Array.newInstance(type, length(seq)); 1.1174 + for(int i = 0; seq != null; ++i, seq = seq.next()) 1.1175 + Array.set(ret, i, seq.first()); 1.1176 + return ret; 1.1177 +} 1.1178 + 1.1179 +static public int length(ISeq list){ 1.1180 + int i = 0; 1.1181 + for(ISeq c = list; c != null; c = c.next()) { 1.1182 + i++; 1.1183 + } 1.1184 + return i; 1.1185 +} 1.1186 + 1.1187 +static public int boundedLength(ISeq list, int limit) throws Exception{ 1.1188 + int i = 0; 1.1189 + for(ISeq c = list; c != null && i <= limit; c = c.next()) { 1.1190 + i++; 1.1191 + } 1.1192 + return i; 1.1193 +} 1.1194 + 1.1195 +///////////////////////////////// reader support //////////////////////////////// 1.1196 + 1.1197 +static Character readRet(int ret){ 1.1198 + if(ret == -1) 1.1199 + return null; 1.1200 + return box((char) ret); 1.1201 +} 1.1202 + 1.1203 +static public Character readChar(Reader r) throws Exception{ 1.1204 + int ret = r.read(); 1.1205 + return readRet(ret); 1.1206 +} 1.1207 + 1.1208 +static public Character peekChar(Reader r) throws Exception{ 1.1209 + int ret; 1.1210 + if(r instanceof PushbackReader) { 1.1211 + ret = r.read(); 1.1212 + ((PushbackReader) r).unread(ret); 1.1213 + } 1.1214 + else { 1.1215 + r.mark(1); 1.1216 + ret = r.read(); 1.1217 + r.reset(); 1.1218 + } 1.1219 + 1.1220 + return readRet(ret); 1.1221 +} 1.1222 + 1.1223 +static public int getLineNumber(Reader r){ 1.1224 + if(r instanceof LineNumberingPushbackReader) 1.1225 + return ((LineNumberingPushbackReader) r).getLineNumber(); 1.1226 + return 0; 1.1227 +} 1.1228 + 1.1229 +static public LineNumberingPushbackReader getLineNumberingReader(Reader r){ 1.1230 + if(isLineNumberingReader(r)) 1.1231 + return (LineNumberingPushbackReader) r; 1.1232 + return new LineNumberingPushbackReader(r); 1.1233 +} 1.1234 + 1.1235 +static public boolean isLineNumberingReader(Reader r){ 1.1236 + return r instanceof LineNumberingPushbackReader; 1.1237 +} 1.1238 + 1.1239 +static public String resolveClassNameInContext(String className){ 1.1240 + //todo - look up in context var 1.1241 + return className; 1.1242 +} 1.1243 + 1.1244 +static public boolean suppressRead(){ 1.1245 + //todo - look up in suppress-read var 1.1246 + return false; 1.1247 +} 1.1248 + 1.1249 +static public String printString(Object x){ 1.1250 + try { 1.1251 + StringWriter sw = new StringWriter(); 1.1252 + print(x, sw); 1.1253 + return sw.toString(); 1.1254 + } 1.1255 + catch(Exception e) { 1.1256 + throw new RuntimeException(e); 1.1257 + } 1.1258 +} 1.1259 + 1.1260 +static public Object readString(String s){ 1.1261 + PushbackReader r = new PushbackReader(new StringReader(s)); 1.1262 + try { 1.1263 + return LispReader.read(r, true, null, false); 1.1264 + } 1.1265 + catch(Exception e) { 1.1266 + throw new RuntimeException(e); 1.1267 + } 1.1268 +} 1.1269 + 1.1270 +static public void print(Object x, Writer w) throws Exception{ 1.1271 + //call multimethod 1.1272 + if(PRINT_INITIALIZED.isBound() && RT.booleanCast(PRINT_INITIALIZED.deref())) 1.1273 + PR_ON.invoke(x, w); 1.1274 +//* 1.1275 + else { 1.1276 + boolean readably = booleanCast(PRINT_READABLY.deref()); 1.1277 + if(x instanceof Obj) { 1.1278 + Obj o = (Obj) x; 1.1279 + if(RT.count(o.meta()) > 0 && 1.1280 + ((readably && booleanCast(PRINT_META.deref())) 1.1281 + || booleanCast(PRINT_DUP.deref()))) { 1.1282 + IPersistentMap meta = o.meta(); 1.1283 + w.write("#^"); 1.1284 + if(meta.count() == 1 && meta.containsKey(TAG_KEY)) 1.1285 + print(meta.valAt(TAG_KEY), w); 1.1286 + else 1.1287 + print(meta, w); 1.1288 + w.write(' '); 1.1289 + } 1.1290 + } 1.1291 + if(x == null) 1.1292 + w.write("nil"); 1.1293 + else if(x instanceof ISeq || x instanceof IPersistentList) { 1.1294 + w.write('('); 1.1295 + printInnerSeq(seq(x), w); 1.1296 + w.write(')'); 1.1297 + } 1.1298 + else if(x instanceof String) { 1.1299 + String s = (String) x; 1.1300 + if(!readably) 1.1301 + w.write(s); 1.1302 + else { 1.1303 + w.write('"'); 1.1304 + //w.write(x.toString()); 1.1305 + for(int i = 0; i < s.length(); i++) { 1.1306 + char c = s.charAt(i); 1.1307 + switch(c) { 1.1308 + case '\n': 1.1309 + w.write("\\n"); 1.1310 + break; 1.1311 + case '\t': 1.1312 + w.write("\\t"); 1.1313 + break; 1.1314 + case '\r': 1.1315 + w.write("\\r"); 1.1316 + break; 1.1317 + case '"': 1.1318 + w.write("\\\""); 1.1319 + break; 1.1320 + case '\\': 1.1321 + w.write("\\\\"); 1.1322 + break; 1.1323 + case '\f': 1.1324 + w.write("\\f"); 1.1325 + break; 1.1326 + case '\b': 1.1327 + w.write("\\b"); 1.1328 + break; 1.1329 + default: 1.1330 + w.write(c); 1.1331 + } 1.1332 + } 1.1333 + w.write('"'); 1.1334 + } 1.1335 + } 1.1336 + else if(x instanceof IPersistentMap) { 1.1337 + w.write('{'); 1.1338 + for(ISeq s = seq(x); s != null; s = s.next()) { 1.1339 + IMapEntry e = (IMapEntry) s.first(); 1.1340 + print(e.key(), w); 1.1341 + w.write(' '); 1.1342 + print(e.val(), w); 1.1343 + if(s.next() != null) 1.1344 + w.write(", "); 1.1345 + } 1.1346 + w.write('}'); 1.1347 + } 1.1348 + else if(x instanceof IPersistentVector) { 1.1349 + IPersistentVector a = (IPersistentVector) x; 1.1350 + w.write('['); 1.1351 + for(int i = 0; i < a.count(); i++) { 1.1352 + print(a.nth(i), w); 1.1353 + if(i < a.count() - 1) 1.1354 + w.write(' '); 1.1355 + } 1.1356 + w.write(']'); 1.1357 + } 1.1358 + else if(x instanceof IPersistentSet) { 1.1359 + w.write("#{"); 1.1360 + for(ISeq s = seq(x); s != null; s = s.next()) { 1.1361 + print(s.first(), w); 1.1362 + if(s.next() != null) 1.1363 + w.write(" "); 1.1364 + } 1.1365 + w.write('}'); 1.1366 + } 1.1367 + else if(x instanceof Character) { 1.1368 + char c = ((Character) x).charValue(); 1.1369 + if(!readably) 1.1370 + w.write(c); 1.1371 + else { 1.1372 + w.write('\\'); 1.1373 + switch(c) { 1.1374 + case '\n': 1.1375 + w.write("newline"); 1.1376 + break; 1.1377 + case '\t': 1.1378 + w.write("tab"); 1.1379 + break; 1.1380 + case ' ': 1.1381 + w.write("space"); 1.1382 + break; 1.1383 + case '\b': 1.1384 + w.write("backspace"); 1.1385 + break; 1.1386 + case '\f': 1.1387 + w.write("formfeed"); 1.1388 + break; 1.1389 + case '\r': 1.1390 + w.write("return"); 1.1391 + break; 1.1392 + default: 1.1393 + w.write(c); 1.1394 + } 1.1395 + } 1.1396 + } 1.1397 + else if(x instanceof Class) { 1.1398 + w.write("#="); 1.1399 + w.write(((Class) x).getName()); 1.1400 + } 1.1401 + else if(x instanceof BigDecimal && readably) { 1.1402 + w.write(x.toString()); 1.1403 + w.write('M'); 1.1404 + } 1.1405 + else if(x instanceof Var) { 1.1406 + Var v = (Var) x; 1.1407 + w.write("#=(var " + v.ns.name + "/" + v.sym + ")"); 1.1408 + } 1.1409 + else if(x instanceof Pattern) { 1.1410 + Pattern p = (Pattern) x; 1.1411 + w.write("#\"" + p.pattern() + "\""); 1.1412 + } 1.1413 + else w.write(x.toString()); 1.1414 + } 1.1415 + //*/ 1.1416 +} 1.1417 + 1.1418 +private static void printInnerSeq(ISeq x, Writer w) throws Exception{ 1.1419 + for(ISeq s = x; s != null; s = s.next()) { 1.1420 + print(s.first(), w); 1.1421 + if(s.next() != null) 1.1422 + w.write(' '); 1.1423 + } 1.1424 +} 1.1425 + 1.1426 +static public void formatAesthetic(Writer w, Object obj) throws IOException{ 1.1427 + if(obj == null) 1.1428 + w.write("null"); 1.1429 + else 1.1430 + w.write(obj.toString()); 1.1431 +} 1.1432 + 1.1433 +static public void formatStandard(Writer w, Object obj) throws IOException{ 1.1434 + if(obj == null) 1.1435 + w.write("null"); 1.1436 + else if(obj instanceof String) { 1.1437 + w.write('"'); 1.1438 + w.write((String) obj); 1.1439 + w.write('"'); 1.1440 + } 1.1441 + else if(obj instanceof Character) { 1.1442 + w.write('\\'); 1.1443 + char c = ((Character) obj).charValue(); 1.1444 + switch(c) { 1.1445 + case '\n': 1.1446 + w.write("newline"); 1.1447 + break; 1.1448 + case '\t': 1.1449 + w.write("tab"); 1.1450 + break; 1.1451 + case ' ': 1.1452 + w.write("space"); 1.1453 + break; 1.1454 + case '\b': 1.1455 + w.write("backspace"); 1.1456 + break; 1.1457 + case '\f': 1.1458 + w.write("formfeed"); 1.1459 + break; 1.1460 + default: 1.1461 + w.write(c); 1.1462 + } 1.1463 + } 1.1464 + else 1.1465 + w.write(obj.toString()); 1.1466 +} 1.1467 + 1.1468 +static public Object format(Object o, String s, Object... args) throws Exception{ 1.1469 + Writer w; 1.1470 + if(o == null) 1.1471 + w = new StringWriter(); 1.1472 + else if(Util.equals(o, T)) 1.1473 + w = (Writer) OUT.deref(); 1.1474 + else 1.1475 + w = (Writer) o; 1.1476 + doFormat(w, s, ArraySeq.create(args)); 1.1477 + if(o == null) 1.1478 + return w.toString(); 1.1479 + return null; 1.1480 +} 1.1481 + 1.1482 +static public ISeq doFormat(Writer w, String s, ISeq args) throws Exception{ 1.1483 + for(int i = 0; i < s.length();) { 1.1484 + char c = s.charAt(i++); 1.1485 + switch(Character.toLowerCase(c)) { 1.1486 + case '~': 1.1487 + char d = s.charAt(i++); 1.1488 + switch(Character.toLowerCase(d)) { 1.1489 + case '%': 1.1490 + w.write('\n'); 1.1491 + break; 1.1492 + case 't': 1.1493 + w.write('\t'); 1.1494 + break; 1.1495 + case 'a': 1.1496 + if(args == null) 1.1497 + throw new IllegalArgumentException("Missing argument"); 1.1498 + RT.formatAesthetic(w, RT.first(args)); 1.1499 + args = RT.next(args); 1.1500 + break; 1.1501 + case 's': 1.1502 + if(args == null) 1.1503 + throw new IllegalArgumentException("Missing argument"); 1.1504 + RT.formatStandard(w, RT.first(args)); 1.1505 + args = RT.next(args); 1.1506 + break; 1.1507 + case '{': 1.1508 + int j = s.indexOf("~}", i); //note - does not nest 1.1509 + if(j == -1) 1.1510 + throw new IllegalArgumentException("Missing ~}"); 1.1511 + String subs = s.substring(i, j); 1.1512 + for(ISeq sargs = RT.seq(RT.first(args)); sargs != null;) 1.1513 + sargs = doFormat(w, subs, sargs); 1.1514 + args = RT.next(args); 1.1515 + i = j + 2; //skip ~} 1.1516 + break; 1.1517 + case '^': 1.1518 + if(args == null) 1.1519 + return null; 1.1520 + break; 1.1521 + case '~': 1.1522 + w.write('~'); 1.1523 + break; 1.1524 + default: 1.1525 + throw new IllegalArgumentException("Unsupported ~ directive: " + d); 1.1526 + } 1.1527 + break; 1.1528 + default: 1.1529 + w.write(c); 1.1530 + } 1.1531 + } 1.1532 + return args; 1.1533 +} 1.1534 +///////////////////////////////// values ////////////////////////// 1.1535 + 1.1536 +static public Object[] setValues(Object... vals){ 1.1537 + //ThreadLocalData.setValues(vals); 1.1538 + if(vals.length > 0) 1.1539 + return vals;//[0]; 1.1540 + return null; 1.1541 +} 1.1542 + 1.1543 + 1.1544 +static public ClassLoader makeClassLoader(){ 1.1545 + return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction(){ 1.1546 + public Object run(){ 1.1547 + try{ 1.1548 + Var.pushThreadBindings(RT.map(USE_CONTEXT_CLASSLOADER, RT.T)); 1.1549 +// getRootClassLoader(); 1.1550 + return new DynamicClassLoader(baseLoader()); 1.1551 + } 1.1552 + finally{ 1.1553 + Var.popThreadBindings(); 1.1554 + } 1.1555 + } 1.1556 + }); 1.1557 +} 1.1558 + 1.1559 +static public ClassLoader baseLoader(){ 1.1560 + if(Compiler.LOADER.isBound()) 1.1561 + return (ClassLoader) Compiler.LOADER.deref(); 1.1562 + else if(booleanCast(USE_CONTEXT_CLASSLOADER.deref())) 1.1563 + return Thread.currentThread().getContextClassLoader(); 1.1564 + return Compiler.class.getClassLoader(); 1.1565 +} 1.1566 + 1.1567 +static public Class classForName(String name) throws ClassNotFoundException{ 1.1568 + 1.1569 + return Class.forName(name, true, baseLoader()); 1.1570 +} 1.1571 + 1.1572 +static public Class loadClassForName(String name) throws ClassNotFoundException{ 1.1573 + try 1.1574 + { 1.1575 + Class.forName(name, false, baseLoader()); 1.1576 + } 1.1577 + catch(ClassNotFoundException e) 1.1578 + { 1.1579 + return null; 1.1580 + } 1.1581 + return Class.forName(name, true, baseLoader()); 1.1582 +} 1.1583 + 1.1584 +static public float aget(float[] xs, int i){ 1.1585 + return xs[i]; 1.1586 +} 1.1587 + 1.1588 +static public float aset(float[] xs, int i, float v){ 1.1589 + xs[i] = v; 1.1590 + return v; 1.1591 +} 1.1592 + 1.1593 +static public int alength(float[] xs){ 1.1594 + return xs.length; 1.1595 +} 1.1596 + 1.1597 +static public float[] aclone(float[] xs){ 1.1598 + return xs.clone(); 1.1599 +} 1.1600 + 1.1601 +static public double aget(double[] xs, int i){ 1.1602 + return xs[i]; 1.1603 +} 1.1604 + 1.1605 +static public double aset(double[] xs, int i, double v){ 1.1606 + xs[i] = v; 1.1607 + return v; 1.1608 +} 1.1609 + 1.1610 +static public int alength(double[] xs){ 1.1611 + return xs.length; 1.1612 +} 1.1613 + 1.1614 +static public double[] aclone(double[] xs){ 1.1615 + return xs.clone(); 1.1616 +} 1.1617 + 1.1618 +static public int aget(int[] xs, int i){ 1.1619 + return xs[i]; 1.1620 +} 1.1621 + 1.1622 +static public int aset(int[] xs, int i, int v){ 1.1623 + xs[i] = v; 1.1624 + return v; 1.1625 +} 1.1626 + 1.1627 +static public int alength(int[] xs){ 1.1628 + return xs.length; 1.1629 +} 1.1630 + 1.1631 +static public int[] aclone(int[] xs){ 1.1632 + return xs.clone(); 1.1633 +} 1.1634 + 1.1635 +static public long aget(long[] xs, int i){ 1.1636 + return xs[i]; 1.1637 +} 1.1638 + 1.1639 +static public long aset(long[] xs, int i, long v){ 1.1640 + xs[i] = v; 1.1641 + return v; 1.1642 +} 1.1643 + 1.1644 +static public int alength(long[] xs){ 1.1645 + return xs.length; 1.1646 +} 1.1647 + 1.1648 +static public long[] aclone(long[] xs){ 1.1649 + return xs.clone(); 1.1650 +} 1.1651 + 1.1652 +static public char aget(char[] xs, int i){ 1.1653 + return xs[i]; 1.1654 +} 1.1655 + 1.1656 +static public char aset(char[] xs, int i, char v){ 1.1657 + xs[i] = v; 1.1658 + return v; 1.1659 +} 1.1660 + 1.1661 +static public int alength(char[] xs){ 1.1662 + return xs.length; 1.1663 +} 1.1664 + 1.1665 +static public char[] aclone(char[] xs){ 1.1666 + return xs.clone(); 1.1667 +} 1.1668 + 1.1669 +static public byte aget(byte[] xs, int i){ 1.1670 + return xs[i]; 1.1671 +} 1.1672 + 1.1673 +static public byte aset(byte[] xs, int i, byte v){ 1.1674 + xs[i] = v; 1.1675 + return v; 1.1676 +} 1.1677 + 1.1678 +static public int alength(byte[] xs){ 1.1679 + return xs.length; 1.1680 +} 1.1681 + 1.1682 +static public byte[] aclone(byte[] xs){ 1.1683 + return xs.clone(); 1.1684 +} 1.1685 + 1.1686 +static public short aget(short[] xs, int i){ 1.1687 + return xs[i]; 1.1688 +} 1.1689 + 1.1690 +static public short aset(short[] xs, int i, short v){ 1.1691 + xs[i] = v; 1.1692 + return v; 1.1693 +} 1.1694 + 1.1695 +static public int alength(short[] xs){ 1.1696 + return xs.length; 1.1697 +} 1.1698 + 1.1699 +static public short[] aclone(short[] xs){ 1.1700 + return xs.clone(); 1.1701 +} 1.1702 + 1.1703 +static public boolean aget(boolean[] xs, int i){ 1.1704 + return xs[i]; 1.1705 +} 1.1706 + 1.1707 +static public boolean aset(boolean[] xs, int i, boolean v){ 1.1708 + xs[i] = v; 1.1709 + return v; 1.1710 +} 1.1711 + 1.1712 +static public int alength(boolean[] xs){ 1.1713 + return xs.length; 1.1714 +} 1.1715 + 1.1716 +static public boolean[] aclone(boolean[] xs){ 1.1717 + return xs.clone(); 1.1718 +} 1.1719 + 1.1720 +static public Object aget(Object[] xs, int i){ 1.1721 + return xs[i]; 1.1722 +} 1.1723 + 1.1724 +static public Object aset(Object[] xs, int i, Object v){ 1.1725 + xs[i] = v; 1.1726 + return v; 1.1727 +} 1.1728 + 1.1729 +static public int alength(Object[] xs){ 1.1730 + return xs.length; 1.1731 +} 1.1732 + 1.1733 +static public Object[] aclone(Object[] xs){ 1.1734 + return xs.clone(); 1.1735 +} 1.1736 + 1.1737 + 1.1738 +}