command to infer stack effects of all words in file
parent
fd64bc4ccc
commit
c56ca0ca1f
|
@ -82,4 +82,10 @@
|
||||||
FactorPlugin.extractWord(view);
|
FactorPlugin.extractWord(view);
|
||||||
</CODE>
|
</CODE>
|
||||||
</ACTION>
|
</ACTION>
|
||||||
|
<ACTION NAME="factor-infer-effects">
|
||||||
|
<CODE>
|
||||||
|
InferBufferProcessor.createInferUnitTests(view,buffer,
|
||||||
|
FactorPlugin.getExternalInstance());
|
||||||
|
</CODE>
|
||||||
|
</ACTION>
|
||||||
</ACTIONS>
|
</ACTIONS>
|
||||||
|
|
|
@ -38,22 +38,15 @@ import org.gjt.sp.jedit.Buffer;
|
||||||
* A class used to compile all words in a file, or infer stack effects of all
|
* A class used to compile all words in a file, or infer stack effects of all
|
||||||
* words in a file, etc.
|
* words in a file, etc.
|
||||||
*/
|
*/
|
||||||
public class FactorBufferProcessor
|
public abstract class FactorBufferProcessor
|
||||||
{
|
{
|
||||||
private String code;
|
|
||||||
private LinkedHashMap results;
|
private LinkedHashMap results;
|
||||||
|
|
||||||
//{{{ FactorBufferProcessor constructor
|
//{{{ FactorBufferProcessor constructor
|
||||||
/**
|
public FactorBufferProcessor(Buffer buffer, ExternalFactor factor)
|
||||||
* @param buffer The buffer
|
throws Exception
|
||||||
* @param code The snippet of code to apply to each word. The snippet
|
|
||||||
* should print a string.
|
|
||||||
*/
|
|
||||||
public FactorBufferProcessor(Buffer buffer, String code, ExternalFactor factor)
|
|
||||||
throws IOException
|
|
||||||
{
|
{
|
||||||
results = new LinkedHashMap();
|
results = new LinkedHashMap();
|
||||||
this.code = code;
|
|
||||||
|
|
||||||
Cons words = (Cons)buffer.getProperty(
|
Cons words = (Cons)buffer.getProperty(
|
||||||
FactorSideKickParser.WORDS_PROPERTY);
|
FactorSideKickParser.WORDS_PROPERTY);
|
||||||
|
@ -61,33 +54,26 @@ public class FactorBufferProcessor
|
||||||
while(words != null)
|
while(words != null)
|
||||||
{
|
{
|
||||||
FactorWord word = (FactorWord)words.car;
|
FactorWord word = (FactorWord)words.car;
|
||||||
|
String expr = processWord(word);
|
||||||
StringBuffer expression = new StringBuffer();
|
System.err.println(expr);
|
||||||
expression.append(FactorPlugin.factorWord(word));
|
results.put(word,factor.eval(expr));
|
||||||
expression.append(" ");
|
|
||||||
expression.append(code);
|
|
||||||
|
|
||||||
results.put(word,factor.eval(expression.toString()));
|
|
||||||
|
|
||||||
words = words.next();
|
words = words.next();
|
||||||
}
|
}
|
||||||
} //}}}
|
} //}}}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Code to process the word.
|
||||||
|
*/
|
||||||
|
public abstract String processWord(FactorWord word);
|
||||||
|
|
||||||
//{{{ insertResults() method
|
//{{{ insertResults() method
|
||||||
public void insertResults(Buffer buffer, int offset)
|
public void insertResults(Buffer buffer, int offset)
|
||||||
|
throws Exception
|
||||||
{
|
{
|
||||||
StringBuffer result = new StringBuffer();
|
StringBuffer result = new StringBuffer();
|
||||||
Iterator iter = results.entrySet().iterator();
|
Iterator iter = results.values().iterator();
|
||||||
while(iter.hasNext())
|
while(iter.hasNext())
|
||||||
{
|
result.append(iter.next());
|
||||||
Map.Entry entry = (Map.Entry)iter.next();
|
buffer.insert(offset,result.toString().trim());
|
||||||
result.append("[ ");
|
|
||||||
result.append(((String)entry.getValue()).trim());
|
|
||||||
result.append(" ] [ \\ ");
|
|
||||||
result.append(FactorReader.unparseObject(entry.getKey()));
|
|
||||||
result.append(code);
|
|
||||||
result.append(" ] unit-test\n");
|
|
||||||
}
|
|
||||||
buffer.insert(offset,result.toString());
|
|
||||||
} //}}}
|
} //}}}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,8 @@ plugin.factor.jedit.FactorPlugin.menu=factor-listener \
|
||||||
- \
|
- \
|
||||||
factor-extract-word \
|
factor-extract-word \
|
||||||
- \
|
- \
|
||||||
|
factor-infer-effects \
|
||||||
|
- \
|
||||||
factor-restart
|
factor-restart
|
||||||
|
|
||||||
factor-listener.label=Listener
|
factor-listener.label=Listener
|
||||||
|
@ -41,6 +43,7 @@ factor-edit.label=Edit word at caret
|
||||||
factor-edit-dialog.label=Edit word...
|
factor-edit-dialog.label=Edit word...
|
||||||
factor-usages.label=Word usages at caret
|
factor-usages.label=Word usages at caret
|
||||||
factor-extract-word.label=Extract word...
|
factor-extract-word.label=Extract word...
|
||||||
|
factor-infer-effects.label=Infer word stack effects...
|
||||||
factor-restart.label=Restart Factor
|
factor-restart.label=Restart Factor
|
||||||
|
|
||||||
# SideKick stuff
|
# SideKick stuff
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
/* :folding=explicit:collapseFolds=1: */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* $Id$
|
||||||
|
*
|
||||||
|
* Copyright (C) 2005 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.jedit;
|
||||||
|
|
||||||
|
import factor.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.*;
|
||||||
|
import org.gjt.sp.jedit.io.VFSManager;
|
||||||
|
import org.gjt.sp.jedit.*;
|
||||||
|
import org.gjt.sp.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class used to compile all words in a file, or infer stack effects of all
|
||||||
|
* words in a file, etc.
|
||||||
|
*/
|
||||||
|
public class InferBufferProcessor extends FactorBufferProcessor
|
||||||
|
{
|
||||||
|
//{{{ createInferUnitTests() method
|
||||||
|
public static void createInferUnitTests(View view,
|
||||||
|
final Buffer buffer,
|
||||||
|
final ExternalFactor factor)
|
||||||
|
{
|
||||||
|
final Buffer newBuffer = jEdit.newFile(view);
|
||||||
|
VFSManager.runInAWTThread(new Runnable()
|
||||||
|
{
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
newBuffer.setMode("factor");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
new InferBufferProcessor(buffer,factor)
|
||||||
|
.insertResults(newBuffer,0);
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
Log.log(Log.ERROR,this,e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} //}}}
|
||||||
|
|
||||||
|
//{{{ InferBufferProcessor constructor
|
||||||
|
public InferBufferProcessor(Buffer buffer, ExternalFactor factor)
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
super(buffer,factor);
|
||||||
|
} //}}}
|
||||||
|
|
||||||
|
//{{{ processWord() method
|
||||||
|
/**
|
||||||
|
* @return Code to process the word.
|
||||||
|
*/
|
||||||
|
public String processWord(FactorWord word)
|
||||||
|
{
|
||||||
|
StringBuffer expression = new StringBuffer();
|
||||||
|
expression.append(FactorPlugin.factorWord(word));
|
||||||
|
expression.append(" unit infer>test print");
|
||||||
|
return expression.toString();
|
||||||
|
} //}}}
|
||||||
|
}
|
|
@ -118,6 +118,7 @@ USE: namespaces
|
||||||
"/library/inference/words.factor"
|
"/library/inference/words.factor"
|
||||||
"/library/inference/stack.factor"
|
"/library/inference/stack.factor"
|
||||||
"/library/inference/types.factor"
|
"/library/inference/types.factor"
|
||||||
|
"/library/inference/test.factor"
|
||||||
|
|
||||||
"/library/compiler/assembler.factor"
|
"/library/compiler/assembler.factor"
|
||||||
"/library/compiler/xt.factor"
|
"/library/compiler/xt.factor"
|
||||||
|
|
|
@ -188,10 +188,6 @@ DEFER: apply-word
|
||||||
#! Stack effect of a quotation.
|
#! Stack effect of a quotation.
|
||||||
[ (infer) effect ] with-scope ;
|
[ (infer) effect ] with-scope ;
|
||||||
|
|
||||||
: try-infer ( quot -- effect/f )
|
|
||||||
#! Push f if inference fails.
|
|
||||||
[ infer ] [ [ drop f ] when ] catch ;
|
|
||||||
|
|
||||||
: dataflow ( quot -- dataflow )
|
: dataflow ( quot -- dataflow )
|
||||||
#! Data flow of a quotation.
|
#! Data flow of a quotation.
|
||||||
[ (infer) get-dataflow ] with-scope ;
|
[ (infer) get-dataflow ] with-scope ;
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
! :folding=indent:collapseFolds=1:
|
||||||
|
|
||||||
|
! $Id$
|
||||||
|
!
|
||||||
|
! Copyright (C) 2005 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: test
|
||||||
|
USE: errors
|
||||||
|
USE: inference
|
||||||
|
USE: kernel
|
||||||
|
USE: lists
|
||||||
|
USE: prettyprint
|
||||||
|
USE: stdio
|
||||||
|
USE: strings
|
||||||
|
USE: unparser
|
||||||
|
|
||||||
|
: try-infer ( quot -- effect error )
|
||||||
|
[ infer f ] [ [ >r drop f r> ] when* ] catch ;
|
||||||
|
|
||||||
|
: infer-fail ( quot error -- )
|
||||||
|
"! " , dup string? [ unparse ] unless , "\n" ,
|
||||||
|
[ [ infer ] cons . \ unit-test-fails . ] with-string , ;
|
||||||
|
|
||||||
|
: infer-pass ( quot effect -- )
|
||||||
|
[ unit . [ infer ] cons . \ unit-test . ] with-string , ;
|
||||||
|
|
||||||
|
: infer>test ( quot -- str )
|
||||||
|
#! Make a string representing a unit test for the stack
|
||||||
|
#! effect of a word.
|
||||||
|
[
|
||||||
|
dup try-infer [ infer-fail ] [ infer-pass ] ?ifte
|
||||||
|
] make-string ;
|
|
@ -1,7 +1,5 @@
|
||||||
! Factor test suite.
|
! Factor test suite.
|
||||||
|
|
||||||
! Some of these words should be moved to the standard library.
|
|
||||||
|
|
||||||
IN: test
|
IN: test
|
||||||
USE: errors
|
USE: errors
|
||||||
USE: kernel
|
USE: kernel
|
||||||
|
|
Loading…
Reference in New Issue