diff --git a/TODO.FACTOR.txt b/TODO.FACTOR.txt index 3586470651..8aa8c2b966 100644 --- a/TODO.FACTOR.txt +++ b/TODO.FACTOR.txt @@ -1,9 +1,11 @@ + compiler: - type inference fails with some assembler words +- more accurate type inference in some cases - optimize away dispatch +- goal: to compile hash* optimally +- type check/not-check entry points for compiled words - getenv/setenv: if literal arg, compile as a load/store -- update compiler for new assembler + oop: diff --git a/factor/Cons.java b/factor/Cons.java index 4d678a6be0..76412eafce 100644 --- a/factor/Cons.java +++ b/factor/Cons.java @@ -3,7 +3,7 @@ /* * $Id$ * - * Copyright (C) 2003, 2004 Slava Pestov. + * Copyright (C) 2003, 2005 Slava Pestov. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -30,7 +30,7 @@ package factor; /** - * Used to build up linked lists. + * Used to build up linked lists in Factor style. */ public class Cons implements FactorExternalizable { @@ -51,40 +51,41 @@ public class Cons implements FactorExternalizable } //}}} //{{{ contains() method - public boolean contains(Object obj) + public static boolean contains(Cons list, Object obj) { - Cons iter = this; - while(iter != null) + while(list != null) { - if(FactorLib.objectsEqual(obj,iter.car)) + if(FactorLib.objectsEqual(obj,list.car)) return true; - iter = iter.next(); + list = list.next(); } return false; } //}}} - //{{{ contains() method - public static boolean contains(Cons list, Object obj) - { - if(list == null) - return false; - else - return list.contains(obj); - } //}}} - //{{{ length() method - public int length() + public static int length(Cons list) { int size = 0; - Cons iter = this; - while(iter != null) + while(list != null) { - iter = (Cons)iter.cdr; size++; + list = list.next(); } return size; } //}}} + //{{{ 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; + } //}}} + //{{{ elementsToString() method /** * Returns a whitespace separated string of the unparseObject() of each diff --git a/factor/DefaultVocabularyLookup.java b/factor/DefaultVocabularyLookup.java index 88a7bee609..256336c3b6 100644 --- a/factor/DefaultVocabularyLookup.java +++ b/factor/DefaultVocabularyLookup.java @@ -89,7 +89,7 @@ public class DefaultVocabularyLookup implements VocabularyLookup def.parsing = new Def(def); def.docComment = true; FactorWord ine = define("syntax",";"); - ine.parsing = new Ine(def,ine); + ine.parsing = new Ine(ine); FactorWord symbol = define("syntax","SYMBOL:"); symbol.parsing = new Definer(symbol); @@ -120,9 +120,13 @@ public class DefaultVocabularyLookup implements VocabularyLookup FactorWord traits = define("generic","TRAITS:"); traits.parsing = new Definer(traits); FactorWord beginMethod = define("generic","M:"); - beginMethod.parsing = new BeginMethod(beginMethod,def); + beginMethod.parsing = new BeginMethod(beginMethod); FactorWord beginConstructor = define("generic","C:"); - beginConstructor.parsing = new BeginConstructor(beginConstructor,def); + beginConstructor.parsing = new BeginConstructor(beginConstructor); + FactorWord beginPredicate = define("generic","PREDICATE:"); + beginPredicate.parsing = new BeginPredicate(beginPredicate); + FactorWord beginUnion = define("generic","UNION:"); + beginUnion.parsing = new BeginUnion(beginUnion); } //}}} //{{{ getVocabulary() method diff --git a/factor/ExternalFactor.java b/factor/ExternalFactor.java index 09222d3161..6d6f6a6afc 100644 --- a/factor/ExternalFactor.java +++ b/factor/ExternalFactor.java @@ -104,7 +104,7 @@ public class ExternalFactor extends DefaultVocabularyLookup byte[] discard = new byte[2048]; int len = in.read(discard,0,discard.length); discardStr = new String(discard,0,len); - Log.log(Log.DEBUG,this,"Waiting for ACK: " + discardStr); + // Log.log(Log.DEBUG,this,"Waiting for ACK: " + discardStr); } } //}}} @@ -123,7 +123,7 @@ public class ExternalFactor extends DefaultVocabularyLookup */ public synchronized String eval(String cmd) throws IOException { - if(isClosed) + if(isClosed()) throw new IOException("ExternalFactor stream closed"); try diff --git a/factor/FactorArray.java b/factor/FactorArray.java index 4d4160dd9b..839db77c4b 100644 --- a/factor/FactorArray.java +++ b/factor/FactorArray.java @@ -53,7 +53,7 @@ public class FactorArray implements FactorExternalizable //{{{ FactorArray constructor public FactorArray(Cons list) { - this(list == null ? 0 : list.length()); + this(Cons.length(list)); int i = 0; while(list != null) diff --git a/factor/FactorReader.java b/factor/FactorReader.java index 47a24f954b..db5f6e31a5 100644 --- a/factor/FactorReader.java +++ b/factor/FactorReader.java @@ -258,7 +258,7 @@ public class FactorReader //{{{ getDefinedWords() method public Cons getDefinedWords() { - return definedWords; + return Cons.reverse(definedWords); } //}}} //{{{ nextWord() method @@ -381,12 +381,14 @@ public class FactorReader /** * Pop a parser state, throw exception if it doesn't match the * parameter. + * @param start The start parameter that must match. If this is null, + * any start is acceptable. */ public ParseState popState(FactorWord start, FactorWord end) throws FactorParseException { ParseState state = getCurrentState(); - if(state.start != start) + if(start != null && state.start != start) scanner.error(end + " does not close " + state.start); if(states.next() != null) states = states.next(); diff --git a/factor/jedit/ListenerAttributeSet.java b/factor/jedit/ListenerAttributeSet.java index 6c8f4f07b1..0c230e2686 100644 --- a/factor/jedit/ListenerAttributeSet.java +++ b/factor/jedit/ListenerAttributeSet.java @@ -87,10 +87,7 @@ public class ListenerAttributeSet extends SimpleAttributeSet //{{{ createActionsMenu() method private Action[] createActionsMenu(Cons alist) { - if(alist == null) - return null; - - int length = alist.length(); + int length = Cons.length(alist); int i = 0; Action[] actions = new Action[length]; while(alist != null) diff --git a/factor/parser/BeginConstructor.java b/factor/parser/BeginConstructor.java index 5390d79d60..17f9f1697c 100644 --- a/factor/parser/BeginConstructor.java +++ b/factor/parser/BeginConstructor.java @@ -33,12 +33,9 @@ import factor.*; public class BeginConstructor extends FactorParsingDefinition { - private FactorWord colon; - - public BeginConstructor(FactorWord word, FactorWord colon) + public BeginConstructor(FactorWord word) { super(word); - this.colon = colon; } public void eval(FactorReader reader) @@ -48,6 +45,8 @@ public class BeginConstructor extends FactorParsingDefinition if(type == null) return; - reader.pushExclusiveState(colon,type); + reader.intern("<" + type + ">",true); + + reader.pushExclusiveState(word,type); } } diff --git a/factor/parser/BeginMethod.java b/factor/parser/BeginMethod.java index 969101b8d3..776fb44db0 100644 --- a/factor/parser/BeginMethod.java +++ b/factor/parser/BeginMethod.java @@ -33,12 +33,9 @@ import factor.*; public class BeginMethod extends FactorParsingDefinition { - private FactorWord colon; - - public BeginMethod(FactorWord word, FactorWord colon) + public BeginMethod(FactorWord word) { super(word); - this.colon = colon; } public void eval(FactorReader reader) @@ -48,10 +45,10 @@ public class BeginMethod extends FactorParsingDefinition if(type == null) return; - FactorWord generic = reader.nextWord(false); - if(generic == null) + FactorWord newWord = reader.nextWord(false); + if(newWord == null) return; - reader.pushExclusiveState(colon,generic); + reader.pushExclusiveState(word,newWord); } } diff --git a/factor/parser/BeginPredicate.java b/factor/parser/BeginPredicate.java new file mode 100644 index 0000000000..88aa2ea260 --- /dev/null +++ b/factor/parser/BeginPredicate.java @@ -0,0 +1,58 @@ +/* :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 BeginPredicate extends FactorParsingDefinition +{ + public BeginPredicate(FactorWord word) + { + super(word); + } + + public void eval(FactorReader reader) + throws Exception + { + FactorWord supertype = reader.nextWord(false); + if(supertype == null) + return; + + FactorWord type = reader.nextWord(true); + if(type == null) + return; + + type.setDefiner(word); + + reader.intern(type + "?",true); + + reader.pushExclusiveState(word,type); + } +} diff --git a/factor/parser/BeginUnion.java b/factor/parser/BeginUnion.java new file mode 100644 index 0000000000..2496ed9299 --- /dev/null +++ b/factor/parser/BeginUnion.java @@ -0,0 +1,54 @@ +/* :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 BeginUnion extends FactorParsingDefinition +{ + public BeginUnion(FactorWord word) + { + super(word); + } + + public void eval(FactorReader reader) + throws Exception + { + FactorWord type = reader.nextWord(true); + if(type == null) + return; + + type.setDefiner(word); + + reader.intern(type + "?",true); + + reader.pushExclusiveState(word,type); + } +} diff --git a/factor/parser/Def.java b/factor/parser/Def.java index e71a1be04e..12ac74e448 100644 --- a/factor/parser/Def.java +++ b/factor/parser/Def.java @@ -43,9 +43,10 @@ public class Def extends FactorParsingDefinition { FactorWord newWord = reader.nextWord(true); - if(newWord == null) - return; - - reader.pushExclusiveState(word,newWord); + if(newWord != null) + { + newWord.setDefiner(word); + reader.pushExclusiveState(word,newWord); + } } } diff --git a/factor/parser/Ine.java b/factor/parser/Ine.java index e729053eda..5c6ecc0906 100644 --- a/factor/parser/Ine.java +++ b/factor/parser/Ine.java @@ -33,22 +33,16 @@ import factor.*; public class Ine extends FactorParsingDefinition { - public FactorWord start; - - public Ine(FactorWord start, FactorWord end) + public Ine(FactorWord end) { super(end); - this.start = start; } public void eval(FactorReader reader) throws Exception { - FactorReader.ParseState state = reader.popState(start,word); - FactorWord w = state.defining; - /* Only ever null with restartable scanner; - error already logged, so give up */ - if(w != null) - w.setDefiner(start); + FactorReader.ParseState state = reader.popState(null,word); + if(state.defining == null) + reader.getScanner().error(word + " does not close " + state.start); } } diff --git a/library/hashtables.factor b/library/hashtables.factor index f7fedec75d..277459f16e 100644 --- a/library/hashtables.factor +++ b/library/hashtables.factor @@ -48,7 +48,7 @@ PREDICATE: vector hashtable ( obj -- ? ) : (hashcode) ( key table -- index ) #! Compute the index of the bucket for a key. - >r hashcode r> vector-length rem ; + >r hashcode r> vector-length rem ; inline : hash* ( key table -- [ key | value ] ) #! Look up a value in the hashtable. First the bucket is diff --git a/library/math/math.factor b/library/math/math.factor index d784356444..9b713d4d5d 100644 --- a/library/math/math.factor +++ b/library/math/math.factor @@ -92,7 +92,7 @@ M: number = ( n n -- ? ) number= ; : rem ( x y -- x%y ) #! Like modulus, but always gives a positive result. - [ mod ] keep over 0 < [ + ] [ drop ] ifte ; + [ mod ] keep over 0 < [ + ] [ drop ] ifte ; inline : sgn ( n -- -1/0/1 ) #! Push the sign of a real number.