sequence protocol

cvs
Slava Pestov 2005-04-02 07:39:33 +00:00
parent d57b44b4eb
commit f39394d25e
38 changed files with 206 additions and 221 deletions

View File

@ -1,7 +1,7 @@
! Copyright (C) 2005 Slava Pestov. ! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: kernel-internals IN: kernel-internals
USING: generic math-internals kernel lists vectors ; USING: generic kernel lists math-internals sequences vectors ;
! An array is a range of memory storing pointers to other ! An array is a range of memory storing pointers to other
! objects. Arrays are not used directly, and their access words ! objects. Arrays are not used directly, and their access words
@ -23,15 +23,6 @@ BUILTIN: array 8 [ 1 "array-capacity" f ] ;
#! Unsafe. #! Unsafe.
swap 2 fixnum+ set-slot ; inline swap 2 fixnum+ set-slot ; inline
: (array>list) ( n i array -- list ) M: array length array-capacity ;
#! Unsafe. M: array nth array-nth ;
pick pick fixnum<= [ M: array set-nth set-array-nth ;
3drop [ ]
] [
2dup array-nth >r >r 1 fixnum+ r> (array>list) r>
swap cons
] ifte ;
: array>list ( n array -- list )
#! Unsafe.
0 swap (array>list) ;

View File

@ -16,6 +16,7 @@ hashtables ;
"/version.factor" "/version.factor"
"/library/stack.factor" "/library/stack.factor"
"/library/combinators.factor" "/library/combinators.factor"
"/library/sequences.factor"
"/library/arrays.factor" "/library/arrays.factor"
"/library/kernel.factor" "/library/kernel.factor"
"/library/cons.factor" "/library/cons.factor"
@ -28,6 +29,7 @@ hashtables ;
"/library/lists.factor" "/library/lists.factor"
"/library/vectors.factor" "/library/vectors.factor"
"/library/strings.factor" "/library/strings.factor"
"/library/sequences-epilogue.factor"
"/library/hashtables.factor" "/library/hashtables.factor"
"/library/namespaces.factor" "/library/namespaces.factor"
"/library/words.factor" "/library/words.factor"

View File

@ -16,7 +16,8 @@
IN: image IN: image
USING: errors generic hashtables kernel lists math namespaces USING: errors generic hashtables kernel lists math namespaces
parser prettyprint stdio streams strings vectors words ; parser prettyprint sequences sequences stdio streams strings
vectors words ;
! The image being constructed; a vector of word-size integers ! The image being constructed; a vector of word-size integers
SYMBOL: image SYMBOL: image
@ -196,7 +197,7 @@ M: cons ' ( c -- tagged )
tuck string-length - CHAR: \0 fill cat2 ; tuck string-length - CHAR: \0 fill cat2 ;
: emit-chars ( str -- ) : emit-chars ( str -- )
string>list "big-endian" get [ reverse ] unless >list "big-endian" get [ reverse ] unless
0 swap [ swap 16 shift + ] each emit ; 0 swap [ swap 16 shift + ] each emit ;
: (pack-string) ( n list -- ) : (pack-string) ( n list -- )
@ -235,7 +236,7 @@ M: string ' ( string -- pointer )
align-here r> ; align-here r> ;
: emit-vector ( vector -- pointer ) : emit-vector ( vector -- pointer )
dup vector>list emit-array swap vector-length dup >list emit-array swap vector-length
object-tag here-as >r object-tag here-as >r
vector-type >header emit vector-type >header emit
emit-fixnum ( length ) emit-fixnum ( length )
@ -309,7 +310,7 @@ M: hashtable ' ( hashtable -- pointer )
] ifte ; ] ifte ;
: write-image ( image file -- ) : write-image ( image file -- )
<file-writer> [ [ write-word ] vector-each ] with-stream ; <file-writer> [ [ write-word ] seq-each ] with-stream ;
: with-minimal-image ( quot -- image ) : with-minimal-image ( quot -- image )
[ [
@ -323,7 +324,7 @@ M: hashtable ' ( hashtable -- pointer )
#! The quotation leaves a boot quotation on the stack. #! The quotation leaves a boot quotation on the stack.
[ begin call end ] with-minimal-image ; [ begin call end ] with-minimal-image ;
: test-image ( quot -- ) with-image vector>list . ; : test-image ( quot -- ) with-image >list . ;
: make-image ( name -- ) : make-image ( name -- )
#! Make an image for the C interpreter. #! Make an image for the C interpreter.

View File

@ -2,8 +2,8 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: alien IN: alien
USING: assembler compiler errors generic hashtables inference USING: assembler compiler errors generic hashtables inference
interpreter kernel lists math namespaces parser stdio strings interpreter kernel lists math namespaces parser sequences stdio
unparser words ; strings unparser words ;
! ! ! WARNING ! ! ! ! ! ! WARNING ! ! !
! Reloading this file into a running Factor instance on Win32 ! Reloading this file into a running Factor instance on Win32

View File

@ -1,8 +1,8 @@
! Copyright (C) 2004, 2005 Slava Pestov. ! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: compiler IN: compiler
USING: assembler inference errors kernel lists math namespaces USING: assembler errors inference kernel lists math namespaces
strings words vectors ; sequences strings vectors words ;
: generate-node ( [[ op params ]] -- ) : generate-node ( [[ op params ]] -- )
#! Generate machine code for a node. #! Generate machine code for a node.
@ -22,7 +22,7 @@ strings words vectors ;
: generate-reloc ( -- length ) : generate-reloc ( -- length )
relocation-table get relocation-table get
dup [ compile-cell ] vector-each dup [ compile-cell ] seq-each
vector-length cell * ; vector-length cell * ;
: (generate) ( word linear -- ) : (generate) ( word linear -- )

View File

@ -1,39 +1,8 @@
! :folding=indent:collapseFolds=1: ! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
! $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.
IN: compiler IN: compiler
USE: lists USING: inference kernel kernel-internals lists namespaces
USE: namespaces sequences vectors words words ;
USE: kernel
USE: inference
USE: words
USE: prettyprint
USE: kernel-internals
USE: vectors
! The optimizer transforms dataflow IR to dataflow IR. Currently ! The optimizer transforms dataflow IR to dataflow IR. Currently
! it removes literals that are eventually dropped, and never ! it removes literals that are eventually dropped, and never
@ -104,7 +73,7 @@ SYMBOL: branch-returns
dup [ dup [
last [ node-consume-d get list>vector ] bind last [ node-consume-d get list>vector ] bind
] map ] map
unify-stacks vector>list unify-stacks >list
branch-returns set branch-returns set
[ dupd can-kill? ] all? nip [ dupd can-kill? ] all? nip
] with-scope ] with-scope

