update Java Factor parser
parent
72ac889e1b
commit
d2e68b7f9e
|
@ -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:
|
||||
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
@ -50,41 +50,42 @@ public class Cons implements FactorExternalizable
|
|||
return (Cons)cdr;
|
||||
} //}}}
|
||||
|
||||
//{{{ contains() method
|
||||
public boolean contains(Object obj)
|
||||
{
|
||||
Cons iter = this;
|
||||
while(iter != null)
|
||||
{
|
||||
if(FactorLib.objectsEqual(obj,iter.car))
|
||||
return true;
|
||||
iter = iter.next();
|
||||
}
|
||||
return false;
|
||||
} //}}}
|
||||
|
||||
//{{{ contains() method
|
||||
public static boolean contains(Cons list, Object obj)
|
||||
{
|
||||
if(list == null)
|
||||
while(list != null)
|
||||
{
|
||||
if(FactorLib.objectsEqual(obj,list.car))
|
||||
return true;
|
||||
list = list.next();
|
||||
}
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -43,9 +43,10 @@ public class Def extends FactorParsingDefinition
|
|||
{
|
||||
FactorWord newWord = reader.nextWord(true);
|
||||
|
||||
if(newWord == null)
|
||||
return;
|
||||
|
||||
if(newWord != null)
|
||||
{
|
||||
newWord.setDefiner(word);
|
||||
reader.pushExclusiveState(word,newWord);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue