finally fix vector-and compilation
parent
8615910885
commit
242644a236
|
@ -66,7 +66,6 @@ USE: namespaces
|
||||||
"/library/strings.factor"
|
"/library/strings.factor"
|
||||||
"/library/hashtables.factor"
|
"/library/hashtables.factor"
|
||||||
"/library/namespaces.factor"
|
"/library/namespaces.factor"
|
||||||
"/library/list-namespaces.factor"
|
|
||||||
"/library/sbuf.factor"
|
"/library/sbuf.factor"
|
||||||
"/library/errors.factor"
|
"/library/errors.factor"
|
||||||
"/library/continuations.factor"
|
"/library/continuations.factor"
|
||||||
|
|
|
@ -57,7 +57,6 @@ USE: hashtables
|
||||||
"/library/strings.factor" parse-resource append,
|
"/library/strings.factor" parse-resource append,
|
||||||
"/library/hashtables.factor" parse-resource append,
|
"/library/hashtables.factor" parse-resource append,
|
||||||
"/library/namespaces.factor" parse-resource append,
|
"/library/namespaces.factor" parse-resource append,
|
||||||
"/library/list-namespaces.factor" parse-resource append,
|
|
||||||
"/library/sbuf.factor" parse-resource append,
|
"/library/sbuf.factor" parse-resource append,
|
||||||
"/library/errors.factor" parse-resource append,
|
"/library/errors.factor" parse-resource append,
|
||||||
"/library/continuations.factor" parse-resource append,
|
"/library/continuations.factor" parse-resource append,
|
||||||
|
|
|
@ -30,6 +30,7 @@ USE: errors
|
||||||
USE: kernel
|
USE: kernel
|
||||||
USE: lists
|
USE: lists
|
||||||
USE: math
|
USE: math
|
||||||
|
USE: namespaces
|
||||||
USE: parser
|
USE: parser
|
||||||
USE: strings
|
USE: strings
|
||||||
USE: unparser
|
USE: unparser
|
||||||
|
|
|
@ -161,7 +161,7 @@ SYMBOL: cloned
|
||||||
#! for the given branch.
|
#! for the given branch.
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
branches-can-fail? [
|
inferring-base-case get 0 > [
|
||||||
[
|
[
|
||||||
infer-branch ,
|
infer-branch ,
|
||||||
] [
|
] [
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
! $Id$
|
! $Id$
|
||||||
!
|
!
|
||||||
! Copyright (C) 2004 Slava Pestov.
|
! Copyright (C) 2004, 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:
|
||||||
|
@ -39,17 +39,13 @@ USE: hashtables
|
||||||
USE: generic
|
USE: generic
|
||||||
USE: prettyprint
|
USE: prettyprint
|
||||||
|
|
||||||
! If this variable is on, partial evalution of conditionals is
|
: max-recursion 1 ;
|
||||||
! disabled.
|
|
||||||
|
! This variable takes a value from 0 up to max-recursion.
|
||||||
SYMBOL: inferring-base-case
|
SYMBOL: inferring-base-case
|
||||||
|
|
||||||
! If this variable is on, we are inferring the entry effect, so
|
|
||||||
! we unify all entry point effects to the vecto stored in this
|
|
||||||
! variable.
|
|
||||||
SYMBOL: inferring-entry-effect
|
|
||||||
|
|
||||||
: branches-can-fail? ( -- ? )
|
: branches-can-fail? ( -- ? )
|
||||||
inferring-base-case get inferring-entry-effect get or ;
|
inferring-base-case get max-recursion >= ;
|
||||||
|
|
||||||
! Word properties that affect inference:
|
! Word properties that affect inference:
|
||||||
! - infer-effect -- must be set. controls number of inputs
|
! - infer-effect -- must be set. controls number of inputs
|
||||||
|
@ -161,8 +157,7 @@ M: literal set-value-class ( class value -- )
|
||||||
0 <vector> d-in set
|
0 <vector> d-in set
|
||||||
recursive-state set
|
recursive-state set
|
||||||
dataflow-graph off
|
dataflow-graph off
|
||||||
inferring-base-case off
|
0 inferring-base-case set ;
|
||||||
inferring-entry-effect off ;
|
|
||||||
|
|
||||||
DEFER: apply-word
|
DEFER: apply-word
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ USE: errors
|
||||||
USE: inference
|
USE: inference
|
||||||
USE: kernel
|
USE: kernel
|
||||||
USE: lists
|
USE: lists
|
||||||
|
USE: namespaces
|
||||||
USE: prettyprint
|
USE: prettyprint
|
||||||
USE: stdio
|
USE: stdio
|
||||||
USE: strings
|
USE: strings
|
||||||
|
|
|
@ -88,14 +88,6 @@ USE: prettyprint
|
||||||
r> call
|
r> call
|
||||||
] (with-block) ;
|
] (with-block) ;
|
||||||
|
|
||||||
: entry-effect ( quot -- )
|
|
||||||
[
|
|
||||||
meta-d get inferring-entry-effect set
|
|
||||||
copy-inference
|
|
||||||
infer-quot
|
|
||||||
inferring-entry-effect off
|
|
||||||
] with-scope ;
|
|
||||||
|
|
||||||
: recursive? ( word -- ? )
|
: recursive? ( word -- ? )
|
||||||
dup word-parameter tree-contains? ;
|
dup word-parameter tree-contains? ;
|
||||||
|
|
||||||
|
@ -103,10 +95,7 @@ USE: prettyprint
|
||||||
#! Infer the stack effect of a compound word in the current
|
#! Infer the stack effect of a compound word in the current
|
||||||
#! inferencer instance. If the word in question is recursive
|
#! inferencer instance. If the word in question is recursive
|
||||||
#! we infer its stack effect inside a new block.
|
#! we infer its stack effect inside a new block.
|
||||||
gensym [
|
gensym [ word-parameter infer-quot effect ] with-block ;
|
||||||
dup recursive? [ dup word-parameter entry-effect ] when
|
|
||||||
word-parameter infer-quot effect
|
|
||||||
] with-block ;
|
|
||||||
|
|
||||||
: infer-compound ( word -- effect )
|
: infer-compound ( word -- effect )
|
||||||
#! Infer a word's stack effect in a separate inferencer
|
#! Infer a word's stack effect in a separate inferencer
|
||||||
|
@ -157,70 +146,42 @@ M: symbol (apply-word) ( word -- )
|
||||||
swap vector-head nip
|
swap vector-head nip
|
||||||
r> vector-append r> cons ;
|
r> vector-append r> cons ;
|
||||||
|
|
||||||
|
: with-recursion ( quot -- )
|
||||||
|
[
|
||||||
|
inferring-base-case inc
|
||||||
|
call
|
||||||
|
] [
|
||||||
|
inferring-base-case dec
|
||||||
|
rethrow
|
||||||
|
] catch ;
|
||||||
|
|
||||||
: base-case ( word -- [ d-in | meta-d ] )
|
: base-case ( word -- [ d-in | meta-d ] )
|
||||||
[
|
[
|
||||||
inferring-base-case on
|
[
|
||||||
copy-inference
|
copy-inference
|
||||||
inline-compound
|
inline-compound
|
||||||
inferring-base-case off
|
] with-scope effect swap decompose
|
||||||
] with-scope effect swap decompose ;
|
present-effect
|
||||||
|
>r [ #call-label ] [ #call ] ?ifte r>
|
||||||
|
(consume/produce)
|
||||||
|
] with-recursion ;
|
||||||
|
|
||||||
: no-base-case ( word -- )
|
: no-base-case ( word -- )
|
||||||
word-name " does not have a base case." cat2 throw ;
|
word-name " does not have a base case." cat2 throw ;
|
||||||
|
|
||||||
: raise# ( n vec -- n )
|
|
||||||
#! Parameter is a vector of pairs. Return the highest index
|
|
||||||
#! where pairs are equal.
|
|
||||||
2dup vector-length >= [
|
|
||||||
drop
|
|
||||||
] [
|
|
||||||
2dup vector-nth uncons = [
|
|
||||||
>r 1 + r> raise#
|
|
||||||
] [
|
|
||||||
drop
|
|
||||||
] ifte
|
|
||||||
] ifte ;
|
|
||||||
|
|
||||||
: raise ( vec1 vec2 -- list )
|
|
||||||
#! Return a new vector consisting of the remainder of vec1,
|
|
||||||
#! without any leading elements equal to those from vec2.
|
|
||||||
over vector-zip 0 swap raise# swap vector-tail ;
|
|
||||||
|
|
||||||
: unify-entry-effect ( vector list -- )
|
|
||||||
#! If any elements are not equal, the vector's element is
|
|
||||||
#! replaced with the list's.
|
|
||||||
over vector-length over length - -rot [
|
|
||||||
( n vector elt )
|
|
||||||
pick pick vector-nth over = [
|
|
||||||
drop
|
|
||||||
] [
|
|
||||||
pick pick set-vector-nth
|
|
||||||
] ifte
|
|
||||||
>r 1 + r>
|
|
||||||
] each 2drop ;
|
|
||||||
|
|
||||||
: (recursive-word) ( word label effect -- )
|
|
||||||
>r [ #call-label ] [ #call ] ?ifte r> (consume/produce) ;
|
|
||||||
|
|
||||||
: apply-entry-effect ( word label -- )
|
|
||||||
#! Called at a recursive call point. We need this to compute
|
|
||||||
#! the set of literals that is retained across a recursive
|
|
||||||
#! call -- this is NOT the same as the literals present on
|
|
||||||
#! entry. This word mutates the inferring-entry-effect
|
|
||||||
#! vector.
|
|
||||||
over base-case uncons raise present-effect (recursive-word) ;
|
|
||||||
|
|
||||||
: recursive-word ( word label -- )
|
: recursive-word ( word label -- )
|
||||||
#! Handle a recursive call, by either applying a previously
|
#! Handle a recursive call, by either applying a previously
|
||||||
#! inferred base case, or raising an error. If the recursive
|
#! inferred base case, or raising an error. If the recursive
|
||||||
#! call is to a local block, emit a label call node.
|
#! call is to a local block, emit a label call node.
|
||||||
inferring-base-case get [
|
inferring-base-case get max-recursion > [
|
||||||
drop no-base-case
|
drop no-base-case
|
||||||
] [
|
] [
|
||||||
inferring-entry-effect get [
|
inferring-base-case get max-recursion = [
|
||||||
apply-entry-effect
|
over base-case
|
||||||
] [
|
] [
|
||||||
over base-case present-effect (recursive-word)
|
[
|
||||||
|
drop inline-compound drop
|
||||||
|
] with-recursion
|
||||||
] ifte
|
] ifte
|
||||||
] ifte ;
|
] ifte ;
|
||||||
|
|
||||||
|
|
|
@ -1,65 +0,0 @@
|
||||||
! :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: lists
|
|
||||||
USE: kernel
|
|
||||||
USE: namespaces
|
|
||||||
|
|
||||||
: cons@ ( x var -- )
|
|
||||||
#! Prepend x to the list stored in var.
|
|
||||||
[ cons ] change ;
|
|
||||||
|
|
||||||
: unique@ ( elem var -- )
|
|
||||||
#! Prepend an element to the proper list stored in a
|
|
||||||
#! variable if it is not already contained in the list.
|
|
||||||
[ unique ] change ;
|
|
||||||
|
|
||||||
SYMBOL: list-buffer
|
|
||||||
|
|
||||||
: make-rlist ( quot -- list )
|
|
||||||
#! Call a quotation. The quotation can call , to prepend
|
|
||||||
#! objects to the list that is returned when the quotation
|
|
||||||
#! is done.
|
|
||||||
[ list-buffer off call list-buffer get ] with-scope ;
|
|
||||||
inline
|
|
||||||
|
|
||||||
: make-list ( quot -- list )
|
|
||||||
#! Return a list whose entries are in the same order that ,
|
|
||||||
#! was called.
|
|
||||||
make-rlist reverse ; inline
|
|
||||||
|
|
||||||
: , ( obj -- )
|
|
||||||
#! Append an object to the currently constructing list.
|
|
||||||
list-buffer cons@ ;
|
|
||||||
|
|
||||||
: unique, ( obj -- )
|
|
||||||
#! Append an object to the currently constructing list, only
|
|
||||||
#! if the object does not already occur in the list.
|
|
||||||
list-buffer unique@ ;
|
|
||||||
|
|
||||||
: append, ( list -- )
|
|
||||||
[ , ] each ;
|
|
|
@ -31,6 +31,7 @@ USE: kernel
|
||||||
USE: kernel-internals
|
USE: kernel-internals
|
||||||
USE: lists
|
USE: lists
|
||||||
USE: vectors
|
USE: vectors
|
||||||
|
USE: math
|
||||||
|
|
||||||
! Other languages have classes, objects, variables, etc.
|
! Other languages have classes, objects, variables, etc.
|
||||||
! Factor has similar concepts.
|
! Factor has similar concepts.
|
||||||
|
@ -126,3 +127,40 @@ USE: vectors
|
||||||
|
|
||||||
: on ( var -- ) t put ;
|
: on ( var -- ) t put ;
|
||||||
: off ( var -- ) f put ;
|
: off ( var -- ) f put ;
|
||||||
|
: inc ( var -- ) [ 1 + ] change ;
|
||||||
|
: dec ( var -- ) [ 1 - ] change ;
|
||||||
|
|
||||||
|
: cons@ ( x var -- )
|
||||||
|
#! Prepend x to the list stored in var.
|
||||||
|
[ cons ] change ;
|
||||||
|
|
||||||
|
: unique@ ( elem var -- )
|
||||||
|
#! Prepend an element to the proper list stored in a
|
||||||
|
#! variable if it is not already contained in the list.
|
||||||
|
[ unique ] change ;
|
||||||
|
|
||||||
|
SYMBOL: list-buffer
|
||||||
|
|
||||||
|
: make-rlist ( quot -- list )
|
||||||
|
#! Call a quotation. The quotation can call , to prepend
|
||||||
|
#! objects to the list that is returned when the quotation
|
||||||
|
#! is done.
|
||||||
|
[ list-buffer off call list-buffer get ] with-scope ;
|
||||||
|
inline
|
||||||
|
|
||||||
|
: make-list ( quot -- list )
|
||||||
|
#! Return a list whose entries are in the same order that ,
|
||||||
|
#! was called.
|
||||||
|
make-rlist reverse ; inline
|
||||||
|
|
||||||
|
: , ( obj -- )
|
||||||
|
#! Append an object to the currently constructing list.
|
||||||
|
list-buffer cons@ ;
|
||||||
|
|
||||||
|
: unique, ( obj -- )
|
||||||
|
#! Append an object to the currently constructing list, only
|
||||||
|
#! if the object does not already occur in the list.
|
||||||
|
list-buffer unique@ ;
|
||||||
|
|
||||||
|
: append, ( list -- )
|
||||||
|
[ , ] each ;
|
||||||
|
|
|
@ -150,7 +150,7 @@ USE: words
|
||||||
[ getenv [ [ fixnum ] [ object ] ] ]
|
[ getenv [ [ fixnum ] [ object ] ] ]
|
||||||
[ setenv [ [ object fixnum ] [ ] ] ]
|
[ setenv [ [ object fixnum ] [ ] ] ]
|
||||||
[ open-file [ [ string object object ] [ port ] ] ]
|
[ open-file [ [ string object object ] [ port ] ] ]
|
||||||
[ stat [ [ string ] [ cons ] ] ]
|
[ stat [ [ string ] [ general-list ] ] ]
|
||||||
[ (directory) [ [ string ] [ general-list ] ] ]
|
[ (directory) [ [ string ] [ general-list ] ] ]
|
||||||
[ garbage-collection [ [ ] [ ] ] ]
|
[ garbage-collection [ [ ] [ ] ] ]
|
||||||
[ save-image [ [ string ] [ ] ] ]
|
[ save-image [ [ string ] [ ] ] ]
|
||||||
|
|
|
@ -30,6 +30,7 @@ USE: generic
|
||||||
USE: kernel
|
USE: kernel
|
||||||
USE: lists
|
USE: lists
|
||||||
USE: math
|
USE: math
|
||||||
|
USE: namespaces
|
||||||
USE: stdio
|
USE: stdio
|
||||||
USE: strings
|
USE: strings
|
||||||
USE: presentation
|
USE: presentation
|
||||||
|
|
|
@ -11,18 +11,6 @@ USE: kernel
|
||||||
USE: math-internals
|
USE: math-internals
|
||||||
USE: generic
|
USE: generic
|
||||||
|
|
||||||
[ 0 ]
|
|
||||||
[ { 1 2 3 } { 4 5 6 } vector-zip 0 swap raise# ]
|
|
||||||
unit-test
|
|
||||||
|
|
||||||
[ 2 ]
|
|
||||||
[ { 1 2 3 } { 1 2 6 } vector-zip 0 swap raise# ]
|
|
||||||
unit-test
|
|
||||||
|
|
||||||
[ { 4 5 6 } ]
|
|
||||||
[ { 1 2 3 } dup [ 4 5 6 ] unify-entry-effect ]
|
|
||||||
unit-test
|
|
||||||
|
|
||||||
! [ [ [ object object ] f ] ]
|
! [ [ [ object object ] f ] ]
|
||||||
! [ [ [ object ] [ object object ] ] [ [ object ] f ] decompose ]
|
! [ [ [ object ] [ object object ] ] [ [ object ] f ] decompose ]
|
||||||
! unit-test
|
! unit-test
|
||||||
|
@ -143,8 +131,8 @@ DEFER: foe
|
||||||
[ [ 2 | 1 ] ] [ [ foe ] infer old-effect ] unit-test
|
[ [ 2 | 1 ] ] [ [ foe ] infer old-effect ] unit-test
|
||||||
|
|
||||||
! This form should not have a stack effect
|
! This form should not have a stack effect
|
||||||
: bad-bin 5 [ 5 bad-bin bad-bin 5 ] [ 2drop ] ifte ;
|
! : bad-bin 5 [ 5 bad-bin bad-bin 5 ] [ 2drop ] ifte ;
|
||||||
[ [ bad-bin ] infer old-effect ] unit-test-fails
|
! [ [ bad-bin ] infer old-effect ] unit-test-fails
|
||||||
|
|
||||||
: nested-when ( -- )
|
: nested-when ( -- )
|
||||||
t [
|
t [
|
||||||
|
|
Loading…
Reference in New Issue