View File

@ -34,6 +34,7 @@ USE: generic
USE: lists USE: lists
USE: math USE: math
USE: errors USE: errors
USE: sequences
! A postfix assembler. ! A postfix assembler.
! !

View File

@ -5,7 +5,7 @@
! implement tuples, as well as builtin types. ! implement tuples, as well as builtin types.
IN: generic IN: generic
USING: kernel kernel-internals lists math namespaces parser USING: kernel kernel-internals lists math namespaces parser
strings words ; sequences strings words ;
: simple-generic ( class generic def -- ) : simple-generic ( class generic def -- )
#! Just like: #! Just like:

View File

@ -2,7 +2,7 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: kernel-internals IN: kernel-internals
USING: words parser kernel namespaces lists strings math USING: words parser kernel namespaces lists strings math
hashtables errors vectors ; hashtables errors sequences vectors ;
! Tuples are really arrays in the runtime, but with a different ! Tuples are really arrays in the runtime, but with a different
! type number. The layout is as follows: ! type number. The layout is as follows:
@ -36,6 +36,15 @@ M: tuple delegate 3 slot ;
M: object set-delegate 2drop ; M: object set-delegate 2drop ;
M: tuple set-delegate 3 set-slot ; M: tuple set-delegate 3 set-slot ;
: check-array ( n array -- )
array-capacity 0 swap between? [
"Array index out of bounds" throw
] unless ;
M: tuple length array-capacity ;
M: tuple nth 2dup check-array array-nth ;
M: tuple set-nth 2dup check-array set-array-nth ;
#! arrayed objects can be passed to array-capacity, #! arrayed objects can be passed to array-capacity,
#! array-nth, and set-array-nth. #! array-nth, and set-array-nth.
UNION: arrayed array tuple ; UNION: arrayed array tuple ;
@ -168,27 +177,13 @@ M: tuple clone ( tuple -- tuple )
#! Clone a tuple and its delegate. #! Clone a tuple and its delegate.
clone-tuple dup delegate clone over set-delegate ; clone-tuple dup delegate clone over set-delegate ;
: tuple>list ( tuple -- list )
dup array-capacity swap array>list ;
M: tuple = ( obj tuple -- ? )
over tuple? [
over class-tuple over class-tuple eq? [
swap tuple>list swap tuple>list =
] [
2drop f
] ifte
] [
2drop f
] ifte ;
M: tuple hashcode ( vec -- n ) M: tuple hashcode ( vec -- n )
#! If the capacity is two, then all we have is the class #! If the capacity is two, then all we have is the class
#! slot and delegate. #! slot and delegate.
dup array-capacity 2 number= [ dup length 2 number= [
drop 0 drop 0
] [ ] [
2 swap array-nth hashcode 2 swap nth hashcode
] ifte ; ] ifte ;
tuple [ tuple [

View File

@ -7,7 +7,7 @@ DEFER: set-hash-array
DEFER: set-hash-size DEFER: set-hash-size
IN: hashtables IN: hashtables
USING: generic kernel lists math vectors ; USING: generic kernel lists math sequences vectors ;
! We put hash-size in the hashtables vocabulary, and ! We put hash-size in the hashtables vocabulary, and
! the other words in kernel-internals. ! the other words in kernel-internals.
@ -117,7 +117,7 @@ IN: hashtables
: buckets>list ( hash -- list ) : buckets>list ( hash -- list )
#! Push a list of key/value pairs in a hashtable. #! Push a list of key/value pairs in a hashtable.
dup bucket-count swap hash-array array>list ; hash-array >list ;
: alist>hash ( alist -- hash ) : alist>hash ( alist -- hash )
dup length 1 max <hashtable> swap dup length 1 max <hashtable> swap

View File

@ -2,7 +2,7 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: inference IN: inference
USING: errors generic interpreter kernel lists math namespaces USING: errors generic interpreter kernel lists math namespaces
strings vectors words hashtables prettyprint ; sequences strings vectors words hashtables prettyprint ;
: longest-vector ( list -- length ) : longest-vector ( list -- length )
[ vector-length ] map [ > ] top ; [ vector-length ] map [ > ] top ;
@ -213,7 +213,7 @@ SYMBOL: cloned
\ ifte [ infer-ifte ] "infer" set-word-prop \ ifte [ infer-ifte ] "infer" set-word-prop
: vtable>list ( value -- list ) : vtable>list ( value -- list )
dup value-recursion swap literal-value vector>list dup value-recursion swap literal-value >list
[ over <literal> ] map nip ; [ over <literal> ] map nip ;
: <dispatch-index> ( value -- value ) : <dispatch-index> ( value -- value )

View File

@ -33,6 +33,7 @@ USE: math
USE: namespaces USE: namespaces
USE: words USE: words
USE: vectors USE: vectors
USE: sequences
! We build a dataflow graph for the compiler. ! We build a dataflow graph for the compiler.
SYMBOL: dataflow-graph SYMBOL: dataflow-graph

View File

@ -2,7 +2,7 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: inference IN: inference
USING: errors generic interpreter kernel lists math namespaces USING: errors generic interpreter kernel lists math namespaces
prettyprint strings unparser vectors words ; prettyprint sequences strings unparser vectors words ;
: max-recursion 0 ; : max-recursion 0 ;
@ -114,7 +114,7 @@ M: computed literal-value ( value -- )
d-in [ vector-prepend ] change ; d-in [ vector-prepend ] change ;
: (present-effect) ( vector -- list ) : (present-effect) ( vector -- list )
vector>list [ value-class ] map ; >list [ value-class ] map ;
: present-effect ( [[ d-in meta-d ]] -- [ in-types out-types ] ) : present-effect ( [[ d-in meta-d ]] -- [ in-types out-types ] )
#! After inference is finished, collect information. #! After inference is finished, collect information.
@ -184,7 +184,7 @@ M: object apply-object apply-literal ;
: values-node ( op -- ) : values-node ( op -- )
#! Add a #values or #return node to the graph. #! Add a #values or #return node to the graph.
f swap dataflow, [ f swap dataflow, [
meta-d get vector>list node-consume-d set meta-d get >list node-consume-d set
] bind ; ] bind ;
: (infer) ( quot -- ) : (infer) ( quot -- )

View File

@ -2,7 +2,8 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: inference IN: inference
USING: errors generic interpreter kernel kernel-internals USING: errors generic interpreter kernel kernel-internals
lists math namespaces strings vectors words stdio prettyprint ; lists math namespaces strings vectors words sequences
stdio prettyprint ;
: fast-slot? ( -- ? ) : fast-slot? ( -- ? )
#! If the slot number is literal and the object's type is #! If the slot number is literal and the object's type is

View File

@ -2,7 +2,7 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: inference IN: inference
USING: errors generic interpreter kernel lists math namespaces USING: errors generic interpreter kernel lists math namespaces
strings vectors words hashtables parser prettyprint ; sequences strings vectors words hashtables parser prettyprint ;
: with-dataflow ( param op [[ in# out# ]] quot -- ) : with-dataflow ( param op [[ in# out# ]] quot -- )
#! Take input parameters, execute quotation, take output #! Take input parameters, execute quotation, take output

View File

@ -1,6 +1,10 @@
! Copyright (C) 2003, 2005 Slava Pestov. ! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: lists USING: generic kernel math ; IN: lists USING: generic kernel math sequences ;
! Sequence protocol
M: cons length 0 swap [ drop 1 + ] each ;
M: f length drop 0 ;
: 2list ( a b -- [ a b ] ) : 2list ( a b -- [ a b ] )
unit cons ; unit cons ;
@ -83,9 +87,6 @@ IN: lists USING: generic kernel math ;
#! Remove all occurrences of the object from the list. #! Remove all occurrences of the object from the list.
[ eq? not ] subset-with ; [ eq? not ] subset-with ;
: length ( list -- length )
0 swap [ drop 1 + ] each ;
: prune ( list -- list ) : prune ( list -- list )
#! Remove duplicate elements. #! Remove duplicate elements.
dup [ dup [

View File

@ -1,6 +1,12 @@
! Copyright (C) 2004, 2005 Slava Pestov. ! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: strings USING: kernel lists math namespaces strings ; IN: strings
USING: kernel lists math namespaces sequences strings ;
M: sbuf length sbuf-length ;
M: sbuf set-length set-sbuf-length ;
M: sbuf nth sbuf-nth ;
M: sbuf set-nth set-sbuf-nth ;
: fill ( count char -- string ) : fill ( count char -- string )
#! Push a string that consists of the same character #! Push a string that consists of the same character
@ -18,7 +24,7 @@ IN: strings USING: kernel lists math namespaces strings ;
#! Apply a quotation to each character in the string, and #! Apply a quotation to each character in the string, and
#! push a new string constructed from return values. #! push a new string constructed from return values.
#! The quotation must have stack effect ( X -- X ). #! The quotation must have stack effect ( X -- X ).
>r string>list r> map cat ; inline >r >list r> map cat ; inline
: split-next ( index string split -- next ) : split-next ( index string split -- next )
3dup index-of* dup -1 = [ 3dup index-of* dup -1 = [
@ -55,3 +61,6 @@ IN: strings USING: kernel lists math namespaces strings ;
: ch>string ( ch -- str ) : ch>string ( ch -- str )
1 <sbuf> [ sbuf-append ] keep sbuf>string ; 1 <sbuf> [ sbuf-append ] keep sbuf>string ;
: string>sbuf ( str -- sbuf )
dup string-length <sbuf> [ sbuf-append ] keep ;

View File

@ -0,0 +1,41 @@
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: sequences
USING: generic kernel kernel-internals lists math strings
vectors ;
! This is loaded once everything else is available.
UNION: sequence array vector string sbuf tuple ;
M: object (>list) ( n i seq -- list )
pick pick <= [
3drop [ ]
] [
2dup nth >r >r 1 + r> (>list) r> swons
] ifte ;
M: vector (>list) vector-array (>list) ;
: seq-each ( seq quot -- )
>r >list r> each ; inline
: seq-each-with ( obj seq quot -- )
swap [ with ] seq-each 2drop ; inline
: length= ( seq seq -- ? )
length swap length number= ;
M: sequence = ( obj seq -- ? )
2dup eq? [
2drop t
] [
over sequence? [
2dup length= [
swap >list swap >list =
] [
2drop f
] ifte
] [
2drop f
] ifte
] ifte ;

21
library/sequences.factor Normal file
View File

@ -0,0 +1,21 @@
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: sequences
USING: generic kernel kernel-internals math strings
vectors ;
! This file is needed very early in bootstrap.
! Sequences support the following protocol. Concrete examples
! are strings, string buffers, vectors, and arrays. Arrays are
! low level and not bounds-checked; they are in the
! kernel-internals vocabulary, so don't use them unless you have
! a good reason.
GENERIC: length ( sequence -- n )
GENERIC: set-length ( n sequence -- )
GENERIC: nth ( n sequence -- obj )
GENERIC: set-nth ( value n sequence -- obj )
GENERIC: (>list) ( n i seq -- list )
: >list ( seq -- list ) dup length 0 rot (>list) ;

View File

@ -1,6 +1,7 @@
! Copyright (C) 2003, 2005 Slava Pestov. ! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: strings USING: generic kernel kernel-internals lists math ; IN: strings USING: generic kernel kernel-internals lists math
sequences ;
BUILTIN: string 12 [ 1 "string-length" f ] [ 2 hashcode f ] ; BUILTIN: string 12 [ 1 "string-length" f ] [ 2 hashcode f ] ;
M: string = string= ; M: string = string= ;
@ -10,6 +11,9 @@ M: sbuf = sbuf= ;
UNION: text string integer ; UNION: text string integer ;
M: string length string-length ;
M: string nth string-nth ;
: f-or-"" ( obj -- ? ) : f-or-"" ( obj -- ? )
dup not swap "" = or ; dup not swap "" = or ;
@ -99,21 +103,6 @@ UNION: text string integer ;
rot string-head swap rot string-head swap
] ifte ; ] ifte ;
: (string>list) ( i str -- list )
2dup string-length >= [
2drop [ ]
] [
2dup string-nth >r >r 1 + r> (string>list) r> swons
] ifte ;
: string>list ( str -- list )
0 swap (string>list) ;
: string-each ( str quot -- )
#! Execute the quotation with each character of the string
#! pushed onto the stack.
>r string>list r> each ; inline
PREDICATE: integer blank " \t\n\r" string-contains? ; PREDICATE: integer blank " \t\n\r" string-contains? ;
PREDICATE: integer letter CHAR: a CHAR: z between? ; PREDICATE: integer letter CHAR: a CHAR: z between? ;
PREDICATE: integer LETTER CHAR: A CHAR: Z between? ; PREDICATE: integer LETTER CHAR: A CHAR: Z between? ;

View File

@ -1,40 +1,7 @@
! :folding=indent:collapseFolds=1: ! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
! $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.
IN: parser IN: parser
USE: errors USING: errors generic kernel math sequences strings ;
USE: generic
USE: kernel
USE: lists
USE: math
USE: namespaces
USE: strings
USE: words
USE: unparser
! Number parsing ! Number parsing
@ -53,7 +20,7 @@ M: object digit> not-a-number ;
dup string-length 0 = [ dup string-length 0 = [
not-a-number not-a-number
] [ ] [
0 swap [ digit> pick digit+ ] string-each nip 0 swap [ digit> pick digit+ ] seq-each nip
] ifte ; ] ifte ;
: base> ( str base -- num ) : base> ( str base -- num )

View File

@ -107,16 +107,22 @@ BUILTIN: f 9 ; : f f swons ; parsing
: CHAR: ( -- ) 0 scan next-char drop swons ; parsing : CHAR: ( -- ) 0 scan next-char drop swons ; parsing
! String literal ! String literal
: parse-string ( n str -- n ) : (parse-string) ( n str -- n )
2dup string-nth CHAR: " = [ 2dup string-nth CHAR: " = [
drop 1 + drop 1 +
] [ ] [
[ next-char swap , ] keep parse-string [ next-char swap , ] keep (parse-string)
] ifte ; ] ifte ;
: parse-string [ "line" get (parse-string) ] make-string ;
: " : "
"col" [ "col" [
"line" get [ parse-string ] make-string swap parse-string swap
] change swons ; parsing
: s"
"col" [
"line" get skip-blank parse-string string>sbuf swap
] change swons ; parsing ] change swons ; parsing
! Comments ! Comments

View File

@ -2,7 +2,8 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: prettyprint IN: prettyprint
USING: errors generic hashtables kernel lists math namespaces USING: errors generic hashtables kernel lists math namespaces
parser presentation stdio streams strings unparser vectors words ; parser presentation sequences stdio streams strings unparser
vectors words ;
SYMBOL: prettyprint-limit SYMBOL: prettyprint-limit
SYMBOL: one-line SYMBOL: one-line
@ -117,7 +118,7 @@ M: cons prettyprint* ( indent cons -- indent )
M: vector prettyprint* ( indent vector -- indent ) M: vector prettyprint* ( indent vector -- indent )
[ [
\ { swap vector>list \ } prettyprint-sequence \ { swap >list \ } prettyprint-sequence
] check-recursion ; ] check-recursion ;
M: hashtable prettyprint* ( indent hashtable -- indent ) M: hashtable prettyprint* ( indent hashtable -- indent )
@ -127,7 +128,7 @@ M: hashtable prettyprint* ( indent hashtable -- indent )
M: tuple prettyprint* ( indent tuple -- indent ) M: tuple prettyprint* ( indent tuple -- indent )
[ [
\ << swap tuple>list \ >> prettyprint-sequence \ << swap >list \ >> prettyprint-sequence
] check-recursion ; ] check-recursion ;
: prettyprint ( obj -- ) : prettyprint ( obj -- )
@ -152,7 +153,7 @@ M: tuple prettyprint* ( indent tuple -- indent )
: {.} ( vector -- ) : {.} ( vector -- )
#! Unparse each element on its own line. #! Unparse each element on its own line.
vector>list reverse [ . ] each ; >list reverse [ . ] each ;
: .s datastack {.} ; : .s datastack {.} ;
: .r callstack {.} ; : .r callstack {.} ;

View File

@ -1,8 +1,8 @@
! Copyright (C) 2004, 2005 Slava Pestov. ! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: unparser IN: unparser
USING: generic kernel lists math namespaces parser stdio strings USING: generic kernel lists math memory namespaces parser
words memory ; sequences sequences stdio strings words ;
GENERIC: unparse ( obj -- str ) GENERIC: unparse ( obj -- str )
@ -88,13 +88,15 @@ M: complex unparse ( num -- str )
dup ch>ascii-escape [ ] [ ch>unicode-escape ] ?ifte dup ch>ascii-escape [ ] [ ch>unicode-escape ] ?ifte
] unless ; ] unless ;
M: string unparse ( str -- str ) : unparse-string [ unparse-ch , ] seq-each ;
[
CHAR: " , [ unparse-ch , ] string-each CHAR: " ,
] make-string ;
M: word unparse ( obj -- str ) M: string unparse ( str -- str )
word-name dup "#<unnamed>" ? ; [ CHAR: " , unparse-string CHAR: " , ] make-string ;
M: sbuf unparse ( str -- str )
[ "s\" " , unparse-string CHAR: " , ] make-string ;
M: word unparse ( obj -- str ) word-name dup "#<unnamed>" ? ;
M: t unparse drop "t" ; M: t unparse drop "t" ;
M: f unparse drop "f" ; M: f unparse drop "f" ;

View File

@ -13,3 +13,5 @@ USE: test
"World" "buf-clone" get sbuf-append "World" "buf-clone" get sbuf-append
"buf" get sbuf>string "buf" get sbuf>string
] unit-test ] unit-test
[ CHAR: h ] [ 0 s" hello world" sbuf-nth ] unit-test

View File

@ -1,4 +1,6 @@
IN: temporary IN: temporary
USING: sequences ;
USE: errors USE: errors
USE: kernel USE: kernel
USE: math USE: math
@ -80,7 +82,7 @@ unit-test
[ 4 ] [ [ 4 ] [
0 "There are Four Upper Case characters" 0 "There are Four Upper Case characters"
[ LETTER? [ 1 + ] when ] string-each [ LETTER? [ 1 + ] when ] seq-each
] unit-test ] unit-test
[ "Replacing+spaces+with+plus" ] [ "Replacing+spaces+with+plus" ]

View File

@ -2,7 +2,7 @@
IN: test IN: test
USING: errors kernel lists math memory namespaces parser USING: errors kernel lists math memory namespaces parser
prettyprint stdio strings words vectors unparser ; prettyprint sequences stdio strings unparser vectors words ;
: assert ( t -- ) : assert ( t -- )
[ "Assertion failed!" throw ] unless ; [ "Assertion failed!" throw ] unless ;
@ -24,7 +24,7 @@ prettyprint stdio strings words vectors unparser ;
[ [
[ [
2dup print-test 2dup print-test
swap >r >r clear r> call datastack vector>list r> swap >r >r clear r> call datastack >list r>
= assert = assert
] keep-datastack 2drop ] keep-datastack 2drop
] time ; ] time ;

View File

@ -28,3 +28,5 @@ unit-test
[ ] [ { 1 2 3 } unparse drop ] unit-test [ ] [ { 1 2 3 } unparse drop ] unit-test
[ stdin unparse parse ] unit-test-fails [ stdin unparse parse ] unit-test-fails
[ "s\" hello world\"" ] [ s" hello world" unparse ] unit-test

View File

@ -29,7 +29,7 @@ USE: kernel-internals
[ t ] [ [ t ] [
100 empty-vector [ drop 0 100 random-int ] vector-map 100 empty-vector [ drop 0 100 random-int ] vector-map
dup vector>list list>vector = dup >list list>vector =
] unit-test ] unit-test
[ f ] [ { } { 1 2 3 } = ] unit-test [ f ] [ { } { 1 2 3 } = ] unit-test
@ -40,7 +40,7 @@ USE: kernel-internals
[ [ 1 4 9 16 ] ] [ [ 1 4 9 16 ] ]
[ [
[ 1 2 3 4 ] [ 1 2 3 4 ]
list>vector [ dup * ] vector-map vector>list list>vector [ dup * ] vector-map >list
] unit-test ] unit-test
[ t ] [ { } hashcode { } hashcode = ] unit-test [ t ] [ { } hashcode { } hashcode = ] unit-test

View File

@ -1,8 +1,8 @@
! Copyright (C) 2004, 2005 Slava Pestov. ! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: memory IN: memory
USING: kernel-internals errors generic kernel lists math USING: errors generic kernel kernel-internals lists math
namespaces prettyprint stdio unparser vectors words ; namespaces prettyprint sequences stdio unparser vectors words ;
! Printing an overview of heap usage. ! Printing an overview of heap usage.
@ -80,7 +80,7 @@ M: object (each-slot) ( quot obj -- )
#! Return a list of instance count/total size pairs. #! Return a list of instance count/total size pairs.
num-types zero-vector num-types zero-vector num-types zero-vector num-types zero-vector
[ >r 2dup r> heap-stat-step ] each-object [ >r 2dup r> heap-stat-step ] each-object
swap vector>list swap vector>list zip ; swap >list swap >list zip ;
: heap-stat. ( type instances bytes -- ) : heap-stat. ( type instances bytes -- )
dup 0 = [ dup 0 = [

View File

@ -1,8 +1,8 @@
! Copyright (C) 2005 Slava Pestov. ! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets IN: gadgets
USING: generic kernel lists math namespaces sdl line-editor USING: generic kernel line-editor lists math namespaces sdl
strings ; sequences strings ;
! An editor gadget wraps a line editor object and passes ! An editor gadget wraps a line editor object and passes
! gestures to the line editor. ! gestures to the line editor.
@ -28,7 +28,7 @@ TUPLE: editor line caret ;
: run-char-widths ( str -- wlist ) : run-char-widths ( str -- wlist )
#! List of x co-ordinates of each character. #! List of x co-ordinates of each character.
0 swap string>list 0 swap >list
[ ch>string shape-w [ + dup ] keep 2 /i - ] map nip ; [ ch>string shape-w [ + dup ] keep 2 /i - ] map nip ;
: (x>offset) ( n x wlist -- offset ) : (x>offset) ( n x wlist -- offset )

View File

@ -2,7 +2,7 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets IN: gadgets
USING: errors gadgets generic hashtables kernel kernel-internals USING: errors gadgets generic hashtables kernel kernel-internals
lists namespaces strings unparser vectors words ; lists namespaces sequences strings unparser vectors words ;
: label-box ( list -- gadget ) : label-box ( list -- gadget )
0 0 0 <pile> swap [ <presentation> over add-gadget ] each ; 0 0 0 <pile> swap [ <presentation> over add-gadget ] each ;
@ -51,10 +51,10 @@ M: list custom-sheet ( list -- gadget )
[ length count ] keep zip alist>sheet "Elements:" <titled> ; [ length count ] keep zip alist>sheet "Elements:" <titled> ;
M: array custom-sheet ( array -- gadget ) M: array custom-sheet ( array -- gadget )
[ array-capacity ] keep array>list custom-sheet ; >list custom-sheet ;
M: vector custom-sheet ( array -- gadget ) M: vector custom-sheet ( array -- gadget )
vector>list custom-sheet ; >list custom-sheet ;
M: hashtable custom-sheet ( array -- gadget ) M: hashtable custom-sheet ( array -- gadget )
hash>alist sort-sheet alist>sheet "Entries:" <titled> ; hash>alist sort-sheet alist>sheet "Entries:" <titled> ;

View File

@ -2,7 +2,7 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets IN: gadgets
USING: errors generic hashtables kernel lists math namespaces USING: errors generic hashtables kernel lists math namespaces
sdl ; sdl sequences ;
! A pile is a box that lays out its contents vertically. ! A pile is a box that lays out its contents vertically.
TUPLE: pile align gap fill ; TUPLE: pile align gap fill ;

View File

@ -2,7 +2,7 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets IN: gadgets
USING: errors generic hashtables kernel lists math namespaces USING: errors generic hashtables kernel lists math namespaces
sdl ; sdl sequences ;
! A shelf is a box that lays out its contents horizontally. ! A shelf is a box that lays out its contents horizontally.
TUPLE: shelf gap align fill ; TUPLE: shelf gap align fill ;

View File

@ -5,9 +5,12 @@ DEFER: (set-vector-length)
DEFER: vector-array DEFER: vector-array
DEFER: set-vector-array DEFER: set-vector-array
IN: sequences
DEFER: seq-each
IN: vectors IN: vectors
USING: generic kernel lists math kernel-internals errors USING: errors generic kernel kernel-internals lists math
math-internals ; math-internals sequences ;
BUILTIN: vector 11 BUILTIN: vector 11
[ 1 "vector-length" (set-vector-length) ] [ 1 "vector-length" (set-vector-length) ]
@ -60,6 +63,11 @@ IN: vectors
>r >fixnum dup assert-positive r> >r >fixnum dup assert-positive r>
2dup grow-capacity (set-vector-length) ; 2dup grow-capacity (set-vector-length) ;
M: vector length vector-length ;
M: vector set-length set-vector-length ;
M: vector nth vector-nth ;
M: vector set-nth set-vector-nth ;
: empty-vector ( len -- vec ) : empty-vector ( len -- vec )
#! Creates a vector with 'len' elements set to f. Unlike #! Creates a vector with 'len' elements set to f. Unlike
#! <vector>, which gives an empty vector with a certain #! <vector>, which gives an empty vector with a certain
@ -82,16 +90,10 @@ IN: vectors
: >pop> ( stack -- stack ) : >pop> ( stack -- stack )
dup vector-pop drop ; dup vector-pop drop ;
: vector>list ( vec -- list )
dup vector-length swap vector-array array>list ;
: vector-each ( vector quotation -- ) : vector-each ( vector quotation -- )
#! Execute the quotation with each element of the vector #! Execute the quotation with each element of the vector
#! pushed onto the stack. #! pushed onto the stack.
>r vector>list r> each ; inline >r >list r> each ; inline
: vector-each-with ( obj vector quot -- )
swap [ with ] vector-each 2drop ; inline
: list>vector ( list -- vector ) : list>vector ( list -- vector )
dup length <vector> swap [ over vector-push ] each ; dup length <vector> swap [ over vector-push ] each ;
@ -100,11 +102,11 @@ IN: vectors
#! Applies code to each element of the vector, return a new #! Applies code to each element of the vector, return a new
#! vector with the results. The code must have stack effect #! vector with the results. The code must have stack effect
#! ( obj -- obj ). #! ( obj -- obj ).
>r vector>list r> map list>vector ; inline >r >list r> map list>vector ; inline
: vector-nappend ( v1 v2 -- ) : vector-nappend ( v1 v2 -- )
#! Destructively append v2 to v1. #! Destructively append v2 to v1.
[ over vector-push ] vector-each drop ; [ over vector-push ] seq-each drop ;
: vector-append ( v1 v2 -- vec ) : vector-append ( v1 v2 -- vec )
over vector-length over vector-length + <vector> over vector-length over vector-length + <vector>
@ -122,34 +124,6 @@ M: vector clone ( vector -- vector )
vector-array rot vector-array rot copy-array vector-array rot vector-array rot copy-array
] keep ; ] keep ;
: vector-length= ( vec vec -- ? )
vector-length swap vector-length number= ;
M: vector = ( obj vec -- ? )
#! Check if two vectors are equal. Two vectors are
#! considered equal if they have the same length and contain
#! equal elements.
2dup eq? [
2drop t
] [
over vector? [
2dup vector-length= [
swap vector>list swap vector>list =
] [
2drop f
] ifte
] [
2drop f
] ifte
] ifte ;
M: vector hashcode ( vec -- n )
dup vector-length 0 number= [
drop 0
] [
0 swap vector-nth hashcode
] ifte ;
: vector-tail ( n vector -- list ) : vector-tail ( n vector -- list )
#! Return a new list with all elements from the nth #! Return a new list with all elements from the nth
#! index upwards. #! index upwards.
@ -163,6 +137,13 @@ M: vector hashcode ( vec -- n )
#! one element. #! one element.
[ vector-length swap - ] keep vector-tail ; [ vector-length swap - ] keep vector-tail ;
M: vector hashcode ( vec -- n )
dup length 0 number= [
drop 0
] [
0 swap nth hashcode
] ifte ;
! Find a better place for this ! Find a better place for this
IN: kernel IN: kernel

View File

@ -86,7 +86,7 @@ SYMBOL: vocabularies
"compiler" "debugger" "errors" "files" "generic" "compiler" "debugger" "errors" "files" "generic"
"hashtables" "inference" "interpreter" "jedit" "kernel" "hashtables" "inference" "interpreter" "jedit" "kernel"
"listener" "lists" "math" "memory" "namespaces" "parser" "listener" "lists" "math" "memory" "namespaces" "parser"
"prettyprint" "processes" "profiler" "streams" "stdio" "prettyprint" "processes" "profiler" "sequences"
"strings" "syntax" "test" "threads" "unparser" "vectors" "streams" "stdio" "strings" "syntax" "test" "threads"
"words" "scratchpad" "unparser" "vectors" "words" "scratchpad"
] "use" set ; ] "use" set ;

View File

@ -2,13 +2,13 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: words IN: words
USING: generic hashtables kernel kernel-internals lists math USING: generic hashtables kernel kernel-internals lists math
namespaces strings vectors ; namespaces sequences strings vectors ;
! Utility ! Utility
GENERIC: (tree-each) ( quot obj -- ) inline GENERIC: (tree-each) ( quot obj -- ) inline
M: object (tree-each) swap call ; M: object (tree-each) swap call ;
M: cons (tree-each) [ car (tree-each) ] 2keep cdr (tree-each) ; M: cons (tree-each) [ car (tree-each) ] 2keep cdr (tree-each) ;
M: vector (tree-each) [ swap call ] vector-each-with ; M: sequence (tree-each) [ swap call ] seq-each-with ;
: tree-each swap (tree-each) ; inline : tree-each swap (tree-each) ; inline
: tree-each-with ( obj vector quot -- ) : tree-each-with ( obj vector quot -- )
swap [ with ] tree-each 2drop ; inline swap [ with ] tree-each 2drop ; inline

View File

@ -47,7 +47,7 @@ void primitive_sbuf_nth(void)
if(index < 0 || index >= sbuf->top) if(index < 0 || index >= sbuf->top)
range_error(tag_object(sbuf),0,tag_fixnum(index),sbuf->top); range_error(tag_object(sbuf),0,tag_fixnum(index),sbuf->top);
dpush(string_nth(untag_string(sbuf->string),index)); dpush(tag_fixnum(string_nth(untag_string(sbuf->string),index)));
} }
void sbuf_ensure_capacity(F_SBUF* sbuf, F_FIXNUM top) void sbuf_ensure_capacity(F_SBUF* sbuf, F_FIXNUM top)