diff --git a/Factor.app/Contents/Info.plist b/Factor.app/Contents/Info.plist
index ca0e6d5f8a..a8943d0d32 100644
--- a/Factor.app/Contents/Info.plist
+++ b/Factor.app/Contents/Info.plist
@@ -32,7 +32,7 @@
CFBundlePackageType
APPL
NSHumanReadableCopyright
- Copyright © 2003-2007, Slava Pestov and friends
+ Copyright © 2003-2008, Slava Pestov and friends
NSServices
diff --git a/core/alien/alien-docs.factor b/core/alien/alien-docs.factor
index 7d13080e3c..0caf0e9a9f 100755
--- a/core/alien/alien-docs.factor
+++ b/core/alien/alien-docs.factor
@@ -265,7 +265,7 @@ ARTICLE: "embedding-restrictions" "Embedding API restrictions"
ARTICLE: "embedding-factor" "What embedding looks like from Factor"
"Factor code will run inside an embedded instance in the same way it would run in a stand-alone instance."
$nl
-"One exception is the global " { $link stdio } " stream, which is by default not bound to the terminal where the process is running, to avoid conflicting with any I/O the host process might perform. To initialize the terminal stream, " { $link init-stdio } " must be called explicitly."
+"One exception is that the global " { $link input-stream } " and " { $link output-stream } " streams are not bound by default, to avoid conflicting with any I/O the host process might perform. The " { $link init-stdio } " words must be called explicitly to initialize terminal streams."
$nl
"There is a word which can detect when Factor is embedded:"
{ $subsection embedded? }
diff --git a/core/assocs/assocs-docs.factor b/core/assocs/assocs-docs.factor
index de62ccd878..68be9c9b06 100755
--- a/core/assocs/assocs-docs.factor
+++ b/core/assocs/assocs-docs.factor
@@ -1,7 +1,7 @@
! Copyright (C) 2007 Daniel Ehrenberg and Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
USING: help.markup help.syntax kernel sequences
-sequences.private namespaces classes math ;
+sequences.private namespaces math ;
IN: assocs
ARTICLE: "alists" "Association lists"
@@ -68,7 +68,7 @@ ARTICLE: "assocs-lookup" "Lookup and querying of assocs"
ARTICLE: "assocs-sets" "Set-theoretic operations on assocs"
"It is often useful to use the keys of an associative mapping as a set, exploiting the constant or logarithmic lookup time of most implementations (" { $link "alists" } " being a notable exception)."
-{ $subsection subassoc? }
+{ $subsection assoc-subset? }
{ $subsection assoc-intersect }
{ $subsection update }
{ $subsection assoc-union }
@@ -215,7 +215,7 @@ HELP: assoc-all?
{ $values { "assoc" assoc } { "quot" "a quotation with stack effect " { $snippet "( key value -- ? )" } } { "?" "a boolean" } }
{ $description "Tests if all entries in the assoc satisfy a predicate by applying the quotation to each entry in turn. a predicate quotation to entry in the assoc. Iteration stops if an entry is found for which the quotation outputs " { $link f } ". If the assoc is empty, always outputs " { $link t } "." } ;
-HELP: subassoc?
+HELP: assoc-subset?
{ $values { "assoc1" assoc } { "assoc2" assoc } { "?" "a new assoc" } }
{ $description "Tests if " { $snippet "assoc2" } " contains all key/value pairs of " { $snippet "assoc1" } "." } ;
diff --git a/core/assocs/assocs-tests.factor b/core/assocs/assocs-tests.factor
index 19e323bdae..30f2ec23c4 100755
--- a/core/assocs/assocs-tests.factor
+++ b/core/assocs/assocs-tests.factor
@@ -3,13 +3,13 @@ USING: kernel math namespaces tools.test vectors sequences
sequences.private hashtables io prettyprint assocs
continuations ;
-[ t ] [ H{ } dup subassoc? ] unit-test
-[ f ] [ H{ { 1 3 } } H{ } subassoc? ] unit-test
-[ t ] [ H{ } H{ { 1 3 } } subassoc? ] unit-test
-[ t ] [ H{ { 1 3 } } H{ { 1 3 } } subassoc? ] unit-test
-[ f ] [ H{ { 1 3 } } H{ { 1 "hey" } } subassoc? ] unit-test
-[ f ] [ H{ { 1 f } } H{ } subassoc? ] unit-test
-[ t ] [ H{ { 1 f } } H{ { 1 f } } subassoc? ] unit-test
+[ t ] [ H{ } dup assoc-subset? ] unit-test
+[ f ] [ H{ { 1 3 } } H{ } assoc-subset? ] unit-test
+[ t ] [ H{ } H{ { 1 3 } } assoc-subset? ] unit-test
+[ t ] [ H{ { 1 3 } } H{ { 1 3 } } assoc-subset? ] unit-test
+[ f ] [ H{ { 1 3 } } H{ { 1 "hey" } } assoc-subset? ] unit-test
+[ f ] [ H{ { 1 f } } H{ } assoc-subset? ] unit-test
+[ t ] [ H{ { 1 f } } H{ { 1 f } } assoc-subset? ] unit-test
! Test some combinators
[
diff --git a/core/assocs/assocs.factor b/core/assocs/assocs.factor
index e68c311836..92db38573a 100755
--- a/core/assocs/assocs.factor
+++ b/core/assocs/assocs.factor
@@ -98,11 +98,11 @@ M: assoc assoc-clone-like ( assoc exemplar -- newassoc )
: assoc-stack ( key seq -- value )
dup length 1- swap (assoc-stack) ;
-: subassoc? ( assoc1 assoc2 -- ? )
+: assoc-subset? ( assoc1 assoc2 -- ? )
[ swapd at* [ = ] [ 2drop f ] if ] curry assoc-all? ;
: assoc= ( assoc1 assoc2 -- ? )
- 2dup subassoc? >r swap subassoc? r> and ;
+ [ assoc-subset? ] [ swap assoc-subset? ] 2bi and ;
: assoc-hashcode ( n assoc -- code )
[
diff --git a/core/bootstrap/image/image.factor b/core/bootstrap/image/image.factor
index b3be0c41e7..1ff04bacc2 100755
--- a/core/bootstrap/image/image.factor
+++ b/core/bootstrap/image/image.factor
@@ -305,12 +305,12 @@ M: wrapper '
[ emit ] emit-object ;
! Strings
-: emit-chars ( seq -- )
+: emit-bytes ( seq -- )
bootstrap-cell
big-endian get [ [ be> ] map ] [ [ le> ] map ] if
emit-seq ;
-: pack-string ( string -- newstr )
+: pad-bytes ( seq -- newseq )
dup length bootstrap-cell align 0 pad-right ;
: emit-string ( string -- ptr )
@@ -318,7 +318,7 @@ M: wrapper '
dup length emit-fixnum
f ' emit
f ' emit
- pack-string emit-chars
+ pad-bytes emit-bytes
] emit-object ;
M: string '
@@ -335,7 +335,11 @@ M: string '
[ 0 emit-fixnum ] emit-object
] bi* ;
-M: byte-array ' byte-array emit-dummy-array ;
+M: byte-array '
+ byte-array type-number object tag-number [
+ dup length emit-fixnum
+ pad-bytes emit-bytes
+ ] emit-object ;
M: bit-array ' bit-array emit-dummy-array ;
@@ -400,8 +404,8 @@ M: quotation '
[
{
dictionary source-files builtins
- update-map class<-cache class-not-cache
- classes-intersect-cache class-and-cache
+ update-map class<=-cache
+ class-not-cache classes-intersect-cache class-and-cache
class-or-cache
} [ dup get swap bootstrap-word set ] each
] H{ } make-assoc
@@ -471,7 +475,7 @@ M: quotation '
"Writing image to " write
architecture get boot-image-name resource-path
[ write "..." print flush ]
- [ binary [ (write-image) ] with-stream ] bi ;
+ [ binary [ (write-image) ] with-file-writer ] bi ;
PRIVATE>
diff --git a/core/bootstrap/primitives.factor b/core/bootstrap/primitives.factor
index bcd75e9854..6149e83893 100755
--- a/core/bootstrap/primitives.factor
+++ b/core/bootstrap/primitives.factor
@@ -59,6 +59,7 @@ num-types get f builtins set
"arrays"
"bit-arrays"
"byte-arrays"
+ "byte-vectors"
"classes.private"
"classes.tuple"
"classes.tuple.private"
@@ -452,6 +453,22 @@ tuple
}
} define-tuple-class
+"byte-vector" "byte-vectors" create
+tuple
+{
+ {
+ { "byte-array" "byte-arrays" }
+ "underlying"
+ { "underlying" "growable" }
+ { "set-underlying" "growable" }
+ } {
+ { "array-capacity" "sequences.private" }
+ "fill"
+ { "length" "sequences" }
+ { "set-fill" "growable" }
+ }
+} define-tuple-class
+
"curry" "kernel" create
tuple
{
diff --git a/core/bootstrap/stage2.factor b/core/bootstrap/stage2.factor
index 8e4108866f..2e087ff5bd 100755
--- a/core/bootstrap/stage2.factor
+++ b/core/bootstrap/stage2.factor
@@ -44,10 +44,6 @@ SYMBOL: bootstrap-time
"Now, you can run Factor:" print
vm write " -i=" write "output-image" get print flush ;
-! Wrap everything in a catch which starts a listener so
-! you can see what went wrong, instead of dealing with a
-! fep
-
! We time bootstrap
millis >r
@@ -91,7 +87,7 @@ f error-continuation set-global
parse-command-line
run-user-init
"run" get run
- stdio get [ stream-flush ] when*
+ output-stream get [ stream-flush ] when*
] [ print-error 1 exit ] recover
] set-boot-quot
diff --git a/core/bootstrap/syntax.factor b/core/bootstrap/syntax.factor
index 4b74804749..7d703d3093 100755
--- a/core/bootstrap/syntax.factor
+++ b/core/bootstrap/syntax.factor
@@ -16,6 +16,7 @@ IN: bootstrap.syntax
"?{"
"BIN:"
"B{"
+ "BV{"
"C:"
"CHAR:"
"DEFER:"
diff --git a/extra/byte-vectors/byte-vectors-docs.factor b/core/byte-vectors/byte-vectors-docs.factor
similarity index 100%
rename from extra/byte-vectors/byte-vectors-docs.factor
rename to core/byte-vectors/byte-vectors-docs.factor
diff --git a/extra/byte-vectors/byte-vectors-tests.factor b/core/byte-vectors/byte-vectors-tests.factor
similarity index 100%
rename from extra/byte-vectors/byte-vectors-tests.factor
rename to core/byte-vectors/byte-vectors-tests.factor
diff --git a/extra/byte-vectors/byte-vectors.factor b/core/byte-vectors/byte-vectors.factor
similarity index 61%
rename from extra/byte-vectors/byte-vectors.factor
rename to core/byte-vectors/byte-vectors.factor
index a8351dc781..e80b797a8d 100755
--- a/extra/byte-vectors/byte-vectors.factor
+++ b/core/byte-vectors/byte-vectors.factor
@@ -1,20 +1,9 @@
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays kernel kernel.private math sequences
-sequences.private growable byte-arrays prettyprint.backend
-parser accessors ;
+sequences.private growable byte-arrays ;
IN: byte-vectors
-TUPLE: byte-vector underlying fill ;
-
-M: byte-vector underlying underlying>> { byte-array } declare ;
-
-M: byte-vector set-underlying (>>underlying) ;
-
-M: byte-vector length fill>> { array-capacity } declare ;
-
-M: byte-vector set-fill (>>fill) ;
-
vector ( byte-array length -- byte-vector )
@@ -43,9 +32,3 @@ M: byte-vector equal?
M: byte-array new-resizable drop ;
INSTANCE: byte-vector growable
-
-: BV{ \ } [ >byte-vector ] parse-literal ; parsing
-
-M: byte-vector >pprint-sequence ;
-
-M: byte-vector pprint-delims drop \ BV{ \ } ;
diff --git a/extra/byte-vectors/summary.txt b/core/byte-vectors/summary.txt
similarity index 100%
rename from extra/byte-vectors/summary.txt
rename to core/byte-vectors/summary.txt
diff --git a/extra/byte-vectors/tags.txt b/core/byte-vectors/tags.txt
similarity index 100%
rename from extra/byte-vectors/tags.txt
rename to core/byte-vectors/tags.txt
diff --git a/core/checksums/checksums-docs.factor b/core/checksums/checksums-docs.factor
new file mode 100644
index 0000000000..c352f02af4
--- /dev/null
+++ b/core/checksums/checksums-docs.factor
@@ -0,0 +1,51 @@
+USING: help.markup help.syntax kernel math sequences quotations
+math.private byte-arrays strings ;
+IN: checksums
+
+HELP: checksum
+{ $class-description "The class of checksum algorithms." } ;
+
+HELP: hex-string
+{ $values { "seq" "a sequence" } { "str" "a string" } }
+{ $description "Converts a sequence of values from 0-255 to a string of hex numbers from 0-ff." }
+{ $examples
+ { $example "USING: checksums io ;" "B{ 1 2 3 4 } hex-string print" "01020304" }
+}
+{ $notes "Numbers are zero-padded on the left." } ;
+
+HELP: checksum-stream
+{ $values { "stream" "an input stream" } { "checksum" "a checksum specifier" } { "value" byte-array } }
+{ $contract "Computes the checksum of all data read from the stream." }
+{ $side-effects "stream" } ;
+
+HELP: checksum-bytes
+{ $values { "bytes" "a sequence of bytes" } { "checksum" "a checksum specifier" } { "value" byte-array } }
+{ $contract "Computes the checksum of all data in a sequence." } ;
+
+HELP: checksum-lines
+{ $values { "lines" "a sequence of sequences of bytes" } { "checksum" "a checksum specifier" } { "value" byte-array } }
+{ $contract "Computes the checksum of all data in a sequence." } ;
+
+HELP: checksum-file
+{ $values { "path" "a pathname specifier" } { "checksum" "a checksum specifier" } { "value" byte-array } }
+{ $contract "Computes the checksum of all data in a file." } ;
+
+ARTICLE: "checksums" "Checksums"
+"A " { $emphasis "checksum" } " is a function mapping sequences of bytes to fixed-length strings. While checksums are not one-to-one, a good checksum should have a low probability of collision. Additionally, some checksum algorithms are designed to be hard to reverse, in the sense that finding an input string which hashes to a given checksum string requires a brute-force search."
+$nl
+"Checksums are instances of a class:"
+{ $subsection checksum }
+"Operations on checksums:"
+{ $subsection checksum-bytes }
+{ $subsection checksum-stream }
+{ $subsection checksum-lines }
+"Checksums should implement at least one of " { $link checksum-bytes } " and " { $link checksum-stream } ". Implementing " { $link checksum-lines } " is optional."
+$nl
+"Utilities:"
+{ $subsection checksum-file }
+{ $subsection hex-string }
+"Checksum implementations:"
+{ $subsection "checksums.crc32" }
+{ $vocab-subsection "MD5 checksum" "checksums.md5" }
+{ $vocab-subsection "SHA1 checksum" "checksums.sha1" }
+{ $vocab-subsection "SHA2 checksum" "checksums.sha2" } ;
diff --git a/core/checksums/checksums-tests.factor b/core/checksums/checksums-tests.factor
new file mode 100644
index 0000000000..1ec675b0cf
--- /dev/null
+++ b/core/checksums/checksums-tests.factor
@@ -0,0 +1,7 @@
+IN: checksums.tests
+USING: checksums tools.test ;
+
+\ checksum-bytes must-infer
+\ checksum-stream must-infer
+\ checksum-lines must-infer
+\ checksum-file must-infer
diff --git a/core/checksums/checksums.factor b/core/checksums/checksums.factor
new file mode 100644
index 0000000000..08a13297d1
--- /dev/null
+++ b/core/checksums/checksums.factor
@@ -0,0 +1,25 @@
+! Copyright (c) 2008 Slava Pestov
+! See http://factorcode.org/license.txt for BSD license.
+USING: sequences math.parser io io.streams.byte-array
+io.encodings.binary io.files kernel ;
+IN: checksums
+
+MIXIN: checksum
+
+GENERIC: checksum-bytes ( bytes checksum -- value )
+
+GENERIC: checksum-stream ( stream checksum -- value )
+
+GENERIC: checksum-lines ( lines checksum -- value )
+
+M: checksum checksum-bytes >r binary r> checksum-stream ;
+
+M: checksum checksum-stream >r contents r> checksum-bytes ;
+
+M: checksum checksum-lines >r B{ CHAR: \n } join r> checksum-bytes ;
+
+: checksum-file ( path checksum -- value )
+ >r binary r> checksum-stream ;
+
+: hex-string ( seq -- str )
+ [ >hex 2 CHAR: 0 pad-left ] { } map-as concat ;
diff --git a/core/io/crc32/authors.txt b/core/checksums/crc32/authors.txt
similarity index 100%
rename from core/io/crc32/authors.txt
rename to core/checksums/crc32/authors.txt
diff --git a/core/checksums/crc32/crc32-docs.factor b/core/checksums/crc32/crc32-docs.factor
new file mode 100644
index 0000000000..0f277bcd16
--- /dev/null
+++ b/core/checksums/crc32/crc32-docs.factor
@@ -0,0 +1,11 @@
+USING: help.markup help.syntax math ;
+IN: checksums.crc32
+
+HELP: crc32
+{ $class-description "The CRC32 checksum algorithm." } ;
+
+ARTICLE: "checksums.crc32" "CRC32 checksum"
+"The CRC32 checksum algorithm provides a quick but unreliable way to detect changes in data."
+{ $subsection crc32 } ;
+
+ABOUT: "checksums.crc32"
diff --git a/core/checksums/crc32/crc32-tests.factor b/core/checksums/crc32/crc32-tests.factor
new file mode 100644
index 0000000000..6fe4b995ee
--- /dev/null
+++ b/core/checksums/crc32/crc32-tests.factor
@@ -0,0 +1,6 @@
+USING: checksums checksums.crc32 kernel math tools.test namespaces ;
+
+[ B{ 0 0 0 0 } ] [ "" crc32 checksum-bytes ] unit-test
+
+[ B{ HEX: cb HEX: f4 HEX: 39 HEX: 26 } ] [ "123456789" crc32 checksum-bytes ] unit-test
+
diff --git a/core/io/crc32/crc32.factor b/core/checksums/crc32/crc32.factor
similarity index 59%
rename from core/io/crc32/crc32.factor
rename to core/checksums/crc32/crc32.factor
index afe7e4bfb7..e1f0b9417b 100755
--- a/core/io/crc32/crc32.factor
+++ b/core/checksums/crc32/crc32.factor
@@ -2,8 +2,8 @@
! See http://factorcode.org/license.txt for BSD license.
USING: kernel math sequences sequences.private namespaces
words io io.binary io.files io.streams.string quotations
-definitions ;
-IN: io.crc32
+definitions checksums ;
+IN: checksums.crc32
: crc32-polynomial HEX: edb88320 ; inline
@@ -20,10 +20,20 @@ IN: io.crc32
mask-byte crc32-table nth-unsafe >bignum
swap -8 shift bitxor ; inline
-: crc32 ( seq -- n )
- >r HEX: ffffffff dup r> [ (crc32) ] each bitxor ;
+SINGLETON: crc32
-: lines-crc32 ( seq -- n )
- HEX: ffffffff tuck [
- [ (crc32) ] each CHAR: \n (crc32)
- ] reduce bitxor ;
+INSTANCE: crc32 checksum
+
+: init-crc32 drop >r HEX: ffffffff dup r> ; inline
+
+: finish-crc32 bitxor 4 >be ; inline
+
+M: crc32 checksum-bytes
+ init-crc32
+ [ (crc32) ] each
+ finish-crc32 ;
+
+M: crc32 checksum-lines
+ init-crc32
+ [ [ (crc32) ] each CHAR: \n (crc32) ] each
+ finish-crc32 ;
diff --git a/core/io/crc32/summary.txt b/core/checksums/crc32/summary.txt
similarity index 100%
rename from core/io/crc32/summary.txt
rename to core/checksums/crc32/summary.txt
diff --git a/core/classes/algebra/algebra-docs.factor b/core/classes/algebra/algebra-docs.factor
index 87c72048f4..810bdbe10f 100755
--- a/core/classes/algebra/algebra-docs.factor
+++ b/core/classes/algebra/algebra-docs.factor
@@ -1,14 +1,14 @@
-USING: help.markup help.syntax kernel classes ;
+USING: help.markup help.syntax kernel classes words
+checksums checksums.crc32 sequences math ;
IN: classes.algebra
ARTICLE: "class-operations" "Class operations"
"Set-theoretic operations on classes:"
{ $subsection class< }
+{ $subsection class<= }
{ $subsection class-and }
{ $subsection class-or }
{ $subsection classes-intersect? }
-"Topological sort:"
-{ $subsection sort-classes }
{ $subsection min-class }
"Low-level implementation detail:"
{ $subsection class-types }
@@ -17,6 +17,29 @@ ARTICLE: "class-operations" "Class operations"
{ $subsection class-types }
{ $subsection class-tags } ;
+ARTICLE: "class-linearization" "Class linearization"
+"Classes have an intrinsic partial order; given two classes A and B, we either have that A is a subset of B, B is a subset of A, A and B are equal as sets, or they are incomparable. The last two situations present difficulties for method dispatch:"
+{ $list
+ "If a generic word defines a method on a mixin class A and another class B, and B is the only instance of A, there is an ambiguity because A and B are equal as sets; any object that is an instance of one is an instance of both."
+ { "If a generic word defines methods on two union classes which are incomparable but not disjoint, for example " { $link sequence } " and " { $link number } ", there is an ambiguity because the generic word may be called on an object that is an instance of both unions." }
+}
+"The first ambiguity is resolved with a tie-breaker that compares metaclasses. The intrinsic meta-class order, from most-specific to least-specific:"
+{ $list
+ "Built-in classes and tuple classes"
+ "Predicate classes"
+ "Union classes"
+ "Mixin classes"
+}
+"This means that in the above example, the generic word with methods on a mixin and its sole instance will always call the method for the sole instance, since it is more specific than a mixin class."
+$nl
+"The second problem is resolved with another tie-breaker. When performing the topological sort of classes, if there are multiple candidates at any given step of the sort, lexicographical order on the class name is used."
+$nl
+"Operations:"
+{ $subsection class< }
+{ $subsection sort-classes }
+"Metaclass order:"
+{ $subsection rank-class } ;
+
HELP: flatten-builtin-class
{ $values { "class" class } { "assoc" "an assoc whose keys are classes" } }
{ $description "Outputs a set of tuple classes whose union is the smallest cover of " { $snippet "class" } " intersected with " { $link tuple } "." } ;
@@ -29,14 +52,14 @@ HELP: class-types
{ $values { "class" class } { "seq" "an increasing sequence of integers" } }
{ $description "Outputs a sequence of builtin type numbers whose instances can possibly be instances of the given class." } ;
-HELP: class<
+HELP: class<=
{ $values { "first" "a class" } { "second" "a class" } { "?" "a boolean" } }
{ $description "Tests if all instances of " { $snippet "class1" } " are also instances of " { $snippet "class2" } "." }
{ $notes "Classes are partially ordered. This means that if " { $snippet "class1 <= class2" } " and " { $snippet "class2 <= class1" } ", then " { $snippet "class1 = class2" } ". Also, if " { $snippet "class1 <= class2" } " and " { $snippet "class2 <= class3" } ", then " { $snippet "class1 <= class3" } "." } ;
HELP: sort-classes
{ $values { "seq" "a sequence of class" } { "newseq" "a new seqence of classes" } }
-{ $description "Outputs a topological sort of a sequence of classes. Larger classes come before their subclasses." } ;
+{ $description "Outputs a linear sort of a sequence of classes. Larger classes come before their subclasses." } ;
HELP: class-or
{ $values { "first" class } { "second" class } { "class" class } }
diff --git a/core/classes/algebra/algebra-tests.factor b/core/classes/algebra/algebra-tests.factor
index dba97c16f5..dfe4a0fbc9 100755
--- a/core/classes/algebra/algebra-tests.factor
+++ b/core/classes/algebra/algebra-tests.factor
@@ -4,9 +4,9 @@ kernel math namespaces parser prettyprint sequences strings
tools.test vectors words quotations classes classes.algebra
classes.private classes.union classes.mixin classes.predicate
vectors definitions source-files compiler.units growable
-random inference effects kernel.private sbufs ;
+random inference effects kernel.private sbufs math.order ;
-: class= [ class< ] 2keep swap class< and ;
+: class= [ class<= ] [ swap class<= ] 2bi and ;
: class-and* >r class-and r> class= ;
@@ -38,43 +38,43 @@ UNION: both first-one union-class ;
[ f ] [ number vector class-and sequence classes-intersect? ] unit-test
-[ t ] [ \ fixnum \ integer class< ] unit-test
-[ t ] [ \ fixnum \ fixnum class< ] unit-test
-[ f ] [ \ integer \ fixnum class< ] unit-test
-[ t ] [ \ integer \ object class< ] unit-test
-[ f ] [ \ integer \ null class< ] unit-test
-[ t ] [ \ null \ object class< ] unit-test
+[ t ] [ \ fixnum \ integer class<= ] unit-test
+[ t ] [ \ fixnum \ fixnum class<= ] unit-test
+[ f ] [ \ integer \ fixnum class<= ] unit-test
+[ t ] [ \ integer \ object class<= ] unit-test
+[ f ] [ \ integer \ null class<= ] unit-test
+[ t ] [ \ null \ object class<= ] unit-test
-[ t ] [ \ generic \ word class< ] unit-test
-[ f ] [ \ word \ generic class< ] unit-test
+[ t ] [ \ generic \ word class<= ] unit-test
+[ f ] [ \ word \ generic class<= ] unit-test
-[ f ] [ \ reversed \ slice class< ] unit-test
-[ f ] [ \ slice \ reversed class< ] unit-test
+[ f ] [ \ reversed \ slice class<= ] unit-test
+[ f ] [ \ slice \ reversed class<= ] unit-test
PREDICATE: no-docs < word "documentation" word-prop not ;
UNION: no-docs-union no-docs integer ;
-[ t ] [ no-docs no-docs-union class< ] unit-test
-[ f ] [ no-docs-union no-docs class< ] unit-test
+[ t ] [ no-docs no-docs-union class<= ] unit-test
+[ f ] [ no-docs-union no-docs class<= ] unit-test
TUPLE: a ;
TUPLE: b ;
UNION: c a b ;
-[ t ] [ \ c \ tuple class< ] unit-test
-[ f ] [ \ tuple \ c class< ] unit-test
+[ t ] [ \ c \ tuple class<= ] unit-test
+[ f ] [ \ tuple \ c class<= ] unit-test
-[ t ] [ \ tuple-class \ class class< ] unit-test
-[ f ] [ \ class \ tuple-class class< ] unit-test
+[ t ] [ \ tuple-class \ class class<= ] unit-test
+[ f ] [ \ class \ tuple-class class<= ] unit-test
TUPLE: tuple-example ;
-[ t ] [ \ null \ tuple-example class< ] unit-test
-[ f ] [ \ object \ tuple-example class< ] unit-test
-[ f ] [ \ object \ tuple-example class< ] unit-test
-[ t ] [ \ tuple-example \ tuple class< ] unit-test
-[ f ] [ \ tuple \ tuple-example class< ] unit-test
+[ t ] [ \ null \ tuple-example class<= ] unit-test
+[ f ] [ \ object \ tuple-example class<= ] unit-test
+[ f ] [ \ object \ tuple-example class<= ] unit-test
+[ t ] [ \ tuple-example \ tuple class<= ] unit-test
+[ f ] [ \ tuple \ tuple-example class<= ] unit-test
TUPLE: a1 ;
TUPLE: b1 ;
@@ -84,57 +84,57 @@ UNION: x1 a1 b1 ;
UNION: y1 a1 c1 ;
UNION: z1 b1 c1 ;
-[ f ] [ z1 x1 y1 class-and class< ] unit-test
+[ f ] [ z1 x1 y1 class-and class<= ] unit-test
-[ t ] [ x1 y1 class-and a1 class< ] unit-test
+[ t ] [ x1 y1 class-and a1 class<= ] unit-test
[ f ] [ y1 z1 class-and x1 classes-intersect? ] unit-test
-[ f ] [ b1 c1 class-or a1 b1 class-or a1 c1 class-and class-and class< ] unit-test
+[ f ] [ b1 c1 class-or a1 b1 class-or a1 c1 class-and class-and class<= ] unit-test
-[ t ] [ a1 b1 class-or a1 c1 class-or class-and a1 class< ] unit-test
+[ t ] [ a1 b1 class-or a1 c1 class-or class-and a1 class<= ] unit-test
[ f ] [ a1 c1 class-or b1 c1 class-or class-and a1 b1 class-or classes-intersect? ] unit-test
[ f ] [ growable \ hi-tag classes-intersect? ] unit-test
[ t ] [
- growable tuple sequence class-and class<
+ growable tuple sequence class-and class<=
] unit-test
[ t ] [
- growable assoc class-and tuple class<
+ growable assoc class-and tuple class<=
] unit-test
-[ t ] [ object \ f \ f class-not class-or class< ] unit-test
+[ t ] [ object \ f \ f class-not class-or class<= ] unit-test
[ t ] [ fixnum class-not integer class-and bignum class= ] unit-test
[ f ] [ integer integer class-not classes-intersect? ] unit-test
-[ t ] [ array number class-not class< ] unit-test
+[ t ] [ array number class-not class<= ] unit-test
-[ f ] [ bignum number class-not class< ] unit-test
+[ f ] [ bignum number class-not class<= ] unit-test
[ vector ] [ vector class-not class-not ] unit-test
-[ t ] [ fixnum fixnum bignum class-or class< ] unit-test
+[ t ] [ fixnum fixnum bignum class-or class<= ] unit-test
-[ f ] [ fixnum class-not integer class-and array class< ] unit-test
+[ f ] [ fixnum class-not integer class-and array class<= ] unit-test
-[ f ] [ fixnum class-not integer class< ] unit-test
+[ f ] [ fixnum class-not integer class<= ] unit-test
-[ f ] [ number class-not array class< ] unit-test
+[ f ] [ number class-not array class<= ] unit-test
-[ f ] [ fixnum class-not array class< ] unit-test
+[ f ] [ fixnum class-not array class<= ] unit-test
-[ t ] [ number class-not integer class-not class< ] unit-test
+[ t ] [ number class-not integer class-not class<= ] unit-test
[ t ] [ vector array class-not class-and vector class= ] unit-test
[ f ] [ fixnum class-not number class-and array classes-intersect? ] unit-test
-[ f ] [ fixnum class-not integer class< ] unit-test
+[ f ] [ fixnum class-not integer class<= ] unit-test
[ t ] [ null class-not object class= ] unit-test
@@ -147,7 +147,7 @@ UNION: z1 b1 c1 ;
[ t ] [
fixnum class-not
fixnum fixnum class-not class-or
- class<
+ class<=
] unit-test
! Test method inlining
@@ -241,3 +241,23 @@ UNION: z1 b1 c1 ;
=
] unit-test
] times
+
+SINGLETON: xxx
+UNION: yyy xxx ;
+
+[ { yyy xxx } ] [ { xxx yyy } sort-classes ] unit-test
+[ { yyy xxx } ] [ { yyy xxx } sort-classes ] unit-test
+
+[ { number ratio integer } ] [ { ratio number integer } sort-classes ] unit-test
+[ { sequence number ratio } ] [ { ratio number sequence } sort-classes ] unit-test
+
+TUPLE: xa ;
+TUPLE: xb ;
+TUPLE: xc < xa ;
+TUPLE: xd < xb ;
+TUPLE: xe ;
+TUPLE: xf < xb ;
+TUPLE: xg < xb ;
+TUPLE: xh < xb ;
+
+[ t ] [ { xa xb xc xd xe xf xg xh } sort-classes dup sort-classes = ] unit-test
diff --git a/core/classes/algebra/algebra.factor b/core/classes/algebra/algebra.factor
index 6a286e3204..4160f4e9d2 100755
--- a/core/classes/algebra/algebra.factor
+++ b/core/classes/algebra/algebra.factor
@@ -2,16 +2,16 @@
! See http://factorcode.org/license.txt for BSD license.
USING: kernel classes classes.builtin combinators accessors
sequences arrays vectors assocs namespaces words sorting layouts
-math hashtables kernel.private sets ;
+math hashtables kernel.private sets math.order ;
IN: classes.algebra
: 2cache ( key1 key2 assoc quot -- value )
>r >r 2array r> [ first2 ] r> compose cache ; inline
-DEFER: (class<)
+DEFER: (class<=)
-: class< ( first second -- ? )
- class<-cache get [ (class<) ] 2cache ;
+: class<= ( first second -- ? )
+ class<=-cache get [ (class<=) ] 2cache ;
DEFER: (class-not)
@@ -45,31 +45,31 @@ TUPLE: anonymous-complement class ;
C: anonymous-complement
-: superclass< ( first second -- ? )
- >r superclass r> class< ;
+: superclass<= ( first second -- ? )
+ >r superclass r> class<= ;
-: left-union-class< ( first second -- ? )
- >r members r> [ class< ] curry all? ;
+: left-union-class<= ( first second -- ? )
+ >r members r> [ class<= ] curry all? ;
-: right-union-class< ( first second -- ? )
- members [ class< ] with contains? ;
+: right-union-class<= ( first second -- ? )
+ members [ class<= ] with contains? ;
: left-anonymous-union< ( first second -- ? )
- >r members>> r> [ class< ] curry all? ;
+ >r members>> r> [ class<= ] curry all? ;
: right-anonymous-union< ( first second -- ? )
- members>> [ class< ] with contains? ;
+ members>> [ class<= ] with contains? ;
: left-anonymous-intersection< ( first second -- ? )
- >r members>> r> [ class< ] curry contains? ;
+ >r members>> r> [ class<= ] curry contains? ;
: right-anonymous-intersection< ( first second -- ? )
- members>> [ class< ] with all? ;
+ members>> [ class<= ] with all? ;
: anonymous-complement< ( first second -- ? )
- [ class>> ] bi@ swap class< ;
+ [ class>> ] bi@ swap class<= ;
-: (class<) ( first second -- -1/0/1 )
+: (class<=) ( first second -- -1/0/1 )
{
{ [ 2dup eq? ] [ 2drop t ] }
{ [ dup object eq? ] [ 2drop t ] }
@@ -77,13 +77,13 @@ C: anonymous-complement
{ [ 2dup [ anonymous-complement? ] both? ] [ anonymous-complement< ] }
{ [ over anonymous-union? ] [ left-anonymous-union< ] }
{ [ over anonymous-intersection? ] [ left-anonymous-intersection< ] }
- { [ over members ] [ left-union-class< ] }
+ { [ over members ] [ left-union-class<= ] }
{ [ dup anonymous-union? ] [ right-anonymous-union< ] }
{ [ dup anonymous-intersection? ] [ right-anonymous-intersection< ] }
{ [ over anonymous-complement? ] [ 2drop f ] }
{ [ dup anonymous-complement? ] [ class>> classes-intersect? not ] }
- { [ dup members ] [ right-union-class< ] }
- { [ over superclass ] [ superclass< ] }
+ { [ dup members ] [ right-union-class<= ] }
+ { [ over superclass ] [ superclass<= ] }
[ 2drop f ]
} cond ;
@@ -94,7 +94,7 @@ C: anonymous-complement
members>> [ classes-intersect? ] with all? ;
: anonymous-complement-intersect? ( first second -- ? )
- class>> class< not ;
+ class>> class<= not ;
: union-class-intersect? ( first second -- ? )
members [ classes-intersect? ] with contains? ;
@@ -103,7 +103,7 @@ C: anonymous-complement
{
{ [ over tuple eq? ] [ 2drop t ] }
{ [ over builtin-class? ] [ 2drop f ] }
- { [ over tuple-class? ] [ [ class< ] [ swap class< ] 2bi or ] }
+ { [ over tuple-class? ] [ [ class<= ] [ swap class<= ] 2bi or ] }
[ swap classes-intersect? ]
} cond ;
@@ -145,8 +145,8 @@ C: anonymous-complement
: (class-and) ( first second -- class )
{
- { [ 2dup class< ] [ drop ] }
- { [ 2dup swap class< ] [ nip ] }
+ { [ 2dup class<= ] [ drop ] }
+ { [ 2dup swap class<= ] [ nip ] }
{ [ 2dup classes-intersect? not ] [ 2drop null ] }
{ [ dup members ] [ right-union-and ] }
{ [ dup anonymous-union? ] [ right-anonymous-union-and ] }
@@ -165,8 +165,8 @@ C: anonymous-complement
: (class-or) ( first second -- class )
{
- { [ 2dup class< ] [ nip ] }
- { [ 2dup swap class< ] [ drop ] }
+ { [ 2dup class<= ] [ nip ] }
+ { [ 2dup swap class<= ] [ drop ] }
{ [ dup anonymous-union? ] [ right-anonymous-union-or ] }
{ [ over anonymous-union? ] [ left-anonymous-union-or ] }
[ 2array ]
@@ -180,14 +180,19 @@ C: anonymous-complement
[ ]
} cond ;
+: class< ( first second -- ? )
+ {
+ { [ 2dup class<= not ] [ 2drop f ] }
+ { [ 2dup swap class<= not ] [ 2drop t ] }
+ [ [ rank-class ] bi@ < ]
+ } cond ;
+
: largest-class ( seq -- n elt )
- dup [
- [ 2dup class< >r swap class< not r> and ]
- with filter empty?
- ] curry find [ "Topological sort failed" throw ] unless* ;
+ dup [ [ class< ] with contains? not ] curry find-last
+ [ "Topological sort failed" throw ] unless* ;
: sort-classes ( seq -- newseq )
- >vector
+ [ [ word-name ] compare ] sort >vector
[ dup empty? not ]
[ dup largest-class >r over delete-nth r> ]
[ ] unfold nip ;
@@ -195,7 +200,7 @@ C: anonymous-complement
: min-class ( class seq -- class/f )
over [ classes-intersect? ] curry filter
dup empty? [ 2drop f ] [
- tuck [ class< ] with all? [ peek ] [ drop f ] if
+ tuck [ class<= ] with all? [ peek ] [ drop f ] if
] if ;
: (flatten-class) ( class -- )
@@ -212,7 +217,7 @@ C: anonymous-complement
: flatten-builtin-class ( class -- assoc )
flatten-class [
- dup tuple class< [ 2drop tuple tuple ] when
+ dup tuple class<= [ 2drop tuple tuple ] when
] assoc-map ;
: class-types ( class -- seq )
diff --git a/core/classes/builtin/builtin.factor b/core/classes/builtin/builtin.factor
index 1c2871b031..8e992b852e 100644
--- a/core/classes/builtin/builtin.factor
+++ b/core/classes/builtin/builtin.factor
@@ -16,3 +16,5 @@ PREDICATE: builtin-class < class
M: hi-tag class hi-tag type>class ;
M: object class tag type>class ;
+
+M: builtin-class rank-class drop 0 ;
diff --git a/core/classes/classes-docs.factor b/core/classes/classes-docs.factor
index 5971ffd9fa..744944c281 100755
--- a/core/classes/classes-docs.factor
+++ b/core/classes/classes-docs.factor
@@ -47,6 +47,7 @@ $nl
$nl
"Classes can be inspected and operated upon:"
{ $subsection "class-operations" }
+{ $subsection "class-linearization" }
{ $see-also "class-index" } ;
ABOUT: "classes"
diff --git a/core/classes/classes-tests.factor b/core/classes/classes-tests.factor
index ae19f38d14..bb9fbd0167 100755
--- a/core/classes/classes-tests.factor
+++ b/core/classes/classes-tests.factor
@@ -18,14 +18,14 @@ GENERIC: generic-update-test ( x -- y )
M: union-1 generic-update-test drop "union-1" ;
-[ f ] [ bignum union-1 class< ] unit-test
-[ t ] [ union-1 number class< ] unit-test
+[ f ] [ bignum union-1 class<= ] unit-test
+[ t ] [ union-1 number class<= ] unit-test
[ "union-1" ] [ 1.0 generic-update-test ] unit-test
"IN: classes.tests USE: math USE: arrays UNION: union-1 rational array ;" eval
-[ t ] [ bignum union-1 class< ] unit-test
-[ f ] [ union-1 number class< ] unit-test
+[ t ] [ bignum union-1 class<= ] unit-test
+[ f ] [ union-1 number class<= ] unit-test
[ "union-1" ] [ { 1.0 } generic-update-test ] unit-test
"IN: classes.tests USE: math PREDICATE: union-1 < integer even? ;" eval
@@ -52,7 +52,7 @@ M: sequence-mixin collection-size length ;
M: assoc-mixin collection-size assoc-size ;
-[ t ] [ array sequence-mixin class< ] unit-test
+[ t ] [ array sequence-mixin class<= ] unit-test
[ t ] [ { 1 2 3 } sequence-mixin? ] unit-test
[ 3 ] [ { 1 2 3 } collection-size ] unit-test
[ f ] [ H{ { 1 2 } { 2 3 } } sequence-mixin? ] unit-test
@@ -67,14 +67,14 @@ MIXIN: mx1
INSTANCE: integer mx1
-[ t ] [ integer mx1 class< ] unit-test
-[ t ] [ mx1 integer class< ] unit-test
-[ t ] [ mx1 number class< ] unit-test
+[ t ] [ integer mx1 class<= ] unit-test
+[ t ] [ mx1 integer class<= ] unit-test
+[ t ] [ mx1 number class<= ] unit-test
"IN: classes.tests USE: arrays INSTANCE: array mx1" eval
-[ t ] [ array mx1 class< ] unit-test
-[ f ] [ mx1 number class< ] unit-test
+[ t ] [ array mx1 class<= ] unit-test
+[ f ] [ mx1 number class<= ] unit-test
[ \ mx1 forget ] with-compilation-unit
@@ -94,14 +94,14 @@ UNION: redefine-bug-1 fixnum ;
UNION: redefine-bug-2 redefine-bug-1 quotation ;
-[ t ] [ fixnum redefine-bug-2 class< ] unit-test
-[ t ] [ quotation redefine-bug-2 class< ] unit-test
+[ t ] [ fixnum redefine-bug-2 class<= ] unit-test
+[ t ] [ quotation redefine-bug-2 class<= ] unit-test
[ ] [ "IN: classes.tests USE: math UNION: redefine-bug-1 bignum ;" eval ] unit-test
-[ t ] [ bignum redefine-bug-1 class< ] unit-test
-[ f ] [ fixnum redefine-bug-2 class< ] unit-test
-[ t ] [ bignum redefine-bug-2 class< ] unit-test
+[ t ] [ bignum redefine-bug-1 class<= ] unit-test
+[ f ] [ fixnum redefine-bug-2 class<= ] unit-test
+[ t ] [ bignum redefine-bug-2 class<= ] unit-test
USE: io.streams.string
diff --git a/core/classes/classes.factor b/core/classes/classes.factor
index c998a1b155..594b2005b8 100755
--- a/core/classes/classes.factor
+++ b/core/classes/classes.factor
@@ -5,21 +5,21 @@ slots.private namespaces sequences strings words vectors math
quotations combinators sorting effects graphs vocabs ;
IN: classes
-SYMBOL: class<-cache
+SYMBOL: class<=-cache
SYMBOL: class-not-cache
SYMBOL: classes-intersect-cache
SYMBOL: class-and-cache
SYMBOL: class-or-cache
: init-caches ( -- )
- H{ } clone class<-cache set
+ H{ } clone class<=-cache set
H{ } clone class-not-cache set
H{ } clone classes-intersect-cache set
H{ } clone class-and-cache set
H{ } clone class-or-cache set ;
: reset-caches ( -- )
- class<-cache get clear-assoc
+ class<=-cache get clear-assoc
class-not-cache get clear-assoc
classes-intersect-cache get clear-assoc
class-and-cache get clear-assoc
@@ -57,6 +57,8 @@ PREDICATE: predicate < word "predicating" word-prop >boolean ;
#! Output f for non-classes to work with algebra code
dup class? [ "members" word-prop ] [ drop f ] if ;
+GENERIC: rank-class ( class -- n )
+
GENERIC: reset-class ( class -- )
M: word reset-class drop ;
diff --git a/core/classes/mixin/mixin.factor b/core/classes/mixin/mixin.factor
index ca2547bacf..6f888ceca1 100755
--- a/core/classes/mixin/mixin.factor
+++ b/core/classes/mixin/mixin.factor
@@ -9,6 +9,8 @@ PREDICATE: mixin-class < union-class "mixin" word-prop ;
M: mixin-class reset-class
{ "class" "metaclass" "members" "mixin" } reset-props ;
+M: mixin-class rank-class drop 3 ;
+
: redefine-mixin-class ( class members -- )
dupd define-union-class
t "mixin" set-word-prop ;
diff --git a/core/classes/predicate/predicate.factor b/core/classes/predicate/predicate.factor
index 4729a6dd5e..4e4d1701e4 100755
--- a/core/classes/predicate/predicate.factor
+++ b/core/classes/predicate/predicate.factor
@@ -30,3 +30,5 @@ M: predicate-class reset-class
"predicate-definition"
"superclass"
} reset-props ;
+
+M: predicate-class rank-class drop 1 ;
diff --git a/core/classes/tuple/tuple-tests.factor b/core/classes/tuple/tuple-tests.factor
index 41776c4eec..fb9530b1c5 100755
--- a/core/classes/tuple/tuple-tests.factor
+++ b/core/classes/tuple/tuple-tests.factor
@@ -233,8 +233,8 @@ TUPLE: laptop < computer battery ;
C: laptop
[ t ] [ laptop tuple-class? ] unit-test
-[ t ] [ laptop tuple class< ] unit-test
-[ t ] [ laptop computer class< ] unit-test
+[ t ] [ laptop tuple class<= ] unit-test
+[ t ] [ laptop computer class<= ] unit-test
[ t ] [ laptop computer classes-intersect? ] unit-test
[ ] [ "Pentium" 128 3 hours "laptop" set ] unit-test
@@ -266,8 +266,8 @@ TUPLE: server < computer rackmount ;
C: server
[ t ] [ server tuple-class? ] unit-test
-[ t ] [ server tuple class< ] unit-test
-[ t ] [ server computer class< ] unit-test
+[ t ] [ server tuple class<= ] unit-test
+[ t ] [ server computer class<= ] unit-test
[ t ] [ server computer classes-intersect? ] unit-test
[ ] [ "PowerPC" 64 "1U" "server" set ] unit-test
@@ -286,8 +286,8 @@ test-server-slot-values
[ f ] [ "server" get laptop? ] unit-test
[ f ] [ "laptop" get server? ] unit-test
-[ f ] [ server laptop class< ] unit-test
-[ f ] [ laptop server class< ] unit-test
+[ f ] [ server laptop class<= ] unit-test
+[ f ] [ laptop server class<= ] unit-test
[ f ] [ laptop server classes-intersect? ] unit-test
[ f ] [ 1 2 laptop? ] unit-test
@@ -306,9 +306,9 @@ TUPLE: electronic-device ;
[ ] [ "IN: classes.tuple.tests TUPLE: computer < electronic-device cpu ram ;" eval ] unit-test
-[ f ] [ electronic-device laptop class< ] unit-test
-[ t ] [ server electronic-device class< ] unit-test
-[ t ] [ laptop server class-or electronic-device class< ] unit-test
+[ f ] [ electronic-device laptop class<= ] unit-test
+[ t ] [ server electronic-device class<= ] unit-test
+[ t ] [ laptop server class-or electronic-device class<= ] unit-test
[ t ] [ "laptop" get electronic-device? ] unit-test
[ t ] [ "laptop" get computer? ] unit-test
@@ -542,3 +542,15 @@ TUPLE: another-forget-accessors-test ;
! Missing error check
[ "IN: tuples.test USE: words TUPLE: wrong-superclass < word ;" eval ] must-fail
+
+TUPLE: subclass-forget-test ;
+
+TUPLE: subclass-forget-test-1 < subclass-forget-test ;
+TUPLE: subclass-forget-test-2 < subclass-forget-test ;
+TUPLE: subclass-forget-test-3 < subclass-forget-test-2 ;
+
+[ ] [ "IN: classes.tuple.tests FORGET: subclass-forget-test" eval ] unit-test
+
+[ f ] [ subclass-forget-test-1 tuple-class? ] unit-test
+[ f ] [ subclass-forget-test-2 tuple-class? ] unit-test
+[ subclass-forget-test-3 new ] must-fail
diff --git a/core/classes/tuple/tuple.factor b/core/classes/tuple/tuple.factor
index 8bcf023131..5ebcc7a286 100755
--- a/core/classes/tuple/tuple.factor
+++ b/core/classes/tuple/tuple.factor
@@ -102,7 +102,7 @@ ERROR: bad-superclass class ;
dup tuple-predicate-quot define-predicate ;
: superclass-size ( class -- n )
- superclasses 1 head-slice*
+ superclasses but-last-slice
[ slot-names length ] map sum ;
: generate-tuple-slots ( class slots -- slot-specs )
@@ -226,6 +226,8 @@ M: tuple-class reset-class
} reset-props
] bi ;
+M: tuple-class rank-class drop 0 ;
+
M: tuple clone
(clone) dup delegate clone over set-delegate ;
diff --git a/core/classes/union/union.factor b/core/classes/union/union.factor
index 09f8f88ced..760844afb9 100755
--- a/core/classes/union/union.factor
+++ b/core/classes/union/union.factor
@@ -30,3 +30,5 @@ M: union-class update-class define-union-predicate ;
M: union-class reset-class
{ "class" "metaclass" "members" } reset-props ;
+
+M: union-class rank-class drop 2 ;
diff --git a/core/compiler/compiler.factor b/core/compiler/compiler.factor
index 806ea914bb..ef00e94dd5 100755
--- a/core/compiler/compiler.factor
+++ b/core/compiler/compiler.factor
@@ -4,38 +4,55 @@ USING: kernel namespaces arrays sequences io inference.backend
inference.state generator debugger words compiler.units
continuations vocabs assocs alien.compiler dlists optimizer
definitions math compiler.errors threads graphs generic
-inference ;
+inference combinators ;
IN: compiler
: ripple-up ( word -- )
compiled-usage [ drop queue-compile ] assoc-each ;
: save-effect ( word effect -- )
- over "compiled-uses" word-prop [
- 2dup swap "compiled-effect" word-prop =
- [ over ripple-up ] unless
- ] when
- "compiled-effect" set-word-prop ;
-
-: finish-compile ( word effect dependencies -- )
- >r dupd save-effect r>
- over compiled-unxref
- over compiled-crossref? [ compiled-xref ] [ 2drop ] if ;
-
-: compile-succeeded ( word -- effect dependencies )
[
- [ word-dataflow optimize ] keep dup generate
- ] computing-dependencies ;
+ over "compiled-effect" word-prop = [
+ dup "compiled-uses" word-prop
+ [ dup ripple-up ] when
+ ] unless drop
+ ]
+ [ "compiled-effect" set-word-prop ] 2bi ;
+
+: compile-begins ( word -- )
+ f swap compiler-error ;
: compile-failed ( word error -- )
- f pick compiled get set-at
- swap compiler-error ;
+ [ swap compiler-error ]
+ [
+ drop
+ [ f swap compiled get set-at ]
+ [ f save-effect ]
+ bi
+ ] 2bi ;
+
+: compile-succeeded ( effect word -- )
+ [ swap save-effect ]
+ [ compiled-unxref ]
+ [
+ dup compiled-crossref?
+ [ dependencies get compiled-xref ] [ drop ] if
+ ] tri ;
: (compile) ( word -- )
- f over compiler-error
- [ dup compile-succeeded finish-compile ]
- [ dupd compile-failed f save-effect ]
- recover ;
+ [
+ H{ } clone dependencies set
+
+ {
+ [ compile-begins ]
+ [
+ [ word-dataflow ] [ compile-failed return ] recover
+ optimize
+ ]
+ [ dup generate ]
+ [ compile-succeeded ]
+ } cleave
+ ] curry with-return ;
: compile-loop ( assoc -- )
dup assoc-empty? [ drop ] [
diff --git a/core/compiler/errors/errors-docs.factor b/core/compiler/errors/errors-docs.factor
index dd71eb704f..d86587662b 100755
--- a/core/compiler/errors/errors-docs.factor
+++ b/core/compiler/errors/errors-docs.factor
@@ -21,19 +21,19 @@ HELP: compiler-error
HELP: compiler-error.
{ $values { "error" "an error" } { "word" word } }
-{ $description "Prints a compiler error to the " { $link stdio } " stream." } ;
+{ $description "Prints a compiler error to " { $link output-stream } "." } ;
HELP: compiler-errors.
{ $values { "type" symbol } }
-{ $description "Prints compiler errors to the " { $link stdio } " stream. The type parameter is one of " { $link +error+ } ", " { $link +warning+ } ", or " { $link +linkage+ } "." } ;
+{ $description "Prints compiler errors to " { $link output-stream } ". The type parameter is one of " { $link +error+ } ", " { $link +warning+ } ", or " { $link +linkage+ } "." } ;
HELP: :errors
-{ $description "Prints all serious compiler errors from the most recent compile to the " { $link stdio } " stream." } ;
+{ $description "Prints all serious compiler errors from the most recent compile to " { $link output-stream } "." } ;
HELP: :warnings
-{ $description "Prints all ignorable compiler warnings from the most recent compile to the " { $link stdio } " stream." } ;
+{ $description "Prints all ignorable compiler warnings from the most recent compile to " { $link output-stream } "." } ;
HELP: :linkage
-{ $description "Prints all C library interface linkage errors from the most recent compile to the " { $link stdio } " stream." } ;
+{ $description "Prints all C library interface linkage errors from the most recent compile to " { $link output-stream } "." } ;
{ :errors :warnings } related-words
diff --git a/core/continuations/continuations-docs.factor b/core/continuations/continuations-docs.factor
index b1db09b6bc..472136da8e 100755
--- a/core/continuations/continuations-docs.factor
+++ b/core/continuations/continuations-docs.factor
@@ -34,7 +34,7 @@ $nl
{ $code
" ... do stuff ... dispose"
}
-"The reason being that if " { $snippet "do stuff" } " throws an error, the resource will not be disposed of. The most important case where this can occur is with I/O streams, and the correct solution is to always use " { $link with-stream } "; see " { $link "stdio" } " for details." ;
+"The reason being that if " { $snippet "do stuff" } " throws an error, the resource will not be disposed of. The most important case where this can occur is with I/O streams, and the correct solution is to always use " { $link with-input-stream } " and " { $link with-output-stream } "; see " { $link "stdio" } " for details." ;
ARTICLE: "errors" "Error handling"
"Support for handling exceptional situations such as bad user input, implementation bugs, and input/output errors is provided by a set of words built using continuations."
diff --git a/core/continuations/continuations-tests.factor b/core/continuations/continuations-tests.factor
index 8b396763e1..28581820fd 100755
--- a/core/continuations/continuations-tests.factor
+++ b/core/continuations/continuations-tests.factor
@@ -1,6 +1,6 @@
USING: kernel math namespaces io tools.test sequences vectors
continuations debugger parser memory arrays words
-kernel.private ;
+kernel.private accessors ;
IN: continuations.tests
: (callcc1-test)
@@ -39,7 +39,7 @@ IN: continuations.tests
"!!! The following error is part of the test" print
-[ ] [ [ [ "2 car" ] eval ] [ print-error ] recover ] unit-test
+[ ] [ [ [ "2 car" ] eval ] try ] unit-test
[ f throw ] must-fail
@@ -100,3 +100,22 @@ SYMBOL: error-counter
[ 3 ] [ always-counter get ] unit-test
[ 1 ] [ error-counter get ] unit-test
] with-scope
+
+TUPLE: dispose-error ;
+
+M: dispose-error dispose 3 throw ;
+
+TUPLE: dispose-dummy disposed? ;
+
+M: dispose-dummy dispose t >>disposed? drop ;
+
+T{ dispose-error } "a" set
+T{ dispose-dummy } "b" set
+
+[ f ] [ "b" get disposed?>> ] unit-test
+
+[ { "a" "b" } [ get ] map dispose-each ] [ 3 = ] must-fail-with
+
+[ t ] [ "b" get disposed?>> ] unit-test
+
+[ ] [ [ return ] with-return ] unit-test
diff --git a/core/continuations/continuations.factor b/core/continuations/continuations.factor
index cf67280cca..78effb043a 100755
--- a/core/continuations/continuations.factor
+++ b/core/continuations/continuations.factor
@@ -101,6 +101,14 @@ PRIVATE>
: continue ( continuation -- )
f swap continue-with ;
+SYMBOL: return-continuation
+
+: with-return ( quot -- )
+ [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
+
+: return ( -- )
+ return-continuation get continue ;
+
GENERIC: compute-restarts ( error -- seq )
> error-help ;
M: assert summary drop "Assertion failed" ;
+M: assert error.
+ "Assertion failed" print
+ standard-table-style [
+ 15 length-limit set
+ 5 line-limit set
+ [ expect>> [ [ "Expect:" write ] with-cell pprint-cell ] with-row ]
+ [ got>> [ [ "Got:" write ] with-cell pprint-cell ] with-row ] bi
+ ] tabular-output ;
+
M: immutable summary drop "Sequence is immutable" ;
M: redefine-error error.
@@ -267,8 +270,7 @@ M: double-free summary
M: realloc-error summary
drop "Memory reallocation failed" ;
-: error-in-thread. ( -- )
- error-thread get-global
+: error-in-thread. ( thread -- )
"Error in thread " write
[
dup thread-id #
@@ -282,7 +284,7 @@ M: thread error-in-thread ( error thread -- )
die drop
] [
global [
- error-in-thread. print-error flush
+ error-thread get-global error-in-thread. print-error flush
] bind
] if ;
diff --git a/core/generator/registers/registers.factor b/core/generator/registers/registers.factor
index e0fd7bd457..c5e1ea54a6 100755
--- a/core/generator/registers/registers.factor
+++ b/core/generator/registers/registers.factor
@@ -181,11 +181,11 @@ INSTANCE: constant value
: %unbox-c-ptr ( dst src -- )
dup operand-class {
- { [ dup \ f class< ] [ drop %unbox-f ] }
- { [ dup simple-alien class< ] [ drop %unbox-alien ] }
- { [ dup byte-array class< ] [ drop %unbox-byte-array ] }
- { [ dup bit-array class< ] [ drop %unbox-byte-array ] }
- { [ dup float-array class< ] [ drop %unbox-byte-array ] }
+ { [ dup \ f class<= ] [ drop %unbox-f ] }
+ { [ dup simple-alien class<= ] [ drop %unbox-alien ] }
+ { [ dup byte-array class<= ] [ drop %unbox-byte-array ] }
+ { [ dup bit-array class<= ] [ drop %unbox-byte-array ] }
+ { [ dup float-array class<= ] [ drop %unbox-byte-array ] }
[ drop %unbox-any-c-ptr ]
} cond ; inline
@@ -569,7 +569,7 @@ M: loc lazy-store
{
{ f [ drop t ] }
{ known-tag [ class-tag >boolean ] }
- [ class< ]
+ [ class<= ]
} case ;
: spec-matches? ( value spec -- ? )
@@ -644,7 +644,7 @@ PRIVATE>
UNION: immediate fixnum POSTPONE: f ;
: operand-immediate? ( operand -- ? )
- operand-class immediate class< ;
+ operand-class immediate class<= ;
: phantom-push ( obj -- )
1 phantom-datastack get adjust-phantom
diff --git a/core/generic/generic-docs.factor b/core/generic/generic-docs.factor
index 1024c377a8..39293bfec9 100755
--- a/core/generic/generic-docs.factor
+++ b/core/generic/generic-docs.factor
@@ -4,22 +4,22 @@ generic.standard generic.math combinators ;
IN: generic
ARTICLE: "method-order" "Method precedence"
-"Consider the case where a generic word has methods on two classes, say A and B, which share a non-empty intersection. If the generic word is called on an object which is an instance of both A and B, a choice of method must be made. If A is a subclass of B, the method for A to be called; this makes sense, because we're defining general behavior for instances of B, and refining it for instances of A. Conversely, if B is a subclass of A, then we expect B's method to be called. However, if neither is a subclass of the other, we have an ambiguous situation and undefined behavior will result. Either the method for A or B will be called, and there is no way to predict ahead of time."
-$nl
-"The generic word system linearly orders all the methods on a generic word by their class. Conceptually, method dispatch is implemented by testing the object against the predicate word for every class, in order. If methods are defined on overlapping classes, this order will fail to be unique and the problem described above can occur."
+"Conceptually, method dispatch is implemented by testing the object against the predicate word for every class, in linear order (" { $link "class-linearization" } ")."
$nl
"Here is an example:"
{ $code
"GENERIC: explain"
- "M: number explain drop \"an integer\" print ;"
- "M: sequence explain drop \"a sequence\" print ;"
"M: object explain drop \"an object\" print ;"
+ "M: number explain drop \"a number\" print ;"
+ "M: sequence explain drop \"a sequence\" print ;"
}
-"Neither " { $link number } " nor " { $link sequence } " are subclasses of each other, yet their intersection is the non-empty " { $link integer } " class. As a result, the outcome of calling " { $snippet "bar" } " with an " { $link integer } " on the stack is undefined - either one of the two methods may be called. This situation can lead to subtle bugs. To avoid it, explicitly disambiguate the method order by defining a method on the intersection. If in this case we want integers to behave like numbers, we would also define:"
-{ $code "M: integer explain drop \"an integer\" print ;" }
-"On the other hand, if we want integers to behave like sequences here, we could define:"
+"The linear order is the following, from least-specific to most-specific:"
+{ $code "{ object sequence number }" }
+"Neither " { $link number } " nor " { $link sequence } " are subclasses of each other, yet their intersection is the non-empty " { $link integer } " class. Calling " { $snippet "explain" } " with an integer on the stack will print " { $snippet "a number" } " because " { $link number } " precedes " { $link sequence } " in the class linearization order. If this was not the desired outcome, define a method on the intersection:"
{ $code "M: integer explain drop \"a sequence\" print ;" }
-"The " { $link order } " word can be useful to clarify method dispatch order."
+"Now, the linear order is the following, from least-specific to most-specific:"
+{ $code "{ object sequence number integer }" }
+"The " { $link order } " word can be useful to clarify method dispatch order:"
{ $subsection order } ;
ARTICLE: "generic-introspection" "Generic word introspection"
diff --git a/core/generic/generic.factor b/core/generic/generic.factor
index 82bab475b3..d35ba01e52 100755
--- a/core/generic/generic.factor
+++ b/core/generic/generic.factor
@@ -35,7 +35,7 @@ PREDICATE: method-spec < pair
GENERIC: effective-method ( ... generic -- method )
: next-method-class ( class generic -- class/f )
- order [ class< ] with filter reverse dup length 1 =
+ order [ class<= ] with filter reverse dup length 1 =
[ drop f ] [ second ] if ;
: next-method ( class generic -- class/f )
diff --git a/core/generic/math/math.factor b/core/generic/math/math.factor
index 90590fe565..1c1368a6c2 100755
--- a/core/generic/math/math.factor
+++ b/core/generic/math/math.factor
@@ -10,14 +10,14 @@ PREDICATE: math-class < class
dup null bootstrap-word eq? [
drop f
] [
- number bootstrap-word class<
+ number bootstrap-word class<=
] if ;
: last/first ( seq -- pair ) [ peek ] [ first ] bi 2array ;
: math-precedence ( class -- pair )
{
- { [ dup null class< ] [ drop { -1 -1 } ] }
+ { [ dup null class<= ] [ drop { -1 -1 } ] }
{ [ dup math-class? ] [ class-types last/first ] }
[ drop { 100 100 } ]
} cond ;
diff --git a/core/generic/standard/engines/engines.factor b/core/generic/standard/engines/engines.factor
index c09f1abfd4..20e22fde82 100644
--- a/core/generic/standard/engines/engines.factor
+++ b/core/generic/standard/engines/engines.factor
@@ -26,8 +26,8 @@ M: method-body engine>quot 1quotation ;
alist>quot ;
: split-methods ( assoc class -- first second )
- [ [ nip class< not ] curry assoc-filter ]
- [ [ nip class< ] curry assoc-filter ] 2bi ;
+ [ [ nip class<= not ] curry assoc-filter ]
+ [ [ nip class<= ] curry assoc-filter ] 2bi ;
: convert-methods ( assoc class word -- assoc' )
over >r >r split-methods dup assoc-empty? [
diff --git a/core/generic/standard/engines/predicate/predicate.factor b/core/generic/standard/engines/predicate/predicate.factor
index e4643b2f3d..b1bfc659df 100644
--- a/core/generic/standard/engines/predicate/predicate.factor
+++ b/core/generic/standard/engines/predicate/predicate.factor
@@ -11,7 +11,7 @@ C: predicate-dispatch-engine
[ >r "predicate" word-prop picker prepend r> ] assoc-map ;
: keep-going? ( assoc -- ? )
- assumed get swap second first class< ;
+ assumed get swap second first class<= ;
: prune-redundant-predicates ( assoc -- default assoc' )
{
diff --git a/core/generic/standard/engines/tuple/tuple.factor b/core/generic/standard/engines/tuple/tuple.factor
index 7639d1d499..51ea4f8225 100644
--- a/core/generic/standard/engines/tuple/tuple.factor
+++ b/core/generic/standard/engines/tuple/tuple.factor
@@ -127,8 +127,6 @@ M: echelon-dispatch-engine engine>quot
1 slot { tuple-layout } declare
5 slot ; inline
-: unclip-last [ 1 head* ] [ peek ] bi ;
-
M: tuple-dispatch-engine engine>quot
[
picker %
diff --git a/core/inference/class/class.factor b/core/inference/class/class.factor
index 9d0c55afeb..933710aaca 100755
--- a/core/inference/class/class.factor
+++ b/core/inference/class/class.factor
@@ -143,7 +143,7 @@ M: literal-constraint constraint-satisfied?
[ swap literal>> eql? ] [ 2drop f ] if ;
M: class-constraint constraint-satisfied?
- [ value>> value-class* ] [ class>> ] bi class< ;
+ [ value>> value-class* ] [ class>> ] bi class<= ;
M: pair apply-constraint
first2 2dup constraints get set-at
diff --git a/core/inference/inference-docs.factor b/core/inference/inference-docs.factor
index e32c94ed37..d79c82ed65 100755
--- a/core/inference/inference-docs.factor
+++ b/core/inference/inference-docs.factor
@@ -135,7 +135,7 @@ HELP: infer
HELP: infer.
{ $values { "quot" "a quotation" } }
-{ $description "Attempts to infer the quotation's stack effect, and prints this data to the " { $link stdio } " stream." }
+{ $description "Attempts to infer the quotation's stack effect, and prints this data to " { $link output-stream } "." }
{ $errors "Throws an " { $link inference-error } " if stack effect inference fails." } ;
{ infer infer. } related-words
diff --git a/core/inference/state/state-tests.factor b/core/inference/state/state-tests.factor
index 84d72bdd9b..c63786dc9e 100644
--- a/core/inference/state/state-tests.factor
+++ b/core/inference/state/state-tests.factor
@@ -1,5 +1,9 @@
IN: inference.state.tests
-USING: tools.test inference.state words ;
+USING: tools.test inference.state words kernel namespaces ;
+
+: computing-dependencies ( quot -- dependencies )
+ H{ } clone [ dependencies rot with-variable ] keep ;
+ inline
SYMBOL: a
SYMBOL: b
diff --git a/core/inference/state/state.factor b/core/inference/state/state.factor
index a426f410e2..6f0eecf2d9 100755
--- a/core/inference/state/state.factor
+++ b/core/inference/state/state.factor
@@ -36,10 +36,6 @@ SYMBOL: dependencies
2dup at +inlined+ eq? [ 3drop ] [ set-at ] if
] [ 3drop ] if ;
-: computing-dependencies ( quot -- dependencies )
- H{ } clone [ dependencies rot with-variable ] keep ;
- inline
-
! Did the current control-flow path throw an error?
SYMBOL: terminated?
diff --git a/core/inference/transforms/transforms.factor b/core/inference/transforms/transforms.factor
index 624dcbbf98..0040629edd 100755
--- a/core/inference/transforms/transforms.factor
+++ b/core/inference/transforms/transforms.factor
@@ -32,7 +32,7 @@ IN: inference.transforms
drop [ no-case ]
] [
dup peek quotation? [
- dup peek swap 1 head*
+ dup peek swap but-last
] [
[ no-case ] swap
] if case>quot
diff --git a/core/inspector/inspector-docs.factor b/core/inspector/inspector-docs.factor
index 84ae34480d..ab1c38b0b7 100644
--- a/core/inspector/inspector-docs.factor
+++ b/core/inspector/inspector-docs.factor
@@ -108,4 +108,4 @@ HELP: me
HELP: inspector-hook
{ $var-description "A quotation with stack effect " { $snippet "( obj -- )" } ", called by the inspector to display an overview of an object."
$nl
-"The default implementation calls " { $link describe } " which outputs on the " { $link stdio } " stream, but the graphical listener sets this variable so that calling " { $link inspect } " in the UI opens the graphical inspector." } ;
+"The default implementation calls " { $link describe } " which outputs on " { $link output-stream } ", but the graphical listener sets this variable so that calling " { $link inspect } " in the UI opens the graphical inspector." } ;
diff --git a/core/io/backend/backend-docs.factor b/core/io/backend/backend-docs.factor
index 8bf761e2a6..48b49ed32b 100644
--- a/core/io/backend/backend-docs.factor
+++ b/core/io/backend/backend-docs.factor
@@ -9,4 +9,4 @@ HELP: init-io
{ $contract "Initializes the I/O system. Called on startup." } ;
HELP: init-stdio
-{ $contract "Initializes the global " { $link stdio } " stream. Called on startup." } ;
+{ $contract "Initializes the global " { $link input-stream } " and " { $link output-stream } ". Called on startup." } ;
diff --git a/core/io/backend/backend.factor b/core/io/backend/backend.factor
index 44b1eea349..0760063f0d 100755
--- a/core/io/backend/backend.factor
+++ b/core/io/backend/backend.factor
@@ -11,8 +11,10 @@ HOOK: init-io io-backend ( -- )
HOOK: (init-stdio) io-backend ( -- stdin stdout stderr )
: init-stdio ( -- )
- (init-stdio) utf8 stderr set-global
- utf8 stdio set-global ;
+ (init-stdio)
+ [ utf8 input-stream set-global ]
+ [ utf8 output-stream set-global ]
+ [ utf8 error-stream set-global ] tri* ;
HOOK: io-multiplex io-backend ( ms -- )
diff --git a/core/io/crc32/crc32-docs.factor b/core/io/crc32/crc32-docs.factor
deleted file mode 100644
index 7f85ee2b4e..0000000000
--- a/core/io/crc32/crc32-docs.factor
+++ /dev/null
@@ -1,17 +0,0 @@
-USING: help.markup help.syntax math ;
-IN: io.crc32
-
-HELP: crc32
-{ $values { "seq" "a sequence of bytes" } { "n" integer } }
-{ $description "Computes the CRC32 checksum of a sequence of bytes." } ;
-
-HELP: lines-crc32
-{ $values { "seq" "a sequence of strings" } { "n" integer } }
-{ $description "Computes the CRC32 checksum of a sequence of lines of bytes." } ;
-
-ARTICLE: "io.crc32" "CRC32 checksum calculation"
-"The CRC32 checksum algorithm provides a quick but unreliable way to detect changes in data."
-{ $subsection crc32 }
-{ $subsection lines-crc32 } ;
-
-ABOUT: "io.crc32"
diff --git a/core/io/crc32/crc32-tests.factor b/core/io/crc32/crc32-tests.factor
deleted file mode 100644
index 5eafae23cb..0000000000
--- a/core/io/crc32/crc32-tests.factor
+++ /dev/null
@@ -1,5 +0,0 @@
-USING: io.crc32 kernel math tools.test namespaces ;
-
-[ 0 ] [ "" crc32 ] unit-test
-[ HEX: cbf43926 ] [ "123456789" crc32 ] unit-test
-
diff --git a/core/io/encodings/encodings-docs.factor b/core/io/encodings/encodings-docs.factor
index 8a176ce4ec..92471acb5d 100644
--- a/core/io/encodings/encodings-docs.factor
+++ b/core/io/encodings/encodings-docs.factor
@@ -12,8 +12,7 @@ ARTICLE: "io.encodings" "I/O encodings"
ARTICLE: "encodings-constructors" "Manually constructing an encoded stream"
"The following words can be used to construct encoded streams. Note that they are usually not used directly, but rather by the stream constructors themselves. Most stream constructors take an encoding descriptor as a parameter and internally call these constructors."
{ $subsection }
-{ $subsection }
-{ $subsection } ;
+{ $subsection } ;
HELP:
{ $values { "stream" "an output stream" }
@@ -29,16 +28,6 @@ HELP:
{ $description "Wraps the given stream in a new stream using the given encoding for all input. The encoding descriptor can either be a class or an instance of something conforming to the " { $link "encodings-protocol" } "." }
$low-level-note ;
-HELP:
-{ $values { "stream-in" "an input stream" }
- { "stream-out" "an output stream" }
- { "encoding" "an encoding descriptor" }
- { "duplex" "an encoded duplex stream" } }
-{ $description "Wraps the given streams in an encoder or decoder stream, and puts them together in a duplex stream for input and output. If either input stream is already encoded, that encoding is stripped off before it is reencoded. The encoding descriptor must conform to the " { $link "encodings-protocol" } "." }
-$low-level-note ;
-
-{ } related-words
-
ARTICLE: "encodings-descriptors" "Encoding descriptors"
"An encoding descriptor is something which can be used for input or output streams to encode or decode files. It must conform to the " { $link "encodings-protocol" } ". Encodings which you can use are defined in the following vocabularies:"
{ $subsection "io.encodings.binary" }
diff --git a/core/io/encodings/encodings-tests.factor b/core/io/encodings/encodings-tests.factor
index 397d1ea89c..e6b180fde2 100755
--- a/core/io/encodings/encodings-tests.factor
+++ b/core/io/encodings/encodings-tests.factor
@@ -2,35 +2,35 @@ USING: io.files io.streams.string io
tools.test kernel io.encodings.ascii ;
IN: io.streams.encodings.tests
-: ( resource -- stream )
- resource-path ascii ;
-
[ { } ]
-[ "core/io/test/empty-file.txt" lines ]
+[ "resource:core/io/test/empty-file.txt" ascii lines ]
unit-test
: lines-test ( stream -- line1 line2 )
- [ readln readln ] with-stream ;
+ [ readln readln ] with-input-stream ;
[
"This is a line."
"This is another line."
] [
- "core/io/test/windows-eol.txt" lines-test
+ "resource:core/io/test/windows-eol.txt"
+ ascii lines-test
] unit-test
[
"This is a line."
"This is another line."
] [
- "core/io/test/mac-os-eol.txt" lines-test
+ "resource:core/io/test/mac-os-eol.txt"
+ ascii lines-test
] unit-test
[
"This is a line."
"This is another line."
] [
- "core/io/test/unix-eol.txt" lines-test
+ "resource:core/io/test/unix-eol.txt"
+ ascii lines-test
] unit-test
[
diff --git a/core/io/encodings/encodings.factor b/core/io/encodings/encodings.factor
index 4559cec666..0f6e58bdc9 100755
--- a/core/io/encodings/encodings.factor
+++ b/core/io/encodings/encodings.factor
@@ -2,8 +2,8 @@
! See http://factorcode.org/license.txt for BSD license.
USING: math kernel sequences sbufs vectors namespaces growable
strings io classes continuations combinators io.styles
-io.streams.plain splitting io.streams.duplex byte-arrays
-sequences.private accessors ;
+io.streams.plain splitting byte-arrays sequences.private
+accessors ;
IN: io.encodings
! The encoding descriptor protocol
@@ -131,6 +131,3 @@ INSTANCE: encoder plain-writer
over decoder? [ >r decoder-stream r> ] when ;
PRIVATE>
-
-: ( stream-in stream-out encoding -- duplex )
- tuck reencode >r redecode r> ;
diff --git a/core/io/files/files-docs.factor b/core/io/files/files-docs.factor
index ba17223a29..d18babf31b 100755
--- a/core/io/files/files-docs.factor
+++ b/core/io/files/files-docs.factor
@@ -184,8 +184,12 @@ HELP: +unknown+
{ $description "A unknown file type." } ;
HELP:
-{ $values { "path" "a pathname string" } { "encoding" "an encoding descriptor" { "stream" "an input stream" } }
- { "stream" "an input stream" } }
+{
+ $values
+ { "path" "a pathname string" }
+ { "encoding" "an encoding descriptor" }
+ { "stream" "an input stream" }
+}
{ $description "Outputs an input stream for reading from the specified pathname using the given encoding." }
{ $errors "Throws an error if the file is unreadable." } ;
@@ -201,17 +205,17 @@ HELP:
HELP: with-file-reader
{ $values { "path" "a pathname string" } { "encoding" "an encoding descriptor" } { "quot" "a quotation" } }
-{ $description "Opens a file for reading and calls the quotation using " { $link with-stream } "." }
+{ $description "Opens a file for reading and calls the quotation using " { $link with-input-stream } "." }
{ $errors "Throws an error if the file is unreadable." } ;
HELP: with-file-writer
{ $values { "path" "a pathname string" } { "encoding" "an encoding descriptor" } { "quot" "a quotation" } }
-{ $description "Opens a file for writing using the given encoding and calls the quotation using " { $link with-stream } "." }
+{ $description "Opens a file for writing using the given encoding and calls the quotation using " { $link with-output-stream } "." }
{ $errors "Throws an error if the file cannot be opened for writing." } ;
HELP: with-file-appender
{ $values { "path" "a pathname string" } { "encoding" "an encoding descriptor" } { "quot" "a quotation" } }
-{ $description "Opens a file for appending using the given encoding and calls the quotation using " { $link with-stream } "." }
+{ $description "Opens a file for appending using the given encoding and calls the quotation using " { $link with-output-stream } "." }
{ $errors "Throws an error if the file cannot be opened for writing." } ;
HELP: set-file-lines
diff --git a/core/io/files/files.factor b/core/io/files/files.factor
index 576307b589..76c7b144d0 100755
--- a/core/io/files/files.factor
+++ b/core/io/files/files.factor
@@ -25,13 +25,13 @@ HOOK: (file-appender) io-backend ( path -- stream )
lines ;
: with-file-reader ( path encoding quot -- )
- >r r> with-stream ; inline
+ >r r> with-input-stream ; inline
: file-contents ( path encoding -- str )
contents ;
: with-file-writer ( path encoding quot -- )
- >r r> with-stream ; inline
+ >r r> with-output-stream ; inline
: set-file-lines ( seq path encoding -- )
[ [ print ] each ] with-file-writer ;
@@ -40,7 +40,7 @@ HOOK: (file-appender) io-backend ( path -- stream )
[ write ] with-file-writer ;
: with-file-appender ( path encoding quot -- )
- >r r> with-stream ; inline
+ >r r> with-output-stream ; inline
! Pathnames
: path-separator? ( ch -- ? ) os windows? "/\\" "/" ? member? ;
diff --git a/core/io/io-docs.factor b/core/io/io-docs.factor
index fd40950e62..ddea4da556 100755
--- a/core/io/io-docs.factor
+++ b/core/io/io-docs.factor
@@ -5,7 +5,7 @@ IN: io
ARTICLE: "stream-protocol" "Stream protocol"
"The stream protocol consists of a large number of generic words, many of which are optional."
$nl
-"Stream protocol words are rarely called directly, since code which only works with one stream at a time should be written use " { $link "stdio" } " instead, wrapping I/O operations such as " { $link read } " and " { $link write } " in a " { $link with-stream } ". This leads more simpler, more reusable and more robust code."
+"Stream protocol words are rarely called directly, since code which only works with one stream at a time should be written use " { $link "stdio" } " instead, wrapping I/O operations such as " { $link read } " and " { $link write } " in " { $link with-input-stream } " and " { $link with-output-stream } "."
$nl
"All streams must implement the " { $link dispose } " word in addition to the stream protocol."
$nl
@@ -26,24 +26,24 @@ $nl
{ $subsection stream-write-table }
{ $see-also "io.timeouts" } ;
-ARTICLE: "stdio" "The default stream"
-"Most I/O code only operates on one stream at a time. The " { $emphasis "default stream" } " is an implicit parameter used by many I/O words designed for this particular use-case. Using this idiom improves code in three ways:"
+ARTICLE: "stdio" "Default input and output streams"
+"Most I/O code only operates on one stream at a time. The " { $link input-stream } " and " { $link output-stream } " variables are implicit parameters used by many I/O words. Using this idiom improves code in three ways:"
{ $list
{ "Code becomes simpler because there is no need to keep a stream around on the stack." }
- { "Code becomes more robust because " { $link with-stream } " automatically closes the stream if there is an error." }
- { "Code becomes more reusable because it can be written to not worry about which stream is being used, and instead the caller can use " { $link with-stream } " to specify the source or destination for I/O operations." }
+ { "Code becomes more robust because " { $link with-input-stream } " and " { $link with-output-stream } " automatically close the streams if there is an error." }
+ { "Code becomes more reusable because it can be written to not worry about which stream is being used, and instead the caller can use " { $link with-input-stream } " or " { $link with-output-stream } " to specify the source or destination for I/O operations." }
}
"For example, here is a program which reads the first line of a file, converts it to an integer, then reads that many characters, and splits them into groups of 16:"
{ $code
"USING: continuations kernel io io.files math.parser splitting ;"
- "\"data.txt\" "
+ "\"data.txt\" utf8 "
"dup stream-readln number>string over stream-read 16 group"
"swap dispose"
}
"This code has two problems: it has some unnecessary stack shuffling, and if either " { $link stream-readln } " or " { $link stream-read } " throws an I/O error, the stream is not closed because " { $link dispose } " is never reached. So we can add a call to " { $link with-disposal } " to ensure the stream is always closed:"
{ $code
"USING: continuations kernel io io.files math.parser splitting ;"
- "\"data.txt\" ["
+ "\"data.txt\" utf8 ["
" dup stream-readln number>string over stream-read"
" 16 group"
"] with-disposal"
@@ -51,17 +51,34 @@ ARTICLE: "stdio" "The default stream"
"This code is robust however it is more complex than it needs to be since. This is where the default stream words come in; using them, the above can be rewritten as follows:"
{ $code
"USING: continuations kernel io io.files math.parser splitting ;"
- "\"data.txt\" ["
+ "\"data.txt\" utf8 ["
" readln number>string read 16 group"
- "] with-stream"
+ "] with-input-stream"
}
-"The default stream is stored in a dynamically-scoped variable:"
-{ $subsection stdio }
-"Unless rebound in a child namespace, this variable will be set to a console stream for interacting with the user."
+"An even better implementation that takes advantage of a utility word:"
+{ $code
+ "USING: continuations kernel io io.files math.parser splitting ;"
+ "\"data.txt\" utf8 ["
+ " readln number>string read 16 group"
+ "] with-file-reader"
+}
+"The default input stream is stored in a dynamically-scoped variable:"
+{ $subsection input-stream }
+"Unless rebound in a child namespace, this variable will be set to a console stream for reading input from the user."
+$nl
+"Words reading from the default input stream:"
{ $subsection read1 }
{ $subsection read }
{ $subsection read-until }
{ $subsection readln }
+"A pair of combinators for rebinding the " { $link input-stream } " variable:"
+{ $subsection with-input-stream }
+{ $subsection with-input-stream* }
+"The default output stream is stored in a dynamically-scoped variable:"
+{ $subsection output-stream }
+"Unless rebound in a child namespace, this variable will be set to a console stream for showing output to the user."
+$nl
+"Words writing to the default input stream:"
{ $subsection flush }
{ $subsection write1 }
{ $subsection write }
@@ -78,9 +95,12 @@ ARTICLE: "stdio" "The default stream"
{ $subsection with-row }
{ $subsection with-cell }
{ $subsection write-cell }
-"A pair of combinators support rebinding the " { $link stdio } " variable:"
-{ $subsection with-stream }
-{ $subsection with-stream* } ;
+"A pair of combinators for rebinding the " { $link output-stream } " variable:"
+{ $subsection with-output-stream }
+{ $subsection with-output-stream* }
+"A pair of combinators for rebinding both default streams at once:"
+{ $subsection with-streams }
+{ $subsection with-streams* } ;
ARTICLE: "stream-utils" "Stream utilities"
"There are a few useful stream-related words which are not generic, but merely built up from the stream protocol."
@@ -204,62 +224,65 @@ HELP: stream-copy
{ $description "Copies the contents of one stream into another, closing both streams when done." }
$io-error ;
-HELP: stdio
-{ $var-description "Holds a stream, used for various implicit stream operations. Rebound using " { $link with-stream } " and " { $link with-stream* } "." } ;
+HELP: input-stream
+{ $var-description "Holds an input stream for various implicit stream operations. Rebound using " { $link with-input-stream } " and " { $link with-input-stream* } "." } ;
+
+HELP: output-stream
+{ $var-description "Holds an output stream for various implicit stream operations. Rebound using " { $link with-output-stream } " and " { $link with-output-stream* } "." } ;
HELP: readln
{ $values { "str/f" "a string or " { $link f } } }
-{ $description "Reads a line of input from the " { $link stdio } " stream. Outputs " { $link f } " on stream exhaustion." }
+{ $description "Reads a line of input from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
$io-error ;
HELP: read1
{ $values { "ch/f" "a character or " { $link f } } }
-{ $description "Reads a character of input from the " { $link stdio } " stream. Outputs " { $link f } " on stream exhaustion." }
+{ $description "Reads a character of input from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
$io-error ;
HELP: read
{ $values { "n" "a non-negative integer" } { "str/f" "a string or " { $link f } } }
-{ $description "Reads " { $snippet "n" } " characters of input from the " { $link stdio } " stream. Outputs a truncated string or " { $link f } " on stream exhaustion." }
+{ $description "Reads " { $snippet "n" } " characters of input from " { $link input-stream } ". Outputs a truncated string or " { $link f } " on stream exhaustion." }
$io-error ;
HELP: read-until
{ $values { "seps" string } { "str/f" "a string or " { $link f } } { "sep/f" "a character or " { $link f } } }
-{ $contract "Reads characters from the " { $link stdio } " stream. until the first occurrence of a separator character, or stream exhaustion. In the former case, the separator character is pushed on the stack, and is not part of the output string. In the latter case, the entire stream contents are output, along with " { $link f } "." }
+{ $contract "Reads characters from " { $link input-stream } ". until the first occurrence of a separator character, or stream exhaustion. In the former case, the separator character is pushed on the stack, and is not part of the output string. In the latter case, the entire stream contents are output, along with " { $link f } "." }
$io-error ;
HELP: write1
{ $values { "ch" "a character" } }
-{ $contract "Writes a character of output to the " { $link stdio } " stream. If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
+{ $contract "Writes a character of output to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
$io-error ;
HELP: write
{ $values { "str" string } }
-{ $description "Writes a string of output to the " { $link stdio } " stream. If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
+{ $description "Writes a string of output to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
$io-error ;
HELP: flush
-{ $description "Waits for any pending output to the " { $link stdio } " stream to complete." }
+{ $description "Waits for any pending output on " { $link output-stream } " to complete." }
$io-error ;
HELP: nl
-{ $description "Writes a line terminator to the " { $link stdio } " stream. If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
+{ $description "Writes a line terminator to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
$io-error ;
HELP: format
{ $values { "str" string } { "style" "a hashtable" } }
-{ $description "Writes formatted text to the " { $link stdio } " stream. If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
+{ $description "Writes formatted text to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
{ $notes "Details are in the documentation for " { $link stream-format } "." }
$io-error ;
HELP: with-nesting
-{ $values { "style" "a hashtable" } { "quot" "a quotation" } }
-{ $description "Calls the quotation in a new dynamic scope with the " { $link stdio } " stream rebound to a nested paragraph stream, with formatting information applied." }
+{ $values { "style" "a hashtable" } { "quot" quotation } }
+{ $description "Calls the quotation in a new dynamic scope with " { $link output-stream } " rebound to a nested paragraph stream, with formatting information applied." }
{ $notes "Details are in the documentation for " { $link make-block-stream } "." }
$io-error ;
HELP: tabular-output
{ $values { "style" "a hashtable" } { "quot" quotation } }
-{ $description "Calls a quotation which emits a series of equal-length table rows using " { $link with-row } ". The results are laid out in a tabular fashion on the " { $link stdio } " stream."
+{ $description "Calls a quotation which emits a series of equal-length table rows using " { $link with-row } ". The results are laid out in a tabular fashion on " { $link output-stream } "."
$nl
"The " { $snippet "style" } " hashtable holds table style information. See " { $link "table-styles" } "." }
{ $examples
@@ -279,7 +302,7 @@ $io-error ;
HELP: with-cell
{ $values { "quot" quotation } }
-{ $description "Calls a quotation in a new scope with the " { $link stdio } " stream rebound. Output performed by the quotation is displayed in a table cell. This word can only be called inside the quotation given to " { $link with-row } "." }
+{ $description "Calls a quotation in a new scope with " { $link output-stream } " rebound. Output performed by the quotation is displayed in a table cell. This word can only be called inside the quotation given to " { $link with-row } "." }
$io-error ;
HELP: write-cell
@@ -288,34 +311,54 @@ HELP: write-cell
$io-error ;
HELP: with-style
-{ $values { "style" "a hashtable" } { "quot" "a quotation" } }
+{ $values { "style" "a hashtable" } { "quot" quotation } }
{ $description "Calls the quotation in a new dynamic scope where calls to " { $link write } ", " { $link format } " and other stream output words automatically inherit style settings from " { $snippet "style" } "." }
{ $notes "Details are in the documentation for " { $link make-span-stream } "." }
$io-error ;
HELP: print
{ $values { "string" string } }
-{ $description "Writes a newline-terminated string to the " { $link stdio } " stream." }
+{ $description "Writes a newline-terminated string to " { $link output-stream } "." }
$io-error ;
-HELP: with-stream
-{ $values { "stream" "an input or output stream" } { "quot" "a quotation" } }
-{ $description "Calls the quotation in a new dynamic scope, with the " { $link stdio } " variable rebound to " { $snippet "stream" } ". The stream is closed if the quotation returns or throws an error." } ;
+HELP: with-input-stream
+{ $values { "stream" "an input stream" } { "quot" quotation } }
+{ $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "stream" } ". The stream is closed if the quotation returns or throws an error." } ;
-{ with-stream with-stream* } related-words
+HELP: with-output-stream
+{ $values { "stream" "an output stream" } { "quot" quotation } }
+{ $description "Calls the quotation in a new dynamic scope, with " { $link output-stream } " rebound to " { $snippet "stream" } ". The stream is closed if the quotation returns or throws an error." } ;
-HELP: with-stream*
-{ $values { "stream" "an input or output stream" } { "quot" "a quotation" } }
-{ $description "Calls the quotation in a new dynamic scope, with the " { $link stdio } " variable rebound to " { $snippet "stream" } "." }
-{ $notes "This word does not close the stream. Compare with " { $link with-stream } "." } ;
+HELP: with-streams
+{ $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
+{ $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "input" } " and " { $link output-stream } " rebound to " { $snippet "output" } ". The stream is closed if the quotation returns or throws an error." } ;
+
+HELP: with-streams*
+{ $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
+{ $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "input" } " and " { $link output-stream } " rebound to " { $snippet "output" } "." }
+{ $notes "This word does not close the stream. Compare with " { $link with-streams } "." } ;
+
+{ with-input-stream with-input-stream* } related-words
+
+{ with-output-stream with-output-stream* } related-words
+
+HELP: with-input-stream*
+{ $values { "stream" "an input stream" } { "quot" quotation } }
+{ $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "stream" } "." }
+{ $notes "This word does not close the stream. Compare with " { $link with-input-stream } "." } ;
+
+HELP: with-output-stream*
+{ $values { "stream" "an output stream" } { "quot" quotation } }
+{ $description "Calls the quotation in a new dynamic scope, with " { $link output-stream } " rebound to " { $snippet "stream" } "." }
+{ $notes "This word does not close the stream. Compare with " { $link with-output-stream } "." } ;
HELP: bl
-{ $description "Outputs a space character (" { $snippet "\" \"" } ")." }
+{ $description "Outputs a space character (" { $snippet "\" \"" } ") to " { $link output-stream } "." }
$io-error ;
HELP: write-object
{ $values { "str" string } { "obj" "an object" } }
-{ $description "Writes a string to the " { $link stdio } " stream, associating it with the object. If formatted output is supported, the string will become a clickable presentation of the object, otherwise this word behaves like a call to " { $link write } "." }
+{ $description "Writes a string to " { $link output-stream } ", associating it with the object. If formatted output is supported, the string will become a clickable presentation of the object, otherwise this word behaves like a call to " { $link write } "." }
$io-error ;
HELP: lines
diff --git a/core/io/io-tests.factor b/core/io/io-tests.factor
index b7d1cf81c8..50a798d290 100755
--- a/core/io/io-tests.factor
+++ b/core/io/io-tests.factor
@@ -8,21 +8,18 @@ IN: io.tests
"foo" "io.tests" lookup
] unit-test
-: ( resource -- stream )
- resource-path latin1 ;
-
[
"This is a line.\rThis is another line.\r"
] [
- "core/io/test/mac-os-eol.txt"
- [ 500 read ] with-stream
+ "resource:core/io/test/mac-os-eol.txt" latin1
+ [ 500 read ] with-input-stream
] unit-test
[
255
] [
- "core/io/test/binary.txt"
- [ read1 ] with-stream >fixnum
+ "resource:core/io/test/binary.txt" latin1
+ [ read1 ] with-input-stream >fixnum
] unit-test
! Make sure we use correct to_c_string form when writing
@@ -36,11 +33,12 @@ IN: io.tests
}
] [
[
- "core/io/test/separator-test.txt" [
+ "resource:core/io/test/separator-test.txt"
+ latin1 [
"J" read-until 2array ,
"i" read-until 2array ,
"X" read-until 2array ,
- ] with-stream
+ ] with-input-stream
] { } make
] unit-test
@@ -49,12 +47,3 @@ IN: io.tests
10 [ 65536 read drop ] times
] with-file-reader
] unit-test
-
-! [ "" ] [ 0 read ] unit-test
-
-! [ ] [ "123" write 9000 CHAR: x write flush ] unit-test
-
-! [
-! "/core/io/test/binary.txt"
-! [ 0.2 read ] with-stream
-! ] must-fail
diff --git a/core/io/io.factor b/core/io/io.factor
index ef9eae7902..e28fd28fb3 100755
--- a/core/io/io.factor
+++ b/core/io/io.factor
@@ -30,39 +30,52 @@ GENERIC: stream-write-table ( table-cells style stream -- )
[ 2dup (stream-copy) ] [ dispose dispose ] [ ]
cleanup ;
-! Default stream
-SYMBOL: stdio
+! Default streams
+SYMBOL: input-stream
+SYMBOL: output-stream
+SYMBOL: error-stream
-! Default error stream
-SYMBOL: stderr
+: readln ( -- str/f ) input-stream get stream-readln ;
+: read1 ( -- ch/f ) input-stream get stream-read1 ;
+: read ( n -- str/f ) input-stream get stream-read ;
+: read-until ( seps -- str/f sep/f ) input-stream get stream-read-until ;
-: readln ( -- str/f ) stdio get stream-readln ;
-: read1 ( -- ch/f ) stdio get stream-read1 ;
-: read ( n -- str/f ) stdio get stream-read ;
-: read-until ( seps -- str/f sep/f ) stdio get stream-read-until ;
+: write1 ( ch -- ) output-stream get stream-write1 ;
+: write ( str -- ) output-stream get stream-write ;
+: flush ( -- ) output-stream get stream-flush ;
-: write1 ( ch -- ) stdio get stream-write1 ;
-: write ( str -- ) stdio get stream-write ;
-: flush ( -- ) stdio get stream-flush ;
+: nl ( -- ) output-stream get stream-nl ;
+: format ( str style -- ) output-stream get stream-format ;
-: nl ( -- ) stdio get stream-nl ;
-: format ( str style -- ) stdio get stream-format ;
+: with-input-stream* ( stream quot -- )
+ input-stream swap with-variable ; inline
-: with-stream* ( stream quot -- )
- stdio swap with-variable ; inline
+: with-input-stream ( stream quot -- )
+ [ with-input-stream* ] curry with-disposal ; inline
-: with-stream ( stream quot -- )
- [ with-stream* ] curry with-disposal ; inline
+: with-output-stream* ( stream quot -- )
+ output-stream swap with-variable ; inline
+
+: with-output-stream ( stream quot -- )
+ [ with-output-stream* ] curry with-disposal ; inline
+
+: with-streams* ( input output quot -- )
+ [ output-stream set input-stream set ] prepose with-scope ; inline
+
+: with-streams ( input output quot -- )
+ [ [ with-streams* ] 3curry ]
+ [ [ drop dispose dispose ] 3curry ] 3bi
+ [ ] cleanup ; inline
: tabular-output ( style quot -- )
- swap >r { } make r> stdio get stream-write-table ; inline
+ swap >r { } make r> output-stream get stream-write-table ; inline
: with-row ( quot -- )
{ } make , ; inline
: with-cell ( quot -- )
- H{ } stdio get make-cell-stream
- [ swap with-stream ] keep , ; inline
+ H{ } output-stream get make-cell-stream
+ [ swap with-output-stream ] keep , ; inline
: write-cell ( str -- )
[ write ] with-cell ; inline
@@ -71,13 +84,14 @@ SYMBOL: stderr
swap dup assoc-empty? [
drop call
] [
- stdio get make-span-stream swap with-stream
+ output-stream get make-span-stream swap with-output-stream
] if ; inline
: with-nesting ( style quot -- )
- >r stdio get make-block-stream r> with-stream ; inline
+ >r output-stream get make-block-stream
+ r> with-output-stream ; inline
-: print ( string -- ) stdio get stream-print ;
+: print ( string -- ) output-stream get stream-print ;
: bl ( -- ) " " write ;
@@ -85,9 +99,9 @@ SYMBOL: stderr
presented associate format ;
: lines ( stream -- seq )
- [ [ readln dup ] [ ] [ drop ] unfold ] with-stream ;
+ [ [ readln dup ] [ ] [ drop ] unfold ] with-input-stream ;
: contents ( stream -- str )
[
[ 65536 read dup ] [ ] [ drop ] unfold concat f like
- ] with-stream ;
+ ] with-input-stream ;
diff --git a/core/io/streams/byte-array/byte-array-docs.factor b/core/io/streams/byte-array/byte-array-docs.factor
index 741725af41..7b27621343 100644
--- a/core/io/streams/byte-array/byte-array-docs.factor
+++ b/core/io/streams/byte-array/byte-array-docs.factor
@@ -25,10 +25,10 @@ HELP:
HELP: with-byte-reader
{ $values { "encoding" "an encoding descriptor" }
{ "quot" quotation } { "byte-array" byte-array } }
-{ $description "Calls the quotation in a new dynamic scope with " { $link stdio } " rebound to an input stream for reading from a byte array using an encoding." } ;
+{ $description "Calls the quotation in a new dynamic scope with " { $link input-stream } " rebound to an input stream for reading from a byte array using an encoding." } ;
HELP: with-byte-writer
{ $values { "encoding" "an encoding descriptor" }
{ "quot" quotation }
{ "byte-array" byte-array } }
-{ $description "Calls the quotation in a new dynamic scope with " { $link stdio } " rebound to an output stream writing data to a byte array using an encoding." } ;
+{ $description "Calls the quotation in a new dynamic scope with " { $link output-stream } " rebound to an output stream writing data to a byte array using an encoding." } ;
diff --git a/core/io/streams/byte-array/byte-array.factor b/core/io/streams/byte-array/byte-array.factor
index 2a8441ff23..28d789d66f 100644
--- a/core/io/streams/byte-array/byte-array.factor
+++ b/core/io/streams/byte-array/byte-array.factor
@@ -1,16 +1,16 @@
USING: byte-arrays byte-vectors kernel io.encodings io.streams.string
-sequences io namespaces io.encodings.private ;
+sequences io namespaces io.encodings.private accessors ;
IN: io.streams.byte-array
: ( encoding -- stream )
512 swap ;
: with-byte-writer ( encoding quot -- byte-array )
- >r r> [ stdio get ] compose with-stream*
- dup encoder? [ encoder-stream ] when >byte-array ; inline
+ >r r> [ output-stream get ] compose with-output-stream*
+ dup encoder? [ stream>> ] when >byte-array ; inline
: ( byte-array encoding -- stream )
>r >byte-vector dup reverse-here r> ;
: with-byte-reader ( byte-array encoding quot -- )
- >r r> with-stream ; inline
+ >r r> with-input-stream* ; inline
diff --git a/core/io/streams/c/c.factor b/core/io/streams/c/c.factor
index 372acbe0c1..91732f3211 100755
--- a/core/io/streams/c/c.factor
+++ b/core/io/streams/c/c.factor
@@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license.
USING: kernel kernel.private namespaces io io.encodings
sequences math generic threads.private classes io.backend
-io.streams.duplex io.files continuations byte-arrays ;
+io.files continuations byte-arrays ;
IN: io.streams.c
TUPLE: c-writer handle ;
diff --git a/core/io/streams/duplex/duplex-docs.factor b/core/io/streams/duplex/duplex-docs.factor
deleted file mode 100755
index c9691af5ba..0000000000
--- a/core/io/streams/duplex/duplex-docs.factor
+++ /dev/null
@@ -1,19 +0,0 @@
-USING: help.markup help.syntax io continuations ;
-IN: io.streams.duplex
-
-ARTICLE: "io.streams.duplex" "Duplex streams"
-"Duplex streams combine an input stream and an output stream into a bidirectional stream."
-{ $subsection duplex-stream }
-{ $subsection } ;
-
-ABOUT: "io.streams.duplex"
-
-HELP: duplex-stream
-{ $class-description "A bidirectional stream wrapping an input and output stream." } ;
-
-HELP:
-{ $values { "in" "an input stream" } { "out" "an output stream" } { "stream" " a duplex stream" } }
-{ $description "Creates a duplex stream. Writing to a duplex stream will write to " { $snippet "out" } ", and reading from a duplex stream will read from " { $snippet "in" } ". Closing a duplex stream closes both the input and output streams." } ;
-
-HELP: stream-closed-twice
-{ $error-description "This error is thrown when performing an I/O operation on a " { $link duplex-stream } " which has been closed with " { $link dispose } "." } ;
diff --git a/core/io/streams/string/string-docs.factor b/core/io/streams/string/string-docs.factor
index 5b09baa56d..b87e5ca591 100644
--- a/core/io/streams/string/string-docs.factor
+++ b/core/io/streams/string/string-docs.factor
@@ -17,7 +17,7 @@ HELP:
HELP: with-string-writer
{ $values { "quot" quotation } { "str" string } }
-{ $description "Calls the quotation in a new dynamic scope with " { $link stdio } " rebound to a new string writer. The accumulated string is output when the quotation returns." } ;
+{ $description "Calls the quotation in a new dynamic scope with " { $link output-stream } " rebound to a new string writer. The accumulated string is output when the quotation returns." } ;
HELP:
{ $values { "str" string } { "stream" "an input stream" } }
@@ -26,4 +26,4 @@ HELP:
HELP: with-string-reader
{ $values { "str" string } { "quot" quotation } }
-{ $description "Calls the quotation in a new dynamic scope with " { $link stdio } " rebound to an input stream reading " { $snippet "str" } " from beginning to end." } ;
+{ $description "Calls the quotation in a new dynamic scope with " { $link input-stream } " rebound to an input stream reading " { $snippet "str" } " from beginning to end." } ;
diff --git a/core/io/streams/string/string-tests.factor b/core/io/streams/string/string-tests.factor
index ca117534da..3512ac871d 100644
--- a/core/io/streams/string/string-tests.factor
+++ b/core/io/streams/string/string-tests.factor
@@ -35,7 +35,7 @@ unit-test
"J" read-until 2array ,
"i" read-until 2array ,
"X" read-until 2array ,
- ] with-stream
+ ] with-input-stream
] { } make
] unit-test
diff --git a/core/io/streams/string/string.factor b/core/io/streams/string/string.factor
index 531d0401b2..d43599776b 100755
--- a/core/io/streams/string/string.factor
+++ b/core/io/streams/string/string.factor
@@ -15,7 +15,7 @@ M: growable stream-flush drop ;
512 ;
: with-string-writer ( quot -- str )
- swap [ stdio get ] compose with-stream*
+ swap [ output-stream get ] compose with-output-stream*
>string ; inline
M: growable stream-read1 dup empty? [ drop f ] [ pop ] if ;
@@ -56,7 +56,7 @@ M: null decode-char drop stream-read1 ;
>sbuf dup reverse-here null ;
: with-string-reader ( str quot -- )
- >r r> with-stream ; inline
+ >r r> with-input-stream ; inline
INSTANCE: growable plain-writer
@@ -67,15 +67,14 @@ INSTANCE: growable plain-writer
] unless ;
: map-last ( seq quot -- seq )
- swap dup length
- [ zero? rot [ call ] keep swap ] 2map nip ; inline
+ >r dup length [ zero? ] r> compose 2map ; inline
: format-table ( table -- seq )
flip [ format-column ] map-last
flip [ " " join ] map ;
M: plain-writer stream-write-table
- [ drop format-table [ print ] each ] with-stream* ;
+ [ drop format-table [ print ] each ] with-output-stream* ;
M: plain-writer make-cell-stream 2drop ;
diff --git a/core/listener/listener-docs.factor b/core/listener/listener-docs.factor
index 755c79ac68..beea9005b4 100755
--- a/core/listener/listener-docs.factor
+++ b/core/listener/listener-docs.factor
@@ -32,14 +32,14 @@ HELP: listener-hook
HELP: read-quot
{ $values { "quot/f" "a parsed quotation, or " { $link f } " indicating end of file" } }
-{ $description "Reads a Factor expression which possibly spans more than one line from " { $link stdio } " stream. Additional lines of input are read while the parser stack height is greater than one. Since structural parsing words push partial quotations on the stack, this will keep on reading input until all delimited parsing words are terminated." } ;
+{ $description "Reads a Factor expression which possibly spans more than one line from " { $link input-stream } ". Additional lines of input are read while the parser stack height is greater than one. Since structural parsing words push partial quotations on the stack, this will keep on reading input until all delimited parsing words are terminated." } ;
HELP: listen
-{ $description "Prompts for an expression on the " { $link stdio } " stream and evaluates it. On end of file, " { $link quit-flag } " is set to terminate the listener loop." }
-{ $errors "If the expression input by the user throws an error, the error is printed to the " { $link stdio } " stream and the word returns normally." } ;
+{ $description "Prompts for an expression on " { $link input-stream } " and evaluates it. On end of file, " { $link quit-flag } " is set to terminate the listener loop." }
+{ $errors "If the expression input by the user throws an error, the error is printed to " { $link output-stream } " and the word returns normally." } ;
HELP: listener
-{ $description "Prompts for expressions on the " { $link stdio } " stream and evaluates them until end of file is reached." } ;
+{ $description "Prompts for expressions on " { $link input-stream } " and evaluates them until end of file is reached." } ;
HELP: bye
{ $description "Exits the current listener." }
diff --git a/core/listener/listener-tests.factor b/core/listener/listener-tests.factor
index 2c05c049a7..24449049e0 100755
--- a/core/listener/listener-tests.factor
+++ b/core/listener/listener-tests.factor
@@ -51,6 +51,6 @@ IN: listener.tests
[
[ ] [
"IN: listener.tests : hello\n\"world\" ;" parse-interactive
- drop
+ drop
] unit-test
] with-file-vocabs
diff --git a/core/listener/listener.factor b/core/listener/listener.factor
index ddb29bb768..e00e64f4bc 100755
--- a/core/listener/listener.factor
+++ b/core/listener/listener.factor
@@ -2,8 +2,8 @@
! See http://factorcode.org/license.txt for BSD license.
USING: arrays hashtables io kernel math math.parser memory
namespaces parser sequences strings io.styles
-io.streams.duplex vectors words generic system combinators
-continuations debugger definitions compiler.units accessors ;
+vectors words generic system combinators continuations debugger
+definitions compiler.units accessors ;
IN: listener
SYMBOL: quit-flag
@@ -35,10 +35,7 @@ GENERIC: stream-read-quot ( stream -- quot/f )
M: object stream-read-quot
V{ } clone read-quot-loop ;
-M: duplex-stream stream-read-quot
- duplex-stream-in stream-read-quot ;
-
-: read-quot ( -- quot/f ) stdio get stream-read-quot ;
+: read-quot ( -- quot/f ) input-stream get stream-read-quot ;
: bye ( -- ) quit-flag on ;
@@ -46,9 +43,13 @@ M: duplex-stream stream-read-quot
"( " in get " )" 3append
H{ { background { 1 0.7 0.7 1 } } } format bl flush ;
+SYMBOL: error-hook
+
+[ print-error-and-restarts ] error-hook set-global
+
: listen ( -- )
listener-hook get call prompt.
- [ read-quot [ try ] [ bye ] if* ]
+ [ read-quot [ [ error-hook get call ] recover ] [ bye ] if* ]
[
dup parse-error? [
error-hook get call
diff --git a/core/math/math.factor b/core/math/math.factor
index d5040757d4..0218ded6ff 100755
--- a/core/math/math.factor
+++ b/core/math/math.factor
@@ -80,9 +80,6 @@ M: number equal? number= ;
M: real hashcode* nip >fixnum ;
-! real and sequence overlap. we disambiguate:
-M: integer hashcode* nip >fixnum ;
-
GENERIC: fp-nan? ( x -- ? )
M: object fp-nan?
diff --git a/core/math/order/order-docs.factor b/core/math/order/order-docs.factor
index 98ff1920fa..23ea1058ad 100644
--- a/core/math/order/order-docs.factor
+++ b/core/math/order/order-docs.factor
@@ -25,8 +25,8 @@ HELP: +gt+
{ $description "Returned by " { $link <=> } " when the first object is strictly greater than the second object." } ;
HELP: invert-comparison
-{ $values { "symbol" "a comparison symbol, +lt+, +eq+, or +gt+" }
- { "new-symbol" "a comparison symbol, +lt+, +eq+, or +gt+" } }
+{ $values { "symbol" symbol }
+ { "new-symbol" symbol } }
{ $description "Invert the comparison symbol returned by " { $link <=> } ". The output for the symbol " { $snippet "+eq+" } " is itself." }
{ $examples
{ $example "USING: math.order prettyprint ;" "+lt+ invert-comparison ." "+gt+" } } ;
diff --git a/core/math/order/order.factor b/core/math/order/order.factor
index 7cbef68dcc..76fe058ffa 100644
--- a/core/math/order/order.factor
+++ b/core/math/order/order.factor
@@ -7,17 +7,13 @@ SYMBOL: +lt+
SYMBOL: +eq+
SYMBOL: +gt+
-GENERIC: <=> ( obj1 obj2 -- symbol )
-
-: (<=>) ( a b -- symbol )
- 2dup < [ 2drop +lt+ ] [ number= +eq+ +gt+ ? ] if ; inline
-
: invert-comparison ( symbol -- new-symbol )
#! Can't use case, index or nth here
dup +lt+ eq? [ drop +gt+ ] [ +eq+ eq? +eq+ +lt+ ? ] if ;
-M: real <=> (<=>) ;
-M: integer <=> (<=>) ;
+GENERIC: <=> ( obj1 obj2 -- symbol )
+
+M: real <=> 2dup < [ 2drop +lt+ ] [ number= +eq+ +gt+ ? ] if ;
GENERIC: before? ( obj1 obj2 -- ? )
GENERIC: after? ( obj1 obj2 -- ? )
diff --git a/core/math/parser/parser-tests.factor b/core/math/parser/parser-tests.factor
index baa6634a9f..15234ee310 100755
--- a/core/math/parser/parser-tests.factor
+++ b/core/math/parser/parser-tests.factor
@@ -98,3 +98,9 @@ unit-test
[ 1 1 >base ] must-fail
[ 1 0 >base ] must-fail
[ 1 -1 >base ] must-fail
+
+[ "0.0/0.0" ] [ 0.0 0.0 / number>string ] unit-test
+
+[ "1.0/0.0" ] [ 1.0 0.0 / number>string ] unit-test
+
+[ "-1.0/0.0" ] [ -1.0 0.0 / number>string ] unit-test
diff --git a/core/math/parser/parser.factor b/core/math/parser/parser.factor
index 1a1a080564..d1b8e6fd37 100755
--- a/core/math/parser/parser.factor
+++ b/core/math/parser/parser.factor
@@ -140,9 +140,9 @@ M: ratio >base
M: float >base
drop {
+ { [ dup fp-nan? ] [ drop "0.0/0.0" ] }
{ [ dup 1.0/0.0 = ] [ drop "1.0/0.0" ] }
{ [ dup -1.0/0.0 = ] [ drop "-1.0/0.0" ] }
- { [ dup fp-nan? ] [ drop "0.0/0.0" ] }
[ float>string fix-float ]
} cond ;
diff --git a/core/optimizer/control/control.factor b/core/optimizer/control/control.factor
index de7aec2bb1..7ab0ffc806 100755
--- a/core/optimizer/control/control.factor
+++ b/core/optimizer/control/control.factor
@@ -154,9 +154,9 @@ SYMBOL: potential-loops
node-literal t
] [
node-class {
- { [ dup null class< ] [ drop f f ] }
- { [ dup \ f class-not class< ] [ drop t t ] }
- { [ dup \ f class< ] [ drop f t ] }
+ { [ dup null class<= ] [ drop f f ] }
+ { [ dup \ f class-not class<= ] [ drop t t ] }
+ { [ dup \ f class<= ] [ drop f t ] }
[ drop f f ]
} cond
] if ;
diff --git a/core/optimizer/def-use/def-use-tests.factor b/core/optimizer/def-use/def-use-tests.factor
index 914018437a..f49ab7fcba 100755
--- a/core/optimizer/def-use/def-use-tests.factor
+++ b/core/optimizer/def-use/def-use-tests.factor
@@ -1,6 +1,6 @@
IN: optimizer.def-use.tests
USING: inference inference.dataflow optimizer optimizer.def-use
-namespaces assocs kernel sequences math tools.test words ;
+namespaces assocs kernel sequences math tools.test words sets ;
[ 3 { 1 1 1 } ] [
[ 1 2 3 ] dataflow compute-def-use drop
@@ -11,10 +11,6 @@ namespaces assocs kernel sequences math tools.test words ;
dataflow compute-def-use drop compute-dead-literals keys
[ value-literal ] map ;
-: subset? [ member? ] curry all? ;
-
-: set= 2dup subset? >r swap subset? r> and ;
-
[ { [ + ] } ] [
[ [ 1 2 3 ] [ + ] over drop drop ] kill-set
] unit-test
diff --git a/core/optimizer/inlining/inlining.factor b/core/optimizer/inlining/inlining.factor
index 33c8244b4c..393264e459 100755
--- a/core/optimizer/inlining/inlining.factor
+++ b/core/optimizer/inlining/inlining.factor
@@ -77,7 +77,7 @@ DEFER: (flat-length)
float real
complex number
object
- } [ class< ] with find nip ;
+ } [ class<= ] with find nip ;
: inlining-math-method ( #call word -- quot/f )
swap node-input-classes
@@ -111,7 +111,7 @@ DEFER: (flat-length)
: comparable? ( actual testing -- ? )
#! If actual is a subset of testing or if the two classes
#! are disjoint, return t.
- 2dup class< >r classes-intersect? not r> or ;
+ 2dup class<= >r classes-intersect? not r> or ;
: optimize-predicate? ( #call -- ? )
dup node-param "predicating" word-prop dup [
@@ -132,7 +132,7 @@ DEFER: (flat-length)
: evaluate-predicate ( #call -- ? )
dup node-param "predicating" word-prop >r
- node-class-first r> class< ;
+ node-class-first r> class<= ;
: optimize-predicate ( #call -- node )
#! If the predicate is followed by a branch we fold it
diff --git a/core/optimizer/known-words/known-words.factor b/core/optimizer/known-words/known-words.factor
index 6e1aacff44..d1dbefe26b 100755
--- a/core/optimizer/known-words/known-words.factor
+++ b/core/optimizer/known-words/known-words.factor
@@ -4,7 +4,7 @@ IN: optimizer.known-words
USING: alien arrays generic hashtables inference.dataflow
inference.class kernel assocs math math.private kernel.private
sequences words parser vectors strings sbufs io namespaces
-assocs quotations sequences.private io.binary io.crc32
+assocs quotations sequences.private io.binary
io.streams.string layouts splitting math.intervals
math.floats.private classes.tuple classes.tuple.private classes
classes.algebra optimizer.def-use optimizer.backend
@@ -126,8 +126,6 @@ sequences.private combinators ;
\ >sbuf { string } "specializer" set-word-prop
-\ crc32 { string } "specializer" set-word-prop
-
\ split, { string string } "specializer" set-word-prop
\ memq? { array } "specializer" set-word-prop
diff --git a/core/optimizer/math/math.factor b/core/optimizer/math/math.factor
index ab8a1f3eda..72e64d5b95 100755
--- a/core/optimizer/math/math.factor
+++ b/core/optimizer/math/math.factor
@@ -96,7 +96,7 @@ optimizer.math.partial generic.standard system accessors ;
: math-closure ( class -- newclass )
{ null fixnum bignum integer rational float real number }
- [ class< ] with find nip number or ;
+ [ class<= ] with find nip number or ;
: fits? ( interval class -- ? )
"interval" word-prop dup
@@ -108,7 +108,7 @@ optimizer.math.partial generic.standard system accessors ;
dup r> at swap or ;
: won't-overflow? ( interval node -- ? )
- node-in-d [ value-class* fixnum class< ] all?
+ node-in-d [ value-class* fixnum class<= ] all?
swap fixnum fits? and ;
: post-process ( class interval node -- classes intervals )
@@ -214,7 +214,7 @@ optimizer.math.partial generic.standard system accessors ;
: twiddle-interval ( i1 -- i2 )
dup [
node get node-in-d
- [ value-class* integer class< ] all?
+ [ value-class* integer class<= ] all?
[ integral-closure ] when
] when ;
@@ -293,7 +293,7 @@ most-negative-fixnum most-positive-fixnum [a,b]
! Removing overflow checks
: remove-overflow-check? ( #call -- ? )
dup out-d>> first node-class
- [ fixnum class< ] [ null eq? not ] bi and ;
+ [ fixnum class<= ] [ null eq? not ] bi and ;
{
{ + [ fixnum+fast ] }
@@ -356,7 +356,7 @@ most-negative-fixnum most-positive-fixnum [a,b]
dup #call? [ node-param eq? ] [ 2drop f ] if ;
: coerced-to-fixnum? ( #call -- ? )
- dup dup node-in-d [ node-class integer class< ] with all?
+ dup dup node-in-d [ node-class integer class<= ] with all?
[ \ >fixnum consumed-by? ] [ drop f ] if ;
{
@@ -377,7 +377,7 @@ most-negative-fixnum most-positive-fixnum [a,b]
: convert-rem-to-and? ( #call -- ? )
dup node-in-d {
- { [ 2dup first node-class integer class< not ] [ f ] }
+ { [ 2dup first node-class integer class<= not ] [ f ] }
{ [ 2dup second node-literal integer? not ] [ f ] }
{ [ 2dup second node-literal power-of-2? not ] [ f ] }
[ t ]
diff --git a/core/optimizer/pattern-match/pattern-match.factor b/core/optimizer/pattern-match/pattern-match.factor
index 5beb2555f0..51fa254a25 100755
--- a/core/optimizer/pattern-match/pattern-match.factor
+++ b/core/optimizer/pattern-match/pattern-match.factor
@@ -12,7 +12,7 @@ SYMBOL: @
@ get [ eq? ] [ @ set t ] if* ;
: match-class ( value spec -- ? )
- >r node get swap node-class r> class< ;
+ >r node get swap node-class r> class<= ;
: value-match? ( value spec -- ? )
{
diff --git a/core/parser/parser-docs.factor b/core/parser/parser-docs.factor
index b69985fb1d..418278baee 100755
--- a/core/parser/parser-docs.factor
+++ b/core/parser/parser-docs.factor
@@ -5,7 +5,7 @@ quotations namespaces compiler.units assocs ;
IN: parser
ARTICLE: "vocabulary-search-shadow" "Shadowing word names"
-"If adding a vocabulary to the search path results in a word in another vocabulary becoming inaccessible due to the new vocabulary defining a word with the same name, a message is printed to the " { $link stdio } " stream. Except when debugging suspected name clashes, these messages can be ignored."
+"If adding a vocabulary to the search path results in a word in another vocabulary becoming inaccessible due to the new vocabulary defining a word with the same name, we say that the old word has been " { $emphasis "shadowed" } "."
$nl
"Here is an example where shadowing occurs:"
{ $code
@@ -13,18 +13,18 @@ $nl
"USING: sequences io ;"
""
": append"
- " \"foe::append calls sequences::append\" print append ;"
+ " \"foe::append calls sequences:append\" print append ;"
""
"IN: fee"
""
": append"
- " \"fee::append calls fee::append\" print append ;"
+ " \"fee::append calls fee:append\" print append ;"
""
"IN: fox"
"USE: foe"
""
": append"
- " \"fox::append calls foe::append\" print append ;"
+ " \"fox::append calls foe:append\" print append ;"
""
"\"1234\" \"5678\" append print"
""
@@ -33,12 +33,13 @@ $nl
}
"When placed in a source file and run, the above code produces the following output:"
{ $code
- "foe::append calls sequences::append"
+ "foe:append calls sequences:append"
"12345678"
- "fee::append calls foe::append"
- "foe::append calls sequences::append"
+ "fee:append calls foe:append"
+ "foe:append calls sequences:append"
"12345678"
-} ;
+}
+"The " { $vocab-link "qualified" } " vocabulary contains some tools for helping with shadowing." ;
ARTICLE: "vocabulary-search-errors" "Word lookup errors"
"If the parser cannot not find a word in the current vocabulary search path, it attempts to look for the word in all loaded vocabularies. Then, one of three things happen:"
@@ -215,7 +216,7 @@ HELP: save-location
{ $description "Saves the location of a definition and associates this definition with the current source file." } ;
HELP: parser-notes
-{ $var-description "A boolean controlling whether the parser will print various notes and warnings. Switched on by default. If a source file is being run for its effect on the " { $link stdio } " stream, this variable should be switched off, to prevent parser notes from polluting the output." } ;
+{ $var-description "A boolean controlling whether the parser will print various notes and warnings. Switched on by default. If a source file is being run for its effect on " { $link output-stream } ", this variable should be switched off, to prevent parser notes from polluting the output." } ;
HELP: parser-notes?
{ $values { "?" "a boolean" } }
@@ -506,7 +507,7 @@ HELP: bootstrap-file
HELP: eval>string
{ $values { "str" string } { "output" string } }
-{ $description "Evaluates the Factor code in " { $snippet "str" } " with the " { $link stdio } " stream rebound to a string output stream, then outputs the resulting string." } ;
+{ $description "Evaluates the Factor code in " { $snippet "str" } " with " { $link output-stream } " rebound to a string output stream, then outputs the resulting string." } ;
HELP: staging-violation
{ $values { "word" word } }
diff --git a/core/parser/parser-tests.factor b/core/parser/parser-tests.factor
index 20d51f3461..9c3c1d9f6c 100755
--- a/core/parser/parser-tests.factor
+++ b/core/parser/parser-tests.factor
@@ -432,3 +432,6 @@ must-fail-with
] must-fail
[ ": foo ;" eval ] [ error>> no-current-vocab? ] must-fail-with
+
+[ 92 ] [ "CHAR: \\" eval ] unit-test
+[ 92 ] [ "CHAR: \\\\" eval ] unit-test
diff --git a/core/parser/parser.factor b/core/parser/parser.factor
index 23c0c0a1a5..76c831cf13 100755
--- a/core/parser/parser.factor
+++ b/core/parser/parser.factor
@@ -421,14 +421,17 @@ ERROR: bad-number ;
SYMBOL: current-class
SYMBOL: current-generic
-: (M:)
- CREATE-METHOD
+: with-method-definition ( quot -- parsed )
[
+ >r
[ "method-class" word-prop current-class set ]
[ "method-generic" word-prop current-generic set ]
[ ] tri
- parse-definition
- ] with-scope ;
+ r> call
+ ] with-scope ; inline
+
+: (M:)
+ CREATE-METHOD [ parse-definition ] with-method-definition ;
: scan-object ( -- object )
scan-word dup parsing?
diff --git a/core/prettyprint/backend/backend.factor b/core/prettyprint/backend/backend.factor
index e13a991e2b..f992b9ca01 100755
--- a/core/prettyprint/backend/backend.factor
+++ b/core/prettyprint/backend/backend.factor
@@ -1,10 +1,11 @@
! Copyright (C) 2003, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: arrays byte-arrays bit-arrays generic hashtables io
-assocs kernel math namespaces sequences strings sbufs io.styles
-vectors words prettyprint.config prettyprint.sections quotations
-io io.files math.parser effects classes.tuple math.order
-classes.tuple.private classes float-arrays ;
+USING: arrays byte-arrays byte-vectors bit-arrays generic
+hashtables io assocs kernel math namespaces sequences strings
+sbufs io.styles vectors words prettyprint.config
+prettyprint.sections quotations io io.files math.parser effects
+classes.tuple math.order classes.tuple.private classes
+float-arrays ;
IN: prettyprint.backend
GENERIC: pprint* ( obj -- )
@@ -140,6 +141,7 @@ M: compose pprint-delims drop \ [ \ ] ;
M: array pprint-delims drop \ { \ } ;
M: byte-array pprint-delims drop \ B{ \ } ;
M: bit-array pprint-delims drop \ ?{ \ } ;
+M: byte-vector pprint-delims drop \ BV{ \ } ;
M: float-array pprint-delims drop \ F{ \ } ;
M: vector pprint-delims drop \ V{ \ } ;
M: hashtable pprint-delims drop \ H{ \ } ;
@@ -152,6 +154,7 @@ GENERIC: >pprint-sequence ( obj -- seq )
M: object >pprint-sequence ;
M: vector >pprint-sequence ;
+M: byte-vector >pprint-sequence ;
M: curry >pprint-sequence ;
M: compose >pprint-sequence ;
M: hashtable >pprint-sequence >alist ;
diff --git a/core/prettyprint/prettyprint-docs.factor b/core/prettyprint/prettyprint-docs.factor
index 2933c8ee6f..f7f0f7ee44 100755
--- a/core/prettyprint/prettyprint-docs.factor
+++ b/core/prettyprint/prettyprint-docs.factor
@@ -135,7 +135,7 @@ ARTICLE: "prettyprint" "The prettyprinter"
$nl
"Prettyprinter words are found in the " { $vocab-link "prettyprint" } " vocabulary."
$nl
-"The key words to print an object to the " { $link stdio } " stream; the first two emit a trailing newline, the second two do not:"
+"The key words to print an object to " { $link output-stream } "; the first two emit a trailing newline, the second two do not:"
{ $subsection . }
{ $subsection short. }
{ $subsection pprint }
@@ -161,17 +161,17 @@ ABOUT: "prettyprint"
HELP: with-pprint
{ $values { "obj" object } { "quot" quotation } }
-{ $description "Sets up the prettyprinter and calls the quotation in a new scope. The quotation should add sections to the top-level block. When the quotation returns, the top-level block is printed to the " { $link stdio } " stream." } ;
+{ $description "Sets up the prettyprinter and calls the quotation in a new scope. The quotation should add sections to the top-level block. When the quotation returns, the top-level block is printed to " { $link output-stream } "." } ;
HELP: pprint
{ $values { "obj" object } }
-{ $description "Prettyprints an object to the " { $link stdio } " stream. Output is influenced by many variables; see " { $link "prettyprint-variables" } "." } ;
+{ $description "Prettyprints an object to " { $link output-stream } ". Output is influenced by many variables; see " { $link "prettyprint-variables" } "." } ;
{ pprint pprint* with-pprint } related-words
HELP: .
{ $values { "obj" object } }
-{ $description "Prettyprints an object to the " { $link stdio } " stream with a trailing line break. Output is influenced by many variables; see " { $link "prettyprint-variables" } "." } ;
+{ $description "Prettyprints an object to " { $link output-stream } " with a trailing line break. Output is influenced by many variables; see " { $link "prettyprint-variables" } "." } ;
HELP: unparse
{ $values { "obj" object } { "str" "Factor source string" } }
@@ -179,11 +179,11 @@ HELP: unparse
HELP: pprint-short
{ $values { "obj" object } }
-{ $description "Prettyprints an object to the " { $link stdio } " stream. This word rebinds printer control variables to enforce ``shorter'' output. See " { $link "prettyprint-variables" } "." } ;
+{ $description "Prettyprints an object to " { $link output-stream } ". This word rebinds printer control variables to enforce ``shorter'' output. See " { $link "prettyprint-variables" } "." } ;
HELP: short.
{ $values { "obj" object } }
-{ $description "Prettyprints an object to the " { $link stdio } " stream with a trailing line break. This word rebinds printer control variables to enforce ``shorter'' output." } ;
+{ $description "Prettyprints an object to " { $link output-stream } " with a trailing line break. This word rebinds printer control variables to enforce ``shorter'' output." } ;
HELP: .b
{ $values { "n" "an integer" } }
diff --git a/core/prettyprint/prettyprint-tests.factor b/core/prettyprint/prettyprint-tests.factor
index e94670992c..0faae398e9 100755
--- a/core/prettyprint/prettyprint-tests.factor
+++ b/core/prettyprint/prettyprint-tests.factor
@@ -114,7 +114,7 @@ unit-test
[ parse-fresh drop ] with-compilation-unit
[
"prettyprint.tests" lookup see
- ] with-string-writer "\n" split 1 head*
+ ] with-string-writer "\n" split but-last
] keep =
] with-scope ;
diff --git a/core/prettyprint/sections/sections-docs.factor b/core/prettyprint/sections/sections-docs.factor
index ceb37c2fe4..842a36a13b 100755
--- a/core/prettyprint/sections/sections-docs.factor
+++ b/core/prettyprint/sections/sections-docs.factor
@@ -15,7 +15,7 @@ HELP: line-limit?
HELP: do-indent
-{ $description "Outputs the current indent nesting to the " { $link stdio } " stream." } ;
+{ $description "Outputs the current indent nesting to " { $link output-stream } "." } ;
HELP: fresh-line
{ $values { "n" "the current column position" } }
diff --git a/core/prettyprint/sections/sections.factor b/core/prettyprint/sections/sections.factor
index 5f32539115..11fa4da28e 100644
--- a/core/prettyprint/sections/sections.factor
+++ b/core/prettyprint/sections/sections.factor
@@ -15,9 +15,9 @@ SYMBOL: pprinter-stack
SYMBOL: pprinter-in
SYMBOL: pprinter-use
-TUPLE: pprinter last-newline line-count end-printing indent ;
+TUPLE: pprinter last-newline line-count indent ;
-: ( -- pprinter ) 0 1 f 0 pprinter boa ;
+: ( -- pprinter ) 0 1 0 pprinter boa ;
: record-vocab ( word -- )
word-vocabulary [ dup pprinter-use get set-at ] when* ;
@@ -34,7 +34,7 @@ TUPLE: pprinter last-newline line-count end-printing indent ;
] [
pprinter get (>>last-newline)
line-limit? [
- "..." write pprinter get end-printing>> continue
+ "..." write pprinter get return
] when
pprinter get [ 1+ ] change-line-count drop
nl do-indent
@@ -275,16 +275,15 @@ M: colon unindent-first-line? drop t ;
[
dup style>> [
[
- >r pprinter get (>>end-printing) r>
short-section
- ] curry callcc0
+ ] curry with-return
] with-nesting
] if-nonempty
] with-variable ;
! Long section layout algorithm
: chop-break ( seq -- seq )
- dup peek line-break? [ 1 head-slice* chop-break ] when ;
+ dup peek line-break? [ but-last-slice chop-break ] when ;
SYMBOL: prev
SYMBOL: next
diff --git a/core/sequences/sequences-docs.factor b/core/sequences/sequences-docs.factor
index 2a2fcf29cd..8b15f5b980 100755
--- a/core/sequences/sequences-docs.factor
+++ b/core/sequences/sequences-docs.factor
@@ -92,9 +92,11 @@ ARTICLE: "sequences-slices" "Subsequences and slices"
{ $subsection subseq }
{ $subsection head }
{ $subsection tail }
-{ $subsection rest }
{ $subsection head* }
{ $subsection tail* }
+"Removing the first or last element:"
+{ $subsection rest }
+{ $subsection but-last }
"Taking a sequence apart into a head and a tail:"
{ $subsection unclip }
{ $subsection cut }
@@ -106,6 +108,7 @@ ARTICLE: "sequences-slices" "Subsequences and slices"
{ $subsection }
{ $subsection head-slice }
{ $subsection tail-slice }
+{ $subsection but-last-slice }
{ $subsection rest-slice }
{ $subsection head-slice* }
{ $subsection tail-slice* }
@@ -836,11 +839,16 @@ HELP: tail-slice
{ $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
{ $errors "Throws an error if the index is out of bounds." } ;
+HELP: but-last-slice
+{ $values { "seq" sequence } { "slice" "a slice" } }
+{ $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
+{ $errors "Throws an error on an empty sequence." } ;
+
HELP: rest-slice
{ $values { "seq" sequence } { "slice" "a slice" } }
{ $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
{ $notes "Equivalent to " { $snippet "1 tail" } }
-{ $errors "Throws an error if the index is out of bounds." } ;
+{ $errors "Throws an error on an empty sequence." } ;
HELP: head-slice*
{ $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
@@ -862,6 +870,11 @@ HELP: tail
{ $description "Outputs a new sequence consisting of the input sequence with the first n items removed." }
{ $errors "Throws an error if the index is out of bounds." } ;
+HELP: but-last
+{ $values { "seq" sequence } { "headseq" "a new sequence" } }
+{ $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
+{ $errors "Throws an error on an empty sequence." } ;
+
HELP: rest
{ $values { "seq" sequence } { "tailseq" "a new sequence" } }
{ $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
diff --git a/core/sequences/sequences.factor b/core/sequences/sequences.factor
index a63e6d2835..8d0e3eec18 100755
--- a/core/sequences/sequences.factor
+++ b/core/sequences/sequences.factor
@@ -216,6 +216,8 @@ M: slice length dup slice-to swap slice-from - ;
: tail-slice* ( seq n -- slice ) from-end tail-slice ;
+: but-last-slice ( seq -- slice ) 1 head-slice* ;
+
INSTANCE: slice virtual-sequence
! One element repeated many times
@@ -263,6 +265,8 @@ PRIVATE>
: tail* ( seq n -- tailseq ) from-end tail ;
+: but-last ( seq -- headseq ) 1 head* ;
+
: copy ( src i dst -- )
pick length >r 3dup check-copy spin 0 r>
(copy) drop ; inline
@@ -670,9 +674,15 @@ PRIVATE>
: unclip ( seq -- rest first )
[ rest ] [ first ] bi ;
+: unclip-last ( seq -- butfirst last )
+ [ but-last ] [ peek ] bi ;
+
: unclip-slice ( seq -- rest first )
[ rest-slice ] [ first ] bi ;
+: unclip-last-slice ( seq -- butfirst last )
+ [ but-last-slice ] [ peek ] bi ;
+
: ( seq -- slice )
dup slice? [ { } like ] when 0 over length rot ;
inline
diff --git a/core/sets/sets-docs.factor b/core/sets/sets-docs.factor
index 55ef3ccddd..f4e2557a71 100644
--- a/core/sets/sets-docs.factor
+++ b/core/sets/sets-docs.factor
@@ -2,7 +2,7 @@ USING: kernel help.markup help.syntax sequences ;
IN: sets
ARTICLE: "sets" "Set-theoretic operations on sequences"
-"Set-theoretic operations on sequences are defined on the " { $vocab-link "sets" } " vocabulary. These operations use hashtables internally to achieve linear running time."
+"Set-theoretic operations on sequences are defined on the " { $vocab-link "sets" } " vocabulary. All of these operations use hashtables internally to achieve linear running time."
$nl
"Remove duplicates:"
{ $subsection prune }
@@ -12,8 +12,14 @@ $nl
{ $subsection diff }
{ $subsection intersect }
{ $subsection union }
+{ $subsection subset? }
+{ $subsection set= }
+"A word used to implement the above:"
+{ $subsection unique }
{ $see-also member? memq? contains? all? "assocs-sets" } ;
+ABOUT: "sets"
+
HELP: unique
{ $values { "seq" "a sequence" } { "assoc" "an assoc" } }
{ $description "Outputs a new assoc where the keys and values are equal." }
@@ -59,3 +65,11 @@ HELP: union
} ;
{ diff intersect union } related-words
+
+HELP: subset?
+{ $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
+{ $description "Tests if every element of " { $snippet "seq1" } " is contained in " { $snippet "seq2" } "." } ;
+
+HELP: set=
+{ $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
+{ $description "Tests if both sequences contain the same elements, disregrading order and duplicates." } ;
diff --git a/core/sets/sets.factor b/core/sets/sets.factor
index 78a92155fc..b0d26e0f30 100644
--- a/core/sets/sets.factor
+++ b/core/sets/sets.factor
@@ -29,3 +29,9 @@ IN: sets
: union ( seq1 seq2 -- newseq )
append prune ;
+
+: subset? ( seq1 seq2 -- ? )
+ unique [ key? ] curry all? ;
+
+: set= ( seq1 seq2 -- ? )
+ [ unique ] bi@ = ;
diff --git a/core/source-files/source-files.factor b/core/source-files/source-files.factor
index 5ef2d46790..36a1806e12 100755
--- a/core/source-files/source-files.factor
+++ b/core/source-files/source-files.factor
@@ -3,8 +3,8 @@
USING: arrays definitions generic assocs kernel math namespaces
prettyprint sequences strings vectors words quotations inspector
io.styles io combinators sorting splitting math.parser effects
-continuations debugger io.files io.crc32 vocabs hashtables
-graphs compiler.units io.encodings.utf8 accessors ;
+continuations debugger io.files checksums checksums.crc32 vocabs
+hashtables graphs compiler.units io.encodings.utf8 accessors ;
IN: source-files
SYMBOL: source-files
@@ -15,7 +15,7 @@ checksum
uses definitions ;
: record-checksum ( lines source-file -- )
- >r lines-crc32 r> set-source-file-checksum ;
+ >r crc32 checksum-lines r> set-source-file-checksum ;
: (xref-source) ( source-file -- pathname uses )
dup source-file-path
diff --git a/core/splitting/splitting.factor b/core/splitting/splitting.factor
index eb10b9fe4a..9f6ae75d32 100755
--- a/core/splitting/splitting.factor
+++ b/core/splitting/splitting.factor
@@ -1,42 +1,72 @@
-! Copyright (C) 2005, 2007 Slava Pestov.
+! Copyright (C) 2005, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel math namespaces strings arrays vectors sequences
-sets math.order ;
+sets math.order accessors ;
IN: splitting
-TUPLE: groups seq n sliced? ;
+TUPLE: abstract-groups seq n ;
-: check-groups 0 <= [ "Invalid group count" throw ] when ;
+: check-groups dup 0 <= [ "Invalid group count" throw ] when ; inline
+
+: construct-groups ( seq n class -- groups )
+ >r check-groups r> boa ; inline
+
+GENERIC: group@ ( n groups -- from to seq )
+
+M: abstract-groups nth group@ subseq ;
+
+M: abstract-groups set-nth group@ 0 swap copy ;
+
+M: abstract-groups like drop { } like ;
+
+INSTANCE: abstract-groups sequence
+
+TUPLE: groups < abstract-groups ;
: ( seq n -- groups )
- dup check-groups f groups boa ; inline
-
-: ( seq n -- groups )
- t over set-groups-sliced? ;
+ groups construct-groups ; inline
M: groups length
- dup groups-seq length swap groups-n [ + 1- ] keep /i ;
+ [ seq>> length ] [ n>> ] bi [ + 1- ] keep /i ;
M: groups set-length
- [ groups-n * ] keep groups-seq set-length ;
+ [ n>> * ] [ seq>> ] bi set-length ;
-: group@ ( n groups -- from to seq )
- [ groups-n [ * dup ] keep + ] keep
- groups-seq [ length min ] keep ;
+M: groups group@
+ [ n>> [ * dup ] keep + ] [ seq>> ] bi [ length min ] keep ;
-M: groups nth
- [ group@ ] keep
- groups-sliced? [ ] [ subseq ] if ;
+TUPLE: sliced-groups < groups ;
-M: groups set-nth
- group@ 0 swap copy ;
+: ( seq n -- groups )
+ sliced-groups construct-groups ; inline
-M: groups like drop { } like ;
+M: sliced-groups nth group@ ;
-INSTANCE: groups sequence
+TUPLE: clumps < abstract-groups ;
+
+: ( seq n -- groups )
+ clumps construct-groups ; inline
+
+M: clumps length
+ [ seq>> length ] [ n>> ] bi - 1+ ;
+
+M: clumps set-length
+ [ n>> + 1- ] [ seq>> ] bi set-length ;
+
+M: clumps group@
+ [ n>> over + ] [ seq>> ] bi ;
+
+TUPLE: sliced-clumps < groups ;
+
+: ( seq n -- groups )
+ sliced-clumps construct-groups ; inline
+
+M: sliced-clumps nth group@ ;
: group ( seq n -- array ) { } like ;
+: clump ( seq n -- array ) { } like ;
+
: ?head ( seq begin -- newseq ? )
2dup head? [ length tail t ] [ drop f ] if ;
@@ -74,7 +104,7 @@ INSTANCE: groups sequence
1array
] [
"\n" split [
- 1 head-slice* [
+ but-last-slice [
"\r" ?tail drop "\r" split
] map
] keep peek "\r" split suffix concat
diff --git a/core/syntax/syntax.factor b/core/syntax/syntax.factor
index b2f063ddf1..2e1c46fac1 100755
--- a/core/syntax/syntax.factor
+++ b/core/syntax/syntax.factor
@@ -1,6 +1,6 @@
! Copyright (C) 2004, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: alien arrays bit-arrays byte-arrays
+USING: alien arrays bit-arrays byte-arrays byte-vectors
definitions generic hashtables kernel math
namespaces parser sequences strings sbufs vectors words
quotations io assocs splitting classes.tuple generic.standard
@@ -79,6 +79,7 @@ IN: bootstrap.syntax
"{" [ \ } [ >array ] parse-literal ] define-syntax
"V{" [ \ } [ >vector ] parse-literal ] define-syntax
"B{" [ \ } [ >byte-array ] parse-literal ] define-syntax
+ "BV{" [ \ } [ >byte-vector ] parse-literal ] define-syntax
"?{" [ \ } [ >bit-array ] parse-literal ] define-syntax
"F{" [ \ } [ >float-array ] parse-literal ] define-syntax
"H{" [ \ } [ >hashtable ] parse-literal ] define-syntax
diff --git a/core/threads/threads-docs.factor b/core/threads/threads-docs.factor
index 3f9ff54ac8..7d8791d493 100755
--- a/core/threads/threads-docs.factor
+++ b/core/threads/threads-docs.factor
@@ -116,10 +116,13 @@ $nl
"Other threads may interrupt the sleep by calling " { $link interrupt } "." } ;
HELP: sleep
-{ $values { "ms" "a non-negative integer" } }
-{ $description "Suspends the current thread for " { $snippet "ms" } " milliseconds."
+{ $values { "dt" "a duration" } }
+{ $description "Suspends the current thread for the given duration."
$nl
-"Other threads may interrupt the sleep by calling " { $link interrupt } "." } ;
+"Other threads may interrupt the sleep by calling " { $link interrupt } "." }
+{ $examples
+ { $code "USING: threads calendar ;" "10 seconds sleep" }
+} ;
HELP: interrupt
{ $values { "thread" thread } }
diff --git a/core/threads/threads-tests.factor b/core/threads/threads-tests.factor
index 0ac607f0ed..0e33ccd94c 100755
--- a/core/threads/threads-tests.factor
+++ b/core/threads/threads-tests.factor
@@ -1,5 +1,6 @@
USING: namespaces io tools.test threads kernel
-concurrency.combinators math ;
+concurrency.combinators concurrency.promises locals math
+words ;
IN: threads.tests
3 "x" set
@@ -27,3 +28,16 @@ yield
"i" tget
] parallel-map
] unit-test
+
+[ [ 3 throw ] "A" suspend ] [ 3 = ] must-fail-with
+
+:: spawn-namespace-test ( -- )
+ [let | p [ ] g [ gensym ] |
+ [
+ g "x" set
+ [ "x" get p fulfill ] "B" spawn drop
+ ] with-scope
+ p ?promise g eq?
+ ] ;
+
+[ t ] [ spawn-namespace-test ] unit-test
diff --git a/core/threads/threads.factor b/core/threads/threads.factor
index 8b89cd5732..a1c7e208dc 100755
--- a/core/threads/threads.factor
+++ b/core/threads/threads.factor
@@ -12,7 +12,7 @@ SYMBOL: initial-thread
TUPLE: thread
name quot exit-handler
id
-continuation state
+continuation state runnable
mailbox variables sleep-entry ;
: self ( -- thread ) 40 getenv ; inline
@@ -91,9 +91,11 @@ PRIVATE>
[ sleep-queue heap-peek nip millis [-] ]
} cond ;
+DEFER: stop
+
r check-registered dup r> sleep-queue heap-push*
>>sleep-entry drop ;
@@ -111,36 +113,57 @@ PRIVATE>
[ ] while
drop ;
+: start ( namestack thread -- )
+ [
+ set-self
+ set-namestack
+ V{ } set-catchstack
+ { } set-retainstack
+ { } set-datastack
+ self quot>> [ call stop ] call-clear
+ ] 2 (throw) ;
+
+DEFER: next
+
+: no-runnable-threads ( -- * )
+ ! We should never be in a state where the only threads
+ ! are sleeping; the I/O wait thread is always runnable.
+ ! However, if it dies, we handle this case
+ ! semi-gracefully.
+ !
+ ! And if sleep-time outputs f, there are no sleeping
+ ! threads either... so WTF.
+ sleep-time [ die 0 ] unless* (sleep) next ;
+
+: (next) ( arg thread -- * )
+ f >>state
+ dup set-self
+ dup runnable>> [
+ continuation>> box> continue-with
+ ] [
+ t >>runnable start
+ ] if ;
+
: next ( -- * )
expire-sleep-loop
run-queue dup dlist-empty? [
- ! We should never be in a state where the only threads
- ! are sleeping; the I/O wait thread is always runnable.
- ! However, if it dies, we handle this case
- ! semi-gracefully.
- !
- ! And if sleep-time outputs f, there are no sleeping
- ! threads either... so WTF.
- drop sleep-time [ die 0 ] unless* (sleep) next
+ drop no-runnable-threads
] [
- pop-back
- dup array? [ first2 ] [ f swap ] if dup set-self
- f >>state
- continuation>> box>
- continue-with
+ pop-back dup array? [ first2 ] [ f swap ] if (next)
] if ;
PRIVATE>
: stop ( -- )
- self dup exit-handler>> call
- unregister-thread next ;
+ self [ exit-handler>> call ] [ unregister-thread ] bi next ;
: suspend ( quot state -- obj )
[
- self continuation>> >box
- self (>>state)
- self swap call next
+ >r
+ >r self swap call
+ r> self (>>state)
+ r> self continuation>> >box
+ next
] callcc1 2nip ; inline
: yield ( -- ) [ resume ] f suspend drop ;
@@ -153,7 +176,7 @@ M: integer sleep-until
M: f sleep-until
drop [ drop ] "interrupt" suspend drop ;
-GENERIC: sleep ( ms -- )
+GENERIC: sleep ( dt -- )
M: real sleep
millis + >integer sleep-until ;
@@ -166,16 +189,7 @@ M: real sleep
] when drop ;
: (spawn) ( thread -- )
- [
- resume-now [
- dup set-self
- dup register-thread
- V{ } set-catchstack
- { } set-retainstack
- >r { } set-datastack r>
- quot>> [ call stop ] call-clear
- ] 1 (throw)
- ] "spawn" suspend 2drop ;
+ [ register-thread ] [ namestack swap resume-with ] bi ;
: spawn ( quot name -- thread )
[ (spawn) ] keep ;
@@ -184,8 +198,8 @@ M: real sleep
>r [ [ ] [ ] while ] curry r> spawn ;
: in-thread ( quot -- )
- >r datastack namestack r>
- [ >r set-namestack set-datastack r> call ] 3curry
+ >r datastack r>
+ [ >r set-datastack r> call ] 2curry
"Thread" spawn drop ;
GENERIC: error-in-thread ( error thread -- )
@@ -199,6 +213,7 @@ GENERIC: error-in-thread ( error thread -- )
initial-thread global
[ drop f "Initial" ] cache
>>continuation
+ t >>runnable
f >>state
dup register-thread
set-self ;
diff --git a/extra/asn1/asn1-tests.factor b/extra/asn1/asn1-tests.factor
index 329ba8256d..1908e28d39 100755
--- a/extra/asn1/asn1-tests.factor
+++ b/extra/asn1/asn1-tests.factor
@@ -1,16 +1,16 @@
USING: asn1 asn1.ldap io io.streams.string tools.test ;
[ 6 ] [
- "\u000002\u000001\u000006" [ asn-syntax read-ber ] with-stream
+ "\u000002\u000001\u000006" [ asn-syntax read-ber ] with-input-stream
] unit-test
[ "testing" ] [
- "\u000004\u000007testing" [ asn-syntax read-ber ] with-stream
+ "\u000004\u000007testing" [ asn-syntax read-ber ] with-input-stream
] unit-test
[ { 1 { 3 "Administrator" "ad_is_bogus" } } ] [
"0$\u000002\u000001\u000001`\u00001f\u000002\u000001\u000003\u000004\rAdministrator\u000080\u00000bad_is_bogus"
- [ asn-syntax read-ber ] with-stream
+ [ asn-syntax read-ber ] with-input-stream
] unit-test
[
diff --git a/extra/asn1/asn1.factor b/extra/asn1/asn1.factor
index 32e3602f8f..50102d1929 100644
--- a/extra/asn1/asn1.factor
+++ b/extra/asn1/asn1.factor
@@ -98,7 +98,7 @@ DEFER: read-ber
SYMBOL: end
-: (read-array) ( stream -- )
+: (read-array) ( -- )
elements get element-id [
elements get element-syntax read-ber
dup end = [ drop ] [ , (read-array) ] if
@@ -106,7 +106,7 @@ SYMBOL: end
: read-array ( -- array ) [ (read-array) ] { } make ;
-: set-case ( -- )
+: set-case ( -- object )
elements get element-newobj
elements get element-objtype {
{ "boolean" [ "\0" = not ] }
diff --git a/extra/bank/authors.txt b/extra/bank/authors.txt
new file mode 100644
index 0000000000..e9c193bac7
--- /dev/null
+++ b/extra/bank/authors.txt
@@ -0,0 +1 @@
+Alex Chapman
diff --git a/extra/bank/bank-tests.factor b/extra/bank/bank-tests.factor
new file mode 100644
index 0000000000..2aa31f1e85
--- /dev/null
+++ b/extra/bank/bank-tests.factor
@@ -0,0 +1,34 @@
+USING: accessors arrays bank calendar kernel math math.functions namespaces tools.test tools.walker ;
+IN: bank.tests
+
+SYMBOL: my-account
+[
+ "Alex's Take Over the World Fund" 0.07 1 2007 11 1 6101.94 open-account my-account set
+ [ 6137 ] [ my-account get 2007 12 2 process-to-date balance>> round >integer ] unit-test
+ [ 6137 ] [ my-account get 2007 12 2 process-to-date balance>> round >integer ] unit-test
+] with-scope
+
+[
+ "Petty Cash" 0.07 1 2006 12 1 10962.18 open-account my-account set
+ [ 11027 ] [ my-account get 2007 1 2 process-to-date balance>> round >integer ] unit-test
+] with-scope
+
+[
+ "Saving to buy a pony" 0.0725 1 2008 3 3 11106.24 open-account my-account set
+ [ 8416 ] [
+ my-account get [
+ 2008 3 11 -750 "Need to buy food" ,
+ 2008 3 25 -500 "Going to a party" ,
+ 2008 4 8 -800 "Losing interest in the pony..." ,
+ 2008 4 8 -700 "Buying a rocking horse" ,
+ ] { } make inserting-transactions balance>> round >integer
+ ] unit-test
+] with-scope
+
+[
+ [ 6781 ] [
+ "..." 0.07 1 2007 4 10 4398.50 open-account
+ 2007 10 26 2000 "..." 1array inserting-transactions
+ 2008 4 10 process-to-date dup balance>> swap unpaid-interest>> + round >integer
+ ] unit-test
+] with-scope
diff --git a/extra/bank/bank.factor b/extra/bank/bank.factor
new file mode 100644
index 0000000000..abe3250ecf
--- /dev/null
+++ b/extra/bank/bank.factor
@@ -0,0 +1,67 @@
+USING: accessors calendar kernel math math.order money sequences ;
+IN: bank
+
+TUPLE: account name interest-rate interest-payment-day opening-date transactions unpaid-interest interest-last-paid ;
+
+: ( name interest-rate interest-payment-day opening-date -- account )
+ V{ } clone 0 pick account boa ;
+
+TUPLE: transaction date amount description ;
+C: transaction
+
+: >>transaction ( account transaction -- account )
+ over transactions>> push ;
+
+: total ( transactions -- balance )
+ 0 [ amount>> + ] reduce ;
+
+: balance>> ( account -- balance ) transactions>> total ;
+
+: open-account ( name interest-rate interest-payment-day opening-date opening-balance -- account )
+ >r [ ] keep r> "Account Opened" >>transaction ;
+
+: daily-rate ( yearly-rate day -- daily-rate )
+ days-in-year / ;
+
+: daily-rate>> ( account date -- rate )
+ [ interest-rate>> ] dip daily-rate ;
+
+: transactions-on-date ( account date -- transactions )
+ [ before? ] curry filter ;
+
+: balance-on-date ( account date -- balance )
+ transactions-on-date total ;
+
+: pay-interest ( account date -- )
+ over unpaid-interest>> "Interest Credit"
+ >>transaction 0 >>unpaid-interest drop ;
+
+: interest-payment-day? ( account date -- ? )
+ day>> swap interest-payment-day>> = ;
+
+: ?pay-interest ( account date -- )
+ 2dup interest-payment-day? [ pay-interest ] [ 2drop ] if ;
+
+: unpaid-interest+ ( account amount -- account )
+ over unpaid-interest>> + >>unpaid-interest ;
+
+: accumulate-interest ( account date -- )
+ [ dupd daily-rate>> over balance>> * unpaid-interest+ ] keep
+ >>interest-last-paid drop ;
+
+: process-day ( account date -- )
+ 2dup accumulate-interest ?pay-interest ;
+
+: each-day ( quot start end -- )
+ 2dup before? [
+ >r dup >r over >r swap call r> r> 1 days time+ r> each-day
+ ] [
+ 3drop
+ ] if ;
+
+: process-to-date ( account date -- account )
+ over interest-last-paid>> 1 days time+
+ [ dupd process-day ] spin each-day ;
+
+: inserting-transactions ( account transactions -- account )
+ [ [ date>> process-to-date ] keep >>transaction ] each ;
diff --git a/extra/bank/summary.txt b/extra/bank/summary.txt
new file mode 100644
index 0000000000..efd88787a5
--- /dev/null
+++ b/extra/bank/summary.txt
@@ -0,0 +1 @@
+Bank account simulator for compound interest calculated daily and paid monthly
diff --git a/extra/benchmark/crc32/crc32.factor b/extra/benchmark/crc32/crc32.factor
index ec424e89c9..0e5482da30 100755
--- a/extra/benchmark/crc32/crc32.factor
+++ b/extra/benchmark/crc32/crc32.factor
@@ -1,10 +1,10 @@
-USING: io.crc32 io.encodings.ascii io.files kernel math ;
+USING: checksums checksums.crc32 io.encodings.ascii io.files kernel math ;
IN: benchmark.crc32
: crc32-primes-list ( -- )
10 [
- "extra/math/primes/list/list.factor" resource-path
- ascii file-contents crc32 drop
+ "resource:extra/math/primes/list/list.factor"
+ crc32 checksum-file drop
] times ;
MAIN: crc32-primes-list
diff --git a/extra/benchmark/fasta/fasta.factor b/extra/benchmark/fasta/fasta.factor
index 215b677e16..d449c0fc5b 100755
--- a/extra/benchmark/fasta/fasta.factor
+++ b/extra/benchmark/fasta/fasta.factor
@@ -81,7 +81,7 @@ HINTS: random fixnum ;
write-description
[let | k! [ 0 ] alu [ ] |
[| len | k len alu make-repeat-fasta k! ] split-lines
- ] with-locals ; inline
+ ] ; inline
: fasta ( n out -- )
homo-sapiens make-cumulative
@@ -103,7 +103,7 @@ HINTS: random fixnum ;
drop
] with-file-writer
- ] with-locals ;
+ ] ;
: run-fasta 2500000 reverse-complement-in fasta ;
diff --git a/extra/benchmark/knucleotide/knucleotide.factor b/extra/benchmark/knucleotide/knucleotide.factor
index e06b81f6de..6bd2d69cfa 100644
--- a/extra/benchmark/knucleotide/knucleotide.factor
+++ b/extra/benchmark/knucleotide/knucleotide.factor
@@ -56,7 +56,7 @@ IN: benchmark.knucleotide
drop ;
: knucleotide ( -- )
- "extra/benchmark/knucleotide/knucleotide-input.txt" resource-path
+ "resource:extra/benchmark/knucleotide/knucleotide-input.txt"
ascii [ read-input ] with-file-reader
process-input ;
diff --git a/extra/benchmark/md5/md5.factor b/extra/benchmark/md5/md5.factor
index 3043725acd..8a259c1217 100644
--- a/extra/benchmark/md5/md5.factor
+++ b/extra/benchmark/md5/md5.factor
@@ -1,7 +1,7 @@
-USING: crypto.md5 io.files kernel ;
+USING: checksums checksums.md5 io.files kernel ;
IN: benchmark.md5
: md5-primes-list ( -- )
- "extra/math/primes/list/list.factor" resource-path file>md5 drop ;
+ "resource:extra/math/primes/list/list.factor" md5 checksum-file drop ;
MAIN: md5-primes-list
diff --git a/extra/benchmark/partial-sums/partial-sums.factor b/extra/benchmark/partial-sums/partial-sums.factor
index b4bb1fa8d2..8eb883241b 100644
--- a/extra/benchmark/partial-sums/partial-sums.factor
+++ b/extra/benchmark/partial-sums/partial-sums.factor
@@ -3,7 +3,8 @@ prettyprint words hints ;
IN: benchmark.partial-sums
: summing ( n quot -- y )
- [ + ] compose 0.0 -rot 1 -rot (each-integer) ; inline
+ [ >float ] swap [ + ] 3compose
+ 0.0 -rot 1 -rot (each-integer) ; inline
: 2/3^k ( n -- y ) [ 2.0 3.0 / swap 1- ^ ] summing ;
diff --git a/extra/benchmark/reverse-complement/reverse-complement-tests.factor b/extra/benchmark/reverse-complement/reverse-complement-tests.factor
index c66de87cb5..883124105b 100755
--- a/extra/benchmark/reverse-complement/reverse-complement-tests.factor
+++ b/extra/benchmark/reverse-complement/reverse-complement-tests.factor
@@ -1,13 +1,13 @@
IN: benchmark.reverse-complement.tests
-USING: tools.test benchmark.reverse-complement crypto.md5
+USING: tools.test benchmark.reverse-complement
+checksums checksums.md5
io.files kernel ;
[ "c071aa7e007a9770b2fb4304f55a17e5" ] [
- "extra/benchmark/reverse-complement/reverse-complement-test-in.txt"
- "extra/benchmark/reverse-complement/reverse-complement-test-out.txt"
- [ resource-path ] bi@
+ "resource:extra/benchmark/reverse-complement/reverse-complement-test-in.txt"
+ "resource:extra/benchmark/reverse-complement/reverse-complement-test-out.txt"
reverse-complement
- "extra/benchmark/reverse-complement/reverse-complement-test-out.txt"
- resource-path file>md5str
+ "resource:extra/benchmark/reverse-complement/reverse-complement-test-out.txt"
+ md5 checksum-file hex-string
] unit-test
diff --git a/extra/benchmark/reverse-complement/reverse-complement.factor b/extra/benchmark/reverse-complement/reverse-complement.factor
index d83b720187..5fdaf49d8f 100755
--- a/extra/benchmark/reverse-complement/reverse-complement.factor
+++ b/extra/benchmark/reverse-complement/reverse-complement.factor
@@ -32,13 +32,11 @@ HINTS: do-line vector string ;
readln [ do-line (reverse-complement) ] [ show-seq ] if* ;
: reverse-complement ( infile outfile -- )
- ascii [
- swap ascii [
- swap [
- 500000 (reverse-complement)
- ] with-stream
- ] with-disposal
- ] with-disposal ;
+ ascii [
+ ascii [
+ 500000 (reverse-complement)
+ ] with-file-reader
+ ] with-file-writer ;
: reverse-complement-in
"reverse-complement-in.txt" temp-file ;
diff --git a/extra/benchmark/sha1/sha1.factor b/extra/benchmark/sha1/sha1.factor
index 897d83ea0e..d5ff5673c2 100644
--- a/extra/benchmark/sha1/sha1.factor
+++ b/extra/benchmark/sha1/sha1.factor
@@ -1,7 +1,7 @@
-USING: crypto.sha1 io.files kernel ;
+USING: checksums checksums.sha1 io.files kernel ;
IN: benchmark.sha1
: sha1-primes-list ( -- )
- "extra/math/primes/list/list.factor" resource-path file>sha1 drop ;
+ "resource:extra/math/primes/list/list.factor" sha1 checksum-file drop ;
MAIN: sha1-primes-list
diff --git a/extra/benchmark/sockets/sockets.factor b/extra/benchmark/sockets/sockets.factor
index 25212c7264..6defd94290 100755
--- a/extra/benchmark/sockets/sockets.factor
+++ b/extra/benchmark/sockets/sockets.factor
@@ -1,6 +1,6 @@
USING: io.sockets io kernel math threads io.encodings.ascii
-debugger tools.time prettyprint concurrency.count-downs
-namespaces arrays continuations ;
+io.streams.duplex debugger tools.time prettyprint
+concurrency.count-downs namespaces arrays continuations ;
IN: benchmark.sockets
SYMBOL: counter
@@ -10,7 +10,7 @@ SYMBOL: counter
: server-addr "127.0.0.1" 7777 ;
: server-loop ( server -- )
- dup accept [
+ dup accept drop [
[
read1 CHAR: x = [
"server" get dispose
@@ -30,17 +30,17 @@ SYMBOL: counter
] ignore-errors ;
: simple-client ( -- )
- server-addr ascii [
+ server-addr ascii [
CHAR: b write1 flush
number-of-requests
[ CHAR: a dup write1 flush read1 assert= ] times
counter get count-down
- ] with-stream ;
+ ] with-client ;
: stop-server ( -- )
- server-addr ascii [
+ server-addr ascii [
CHAR: x write1
- ] with-stream ;
+ ] with-client ;
: clients ( n -- )
dup pprint " clients: " write [
diff --git a/extra/bootstrap/image/download/download.factor b/extra/bootstrap/image/download/download.factor
index a186954ef0..46aca6cc6b 100644
--- a/extra/bootstrap/image/download/download.factor
+++ b/extra/bootstrap/image/download/download.factor
@@ -1,8 +1,8 @@
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: bootstrap.image.download
-USING: http.client crypto.md5 splitting assocs kernel io.files
-bootstrap.image sequences io ;
+USING: http.client checksums checksums.md5 splitting assocs
+kernel io.files bootstrap.image sequences io ;
: url "http://factorcode.org/images/latest/" ;
@@ -12,7 +12,7 @@ bootstrap.image sequences io ;
: need-new-image? ( image -- ? )
dup exists?
- [ dup file>md5str swap download-checksums at = not ]
+ [ [ md5 checksum-file hex-string ] [ download-checksums at ] bi = not ]
[ drop t ] if ;
: download-image ( arch -- )
diff --git a/extra/bootstrap/image/upload/upload.factor b/extra/bootstrap/image/upload/upload.factor
index ab26a4ff13..30d0428744 100755
--- a/extra/bootstrap/image/upload/upload.factor
+++ b/extra/bootstrap/image/upload/upload.factor
@@ -1,8 +1,9 @@
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
+USING: http.client checksums checksums.md5 splitting assocs
+kernel io.files bootstrap.image sequences io namespaces
+io.launcher math io.encodings.ascii ;
IN: bootstrap.image.upload
-USING: http.client crypto.md5 splitting assocs kernel io.files
-bootstrap.image sequences io namespaces io.launcher math io.encodings.ascii ;
SYMBOL: upload-images-destination
@@ -17,7 +18,9 @@ SYMBOL: upload-images-destination
: compute-checksums ( -- )
checksums ascii [
- boot-image-names [ dup write bl file>md5str print ] each
+ boot-image-names [
+ [ write bl ] [ md5 checksum-file hex-string print ] bi
+ ] each
] with-file-writer ;
: upload-images ( -- )
diff --git a/extra/builder/util/util.factor b/extra/builder/util/util.factor
index 3b0834b190..c40efaaa04 100644
--- a/extra/builder/util/util.factor
+++ b/extra/builder/util/util.factor
@@ -16,7 +16,7 @@ IN: builder.util
: minutes>ms ( min -- ms ) 60 * 1000 * ;
-: file>string ( file -- string ) utf8 [ stdio get contents ] with-file-reader ;
+: file>string ( file -- string ) utf8 file-contents ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -107,5 +107,5 @@ USE: prettyprint
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: git-id ( -- id )
- { "git" "show" } utf8 [ readln ] with-stream
+ { "git" "show" } utf8 [ readln ] with-input-stream
" " split second ;
diff --git a/extra/cairo/png/png.factor b/extra/cairo/png/png.factor
index 1bbad29835..a3b13c9691 100755
--- a/extra/cairo/png/png.factor
+++ b/extra/cairo/png/png.factor
@@ -21,7 +21,7 @@ ERROR: cairo-error string ;
{ CAIRO_STATUS_FILE_NOT_FOUND [ "Cairo: file not found" cairo-error ] }
{ CAIRO_STATUS_READ_ERROR [ "Cairo: read error" cairo-error ] }
[ drop ]
- } cond ;
+ } case ;
: ( path -- png )
normalize-path
diff --git a/extra/crypto/md5/authors.txt b/extra/checksums/md5/authors.txt
similarity index 100%
rename from extra/crypto/md5/authors.txt
rename to extra/checksums/md5/authors.txt
diff --git a/extra/checksums/md5/md5-docs.factor b/extra/checksums/md5/md5-docs.factor
new file mode 100755
index 0000000000..dca039d1d3
--- /dev/null
+++ b/extra/checksums/md5/md5-docs.factor
@@ -0,0 +1,11 @@
+USING: help.markup help.syntax ;
+IN: checksums.md5
+
+HELP: md5
+{ $description "MD5 checksum algorithm." } ;
+
+ARTICLE: "checksums.md5" "MD5 checksum"
+"The MD5 checksum algorithm implements a one-way hash function. While it is widely used, many weaknesses are known and it should not be used in new applications (" { $url "http://www.schneier.com/blog/archives/2005/03/more_hash_funct.html" } ")."
+{ $subsection md5 } ;
+
+ABOUT: "checksums.md5"
diff --git a/extra/checksums/md5/md5-tests.factor b/extra/checksums/md5/md5-tests.factor
new file mode 100755
index 0000000000..8e314f7c28
--- /dev/null
+++ b/extra/checksums/md5/md5-tests.factor
@@ -0,0 +1,10 @@
+USING: kernel math namespaces checksums checksums.md5 tools.test byte-arrays ;
+
+[ "d41d8cd98f00b204e9800998ecf8427e" ] [ "" >byte-array md5 checksum-bytes hex-string ] unit-test
+[ "0cc175b9c0f1b6a831c399e269772661" ] [ "a" >byte-array md5 checksum-bytes hex-string ] unit-test
+[ "900150983cd24fb0d6963f7d28e17f72" ] [ "abc" >byte-array md5 checksum-bytes hex-string ] unit-test
+[ "f96b697d7cb7938d525a2f31aaf161d0" ] [ "message digest" >byte-array md5 checksum-bytes hex-string ] unit-test
+[ "c3fcd3d76192e4007dfb496cca67e13b" ] [ "abcdefghijklmnopqrstuvwxyz" >byte-array md5 checksum-bytes hex-string ] unit-test
+[ "d174ab98d277d9f5a5611c2c9f419d9f" ] [ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" >byte-array md5 checksum-bytes hex-string ] unit-test
+[ "57edf4a22be3c955ac49da2e2107b67a" ] [ "12345678901234567890123456789012345678901234567890123456789012345678901234567890" >byte-array md5 checksum-bytes hex-string ] unit-test
+
diff --git a/extra/crypto/md5/md5.factor b/extra/checksums/md5/md5.factor
similarity index 88%
rename from extra/crypto/md5/md5.factor
rename to extra/checksums/md5/md5.factor
index 45e10da74d..a385f6d04f 100755
--- a/extra/crypto/md5/md5.factor
+++ b/extra/checksums/md5/md5.factor
@@ -3,8 +3,8 @@
USING: kernel io io.binary io.files io.streams.byte-array math
math.functions math.parser namespaces splitting strings
sequences crypto.common byte-arrays locals sequences.private
-io.encodings.binary symbols math.bitfields.lib ;
-IN: crypto.md5
+io.encodings.binary symbols math.bitfields.lib checksums ;
+IN: checksums.md5
md5) ( -- )
+: stream>md5 ( -- )
64 read [ process-md5-block ] keep
- length 64 = [ (stream>md5) ] when ;
+ length 64 = [ stream>md5 ] when ;
: get-md5 ( -- str )
[ a b c d ] [ get 4 >le ] map concat >byte-array ;
PRIVATE>
-: stream>md5 ( stream -- byte-array )
- [ initialize-md5 (stream>md5) get-md5 ] with-stream ;
+SINGLETON: md5
-: byte-array>md5 ( byte-array -- checksum )
- binary stream>md5 ;
+INSTANCE: md5 checksum
-: byte-array>md5str ( byte-array -- md5-string )
- byte-array>md5 hex-string ;
-
-: file>md5 ( path -- byte-array )
- binary stream>md5 ;
-
-: file>md5str ( path -- md5-string )
- file>md5 hex-string ;
+M: md5 checksum-stream ( stream -- byte-array )
+ drop [ initialize-md5 stream>md5 get-md5 ] with-input-stream ;
diff --git a/extra/checksums/null/null.factor b/extra/checksums/null/null.factor
new file mode 100644
index 0000000000..d3ab878a12
--- /dev/null
+++ b/extra/checksums/null/null.factor
@@ -0,0 +1,8 @@
+USING: checksums kernel ;
+IN: checksums.null
+
+SINGLETON: null
+
+INSTANCE: null checksum
+
+M: null checksum-bytes drop ;
diff --git a/extra/crypto/sha1/authors.txt b/extra/checksums/sha1/authors.txt
similarity index 100%
rename from extra/crypto/sha1/authors.txt
rename to extra/checksums/sha1/authors.txt
diff --git a/extra/checksums/sha1/sha1-docs.factor b/extra/checksums/sha1/sha1-docs.factor
new file mode 100644
index 0000000000..8b8bf1cfa9
--- /dev/null
+++ b/extra/checksums/sha1/sha1-docs.factor
@@ -0,0 +1,11 @@
+USING: help.markup help.syntax ;
+IN: checksums.sha1
+
+HELP: sha1
+{ $description "SHA1 checksum algorithm." } ;
+
+ARTICLE: "checksums.sha1" "SHA1 checksum"
+"The SHA1 checksum algorithm implements a one-way hash function. It is generally considered to be stronger than MD5, however there is a known algorithm for finding collisions more effectively than a brute-force search (" { $url "http://www.schneier.com/blog/archives/2005/02/sha1_broken.html" } ")."
+{ $subsection sha1 } ;
+
+ABOUT: "checksums.sha1"
diff --git a/extra/crypto/sha1/sha1-tests.factor b/extra/checksums/sha1/sha1-tests.factor
similarity index 69%
rename from extra/crypto/sha1/sha1-tests.factor
rename to extra/checksums/sha1/sha1-tests.factor
index 14307355c2..808d37d1e4 100755
--- a/extra/crypto/sha1/sha1-tests.factor
+++ b/extra/checksums/sha1/sha1-tests.factor
@@ -1,14 +1,14 @@
-USING: arrays kernel math namespaces sequences tools.test crypto.sha1 ;
+USING: arrays kernel math namespaces sequences tools.test checksums checksums.sha1 ;
-[ "a9993e364706816aba3e25717850c26c9cd0d89d" ] [ "abc" byte-array>sha1str ] unit-test
-[ "84983e441c3bd26ebaae4aa1f95129e5e54670f1" ] [ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" byte-array>sha1str ] unit-test
+[ "a9993e364706816aba3e25717850c26c9cd0d89d" ] [ "abc" sha1 checksum-bytes hex-string ] unit-test
+[ "84983e441c3bd26ebaae4aa1f95129e5e54670f1" ] [ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" sha1 checksum-bytes hex-string ] unit-test
! [ "34aa973cd4c4daa4f61eeb2bdbad27316534016f" ] [ 1000000 CHAR: a fill string>sha1str ] unit-test ! takes a long time...
[ "dea356a2cddd90c7a7ecedc5ebb563934f460452" ] [ "0123456701234567012345670123456701234567012345670123456701234567"
-10 swap concat byte-array>sha1str ] unit-test
+10 swap concat sha1 checksum-bytes hex-string ] unit-test
[
";\u00009b\u0000fd\u0000cdK\u0000a3^s\u0000d0*\u0000e3\\\u0000b5\u000013<\u0000e8wA\u0000b2\u000083\u0000d20\u0000f1\u0000e6\u0000cc\u0000d8\u00001e\u00009c\u000004\u0000d7PT]\u0000ce,\u000001\u000012\u000080\u000096\u000099"
] [
"\u000066\u000053\u0000f1\u00000c\u00001a\u0000fa\u0000b5\u00004c\u000061\u0000c8\u000025\u000075\u0000a8\u00004a\u0000fe\u000030\u0000d8\u0000aa\u00001a\u00003a\u000096\u000096\u0000b3\u000018\u000099\u000092\u0000bf\u0000e1\u0000cb\u00007f\u0000a6\u0000a7"
- byte-array>sha1-interleave
+ sha1-interleave
] unit-test
diff --git a/extra/crypto/sha1/sha1.factor b/extra/checksums/sha1/sha1.factor
similarity index 83%
rename from extra/crypto/sha1/sha1.factor
rename to extra/checksums/sha1/sha1.factor
index 3a74d1f5db..6427e0e8eb 100755
--- a/extra/crypto/sha1/sha1.factor
+++ b/extra/checksums/sha1/sha1.factor
@@ -1,8 +1,8 @@
USING: arrays combinators crypto.common kernel io
io.encodings.binary io.files io.streams.byte-array math.vectors
strings sequences namespaces math parser sequences vectors
-io.binary hashtables symbols math.bitfields.lib ;
-IN: crypto.sha1
+io.binary hashtables symbols math.bitfields.lib checksums ;
+IN: checksums.sha1
! Implemented according to RFC 3174.
@@ -99,30 +99,22 @@ SYMBOLS: h0 h1 h2 h3 h4 A B C D E w K ;
[ (process-sha1-block) ] each
] if ;
-: (stream>sha1) ( -- )
+: stream>sha1 ( -- )
64 read [ process-sha1-block ] keep
- length 64 = [ (stream>sha1) ] when ;
+ length 64 = [ stream>sha1 ] when ;
: get-sha1 ( -- str )
[ [ h0 h1 h2 h3 h4 ] [ get 4 >be % ] each ] "" make ;
-: stream>sha1 ( stream -- sha1 )
- [ initialize-sha1 (stream>sha1) get-sha1 ] with-stream ;
+SINGLETON: sha1
-: byte-array>sha1 ( string -- sha1 )
- binary stream>sha1 ;
+INSTANCE: sha1 checksum
-: byte-array>sha1str ( string -- str )
- byte-array>sha1 hex-string ;
+M: sha1 checksum-stream ( stream -- sha1 )
+ drop [ initialize-sha1 stream>sha1 get-sha1 ] with-input-stream ;
-: byte-array>sha1-bignum ( string -- n )
- byte-array>sha1 be> ;
-
-: file>sha1 ( file -- sha1 )
- binary stream>sha1 ;
-
-: byte-array>sha1-interleave ( string -- seq )
+: sha1-interleave ( string -- seq )
[ zero? ] left-trim
dup length odd? [ rest ] when
- seq>2seq [ byte-array>sha1 ] bi@
+ seq>2seq [ sha1 checksum-bytes ] bi@
2seq>seq ;
diff --git a/extra/crypto/sha2/authors.txt b/extra/checksums/sha2/authors.txt
similarity index 100%
rename from extra/crypto/sha2/authors.txt
rename to extra/checksums/sha2/authors.txt
diff --git a/extra/checksums/sha2/sha2-docs.factor b/extra/checksums/sha2/sha2-docs.factor
new file mode 100644
index 0000000000..c39831b266
--- /dev/null
+++ b/extra/checksums/sha2/sha2-docs.factor
@@ -0,0 +1,11 @@
+USING: help.markup help.syntax ;
+IN: checksums.sha2
+
+HELP: sha-256
+{ $description "SHA-256 checksum algorithm." } ;
+
+ARTICLE: "checksums.sha2" "SHA2 checksum"
+"The SHA2 checksum algorithm implements a one-way hash function. It is generally considered to be pretty strong."
+{ $subsection sha-256 } ;
+
+ABOUT: "checksums.sha2"
diff --git a/extra/crypto/sha2/sha2-tests.factor b/extra/checksums/sha2/sha2-tests.factor
similarity index 51%
rename from extra/crypto/sha2/sha2-tests.factor
rename to extra/checksums/sha2/sha2-tests.factor
index 8fe655f205..2f4e3c51c4 100755
--- a/extra/crypto/sha2/sha2-tests.factor
+++ b/extra/checksums/sha2/sha2-tests.factor
@@ -1,7 +1,7 @@
-USING: arrays kernel math namespaces sequences tools.test crypto.sha2 ;
-[ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ] [ "" byte-array>sha-256-string ] unit-test
-[ "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" ] [ "abc" byte-array>sha-256-string ] unit-test
-[ "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650" ] [ "message digest" byte-array>sha-256-string ] unit-test
-[ "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73" ] [ "abcdefghijklmnopqrstuvwxyz" byte-array>sha-256-string ] unit-test
-[ "db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0" ] [ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" byte-array>sha-256-string ] unit-test
-[ "f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e" ] [ "12345678901234567890123456789012345678901234567890123456789012345678901234567890" byte-array>sha-256-string ] unit-test
+USING: arrays kernel math namespaces sequences tools.test checksums.sha2 checksums ;
+[ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ] [ "" sha-256 checksum-bytes hex-string ] unit-test
+[ "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" ] [ "abc" sha-256 checksum-bytes hex-string ] unit-test
+[ "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650" ] [ "message digest" sha-256 checksum-bytes hex-string ] unit-test
+[ "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73" ] [ "abcdefghijklmnopqrstuvwxyz" sha-256 checksum-bytes hex-string ] unit-test
+[ "db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0" ] [ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" sha-256 checksum-bytes hex-string ] unit-test
+[ "f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e" ] [ "12345678901234567890123456789012345678901234567890123456789012345678901234567890" sha-256 checksum-bytes hex-string ] unit-test
diff --git a/extra/crypto/sha2/sha2.factor b/extra/checksums/sha2/sha2.factor
similarity index 94%
rename from extra/crypto/sha2/sha2.factor
rename to extra/checksums/sha2/sha2.factor
index 0acc5c1388..e5f16c9c11 100755
--- a/extra/crypto/sha2/sha2.factor
+++ b/extra/checksums/sha2/sha2.factor
@@ -1,6 +1,6 @@
USING: crypto.common kernel splitting math sequences namespaces
-io.binary symbols math.bitfields.lib ;
-IN: crypto.sha2
+io.binary symbols math.bitfields.lib checksums ;
+IN: checksums.sha2
-: byte-array>sha-256 ( string -- string )
- [
+SINGLETON: sha-256
+
+INSTANCE: sha-256 checksum
+
+M: sha-256 checksum-bytes
+ drop [
K-256 K set
initial-H-256 H set
4 word-size set
64 block-size set
byte-array>sha2
] with-scope ;
-
-: byte-array>sha-256-string ( string -- hexstring )
- byte-array>sha-256 hex-string ;
diff --git a/extra/combinators/lib/lib.factor b/extra/combinators/lib/lib.factor
index 84b41a91ff..5dfe8527c1 100755
--- a/extra/combinators/lib/lib.factor
+++ b/extra/combinators/lib/lib.factor
@@ -169,3 +169,8 @@ MACRO: multikeep ( word out-indexes -- ... )
: generate ( generator predicate -- obj )
[ dup ] swap [ dup [ nip ] unless not ] 3compose
swap [ ] do-while ;
+
+MACRO: predicates ( seq -- quot/f )
+ dup [ 1quotation [ drop ] prepend ] map
+ >r [ [ dup ] prepend ] map r> zip [ drop f ] suffix
+ [ cond ] curry ;
diff --git a/extra/concurrency/combinators/combinators-docs.factor b/extra/concurrency/combinators/combinators-docs.factor
index bbf8fb0f5f..a23301c1e2 100755
--- a/extra/concurrency/combinators/combinators-docs.factor
+++ b/extra/concurrency/combinators/combinators-docs.factor
@@ -6,11 +6,21 @@ HELP: parallel-map
{ $description "Spawns a new thread for applying " { $snippet "quot" } " to every element of " { $snippet "seq" } ", collecting the results at the end." }
{ $errors "Throws an error if one of the iterations throws an error." } ;
+HELP: 2parallel-map
+{ $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- newelt )" } } { "newseq" sequence } }
+{ $description "Spawns a new thread for applying " { $snippet "quot" } " to pairwise elements of " { $snippet "seq1" } " and " { $snippet "seq2" } ", collecting the results at the end." }
+{ $errors "Throws an error if one of the iterations throws an error." } ;
+
HELP: parallel-each
{ $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- )" } } }
{ $description "Spawns a new thread for applying " { $snippet "quot" } " to every element of " { $snippet "seq" } ", blocking until all quotations complete." }
{ $errors "Throws an error if one of the iterations throws an error." } ;
+HELP: 2parallel-each
+{ $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- )" } } }
+{ $description "Spawns a new thread for applying " { $snippet "quot" } " to pairwise elements of " { $snippet "seq1" } " and " { $snippet "seq2" } ", blocking until all quotations complete." }
+{ $errors "Throws an error if one of the iterations throws an error." } ;
+
HELP: parallel-filter
{ $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "newseq" sequence } }
{ $description "Spawns a new thread for applying " { $snippet "quot" } " to every element of " { $snippet "seq" } ", collecting the elements for which the quotation yielded a true value." }
@@ -19,7 +29,9 @@ HELP: parallel-filter
ARTICLE: "concurrency.combinators" "Concurrent combinators"
"The " { $vocab-link "concurrency.combinators" } " vocabulary provides concurrent variants of " { $link each } ", " { $link map } " and " { $link filter } ":"
{ $subsection parallel-each }
+{ $subsection 2parallel-each }
{ $subsection parallel-map }
+{ $subsection 2parallel-map }
{ $subsection parallel-filter } ;
ABOUT: "concurrency.combinators"
diff --git a/extra/concurrency/combinators/combinators-tests.factor b/extra/concurrency/combinators/combinators-tests.factor
index 3381cba5e8..562111242d 100755
--- a/extra/concurrency/combinators/combinators-tests.factor
+++ b/extra/concurrency/combinators/combinators-tests.factor
@@ -1,9 +1,11 @@
IN: concurrency.combinators.tests
USING: concurrency.combinators tools.test random kernel math
-concurrency.mailboxes threads sequences accessors ;
+concurrency.mailboxes threads sequences accessors arrays ;
[ [ drop ] parallel-each ] must-infer
+{ 2 0 } [ [ 2drop ] 2parallel-each ] must-infer-as
[ [ ] parallel-map ] must-infer
+{ 2 1 } [ [ 2array ] 2parallel-map ] must-infer-as
[ [ ] parallel-filter ] must-infer
[ { 1 4 9 } ] [ { 1 2 3 } [ sq ] parallel-map ] unit-test
@@ -22,3 +24,24 @@ concurrency.mailboxes threads sequences accessors ;
10 over [ push ] curry parallel-each
length
] unit-test
+
+[ { 10 20 30 } ] [
+ { 1 4 3 } { 10 5 10 } [ * ] 2parallel-map
+] unit-test
+
+[ { -9 -1 -7 } ] [
+ { 1 4 3 } { 10 5 10 } [ - ] 2parallel-map
+] unit-test
+
+[
+ { 1 4 3 } { 1 0 1 } [ / drop ] 2parallel-each
+] must-fail
+
+[ 20 ]
+[
+ V{ } clone
+ 10 10 pick [ [ push ] [ push ] bi ] curry 2parallel-each
+ length
+] unit-test
+
+[ { f } [ "OOPS" throw ] parallel-each ] must-fail
diff --git a/extra/concurrency/combinators/combinators.factor b/extra/concurrency/combinators/combinators.factor
index 3c4101e381..eab0ed4cb4 100755
--- a/extra/concurrency/combinators/combinators.factor
+++ b/extra/concurrency/combinators/combinators.factor
@@ -4,14 +4,27 @@ USING: concurrency.futures concurrency.count-downs sequences
kernel ;
IN: concurrency.combinators
-: parallel-map ( seq quot -- newseq )
- [ curry future ] curry map dup [ ?future ] change-each ;
- inline
+: (parallel-each) ( n quot -- )
+ >r r> keep await ; inline
: parallel-each ( seq quot -- )
- over length
- [ [ >r curry r> spawn-stage ] 2curry each ] keep await ;
- inline
+ over length [
+ [ >r curry r> spawn-stage ] 2curry each
+ ] (parallel-each) ; inline
+
+: 2parallel-each ( seq1 seq2 quot -- )
+ 2over min-length [
+ [ >r 2curry r> spawn-stage ] 2curry 2each
+ ] (parallel-each) ; inline
: parallel-filter ( seq quot -- newseq )
over >r pusher >r each r> r> like ; inline
+
+: future-values dup [ ?future ] change-each ; inline
+
+: parallel-map ( seq quot -- newseq )
+ [ curry future ] curry map future-values ;
+ inline
+
+: 2parallel-map ( seq1 seq2 quot -- newseq )
+ [ 2curry future ] curry 2map future-values ;
diff --git a/extra/concurrency/count-downs/count-downs.factor b/extra/concurrency/count-downs/count-downs.factor
index 6a75f7206c..93cef250a1 100755
--- a/extra/concurrency/count-downs/count-downs.factor
+++ b/extra/concurrency/count-downs/count-downs.factor
@@ -1,7 +1,7 @@
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: dlists kernel math concurrency.promises
-concurrency.mailboxes ;
+concurrency.mailboxes debugger accessors ;
IN: concurrency.count-downs
! http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CountDownLatch.html
@@ -9,9 +9,7 @@ IN: concurrency.count-downs
TUPLE: count-down n promise ;
: count-down-check ( count-down -- )
- dup count-down-n zero? [
- t swap count-down-promise fulfill
- ] [ drop ] if ;
+ dup n>> zero? [ t swap promise>> fulfill ] [ drop ] if ;
: ( n -- count-down )
dup 0 < [ "Invalid count for count down" throw ] when
@@ -19,15 +17,12 @@ TUPLE: count-down n promise ;
dup count-down-check ;
: count-down ( count-down -- )
- dup count-down-n dup zero? [
- "Count down already done" throw
- ] [
- 1- over set-count-down-n
- count-down-check
- ] if ;
+ dup n>> dup zero?
+ [ "Count down already done" throw ]
+ [ 1- >>n count-down-check ] if ;
: await-timeout ( count-down timeout -- )
- >r count-down-promise r> ?promise-timeout drop ;
+ >r promise>> r> ?promise-timeout ?linked t assert= ;
: await ( count-down -- )
f await-timeout ;
@@ -35,5 +30,4 @@ TUPLE: count-down n promise ;
: spawn-stage ( quot count-down -- )
[ [ count-down ] curry compose ] keep
"Count down stage"
- swap count-down-promise
- promise-mailbox spawn-linked-to drop ;
+ swap promise>> mailbox>> spawn-linked-to drop ;
diff --git a/extra/concurrency/distributed/distributed.factor b/extra/concurrency/distributed/distributed.factor
index 6704272305..c637f4baa3 100755
--- a/extra/concurrency/distributed/distributed.factor
+++ b/extra/concurrency/distributed/distributed.factor
@@ -3,7 +3,7 @@
USING: serialize sequences concurrency.messaging threads io
io.server qualified arrays namespaces kernel io.encodings.binary
accessors ;
-QUALIFIED: io.sockets
+FROM: io.sockets => host-name with-client ;
IN: concurrency.distributed
SYMBOL: local-node
@@ -23,7 +23,7 @@ SYMBOL: local-node
: start-node ( port -- )
[ internet-server ]
- [ io.sockets:host-name swap io.sockets: ] bi
+ [ host-name swap ] bi
(start-node) ;
TUPLE: remote-process id node ;
@@ -31,8 +31,7 @@ TUPLE: remote-process id node ;
C: remote-process
: send-remote-message ( message node -- )
- binary io.sockets:
- [ serialize ] with-stream ;
+ binary [ serialize ] with-client ;
M: remote-process send ( message thread -- )
[ id>> 2array ] [ node>> ] bi
diff --git a/extra/concurrency/flags/flags-tests.factor b/extra/concurrency/flags/flags-tests.factor
index f23ea95167..9d3f6de98c 100755
--- a/extra/concurrency/flags/flags-tests.factor
+++ b/extra/concurrency/flags/flags-tests.factor
@@ -1,11 +1,12 @@
IN: concurrency.flags.tests
-USING: tools.test concurrency.flags kernel threads locals ;
+USING: tools.test concurrency.flags concurrency.combinators
+kernel threads locals accessors ;
:: flag-test-1 ( -- )
[let | f [ ] |
[ f raise-flag ] "Flag test" spawn drop
f lower-flag
- f flag-value?
+ f value>>
] ;
[ f ] [ flag-test-1 ] unit-test
@@ -14,7 +15,7 @@ USING: tools.test concurrency.flags kernel threads locals ;
[let | f [ ] |
[ 1000 sleep f raise-flag ] "Flag test" spawn drop
f lower-flag
- f flag-value?
+ f value>>
] ;
[ f ] [ flag-test-2 ] unit-test
@@ -22,7 +23,7 @@ USING: tools.test concurrency.flags kernel threads locals ;
:: flag-test-3 ( -- )
[let | f [ ] |
f raise-flag
- f flag-value?
+ f value>>
] ;
[ t ] [ flag-test-3 ] unit-test
@@ -31,7 +32,7 @@ USING: tools.test concurrency.flags kernel threads locals ;
[let | f [ ] |
[ f raise-flag ] "Flag test" spawn drop
f wait-for-flag
- f flag-value?
+ f value>>
] ;
[ t ] [ flag-test-4 ] unit-test
@@ -40,7 +41,13 @@ USING: tools.test concurrency.flags kernel threads locals ;
[let | f [ ] |
[ 1000 sleep f raise-flag ] "Flag test" spawn drop
f wait-for-flag
- f flag-value?
+ f value>>
] ;
[ t ] [ flag-test-5 ] unit-test
+
+[ ] [
+ { 1 2 }
+ [ [ 1000 sleep raise-flag ] curry "Flag test" spawn drop ]
+ [ [ wait-for-flag drop ] curry parallel-each ] bi
+] unit-test
diff --git a/extra/concurrency/flags/flags.factor b/extra/concurrency/flags/flags.factor
index b3c76a7a01..ec260961d0 100755
--- a/extra/concurrency/flags/flags.factor
+++ b/extra/concurrency/flags/flags.factor
@@ -1,22 +1,20 @@
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: boxes kernel threads ;
+USING: dlists kernel threads concurrency.conditions accessors ;
IN: concurrency.flags
-TUPLE: flag value? thread ;
+TUPLE: flag value threads ;
-: ( -- flag ) f flag boa ;
+: ( -- flag ) f flag boa ;
: raise-flag ( flag -- )
- dup flag-value? [
- t over set-flag-value?
- dup flag-thread [ resume ] if-box?
- ] unless drop ;
+ dup value>> [ drop ] [ t >>value threads>> notify-all ] if ;
+
+: wait-for-flag-timeout ( flag timeout -- )
+ over value>> [ 2drop ] [ >r threads>> r> "flag" wait ] if ;
: wait-for-flag ( flag -- )
- dup flag-value? [ drop ] [
- [ flag-thread >box ] curry "flag" suspend drop
- ] if ;
+ f wait-for-flag-timeout ;
: lower-flag ( flag -- )
- dup wait-for-flag f swap set-flag-value? ;
+ [ wait-for-flag ] [ f >>value drop ] bi ;
diff --git a/extra/concurrency/mailboxes/mailboxes.factor b/extra/concurrency/mailboxes/mailboxes.factor
index ac03197708..aa4dc2df3d 100755
--- a/extra/concurrency/mailboxes/mailboxes.factor
+++ b/extra/concurrency/mailboxes/mailboxes.factor
@@ -3,7 +3,7 @@
IN: concurrency.mailboxes
USING: dlists threads sequences continuations
namespaces random math quotations words kernel arrays assocs
-init system concurrency.conditions accessors ;
+init system concurrency.conditions accessors debugger ;
TUPLE: mailbox threads data closed ;
@@ -83,6 +83,9 @@ M: mailbox dispose
TUPLE: linked-error error thread ;
+M: linked-error error.
+ [ thread>> error-in-thread. ] [ error>> error. ] bi ;
+
C: linked-error
: ?linked dup linked-error? [ rethrow ] when ;
diff --git a/extra/contributors/contributors-tests.factor b/extra/contributors/contributors-tests.factor
new file mode 100644
index 0000000000..1476715588
--- /dev/null
+++ b/extra/contributors/contributors-tests.factor
@@ -0,0 +1,5 @@
+IN: contributors.tests
+USING: contributors tools.test ;
+
+\ contributors must-infer
+[ ] [ contributors ] unit-test
diff --git a/extra/contributors/contributors.factor b/extra/contributors/contributors.factor
index 868e968169..9f2d5a55fa 100755
--- a/extra/contributors/contributors.factor
+++ b/extra/contributors/contributors.factor
@@ -1,13 +1,13 @@
! Copyright (C) 2007 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: io.files io.launcher io.styles io hashtables kernel
-sequences sequences.lib assocs system sorting math.parser
-sets ;
+USING: io.files io.launcher io.styles io.encodings.ascii io
+hashtables kernel sequences sequences.lib assocs system sorting
+math.parser sets ;
IN: contributors
: changelog ( -- authors )
image parent-directory [
- "git-log --pretty=format:%an" lines
+ "git-log --pretty=format:%an" ascii lines
] with-directory ;
: patch-counts ( authors -- assoc )
diff --git a/extra/cpu/8080/test/test.factor b/extra/cpu/8080/test/test.factor
index 85f27d7e40..f88db2935f 100755
--- a/extra/cpu/8080/test/test.factor
+++ b/extra/cpu/8080/test/test.factor
@@ -1,5 +1,5 @@
USING: kernel cpu.8080 cpu.8080.emulator math math io
-tools.time combinators sequences io.files ;
+tools.time combinators sequences io.files io.encodings.ascii ;
IN: cpu.8080.test
: step ( cpu -- )
@@ -29,7 +29,7 @@ IN: cpu.8080.test
: >ppm ( cpu filename -- cpu )
#! Dump the current screen image to a ppm image file with the given name.
- [
+ ascii [
"P3" print
"256 224" print
"1" print
@@ -45,7 +45,7 @@ IN: cpu.8080.test
] each-8bit drop
] each drop nl
] each
- ] with-stream ;
+ ] with-file-writer ;
: time-test ( -- )
test-cpu [ 1000000 run-n drop ] time ;
diff --git a/extra/crypto/common/common-docs.factor b/extra/crypto/common/common-docs.factor
deleted file mode 100644
index 559c7934d0..0000000000
--- a/extra/crypto/common/common-docs.factor
+++ /dev/null
@@ -1,13 +0,0 @@
-USING: help.markup help.syntax kernel math sequences quotations
-math.private ;
-IN: crypto.common
-
-HELP: hex-string
-{ $values { "seq" "a sequence" } { "str" "a string" } }
-{ $description "Converts a sequence of values from 0-255 to a string of hex numbers from 0-ff." }
-{ $examples
- { $example "USING: crypto.common io ;" "B{ 1 2 3 4 } hex-string print" "01020304" }
-}
-{ $notes "Numbers are zero-padded on the left." } ;
-
-
diff --git a/extra/crypto/common/common.factor b/extra/crypto/common/common.factor
index a714727ad9..efe4653eba 100644
--- a/extra/crypto/common/common.factor
+++ b/extra/crypto/common/common.factor
@@ -1,5 +1,6 @@
USING: arrays kernel io io.binary sbufs splitting strings sequences
-namespaces math math.parser parser hints math.bitfields.lib ;
+namespaces math math.parser parser hints math.bitfields.lib
+assocs ;
IN: crypto.common
: w+ ( int int -- int ) + 32 bits ; inline
@@ -39,9 +40,6 @@ SYMBOL: big-endian?
: update-old-new ( old new -- )
[ get >r get r> ] 2keep >r >r w+ dup r> set r> set ; inline
-: hex-string ( seq -- str )
- [ [ >hex 2 48 pad-left % ] each ] "" make ;
-
: slice3 ( n seq -- a b c ) >r dup 3 + r> first3 ;
: seq>2seq ( seq -- seq1 seq2 )
@@ -50,7 +48,7 @@ SYMBOL: big-endian?
: 2seq>seq ( seq1 seq2 -- seq )
#! { aceg } { bdfh } -> { abcdefgh }
- [ 2array flip concat ] keep like ;
+ [ zip concat ] keep like ;
: mod-nth ( n seq -- elt )
#! 5 "abcd" -> b
diff --git a/extra/crypto/hmac/hmac.factor b/extra/crypto/hmac/hmac.factor
index 91d404aead..6e30f19775 100755
--- a/extra/crypto/hmac/hmac.factor
+++ b/extra/crypto/hmac/hmac.factor
@@ -1,18 +1,19 @@
-USING: arrays combinators crypto.common crypto.md5 crypto.sha1
-crypto.md5.private io io.binary io.files io.streams.byte-array
-kernel math math.vectors memoize sequences io.encodings.binary ;
+USING: arrays combinators crypto.common checksums checksums.md5
+checksums.sha1 checksums.md5.private io io.binary io.files
+io.streams.byte-array kernel math math.vectors memoize sequences
+io.encodings.binary ;
IN: crypto.hmac
: sha1-hmac ( Ko Ki -- hmac )
initialize-sha1 process-sha1-block
- (stream>sha1) get-sha1
+ stream>sha1 get-sha1
initialize-sha1
>r process-sha1-block r>
process-sha1-block get-sha1 ;
: md5-hmac ( Ko Ki -- hmac )
initialize-md5 process-md5-block
- (stream>md5) get-md5
+ stream>md5 get-md5
initialize-md5
>r process-md5-block r>
process-md5-block get-md5 ;
@@ -29,7 +30,7 @@ MEMO: opad ( -- seq ) 64 HEX: 5c ;
ipad seq-bitxor ;
: stream>sha1-hmac ( K stream -- hmac )
- [ init-hmac sha1-hmac ] with-stream ;
+ [ init-hmac sha1-hmac ] with-input-stream ;
: file>sha1-hmac ( K path -- hmac )
binary stream>sha1-hmac ;
@@ -38,7 +39,7 @@ MEMO: opad ( -- seq ) 64 HEX: 5c ;
binary stream>sha1-hmac ;
: stream>md5-hmac ( K stream -- hmac )
- [ init-hmac md5-hmac ] with-stream ;
+ [ init-hmac md5-hmac ] with-input-stream ;
: file>md5-hmac ( K path -- hmac )
binary stream>md5-hmac ;
diff --git a/extra/crypto/md5/md5-docs.factor b/extra/crypto/md5/md5-docs.factor
deleted file mode 100755
index 667e0449ae..0000000000
--- a/extra/crypto/md5/md5-docs.factor
+++ /dev/null
@@ -1,18 +0,0 @@
-USING: help.markup help.syntax kernel math sequences quotations
-crypto.common byte-arrays ;
-IN: crypto.md5
-
-HELP: stream>md5
-{ $values { "stream" "a stream" } { "byte-array" "md5 hash" } }
-{ $description "Take the MD5 hash until end of stream." }
-{ $notes "Used to implement " { $link byte-array>md5 } " and " { $link file>md5 } ". Call " { $link hex-string } " to convert to the canonical string representation." } ;
-
-HELP: byte-array>md5
-{ $values { "byte-array" byte-array } { "checksum" "an md5 hash" } }
-{ $description "Outputs the MD5 hash of a byte array." }
-{ $notes "Call " { $link hex-string } " to convert to the canonical string representation." } ;
-
-HELP: file>md5
-{ $values { "path" "a path" } { "byte-array" "byte-array md5 hash" } }
-{ $description "Outputs the MD5 hash of a file." }
-{ $notes "Call " { $link hex-string } " to convert to the canonical string representation." } ;
diff --git a/extra/crypto/md5/md5-tests.factor b/extra/crypto/md5/md5-tests.factor
deleted file mode 100755
index 73bd240455..0000000000
--- a/extra/crypto/md5/md5-tests.factor
+++ /dev/null
@@ -1,10 +0,0 @@
-USING: kernel math namespaces crypto.md5 tools.test byte-arrays ;
-
-[ "d41d8cd98f00b204e9800998ecf8427e" ] [ "" >byte-array byte-array>md5str ] unit-test
-[ "0cc175b9c0f1b6a831c399e269772661" ] [ "a" >byte-array byte-array>md5str ] unit-test
-[ "900150983cd24fb0d6963f7d28e17f72" ] [ "abc" >byte-array byte-array>md5str ] unit-test
-[ "f96b697d7cb7938d525a2f31aaf161d0" ] [ "message digest" >byte-array byte-array>md5str ] unit-test
-[ "c3fcd3d76192e4007dfb496cca67e13b" ] [ "abcdefghijklmnopqrstuvwxyz" >byte-array byte-array>md5str ] unit-test
-[ "d174ab98d277d9f5a5611c2c9f419d9f" ] [ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" >byte-array byte-array>md5str ] unit-test
-[ "57edf4a22be3c955ac49da2e2107b67a" ] [ "12345678901234567890123456789012345678901234567890123456789012345678901234567890" >byte-array byte-array>md5str ] unit-test
-
diff --git a/extra/csv/csv-tests.factor b/extra/csv/csv-tests.factor
index 6ab26c7e40..7e96dbc0a6 100644
--- a/extra/csv/csv-tests.factor
+++ b/extra/csv/csv-tests.factor
@@ -46,9 +46,7 @@ IN: csv.tests
[ "Year,Make,Model\n1997,Ford,E350\n2000,Mercury,Cougar"
csv ] named-unit-test
-
-
! !!!!!!!! other tests
[ { { "Phil Dawes" } } ]
@@ -65,3 +63,8 @@ IN: csv.tests
"allows setting of delimiting character"
[ { { "foo" "bah" "baz" } } ]
[ "foo\tbah\tbaz\n" CHAR: \t [ csv ] with-delimiter ] named-unit-test
+
+"Quoted field followed immediately by newline"
+[ { { "foo" "bar" }
+ { "1" "2" } } ]
+[ "foo,\"bar\"\n1,2" csv ] named-unit-test
diff --git a/extra/csv/csv.factor b/extra/csv/csv.factor
index 3953ce057b..8ba0832b29 100644
--- a/extra/csv/csv.factor
+++ b/extra/csv/csv.factor
@@ -31,6 +31,7 @@ VAR: delimiter
read1 dup
{ { CHAR: " [ , quoted-field ] } ! " is an escaped quote
{ delimiter> [ ] } ! end of quoted field
+ { CHAR: \n [ ] }
[ 2drop skip-to-field-end ] ! end of quoted field + padding
} case ;
@@ -60,11 +61,11 @@ VAR: delimiter
: csv-row ( stream -- row )
init-vars
- [ row nip ] with-stream ;
+ [ row nip ] with-input-stream ;
: csv ( stream -- rows )
init-vars
- [ [ (csv) ] { } make ] with-stream ;
+ [ [ (csv) ] { } make ] with-input-stream ;
: with-delimiter ( char quot -- )
delimiter swap with-variable ; inline
diff --git a/extra/db/pooling/pooling-tests.factor b/extra/db/pooling/pooling-tests.factor
new file mode 100644
index 0000000000..7b0de65ac4
--- /dev/null
+++ b/extra/db/pooling/pooling-tests.factor
@@ -0,0 +1,8 @@
+IN: db.pooling.tests
+USING: db.pooling tools.test ;
+
+\ must-infer
+
+{ 2 0 } [ [ ] with-db-pool ] must-infer-as
+
+{ 1 0 } [ [ ] with-pooled-connection ] must-infer-as
diff --git a/extra/db/pooling/pooling.factor b/extra/db/pooling/pooling.factor
new file mode 100644
index 0000000000..83820294d6
--- /dev/null
+++ b/extra/db/pooling/pooling.factor
@@ -0,0 +1,43 @@
+! Copyright (C) 2008 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors kernel arrays namespaces sequences continuations
+destructors db ;
+IN: db.pooling
+
+TUPLE: pool db params connections ;
+
+: ( db params -- pool )
+ V{ } clone pool boa ;
+
+M: pool dispose [ dispose-each f ] change-connections drop ;
+
+: with-db-pool ( db params quot -- )
+ >r r> [ pool swap with-variable ] curry with-disposal ; inline
+
+TUPLE: return-connection db pool ;
+
+: return-connection ( db pool -- )
+ connections>> push ;
+
+: new-connection ( pool -- )
+ [ [ db>> ] [ params>> ] bi make-db db-open ] keep
+ return-connection ;
+
+: acquire-connection ( pool -- db )
+ [ dup connections>> empty? ] [ dup new-connection ] [ ] while
+ connections>> pop ;
+
+: (with-pooled-connection) ( db pool quot -- )
+ [ >r drop db r> with-variable ]
+ [ drop return-connection ]
+ 3bi ; inline
+
+: with-pooled-connection ( pool quot -- )
+ >r [ acquire-connection ] keep r>
+ [ (with-pooled-connection) ] [ ] [ 2drop dispose ] cleanup ; inline
+
+M: return-connection dispose
+ [ db>> ] [ pool>> ] bi return-connection ;
+
+: return-connection-later ( db pool -- )
+ \ return-connection boa add-always-destructor ;
diff --git a/extra/delegate/delegate-docs.factor b/extra/delegate/delegate-docs.factor
index f123c3a802..e6a2ad7bf4 100644
--- a/extra/delegate/delegate-docs.factor
+++ b/extra/delegate/delegate-docs.factor
@@ -24,30 +24,17 @@ HELP: CONSULT:
{ define-consult POSTPONE: CONSULT: } related-words
-HELP: define-mimic
-{ $values { "group" "a protocol, generic word or tuple class" } { "mimicker" "a class" } { "mimicked" "a class" } }
-{ $description "For the generic words in the group, the given mimicker copies the methods of the mimicked. This only works for the methods that have already been defined when the word is called." }
-{ $notes "Usually, " { $link POSTPONE: MIMIC: } " should be used instead. This is only for runtime use." } ;
-
-HELP: MIMIC:
-{ $syntax "MIMIC: group mimicker mimicked" }
-{ $values { "group" "a protocol, generic word or tuple class" } { "mimicker" "a class" } { "mimicked" "a class" } }
-{ $description "For the generic words in the group, the given mimicker copies the methods of the mimicked. This only works for the methods that have already been defined when the syntax is used. Mimicking overwrites existing methods." } ;
-
HELP: group-words
{ $values { "group" "a group" } { "words" "an array of words" } }
-{ $description "Given a protocol, generic word or tuple class, this returns the corresponding generic words that this group contains." } ;
+{ $description "Given a protocol or tuple class, this returns the corresponding generic words that this group contains." } ;
ARTICLE: { "delegate" "intro" } "Delegation module"
-"This vocabulary defines methods for consultation and mimicry, independent of the current Factor object system; it is a replacement for Factor's builtin delegation system. Fundamental to the concept of generic word groups, which can be specific protocols, generic words or tuple slot accessors. Fundamentally, a group is a word which has a method for " { $link group-words } ". To define a group as a set of words, use"
+"This vocabulary defines methods for consultation and mimicry, independent of the current Factor object system; it is a replacement for Factor's builtin delegation system. Fundamental to the concept of generic word groups, which can be specific protocols, generic words or tuple slot accessors. Fundamentally, a group is a word which has a method for " { $link group-words } ". One type of group is a tuple, which consists of the slot words. To define a group as a set of words, use"
{ $subsection POSTPONE: PROTOCOL: }
{ $subsection define-protocol }
"One method of object extension which this vocabulary defines is consultation. This is slightly different from the current Factor concept of delegation, in that instead of delegating for all generic words not implemented, only generic words included in a specific group are consulted. Additionally, instead of using a single hard-coded delegate slot, you can specify any quotation to execute in order to retrieve who to consult. The literal syntax and defining word are"
{ $subsection POSTPONE: CONSULT: }
-{ $subsection define-consult }
-"Another object extension mechanism is mimicry. This is the copying of methods in a group from one class to another. For certain applications, this is more appropriate than delegation, as it avoids the slicing problem. It is inappropriate for tuple slots, however. The literal syntax and defining word are"
-{ $subsection POSTPONE: MIMIC: }
-{ $subsection define-mimic } ;
+{ $subsection define-consult } ;
IN: delegate
ABOUT: { "delegate" "intro" }
diff --git a/extra/delegate/delegate-tests.factor b/extra/delegate/delegate-tests.factor
index 5e0abcd5ba..6aa015a74d 100644
--- a/extra/delegate/delegate-tests.factor
+++ b/extra/delegate/delegate-tests.factor
@@ -2,11 +2,6 @@ USING: delegate kernel arrays tools.test words math definitions
compiler.units parser generic prettyprint io.streams.string ;
IN: delegate.tests
-DEFER: example
-[ 1 ] [ \ example 1 "prop" set-word-prop \ example "prop" word-prop ] unit-test
-[ ] [ \ example "prop" [ 1+ ] change-word-prop ] unit-test
-[ 2 ] [ \ example "prop" word-prop ] unit-test
-
TUPLE: hello this that ;
C: hello
@@ -30,21 +25,19 @@ GENERIC: bing ( c -- d )
PROTOCOL: bee bing ;
CONSULT: hello goodbye goodbye-those ;
M: hello bing hello-test ;
-MIMIC: bee goodbye hello
[ 1 { t 1 0 } ] [ 1 0 [ foo ] [ bar ] bi ] unit-test
[ { t 1 0 } ] [ 1 0 bing ] unit-test
[ 1 ] [ 1 0 f foo ] unit-test
[ { t 1 0 } ] [ 1 0 f bar ] unit-test
-! [ { f 1 0 } ] [ f 1 0 bing ] unit-test
[ 3 ] [ 1 0 2 whoa ] unit-test
[ 3 ] [ 1 0 f 2 whoa ] unit-test
[ ] [ 10 [ "USE: delegate IN: delegate.tests CONSULT: baz goodbye goodbye-these ;" eval ] times ] unit-test
-[ V{ goodbye } ] [ baz protocol-users ] unit-test
+[ H{ { goodbye [ goodbye-these ] } } ] [ baz protocol-consult ] unit-test
+[ H{ } ] [ bee protocol-consult ] unit-test
-! [ "USING: delegate ;\nIN: delegate.tests\nPROTOCOL: baz foo bar { whoa 1 } ;\n" ]
-! [ [ baz see ] with-string-writer ] unit-test
+[ "USING: delegate ;\nIN: delegate.tests\nPROTOCOL: baz foo bar { whoa 1 } ;\n" ] [ [ baz see ] with-string-writer ] unit-test
! [ ] [ [ baz forget ] with-compilation-unit ] unit-test
! [ f ] [ goodbye baz method ] unit-test
diff --git a/extra/delegate/delegate.factor b/extra/delegate/delegate.factor
index 0ae8592e66..39eccfd194 100755
--- a/extra/delegate/delegate.factor
+++ b/extra/delegate/delegate.factor
@@ -1,9 +1,44 @@
! Copyright (C) 2007 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
USING: parser generic kernel classes words slots assocs sequences arrays
-vectors definitions prettyprint combinators.lib math sets ;
+vectors definitions prettyprint combinators.lib math hashtables sets ;
IN: delegate
+: protocol-words ( protocol -- words )
+ \ protocol-words word-prop ;
+
+: protocol-consult ( protocol -- consulters )
+ \ protocol-consult word-prop ;
+
+GENERIC: group-words ( group -- words )
+
+M: tuple-class group-words
+ "slot-names" word-prop [
+ [ reader-word ] [ writer-word ] bi
+ 2array [ 0 2array ] map
+ ] map concat ;
+
+! Consultation
+
+: consult-method ( word class quot -- )
+ [ drop swap first create-method ]
+ [ nip swap first2 swapd [ ndip ] 2curry swap suffix ] 3bi define ;
+
+: change-word-prop ( word prop quot -- )
+ rot word-props swap change-at ; inline
+
+: register-protocol ( group class quot -- )
+ rot \ protocol-consult [ swapd ?set-at ] change-word-prop ;
+
+: define-consult ( group class quot -- )
+ [ register-protocol ] [
+ rot group-words -rot
+ [ consult-method ] 2curry each
+ ] 3bi ;
+
+: CONSULT:
+ scan-word scan-word parse-definition define-consult ; parsing
+
! Protocols
: cross-2each ( seq1 seq2 quot -- )
@@ -12,36 +47,46 @@ IN: delegate
: forget-all-methods ( classes words -- )
[ 2array forget ] cross-2each ;
-: protocol-words ( protocol -- words )
- "protocol-words" word-prop ;
-
: protocol-users ( protocol -- users )
- "protocol-users" word-prop ;
+ protocol-consult keys ;
-: users-and-words ( protocol -- users words )
- [ protocol-users ] [ protocol-words ] bi ;
+: lost-words ( protocol wordlist -- lost-words )
+ >r protocol-words r> diff ;
: forget-old-definitions ( protocol new-wordlist -- )
- >r users-and-words r>
+ >r [ protocol-users ] [ protocol-words ] bi r>
swap diff forget-all-methods ;
-: define-protocol ( protocol wordlist -- )
- ! 2dup forget-old-definitions
- { } like "protocol-words" set-word-prop ;
+: added-words ( protocol wordlist -- added-words )
+ swap protocol-words swap diff ;
+
+: add-new-definitions ( protocol wordlist -- )
+ dupd added-words >r protocol-consult >alist r>
+ [ first2 consult-method ] cross-2each ;
+
+: initialize-protocol-props ( protocol wordlist -- )
+ [ drop H{ } clone \ protocol-consult set-word-prop ]
+ [ { } like \ protocol-words set-word-prop ] 2bi ;
: fill-in-depth ( wordlist -- wordlist' )
[ dup word? [ 0 2array ] when ] map ;
+: define-protocol ( protocol wordlist -- )
+ fill-in-depth
+ [ forget-old-definitions ]
+ [ add-new-definitions ]
+ [ initialize-protocol-props ] 2tri ;
+
: PROTOCOL:
CREATE-WORD
- dup define-symbol
- dup f "inline" set-word-prop
- parse-definition fill-in-depth define-protocol ; parsing
+ [ define-symbol ]
+ [ f "inline" set-word-prop ]
+ [ parse-definition define-protocol ] tri ; parsing
PREDICATE: protocol < word protocol-words ; ! Subclass of symbol?
M: protocol forget*
- [ users-and-words forget-all-methods ] [ call-next-method ] bi ;
+ [ f forget-old-definitions ] [ call-next-method ] bi ;
: show-words ( wordlist' -- wordlist )
[ dup second zero? [ first ] when ] map ;
@@ -52,51 +97,4 @@ M: protocol definer drop \ PROTOCOL: \ ; ;
M: protocol synopsis* word-synopsis ; ! Necessary?
-GENERIC: group-words ( group -- words )
-
-M: protocol group-words
- "protocol-words" word-prop ;
-
-M: tuple-class group-words
- "slot-names" word-prop [
- [ reader-word ] [ writer-word ] bi
- 2array [ 0 2array ] map
- ] map concat ;
-
-! Consultation
-
-: define-consult-method ( word class quot -- )
- [ drop swap first create-method ]
- [ nip swap first2 swapd [ ndip ] 2curry swap suffix ] 3bi define ;
-
-: change-word-prop ( word prop quot -- )
- >r swap word-props r> change-at ; inline
-
-: add ( item vector/f -- vector )
- 2dup member? [ nip ] [ ?push ] if ;
-
-: use-protocol ( class group -- )
- "protocol-users" [ add ] change-word-prop ;
-
-: define-consult ( group class quot -- )
- swapd >r 2dup use-protocol group-words swap r>
- [ define-consult-method ] 2curry each ;
-
-: CONSULT:
- scan-word scan-word parse-definition define-consult ; parsing
-
-! Mimic still needs to be updated
-
-: mimic-method ( mimicker mimicked generic -- )
- tuck method
- [ [ create-method-in ] [ word-def ] bi* define ]
- [ 2drop ] if* ;
-
-: define-mimic ( group mimicker mimicked -- )
- [ drop swap use-protocol ] [
- rot group-words -rot
- [ rot first mimic-method ] 2curry each
- ] 3bi ;
-
-: MIMIC:
- scan-word scan-word scan-word define-mimic ; parsing
+M: protocol group-words protocol-words ;
diff --git a/extra/delegate/protocols/protocols.factor b/extra/delegate/protocols/protocols.factor
index f1ad068fe2..c1d7e1e4ab 100755
--- a/extra/delegate/protocols/protocols.factor
+++ b/extra/delegate/protocols/protocols.factor
@@ -1,7 +1,7 @@
! Copyright (C) 2007 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
USING: delegate sequences.private sequences assocs prettyprint.sections
-io definitions kernel continuations ;
+io definitions kernel continuations listener ;
IN: delegate.protocols
PROTOCOL: sequence-protocol
@@ -12,8 +12,10 @@ PROTOCOL: assoc-protocol
at* assoc-size >alist set-at assoc-clone-like { assoc-find 1 }
delete-at clear-assoc new-assoc assoc-like ;
-PROTOCOL: stream-protocol
- stream-read1 stream-read stream-read-until dispose
+PROTOCOL: input-stream-protocol
+ stream-read1 stream-read stream-read-until stream-read-quot ;
+
+PROTOCOL: output-stream-protocol
stream-flush stream-write1 stream-write stream-format
stream-nl make-span-stream make-block-stream stream-readln
make-cell-stream stream-write-table ;
diff --git a/extra/destructors/destructors.factor b/extra/destructors/destructors.factor
index 87b5740786..6fc7ab249f 100755
--- a/extra/destructors/destructors.factor
+++ b/extra/destructors/destructors.factor
@@ -1,7 +1,7 @@
! Copyright (C) 2007 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
-USING: continuations io.backend libc kernel namespaces
-sequences system vectors ;
+USING: continuations io.backend io.nonblocking libc kernel
+namespaces sequences system vectors ;
IN: destructors
SYMBOL: error-destructors
@@ -26,14 +26,11 @@ M: destructor dispose
: add-always-destructor ( obj -- )
always-destructors get push ;
-: dispose-each ( seq -- )
- [ dispose ] each ;
-
: do-always-destructors ( -- )
- always-destructors get dispose-each ;
+ always-destructors get dispose-each ;
: do-error-destructors ( -- )
- error-destructors get dispose-each ;
+ error-destructors get dispose-each ;
: with-destructors ( quot -- )
[
@@ -62,10 +59,8 @@ TUPLE: handle-destructor alien ;
C: handle-destructor
-HOOK: destruct-handle io-backend ( obj -- )
-
M: handle-destructor dispose ( obj -- )
- handle-destructor-alien destruct-handle ;
+ handle-destructor-alien close-handle ;
: close-always ( handle -- )
add-always-destructor ;
diff --git a/extra/editors/jedit/jedit.factor b/extra/editors/jedit/jedit.factor
index e4f19781ef..fe9abc0e76 100755
--- a/extra/editors/jedit/jedit.factor
+++ b/extra/editors/jedit/jedit.factor
@@ -25,11 +25,11 @@ IN: editors.jedit
] with-byte-writer ;
: send-jedit-request ( request -- )
- jedit-server-info "localhost" rot binary [
+ jedit-server-info "localhost" rot binary [
4 >be write
dup length 2 >be write
write
- ] with-stream ;
+ ] with-client ;
: jedit-location ( file line -- )
number>string "+line:" prepend 2array
diff --git a/extra/farkup/farkup.factor b/extra/farkup/farkup.factor
index 527ba8b4fa..15b7b4b72c 100755
--- a/extra/farkup/farkup.factor
+++ b/extra/farkup/farkup.factor
@@ -63,8 +63,14 @@ MEMO: eq ( -- parser )
] with-html-stream
] with-string-writer ;
+: check-url ( href -- href' )
+ CHAR: : over member? [
+ dup { "http://" "https://" "ftp://" } [ head? ] with contains?
+ [ drop "/" ] unless
+ ] when ;
+
: escape-link ( href text -- href-esc text-esc )
- >r escape-quoted-string r> escape-string ;
+ >r check-url escape-quoted-string r> escape-string ;
: make-link ( href text -- seq )
escape-link
diff --git a/extra/fry/fry-docs.factor b/extra/fry/fry-docs.factor
index 84d02d529d..7a444fecbc 100755
--- a/extra/fry/fry-docs.factor
+++ b/extra/fry/fry-docs.factor
@@ -87,7 +87,7 @@ $nl
} ;
ARTICLE: "fry.limitations" "Fried quotation limitations"
-"As with " { $link "locals" } ", fried quotations cannot contain " { $link >r } " and " { $link r> } ". Unlike " { $link "locals" } ", using " { $link dip } " is not a suitable workaround since unlike closure conversion, fry conversion is not recursive, and so the quotation passed to " { $link dip } " cannot contain fry specifiers." ;
+"As with " { $vocab-link "locals" } ", fried quotations cannot contain " { $link >r } " and " { $link r> } "." ;
ARTICLE: "fry" "Fried quotations"
"A " { $emphasis "fried quotation" } " differs from a literal quotation in that when it is evaluated, instead of just pushing itself on the stack, it consumes zero or more stack values and inserts them into the quotation."
diff --git a/extra/fry/fry-tests.factor b/extra/fry/fry-tests.factor
index 7586e254b2..eb59ffae4e 100755
--- a/extra/fry/fry-tests.factor
+++ b/extra/fry/fry-tests.factor
@@ -48,3 +48,7 @@ sequences ;
[ { 1 2 3 } ] [
3 1 '[ , [ , + ] map ] call
] unit-test
+
+[ { 1 { 2 { 3 } } } ] [
+ 1 2 3 '[ , [ , [ , 1array ] call 2array ] call 2array ] call
+] unit-test
diff --git a/extra/fry/fry.factor b/extra/fry/fry.factor
index 1b9e2dc82b..27a321ed92 100755
--- a/extra/fry/fry.factor
+++ b/extra/fry/fry.factor
@@ -54,7 +54,7 @@ DEFER: (shallow-fry)
[ { , namespaces:, @ } member? ] filter length
\ , %
]
- [ deep-fry % ] bi
+ [ fry % ] bi
] [ namespaces:, ] if
] each
] [ ] make deep-fry ;
diff --git a/extra/gap-buffer/tags.txt b/extra/gap-buffer/tags.txt
index fd3a2d285a..b5e4471134 100644
--- a/extra/gap-buffer/tags.txt
+++ b/extra/gap-buffer/tags.txt
@@ -1,2 +1,2 @@
collections
-collections sequences
+sequences
diff --git a/core/io/streams/duplex/authors.txt b/extra/geo-ip/authors.txt
similarity index 100%
rename from core/io/streams/duplex/authors.txt
rename to extra/geo-ip/authors.txt
diff --git a/extra/geo-ip/geo-ip.factor b/extra/geo-ip/geo-ip.factor
new file mode 100644
index 0000000000..5926dd596d
--- /dev/null
+++ b/extra/geo-ip/geo-ip.factor
@@ -0,0 +1,46 @@
+USING: kernel sequences io.files io.launcher io.encodings.ascii
+io.streams.string http.client sequences.lib combinators
+math.parser math.vectors math.intervals interval-maps memoize
+csv accessors assocs strings math splitting ;
+IN: geo-ip
+
+: db-path "IpToCountry.csv" temp-file ;
+
+: db-url "http://software77.net/cgi-bin/ip-country/geo-ip.pl?action=download" ;
+
+: download-db ( -- path )
+ db-path dup exists? [
+ db-url over ".gz" append download-to
+ { "gunzip" } over ".gz" append (normalize-path) suffix try-process
+ ] unless ;
+
+TUPLE: ip-entry from to registry assigned city cntry country ;
+
+: parse-ip-entry ( row -- ip-entry )
+ 7 firstn {
+ [ string>number ]
+ [ string>number ]
+ [ ]
+ [ ]
+ [ ]
+ [ ]
+ [ ]
+ } spread ip-entry boa ;
+
+MEMO: ip-db ( -- seq )
+ download-db ascii file-lines
+ [ "#" head? not ] filter "\n" join csv
+ [ parse-ip-entry ] map ;
+
+MEMO: ip-intervals ( -- interval-map )
+ ip-db [ [ [ from>> ] [ to>> ] bi [a,b] ] keep ] { } map>assoc
+ ;
+
+GENERIC: lookup-ip ( ip -- ip-entry )
+
+M: string lookup-ip
+ "." split [ string>number ] map
+ { HEX: 1000000 HEX: 10000 HEX: 100 1 } v.
+ lookup-ip ;
+
+M: integer lookup-ip ip-intervals interval-at ;
diff --git a/extra/geo-ip/summary.txt b/extra/geo-ip/summary.txt
new file mode 100644
index 0000000000..402d3230f1
--- /dev/null
+++ b/extra/geo-ip/summary.txt
@@ -0,0 +1 @@
+IP address geolocation using database from http://software77.net/cgi-bin/ip-country/
diff --git a/extra/geo-ip/tags.txt b/extra/geo-ip/tags.txt
new file mode 100644
index 0000000000..0aef4feca8
--- /dev/null
+++ b/extra/geo-ip/tags.txt
@@ -0,0 +1 @@
+enterprise
diff --git a/extra/gesture-logger/gesture-logger.factor b/extra/gesture-logger/gesture-logger.factor
index 1977efd3f9..b9de7c1b74 100644
--- a/extra/gesture-logger/gesture-logger.factor
+++ b/extra/gesture-logger/gesture-logger.factor
@@ -15,13 +15,13 @@ TUPLE: gesture-logger stream ;
M: gesture-logger handle-gesture*
drop
dup T{ button-down } = [ over request-focus ] when
- swap gesture-logger-stream [ . ] with-stream*
+ swap gesture-logger-stream [ . ] with-output-stream*
t ;
M: gesture-logger user-input*
gesture-logger-stream [
"User input: " write print
- ] with-stream* t ;
+ ] with-output-stream* t ;
: gesture-logger ( -- )
[
diff --git a/extra/help/cookbook/cookbook.factor b/extra/help/cookbook/cookbook.factor
index 995b8540f5..c2e12469c5 100755
--- a/extra/help/cookbook/cookbook.factor
+++ b/extra/help/cookbook/cookbook.factor
@@ -205,8 +205,8 @@ ARTICLE: "cookbook-io" "Input and output cookbook"
}
"Send some bytes to a remote host:"
{ $code
- "\"myhost\" 1033 "
- "[ { 12 17 102 } >string write ] with-stream"
+ "\"myhost\" 1033 "
+ "[ { 12 17 102 } >string write ] with-client"
}
{ $references
{ }
diff --git a/extra/help/handbook/handbook.factor b/extra/help/handbook/handbook.factor
index ce875b32d1..a8271a0e3b 100755
--- a/extra/help/handbook/handbook.factor
+++ b/extra/help/handbook/handbook.factor
@@ -31,7 +31,7 @@ $nl
{ { $snippet "set-" { $emphasis "foo" } } { "sets " { $snippet "foo" } " to a new value" } { $links set-length } }
{ { $snippet { $emphasis "foo" } "-" { $emphasis "bar" } } { "(tuple accessors) outputs the value of the " { $snippet "bar" } " slot of the " { $snippet "foo" } " at the top of the stack" } { } }
{ { $snippet "set-" { $emphasis "foo" } "-" { $emphasis "bar" } } { "(tuple mutators) sets the value of the " { $snippet "bar" } " slot of the " { $snippet "foo" } " at the top of the stack" } { } }
- { { $snippet "with-" { $emphasis "foo" } } { "performs some kind of initialization and cleanup related to " { $snippet "foo" } ", usually in a new dynamic scope" } { $links with-scope with-stream } }
+ { { $snippet "with-" { $emphasis "foo" } } { "performs some kind of initialization and cleanup related to " { $snippet "foo" } ", usually in a new dynamic scope" } { $links with-scope with-input-stream with-output-stream } }
{ { $snippet "$" { $emphasis "foo" } } { "help markup" } { $links $heading $emphasis } }
}
{ $heading "Stack effect conventions" }
@@ -193,18 +193,21 @@ ARTICLE: "io" "Input and output"
"Utilities:"
{ $subsection "stream-binary" }
{ $subsection "styles" }
-{ $heading "Files" }
-{ $subsection "io.files" }
-{ $subsection "io.mmap" }
-{ $subsection "io.monitors" }
{ $heading "Encodings" }
{ $subsection "encodings-introduction" }
{ $subsection "io.encodings" }
{ $subsection "io.encodings.string" }
-{ $heading "Other features" }
+{ $heading "Files" }
+{ $subsection "io.files" }
+{ $subsection "io.mmap" }
+{ $subsection "io.monitors" }
+{ $heading "Communications" }
{ $subsection "network-streams" }
{ $subsection "io.launcher" }
-{ $subsection "io.timeouts" } ;
+{ $subsection "io.pipes" }
+{ $heading "Other features" }
+{ $subsection "io.timeouts" }
+{ $subsection "checksums" } ;
ARTICLE: "tools" "Developer tools"
{ $subsection "tools.vocabs" }
diff --git a/extra/help/help-docs.factor b/extra/help/help-docs.factor
index d4981751e2..f20ce89263 100755
--- a/extra/help/help-docs.factor
+++ b/extra/help/help-docs.factor
@@ -126,7 +126,7 @@ HELP: $title
HELP: help
{ $values { "topic" "an article name or a word" } }
{ $description
- "Displays a help article or documentation associated to a word on the " { $link stdio } " stream."
+ "Displays a help article or documentation associated to a word on " { $link output-stream } "."
} ;
HELP: about
@@ -151,7 +151,7 @@ HELP: $index
HELP: ($index)
{ $values { "articles" "a sequence of help articles" } }
-{ $description "Writes a list of " { $link $subsection } " elements to the " { $link stdio } " stream." } ;
+{ $description "Writes a list of " { $link $subsection } " elements to " { $link output-stream } "." } ;
HELP: xref-help
{ $description "Update help cross-referencing. Usually this is done automatically." } ;
@@ -168,11 +168,11 @@ HELP: $predicate
HELP: print-element
{ $values { "element" "a markup element" } }
-{ $description "Prints a markup element to the " { $link stdio } " stream." } ;
+{ $description "Prints a markup element to " { $link output-stream } "." } ;
HELP: print-content
{ $values { "element" "a markup element" } }
-{ $description "Prints a top-level markup element to the " { $link stdio } " stream." } ;
+{ $description "Prints a top-level markup element to " { $link output-stream } "." } ;
HELP: simple-element
{ $class-description "Class of simple elements, which are just arrays of elements." } ;
diff --git a/extra/help/lint/lint.factor b/extra/help/lint/lint.factor
index fc4b7f6f25..a9ec7f9267 100755
--- a/extra/help/lint/lint.factor
+++ b/extra/help/lint/lint.factor
@@ -10,7 +10,7 @@ IN: help.lint
: check-example ( element -- )
rest [
- 1 head* "\n" join 1vector
+ but-last "\n" join 1vector
[
use [ clone ] change
[ eval>string ] with-datastack
diff --git a/extra/help/tutorial/tutorial.factor b/extra/help/tutorial/tutorial.factor
index fffcda69b6..cafa758c7e 100755
--- a/extra/help/tutorial/tutorial.factor
+++ b/extra/help/tutorial/tutorial.factor
@@ -135,7 +135,7 @@ $nl
{ $code "[ Letter? ] filter >lower" }
"This code starts with a string on the stack, removes non-alphabetical characters, and converts the result to lower case, leaving a new string on the stack. We put this code in a new word, and add the new word to " { $snippet "palindrome.factor" } ":"
{ $code ": normalize ( str -- newstr ) [ Letter? ] filter >lower ;" }
-"You will need to add " { $vocab-link "unicode.categories" } " to the vocabulary search path, so that " { $link Letter? } " can be used in the source file."
+"You will need to add " { $vocab-link "unicode.case" } " and " { $vocab-link "unicode.categories" } " to the vocabulary search path, so that " { $link Letter? } " can be used in the source file."
$nl
"We modify " { $snippet "palindrome?" } " to first apply " { $snippet "normalize" } " to its input:"
{ $code ": palindrome? ( str -- ? ) normalize dup reverse = ;" }
diff --git a/extra/html/elements/elements.factor b/extra/html/elements/elements.factor
index 41e29fc712..49782fa305 100644
--- a/extra/html/elements/elements.factor
+++ b/extra/html/elements/elements.factor
@@ -143,7 +143,7 @@ SYMBOL: html
"h1" "h2" "h3" "h4" "h5" "h6" "h7" "h8" "h9"
"ol" "li" "form" "a" "p" "html" "head" "body" "title"
"b" "i" "ul" "table" "tbody" "tr" "td" "th" "pre" "textarea"
- "script" "div" "span" "select" "option" "style"
+ "script" "div" "span" "select" "option" "style" "input"
] [ define-closed-html-word ] each
! Define some open HTML tags
@@ -161,6 +161,6 @@ SYMBOL: html
"id" "onclick" "style" "valign" "accesskey"
"src" "language" "colspan" "onchange" "rel"
"width" "selected" "onsubmit" "xmlns" "lang" "xml:lang"
- "media" "title"
+ "media" "title" "multiple"
] [ define-attribute-word ] each
] with-compilation-unit
diff --git a/extra/html/html-tests.factor b/extra/html/html-tests.factor
index ce320ca75b..9f1ce6b689 100644
--- a/extra/html/html-tests.factor
+++ b/extra/html/html-tests.factor
@@ -24,7 +24,7 @@ IN: html.tests
] unit-test
[ "<" ] [
- [ "<" H{ } stdio get format-html-span ] make-html-string
+ [ "<" H{ } output-stream get format-html-span ] make-html-string
] unit-test
TUPLE: funky town ;
diff --git a/extra/html/html.factor b/extra/html/html.factor
index f0ae424760..c154c35223 100755
--- a/extra/html/html.factor
+++ b/extra/html/html.factor
@@ -44,7 +44,7 @@ TUPLE: html-sub-stream style stream ;
rot html-sub-stream-stream ;
: delegate-write ( string -- )
- stdio get delegate stream-write ;
+ output-stream get delegate stream-write ;
: object-link-tag ( style quot -- )
presented pick at [
@@ -101,7 +101,7 @@ TUPLE: html-sub-stream style stream ;
: format-html-span ( string style stream -- )
[
[ [ drop delegate-write ] span-tag ] object-link-tag
- ] with-stream* ;
+ ] with-output-stream* ;
TUPLE: html-span-stream ;
@@ -134,7 +134,7 @@ M: html-span-stream dispose
: format-html-div ( string style stream -- )
[
[ [ delegate-write ] div-tag ] object-link-tag
- ] with-stream* ;
+ ] with-output-stream* ;
TUPLE: html-block-stream ;
@@ -184,17 +184,17 @@ M: html-stream stream-write-table ( grid style stream -- )
] with each
] with each
- ] with-stream* ;
+ ] with-output-stream* ;
M: html-stream make-cell-stream ( style stream -- stream' )
(html-sub-stream) ;
M: html-stream stream-nl ( stream -- )
- dup test-last-div? [ drop ] [ [
] with-stream* ] if ;
+ dup test-last-div? [ drop ] [ [
] with-output-stream* ] if ;
! Utilities
: with-html-stream ( quot -- )
- stdio get swap with-stream* ; inline
+ output-stream get swap with-output-stream* ; inline
: xhtml-preamble
"" write-html
@@ -225,13 +225,13 @@ M: html-stream stream-nl ( stream -- )
: vertical-layout ( list -- )
#! Given a list of HTML components, arrange them vertically.
-
+ ;
: horizontal-layout ( list -- )
#! Given a list of HTML components, arrange them horizontally.
-
+ ;
@@ -246,8 +246,8 @@ M: html-stream stream-nl ( stream -- )
: simple-page ( title quot -- )
#! Call the quotation, with all output going to the
#! body of an html page with the given title.
-
- swap write
+
+ swap write
call
;
@@ -255,10 +255,13 @@ M: html-stream stream-nl ( stream -- )
#! Call the quotation, with all output going to the
#! body of an html page with the given title. stylesheet-quot
#! is called to generate the required stylesheet.
-
-
- rot write
- swap call
-
+
+
+ rot write
+ swap call
+
call
;
+
+: render-error ( message -- )
+ escape-string write ;
diff --git a/extra/html/parser/analyzer/analyzer.factor b/extra/html/parser/analyzer/analyzer.factor
index 160b95ab1d..e9906f3f2a 100755
--- a/extra/html/parser/analyzer/analyzer.factor
+++ b/extra/html/parser/analyzer/analyzer.factor
@@ -99,7 +99,7 @@ IN: html.parser.analyzer
: find-between ( i/f tag/f vector -- vector )
find-between* dup length 3 >= [
- [ rest-slice 1 head-slice* ] keep like
+ [ rest-slice but-last-slice ] keep like
] when ;
: find-between-first ( string vector -- vector' )
diff --git a/extra/html/parser/utils/utils.factor b/extra/html/parser/utils/utils.factor
index 0ae75e41fd..5083b1cec2 100644
--- a/extra/html/parser/utils/utils.factor
+++ b/extra/html/parser/utils/utils.factor
@@ -36,7 +36,7 @@ IN: html.parser.utils
dup quoted? [ quote ] unless ;
: unquote ( str -- newstr )
- dup quoted? [ 1 head-slice* rest-slice >string ] when ;
+ dup quoted? [ but-last-slice rest-slice >string ] when ;
: quote? ( ch -- ? ) "'\"" member? ;
diff --git a/extra/http/client/client.factor b/extra/http/client/client.factor
index 7762b01843..17882277a3 100755
--- a/extra/http/client/client.factor
+++ b/extra/http/client/client.factor
@@ -3,7 +3,8 @@
USING: assocs http kernel math math.parser namespaces sequences
io io.sockets io.streams.string io.files io.timeouts strings
splitting calendar continuations accessors vectors math.order
-io.encodings.8-bit io.encodings.binary fry debugger inspector ;
+io.encodings.8-bit io.encodings.binary io.streams.duplex
+fry debugger inspector ;
IN: http.client
: max-redirects 10 ;
@@ -26,73 +27,56 @@ DEFER: http-request
: store-path ( request path -- request )
"?" split1 >r >>path r> dup [ query>assoc ] when >>query ;
-: request-with-url ( url request -- request )
- swap parse-url >r >r store-path r> >>host r> >>port ;
-
-! This is all pretty complex because it needs to handle
-! HTTP redirects, which might be absolute or relative
-: absolute-redirect ( url -- request )
- request get request-with-url ;
-
-: relative-redirect ( path -- request )
- request get swap store-path ;
+: request-with-url ( request url -- request )
+ parse-url >r >r store-path r> >>host r> >>port ;
SYMBOL: redirects
: absolute-url? ( url -- ? )
[ "http://" head? ] [ "https://" head? ] bi or ;
-: do-redirect ( response -- response stream )
- dup response-code 300 399 between? [
- stdio get dispose
+: do-redirect ( response data -- response data )
+ over code>> 300 399 between? [
+ drop
redirects inc
redirects get max-redirects < [
- header>> "location" swap at
- dup absolute-url? [
- absolute-redirect
- ] [
- relative-redirect
- ] if "GET" >>method http-request
+ request get
+ swap "location" header dup absolute-url?
+ [ request-with-url ] [ store-path ] if
+ "GET" >>method http-request
] [
too-many-redirects
] if
- ] [
- stdio get
- ] if ;
-
-: close-on-error ( stream quot -- )
- '[ , with-stream* ] [ ] pick '[ , dispose ] cleanup ; inline
+ ] when ;
PRIVATE>
-: http-request ( request -- response stream )
- dup request [
- dup request-addr latin1
- 1 minutes over set-timeout
- [
- write-request flush
- read-response
- do-redirect
- ] close-on-error
- ] with-variable ;
-
: read-chunks ( -- )
read-crlf ";" split1 drop hex> dup { f 0 } member?
[ drop ] [ read % read-crlf "" assert= read-chunks ] if ;
-: do-chunked-encoding ( response stream -- response stream/string )
- over "transfer-encoding" header "chunked" = [
- [ [ read-chunks ] "" make ] with-stream
- ] when ;
+: read-response-body ( response -- response data )
+ dup "transfer-encoding" header "chunked" =
+ [ [ read-chunks ] "" make ] [ input-stream get contents ] if ;
+
+: http-request ( request -- response data )
+ dup request [
+ dup request-addr latin1 [
+ 1 minutes timeouts
+ write-request
+ read-response
+ read-response-body
+ ] with-client
+ do-redirect
+ ] with-variable ;
: ( url -- request )
- request-with-url "GET" >>method ;
+
+ swap request-with-url
+ "GET" >>method ;
-: string-or-contents ( stream/string -- string )
- dup string? [ contents ] unless ;
-
-: http-get-stream ( url -- response stream/string )
- http-request do-chunked-encoding ;
+: http-get* ( url -- response data )
+ http-request ;
: success? ( code -- ? ) 200 = ;
@@ -112,29 +96,24 @@ M: download-failed error.
over code>> success? [ nip ] [ download-failed ] if ;
: http-get ( url -- string )
- http-get-stream string-or-contents check-response ;
+ http-get* check-response ;
: download-name ( url -- name )
file-name "?" split1 drop "/" ?tail drop ;
: download-to ( url file -- )
#! Downloads the contents of a URL to a file.
- swap http-get-stream check-response
- dup string? [
- latin1 [ write ] with-file-writer
- ] [
- [ swap latin1 stream-copy ] with-disposal
- ] if ;
+ >r http-get r> latin1 [ write ] with-file-writer ;
: download ( url -- )
dup download-name download-to ;
: ( content-type content url -- request )
- request-with-url
- "POST" >>method
- swap >>post-data
- swap >>post-data-type ;
+ "POST" >>method
+ swap request-with-url
+ swap >>post-data
+ swap >>post-data-type ;
-: http-post ( content-type content url -- response string )
- http-request do-chunked-encoding string-or-contents ;
+: http-post ( content-type content url -- response data )
+ http-request ;
diff --git a/extra/http/http-tests.factor b/extra/http/http-tests.factor
index 39e708c879..21eb241b84 100755
--- a/extra/http/http-tests.factor
+++ b/extra/http/http-tests.factor
@@ -1,6 +1,6 @@
USING: http tools.test multiline tuple-syntax
io.streams.string kernel arrays splitting sequences
-assocs io.sockets db db.sqlite ;
+assocs io.sockets db db.sqlite continuations ;
IN: http.tests
[ "hello%20world" ] [ "hello world" url-encode ] unit-test
@@ -24,6 +24,14 @@ IN: http.tests
[ "/bar" ] [ "http://foo.com/bar" url>path ] unit-test
[ "/bar" ] [ "/bar" url>path ] unit-test
+[ "a=b&a=c" ] [ { { "a" { "b" "c" } } } assoc>query ] unit-test
+
+[ H{ { "a" "b" } } ] [ "a=b" query>assoc ] unit-test
+
+[ H{ { "a" { "b" "c" } } } ] [ "a=b&a=c" query>assoc ] unit-test
+
+[ "a=3" ] [ { { "a" 3 } } assoc>query ] unit-test
+
: lf>crlf "\n" split "\r\n" join ;
STRING: read-request-test-1
@@ -93,7 +101,7 @@ Host: www.sex.com
STRING: read-response-test-1
HTTP/1.1 404 not found
-Content-Type: text/html
+Content-Type: text/html; charset=UTF8
blah
;
@@ -103,8 +111,10 @@ blah
version: "1.1"
code: 404
message: "not found"
- header: H{ { "content-type" "text/html" } }
+ header: H{ { "content-type" "text/html; charset=UTF8" } }
cookies: V{ }
+ content-type: "text/html"
+ content-charset: "UTF8"
}
] [
read-response-test-1 lf>crlf
@@ -114,7 +124,7 @@ blah
STRING: read-response-test-1'
HTTP/1.1 404 not found
-content-type: text/html
+content-type: text/html; charset=UTF8
;
@@ -140,11 +150,13 @@ accessors namespaces threads ;
: add-quit-action