update Java Factor parser

cvs
Slava Pestov 2005-01-07 19:37:21 +00:00
parent 72ac889e1b
commit d2e68b7f9e
15 changed files with 170 additions and 61 deletions

View File

@ -1,9 +1,11 @@
+ compiler: + compiler:
- type inference fails with some assembler words - type inference fails with some assembler words
- more accurate type inference in some cases
- optimize away dispatch - 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 - getenv/setenv: if literal arg, compile as a load/store
- update compiler for new assembler
+ oop: + oop:

View File

@ -3,7 +3,7 @@
/* /*
* $Id$ * $Id$
* *
* Copyright (C) 2003, 2004 Slava Pestov. * Copyright (C) 2003, 2005 Slava Pestov.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -30,7 +30,7 @@
package factor; package factor;
/** /**
* Used to build up linked lists. * Used to build up linked lists in Factor style.
*/ */
public class Cons implements FactorExternalizable public class Cons implements FactorExternalizable
{ {
@ -51,40 +51,41 @@ public class Cons implements FactorExternalizable
} //}}} } //}}}
//{{{ contains() method //{{{ contains() method
public boolean contains(Object obj) public static boolean contains(Cons list, Object obj)
{ {
Cons iter = this; while(list != null)
while(iter != null)
{ {
if(FactorLib.objectsEqual(obj,iter.car)) if(FactorLib.objectsEqual(obj,list.car))
return true; return true;
iter = iter.next(); list = list.next();
} }
return false; return false;
} //}}} } //}}}
//{{{ contains() method
public static boolean contains(Cons list, Object obj)
{
if(list == null)
return false;
else
return list.contains(obj);
} //}}}
//{{{ length() method //{{{ length() method
public int length() public static int length(Cons list)
{ {
int size = 0; int size = 0;
Cons iter = this; while(list != null)
while(iter != null)
{ {
iter = (Cons)iter.cdr;
size++; size++;
list = list.next();
} }
return size; 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 //{{{ elementsToString() method
/** /**
* Returns a whitespace separated string of the unparseObject() of each * Returns a whitespace separated string of the unparseObject() of each

View File

@ -89,7 +89,7 @@ public class DefaultVocabularyLookup implements VocabularyLookup
def.parsing = new Def(def); def.parsing = new Def(def);
def.docComment = true; def.docComment = true;
FactorWord ine = define("syntax",";"); FactorWord ine = define("syntax",";");
ine.parsing = new Ine(def,ine); ine.parsing = new Ine(ine);
FactorWord symbol = define("syntax","SYMBOL:"); FactorWord symbol = define("syntax","SYMBOL:");
symbol.parsing = new Definer(symbol); symbol.parsing = new Definer(symbol);
@ -120,9 +120,13 @@ public class DefaultVocabularyLookup implements VocabularyLookup
FactorWord traits = define("generic","TRAITS:"); FactorWord traits = define("generic","TRAITS:");
traits.parsing = new Definer(traits); traits.parsing = new Definer(traits);
FactorWord beginMethod = define("generic","M:"); FactorWord beginMethod = define("generic","M:");
beginMethod.parsing = new BeginMethod(beginMethod,def); beginMethod.parsing = new BeginMethod(beginMethod);
FactorWord beginConstructor = define("generic","C:"); 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 //{{{ getVocabulary() method

View File

@ -104,7 +104,7 @@ public class ExternalFactor extends DefaultVocabularyLookup
byte[] discard = new byte[2048]; byte[] discard = new byte[2048];
int len = in.read(discard,0,discard.length); int len = in.read(discard,0,discard.length);
discardStr = new String(discard,0,len); 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 public synchronized String eval(String cmd) throws IOException
{ {
if(isClosed) if(isClosed())
throw new IOException("ExternalFactor stream closed"); throw new IOException("ExternalFactor stream closed");
try try

View File

@ -53,7 +53,7 @@ public class FactorArray implements FactorExternalizable
//{{{ FactorArray constructor //{{{ FactorArray constructor
public FactorArray(Cons list) public FactorArray(Cons list)
{ {
this(list == null ? 0 : list.length()); this(Cons.length(list));
int i = 0; int i = 0;
while(list != null) while(list != null)

View File

@ -258,7 +258,7 @@ public class FactorReader
//{{{ getDefinedWords() method //{{{ getDefinedWords() method
public Cons getDefinedWords() public Cons getDefinedWords()
{ {
return definedWords; return Cons.reverse(definedWords);
} //}}} } //}}}
//{{{ nextWord() method //{{{ nextWord() method
@ -381,12 +381,14 @@ public class FactorReader
/** /**
* Pop a parser state, throw exception if it doesn't match the * Pop a parser state, throw exception if it doesn't match the
* parameter. * parameter.
* @param start The start parameter that must match. If this is null,
* any start is acceptable.
*/ */
public ParseState popState(FactorWord start, FactorWord end) public ParseState popState(FactorWord start, FactorWord end)
throws FactorParseException throws FactorParseException
{ {
ParseState state = getCurrentState(); ParseState state = getCurrentState();
if(state.start != start) if(start != null && state.start != start)
scanner.error(end + " does not close " + state.start); scanner.error(end + " does not close " + state.start);
if(states.next() != null) if(states.next() != null)
states = states.next(); states = states.next();

View File

@ -87,10 +87,7 @@ public class ListenerAttributeSet extends SimpleAttributeSet
//{{{ createActionsMenu() method //{{{ createActionsMenu() method
private Action[] createActionsMenu(Cons alist) private Action[] createActionsMenu(Cons alist)
{ {
if(alist == null) int length = Cons.length(alist);
return null;
int length = alist.length();
int i = 0; int i = 0;
Action[] actions = new Action[length]; Action[] actions = new Action[length];
while(alist != null) while(alist != null)

View File

@ -33,12 +33,9 @@ import factor.*;
public class BeginConstructor extends FactorParsingDefinition public class BeginConstructor extends FactorParsingDefinition
{ {
private FactorWord colon; public BeginConstructor(FactorWord word)
public BeginConstructor(FactorWord word, FactorWord colon)
{ {
super(word); super(word);
this.colon = colon;
} }
public void eval(FactorReader reader) public void eval(FactorReader reader)
@ -48,6 +45,8 @@ public class BeginConstructor extends FactorParsingDefinition
if(type == null) if(type == null)
return; return;
reader.pushExclusiveState(colon,type); reader.intern("<" + type + ">",true);
reader.pushExclusiveState(word,type);
} }
} }

View File

@ -33,12 +33,9 @@ import factor.*;
public class BeginMethod extends FactorParsingDefinition public class BeginMethod extends FactorParsingDefinition
{ {
private FactorWord colon; public BeginMethod(FactorWord word)
public BeginMethod(FactorWord word, FactorWord colon)
{ {
super(word); super(word);
this.colon = colon;
} }
public void eval(FactorReader reader) public void eval(FactorReader reader)
@ -48,10 +45,10 @@ public class BeginMethod extends FactorParsingDefinition
if(type == null) if(type == null)
return; return;
FactorWord generic = reader.nextWord(false); FactorWord newWord = reader.nextWord(false);
if(generic == null) if(newWord == null)
return; return;
reader.pushExclusiveState(colon,generic); reader.pushExclusiveState(word,newWord);
} }
} }

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -43,9 +43,10 @@ public class Def extends FactorParsingDefinition
{ {
FactorWord newWord = reader.nextWord(true); FactorWord newWord = reader.nextWord(true);
if(newWord == null) if(newWord != null)
return; {
newWord.setDefiner(word);
reader.pushExclusiveState(word,newWord); reader.pushExclusiveState(word,newWord);
}
} }
} }

View File

@ -33,22 +33,16 @@ import factor.*;
public class Ine extends FactorParsingDefinition public class Ine extends FactorParsingDefinition
{ {
public FactorWord start; public Ine(FactorWord end)
public Ine(FactorWord start, FactorWord end)
{ {
super(end); super(end);
this.start = start;
} }
public void eval(FactorReader reader) public void eval(FactorReader reader)
throws Exception throws Exception
{ {
FactorReader.ParseState state = reader.popState(start,word); FactorReader.ParseState state = reader.popState(null,word);
FactorWord w = state.defining; if(state.defining == null)
/* Only ever null with restartable scanner; reader.getScanner().error(word + " does not close " + state.start);
error already logged, so give up */
if(w != null)
w.setDefiner(start);
} }
} }

View File

@ -48,7 +48,7 @@ PREDICATE: vector hashtable ( obj -- ? )
: (hashcode) ( key table -- index ) : (hashcode) ( key table -- index )
#! Compute the index of the bucket for a key. #! 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 ] ) : hash* ( key table -- [ key | value ] )
#! Look up a value in the hashtable. First the bucket is #! Look up a value in the hashtable. First the bucket is

View File

@ -92,7 +92,7 @@ M: number = ( n n -- ? ) number= ;
: rem ( x y -- x%y ) : rem ( x y -- x%y )
#! Like modulus, but always gives a positive result. #! 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 ) : sgn ( n -- -1/0/1 )
#! Push the sign of a real number. #! Push the sign of a real number.