+++ /dev/null
-Main-Class: factor.FactorInterpreter
<project name=" Factor" default="dist" basedir=".">
- <target name="init">
- <available property="jedit" classname="org.gjt.sp.jedit.jEdit" />
- </target>
+ <path id="jedit-classpath">
+ <pathelement location="${user.home}/.jedit/jars/ErrorList.jar" />
+ <pathelement location="${user.home}/.jedit/jars/SideKick.jar" />
+ <pathelement location="${user.home}/.jedit/jars/Console.jar" />
+ </path>
- <target name="compile" depends="init">
+ <target name="compile">
<javac
srcdir="."
destdir="."
includeJavaRuntime="yes"
debug="true"
optimize="true"
+ classpathref="jedit-classpath"
>
<include name="**/*.java"/>
- <exclude name="factor/jedit/*.java"/>
- </javac>
- </target>
-
- <path id="jedit-classpath">
- <pathelement location="${user.home}/.jedit/jars/ErrorList.jar" />
- <pathelement location="${user.home}/.jedit/jars/SideKick.jar" />
- <pathelement location="${user.home}/.jedit/jars/Console.jar" />
- </path>
-
- <target name="compile-jedit" depends="init" if="jedit">
- <javac
- classpathref="jedit-classpath"
- debug="true"
- deprecation="on"
- destdir="."
- optimize="true"
- srcdir=".">
- <include name="factor/jedit/*.java"/>
</javac>
</target>
- <target name="dist" depends="compile,compile-jedit">
+ <target name="dist" depends="compile">
<jar
jarfile="Factor.jar"
- manifest="Factor.manifest"
compress="true"
>
<fileset dir=".">
<include name="factor/**/*.bsh"/>
<include name="factor/**/*.txt"/>
<include name="*.xml"/>
- <include name="library/**/*.factor"/>
- <include name="library/**/*.txt"/>
- <include name="library/**/*.png"/>
- <include name="org/**/*.class"/>
- <include name="*.factor"/>
<include name="doc/**/*.html"/>
<include name="doc/**/*.png"/>
<include name="doc/*.html"/>
- <include name="Factor.manifest"/>
</fileset>
</jar>
</target>
/**
* Used to build up linked lists.
*/
-public class Cons implements PublicCloneable, FactorExternalizable
+public class Cons implements FactorExternalizable
{
- public static int COUNT;
-
public Object car;
public Object cdr;
{
this.car = car;
this.cdr = cdr;
-
- COUNT++;
- } //}}}
-
- //{{{ car() method
- public Object car(Class clas) throws Exception
- {
- return FactorJava.convertToJavaType(car,clas);
- } //}}}
-
- //{{{ cdr() method
- public Object cdr(Class clas) throws Exception
- {
- return FactorJava.convertToJavaType(cdr,clas);
} //}}}
//{{{ next() method
return (Cons)cdr;
} //}}}
- //{{{ get() method
- public Object get(int index)
- {
- return _get(index).car;
- } //}}}
-
- //{{{ _get() method
- public Cons _get(int index)
- {
- Cons iter = this;
- while(index != 0)
- {
- iter = (Cons)iter.cdr;
- index--;
- }
- return iter;
- } //}}}
-
//{{{ contains() method
public boolean contains(Object obj)
{
return size;
} //}}}
- //{{{ nappend() method
- public static Cons nappend(Cons l1, Cons l2)
- {
- if(l1 == null)
- return l2;
- if(l2 == null)
- return l1;
- Cons last = l1;
- while(last.cdr != null)
- last = last.next();
- last.cdr = l2;
- return l1;
- } //}}}
-
- //{{{ reverse() method
- public static Cons reverse(Cons list)
- {
- Cons reversed = null;
- while(list != null)
- {
- reversed = new Cons(list.car,reversed);
- list = list.next();
- }
- return reversed;
- } //}}}
-
- //{{{ assoc() method
- public static Object assoc(Cons assoc, Object key)
- {
- if(assoc == null)
- return null;
- else
- {
- Cons first = (Cons)assoc.car;
- if(FactorLib.equal(first.car,key))
- return first.cdr;
- else
- return assoc(assoc.next(),key);
- }
- } //}}}
-
//{{{ elementsToString() method
/**
* Returns a whitespace separated string of the unparseObject() of each
else
return car.hashCode();
} //}}}
-
- //{{{ clone() method
- public Object clone()
- {
- if(cdr instanceof Cons)
- return new Cons(car,((Cons)cdr).clone());
- else
- return new Cons(car,cdr);
- } //}}}
-
- //{{{ deepClone() method
- public static Cons deepClone(Cons list)
- {
- if(list == null)
- return null;
-
- Object ccar;
- if(list.car instanceof PublicCloneable)
- ccar = ((PublicCloneable)list.car).clone();
- else
- ccar = list.car;
- if(list.cdr instanceof Cons)
- {
- return new Cons(ccar,deepClone(list.next()));
- }
- else if(list.cdr == null)
- {
- return new Cons(ccar,null);
- }
- else
- {
- Object ccdr;
- if(list.cdr instanceof PublicCloneable)
- ccdr = ((PublicCloneable)list.cdr).clone();
- else
- ccdr = list.cdr;
- return new Cons(ccar,ccdr);
- }
- } //}}}
}
*/
public class FactorArray implements FactorExternalizable, PublicCloneable
{
- public Object[] stack;
+ public Object[] array;
public int top;
//{{{ FactorArray constructor
public FactorArray()
{
- stack = new Object[64];
+ array = new Object[64];
} //}}}
//{{{ FactorArray constructor
public FactorArray(int size)
{
- stack = new Object[size];
+ array = new Object[size];
} //}}}
-
+
//{{{ FactorArray constructor
public FactorArray(Cons list)
{
- this();
+ this(list == null ? 0 : list.length());
- if(list != null)
+ int i = 0;
+ while(list != null)
{
- ensurePush(list.length());
- while(list != null)
- {
- push(list.car);
- list = list.next();
- }
+ array[i++] = list.car;
+ list = list.next();
}
} //}}}
//{{{ FactorArray constructor
- public FactorArray(Object[] stack, int top)
+ public FactorArray(Object[] array, int top)
{
- this.stack = stack;
+ this.array = array;
this.top = top;
} //}}}
- //{{{ pop() method
- public Object pop(Class clas) throws Exception
- {
- return FactorJava.convertToJavaType(pop(),clas);
- } //}}}
-
- //{{{ pop() method
- public Object pop() throws FactorStackException
- {
- ensurePop(1);
- Object returnValue = stack[--top];
- stack[top] = null;
- return returnValue;
- } //}}}
-
- //{{{ peek() method
- public Object peek() throws FactorStackException
- {
- ensurePop(1);
- return stack[top - 1];
- } //}}}
-
- //{{{ ensurePop() method
- public void ensurePop(int amount) throws FactorStackException
- {
- if(amount > top)
- throw new FactorStackException(amount);
- } //}}}
-
- //{{{ push() method
- public void push(Object o)
- {
- ensurePush(1);
- stack[top++] = o;
- } //}}}
-
- //{{{ pushAll() method
- public void pushAll(Object[] array)
- {
- ensurePush(array.length);
- System.arraycopy(array,0,stack,top,array.length);
- top += array.length;
- } //}}}
-
- //{{{ ensureCapacity() method
- private void ensureCapacity(int index)
- {
- if(index >= stack.length)
- {
- Object[] newStack = new Object[index * 2 + 1];
- System.arraycopy(stack,0,newStack,0,top);
- stack = newStack;
- }
- } //}}}
-
- //{{{ ensurePush() method
- public void ensurePush(int amount)
- {
- ensureCapacity(top + amount);
- } //}}}
-
- //{{{ get() method
- public Object get(int index)
- {
- return stack[index];
- } //}}}
-
- //{{{ set() method
- public void set(Object value, int index)
- {
- ensureCapacity(index);
- if(index >= top)
- {
- for(int i = top; i < index; i++)
- stack[i] = null;
- top = index + 1;
- }
- stack[index] = value;
- } //}}}
-
- //{{{ getCapacity() method
- public int getCapacity()
- {
- return stack.length;
- } //}}}
-
//{{{ toString() method
/**
* Returns elementsToString() enclosed with [ and ].
StringBuffer buf = new StringBuffer("{ ");
for(int i = 0; i < top; i++)
{
- buf.append(FactorReader.unparseObject(stack[i]));
+ buf.append(FactorReader.unparseObject(array[i]));
buf.append(' ');
}
return buf.append("}").toString();
} //}}}
-
- //{{{ toList() method
- public Cons toList()
- {
- Cons first = null, last = null;
- for(int i = 0; i < top; i++)
- {
- Cons cons = new Cons(stack[i],null);
- if(first == null)
- first = cons;
- else
- last.cdr = cons;
- last = cons;
- }
- return first;
- } //}}}
//{{{ clone() method
public Object clone()
{
- if(stack == null)
+ if(array == null)
return new FactorArray();
else
{
- Object[] newArray = new Object[stack.length];
- System.arraycopy(stack,0,newArray,0,top);
+ Object[] newArray = new Object[array.length];
+ System.arraycopy(array,0,newArray,0,top);
return new FactorArray(newArray,top);
}
} //}}}
int hashCode = 0;
for(int i = 0; i < Math.min(top,4); i++)
{
- Object obj = stack[i];
+ Object obj = array[i];
if(obj != null)
hashCode ^= obj.hashCode();
}
return false;
for(int i = 0; i < top; i++)
{
- if(!FactorLib.equal(stack[i],a.stack[i]))
+ if(!FactorLib.equal(array[i],a.array[i]))
return false;
}
public FactorCompoundDefinition(FactorWord word, Cons definition)
{
super(word);
- fromList(definition);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- interp.call(endOfDocs);
- } //}}}
-
- //{{{ fromList() method
- public void fromList(Cons definition)
- {
this.definition = definition;
if(definition == null)
endOfDocs = null;
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
-* $Id$
-*
-* Copyright (C) 2003, 2004 Slava Pestov.
-*
-* Redistribution and use in source and binary forms, with or without
-* modification, are permitted provided that the following conditions are met:
-*
-* 1. Redistributions of source code must retain the above copyright notice,
-* this list of conditions and the following disclaimer.
-*
-* 2. Redistributions in binary form must reproduce the above copyright notice,
-* this list of conditions and the following disclaimer in the documentation
-* and/or other materials provided with the distribution.
-*
-* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-* DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-
-package factor;
-
-import factor.compiler.*;
-import factor.db.Workspace;
-import factor.db.PersistenceException;
-import java.lang.reflect.*;
-import java.io.FileOutputStream;
-import java.util.*;
-import org.objectweb.asm.*;
-
-/**
- * : name ... ;
- */
-public class FactorCompoundDefinition extends FactorWordDefinition
-{
- private static int compileCount;
-
- public Cons definition;
- private Cons endOfDocs;
-
- //{{{ FactorCompoundDefinition constructor
- /**
- * A new definition.
- */
- public FactorCompoundDefinition(FactorWord word, Cons definition,
- FactorInterpreter interp) throws PersistenceException
- {
- super(word,interp.workspace);
- fromList(definition,interp);
-
- if(interp.workspace != null)
- interp.workspace.put(this);
- } //}}}
-
- //{{{ FactorCompoundDefinition constructor
- /**
- * A blank definition, about to be unpickled.
- */
- public FactorCompoundDefinition(Workspace workspace, long id)
- {
- super(workspace,id);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- interp.call(word,endOfDocs);
- } //}}}
-
- //{{{ getClassName() method
- private static String getClassName(String name)
- {
- return FactorJava.getSanitizedName(name)
- + "_" + (compileCount++);
- } //}}}
-
- //{{{ compile() method
- /**
- * Compile the given word, returning a new word definition.
- */
- FactorWordDefinition compile(FactorInterpreter interp,
- RecursiveState recursiveCheck) throws Exception
- {
- // Each word has its own class loader
- FactorClassLoader loader = new FactorClassLoader(
- interp.workspace);
-
- StackEffect effect = getStackEffect(interp,
- new RecursiveState());
-
- if(effect.inR != 0 || effect.outR != 0)
- throw new FactorCompilerException("Compiled code cannot manipulate call stack frames");
-
- String className = getClassName(word.name);
-
- ClassWriter cw = new ClassWriter(true);
- cw.visit(ACC_PUBLIC, className,
- "factor/compiler/CompiledDefinition",
- null, null);
-
- compileConstructor(cw,className);
-
- FactorCompiler compiler = compileEval(interp,cw,loader,
- className,effect,recursiveCheck);
-
- // Generate auxiliary methods
- compiler.generateAuxiliary(cw);
-
- // Generate fields for storing literals and
- // word references
- compiler.generateFields(cw);
-
- compileToList(interp,compiler,cw);
-
- compileGetStackEffect(cw,effect);
-
- // gets the bytecode of the class, and loads it
- // dynamically
- byte[] code = cw.toByteArray();
-
- if(interp.dump)
- {
- FileOutputStream fos = new FileOutputStream(
- className + ".class");
- try
- {
- fos.write(code);
- }
- finally
- {
- fos.close();
- }
- }
-
- String javaClassName = className.replace('/','.');
- word.setCompiledInfo(compiler.loader,javaClassName);
-
- Class compiledWordClass = loader.addClass(
- javaClassName,code,0,code.length);
-
- return CompiledDefinition.create(interp,word,compiledWordClass);
- } //}}}
-
- //{{{ compileConstructor() method
- private void compileConstructor(ClassVisitor cw, String className)
- {
- // creates a MethodWriter for the constructor
- CodeVisitor mw = cw.visitMethod(ACC_PUBLIC,
- "<init>",
- "(Lfactor/FactorWord;)V",
- null, null);
- // pushes the 'this' variable
- mw.visitVarInsn(ALOAD, 0);
- // pushes the word parameter
- mw.visitVarInsn(ALOAD, 1);
- // invokes the super class constructor
- mw.visitMethodInsn(INVOKESPECIAL,
- "factor/compiler/CompiledDefinition", "<init>",
- "(Lfactor/FactorWord;)V");
- mw.visitInsn(RETURN);
- mw.visitMaxs(0,0);
- } //}}}
-
- //{{{ compileToList() method
- private void compileToList(FactorInterpreter interp,
- FactorCompiler compiler, ClassVisitor cw)
- {
- // creates a MethodWriter for the toList() method
- CodeVisitor mw = cw.visitMethod(ACC_PUBLIC,
- "toList",
- "(Lfactor/FactorInterpreter;)Lfactor/Cons;",
- null, null);
- // push unparsed string representation of this word and parse it
- compiler.generateParse(mw,toList(interp),1);
- mw.visitTypeInsn(CHECKCAST,"factor/Cons");
- mw.visitInsn(ARETURN);
- mw.visitMaxs(0,0);
- } //}}}
-
- //{{{ compileGetStackEffect() method
- private void compileGetStackEffect(ClassVisitor cw, StackEffect effect)
- {
- // creates a MethodWriter for the getStackEffect() method
- CodeVisitor mw = cw.visitMethod(ACC_PUBLIC,
- "getStackEffect",
- "(Lfactor/compiler/RecursiveState;"
- + "Lfactor/compiler/FactorCompiler;)V",
- null, null);
-
- mw.visitVarInsn(ALOAD,2);
- mw.visitTypeInsn(NEW,"factor/compiler/StackEffect");
- mw.visitInsn(DUP);
- mw.visitLdcInsn(new Integer(effect.inD));
- mw.visitLdcInsn(new Integer(effect.outD));
- mw.visitLdcInsn(new Integer(effect.inR));
- mw.visitLdcInsn(new Integer(effect.outR));
- mw.visitMethodInsn(INVOKESPECIAL,"factor/compiler/StackEffect",
- "<init>","(IIII)V");
- mw.visitMethodInsn(INVOKEVIRTUAL,"factor/compiler/FactorCompiler",
- "apply","(Lfactor/compiler/StackEffect;)V");
- mw.visitInsn(RETURN);
- mw.visitMaxs(0,0);
- } //}}}
-
- //{{{ compileEval() method
- /**
- * Write the definition of the eval() method in the compiled word.
- * Local 0 -- this
- * Local 1 -- interpreter
- */
- protected FactorCompiler compileEval(FactorInterpreter interp,
- ClassWriter cw, FactorClassLoader loader,
- String className, StackEffect effect,
- RecursiveState recursiveCheck)
- throws Exception
- {
- cw.visitField(ACC_PRIVATE | ACC_STATIC, "initialized", "Z",
- null, null);
-
- // creates a MethodWriter for the 'eval' method
- CodeVisitor mw = cw.visitMethod(ACC_PUBLIC,
- "eval", "(Lfactor/FactorInterpreter;)V",
- null, null);
-
- // eval() method calls core
- mw.visitVarInsn(ALOAD,1);
-
- compileDataStackToJVMStack(effect,mw);
-
- mw.visitMethodInsn(INVOKESTATIC,className,"core",
- effect.getCorePrototype());
-
- compileJVMStackToDataStack(effect,mw);
-
- mw.visitInsn(RETURN);
- mw.visitMaxs(0,0);
-
- // generate core
- FactorCompiler compiler = new FactorCompiler(interp,word,
- className,loader);
- compiler.init(1,effect.inD,effect.inR,"core");
- compiler.compileCore(endOfDocs,cw,effect,recursiveCheck);
-
- return compiler;
- } //}}}
-
- //{{{ compileDataStackToJVMStack() method
- private void compileDataStackToJVMStack(StackEffect effect,
- CodeVisitor mw)
- {
- if(effect.inD != 0)
- {
- mw.visitVarInsn(ALOAD,1);
- mw.visitFieldInsn(GETFIELD,
- "factor/FactorInterpreter", "datastack",
- "Lfactor/FactorArrayStack;");
-
- // ensure the stack has enough elements
- mw.visitInsn(DUP);
- mw.visitIntInsn(BIPUSH,effect.inD);
- mw.visitMethodInsn(INVOKEVIRTUAL,
- "factor/FactorArrayStack", "ensurePop",
- "(I)V");
-
- // datastack.stack -> 2
- mw.visitInsn(DUP);
- mw.visitFieldInsn(GETFIELD,
- "factor/FactorArrayStack", "stack",
- "[Ljava/lang/Object;");
- mw.visitVarInsn(ASTORE,2);
- // datastack.top-args.length -> 3
- mw.visitInsn(DUP);
- mw.visitFieldInsn(GETFIELD,
- "factor/FactorArrayStack", "top",
- "I");
- mw.visitIntInsn(BIPUSH,effect.inD);
- mw.visitInsn(ISUB);
-
- // datastack.top -= args.length
- mw.visitInsn(DUP_X1);
- mw.visitFieldInsn(PUTFIELD,
- "factor/FactorArrayStack", "top",
- "I");
-
- mw.visitVarInsn(ISTORE,3);
-
- for(int i = 0; i < effect.inD; i++)
- {
- mw.visitVarInsn(ALOAD,2);
- mw.visitVarInsn(ILOAD,3);
- mw.visitInsn(AALOAD);
- if(i != effect.inD - 1)
- mw.visitIincInsn(3,1);
- }
- }
- } //}}}
-
- //{{{ compileJVMStackToDataStack() method
- private void compileJVMStackToDataStack(StackEffect effect,
- CodeVisitor mw)
- {
- if(effect.outD == 1)
- {
- // ( datastack )
- mw.visitVarInsn(ALOAD,1);
- mw.visitFieldInsn(GETFIELD,
- "factor/FactorInterpreter", "datastack",
- "Lfactor/FactorArrayStack;");
-
- mw.visitInsn(SWAP);
- mw.visitMethodInsn(INVOKEVIRTUAL,
- "factor/FactorArrayStack", "push",
- "(Ljava/lang/Object;)V");
- }
- } //}}}
-
- //{{{ fromList() method
- public void fromList(Cons definition, FactorInterpreter interp)
- {
- this.definition = definition;
- if(definition == null)
- endOfDocs = null;
- else
- {
- endOfDocs = definition;
- while(endOfDocs != null
- && endOfDocs.car instanceof FactorDocComment)
- endOfDocs = endOfDocs.next();
- }
- } //}}}
-
- //{{{ toList() method
- public Cons toList(FactorInterpreter interp)
- {
- return definition;
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor;
-
-public class FactorDomainException extends FactorRuntimeException
-{
- public FactorDomainException(Object arg, Class clas)
- {
- super("Cannot turn " + arg + " into " + clas);
- }
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor;
-
-import factor.parser.*;
-import factor.primitives.*;
-import java.io.*;
-
-public class FactorInterpreter implements FactorObject, Runnable
-{
- public static final String VERSION = "0.69";
-
- public static final Cons DEFAULT_USE = new Cons("builtins",
- new Cons("syntax",new Cons("scratchpad",null)));
- public static final String DEFAULT_IN = "scratchpad";
-
- // command line arguments are stored here.
- public Cons args;
-
- // boot.factor sets these.
- public boolean interactive = true;
- public Throwable error;
- public boolean dump = false;
- public boolean verboseCompile = false;
- public boolean mini = false;
- // if this is false and an error occurs, bail out.
- public boolean startupDone = false;
-
- public Cons callframe;
- public FactorArray callstack = new FactorArray();
- public FactorArray datastack = new FactorArray();
- public FactorArray namestack = new FactorArray();
- public FactorArray catchstack = new FactorArray();
-
- /**
- * Maps vocabulary names to vocabularies.
- */
- public FactorNamespace vocabularies;
-
- /**
- * Vocabulary search path for interactive parser.
- */
- public Cons use = DEFAULT_USE;
-
- /**
- * Vocabulary to define new words in.
- */
- public String in = DEFAULT_IN;
-
- /**
- * Most recently defined word.
- */
- public FactorWord last;
-
- public FactorNamespace global = new FactorNamespace();
-
- private FactorNamespace interpNamespace;
-
- private Cons compiledExceptions;
-
- //{{{ main() method
- public static void main(String[] args) throws Exception
- {
- FactorInterpreter interp = new FactorInterpreter();
- interp.init(args);
- interp.run();
- } //}}}
-
- //{{{ init() method
- public void init(FactorInterpreter interp) throws Exception
- {
- this.args = interp.args;
- this.interactive = interp.interactive;
- this.dump = interp.dump;
- this.verboseCompile = interp.verboseCompile;
- this.callframe = interp.callframe;
- this.callstack = (FactorArray)interp.callstack.clone();
- this.datastack = (FactorArray)interp.datastack.clone();
- this.namestack = (FactorArray)interp.namestack.clone();
- this.catchstack = (FactorArray)interp.catchstack.clone();
- this.vocabularies = interp.vocabularies;
- this.use = interp.use;
- this.in = interp.in;
- this.last = interp.last;
- this.global = interp.global;
- this.startupDone = true;
- } //}}}
-
- //{{{ init() method
- public void init(String[] args) throws Exception
- {
- for(int i = 0; i < args.length; i++)
- {
- String arg = args[i];
- // this switch forces minimal libraries to be loaded
- if(arg.equals("-no-mini"))
- {
- mini = false;
- args[i] = null;
- }
- else if(arg.equals("-mini"))
- {
- mini = true;
- args[i] = null;
- }
- }
-
- this.args = Cons.fromArray(args);
-
- vocabularies = new FactorNamespace();
- initBuiltinDictionary();
- initNamespace();
- topLevel();
-
- runBootstrap();
- } //}}}
-
- //{{{ initBuiltinDictionary() method
- private void initBuiltinDictionary() throws Exception
- {
- vocabularies.setVariable("builtins",new FactorNamespace());
- vocabularies.setVariable("combinators",new FactorNamespace());
- vocabularies.setVariable("syntax",new FactorNamespace());
-
- /* comments */
- FactorWord lineComment = define("syntax","!");
- lineComment.parsing = new LineComment(lineComment,false);
- FactorWord stackComment = define("syntax","(");
- stackComment.parsing = new StackComment(stackComment);
- FactorWord docComment = define("syntax","#!");
- docComment.parsing = new LineComment(docComment,true);
-
- /* strings */
- FactorWord str = define("syntax","\"");
- str.parsing = new StringLiteral(str,true);
- FactorWord ch = define("syntax","CHAR:");
- ch.parsing = new CharLiteral(ch);
-
- /* constants */
- FactorWord t = define("syntax","t");
- t.parsing = new T(t);
- FactorWord f = define("syntax","f");
- f.parsing = new F(f);
- FactorWord complex = define("syntax","#{");
- complex.parsing = new ComplexLiteral(complex,"}");
-
- /* lists */
- FactorWord bra = define("syntax","[");
- bra.parsing = new Bra(bra);
- FactorWord ket = define("syntax","]");
- ket.parsing = new Ket(bra,ket);
- FactorWord bar = define("syntax","|");
- bar.parsing = new Bar(bar);
-
- /* vectors */
- FactorWord beginVector = define("syntax","{");
- beginVector.parsing = new BeginVector(beginVector);
- FactorWord endVector = define("syntax","}");
- endVector.parsing = new EndVector(beginVector,endVector);
-
- /* word defs */
- FactorWord def = define("syntax",":");
- def.parsing = new Def(def);
- def.getNamespace().setVariable("doc-comments",Boolean.TRUE);
- FactorWord ine = define("syntax",";");
- ine.parsing = new Ine(def,ine);
- FactorWord shuffle = define("syntax","~<<");
- shuffle.parsing = new Shuffle(shuffle,">>~");
- FactorWord symbol = define("syntax","SYMBOL:");
- symbol.parsing = new Symbol(symbol);
-
- /* reading numbers with another base */
- FactorWord bin = define("syntax","BIN:");
- bin.parsing = new Base(bin,2);
- FactorWord oct = define("syntax","OCT:");
- oct.parsing = new Base(oct,8);
- FactorWord hex = define("syntax","HEX:");
- hex.parsing = new Base(hex,16);
-
- /* vocabulary parsing words */
- FactorWord noParsing = define("syntax","POSTPONE:");
- noParsing.parsing = new NoParsing(noParsing);
- FactorWord defer = define("syntax","DEFER:");
- defer.parsing = new Defer(defer);
- FactorWord in = define("syntax","IN:");
- in.parsing = new In(in);
- FactorWord use = define("syntax","USE:");
- use.parsing = new Use(use);
-
- FactorWord pushWord = define("syntax","\\");
- pushWord.parsing = new PushWord(pushWord);
-
- FactorWord interpreterGet = define("builtins","interpreter");
- interpreterGet.def = new InterpreterGet(interpreterGet);
- interpreterGet.inline = true;
-
- // primitives used by 'expand' and 'map'
- FactorWord restack = define("builtins","restack");
- restack.def = new Restack(restack);
- FactorWord unstack = define("builtins","unstack");
- unstack.def = new Unstack(unstack);
-
- // reflection primitives
- FactorWord jinvoke = define("builtins","jinvoke");
- jinvoke.def = new JInvoke(jinvoke,false);
- jinvoke.inline = true;
- FactorWord jinvokeStatic = define("builtins","jinvoke-static");
- jinvokeStatic.def = new JInvoke(jinvokeStatic,true);
- jinvokeStatic.inline = true;
- FactorWord jnew = define("builtins","jnew");
- jnew.def = new JNew(jnew);
- jnew.inline = true;
- FactorWord jvarGet = define("builtins","jvar-get");
- jvarGet.def = new JVarGet(jvarGet);
- jvarGet.inline = true;
- FactorWord jvarGetStatic = define("builtins","jvar-static-get");
- jvarGetStatic.def = new JVarGetStatic(jvarGetStatic);
- jvarGetStatic.inline = true;
- FactorWord jvarSet = define("builtins","jvar-set");
- jvarSet.def = new JVarSet(jvarSet);
- jvarSet.inline = true;
- FactorWord jvarSetStatic = define("builtins","jvar-static-set");
- jvarSetStatic.def = new JVarSetStatic(jvarSetStatic);
- jvarSetStatic.inline = true;
- FactorWord coerce = define("builtins","coerce");
- coerce.def = new Coerce(coerce);
- coerce.inline = true;
-
- // definition
- FactorWord define = define("builtins","define");
- define.def = new Define(define);
-
- // combinators
- FactorWord execute = define("words","execute");
- execute.def = new Execute(execute);
- FactorWord call = define("combinators","call");
- call.def = new Call(call);
- call.inline = true;
- FactorWord ifte = define("combinators","ifte");
- ifte.def = new Ifte(ifte);
- ifte.inline = true;
- } //}}}
-
- //{{{ initNamespace() method
- private void initNamespace() throws Exception
- {
- global.setVariable("interpreter",this);
-
- global.setVariable("verbose-compile",
- new FactorNamespace.VarBinding(
- getClass().getField("verboseCompile"),
- this));
-
- global.setVariable("startup-done",
- new FactorNamespace.VarBinding(
- getClass().getField("startupDone"),
- this));
-
- String[] boundFields = {
- "global",
- "vocabularies",
- "args",
- "dump",
- "interactive",
- "in",
- "last",
- "use"
- };
-
- for(int i = 0; i < boundFields.length; i++)
- {
- String name = boundFields[i];
- global.setVariable(name,
- new FactorNamespace.VarBinding(
- getClass().getField(name),
- this));
- }
- } //}}}
-
- //{{{ getNamespace() method
- public FactorNamespace getNamespace()
- throws Exception
- {
- if(interpNamespace == null)
- interpNamespace = new FactorNamespace(this);
-
- return interpNamespace;
- } //}}}
-
- //{{{ runBootstrap() method
- private void runBootstrap() throws Exception
- {
- String initFile = "/library/platform/jvm/boot.factor";
- FactorReader parser = new FactorReader(
- initFile,
- new BufferedReader(
- new InputStreamReader(
- getClass().getResourceAsStream(
- initFile))),
- this);
-
- call(parser.parse());
-
- run();
- } //}}}
-
- //{{{ run() method
- /**
- * Runs the top-level loop until there is no more code to execute.
- */
- public void run()
- {
- for(;;)
- {
- try
- {
- if(callframe == null)
- {
- if(callstack.top == 0)
- break;
-
- callframe = (Cons)callstack.pop();
- continue;
- }
-
- Object eval = callframe.car;
- callframe = callframe.next();
- eval(eval);
- }
- catch(Throwable e)
- {
- if(handleError(e))
- return;
- }
- }
-
- callframe = null;
- } //}}}
-
- //{{{ handleError() method
- private boolean handleError(Throwable e)
- {
- error = FactorJava.unwrapException(e);
- if(!startupDone)
- {
- error.printStackTrace();
- topLevel();
- return true;
- }
-
- datastack.push(error);
- try
- {
- FactorWord throwWord = searchVocabulary(
- "errors","throw");
- if(throwWord == null)
- throw new NullPointerException();
- eval(throwWord);
- return false;
- }
- catch(Throwable e2)
- {
- System.err.println("Exception when calling throw:");
- e.printStackTrace();
- topLevel();
-
- return true;
- }
- } //}}}
-
- //{{{ createCompiledCallframe() method
- private Cons createCompiledCallframe(FactorWord word)
- {
- return new Cons(new FactorWord(null,"#<compiled>"),word);
- } //}}}
-
- //{{{ compiledException() method
- /**
- * Called by compiled words to give the user a meaningful call stack
- * trace in the case of an exception.
- */
- public void compiledException(FactorWord word, Throwable t)
- {
- compiledExceptions = new Cons(createCompiledCallframe(word),
- this.compiledExceptions);
- } //}}}
-
- //{{{ call() method
- /**
- * Pushes the given list of code onto the callstack.
- */
- public void call(Cons code)
- {
- // tail call optimization
- if(callframe != null)
- callstack.push(callframe);
-
- callframe = code;
- } //}}}
-
- //{{{ eval() method
- /**
- * Evaluates a word.
- */
- public void eval(Object obj) throws Exception
- {
- if(obj instanceof FactorWord)
- {
- try
- {
- FactorWordDefinition d = ((FactorWord)obj).def;
- if(d == null)
- {
- throw new FactorUndefinedWordException(
- ((FactorWord)obj).name);
- }
- else
- d.eval(this);
- }
- catch(Exception e)
- {
- callstack.push(callframe);
- /* callframe = createCompiledCallframe(
- (FactorWord)obj); */
- while(compiledExceptions != null)
- {
- callstack.push(compiledExceptions.car);
- compiledExceptions = compiledExceptions
- .next();
- }
- throw e;
- }
- }
- else
- datastack.push(obj);
- } //}}}
-
- //{{{ getVariable() method
- /**
- * Return the value of a variable, by searching the namestack
- * in order.
- */
- public Object getVariable(String name) throws Exception
- {
- for(int i = namestack.top - 1; i >= 0; i--)
- {
- FactorNamespace namespace = FactorJava.toNamespace(
- namestack.stack[i]);
- if(namespace.isDefined(name))
- return namespace.getVariable(name);
- }
-
- return null;
- } //}}}
-
- //{{{ isUninterned() method
- /**
- * Words whose name begin with #: but are not #: themselves are not
- * in any vocabulary.
- */
- public static boolean isUninterned(String name)
- {
- return (name.startsWith("#:") && name.length() > 2);
- } //}}}
-
- //{{{ getVocabulary() method
- public FactorNamespace getVocabulary(String name)
- {
- Object value = vocabularies.getVariable(name);
- if(value instanceof FactorNamespace)
- return (FactorNamespace)value;
- else
- return null;
- } //}}}
-
- //{{{ defineVocabulary() method
- public void defineVocabulary(String name)
- {
- Object value = vocabularies.getVariable(name);
- if(value == null)
- {
- value = new FactorNamespace();
- vocabularies.setVariable(name,value);
- }
- } //}}}
-
- //{{{ searchVocabulary() method
- /**
- * Search in the given vocabulary for the given word.
- */
- public FactorWord searchVocabulary(String vname, String name)
- {
- if(isUninterned(name))
- return new FactorWord(null,name);
-
- try
- {
- FactorNamespace v = getVocabulary(vname);
- if(v != null)
- {
- Object word = v.getVariable(name);
- if(word instanceof FactorWord)
- return (FactorWord)word;
- else
- return null;
- }
- else
- return null;
- }
- catch(Exception e)
- {
- // should not happen!
- throw new RuntimeException(e);
- }
- } //}}}
-
- //{{{ searchVocabulary() method
- /**
- * Search through the given vocabulary list for the given word.
- */
- public FactorWord searchVocabulary(Cons vocabulary, String name)
- {
- if(isUninterned(name))
- return new FactorWord(null,name);
-
- while(vocabulary != null)
- {
- FactorWord word = searchVocabulary(
- (String)vocabulary.car,name);
- if(word != null)
- return word;
-
- vocabulary = vocabulary.next();
- }
-
- return null;
- } //}}}
-
- //{{{ define() method
- /**
- * Define a word in the given vocabulary if it doesn't exist already.
- */
- public FactorWord define(String vocabulary, String name)
- {
- if(isUninterned(name))
- return new FactorWord(null,name);
-
- FactorNamespace v = getVocabulary(vocabulary);
- if(v == null)
- {
- v = new FactorNamespace();
- vocabularies.setVariable(vocabulary,v);
- }
- Object value = v.getVariable(name);
- if(value instanceof FactorWord)
- return (FactorWord)value;
- else
- {
- // save to same workspace as vocabulary,
- // or no workspace if vocabulary is builtins
- FactorWord word = new FactorWord(
- vocabulary,name,null);
- v.setVariable(name,word);
- return word;
- }
- } //}}}
-
- //{{{ topLevel() method
- /**
- * Returns the parser to the top level context.
- */
- public void topLevel()
- {
- callstack.top = 0;
- datastack.top = 0;
- namestack.top = 0;
- namestack.push(global);
- catchstack.top = 0;
- // DEFER: the word
- define("kernel","toplevel");
- define("errors","default-error-handler");
- catchstack.push(new Cons(searchVocabulary("errors",
- "default-error-handler"),
- new Cons(searchVocabulary("kernel","toplevel"),
- null)));
- callframe = null;
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor;
-
-import factor.math.*;
-import java.io.UnsupportedEncodingException;
-import java.lang.reflect.*;
-import java.math.BigInteger;
-
-/**
- * A few methods for converting between Java types at runtime.
- * Note that the compiler incorporates calls to some of these methods in
- * generated bytecode.
- */
-public class FactorJava
-{
- public static final Class[] EMPTY_ARRAY = new Class[0];
-
- //{{{ getSanitizedName() method
- public static String getSanitizedName(String name)
- {
- StringBuffer sanitizedName = new StringBuffer();
- for(int i = 0; i < name.length(); i++)
- {
- char ch = name.charAt(i);
- if(!Character.isJavaIdentifierStart(ch))
- sanitizedName.append("_");
- else
- sanitizedName.append(ch);
- }
- return sanitizedName.toString();
- } //}}}
-
- //{{{ classNameToClassList() method
- public static Class[] classNameToClassList(Cons classes)
- throws Exception
- {
- if(classes == null)
- return EMPTY_ARRAY;
-
- Class[] _classes = new Class[classes.length()];
- int i = 0;
- while(classes != null)
- {
- _classes[i] = toClass(classes.car);
- i++;
- classes = classes.next();
- }
-
- return _classes;
- } //}}}
-
- //{{{ toNumber() method
- public static Number toNumber(Object arg)
- throws FactorDomainException
- {
- if(arg instanceof Number)
- return (Number)arg;
- else if(arg instanceof Character)
- return new Integer((int)((Character)arg).charValue());
- else if(arg instanceof String)
- {
- Number num = NumberParser.parseNumber((String)arg,10);
- if(num != null)
- return num;
- }
-
- throw new FactorDomainException(arg,Number.class);
- } //}}}
-
- //{{{ toString() method
- public static String toString(Object arg)
- {
- if(arg == null)
- return null;
- else if(arg instanceof String)
- return (String)arg;
- else
- return String.valueOf(arg);
- } //}}}
-
- //{{{ toCharSequence() method
- public static CharSequence toCharSequence(Object arg)
- {
- if(arg instanceof CharSequence)
- return ((CharSequence)arg);
- else
- return toString(arg);
- } //}}}
-
- //{{{ toBoolean() method
- public static boolean toBoolean(Object arg)
- {
- if(Boolean.FALSE.equals(arg) || arg == null)
- return false;
- else
- return true;
- } //}}}
-
- //{{{ toByte() method
- public static byte toByte(Object arg)
- throws FactorDomainException
- {
- if(arg instanceof Number && !(arg instanceof Complex))
- return ((Number)arg).byteValue();
- else if(arg instanceof Character)
- return (byte)(((Character)arg).charValue());
- else if(arg instanceof String)
- return Byte.parseByte((String)arg);
- else
- throw new FactorDomainException(arg,byte.class);
- } //}}}
-
- //{{{ toShort() method
- public static short toShort(Object arg)
- throws FactorDomainException
- {
- if(arg instanceof Number && !(arg instanceof Complex))
- return ((Number)arg).shortValue();
- else if(arg instanceof Character)
- return (short)(((Character)arg).charValue());
- else if(arg instanceof String)
- return Short.parseShort((String)arg);
- else
- throw new FactorDomainException(arg,short.class);
- } //}}}
-
- //{{{ toChar() method
- public static char toChar(Object arg)
- throws FactorDomainException
- {
- if(arg instanceof Character)
- return ((Character)arg).charValue();
- else if(arg == null)
- return '\0';
- else if(arg instanceof String)
- {
- String s = (String)arg;
- if(s.length() != 1)
- throw new FactorDomainException(arg,char.class);
- return s.charAt(0);
- }
- else if(arg instanceof Number)
- {
- return (char)((Number)arg).intValue();
- }
- else
- throw new FactorDomainException(arg,char.class);
- } //}}}
-
- //{{{ toInt() method
- public static int toInt(Object arg)
- throws FactorDomainException
- {
- if(arg instanceof Number && !(arg instanceof Complex))
- return ((Number)arg).intValue();
- else if(arg instanceof Character)
- return (int)(((Character)arg).charValue());
- else if(arg instanceof String)
- return Integer.parseInt((String)arg);
- else
- throw new FactorDomainException(arg,int.class);
- } //}}}
-
- //{{{ toLong() method
- public static long toLong(Object arg)
- throws FactorDomainException
- {
- if(arg instanceof Number && !(arg instanceof Complex))
- return ((Number)arg).longValue();
- else if(arg instanceof Character)
- return (long)(((Character)arg).charValue());
- else if(arg instanceof String)
- return Long.parseLong((String)arg);
- else
- throw new FactorDomainException(arg,long.class);
- } //}}}
-
- //{{{ toFloat() method
- public static float toFloat(Object arg)
- throws FactorDomainException
- {
- if(arg instanceof Number && !(arg instanceof Complex))
- return ((Number)arg).floatValue();
- else if(arg instanceof Character)
- return (float)(((Character)arg).charValue());
- else if(arg instanceof String)
- return Float.parseFloat((String)arg);
- else
- throw new FactorDomainException(arg,float.class);
- } //}}}
-
- //{{{ toDouble() method
- public static double toDouble(Object arg)
- throws FactorDomainException
- {
- if(arg instanceof Number && !(arg instanceof Complex))
- return ((Number)arg).doubleValue();
- else if(arg instanceof Character)
- return (double)(((Character)arg).charValue());
- else if(arg instanceof String)
- return Double.parseDouble((String)arg);
- else
- throw new FactorDomainException(arg,double.class);
- } //}}}
-
- //{{{ toBigInteger() method
- public static BigInteger toBigInteger(Object arg)
- throws FactorDomainException
- {
- if(arg instanceof BigInteger)
- return (BigInteger)arg;
- else if(arg instanceof Character)
- {
- return BigInteger.valueOf(
- (long)(((Character)arg).charValue()));
- }
- else if(arg instanceof Number)
- return BigInteger.valueOf(((Number)arg).longValue());
- else
- throw new FactorDomainException(arg,BigInteger.class);
- } //}}}
-
- //{{{ toClass() method
- public static Class toClass(Object arg)
- throws Exception
- {
- if(arg instanceof Class)
- return (Class)arg;
- else if(arg instanceof Cons)
- {
- Cons classSpec = (Cons)arg;
- if(classSpec.cdr != null)
- {
- throw new FactorException(
- "Bad class spec: " + classSpec);
- }
- Class clazz = toClass(classSpec.car);
- if(clazz.isPrimitive())
- {
- return getClass("[" + javaClassToVMClass(clazz));
- }
- else
- {
- return getClass("[L" + clazz.getName() + ";");
- }
- }
- else
- {
- return getClass((String)
- convertToJavaType(arg,String.class));
- }
- } //}}}
-
- //{{{ toNamespace() method
- public static FactorNamespace toNamespace(Object obj) throws Exception
- {
- if(obj instanceof FactorNamespace)
- return (FactorNamespace)obj;
- else if(obj instanceof FactorObject)
- {
- FactorNamespace ns = ((FactorObject)obj)
- .getNamespace();
- if(ns == null)
- throw new FactorRuntimeException(
- obj + " has a null"
- + " namespace");
- return ns;
- }
- else
- {
- throw new FactorDomainException(obj,
- FactorObject.class);
- }
- } //}}}
-
- //{{{ toBooleanArray() method
- public static boolean[] toBooleanArray(Object arg)
- throws FactorDomainException
- {
- if(arg == null)
- return new boolean[0];
- else if(arg instanceof Cons)
- arg = toArray(arg,Object[].class);
-
- try
- {
- boolean[] returnValue = new boolean[
- Array.getLength(arg)];
- for(int i = 0; i < returnValue.length; i++)
- {
- returnValue[i] = toBoolean(
- Array.get(arg,i));
- }
- return returnValue;
- }
- catch(IllegalArgumentException e)
- {
- throw new FactorDomainException(arg,boolean[].class);
- }
- } //}}}
-
- //{{{ toByteArray() method
- public static byte[] toByteArray(Object arg)
- throws FactorDomainException
- {
- if(arg == null)
- return new byte[0];
- else if(arg instanceof Cons)
- arg = toArray(arg,Object[].class);
- if(arg.getClass().isArray())
- {
- try
- {
- byte[] returnValue = new byte[
- Array.getLength(arg)];
- for(int i = 0; i < returnValue.length; i++)
- {
- returnValue[i] = toByte(
- Array.get(arg,i));
- }
- return returnValue;
- }
- catch(IllegalArgumentException e)
- {
- throw new FactorDomainException(arg,byte[].class);
- }
- }
- else
- {
- try
- {
- return String.valueOf(arg).getBytes("UTF8");
- }
- catch(UnsupportedEncodingException e)
- {
- throw new RuntimeException(e);
- }
- }
- } //}}}
-
- //{{{ toArray() method
- public static Object[] toArray(Object arg)
- throws FactorDomainException
- {
- return toArray(arg,Object[].class);
- } //}}}
-
- //{{{ toArray() method
- public static Object[] toArray(Object arg, Class clas)
- throws FactorDomainException
- {
- if(arg == null)
- {
- return (Object[])Array.newInstance(
- clas.getComponentType(),0);
- }
- else if(arg instanceof Cons)
- {
- Cons list = (Cons)arg;
- Object[] array = (Object[])
- Array.newInstance(
- clas.getComponentType(),
- list.length());
- list.toArray(array);
- return array;
- }
- else if(arg.getClass().isArray())
- {
- if(arg.getClass() == clas)
- return (Object[])arg;
- else
- {
- Object[] _arg = (Object[])arg;
- Object[] array = (Object[])
- Array.newInstance(
- clas.getComponentType(),
- _arg.length);
- System.arraycopy(arg,0,array,0,
- _arg.length);
- return array;
- }
- }
- else
- throw new FactorDomainException(arg,Object[].class);
- } //}}}
-
- //{{{ convertToJavaType() method
- public static Object convertToJavaType(Object arg, Class clas)
- throws Exception
- {
- if(clas.isPrimitive())
- clas = getBoxingType(clas);
-
- if(clas == Object.class)
- return arg;
- else if(clas == Number.class)
- {
- return toNumber(arg);
- }
- else if(clas == String.class)
- {
- return toString(arg);
- }
- else if(clas == CharSequence.class)
- {
- return toCharSequence(arg);
- }
- else if(clas == Boolean.class)
- {
- if(arg instanceof Boolean)
- return arg;
- else
- return Boolean.valueOf(toBoolean(arg));
- }
- else if(clas == Byte.class)
- {
- if(arg instanceof Byte)
- return arg;
- else
- return new Byte(toByte(arg));
- }
- else if(clas == Character.class)
- {
- if(arg instanceof Character)
- return arg;
- return new Character(toChar(arg));
- }
- else if(clas == Integer.class)
- {
- if(arg instanceof Integer)
- return arg;
- else
- return new Integer(toInt(arg));
- }
- else if(clas == Long.class)
- {
- if(arg instanceof Long)
- return arg;
- else
- return new Long(toLong(arg));
- }
- else if(clas == Float.class)
- {
- if(arg instanceof Float)
- return arg;
- else
- return new Float(toFloat(arg));
- }
- else if(clas == Double.class)
- {
- if(arg instanceof Double)
- return arg;
- else
- return new Double(toDouble(arg));
- }
- else if(clas == BigInteger.class)
- {
- if(arg instanceof BigInteger)
- return arg;
- else
- return toBigInteger(arg);
- }
- else if(clas == FactorNamespace.class)
- {
- if(arg instanceof FactorNamespace)
- return arg;
- else
- return toNamespace(arg);
- }
- else if(clas == Class.class)
- {
- return toClass(arg);
- }
- else if(clas.isArray())
- {
- Class comp = clas.getComponentType();
- if(!comp.isPrimitive())
- return toArray(arg,clas);
- else if(comp == boolean.class)
- return toBooleanArray(arg);
- else if(comp == byte.class)
- return toByteArray(arg);
- }
-
- if(arg != null && !clas.isInstance(arg))
- throw new FactorDomainException(arg,clas);
- else
- return arg;
- } //}}}
-
- //{{{ fromBoolean() method
- public static Object fromBoolean(boolean b)
- {
- return (b ? Boolean.TRUE : null);
- } //}}}
-
- //{{{ convertFromJavaType() method
- public static Object convertFromJavaType(Object arg)
- {
- if(Boolean.FALSE.equals(arg))
- return null;
- else
- return arg;
- } //}}}
-
- //{{{ javaClassToVMClass() method
- public static String javaClassToVMClass(Class clazz)
- {
- String name = clazz.getName();
-
- if(clazz.isArray())
- return clazz.getName().replace('.','/');
- else if(name.equals("boolean"))
- return "Z";
- else if(name.equals("byte"))
- return "B";
- else if(name.equals("char"))
- return "C";
- else if(name.equals("double"))
- return "D";
- else if(name.equals("float"))
- return "F";
- else if(name.equals("int"))
- return "I";
- else if(name.equals("long"))
- return "J";
- else if(name.equals("short"))
- return "S";
- else if(name.equals("void"))
- return "V";
- else
- return "L" + clazz.getName().replace('.','/') + ";";
- } //}}}
-
- //{{{ getBoxingType() method
- public static Class getBoxingType(Class clazz)
- {
- if(clazz == Boolean.TYPE)
- return Boolean.class;
- else if(clazz == Byte.TYPE)
- return Byte.class;
- else if(clazz == Character.TYPE)
- return Character.class;
- else if(clazz == Double.TYPE)
- return Double.class;
- else if(clazz == Float.TYPE)
- return Float.class;
- else if(clazz == Integer.TYPE)
- return Integer.class;
- else if(clazz == Long.TYPE)
- return Long.class;
- else if(clazz == Short.TYPE)
- return Short.class;
- else
- return null;
- } //}}}
-
- //{{{ javaSignatureToVMSignature() method
- public static String javaSignatureToVMSignature(Class[] args,
- Class returnType)
- {
- StringBuffer buf = new StringBuffer("(");
- for(int i = 0; i < args.length; i++)
- {
- buf.append(javaClassToVMClass(args[i]));
- }
- buf.append(")");
- buf.append(javaClassToVMClass(returnType));
- return buf.toString();
- } //}}}
-
- //{{{ getClass() method
- public static Class getClass(String name) throws ClassNotFoundException
- {
- if(name.equals("boolean"))
- return Boolean.TYPE;
- else if(name.equals("byte"))
- return Byte.TYPE;
- else if(name.equals("char"))
- return Character.TYPE;
- else if(name.equals("double"))
- return Double.TYPE;
- else if(name.equals("float"))
- return Float.TYPE;
- else if(name.equals("int"))
- return Integer.TYPE;
- else if(name.equals("long"))
- return Long.TYPE;
- else if(name.equals("short"))
- return Short.TYPE;
- else
- return Class.forName(name);
- } //}}}
-
- //{{{ unwrapException() method
- public static Throwable unwrapException(Throwable e)
- {
- if(e instanceof InvocationTargetException)
- {
- return unwrapException(
- ((InvocationTargetException)e)
- .getTargetException());
- }
- else if(e.getCause() != null)
- return unwrapException(e.getCause());
- else
- return e;
- } //}}}
-}
*/
public class FactorLib
{
- //{{{ branch3() method
- public static Object branch3(float x, float y,
- Object o1, Object o2, Object o3)
+ //{{{ toNumber() method
+ public static Number toNumber(Object arg)
{
- if(x > y)
- return o1;
- else if(x == y)
- return o2;
- else
- return o3;
- } //}}}
+ if(arg instanceof Number)
+ return (Number)arg;
+ else if(arg instanceof Character)
+ return new Integer((int)((Character)arg).charValue());
+ else if(arg instanceof String)
+ {
+ Number num = NumberParser.parseNumber((String)arg,10);
+ if(num != null)
+ return num;
+ }
- //{{{ error() method
- public static void error(Object obj) throws Throwable
- {
- if(obj instanceof Throwable)
- throw (Throwable)obj;
- else
- throw new FactorRuntimeException(String.valueOf(obj));
+ throw new NumberFormatException(String.valueOf(arg));
} //}}}
- //{{{ eq() method
- public static boolean eq(Object o1, Object o2)
- {
- return o1 == o2;
- } //}}}
-
//{{{ equal() method
public static boolean equal(Object o1, Object o2)
{
return o1.equals(o2);
} //}}}
- //{{{ exec() method
- public static int exec(String[] args, String dir) throws Exception
- {
- int exitCode = -1;
-
- try
- {
- Process process = Runtime.getRuntime().exec(args,
- null,new File(dir));
- process.getInputStream().close();
- process.getOutputStream().close();
- process.getErrorStream().close();
- exitCode = process.waitFor();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- // this needs to be handled better
- /* stack.push(MiscUtilities.throwableToString(e));
- Console.print(stack,namespace); */
- }
-
- return exitCode;
- } //}}}
-
//{{{ objectsEqual() method
/**
* Returns if two objects are equal. This correctly handles null
{
return (o1 == null ? o2 == null : o1.equals(o2));
} //}}}
-
- //{{{ copy() method
- /**
- * Copies the contents of an input stream to an output stream.
- */
- public static void copy(InputStream in, OutputStream out)
- throws IOException
- {
- try
- {
- byte[] buf = new byte[4096];
-
- int count;
-
- for(;;)
- {
- count = in.read(buf,0,buf.length);
- if(count == -1 || count == 0)
- break;
-
- out.write(buf,0,count);
- }
- }
- finally
- {
- in.close();
- out.close();
- }
- } //}}}
-
- //{{{ readLine() method
- /**
- * Reads a line of text from the given input stream.
- */
- public static String readLine(InputStream in) throws IOException
- {
- StringBuffer buf = new StringBuffer();
- int b;
- while((b = in.read()) != -1)
- {
- if(b == '\r')
- {
- if(in.markSupported()/* && in.available() >= 1 */)
- {
- in.mark(1);
- b = in.read();
- if(b != '\n')
- in.reset();
- }
- break;
- }
- else if(b == '\n')
- break;
- buf.append((char)b);
- }
-
- /* EOF? */
- if(b == -1 && buf.length() == 0)
- return null;
- else
- return buf.toString();
- } //}}}
-
- //{{{ readCount() method
- public static String readCount(int count, InputStream in)
- throws IOException
- {
- byte[] bytes = new byte[count];
- int offset = 0;
- int read = 0;
- while((read = in.read(bytes,offset,count - offset)) > 0)
- offset += read;
- return new String(bytes,0,offset,"ASCII");
- } //}}}
-
- //{{{ readCount() method
- public static String readCount(int count, Reader in)
- throws IOException
- {
- char[] chars = new char[count];
- int offset = 0;
- int read = 0;
- while((read = in.read(chars,offset,count - offset)) > 0)
- offset += read;
- return new String(chars,0,offset);
- } //}}}
}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.TreeMap;
-import java.util.Iterator;
-import java.util.Map;
-
-/**
- * A namespace is a list of name/value bindings. A namespace can optionally
- * have a bound object, in which case every public field of the
- * object will be accessible through the namespace. Additionally, static fields
- * from arbitrary classes can be imported into the namespace.
- */
-public class FactorNamespace implements PublicCloneable, FactorObject
-{
- private static FactorWord NULL = new FactorWord(null,"#<represent-f>");
- private static FactorWord CHECK_PARENT = new FactorWord(null,"#<check-parent>");
-
- public Object obj;
- protected Map words;
- private Class constraint;
-
- //{{{ createConstrainedNamespace() method
- /**
- * Used for dictionary.
- */
- public static FactorNamespace createConstrainedNamespace(
- Class constraint) throws Exception
- {
- FactorNamespace namespace = new FactorNamespace(null,null);
- namespace.constraint = constraint;
- return namespace;
- } //}}}
-
- //{{{ FactorNamespace constructor
- public FactorNamespace()
- {
- this.words = new TreeMap();
- } //}}}
-
- //{{{ FactorNamespace constructor
- public FactorNamespace(Object obj) throws Exception
- {
- this(null,obj);
- } //}}}
-
- //{{{ FactorNamespace constructor
- /**
- * Cloning constructor.
- */
- public FactorNamespace(Map words, Object obj)
- {
- this.words = new TreeMap();
-
- // used by clone()
- if(words != null)
- {
- Iterator iter = words.entrySet().iterator();
- while(iter.hasNext())
- {
- Map.Entry entry = (Map.Entry)iter.next();
- Object key = entry.getKey();
- Object value = entry.getValue();
- if(value instanceof VarBinding)
- {
- VarBinding b = (VarBinding)value;
- if(b.instance != null)
- continue;
- }
-
- this.words.put(key,value);
- }
- }
-
- this.obj = obj;
- } //}}}
-
- //{{{ getNamespace() method
- public FactorNamespace getNamespace()
- {
- return this;
- } //}}}
-
- //{{{ getThis() method
- /**
- * Returns the object bound to this namespace, or null.
- */
- public Object getThis()
- {
- return obj;
- } //}}}
-
- //{{{ importVars() method
- /**
- * Defines a variable bound to a Java field.
- */
- public synchronized void importVars(String clazz, Cons vars)
- throws Exception
- {
- Class clas = Class.forName(clazz);
- while(vars != null)
- {
- String field = (String)vars.car(String.class);
- vars = vars.next();
- String word = (String)vars.car(String.class);
-
- setVariable(word,new VarBinding(
- clas.getField(field),
- null));
-
- vars = vars.next();
- }
- } //}}}
-
- //{{{ isDefined() method
- public synchronized boolean isDefined(String name)
- {
- Object o = words.get(name);
- if(o instanceof VarBinding)
- return true;
- else if(o == NULL)
- return true;
- else if(o == CHECK_PARENT)
- return false;
- else if(o == null)
- {
- // lazily instantiate object field binding
- if(obj == null)
- return false;
- else
- {
- lazyFieldInit(name);
- return isDefined(name);
- }
- }
- else
- return true;
- } //}}}
-
- //{{{ getVariable() method
- public synchronized Object getVariable(String name)
- {
- Object o = words.get(name);
- if(o instanceof VarBinding)
- return ((VarBinding)o).get();
- else if(o == NULL)
- return null;
- else if(o == CHECK_PARENT)
- {
- // we know this is not a field binding
- return null;
- }
- else if(o == null)
- {
- // lazily instantiate object field binding
- if(obj == null)
- return null;
- else
- {
- lazyFieldInit(name);
- return getVariable(name);
- }
- }
- else
- return o;
- } //}}}
-
- //{{{ setVariable() method
- public synchronized void setVariable(String name, Object value)
- {
- if(name == null)
- throw new NullPointerException();
-
- Object o = words.get(name);
- if(o instanceof VarBinding && !(value instanceof VarBinding))
- ((VarBinding)o).set(value);
- else if(o == null)
- {
- // lazily instantiate object field binding
- if(obj == null)
- {
- if(value == null)
- words.put(name,NULL);
- else
- words.put(name,value);
- }
- else
- {
- lazyFieldInit(name);
- setVariable(name,value);
- }
- }
- else if(value == null)
- words.put(name,NULL);
- else
- {
- if(constraint != null)
- {
- if(!constraint.isAssignableFrom(
- value.getClass()))
- {
- throw new RuntimeException(
- "Can only store "
- + constraint
- + " in " + this);
- }
- }
- words.put(name,value);
- }
- } //}}}
-
- //{{{ lazyFieldInit() method
- protected void lazyFieldInit(String name)
- {
- try
- {
- Field f = obj.getClass().getField(name);
- if(!Modifier.isStatic(f.getModifiers()))
- {
- words.put(name,new VarBinding(f,obj));
- return;
- }
- }
- catch(Exception e)
- {
- }
-
- // not a field, don't check again
- words.put(name,CHECK_PARENT);
- } //}}}
-
- //{{{ initAllFields() method
- private void initAllFields()
- {
- if(obj != null)
- {
- try
- {
- Field[] fields = obj.getClass().getFields();
- for(int i = 0; i < fields.length; i++)
- {
- Field f = fields[i];
- if(Modifier.isStatic(f.getModifiers()))
- continue;
- words.put(f.getName(),
- new VarBinding(f,obj));
- }
- }
- catch(Exception e)
- {
- }
- }
- } //}}}
-
- //{{{ toVarList() method
- /**
- * Returns a list of variable names.
- */
- public synchronized Cons toVarList()
- {
- initAllFields();
-
- Cons first = null;
- Cons last = null;
- Iterator iter = words.keySet().iterator();
- while(iter.hasNext())
- {
- String key = (String)iter.next();
- if(words.get(key) != CHECK_PARENT)
- {
- Cons cons = new Cons(key,null);
- if(first == null)
- first = last = cons;
- else
- {
- last.cdr = cons;
- last = cons;
- }
- }
- }
-
- return first;
- } //}}}
-
- //{{{ toValueList() method
- /**
- * Returns a list of variable values.
- */
- public synchronized Cons toValueList()
- {
- initAllFields();
-
- Cons first = null;
- Cons last = null;
- Cons vars = toVarList();
- while(vars != null)
- {
- String key = (String)vars.car;
- Cons cons = new Cons(getVariable(key),null);
- if(first == null)
- first = last = cons;
- else
- {
- last.cdr = cons;
- last = cons;
- }
- vars = vars.next();
- }
-
- return first;
- } //}}}
-
- //{{{ toVarValueList() method
- /**
- * Returns a list of pairs of variable names, and their values.
- */
- public synchronized Cons toVarValueList()
- {
- initAllFields();
-
- Cons first = null;
- Cons last = null;
- Cons vars = toVarList();
- while(vars != null)
- {
- String key = (String)vars.car;
-
- Object value = getVariable(key);
- Cons cons = new Cons(new Cons(key,value),null);
- if(first == null)
- first = last = cons;
- else
- {
- last.cdr = cons;
- last = cons;
- }
- vars = vars.next();
- }
-
- return first;
- } //}}}
-
- //{{{ VarBinding class
- /**
- * This is messy.
- */
- public static class VarBinding
- {
- private Field field;
- private Object instance;
-
- public VarBinding(Field field, Object instance)
- throws FactorRuntimeException
- {
- this.field = field;
- this.instance = instance;
- }
-
- public Object get()
- {
- try
- {
- return FactorJava.convertFromJavaType(
- field.get(instance));
- }
- catch(Exception e)
- {
- throw new RuntimeException(e);
- }
- }
-
- public void set(Object value)
- {
- try
- {
- field.set(instance,FactorJava.convertToJavaType(
- value,field.getType()));
- }
- catch(Exception e)
- {
- throw new RuntimeException(e);
- }
- }
- } //}}}
-
- //{{{ toString() method
- public String toString()
- {
- initAllFields();
-
- String str = getClass().getName() + ", " + words.size()
- + " items";
-
- if(obj == null)
- return str;
- else
- return str + ", bound: " + obj;
- } //}}}
-
- //{{{ clone() method
- public FactorNamespace clone(Object rebind)
- {
- if(rebind.getClass() != obj.getClass())
- throw new RuntimeException("Cannot rebind to different type");
-
- try
- {
- return new FactorNamespace(words,rebind);
- }
- catch(Exception e)
- {
- throw new InternalError();
- }
- } //}}}
-
- //{{{ clone() method
- public Object clone()
- {
- if(obj != null)
- throw new RuntimeException("Cannot clone namespace that's bound to an object");
-
- try
- {
- return new FactorNamespace(new TreeMap(words),null);
- }
- catch(Exception e)
- {
- throw new InternalError();
- }
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor;
-
-public interface FactorObject
-{
- FactorNamespace getNamespace() throws Exception;
-}
*/
public abstract class FactorParsingDefinition
{
- public static final String ENCODING = "UTF8";
-
public FactorWord word;
//{{{ FactorParsingDefinition constructor
* A new definition.
*/
public FactorParsingDefinition(FactorWord word)
- throws Exception
{
this.word = word;
} //}}}
- public abstract void eval(FactorInterpreter interp, FactorReader reader)
- throws Exception;
+ public abstract void eval(FactorReader reader) throws Exception;
//{{{ toString() method
public String toString()
*/
public class FactorReader
{
- private FactorInterpreter interp;
+ private VocabularyLookup lookup;
private FactorScanner scanner;
private Cons states;
private FactorWord toplevel = new FactorWord(null,"#<EOF>");
private boolean alwaysDocComments;
- // if interactive, use interp.use & interp.in instead of below two
- private boolean interactive;
private Cons use;
private String in;
-
private int base = 10;
-
- //{{{ parseObject() method
- /**
- * Parse the given string. It must be a single literal object.
- * The object is returned.
- */
- public static Object parseObject(String input, FactorInterpreter interp)
- throws Exception
- {
- try
- {
- FactorReader parser = new FactorReader(
- "parseObject()",
- new BufferedReader(new StringReader(input)),
- true,false,interp);
- Cons parsed = parser.parse();
- if(parsed.cdr != null)
- {
- // not a single literal
- throw new FactorParseException("parseObject()",
- 1,"Not a literal: " + input);
- }
- return parsed.car;
- }
- catch(IOException io)
- {
- // can't happen!
- throw new FactorParseException("parseObject()",1,
- io.toString());
- }
- } //}}}
//{{{ getUnreadableString() method
public static String getUnreadableString(String str)
return buf.toString();
} //}}}
- //{{{ getVocabularyDeclaration() method
- /**
- * Return a string of USE: declarations for the given object.
- */
- public static String getVocabularyDeclaration(Object obj)
- {
- StringBuffer buf = new StringBuffer();
- Set vocabs = getAllVocabularies(obj);
- Iterator iter = vocabs.iterator();
- while(iter.hasNext())
- {
- String name = (String)iter.next();
- buf.append("USE: ").append(name).append('\n');
- }
- return buf.toString();
- } //}}}
-
- //{{{ getAllVocabularies() method
- /**
- * Return a set of all vocabularies referenced in the given quotation.
- */
- public static Set getAllVocabularies(Object obj)
- {
- Set set = new TreeSet();
- getAllVocabularies(obj,set);
- return set;
- } //}}}
-
- //{{{ getAllVocabularies() method
- /**
- * Return a set of all vocabularies referenced in the given quotation.
- */
- private static void getAllVocabularies(Object obj, Set set)
- {
- if(obj instanceof FactorWord)
- {
- String vocab = ((FactorWord)obj).vocabulary;
- if(vocab != null)
- set.add(vocab);
- }
- else if(obj instanceof Cons)
- {
- Cons quotation = (Cons)obj;
-
- while(quotation != null)
- {
- getAllVocabularies(quotation.car,set);
- if(quotation.car instanceof Cons)
- getAllVocabularies((Cons)quotation.car,set);
- if(quotation.cdr instanceof Cons)
- quotation = quotation.next();
- else
- {
- getAllVocabularies(quotation.cdr,set);
- return;
- }
- }
- }
- } //}}}
-
//{{{ unparseObject() method
public static String unparseObject(Object obj)
{
public FactorReader(
String filename,
BufferedReader in,
- FactorInterpreter interp)
+ VocabularyLookup lookup)
{
- this(filename,in,false,false,interp);
+ this(filename,in,false,lookup);
} //}}}
//{{{ FactorReader constructor
String filename,
BufferedReader in,
boolean alwaysDocComments,
- boolean interactive,
- FactorInterpreter interp)
+ VocabularyLookup lookup)
{
- this(new FactorScanner(filename,in),alwaysDocComments,
- interactive,interp);
+ this(new FactorScanner(filename,in),alwaysDocComments,lookup);
} //}}}
//{{{ FactorReader constructor
public FactorReader(
FactorScanner scanner,
boolean alwaysDocComments,
- boolean interactive,
- FactorInterpreter interp)
+ VocabularyLookup lookup)
{
- this.interp = interp;
+ this.lookup = lookup;
this.scanner = scanner;
pushState(toplevel,null);
this.alwaysDocComments = alwaysDocComments;
- this.interactive = interactive;
- this.in = FactorInterpreter.DEFAULT_IN;
- this.use = FactorInterpreter.DEFAULT_USE;
+ this.in = DefaultVocabularyLookup.DEFAULT_IN;
+ this.use = DefaultVocabularyLookup.DEFAULT_USE;
} //}}}
//{{{ getScanner() method
//{{{ getIn() method
public String getIn()
{
- if(interactive)
- return interp.in;
- else
- return in;
+ return in;
} //}}}
//{{{ setIn() method
- public void setIn(String in) throws Exception
+ public void setIn(String in)
{
- if(interactive)
- interp.in = in;
- else
- this.in = in;
-
- if(interp.getVocabulary(in) == null)
- interp.defineVocabulary(in);
+ this.in = in;
} //}}}
//{{{ getUse() method
public Cons getUse()
{
- if(interactive)
- return interp.use;
- else
- return use;
+ return use;
} //}}}
//{{{ setUse() method
public void setUse(Cons use)
{
- if(interactive)
- interp.use = use;
- else
- this.use = use;
+ this.use = use;
} //}}}
//{{{ addUse() method
- public void addUse(String name) throws Exception
+ public void addUse(String name)
{
- if(interp.getVocabulary(name) == null)
- error("Undefined vocabulary: " + name);
-
setUse(new Cons(name,getUse()));
} //}}}
}
} //}}}
+ //{{{ searchVocabulary() method
+ public FactorWord searchVocabulary(Cons use, String word)
+ throws Exception
+ {
+ return lookup.searchVocabulary(use,word);
+ } //}}}
+
//{{{ intern() method
public FactorWord intern(String name, boolean define)
throws Exception
{
if(define)
- return interp.define(getIn(),name);
+ return lookup.define(getIn(),name);
else
{
- FactorWord word = interp.searchVocabulary(
- getUse(),name);
+ FactorWord word = searchVocabulary(getUse(),name);
if(word == null)
error("Undefined: " + name);
return word;
if(word.parsing != null)
{
- word.parsing.eval(interp,this);
+ word.parsing.eval(this);
return false;
}
append(word);
ParseState(FactorWord start, Object arg)
{
+ docComment = start.docComment;
warnUndefined = true;
this.start = start;
this.arg = arg;
- try
- {
- this.docComment
- = (start.getNamespace()
- .getVariable("doc-comments")
- != null);
- }
- catch(Exception e)
- {
- throw new RuntimeException(e);
- }
}
void append(Object obj) throws FactorParseException
{
- boolean docComment = (this.docComment
- || alwaysDocComments);
+ boolean docComment = (this.docComment || alwaysDocComments);
// In a doc comment context, first object is always
// a word, then followed by doc comments, then followed
// by code.
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor;
-
-import java.util.*;
-
-/**
- * ~<< name ... -- >>~
- */
-public class FactorShuffleDefinition extends FactorWordDefinition
-{
- public static final int FROM_R_MASK = (1<<15);
- public static final int TO_R_MASK = (1<<16);
- public static final int SPECIFIER = FROM_R_MASK | TO_R_MASK;
-
- /**
- * Elements to consume from stacks.
- */
- private int consumeD;
- private int consumeR;
-
- /**
- * Permutation for elements on stack.
- */
- private int[] shuffleD;
- private int shuffleDstart;
- private int shuffleDlength;
- private int[] shuffleR;
- private int shuffleRstart;
- private int shuffleRlength;
-
- //{{{ FactorShuffleDefinition constructor
- public FactorShuffleDefinition(FactorWord word, Cons definition)
- throws FactorException
- {
- super(word);
- fromList(definition);
- } //}}}
-
- //{{{ FactorShuffleDefinition constructor
- public FactorShuffleDefinition(FactorWord word,
- int consumeD, int consumeR,
- int[] shuffleD, int shuffleDlength,
- int[] shuffleR, int shuffleRlength)
- {
- super(word);
-
- this.consumeD = consumeD;
- this.consumeR = consumeR;
- this.shuffleD = shuffleD;
- this.shuffleDlength = shuffleDlength;
- this.shuffleR = shuffleR;
- this.shuffleRlength = shuffleRlength;
-
- init();
- } //}}}
-
- //{{{ init() method
- private void init()
- {
- if(this.shuffleD != null && this.shuffleDlength == 0)
- this.shuffleD = null;
- if(this.shuffleR != null && this.shuffleRlength == 0)
- this.shuffleR = null;
- if(this.shuffleD != null)
- {
- for(int i = 0; i < shuffleDlength; i++)
- {
- if(shuffleD[i] == i)
- shuffleDstart++;
- else
- break;
- }
- }
- if(this.shuffleR != null)
- {
- for(int i = 0; i < shuffleRlength; i++)
- {
- if(shuffleR[i] == (i | FROM_R_MASK))
- shuffleRstart++;
- else
- break;
- }
- }
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws FactorStackException
- {
- eval(interp,interp.datastack,interp.callstack);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp,
- FactorArray datastack,
- FactorArray callstack)
- throws FactorStackException
- {
- if(datastack.top < consumeD)
- throw new FactorStackException(consumeD);
-
- if(callstack.top < consumeR)
- throw new FactorStackException(consumeR);
-
- Object[] temporaryD;
- if(shuffleD != null)
- {
- temporaryD = new Object[shuffleDlength];
- shuffle(datastack,callstack,datastack,consumeD,consumeR,
- shuffleD,temporaryD);
- }
- else
- temporaryD = null;
-
- Object[] temporaryR;
- if(shuffleR != null)
- {
- temporaryR = new Object[shuffleRlength];
- shuffle(datastack,callstack,callstack,consumeD,consumeR,
- shuffleR,temporaryR);
- }
- else
- temporaryR = null;
-
- datastack.top -= consumeD;
- if(temporaryD != null)
- datastack.pushAll(temporaryD);
-
- callstack.top -= consumeR;
- if(temporaryR != null)
- callstack.pushAll(temporaryR);
-
- } //}}}
-
- //{{{ shuffle() method
- private void shuffle(
- FactorArray datastack,
- FactorArray callstack,
- FactorArray stack,
- int consumeD,
- int consumeR,
- int[] shuffle,
- Object[] temporary)
- throws FactorStackException
- {
- for(int i = 0; i < temporary.length; i++)
- {
- Object[] array;
- int top;
- int index = shuffle[i];
- int consume;
- if((index & FROM_R_MASK) == FROM_R_MASK)
- {
- array = callstack.stack;
- top = callstack.top;
- index = (index & ~FROM_R_MASK);
- consume = consumeR;
- }
- else
- {
- array = datastack.stack;
- top = datastack.top;
- consume = consumeD;
- }
- temporary[i] = array[top - consume + index];
- }
- } //}}}
-
- //{{{ fromList() method
- public void fromList(Cons definition)
- throws FactorRuntimeException
- {
- String f = "--";
-
- // 0 in consume map is last consumed, n is first consumed.
- HashMap consumeMap = new HashMap();
-
- while(definition != null)
- {
- Object next = definition.car;
- if(f.equals(next.toString()))
- {
- definition = definition.next();
- break;
- }
- else if(next instanceof String
- || next instanceof Character)
- {
- String name = next.toString();
- int counter;
- if(name.startsWith("r:"))
- {
- counter = (FactorShuffleDefinition
- .FROM_R_MASK
- | consumeR++);
- name = name.substring(2);
- }
- else
- counter = consumeD++;
-
- Object existing = consumeMap.put(name,
- new Integer(counter));
- if(existing != null)
- throw new FactorRuntimeException(
- word + ": appears twice in shuffle LHS: "
- + next);
- }
- else if(!(next instanceof FactorDocComment))
- {
- throw new FactorRuntimeException(
- word + ": unexpected "
- + FactorReader.unparseObject(
- next));
- }
- definition = definition.next();
- }
-
- int consume = consumeMap.size();
-
- if(definition != null)
- {
- int[] shuffle = new int[definition.length()];
-
- int i = 0;
- while(definition != null)
- {
- if(definition.car instanceof String
- || definition.car instanceof Character)
- {
- String name = definition.car.toString();
- boolean r = (name.startsWith("r:"));
- if(r)
- name = name.substring(2);
-
- Integer _index = (Integer)
- consumeMap.get(name);
- if(_index == null)
- {
- throw new FactorRuntimeException(word +
- ": does not appear in shuffle LHS: "
- + definition.car);
- }
-
- int index = _index.intValue();
-
- if(r)
- {
- shuffleRlength++;
- shuffle[i++] = (index
- | FactorShuffleDefinition
- .TO_R_MASK);
- }
- else
- {
- shuffleDlength++;
- shuffle[i++] = index;
- }
- }
- else
- {
- throw new FactorRuntimeException(
- word + ": unexpected "
- + FactorReader.unparseObject(
- definition.car));
- }
- definition = definition.next();
- }
-
- shuffleD = new int[shuffleDlength];
- shuffleR = new int[shuffleRlength];
-
- int j = 0, k = 0;
- for(i = 0; i < shuffle.length; i++)
- {
- int index = shuffle[i];
- if((index & FactorShuffleDefinition.TO_R_MASK)
- == FactorShuffleDefinition.TO_R_MASK)
- {
- index = (index
- & ~FactorShuffleDefinition.TO_R_MASK);
- shuffleR[j++] = index;
- }
- else
- shuffleD[k++] = index;
- }
- }
-
- init();
- } //}}}
-
- //{{{ toList() method
- public Cons toList()
- {
- Cons list = null;
-
- for(int i = 0; i < consumeD; i++)
- list = new Cons(String.valueOf((char)('A' + i)),list);
-
- for(int i = 0; i < consumeR; i++)
- list = new Cons("r:" + (char)('A' + consumeD + i),list);
-
- list = new Cons("--",list);
-
- if(shuffleD != null)
- {
- for(int i = 0; i < shuffleDlength; i++)
- {
- int index = shuffleD[i];
- if((index & FROM_R_MASK) == FROM_R_MASK)
- {
- index &= ~FROM_R_MASK;
- index += consumeD;
- }
-
- list = new Cons(String.valueOf(
- (char)('A' + index)),
- list);
- }
- }
-
- if(shuffleR != null)
- {
- for(int i = 0; i < shuffleRlength; i++)
- {
- int index = shuffleR[i];
- if((index & FROM_R_MASK) == FROM_R_MASK)
- {
- index &= ~FROM_R_MASK;
- index += consumeD;
- }
-
- list = new Cons(
- "r:" + (char)('A' + index),
- list);
- }
- }
-
- return Cons.reverse(list);
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor;
-
-public class FactorStackException extends FactorRuntimeException
-{
- public FactorStackException(int expected)
- {
- super("Expected " + expected);
- }
-}
this.symbol = symbol;
} //}}}
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- interp.datastack.push(symbol);
- } //}}}
-
- //{{{ fromList() method
- public void fromList(Cons definition)
- {
- this.symbol = definition.car;
- } //}}}
-
//{{{ toList() method
public Cons toList()
{
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor;
-
-public class FactorUndefinedWordException extends FactorRuntimeException
-{
- public FactorUndefinedWordException(String word)
- {
- super(word + " not defined");
- }
-}
/**
* An internalized symbol.
*/
-public class FactorWord implements FactorExternalizable, FactorObject
+public class FactorWord implements FactorExternalizable
{
private static int gensymCount = 0;
public FactorParsingDefinition parsing;
/**
- * Should this word be inlined when compiling?
+ * Should the parser keep doc comments?
*/
- public boolean inline;
-
- /**
- * Raise an error if an attempt is made to compile this word?
- */
- public boolean interpretOnly;
+ public boolean docComment;
/**
* For text editor integration.
public int line;
public int col;
- private FactorNamespace namespace;
-
//{{{ FactorWord constructor
/**
* Do not use this constructor unless you're writing a packages
this.name = name;
} //}}}
- //{{{ getNamespace() method
- public FactorNamespace getNamespace()
- throws Exception
- {
- if(namespace == null)
- namespace = new FactorNamespace(this);
- return namespace;
- } //}}}
-
- //{{{ gensym() method
- /**
- * Returns an un-internalized word with a unique name.
- */
- public synchronized static FactorWord gensym()
- {
- return new FactorWord(null,"#:GENSYM:" + (gensymCount++));
- } //}}}
-
//{{{ define() method
public void define(FactorWordDefinition def)
{
this.word = word;
} //}}}
- public abstract void eval(FactorInterpreter interp)
- throws Exception;
-
- //{{{ fromList() method
- public void fromList(Cons cons)
- throws FactorRuntimeException
- {
- throw new FactorRuntimeException("Cannot unpickle " + this);
- } //}}}
-
//{{{ toList() method
public Cons toList()
{
}
catch(IOException e)
{
- /* Don't care */
- Log.log(Log.ERROR,this,e);
+ throw new RuntimeException(e);
}
dispose();
} //}}}
package factor.jedit;
-import factor.FactorInterpreter;
+import factor.*;
import java.io.*;
import java.net.Socket;
import java.util.*;
/**
* Encapsulates a connection to an external Factor instance.
*/
-public class ExternalFactor
+public class ExternalFactor extends DefaultVocabularyLookup
{
//{{{ ExternalFactor constructor
/**
* We are given two streams that point to a bare REPL.
*/
- public ExternalFactor(FactorInterpreter interp, InputStream in, OutputStream out)
+ public ExternalFactor(InputStream in, OutputStream out)
throws IOException
{
- this.interp = interp;
this.in = new DataInputStream(in);
this.out = new DataOutputStream(out);
public FactorStream openStream() throws IOException
{
Socket client = new Socket("localhost",streamServer);
- return new FactorStream(client,interp);
+ return new FactorStream(client);
} //}}}
//{{{ close() method
} //}}}
//{{{ Private members
- private FactorInterpreter interp;
private DataInputStream in;
private DataOutputStream out;
public String getLongString()
{
- return FactorWordRenderer.getWordHTMLString(
- FactorPlugin.getInterpreter(),word,def,false);
+ return FactorWordRenderer.getWordHTMLString(word,def,false);
}
}
public class FactorPlugin extends EditPlugin
{
- private static FactorInterpreter interp;
private static ExternalFactor external;
//{{{ getPluginPath() method
p.getErrorStream().close();
external = new ExternalFactor(
- getInterpreter(),
p.getInputStream(),
p.getOutputStream());
}
external = null;
}
} //}}}
-
- //{{{ newInterpreter() method
- private static FactorInterpreter newInterpreter(String[] args)
- {
- try
- {
- FactorInterpreter interp = new FactorInterpreter();
- interp.interactive = false;
- interp.init(args);
- return interp;
- }
- catch(Exception e)
- {
- System.err.println("Failed to initialize interpreter:");
- e.printStackTrace();
- return null;
- }
- } //}}}
-
- //{{{ getInterpreter() method
- /**
- * This can be called from the SideKick thread and must be thread safe.
- */
- public static synchronized FactorInterpreter getInterpreter()
- {
- if(interp == null)
- {
- interp = newInterpreter(new String[] { "-jedit", "-no-compile" });
- }
-
- return interp;
- } //}}}
//{{{ getSideKickParser() method
public static FactorSideKickParser getSideKickParser()
*/
public static List getCompletions(String word, boolean anywhere)
{
- return getCompletions(interp.vocabularies.toVarList(),word,
- anywhere);
+ try
+ {
+ return getCompletions(getExternalInstance().getVocabularies(),word,
+ anywhere);
+ }
+ catch(Exception e)
+ {
+ throw new RuntimeException(e);
+ }
} //}}}
//{{{ getCompletions() method
* @param anywhere If true, matches anywhere in the word name are
* returned; otherwise, only matches from beginning.
*/
- public static List getCompletions(Cons use,
- String word, boolean anywhere)
+ public static List getCompletions(Iterator use, String word, boolean anywhere)
{
- List completions = new ArrayList();
- FactorInterpreter interp = FactorPlugin.getInterpreter();
-
- while(use != null)
+ try
{
- String vocab = (String)use.car;
- getCompletions(interp,vocab,word,completions,anywhere);
- use = use.next();
+ List completions = new ArrayList();
+
+ while(use.hasNext())
+ {
+ String vocab = (String)use.next();
+ getExternalInstance().getCompletions(
+ vocab,word,completions,anywhere);
+ }
+
+ Collections.sort(completions,
+ new MiscUtilities.StringICaseCompare());
+
+ return completions;
+ }
+ catch(Exception e)
+ {
+ throw new RuntimeException(e);
}
-
- Collections.sort(completions,
- new MiscUtilities.StringICaseCompare());
-
- return completions;
} //}}}
-
+
//{{{ getCompletions() method
- private static void getCompletions(FactorInterpreter interp,
- String vocab, String word, List completions, boolean anywhere)
+ /**
+ * @param anywhere If true, matches anywhere in the word name are
+ * returned; otherwise, only matches from beginning.
+ */
+ public static List getCompletions(Cons use, String word, boolean anywhere)
{
- FactorNamespace v = interp.getVocabulary(vocab);
- if(v == null)
- return;
-
- Cons words = v.toValueList();
-
- while(words != null)
+ try
{
- FactorWord w = (FactorWord)words.car;
- if(w != null && w.name != null)
+ List completions = new ArrayList();
+
+ while(use != null)
{
- if(!completions.contains(w))
- {
- if(anywhere)
- {
- if(w.name.indexOf(word) != -1)
- completions.add(w);
- }
- else
- {
- if(w.name.startsWith(word))
- completions.add(w);
- }
- }
+ String vocab = (String)use.car;
+ getExternalInstance().getCompletions(
+ vocab,word,completions,anywhere);
+ use = use.next();
}
-
- words = words.next();
+
+ Collections.sort(completions,
+ new MiscUtilities.StringICaseCompare());
+
+ return completions;
+ }
+ catch(Exception e)
+ {
+ throw new RuntimeException(e);
}
} //}}}
//{{{ findAllWordsNamed() method
private static FactorWord[] findAllWordsNamed(View view, String word)
+ throws Exception
{
ArrayList words = new ArrayList();
- Cons vocabs = getInterpreter().vocabularies.toValueList();
- while(vocabs != null)
+ Iterator vocabs = getExternalInstance().getVocabularies();
+ while(vocabs.hasNext())
{
- FactorNamespace vocab = (FactorNamespace)vocabs.car;
- FactorWord w = (FactorWord)vocab.getVariable(word);
+ Map vocab = (Map)vocabs.next();
+ FactorWord w = (FactorWord)vocab.get(word);
if(w != null)
words.add(w);
- vocabs = vocabs.next();
}
- return (FactorWord[])words.toArray(
- new FactorWord[words.size()]);
+ return (FactorWord[])words.toArray(new FactorWord[words.size()]);
} //}}}
//{{{ insertUseDialog() method
public static void insertUseDialog(View view, String word)
{
- FactorWord[] words = findAllWordsNamed(view,word);
- if(words.length == 0)
- view.getToolkit().beep();
- else if(words.length == 1)
- insertUse(view,words[0].vocabulary);
- else
- new InsertUseDialog(view,getSideKickParser(),words);
+ try
+ {
+ FactorWord[] words = findAllWordsNamed(view,word);
+ if(words.length == 0)
+ view.getToolkit().beep();
+ else if(words.length == 1)
+ insertUse(view,words[0].vocabulary);
+ else
+ new InsertUseDialog(view,getSideKickParser(),words);
+ }
+ catch(Exception e)
+ {
+ throw new RuntimeException(e);
+ }
} //}}}
//{{{ insertUse() method
factor.completion.defer=DEFER: <b>{0}</b>
factor.completion.parsing=PARSING: <b>{0}</b>
factor.completion.stack=: <b>{0}</b> {1}
-factor.completion.shuffle=~<< <b>{0}</b> {1} >>~
factor.completion.symbol=SYMBOL: <b>{0}</b>
# Dialog boxes
}
catch(IOException e)
{
- /* We don't care */
- Log.log(Log.ERROR,this,e);
+ throw new RuntimeException(e);
}
stream = null;
public class FactorSideKickParser extends SideKickParser
{
- private FactorInterpreter interp;
private WordPreview wordPreview;
private Map previewMap;
public FactorSideKickParser()
{
super("factor");
- interp = FactorPlugin.getInterpreter();
previewMap = new HashMap();
worddefs = new HashMap();
} //}}}
- //{{{ getInterpreter() method
- public FactorInterpreter getInterpreter()
- {
- return interp;
- } //}}}
-
//{{{ getWordDefinition() method
/**
* Check for a word definition from a parsed source file. If one is
new BufferedReader(new StringReader(text)),
errorSource);
FactorReader r = new FactorReader(scanner,
- false,false,interp);
+ false,FactorPlugin.getExternalInstance());
Cons parsed = r.parse();
package factor.jedit;
import factor.Cons;
-import factor.FactorInterpreter;
import factor.FactorReader;
import javax.swing.text.AttributeSet;
import java.io.*;
/**
* We are given a socket that points to a bare REPL.
*/
- public FactorStream(Socket socket, FactorInterpreter interp) throws IOException
+ public FactorStream(Socket socket) throws IOException
{
- this.interp = interp;
this.socket = socket;
this.in = new DataInputStream(socket.getInputStream());
this.out = new DataOutputStream(socket.getOutputStream());
int len = in.readInt();
byte[] request = new byte[len];
in.readFully(request);
- return new WritePacket(new String(request,0,len),interp);
+ return new WritePacket(new String(request,0,len));
case -1:
return null;
default:
private Socket socket;
private DataInputStream in;
private DataOutputStream out;
- private FactorInterpreter interp;
//}}}
//{{{ Packet class
//{{{ WritePacket class
public static class WritePacket extends Packet
{
- public WritePacket(String input, FactorInterpreter interp)
+ public WritePacket(String input)
throws Exception
{
FactorReader parser = new FactorReader(
"parseObject()",
new BufferedReader(new StringReader(input)),
- true,false,interp);
+ true,FactorPlugin.getExternalInstance());
Cons pair = parser.parse();
this.write = (String)pair.car;
public class FactorWordRenderer extends DefaultListCellRenderer
{
//{{{ getWordHTMLString() method
- public static String getWordHTMLString(FactorInterpreter interp,
- FactorWord word, FactorWordDefinition def, boolean showIn)
+ public static String getWordHTMLString(FactorWord word,
+ FactorWordDefinition def, boolean showIn)
{
String prop = "factor.completion.plain";
String stackEffect = null;
else
prop = "factor.completion.defer";
}
- else if(def instanceof FactorShuffleDefinition)
- {
- prop = "factor.completion.shuffle";
- StringBuffer buf = new StringBuffer();
- Cons d = def.toList(interp);
- while(d != null)
- {
- if(buf.length() != 0)
- buf.append(' ');
-
- buf.append(d.car);
- d = d.next();
- }
- stackEffect = buf.toString();
- }
else if(def instanceof FactorSymbolDefinition)
{
prop = "factor.completion.symbol";
}
else
{
- Cons d = def.toList(interp);
+ Cons d = def.toList();
if(d != null && d.car instanceof FactorDocComment)
{
FactorDocComment comment = (FactorDocComment)
return this;
FactorWord word = (FactorWord)value;
- setText(getWordHTMLString(parser.getInterpreter(),
- word,
+ setText(getWordHTMLString(word,
parser.getWordDefinition(word),
showIn));
import factor.*;
import java.awt.event.*;
+import java.io.IOException;
import java.util.*;
import javax.swing.event.*;
import javax.swing.Timer;
return;
FactorParsedData fdata = (FactorParsedData)data;
- FactorInterpreter interp = fdata.parser
- .getInterpreter();
- FactorWord w = interp.searchVocabulary(fdata.use,word);
- if(w != null)
+
+ try
+ {
+ FactorWord w = FactorPlugin.getExternalInstance()
+ .searchVocabulary(fdata.use,word);
+ if(w != null)
+ {
+ view.getStatus().setMessageAndClear(
+ FactorWordRenderer.getWordHTMLString(
+ w,fdata.parser.getWordDefinition(w),true));
+ }
+ }
+ catch(IOException e)
{
- view.getStatus().setMessageAndClear(
- FactorWordRenderer.getWordHTMLString(
- interp,w,fdata.parser.getWordDefinition(w),true));
+ throw new RuntimeException(e);
}
}
} //}}}
* A new definition.
*/
public Bar(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws Exception
{
FactorReader.ParseState state = reader.getCurrentState();
* A new definition.
*/
public Base(FactorWord word, int base)
- throws Exception
{
super(word);
this.base = base;
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws Exception
{
- Number num = FactorJava.toNumber(
+ Number num = FactorLib.toNumber(
reader.getScanner().next(true,false,base));
reader.append(num);
}
* A new definition.
*/
public BeginVector(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
{
reader.pushState(word,null);
}
* A new definition.
*/
public Bra(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
{
reader.pushState(word,null);
}
* A new definition.
*/
public CharLiteral(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws IOException, FactorParseException
{
FactorScanner scanner = reader.getScanner();
* A new definition.
*/
public ComplexLiteral(FactorWord word, String end)
- throws Exception
{
super(word);
this.end = end;
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws Exception
{
// Read two numbers
- Number real = FactorJava.toNumber(
+ Number real = FactorLib.toNumber(
reader.nextNonEOL(true,false));
- Number imaginary = FactorJava.toNumber(
+ Number imaginary = FactorLib.toNumber(
reader.nextNonEOL(true,false));
// Read the end
* A new definition.
*/
public Def(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws Exception
{
FactorWord newWord = reader.nextWord(true);
* A new definition.
*/
public Defer(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws Exception
{
reader.nextWord(true);
* A new definition.
*/
public EndVector(FactorWord start, FactorWord end)
- throws Exception
{
super(end);
this.start = start;
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws FactorParseException
{
reader.append(new FactorArray(
* A new definition.
*/
public F(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws FactorParseException
{
reader.append(null);
* A new definition.
*/
public In(FactorWord word)
- throws Exception
{
super(word);
} //}}}
//{{{ eval() method
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws Exception
{
String next = (String)reader.nextNonEOL(false,false);
* A new definition.
*/
public Ine(FactorWord start, FactorWord end)
- throws Exception
{
super(end);
this.start = start;
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws Exception
{
FactorReader.ParseState state = reader.popState(start,word);
* A new definition.
*/
public Ket(FactorWord start, FactorWord end)
- throws Exception
{
super(end);
this.start = start;
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws FactorParseException
{
reader.append(reader.popState(start,word).first);
* A new definition.
*/
public LineComment(FactorWord word, boolean doc)
- throws Exception
{
super(word);
this.doc = doc;
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws IOException, FactorParseException
{
String comment = reader.getScanner().readUntilEOL();
* A new definition.
*/
public NoParsing(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws Exception
{
reader.append(reader.nextWord(false));
* A new definition.
*/
public PushWord(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws Exception
{
FactorWord word = reader.nextWord(false);
reader.append(new Cons(word,null));
- reader.append(interp.searchVocabulary(
+ reader.append(reader.searchVocabulary(
new Cons("lists",null),"car"));
}
}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.parser;
-
-import factor.*;
-
-public class Shuffle extends FactorParsingDefinition
-{
- private String end;
-
- //{{{ Shuffle constructor
- /**
- * A new definition.
- */
- public Shuffle(FactorWord word, String end)
- throws Exception
- {
- super(word);
- this.end = end;
- } //}}}
-
- public void eval(FactorInterpreter interp, FactorReader reader)
- throws Exception
- {
- // Read the word name
- FactorWord newWord = reader.nextWord(true);
-
- // Read the definition
- Cons first = null, last = null;
- for(;;)
- {
- String next = (String)reader.next(false,false);
- if(next.equals(end))
- break;
- Cons c = new Cons(next,null);
- if(last != null)
- {
- last.cdr = c;
- last = c;
- }
- else
- first = last = c;
- }
-
- reader.append(newWord.vocabulary);
- reader.append(newWord.name);
- reader.append(new FactorShuffleDefinition(newWord,first));
- reader.append(reader.intern("define",false));
- }
-}
* A new definition.
*/
public StackComment(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
- throws IOException, FactorParseException
+ public void eval(FactorReader reader)
+ throws Exception
{
String comment = reader.getScanner().readUntil( '(',')',false);
reader.append(new FactorDocComment(comment,true));
* A new definition.
*/
public StringLiteral(FactorWord word, boolean escapes)
- throws Exception
{
super(word);
this.escapes = escapes;
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
- throws IOException, FactorParseException
+ public void eval(FactorReader reader)
+ throws Exception
{
String literal = reader.getScanner()
.readUntil('"','"',escapes);
* A new definition.
*/
public Symbol(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws Exception
{
FactorWord w = reader.nextWord(true);
* A new definition.
*/
public T(FactorWord word)
- throws Exception
{
super(word);
} //}}}
- public void eval(FactorInterpreter interp, FactorReader reader)
- throws FactorParseException
+ public void eval(FactorReader reader)
+ throws Exception
{
reader.append(Boolean.TRUE);
}
* A new definition.
*/
public Use(FactorWord word)
- throws Exception
{
super(word);
} //}}}
//{{{ eval() method
- public void eval(FactorInterpreter interp, FactorReader reader)
+ public void eval(FactorReader reader)
throws Exception
{
String next = (String)reader.nextNonEOL(false,false);
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-import java.lang.reflect.*;
-import java.util.Map;
-
-public class Call extends FactorPrimitiveDefinition
-{
- //{{{ Call constructor
- /**
- * A new definition.
- */
- public Call(FactorWord word)
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- interp.call((Cons)interp.datastack.pop());
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-
-public class Coerce extends FactorPrimitiveDefinition
-{
- //{{{ Coerce constructor
- /**
- * A new definition.
- */
- public Coerce(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- FactorArray datastack = interp.datastack;
- Class type = FactorJava.toClass(datastack.pop());
- datastack.push(FactorJava.convertToJavaType(
- datastack.pop(),type));
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-
-public class Define extends FactorPrimitiveDefinition
-{
- //{{{ Define constructor
- /**
- * A new definition.
- */
- public Define(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- Object def = interp.datastack.pop();
- Object w = interp.datastack.pop();
- Object vocab = interp.datastack.pop();
- core(interp,vocab,w,def);
- } //}}}
-
- //{{{ core() method
- public static void core(FactorInterpreter interp,
- Object vocab, Object name, Object def)
- throws Exception
- {
- FactorWordDefinition definition = (FactorWordDefinition)def;
- FactorWord word = interp.searchVocabulary(
- FactorJava.toString(vocab),
- FactorJava.toString(name));
- word.define(definition);
- interp.last = word;
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-import java.lang.reflect.*;
-import java.util.Set;
-import org.objectweb.asm.*;
-
-public class Execute extends FactorPrimitiveDefinition
-{
- //{{{ Execute constructor
- /**
- * A new definition.
- */
- public Execute(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- interp.eval(interp.datastack.pop());
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-
-public class Ifte extends FactorPrimitiveDefinition
-{
- //{{{ Ifte constructor
- /**
- * A new definition.
- */
- public Ifte(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- FactorArray datastack = interp.datastack;
- Cons f = (Cons)datastack.pop();
- Cons t = (Cons)datastack.pop();
- Object cond = datastack.pop();
- if(FactorJava.toBoolean(cond))
- interp.call(t);
- else
- interp.call(f);
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-import java.util.Set;
-
-public class InterpreterGet extends FactorPrimitiveDefinition
-{
- //{{{ InterpreterGet constructor
- /**
- * A new definition.
- */
- public InterpreterGet(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- interp.datastack.push(interp);
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-import java.lang.reflect.*;
-import java.util.Map;
-
-public class JInvoke extends FactorPrimitiveDefinition
-{
- public boolean staticMethod;
-
- //{{{ JInvoke constructor
- /**
- * A new definition.
- */
- public JInvoke(FactorWord word, boolean staticMethod)
- throws Exception
- {
- super(word);
- this.staticMethod = staticMethod;
- } //}}}
-
- //{{{ checkStatic() method
- private void checkStatic(Method method) throws FactorRuntimeException
- {
- if(staticMethod)
- {
- if(!Modifier.isStatic(method.getModifiers()))
- throw new FactorRuntimeException(
- "Use jinvoke with static methods");
- }
- else
- {
- if(Modifier.isStatic(method.getModifiers()))
- throw new FactorRuntimeException(
- "Use jinvoke-static with static methods");
- }
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- FactorArray datastack = interp.datastack;
- String name = FactorJava.toString(datastack.pop());
- Class clazz = FactorJava.toClass(datastack.pop());
- Cons args = (Cons)datastack.pop();
- Class[] _args = FactorJava.classNameToClassList(args);
- Method method = clazz.getMethod(name,_args);
-
- checkStatic(method);
-
- Object instance;
- if(staticMethod)
- instance = null;
- else
- {
- instance = FactorJava.convertToJavaType(
- datastack.pop(),clazz);
- }
-
- Object[] params = new Object[_args.length];
-
- try
- {
- for(int i = params.length - 1; i >= 0; i--)
- {
- params[i] = FactorJava.convertToJavaType(
- datastack.pop(),_args[i]);
- }
-
- if(method.getReturnType() == Void.TYPE)
- method.invoke(instance,params);
- else
- {
- datastack.push(FactorJava.convertFromJavaType(
- method.invoke(instance,params)));
- }
- }
- catch(FactorStackException e)
- {
- throw new FactorStackException(_args.length);
- }
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-import java.lang.reflect.*;
-import java.util.Map;
-
-public class JNew extends FactorPrimitiveDefinition
-{
- //{{{ JNew constructor
- /**
- * A new definition.
- */
- public JNew(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- FactorArray datastack = interp.datastack;
- Class clazz = FactorJava.toClass(datastack.pop());
- Cons args = (Cons)datastack.pop();
- Class[] _args = FactorJava.classNameToClassList(args);
- Constructor constructor = clazz.getConstructor(_args);
-
- Object[] params = new Object[_args.length];
- for(int i = params.length - 1; i >= 0; i--)
- {
- params[i] = FactorJava.convertToJavaType(
- datastack.pop(),_args[i]);
- }
-
- datastack.push(constructor.newInstance(params));
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-import java.lang.reflect.*;
-import java.util.Map;
-
-public class JVarGet extends FactorPrimitiveDefinition
-{
- //{{{ JVarGet constructor
- /**
- * A new definition.
- */
- public JVarGet(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- FactorArray datastack = interp.datastack;
- String fieldName = FactorJava.toString(datastack.pop());
- Class clazz = FactorJava.toClass(datastack.pop());
- Field field = clazz.getField(fieldName);
- Object obj = FactorJava.convertToJavaType(datastack.pop(),
- clazz);
- datastack.push(
- FactorJava.convertFromJavaType(
- field.get(obj)));
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-import java.lang.reflect.*;
-import java.util.Map;
-
-public class JVarGetStatic extends FactorPrimitiveDefinition
-{
- //{{{ JVarGetStatic constructor
- /**
- * A new definition.
- */
- public JVarGetStatic(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- FactorArray datastack = interp.datastack;
- String fieldName = FactorJava.toString(datastack.pop());
- Class clazz = FactorJava.toClass(datastack.pop());
- Field field = clazz.getField(fieldName);
- datastack.push(
- FactorJava.convertFromJavaType(
- field.get(null)));
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-import java.lang.reflect.*;
-import java.util.Map;
-
-public class JVarSet extends FactorPrimitiveDefinition
-{
- //{{{ JVarSet constructor
- /**
- * A new definition.
- */
- public JVarSet(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- FactorArray datastack = interp.datastack;
- String fieldName = FactorJava.toString(datastack.pop());
- Class clazz = FactorJava.toClass(datastack.pop());
- Field field = clazz.getField(fieldName);
- Object obj = FactorJava.convertToJavaType(datastack.pop(),
- clazz);
- Object value = FactorJava.convertToJavaType(datastack.pop(),
- field.getType());
- field.set(obj,value);
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-import java.lang.reflect.*;
-import java.util.Map;
-
-public class JVarSetStatic extends FactorPrimitiveDefinition
-{
- //{{{ JVarSetStatic constructor
- /**
- * A new definition.
- */
- public JVarSetStatic(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- FactorArray datastack = interp.datastack;
- String fieldName = FactorJava.toString(datastack.pop());
- Class clazz = FactorJava.toClass(datastack.pop());
- Field field = clazz.getField(fieldName);
- Object value = FactorJava.convertToJavaType(datastack.pop(),
- field.getType());
- field.set(null,value);
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-import java.util.Set;
-
-public class Restack extends FactorPrimitiveDefinition
-{
- //{{{ Restack constructor
- /**
- * A new definition.
- */
- public Restack(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- FactorArray datastack = interp.datastack;
- Cons list = (Cons)datastack.pop(Cons.class);
- interp.callstack.push(datastack);
- interp.datastack = new FactorArray(list);
- } //}}}
-}
+++ /dev/null
-/* :folding=explicit:collapseFolds=1: */
-
-/*
- * $Id$
- *
- * Copyright (C) 2003, 2004 Slava Pestov.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package factor.primitives;
-
-import factor.*;
-import java.util.Set;
-
-public class Unstack extends FactorPrimitiveDefinition
-{
- //{{{ Unstack constructor
- /**
- * A new definition.
- */
- public Unstack(FactorWord word)
- throws Exception
- {
- super(word);
- } //}}}
-
- //{{{ eval() method
- public void eval(FactorInterpreter interp)
- throws Exception
- {
- Cons unstack = interp.datastack.toList();
- interp.datastack = (FactorArray)interp.callstack.pop();
- interp.datastack.push(unstack);
- } //}}}
-}
+++ /dev/null
-! :folding=indent:collapseFolds=0:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: math
-USE: combinators
-USE: kernel
-USE: logic
-USE: stack
-
-: + ( a b -- a+b )
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "add"
- jinvoke-static ; inline
-
-: - ( a b -- a-b )
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "subtract"
- jinvoke-static ; inline
-
-: * ( a b -- a*b )
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "multiply"
- jinvoke-static ; inline
-
-: / ( a b -- a/b )
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "divide"
- jinvoke-static ; inline
-
-: /i ( a b -- a/b )
- #! Truncating division.
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "_divide"
- jinvoke-static ; inline
-
-: /f / >float ; inline
-
-: mod ( a b -- a%b )
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "mod"
- jinvoke-static ; inline
-
-: /mod ( a b -- a/b a%b )
- 2dup /i -rot mod ;
-
-: > ( a b -- boolean )
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "greater"
- jinvoke-static ; inline
-
-: >= ( a b -- boolean )
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "greaterEqual"
- jinvoke-static ; inline
-
-: < ( a b -- boolean )
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "less"
- jinvoke-static ; inline
-
-: <= ( a b -- boolean )
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "lessEqual"
- jinvoke-static ; inline
-
-: >=< ( x y obj1 obj2 obj3 -- obj )
- ! If x > y, pushes obj1, if x = y, pushes obj2, else obj3.
- [
- "float" "float"
- "java.lang.Object" "java.lang.Object" "java.lang.Object"
- ]
- "factor.FactorLib" "branch3" jinvoke-static ;
-
-: compare ( x y [ if x < y ] [ if x = y ] [ if x > y ] -- )
- >=< call ; inline
-
-: bitand ( x y -- x&y )
- #! Bitwise and.
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "and"
- jinvoke-static ; inline
-
-: bitor ( x y -- x|y )
- #! Bitwise or.
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "or"
- jinvoke-static ; inline
-
-: bitxor ( x y -- x^y )
- #! Bitwise exclusive or.
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "xor"
- jinvoke-static ; inline
-
-: bitnot ( x -- ~x )
- #! Bitwise complement.
- [ "java.lang.Number" ]
- "factor.math.FactorMath" "not"
- jinvoke-static ; inline
-
-: shift ( x by -- )
- #! Shift 'by' bits to the left.
- [ "java.lang.Number" "int" ]
- "factor.math.FactorMath" "shift"
- jinvoke-static ; inline
-
-: rem ( x y -- remainder )
- [ "double" "double" ] "java.lang.Math" "IEEEremainder"
- jinvoke-static ; inline
-
-: round ( x to -- y )
- dupd rem - ;
-
-: gcd ( a b -- c )
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.FactorMath" "gcd" jinvoke-static ;
-
-: float>bits ( f -- bignum )
- [ "double" ]
- "java.lang.Double" "doubleToRawLongBits"
- jinvoke-static >bignum ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-USE: parser
-
-!!! The standard library.
-"/library/platform/jvm/kernel.factor" run-resource ! kernel
-"/library/platform/jvm/vectors.factor" run-resource ! vectors
-"/library/platform/jvm/stack.factor" run-resource ! stack
-"/library/logic.factor" run-resource ! logic
-"/library/platform/jvm/cons.factor" run-resource ! lists
-"/library/cons.factor" run-resource ! lists
-"/library/combinators.factor" run-resource ! combinators
-"/library/platform/jvm/combinators.factor" run-resource ! combinators
-"/library/platform/jvm/math-types.factor" run-resource ! arithmetic
-"/library/platform/jvm/arithmetic.factor" run-resource ! arithmetic
-"/library/math/arithmetic.factor" run-resource ! arithmetic
-"/library/vectors.factor" run-resource ! vectors
-"/library/platform/jvm/stack2.factor" run-resource ! stack
-"/library/math/math-combinators.factor" run-resource ! arithmetic
-"/library/vector-combinators.factor" run-resource ! vectors
-"/library/lists.factor" run-resource ! lists
-"/library/assoc.factor" run-resource ! lists
-"/library/hashtables.factor" run-resource ! hashtables
-"/library/platform/jvm/strings.factor" run-resource ! strings
-"/library/platform/jvm/sbuf.factor" run-resource ! strings
-"/library/strings.factor" run-resource ! strings
-"/library/platform/jvm/errors.factor" run-resource ! errors
-"/library/platform/jvm/namespaces.factor" run-resource ! namespaces
-"/library/namespaces.factor" run-resource ! namespaces
-"/library/list-namespaces.factor" run-resource ! namespaces
-"/library/sbuf.factor" run-resource ! strings
-"/library/math/namespace-math.factor" run-resource ! arithmetic
-"/library/continuations.factor" run-resource ! continuations
-"/library/errors.factor" run-resource ! errors
-"/library/platform/jvm/vocabularies.factor" run-resource ! vocabularies
-"/library/platform/jvm/words.factor" run-resource ! words
-"/library/words.factor" run-resource ! words
-"/library/format.factor" run-resource ! format
-"/library/platform/jvm/random.factor" run-resource ! random
-"/library/random.factor" run-resource ! random
-"/library/platform/jvm/regexp.factor" run-resource ! regexp
-"/library/stream.factor" run-resource ! streams
-"/library/platform/jvm/stream.factor" run-resource ! streams
-"/library/platform/jvm/files.factor" run-resource ! files
-"/library/stdio.factor" run-resource ! stdio
-"/library/extend-stream.factor" run-resource ! streams
-"/library/platform/jvm/unparser.factor" run-resource ! unparser
-"/library/platform/jvm/parser.factor" run-resource ! parser
-"/library/presentation.factor" run-resource ! presentation
-
-!!! Math library.
-"/library/platform/jvm/real-math.factor" run-resource ! real-math
-"/library/math/constants.factor" run-resource ! math
-"/library/math/math.factor" run-resource ! math
-"/library/math/pow.factor" run-resource ! math
-
-!!! Development tools.
-"/library/platform/jvm/processes.factor" run-resource ! processes
-"/library/vocabulary-style.factor" run-resource ! style
-"/library/prettyprint.factor" run-resource ! prettyprint
-"/library/platform/jvm/prettyprint.factor" run-resource ! prettyprint
-"/library/tools/listener.factor" run-resource ! listener
-"/library/tools/inspector.factor" run-resource ! inspector
-"/library/tools/word-tools.factor" run-resource ! inspector
-"/library/platform/jvm/debugger.factor" run-resource ! debugger
-"/library/tools/debugger.factor" run-resource ! debugger
-
-!!! Final initialization...
-"/library/init.factor" run-resource ! init
-"/library/platform/jvm/init.factor" run-resource ! init
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-USE: parser
-
-!!! The standard library.
-"/library/platform/jvm/kernel.factor" run-resource ! kernel
-"/library/platform/jvm/vectors.factor" run-resource ! vectors
-"/library/platform/jvm/stack.factor" run-resource ! stack
-"/library/logic.factor" run-resource ! logic
-"/library/platform/jvm/cons.factor" run-resource ! lists
-"/library/cons.factor" run-resource ! lists
-"/library/combinators.factor" run-resource ! combinators
-"/library/platform/jvm/combinators.factor" run-resource ! combinators
-"/library/platform/jvm/math-types.factor" run-resource ! arithmetic
-"/library/platform/jvm/arithmetic.factor" run-resource ! arithmetic
-"/library/math/arithmetic.factor" run-resource ! arithmetic
-"/library/vectors.factor" run-resource ! vectors
-"/library/platform/jvm/stack2.factor" run-resource ! stack
-"/library/math/math-combinators.factor" run-resource ! arithmetic
-"/library/vector-combinators.factor" run-resource ! vectors
-"/library/lists.factor" run-resource ! lists
-"/library/assoc.factor" run-resource ! lists
-"/library/hashtables.factor" run-resource ! hashtables
-"/library/platform/jvm/strings.factor" run-resource ! strings
-"/library/platform/jvm/sbuf.factor" run-resource ! strings
-"/library/strings.factor" run-resource ! strings
-"/library/platform/jvm/errors.factor" run-resource ! errors
-"/library/platform/jvm/namespaces.factor" run-resource ! namespaces
-"/library/namespaces.factor" run-resource ! namespaces
-"/library/list-namespaces.factor" run-resource ! namespaces
-"/library/sbuf.factor" run-resource ! strings
-"/library/math/namespace-math.factor" run-resource ! arithmetic
-"/library/continuations.factor" run-resource ! continuations
-"/library/errors.factor" run-resource ! errors
-"/library/platform/jvm/vocabularies.factor" run-resource ! vocabularies
-"/library/platform/jvm/words.factor" run-resource ! words
-"/library/words.factor" run-resource ! words
-"/library/format.factor" run-resource ! format
-"/library/platform/jvm/random.factor" run-resource ! random
-"/library/random.factor" run-resource ! random
-"/library/platform/jvm/regexp.factor" run-resource ! regexp
-"/library/stream.factor" run-resource ! streams
-"/library/platform/jvm/stream.factor" run-resource ! streams
-"/library/platform/jvm/files.factor" run-resource ! files
-"/library/stdio.factor" run-resource ! stdio
-"/library/extend-stream.factor" run-resource ! streams
-"/library/platform/jvm/unparser.factor" run-resource ! unparser
-"/library/platform/jvm/parser.factor" run-resource ! parser
-"/library/presentation.factor" run-resource ! presentation
-"/library/platform/jvm/threads.factor" run-resource ! threads
-"/library/logging.factor" run-resource ! logging
-
-!!! Math library.
-"/library/platform/jvm/real-math.factor" run-resource ! real-math
-"/library/math/constants.factor" run-resource ! math
-"/library/math/math.factor" run-resource ! math
-"/library/math/pow.factor" run-resource ! math
-"/library/math/trig-hyp.factor" run-resource ! math
-"/library/math/arc-trig-hyp.factor" run-resource ! math
-
-!!! Development tools.
-"/library/platform/jvm/processes.factor" run-resource ! processes
-"/library/stdio-binary.factor" run-resource ! stdio
-"/library/vocabulary-style.factor" run-resource ! style
-"/library/prettyprint.factor" run-resource ! prettyprint
-"/library/files.factor" run-resource ! files
-"/library/platform/jvm/prettyprint.factor" run-resource ! prettyprint
-"/library/tools/listener.factor" run-resource ! listener
-"/library/tools/inspector.factor" run-resource ! inspector
-"/library/tools/word-tools.factor" run-resource ! inspector
-"/library/platform/jvm/debugger.factor" run-resource ! debugger
-"/library/tools/debugger.factor" run-resource ! debugger
-"/library/test/test.factor" run-resource ! test
-"/library/ansi.factor" run-resource ! ansi
-"/library/tools/telnetd.factor" run-resource ! telnetd
-
-!!! Java -> native VM image cross-compiler.
-"/library/tools/image.factor" run-resource ! cross-compiler
-"/library/tools/cross-compiler.factor" run-resource ! cross-compiler
-"/library/platform/jvm/cross-compiler.factor" run-resource ! cross-compiler
-
-!!! Final initialization...
-"/library/init.factor" run-resource ! init
-"/library/platform/jvm/init.factor" run-resource ! init
-
-!!! jEdit integration.
-"/library/jedit/jedit-local.factor" run-resource ! jedit
-"/library/jedit/jedit-remote.factor" run-resource ! jedit
-"/library/jedit/jedit.factor" run-resource ! jedit
-"/library/jedit/console.factor" run-resource ! console
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-!!! Minimum amount of words needed to be able to read other
-!!! resources.
-
-USE: combinators
-
-IN: stack
-
-~<< dup A -- A A >>~
-~<< swap A B -- B A >>~
-
-IN: streams
-
-: <breader> ( reader -- breader )
- #! Wrap a Reader in a BufferedReader.
- [ "java.io.Reader" ] "java.io.BufferedReader" jnew ;
-
-: <ireader> ( inputstream -- breader )
- #! Wrap a InputStream in an InputStreamReader.
- [ "java.io.InputStream" ] "java.io.InputStreamReader" jnew ;
-
-: <rreader> ( path -- inputstream )
- #! Create a Reader for reading the specified resource from
- #! the classpath.
- "factor.FactorInterpreter"
- [ "java.lang.String" ]
- "java.lang.Class" "getResourceAsStream" jinvoke
- <ireader> <breader> ;
-
-IN: strings
-
-: cat2 ( str str -- str )
- #! Concatenate two strings.
- swap
- [ "java.lang.String" ] "java.lang.String" "concat" jinvoke ;
-
-IN: parser
-
-: parse* ( parser -- list )
- #! Reads until EOF.
- [ ] "factor.FactorReader" "parse" jinvoke ;
-
-: <parser> ( filename reader -- parser )
- #! Creates a parser with the default vocabularies:
- #! IN: user
- #! USE: user USE: builtins
- interpreter
- [
- "java.lang.String"
- "java.io.BufferedReader"
- "factor.FactorInterpreter"
- ]
- "factor.FactorReader" jnew ;
-
-: parse-stream ( filename reader -- list )
- #! Reads until end-of-file from the reader, building a parse
- #! tree. The filename is used for error reporting.
- <parser> parse* ;
-
-: parse-resource ( resource -- list )
- dup <rreader> swap "resource:" swap cat2 swap parse-stream ;
-
-: run-resource ( path -- )
- #! Reads and runs a source file from a resource path.
- parse-resource call ;
-
-!!!
-
-IN: init DEFER: boot
-
-interpreter "factor.FactorInterpreter" "mini" jvar-get [
- "/library/platform/jvm/boot-mini.factor" run-resource
-] [
- "/library/platform/jvm/boot-sumo.factor" run-resource
-] ifte
-
-boot
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: combinators
-USE: kernel
-USE: lists
-USE: stack
-
-! These are DEPRECATED!
-
-: expand ( list -- list )
- #! Evaluates a quotation on a new stack, and pushes the
- #! reversed stack onto the original stack.
- #!
- #! This combinator will not compile.
- unit
- restack
- call
- unstack ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: lists USE: kernel USE: stack
-
-: array>list ( array -- list )
- #! Convert an array into a proper list.
- [ [ "java.lang.Object" ] ] "factor.Cons" "fromArray"
- jinvoke-static ;
-
-: car ( [ car | cdr ] -- car )
- #! Push the head of a list.
- "factor.Cons" "car" jvar-get ; inline
-
-: cdr ( [ car | cdr ] -- cdr )
- #! Push the tail of a list. In a proper list, the tail is
- #! always a cons cell or f; in an improper list, the tail
- #! can be anything.
- "factor.Cons" "cdr" jvar-get ; inline
-
-: cons ( car cdr -- [ car , cdr ] )
- #! Push a new cons cell. If the cdr is f or a proper list,
- #! has the effect of prepending the car to the cdr.
- [ "java.lang.Object" "java.lang.Object" ] "factor.Cons" jnew
- ; inline
-
-: cons? ( list -- boolean )
- #! Test for cons cell type.
- "factor.Cons" is ; inline
+++ /dev/null
-! :folding=none:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: image
-USE: combinators
-USE: kernel
-USE: lists
-USE: math
-USE: parser
-USE: stack
-USE: stdio
-USE: streams
-USE: strings
-USE: vectors
-USE: vectors
-USE: words
-
-: worddef, ( word -- )
- dup compound-or-compiled? [
- dup word-of-worddef swap compound>list compound,
- ] [
- drop
- ] ifte ;
-
-: cross-compile ( quot -- )
- [ dup worddef? [ worddef, ] [ drop ] ifte ] each ;
-
-: cross-compile-resource ( resource -- )
- parse-resource cross-compile ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: errors
-USE: combinators
-USE: kernel
-USE: namespaces
-USE: prettyprint
-USE: stack
-USE: stdio
-
-: error. ( error -- str ) print ;
-
-: exception? ( exception -- boolean )
- "java.lang.Throwable" is ;
-
-: print-stack-trace ( exception -- )
- [ ] "java.lang.Throwable" "printStackTrace" jinvoke ;
-
-: :j ( -- )
- #! Print the stack trace from the last exception.
- "error" get dup exception? [
- print-stack-trace
- ] [
- "Not an exception: " write .
- ] ifte ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: errors
-USE: kernel
-USE: stack
-USE: strings
-
-DEFER: save-error
-DEFER: rethrow
-
-: throw ( error -- )
- #! Throw an error that will be caught by a surrounding
- #! catch block.
- dup save-error rethrow ;
-
-: catchstack* ( -- cs )
- interpreter
- "factor.FactorInterpreter" "catchstack" jvar-get ;
-
-: set-catchstack* ( cs -- )
- interpreter
- "factor.FactorInterpreter" "catchstack" jvar-set ;
-
-: catchstack ( -- cs ) catchstack* clone ;
-: set-catchstack ( cs -- ) clone set-catchstack* ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: files
-USE: combinators
-USE: kernel
-USE: lists
-USE: logic
-USE: stack
-USE: strings
-USE: namespaces
-
-: <file> ( path -- file )
- dup "java.io.File" is not [
- [ "java.lang.String" ] "java.io.File" jnew
- ] when ;
-
-: delete ( file -- ? )
- #! Delete a file.
- <file> [ ] "java.io.File" "delete" jinvoke ;
-
-: exists? ( file -- boolean )
- <file> [ ] "java.io.File" "exists" jinvoke ;
-
-: directory? ( file -- boolean )
- <file> [ ] "java.io.File" "isDirectory" jinvoke ;
-
-: directory ( file -- listing )
- <file> [ ] "java.io.File" "list" jinvoke array>list str-sort ;
-
-: rename ( from to -- ? )
- ! Rename file 'from' to 'to'. These can be paths or
- ! java.io.File instances.
- <file> swap <file>
- [ "java.io.File" ] "java.io.File" "renameTo"
- jinvoke ;
-
-: file-length ( file -- size )
- <file> [ ] "java.io.File" "length" jinvoke ;
-
-: cwd ( -- dir )
- global [ "cwd" get ] bind ;
-
-: cd ( dir --)
- global [ "cwd" set ] bind ;
-
-global [ "user.dir" system-property "cwd" set ] bind
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: init
-USE: combinators
-USE: continuations
-USE: kernel
-USE: lists
-USE: listener
-USE: namespaces
-USE: parser
-USE: stack
-USE: stdio
-USE: streams
-USE: strings
-USE: presentation
-USE: words
-
-: stdin ( -- stdin )
- "java.lang.System" "in" jvar-static-get
- <ireader> <breader> ;
-
-: stdout ( -- stdout )
- "java.lang.System" "out" jvar-static-get <owriter> ;
-
-: init-stdio ( -- )
- #! Initialize standard input/output.
- stdin stdout <char-stream> <stdio-stream> "stdio" set ;
-
-: init-environment ( -- )
- #! Initialize OS-specific constants.
- "user.home" system-property "~" set
- "file.separator" system-property "/" set ;
-
-: boot ( -- )
- #! The boot word is run by the intepreter when starting from
- #! an object database.
-
- ! Some flags are *on* by default, unless user specifies
- ! -no-<flag> CLI switch
- t "user-init" set
-
- init-stdio
- init-environment
- init-search-path
- "args" get parse-command-line
- run-user-init
-
- t "startup-done" set
-
- "interactive" get [ init-listener 1 exit* ] when ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: words
-
-: word ( -- word )
- ! Pushes most recently defined word.
- interpreter "factor.FactorInterpreter" "last" jvar-get ;
-
-IN: kernel
-
-: inline ;
-
-: hashcode ( obj -- hashcode )
- #! If two objects are =, they must have equal hashcodes.
- [ ] "java.lang.Object" "hashCode" jinvoke ;
-
-: eq? ( a b -- ? )
- #! Returns true if a and b are the same object.
- [ "java.lang.Object" "java.lang.Object" ]
- "factor.FactorLib" "eq" jinvoke-static ;
-
-: = ( a b -- ? )
- #! Push t if a is isomorphic to b.
- [ "java.lang.Object" "java.lang.Object" ]
- "factor.FactorLib" "equal" jinvoke-static ;
-
-: class-of* ( obj -- class )
- [ ] "java.lang.Object" "getClass" jinvoke ;
-
-: class-of ( obj -- class )
- class-of* [ ] "java.lang.Class" "getName" jinvoke ;
-
-: is ( obj class -- boolean )
- ! Like "instanceof" in Java.
- [ "java.lang.Object" ] "java.lang.Class" "isInstance"
- jinvoke ;
-
-: clone ( obj -- obj )
- [ ] "factor.PublicCloneable" "clone" jinvoke ;
-
-: toplevel ( -- )
- interpreter
- [ ] "factor.FactorInterpreter" "topLevel" jinvoke ;
-
-: exit* ( code -- )
- [ "int" ] "java.lang.System" "exit" jinvoke-static ;
-
-: garbage-collection ( -- )
- [ ] "java.lang.System" "gc" jinvoke-static ;
-
-IN: math
-DEFER: >bignum
-
-IN: kernel
-
-: millis ( -- millis )
- ! Pushes the current time, in milliseconds.
- [ ] "java.lang.System" "currentTimeMillis" jinvoke-static
- >bignum ;
-
-: system-property ( name -- value )
- [ "java.lang.String" ] "java.lang.System" "getProperty"
- jinvoke-static ;
-
-: java? t ;
-: native? f ;
-: version "factor.FactorInterpreter" "VERSION" jvar-static-get ;
-
-: jvm-runtime ( -- java.lang.Runtime )
- #! Return the java.lang.Runtime object for the JVM
- f "java.lang.Runtime" "getRuntime" jinvoke-static ;
-
-: free-memory ( -- int )
- #! Return the free memory in the JVM.
- jvm-runtime f "java.lang.Runtime" "freeMemory" jinvoke
- >bignum ;
-
-: total-memory ( -- int )
- #! Return the total memory available to the JVM.
- jvm-runtime f "java.lang.Runtime" "totalMemory" jinvoke
- >bignum ;
-
-: room free-memory total-memory ;
-
-: resource ( path -- url )
- interpreter class-of*
- [ "java.lang.String" ]
- "java.lang.Class" "getResource" jinvoke ;
-
-: cpu "jvm" ;
+++ /dev/null
-! :folding=indent:collapseFolds=0:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: math
-USE: combinators
-USE: kernel
-USE: logic
-USE: stack
-
-: number? "java.lang.Number" is ; inline
-: >number "java.lang.Number" coerce ; inline
-
-: fixnum? "java.lang.Integer" is ; inline
-: >fixnum "int" coerce ; inline
-
-: bignum? "java.math.BigInteger" is ; inline
-: >bignum "java.math.BigInteger" coerce ; inline
-
-: ratio? "factor.math.Ratio" is ; inline
-
-: numerator ( x/y -- x )
- dup ratio? [
- "factor.math.Ratio" "numerator" jvar-get
- ] [
- >number
- ] ifte ;
-
-: denominator ( x/y -- y )
- dup ratio? [
- "factor.math.Ratio" "denominator" jvar-get
- ] [
- >number drop 1
- ] ifte ;
-
-: float? ( obj -- boolean )
- dup "java.lang.Float" is
- swap "java.lang.Double" is or ; inline
-: >float "double" coerce ; inline
-
-: complex? "factor.math.Complex" is ; inline
-
-: real ( complex -- real )
- dup complex? [
- "factor.math.Complex" "real" jvar-get
- ] [
- >number
- ] ifte ;
-
-: imaginary ( complex -- imaginary )
- dup complex? [
- "factor.math.Complex" "imaginary" jvar-get
- ] [
- >number drop 0
- ] ifte ;
-
-: rect> ( real imaginary -- complex )
- [ "java.lang.Number" "java.lang.Number" ]
- "factor.math.Complex" "valueOf" jinvoke-static ;
-
-: >rect ( complex -- real imaginary )
- dup complex? [
- dup
- "factor.math.Complex" "real" jvar-get
- swap
- "factor.math.Complex" "imaginary" jvar-get
- ] [
- >number 0
- ] ifte ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: namespaces
-USE: combinators
-USE: kernel
-USE: lists
-USE: logic
-USE: stack
-USE: strings
-USE: vectors
-
-DEFER: namespace
-
-: namestack* ( -- stack )
- #! Push the namespace stack.
- interpreter
- "factor.FactorInterpreter" "namestack" jvar-get ; inline
-
-: set-namestack* ( stack -- )
- #! Set the namespace stack.
- interpreter
- "factor.FactorInterpreter" "namestack" jvar-set ; inline
-
-: >n ( namespace -- n:namespace )
- #! Push a namespace on the namespace stack.
- namestack* vector-push ; inline
-
-: n> ( n:namespace -- namespace )
- #! Pop the top of the namespace stack.
- namestack* vector-pop ; inline
-
-: namestack ( -- stack )
- namestack* clone ; inline
-
-: set-namestack ( stack -- )
- clone set-namestack* ; inline
-
-: global ( -- namespace )
- interpreter "factor.FactorInterpreter" "global" jvar-get ;
-
-: set-global ( namespace -- )
- interpreter "factor.FactorInterpreter" "global" jvar-set ;
-
-: get* ( variable namespace -- value )
- #! Pushes the value of a variable in an explicitly-specified
- #! namespace.
- [ "java.lang.String" ]
- "factor.FactorNamespace" "getVariable" jinvoke ; inline
-
-: get ( variable -- value )
- #! Pushes the value of a variable.
- interpreter
- [ "java.lang.String" ]
- "factor.FactorInterpreter" "getVariable" jinvoke ; inline
-
-: put* ( variable value namespace -- )
- #! Sets the value of a variable in an explicitly-specified
- #! namespace.
- [ "java.lang.String" "java.lang.Object" ]
- "factor.FactorNamespace" "setVariable" jinvoke ; inline
-
-: put ( variable value -- )
- #! Set the value of a variable in the current namespace.
- namespace put* ; inline
-
-: set ( value variable -- )
- #! Set the value of a variable in the current namespace.
- swap put ; inline
-
-: set* ( value variable namespace -- )
- swapd put* ; inline
-
-: <namespace> ( -- namespace )
- [ ] "factor.FactorNamespace" jnew ;
-
-: <objnamespace> ( object -- namespace )
- [ "java.lang.Object" ] "factor.FactorNamespace" jnew ;
-
-: namespace-of ( obj -- namespace )
- [ "java.lang.Object" ] "factor.FactorJava" "toNamespace"
- jinvoke-static ;
-
-: bind ( namespace quot -- )
- #! Execute a quotation with a namespace on the namestack.
- swap namespace-of >n call n> drop ; inline
-
-: has-namespace? ( a -- boolean )
- "factor.FactorObject" is ; inline
-
-: import ( class pairs -- )
- #! Import some static variables from a Java class into the
- #! current namespace.
- namespace [ "java.lang.String" "factor.Cons" ]
- "factor.FactorNamespace" "importVars"
- jinvoke ;
-
-: namespace? ( a -- boolean )
- "factor.FactorNamespace" is ; inline
-
-: this ( -- object )
- ! Returns the object bound to the current namespace, or if
- ! no object is bound, the namespace itself.
- namespace dup
- [ ] "factor.FactorNamespace" "getThis" jinvoke dup rot ?
- ; inline
-
-: vars ( -- list )
- namespace [ ] "factor.FactorNamespace" "toVarList"
- jinvoke ;
-
-: vars-values ( -- list )
- namespace [ ] "factor.FactorNamespace" "toVarValueList"
- jinvoke ;
-
-: values ( -- list )
- namespace [ ] "factor.FactorNamespace" "toValueList"
- jinvoke ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: parser
-USE: combinators
-USE: errors
-USE: namespaces
-USE: stack
-USE: streams
-USE: strings
-
-: parse-file ( file -- list )
- dup <freader> parse-stream ;
-
-: run-file ( path -- )
- parse-file call ;
-
-: parse-resource* ( resource -- list )
- dup <rreader> swap "resource:" swap cat2 swap parse-stream ;
-
-: parse-resource ( resource -- list )
- #! Override this to be slightly more useful for development.
- global [ "resource-path" get ] bind dup [
- swap cat2 parse-file
- ] [
- drop parse-resource*
- ] ifte ;
-
-: <custom-parser> ( filename reader interactive docs -- parser )
- interpreter
- [
- "java.lang.String"
- "java.io.BufferedReader"
- "boolean"
- "boolean"
- "factor.FactorInterpreter"
- ]
- "factor.FactorReader" jnew ;
-
-: interactive-parse-stream ( filename reader -- list )
- #! Reads until end-of-file from the reader, building a parse
- #! tree. The filename is used for error reporting.
- #!
- #! This form should be used by the outer interpreter only.
- #! Its default vocabularies are:
- #! global [ "use" get ] bind
- #! global [ "in" get ] bind
- f t <custom-parser> parse* ;
-
-: (run-file) ( path -- )
- dup <freader> interactive-parse-stream call ;
-
-: parse ( str -- list )
- #! Parse a string using an interactive parser.
- "<interactive>" swap <sreader> <breader> interactive-parse-stream ;
-
-: eval ( "X" -- X )
- parse call ;
-
-: eval-catch ( str -- )
- [ eval ] [ [ default-error-handler drop ] when* ] catch ;
-
-: parse-number* ( str base -- num )
- #! Convert a string to a number. Return f if conversion
- #! fails.
- [ "java.lang.String" "int" ]
- "factor.math.NumberParser"
- "parseNumber"
- jinvoke-static ;
-
-: parse-number 10 parse-number* ;
-
-: not-a-number "Not a number" throw ;
-
-: base> ( str base -- num )
- #! Convert a string to a number. Throw an error if
- #! conversion fails.
- parse-number* [ not-a-number ] unless* ;
-
-: bin> 2 base> ;
-: oct> 8 base> ;
-: dec> 10 base> ;
-: hex> 16 base> ;
-: str>number dec> ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: prettyprint
-USE: combinators
-USE: lists
-USE: math
-USE: prettyprint
-USE: stack
-USE: stdio
-USE: unparser
-USE: words
-
-: prettyprint-:; ( indent word list -- indent )
- over >r >r dup
- >r dupd prettyprint-IN: prettyprint-: r>
- prettyprint-word prettyprint-space
- r>
- prettyprint-list prettyprint-; r> prettyprint-plist ;
-
-: prettyprint-~<< ( indent -- indent )
- "~<<" write prettyprint-space
- tab-size + ;
-
-: prettyprint->>~ ( indent -- indent )
- ">>~" write
- tab-size - ;
-
-: prettyprint-~<<>>~ ( indent word list -- indent )
- >r >r prettyprint-~<< r> prettyprint-word " " write r>
- [ write " " write ] each
- prettyprint->>~ ;
-
-: see ( word -- )
- 0 swap
- dup worddef
- [
- [ compound-or-compiled? ] [ word-parameter prettyprint-:; ]
- [ shuffle? ] [ word-parameter prettyprint-~<<>>~ ]
- [ primitive? ] [ "PRIMITIVE: " write unparse write drop ]
- [ symbol? ] [ "SYMBOL: " write drop unparse write ]
- [ drop t ] [ 2drop "Not defined" write ]
- ] cond prettyprint-newline ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: processes
-USE: files
-USE: kernel
-USE: streams
-USE: stack
-
-: exec ( args -- exitCode )
- cwd
- [
- [ "java.lang.String" ] "java.lang.String"
- ] "factor.FactorLib" "exec"
- jinvoke-static ;
-
-: (pipe) ( args -- process )
- jvm-runtime
- [ [ "java.lang.String" ] ]
- "java.lang.Runtime" "exec" jinvoke ;
-
-: close-stderr ( process -- )
- [ ] "java.lang.Process" "getErrorStream" jinvoke
- close-java-stream ;
-
-: pipe ( args -- stream )
- #! Start a process, and return a stream for communicating
- #! with it.
- (pipe) dup close-stderr
- dup [ ] "java.lang.Process" "getInputStream" jinvoke
- swap [ ] "java.lang.Process" "getOutputStream" jinvoke
- <byte-stream> ;
+++ /dev/null
-! :folding=indent:collapseFolds=0:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: random
-
-: random-angle ( -- theta )
- [ ] "factor.math.FactorMath"
- "randomAngle" jinvoke-static ;
-
-: random-boolean ( -- boolean )
- [ ] "factor.math.FactorMath"
- "randomBoolean" jinvoke-static ;
-
-: random-float ( min max scale -- random )
- [ "int" "int" "float" ] "factor.math.FactorMath"
- "randomFloat" jinvoke-static ;
-
-: random-int ( min max -- random )
- [ "int" "int" ] "factor.math.FactorMath"
- "randomInt" jinvoke-static ;
+++ /dev/null
-! :folding=indent:collapseFolds=0:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-!!! This file defines words that call methods in the
-!!! java.lang.Math class. These work with real arguments only,
-!!! and should not be called directly, use the words in the
-!!! 'math' vocabulary instead.
-
-IN: real-math
-USE: kernel
-USE: math
-USE: stack
-
-: facos ( x -- acos )
- [ "double" ] "java.lang.Math" "acos"
- jinvoke-static ; inline
-
-: fasin ( x -- asin )
- [ "double" ] "java.lang.Math" "asin"
- jinvoke-static ; inline
-
-: fatan ( x -- atan )
- [ "double" ] "java.lang.Math" "atan"
- jinvoke-static ; inline
-
-: fatan2 ( y x -- atan2 )
- [ "double" "double" ] "java.lang.Math" "atan2"
- jinvoke-static ; inline
-
-: fcos ( x -- cos )
- [ "double" ] "java.lang.Math" "cos"
- jinvoke-static ; inline
-
-: fexp ( x -- exp )
- [ "double" ] "java.lang.Math" "exp"
- jinvoke-static ; inline
-
-: fcosh ( x -- cosh )
- fexp dup recip + 2 / ;
-
-: flog ( x -- exp )
- [ "double" ] "java.lang.Math" "log"
- jinvoke-static ; inline
-
-: fpow ( x y -- x^y )
- [ "double" "double" ] "java.lang.Math" "pow"
- jinvoke-static ; inline
-
-: fsin ( x -- sin )
- [ "double" ] "java.lang.Math" "sin"
- jinvoke-static ; inline
-
-: fsinh ( x -- cosh )
- fexp dup recip - 2 / ;
-
-: fsqrt ( x -- sqrt x )
- [ "double" ] "java.lang.Math" "sqrt"
- jinvoke-static ; inline
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: regexp
-USE: combinators
-USE: kernel
-USE: logic
-USE: lists
-USE: math
-USE: stack
-
-: <regex> ( pattern -- regex )
- #! Compile the regex, if its not already compiled.
- dup "java.util.regex.Pattern" is not [
- [ "java.lang.String" ]
- "java.util.regex.Pattern" "compile"
- jinvoke-static
- ] when ;
-
-: <matcher> ( string pattern -- matcher )
- [ "java.lang.CharSequence" ]
- "java.util.regex.Pattern" "matcher"
- jinvoke ;
-
-: re-matches* ( matcher -- boolean )
- [ ] "java.util.regex.Matcher" "matches"
- jinvoke ;
-
-: re-matches ( input regex -- boolean )
- <regex> <matcher> re-matches* ;
-
-: [re-matches] ( matcher code -- boolean )
- #! If the matcher's re-matches* function returns true,
- #! evaluate the code with the matcher at the top of the
- #! stack. Otherwise, pop the matcher off the stack and
- #! push f.
- >r dup re-matches* r> [ drop f ] ifte ;
-
-: re-replace* ( replace matcher -- string )
- [ "java.lang.String" ] "java.util.regex.Matcher"
- "replaceAll" jinvoke ;
-
-: re-replace ( input regex replace -- string )
- #! Replaces all occurrences of the regex in the input string
- #! with the replace string.
- -rot <regex> <matcher> re-replace* ;
-
-: re-split ( string split -- list )
- <regex> [ "java.lang.CharSequence" ]
- "java.util.regex.Pattern" "split" jinvoke array>list ;
-
-: group ( index match -- )
- [ "int" ] "java.util.regex.Matcher" "group"
- jinvoke ;
-
-: group-count ( matcher -- count )
- [ ] "java.util.regex.Matcher" "groupCount"
- jinvoke ;
-
-: groups* ( matcher -- list )
- [
- [
- dup group-count [
- succ over group swap
- ] times* drop
- ] cons expand
- ] [re-matches] ;
-
-: groups ( input regex -- list )
- <regex> <matcher> groups* ;
-
-: group1 ( string regex -- string )
- groups dup [ car ] when ;
+++ /dev/null
-! :folding=indent:collapseFolds=0:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: strings
-USE: kernel
-USE: stack
-
-: sbuf? ( obj -- ? )
- "java.lang.StringBuffer" is ;
-
-: <sbuf> ( capacity -- StringBuffer )
- [ "int" ] "java.lang.StringBuffer" jnew ;
-
-: sbuf-append ( str buf -- )
- [ "java.lang.String" ] "java.lang.StringBuffer" "append"
- jinvoke drop ;
-
-: sbuf-nth ( index sbuf -- char )
- [ "int" ] "java.lang.StringBuffer" "charAt" jinvoke ;
-
-: set-sbuf-nth ( char index sbuf -- )
- swapd
- [ "int" "char" ] "java.lang.StringBuffer" "setCharAt" jinvoke ;
-
-: sbuf-length ( sbuf -- length )
- [ ] "java.lang.StringBuffer" "length" jinvoke ;
-
-: set-sbuf-length ( length sbuf -- )
- [ "int" ] "java.lang.StringBuffer" "setLength" jinvoke ;
-
-: sbuf>str ( sbuf -- str )
- >str ;
-
-: sbuf-reverse ( sbuf -- )
- #! Destructively reverse a string buffer.
- [ ] "java.lang.StringBuffer" "reverse" jinvoke drop ;
-
-: str>sbuf ( str -- sbuf )
- dup str-length <sbuf> tuck sbuf-append ;
-
-: str-reverse ( str -- str )
- str>sbuf dup sbuf-reverse sbuf>str ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: stack
-USE: kernel
-USE: vectors
-
-~<< drop A -- >>~
-~<< 2drop A B -- >>~
-~<< 2dup A B -- A B A B >>~
-~<< dupd A B -- A A B >>~
-~<< 2dupd A B C D -- A B A B C D >>~
-~<< nip A B -- B >>~
-~<< 2nip A B C D -- C D >>~
-~<< nop -- >>~ ! Does nothing!
-~<< over A B -- A B A >>~
-~<< 2over A B C D -- A B C D A B >>~
-~<< pick A B C -- A B C A >>~ ! Not the Forth pick!
-~<< rot A B C -- B C A >>~
-~<< 2rot A B C D E F -- C D E F A B >>~
-~<< -rot A B C -- C A B >>~
-~<< 2-rot A B C D E F -- E F A B C D >>~
-~<< 2swap A B C D -- C D A B >>~
-~<< swapd A B C -- B A C >>~
-~<< 2swapd A B C D E F -- C D A B E F >>~
-~<< transp A B C -- C B A >>~
-~<< 2transp A B C D E F -- E F C D A B >>~
-~<< tuck A B -- B A B >>~
-~<< 2tuck A B C D -- C D A B C D >>~
-
-~<< 3drop A B C -- >>~
-~<< 3dup A B C -- A B C A B C >>~
-
-~<< >r A -- r:A >>~
-~<< r> r:A -- A >>~
-
-: apply-shuffle ( ds cs shuffle -- )
- interpreter swap
- [
- "factor.FactorInterpreter"
- "factor.FactorArray"
- "factor.FactorArray"
- ]
- "factor.FactorShuffleDefinition" "eval" jinvoke ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: stack
-USE: combinators
-USE: kernel
-USE: vectors
-
-: datastack* ( -- datastack )
- interpreter "factor.FactorInterpreter" "datastack" jvar-get ;
-
-: datastack ( -- datastack )
- datastack* clone ;
-
-: set-datastack* ( datastack -- ... )
- interpreter "factor.FactorInterpreter" "datastack" jvar-set ;
-
-: set-datastack ( datastack -- ... )
- clone set-datastack* ;
-
-: callstack* ( -- callstack )
- interpreter "factor.FactorInterpreter" "callstack" jvar-get ;
-
-: callstack ( -- callstack )
- callstack*
- ! When 'clone' is interpreted, 'call' pushes a call frame
- ! which is replaced by 'clone' due to tail call optimization.
- ! When 'clone' is compiled, 'call' pushes a call frame, which
- ! is not affected by 'clone'.
- ! In both cases, the call stack has a frame from 'call' and
- ! a frame from 'callstack', and we pop both off so that
- ! callstack pushes the callstack as it was in the calling
- ! word.
- [ clone ] call
- dup vector-pop drop
- dup vector-pop drop ;
-
-: set-callstack* ( callstack -- ... )
- interpreter "factor.FactorInterpreter" "callstack" jvar-set ;
-
-: set-callstack ( callstack -- ... )
- clone set-callstack* ;
-
-: clear ( -- )
- #! Clear the datastack. For interactive use only; invoking
- #! this from a word definition will clobber any values left
- #! on the data stack by the caller.
- datastack* vector-clear ;
-
-: depth ( -- n )
- #! Push the number of elements on the datastack.
- datastack vector-length ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: streams
-USE: combinators
-USE: errors
-USE: kernel
-USE: lists
-USE: logic
-USE: namespaces
-USE: stack
-USE: strings
-
-: close-java-stream ( stream -- )
- [
- [ "java.io.InputStream" is ] [
- [ ] "java.io.InputStream" "close" jinvoke
- ]
- [ "java.io.OutputStream" is ] [
- [ ] "java.io.OutputStream" "close" jinvoke
- ]
- [ "java.io.Reader" is ] [
- [ ] "java.io.Reader" "close" jinvoke
- ]
- [ "java.io.Writer" is ] [
- [ ] "java.io.Writer" "close" jinvoke
- ]
- ] cond ;
-
-: fcopy ( from to -- )
- #! Copy the contents of the byte-stream 'from' to the
- #! byte-stream 'to'.
- >r [ "in" get ] bind r>
- [ "out" get ] bind
- [ "java.io.InputStream" "java.io.OutputStream" ]
- "factor.FactorLib" "copy" jinvoke-static ;
-
-! These are in separate words so that they can be compiled.
-! Do not call them directly.
-
-: <byte-stream>/freadln ( -- string )
- "in" get
- [ "java.io.InputStream" ] "factor.FactorLib" "readLine"
- jinvoke-static ;
-
-: <eof-exception> ( -- ex )
- [ ] "java.io.EOFException" jnew ;
-
-: >char/eof ( ch -- ch )
- dup -1 = [ drop f ] [ >char ] ifte ;
-
-: <byte-stream>/fread1 ( -- string )
- "in" get [ ] "java.io.InputStream" "read" jinvoke
- >char/eof ;
-
-: <byte-stream>/fread# ( count -- string )
- "in" get
- [ "int" "java.io.InputStream" ]
- "factor.FactorLib" "readCount"
- jinvoke-static ;
-
-: <byte-stream>/fwrite ( string -- )
- dup char? [
- "out" get
- [ "int" ] "java.io.OutputStream" "write" jinvoke
- ] [
- >bytes
- "out" get
- [ [ "byte" ] ] "java.io.OutputStream" "write" jinvoke
- ] ifte ;
-
-: <byte-stream>/fflush ( -- )
- "out" get [ ] "java.io.OutputStream" "flush" jinvoke ;
-
-: <byte-stream>/fclose ( -- )
- "in" get [ close-java-stream ] when*
- "out" get [ close-java-stream ] when* ;
-
-: <bin> ( in -- in )
- [ "java.io.InputStream" ] "java.io.BufferedInputStream" jnew ;
-
-: <bout> ( out -- out )
- [ "java.io.OutputStream" ] "java.io.BufferedOutputStream" jnew ;
-
-: <byte-stream> ( in out -- stream )
- #! Creates a new stream for reading from the
- #! java.io.InputStream in, and writing to the
- #! java.io.OutputStream out. The streams are wrapped in
- #! buffered streams.
- <stream> [
- "out" set
- "in" set
- ( -- string )
- [ <byte-stream>/freadln ] "freadln" set
- ( count -- string )
- [ <byte-stream>/fread1 ] "fread1" set
- ( count -- string )
- [ <byte-stream>/fread# ] "fread#" set
- ( string -- )
- [ <byte-stream>/fwrite ] "fwrite" set
- ( -- )
- [ <byte-stream>/fflush ] "fflush" set
- ( -- )
- [ <byte-stream>/fclose ] "fclose" set
- ] extend ;
-
-: <char-stream>/freadln ( -- string )
- "in" get [ ] "java.io.BufferedReader" "readLine"
- jinvoke ;
-
-: <char-stream>/fread1 ( -- string )
- "in" get [ ] "java.io.Reader" "read" jinvoke
- >char/eof ;
-
-: <char-stream>/fread# ( -- string )
- "in" get
- [ "int" "java.io.Reader" ]
- "factor.FactorLib" "readCount"
- jinvoke-static ;
-
-: <char-stream>/fwrite ( string -- )
- "out" get [ "java.lang.String" ] "java.io.Writer" "write"
- jinvoke ;
-
-: <char-stream>/fflush ( -- )
- "out" get [ ] "java.io.Writer" "flush" jinvoke ;
-
-: <char-stream>/fclose ( -- )
- "in" get [ close-java-stream ] when*
- "out" get [ close-java-stream ] when* ;
-
-: <char-stream> ( in out -- stream )
- #! Creates a new stream for reading from the
- #! java.io.BufferedReader in, and writing to the
- #! java.io.Reader out.
- <stream> [
- "out" set
- "in" set
- ( -- string )
- [ <char-stream>/freadln ] "freadln" set
- ( -- string )
- [ <char-stream>/fread1 ] "fread1" set
- ( count -- string )
- [ <char-stream>/fread# ] "fread#" set
- ( string -- )
- [ <char-stream>/fwrite ] "fwrite" set
- ( -- )
- [ <char-stream>/fflush ] "fflush" set
- ( -- )
- [ <char-stream>/fclose ] "fclose" set
- ] extend ;
-
-: <bwriter> ( writer -- bwriter )
- [ "java.io.Writer" ] "java.io.BufferedWriter" jnew ;
-
-: <owriter> ( outputstream -- owriter )
- [ "java.io.OutputStream" ] "java.io.OutputStreamWriter" jnew ;
-
-: <filecr> ( path -- stream )
- [ "java.lang.String" ] "java.io.FileReader" jnew <breader>
- f
- <char-stream> ;
-
-: <filecw> ( path -- stream )
- [ "java.lang.String" ] "java.io.FileWriter" jnew <bwriter>
- f swap
- <char-stream> ;
-
-: <filebr> ( path -- stream )
- [ "java.lang.String" ] "java.io.FileInputStream" jnew <bin>
- f
- <byte-stream> ;
-
-: <filebw> ( path -- stream )
- [ "java.lang.String" ] "java.io.FileOutputStream" jnew <bout>
- f swap
- <byte-stream> ;
-
-: <freader> ( file -- freader )
- [ "java.lang.String" ] "java.io.FileReader" jnew <breader> ;
-
-: <sreader> ( string -- reader )
- [ "java.lang.String" ] "java.io.StringReader" jnew ;
-
-: <server> ( port -- stream )
- #! Starts listening on localhost:port. Returns a stream that
- #! you can close with fclose, and accept connections from
- #! with accept. No other stream operations are supported.
- [ "int" ] "java.net.ServerSocket" jnew
- <stream> [
- "socket" set
-
- ( -- )
- [
- "socket" get [ ] "java.net.ServerSocket" "close" jinvoke
- ] "fclose" set
- ] extend ;
-
-: socket-closed? ( socket -- ? )
- [ ] "java.net.Socket" "isClosed" jinvoke ;
-
-: close-socket ( socket -- )
- [ ] "java.net.Socket" "close" jinvoke ;
-
-: ?close-socket ( socket -- )
- dup socket-closed? [ drop ] [ close-socket ] ifte ;
-
-: <socket-stream> ( socket -- stream )
- #! Wraps a socket inside a byte-stream.
- dup
- dup
- [ ] "java.net.Socket" "getInputStream" jinvoke <bin>
- swap
- [ ] "java.net.Socket" "getOutputStream" jinvoke <bout>
- <byte-stream> [
- dup >str "client" set "socket" set
-
- ! We "extend" byte-stream's fclose.
- ( -- )
- "fclose" get [
- "socket" get ?close-socket
- ] append "fclose" set
- ] extend ;
-
-: <client> ( server port -- stream )
- #! Open a TCP/IP socket to a port on the given server.
- [ "java.lang.String" "int" ] "java.net.Socket" jnew
- <socket-stream> ;
-
-: accept ( server -- client )
- #! Accept a connection from a server socket.
- [ "socket" get ] bind
- [ ] "java.net.ServerSocket" "accept" jinvoke <socket-stream> ;
-
-: <resource-stream> ( path -- stream )
- <rreader> f <char-stream> ;
+++ /dev/null
-! :folding=indent:collapseFolds=0:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: strings
-USE: combinators
-USE: kernel
-USE: logic
-USE: stack
-
-: char? ( obj -- boolean )
- "java.lang.Character" is ;
-
-: >char ( obj -- char )
- "char" coerce ; inline
-
-: string? ( obj -- ? )
- dup char? swap "java.lang.String" is or ;
-
-: str-length ( str -- length )
- [ ] "java.lang.String" "length" jinvoke ;
-
-: substring ( start end str -- str )
- [ "int" "int" ] "java.lang.String" "substring"
- jinvoke ;
-
-: >str ( obj -- string )
- ! Returns the Java string representation of this object.
- [ ] "java.lang.Object" "toString" jinvoke ;
-
-: >bytes ( string -- array )
- ! Converts a string to an array of ASCII bytes. An exception
- ! is thrown if the string contains non-ASCII characters.
- "ASCII" swap
- [ "java.lang.String" ] "java.lang.String" "getBytes"
- jinvoke ;
-
-: str-nth ( index str -- char )
- [ "int" ] "java.lang.String" "charAt" jinvoke ;
-
-: >lower ( str -- str )
- [ ] "java.lang.String" "toLowerCase" jinvoke ;
-
-: >upper ( str -- str )
- [ ] "java.lang.String" "toUpperCase" jinvoke ;
-
-: index-of* ( index string substring -- index )
- dup char? [
- -rot
- ! Why is the first parameter an int and not a char?
- [ "int" "int" ]
- "java.lang.String" "indexOf"
- jinvoke
- ] [
- -rot
- [ "java.lang.String" "int" ]
- "java.lang.String" "indexOf"
- jinvoke
- ] ifte ;
-
-: str-compare ( str1 str2 -- n )
- swap [ "java.lang.String" ] "java.lang.String" "compareTo"
- jinvoke ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: threads
-
-USE: combinators
-USE: continuations
-USE: errors
-USE: kernel
-USE: stack
-
-: <thread> ( runnable -- thread )
- [ "java.lang.Runnable" ] "java.lang.Thread" jnew ;
-
-: current-thread ( -- thread )
- [ ] "java.lang.Thread" "currentThread" jinvoke-static ;
-
-: start-thread ( thread -- )
- [ ] "java.lang.Thread" "start" jinvoke ;
-
-: clone-interpreter ( interp1 interp2 -- )
- #! Copy the state of interp1 into interp2.
- [ "factor.FactorInterpreter" ]
- "factor.FactorInterpreter"
- "init" jinvoke ;
-
-: <interpreter> ( -- interpreter )
- #! Clone the current interpreter. Note that it will be
- #! pointing to exactly the same point of execution!
- [ ] "factor.FactorInterpreter" jnew ;
-
-: fork* ( current new -- thread )
- dup <thread> >r clone-interpreter r> ;
-
-: fork ( -- ? )
- #! Spawn a new thread. In the original thread, push f.
- #! In the new thread, push t.
- interpreter <interpreter> fork* dup current-thread = [
- drop t
- ] [
- start-thread f
- ] ifte ;
-
-: in-thread ( quot -- )
- #! Execute a quotation in a new thread.
- fork [
- [ call ] [ default-error-handler toplevel ] catch
- ] [
- drop
- ] ifte ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: unparser
-USE: kernel
-USE: strings
-USE: stack
-
-: unparse ( X -- "X" )
- [ "java.lang.Object" ] "factor.FactorReader" "unparseObject"
- jinvoke-static ;
-
-: >base ( num radix -- string )
- #! Convert an integer to a string in a certain base.
- swap [ "int" ] "java.math.BigInteger" "toString" jinvoke ;
-
-: >bin ( num -- string )
- #! Convert a number to its binary representation.
- 2 >base ;
-
-: >oct ( num -- string )
- #! Convert a number to its octal representation.
- 8 >base ;
-
-: >hex ( num -- string )
- #! Convert a number to its hexadecimal representation.
- 16 >base ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003, 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: vectors
-USE: kernel
-USE: stack
-
-: <vector> ( size -- vector )
- [ "int" ] "factor.FactorArray" jnew ;
-
-: vector? ( obj -- ? )
- "factor.FactorArray" is ;
-
-: vector-length ( vector -- length )
- "factor.FactorArray" "top" jvar-get ;
-
-: set-vector-length ( vector -- length )
- "factor.FactorArray" "top" jvar-set ;
-
-: vector-nth ( index vector -- )
- [ "int" ] "factor.FactorArray" "get" jinvoke ;
-
-: set-vector-nth ( value index vector -- )
- [ "java.lang.Object" "int" ]
- "factor.FactorArray" "set" jinvoke ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: words
-USE: combinators
-USE: kernel
-USE: lists
-USE: logic
-USE: namespaces
-USE: stack
-
-: search ( name list -- word )
- #! Look up a word in the list of vocabularies.
- swap interpreter
- [ "factor.Cons" "java.lang.String" ]
- "factor.FactorInterpreter"
- "searchVocabulary" jinvoke ;
-
-: create ( name vocab -- word )
- #! Create a word in the vocabulary.
- swap interpreter
- [ "java.lang.String" "java.lang.String" ]
- "factor.FactorInterpreter"
- "define" jinvoke ;
+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2003 Slava Pestov.
-! Copyright (C) 2004 Chris Double.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: words
-USE: combinators
-USE: kernel
-USE: lists
-USE: logic
-USE: namespaces
-USE: stack
-USE: strings
-
-: worddef? ( obj -- boolean )
- "factor.FactorWordDefinition" is ;
-
-: intern ( "word" -- word )
- #! Returns the top of the stack if it already been interned.
- dup string? [ "use" get search ] when ;
-
-: worddef ( word -- worddef )
- dup worddef? [
- intern dup [ [ "def" get ] bind ] when
- ] unless ;
-
-: word-property ( word pname -- pvalue )
- swap [ get ] bind ;
-
-: set-word-property ( word pvalue pname -- )
- rot [ set ] bind ;
-
-: redefine ( word def -- )
- swap [ "def" set ] bind ;
-
-: word? ( obj -- ? )
- "factor.FactorWord" is ;
-
-: compiled? ( worddef -- ? )
- "factor.compiler.CompiledDefinition" is ;
-
-: compound? ( worddef -- ? )
- "factor.FactorCompoundDefinition" is ;
-
-: compound-or-compiled? ( worddef -- ? )
- dup compiled? swap compound? or ;
-
-: symbol? ( worddef -- ? )
- "factor.FactorSymbolDefinition" is ;
-
-: comment? ( obj -- ? )
- "factor.FactorDocComment" is ;
-
-: gensym ( -- word )
- [ ] "factor.FactorWord" "gensym" jinvoke-static ;
-
-: <compound> ( word def -- worddef )
- swap intern swap interpreter
- [ "factor.FactorWord" "factor.Cons" "factor.FactorInterpreter" ]
- "factor.FactorCompoundDefinition"
- jnew ;
-
-: no-name ( list -- word )
- ! Generates an uninternalized word and gives it a compound
- ! definition created from the given list.
- >r gensym dup dup r> <compound> redefine ;
-
-: primitive? ( worddef -- boolean )
- "factor.FactorPrimitiveDefinition" is ;
-
-: shuffle? ( worddef -- boolean )
- "factor.FactorShuffleDefinition" is ;
-
-: word-of-worddef ( worddef -- word )
- "factor.FactorWordDefinition" "word" jvar-get ;
-
-: defined? ( obj -- ? )
- dup word? [ worddef ] [ drop f ] ifte ;
-
-: word-parameter ( worddef -- list )
- worddef interpreter swap
- [ "factor.FactorInterpreter" ] "factor.FactorWordDefinition"
- "toList" jinvoke ;
-
-: skip-docs ( list -- list )
- dup [ dup car comment? [ cdr skip-docs ] when ] when ;
-
-: compound>list ( worddef -- list )
- word-parameter dup [ skip-docs ] when ;
-
-: define-compound ( word def -- )
- #! Define a compound word at runtime.
- >r dup >r [ "vocabulary" get "name" get ] bind r> r>
- <compound> define ;
DEFER: set-word-parameter
DEFER: word-plist
DEFER: set-word-plist
+DEFER: compiled?
IN: unparser
DEFER: (unparse-float)