From d56acaab86caf7a45ecdf48163863dd9cc58e08d Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 29 Jan 2008 20:50:15 -0800 Subject: [PATCH 001/269] Fix broken opengl shader words --- extra/opengl/opengl.factor | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/extra/opengl/opengl.factor b/extra/opengl/opengl.factor index 4ea91b867b..2c1d4de75c 100755 --- a/extra/opengl/opengl.factor +++ b/extra/opengl/opengl.factor @@ -255,7 +255,7 @@ TUPLE: sprite loc dim dim2 dlist texture ; : c-true? ( int -- ? ) zero? not ; inline : with-gl-shader-source-ptr ( string quot -- ) - swap >byte-array malloc-byte-array [ + swap string>char-alien malloc-byte-array [ swap call ] keep free ; inline @@ -294,9 +294,8 @@ TUPLE: sprite loc dim dim2 dlist texture ; GL_INFO_LOG_LENGTH gl-shader-get-int ; inline : gl-shader-info-log ( shader -- log ) - dup gl-shader-info-log-length - dup [ - 0 over glGetShaderInfoLog + dup gl-shader-info-log-length dup [ + [ 0 swap glGetShaderInfoLog ] keep alien>char-string ] with-malloc ; @@ -330,9 +329,10 @@ PREDICATE: gl-shader fragment-shader (fragment-shader?) ; GL_INFO_LOG_LENGTH gl-program-get-int ; inline : gl-program-info-log ( program -- log ) - dup gl-program-info-log-length - dup [ [ 0 swap glGetProgramInfoLog ] keep - alien>char-string ] with-malloc ; + dup gl-program-info-log-length dup [ + [ 0 swap glGetProgramInfoLog ] keep + alien>char-string + ] with-malloc ; : check-gl-program ( program -- program* ) dup gl-program-ok? [ dup gl-program-info-log throw ] unless ; @@ -342,7 +342,8 @@ PREDICATE: gl-shader fragment-shader (fragment-shader?) ; : gl-program-shaders ( program -- shaders ) dup gl-program-shaders-length [ - dup "GLuint" 0 over glGetAttachedShaders + dup "GLuint" + [ 0 swap glGetAttachedShaders ] keep ] keep c-uint-array> ; : delete-gl-program-only ( program -- ) From 6394eb70bf82005eff70258aa91f133eea7ef10c Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Wed, 30 Jan 2008 00:50:18 -0500 Subject: [PATCH 002/269] Solution to Project Euler problem 37 --- extra/project-euler/037/037.factor | 52 ++++++++++++++++++++++++ extra/project-euler/project-euler.factor | 4 +- 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 extra/project-euler/037/037.factor diff --git a/extra/project-euler/037/037.factor b/extra/project-euler/037/037.factor new file mode 100644 index 0000000000..f2d5d17c4d --- /dev/null +++ b/extra/project-euler/037/037.factor @@ -0,0 +1,52 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel math math.parser math.primes sequences ; +IN: project-euler.037 + +! http://projecteuler.net/index.php?section=problems&id=37 + +! DESCRIPTION +! ----------- + +! The number 3797 has an interesting property. Being prime itself, it is +! possible to continuously remove digits from left to right, and remain prime +! at each stage: 3797, 797, 97, and 7. Similarly we can work from right to +! left: 3797, 379, 37, and 3. + +! Find the sum of the only eleven primes that are both truncatable from left to +! right and right to left. + +! NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. + + +! SOLUTION +! -------- + + [ + dup prime? [ r-trunc? ] [ drop f ] if + ] [ + drop t + ] if ; + +: reverse-digits ( n -- m ) + number>string reverse 10 string>integer ; + +: l-trunc? ( n -- ? ) + reverse-digits 10 /i reverse-digits dup 0 > [ + dup prime? [ l-trunc? ] [ drop f ] if + ] [ + drop t + ] if ; + +PRIVATE> + +: euler037 ( -- answer ) + 23 1000000 primes-between [ r-trunc? ] subset [ l-trunc? ] subset sum ; + +! [ euler037 ] 100 ave-time +! 768 ms run / 9 ms GC ave time - 100 trials + +MAIN: euler037 diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index feef9dbfa8..fbb62961a9 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -11,8 +11,8 @@ USING: definitions io io.files kernel math.parser sequences vocabs project-euler.025 project-euler.026 project-euler.027 project-euler.028 project-euler.029 project-euler.030 project-euler.031 project-euler.032 project-euler.033 project-euler.034 project-euler.035 project-euler.036 - project-euler.067 project-euler.134 project-euler.169 project-euler.173 - project-euler.175 ; + project-euler.037 project-euler.067 project-euler.134 project-euler.169 + project-euler.173 project-euler.175 ; IN: project-euler Date: Tue, 29 Jan 2008 22:01:06 -0800 Subject: [PATCH 003/269] Add with-software-renderer combinator to cocoa.views . Hack up shader code in line-art to make it display properly with the Apple software OpenGL implementation --- extra/cocoa/views/views.factor | 15 ++++++++- extra/line-art/line-art.factor | 56 +++++++++++++++++----------------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/extra/cocoa/views/views.factor b/extra/cocoa/views/views.factor index cc948df55f..7b8de9067c 100644 --- a/extra/cocoa/views/views.factor +++ b/extra/cocoa/views/views.factor @@ -1,7 +1,8 @@ ! Copyright (C) 2006, 2007 Slava Pestov ! See http://factorcode.org/license.txt for BSD license. USING: alien.c-types arrays kernel math namespaces cocoa -cocoa.messages cocoa.classes cocoa.types sequences ; +cocoa.messages cocoa.classes cocoa.types sequences +continuations ; IN: cocoa.views : NSOpenGLPFAAllRenderers 1 ; @@ -35,11 +36,23 @@ IN: cocoa.views : NSOpenGLPFAPixelBuffer 90 ; : NSOpenGLPFAVirtualScreenCount 128 ; + + +: with-software-renderer ( quot -- ) + t +software-renderer+ set + [ f +software-renderer+ set ] + [ ] cleanup ; inline + : ( -- pixelfmt ) NSOpenGLPixelFormat -> alloc [ NSOpenGLPFAWindow , NSOpenGLPFADoubleBuffer , NSOpenGLPFADepthSize , 16 , + +software-renderer+ get [ NSOpenGLPFARobust , ] when 0 , ] { } make >c-int-array -> initWithAttributes: diff --git a/extra/line-art/line-art.factor b/extra/line-art/line-art.factor index 1a0ae6993f..9856921a51 100644 --- a/extra/line-art/line-art.factor +++ b/extra/line-art/line-art.factor @@ -57,13 +57,7 @@ uniform sampler2D colormap, normalmap, depthmap; uniform vec4 line_color; varying vec2 coord; -const float DEPTH_RATIO_THRESHOLD = 1.001, NORMAL_DOT_THRESHOLD = 1.0, SAMPLE_SPREAD = 1.0/512.0; - -bool -is_normal_border(vec3 norm1, vec3 norm2) -{ - return dot(norm1, norm2) < NORMAL_DOT_THRESHOLD; -} +const float DEPTH_RATIO_THRESHOLD = 1.001, SAMPLE_SPREAD = 1.0/512.0; float depth_sample(vec2 c) @@ -97,33 +91,39 @@ border_factor(vec2 c) coord3 = c + vec2(-SAMPLE_SPREAD, SAMPLE_SPREAD), coord4 = c + vec2( SAMPLE_SPREAD, SAMPLE_SPREAD); - vec4 depths = vec4(depth_sample(coord1), - depth_sample(coord2), - depth_sample(coord3), - depth_sample(coord4)); - if (depths == vec4(1, 1, 1, 1)) - return 0.0; - - vec3 ratios1 = depths.xxx/depths.yzw, ratios2 = depths.yyz/depths.zww; - - if (are_depths_border(ratios1) || are_depths_border(ratios2)) - return 1.0; - vec3 normal1 = normal_sample(coord1), normal2 = normal_sample(coord2), normal3 = normal_sample(coord3), normal4 = normal_sample(coord4); + + if (dot(normal1, normal1) < 0.5 + && dot(normal2, normal2) < 0.5 + && dot(normal3, normal3) < 0.5 + && dot(normal4, normal4) < 0.5) { + return 0.0; + } else { + vec4 depths = vec4(depth_sample(coord1), + depth_sample(coord2), + depth_sample(coord3), + depth_sample(coord4)); - float normal_border = 1.0 - min6( - dot(normal1, normal2), - dot(normal1, normal3), - dot(normal1, normal4), - dot(normal2, normal3), - dot(normal2, normal4), - dot(normal3, normal4) - ); + vec3 ratios1 = depths.xxx/depths.yzw, ratios2 = depths.yyz/depths.zww; - return normal_border; + if (are_depths_border(ratios1) || are_depths_border(ratios2)) { + return 1.0; + } else { + float normal_border = 1.0 - min6( + dot(normal1, normal2), + dot(normal1, normal3), + dot(normal1, normal4), + dot(normal2, normal3), + dot(normal2, normal4), + dot(normal3, normal4) + ); + + return normal_border; + } + } } void From fd4254ca094f0e8d6134e02f87c661899a98145e Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 31 Jan 2008 11:34:03 -0600 Subject: [PATCH 004/269] update client to work with more redirects --- extra/http/client/client.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/http/client/client.factor b/extra/http/client/client.factor index 7c385c0bb3..85a8b516ca 100644 --- a/extra/http/client/client.factor +++ b/extra/http/client/client.factor @@ -44,7 +44,7 @@ DEFER: http-get-stream #! Should this support Location: headers that are #! relative URLs? pick 100 /i 3 = [ - stream-close "Location" swap at nip http-get-stream + stream-close "location" swap header-single nip http-get-stream ] when ; : http-get-stream ( url -- code headers stream ) From 7666949e13397dda79dda6ddb6d68d976119ca98 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 31 Jan 2008 20:22:19 -0800 Subject: [PATCH 005/269] byte-length generic word for determining alien buffer sizes of byte-arrays and float-arrays --- core/alien/alien-docs.factor | 4 ++++ core/alien/alien.factor | 2 ++ core/byte-arrays/byte-arrays.factor | 3 +++ core/float-arrays/float-arrays-docs.factor | 2 +- core/float-arrays/float-arrays.factor | 3 ++- 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/core/alien/alien-docs.factor b/core/alien/alien-docs.factor index 8fee0e8c3e..8ae89ed5b1 100755 --- a/core/alien/alien-docs.factor +++ b/core/alien/alien-docs.factor @@ -34,6 +34,10 @@ HELP: { $description "Creates an alien object, wrapping a raw memory address." } { $notes "Alien objects are invalidated between image saves and loads." } ; +HELP: byte-length +{ $values { "seq" "A byte array or float array" } { "n" "a non-negative integer" } } +{ $contract "Outputs the size of the byte array or float array data in bytes as presented to the C library interface." } ; + HELP: c-ptr { $class-description "Class of objects consisting of aliens, byte arrays and " { $link f } ". These objects can convert to pointer C types, which are all aliases of " { $snippet "void*" } "." } ; diff --git a/core/alien/alien.factor b/core/alien/alien.factor index 1c8163e2fa..4b899a15e4 100755 --- a/core/alien/alien.factor +++ b/core/alien/alien.factor @@ -28,6 +28,8 @@ PREDICATE: alien pinned-alien UNION: pinned-c-ptr pinned-alien POSTPONE: f ; +GENERIC: byte-length ( seq -- n ) flushable + M: f expired? drop t ; : ( address -- alien ) diff --git a/core/byte-arrays/byte-arrays.factor b/core/byte-arrays/byte-arrays.factor index f82569c270..d65f243d71 100755 --- a/core/byte-arrays/byte-arrays.factor +++ b/core/byte-arrays/byte-arrays.factor @@ -15,6 +15,9 @@ M: byte-array new drop ; M: byte-array equal? over byte-array? [ sequence= ] [ 2drop f ] if ; +M: byte-array byte-length + length ; + INSTANCE: byte-array sequence INSTANCE: byte-array simple-c-ptr INSTANCE: byte-array c-ptr diff --git a/core/float-arrays/float-arrays-docs.factor b/core/float-arrays/float-arrays-docs.factor index 70bbfe296f..cb36aade6b 100644 --- a/core/float-arrays/float-arrays-docs.factor +++ b/core/float-arrays/float-arrays-docs.factor @@ -32,7 +32,7 @@ HELP: ( n initial -- float-array ) HELP: >float-array { $values { "seq" "a sequence" } { "float-array" float-array } } -{ $description "Outputs a freshly-allocated float array whose elements have the same boolean values as a given sequence." } +{ $description "Outputs a freshly-allocated float array whose elements have the same floating-point values as a given sequence." } { $errors "Throws an error if the sequence contains elements other than real numbers." } ; HELP: 1float-array diff --git a/core/float-arrays/float-arrays.factor b/core/float-arrays/float-arrays.factor index ba0b2bb61d..42a2db7cd8 100755 --- a/core/float-arrays/float-arrays.factor +++ b/core/float-arrays/float-arrays.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2007, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: kernel kernel.private alien sequences +USING: kernel kernel.private alien alien.c-types sequences sequences.private math math.private ; IN: float-arrays @@ -12,6 +12,7 @@ PRIVATE> M: float-array clone (clone) ; M: float-array length array-capacity ; +M: float-array byte-length array-capacity "float" heap-size * ; M: float-array nth-unsafe float-array@ alien-double ; From f3b9e889ff6a41ec68937ab294f1b94b12012270 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 31 Jan 2008 20:24:08 -0800 Subject: [PATCH 006/269] Start work on making bunny demos use vertex buffers to draw --- extra/bunny/bunny.factor | 13 ++++++- extra/cel-shading/cel-shading.factor | 54 ++++++++++++++++++++-------- extra/line-art/line-art.factor | 5 ++- extra/opengl/opengl-docs.factor | 6 ++-- extra/opengl/opengl.factor | 28 +++++++++++++-- 5 files changed, 83 insertions(+), 23 deletions(-) diff --git a/extra/bunny/bunny.factor b/extra/bunny/bunny.factor index 3042b87ad6..73a3efc742 100644 --- a/extra/bunny/bunny.factor +++ b/extra/bunny/bunny.factor @@ -3,7 +3,8 @@ USING: alien alien.c-types arrays sequences math math.vectors math.matrices math.parser io io.files kernel opengl opengl.gl opengl.glu shuffle http.client vectors timers namespaces ui.gadgets ui.gadgets.canvas ui.render ui splitting -combinators tools.time system combinators.lib ; +combinators tools.time system combinators.lib combinators.cleave +float-arrays ; IN: bunny : numbers ( str -- seq ) @@ -45,6 +46,16 @@ IN: bunny parse-model [ normals ] 2keep 3array ] time ; +: make-vertex-buffers ( model -- array element-array ) + [ + [ first concat ] [ second concat ] bi + append >float-array + GL_ARRAY_BUFFER swap GL_STATIC_DRAW + ] [ + third concat >c-uint-array + GL_ELEMENT_ARRAY_BUFFER swap GL_STATIC_DRAW + ] bi ; + : model-path "bun_zipper.ply" ; : model-url "http://factorcode.org/bun_zipper.ply" ; diff --git a/extra/cel-shading/cel-shading.factor b/extra/cel-shading/cel-shading.factor index 64d23275e9..992fd9655d 100644 --- a/extra/cel-shading/cel-shading.factor +++ b/extra/cel-shading/cel-shading.factor @@ -4,12 +4,14 @@ USING: arrays bunny combinators.lib io io.files kernel sequences ui ui.gadgets ui.render ; IN: cel-shading -TUPLE: cel-shading-gadget model program ; +TUPLE: cel-shading-gadget model program vertices elements ; : ( -- cel-shading-gadget ) 0.0 0.0 0.375 - maybe-download read-model - { set-delegate set-cel-shading-gadget-model } cel-shading-gadget construct ; + maybe-download read-model { + set-delegate + set-cel-shading-gadget-model + } cel-shading-gadget construct ; STRING: cel-shading-vertex-shader-source varying vec3 position, normal; @@ -53,34 +55,58 @@ main() ; -: cel-shading-program ( -- program ) +: make-cel-shading-program ( -- program ) cel-shading-vertex-shader-source cel-shading-fragment-shader-source ; M: cel-shading-gadget graft* ( gadget -- ) - [ "2.0" { "GL_ARB_shader_objects" } require-gl-version-or-extensions + "2.0" { + "GL_ARB_shader_objects" + "GL_ARB_vertex_buffer_object" + } require-gl-version-or-extensions 0.0 0.0 0.0 1.0 glClearColor GL_CULL_FACE glEnable GL_DEPTH_TEST glEnable - cel-shading-program swap set-cel-shading-gadget-program ] [ ] [ :c ] cleanup ; + dup cel-shading-gadget-model make-vertex-buffers + make-cel-shading-program roll { + set-cel-shading-gadget-vertices + set-cel-shading-gadget-elements + set-cel-shading-gadget-program + } set-slots ; M: cel-shading-gadget ungraft* ( gadget -- ) - cel-shading-gadget-program [ delete-gl-program ] when* ; + { + [ cel-shading-gadget-program [ delete-gl-program ] when* ] + [ cel-shading-gadget-elements [ delete-gl-buffer ] when* ] + [ cel-shading-gadget-vertices [ delete-gl-buffer ] when* ] + } call-with ; : cel-shading-draw-setup ( gadget -- gadget ) [ demo-gadget-set-matrices ] keep - [ cel-shading-gadget-program - { [ "light_direction" glGetUniformLocation -25.0 45.0 80.0 glUniform3f ] - [ "color" glGetUniformLocation 0.6 0.5 0.5 1.0 glUniform4f ] - [ "ambient" glGetUniformLocation 0.2 0.2 0.2 0.2 glUniform4f ] - [ "diffuse" glGetUniformLocation 0.8 0.8 0.8 0.8 glUniform4f ] } call-with - ] keep ; + [ cel-shading-gadget-program { + [ "light_direction" glGetUniformLocation -25.0 45.0 80.0 glUniform3f ] + [ "color" glGetUniformLocation 0.6 0.5 0.5 1.0 glUniform4f ] + [ "ambient" glGetUniformLocation 0.2 0.2 0.2 0.2 glUniform4f ] + [ "diffuse" glGetUniformLocation 0.8 0.8 0.8 0.8 glUniform4f ] + } call-with ] keep ; M: cel-shading-gadget draw-gadget* ( gadget -- ) dup cel-shading-gadget-program [ cel-shading-draw-setup 0.0 -0.12 0.0 glTranslatef - cel-shading-gadget-model first3 draw-bunny + dup { + cel-shading-gadget-vertices + cel-shading-gadget-elements + } get-slots [ + GL_VERTEX_ARRAY GL_NORMAL_ARRAY 2array [ + GL_FLOAT 0 0 buffer-offset glNormalPointer + cel-shading-gadget-model dup + first length 3 * 4 * buffer-offset + 3 GL_FLOAT 0 roll glVertexPointer + third length 3 * + GL_TRIANGLES swap GL_UNSIGNED_INT 0 buffer-offset glDrawElements + ] all-enabled-client-state + ] with-array-element-buffers ] with-gl-program ; : cel-shading-window ( -- ) diff --git a/extra/line-art/line-art.factor b/extra/line-art/line-art.factor index 9856921a51..d78ea8a4ee 100644 --- a/extra/line-art/line-art.factor +++ b/extra/line-art/line-art.factor @@ -187,7 +187,7 @@ main() ] if ; M: line-art-gadget graft* ( gadget -- ) - [ "2.0" { "GL_ARB_draw_buffers" + "2.0" { "GL_ARB_draw_buffers" "GL_ARB_shader_objects" "GL_ARB_multitexture" "GL_ARB_texture_float" } @@ -196,8 +196,7 @@ M: line-art-gadget graft* ( gadget -- ) GL_CULL_FACE glEnable GL_DEPTH_TEST glEnable (line-art-step1-program) over set-line-art-gadget-step1-program - (line-art-step2-program) swap set-line-art-gadget-step2-program - ] [ ] [ :c ] cleanup ; + (line-art-step2-program) swap set-line-art-gadget-step2-program ; M: line-art-gadget ungraft* ( gadget -- ) dup line-art-gadget-framebuffer [ diff --git a/extra/opengl/opengl-docs.factor b/extra/opengl/opengl-docs.factor index cc8221baa1..63875e91a8 100644 --- a/extra/opengl/opengl-docs.factor +++ b/extra/opengl/opengl-docs.factor @@ -65,7 +65,7 @@ HELP: gen-renderbuffer { $values { "id" integer } } { $description "Wrapper for " { $link glGenRenderbuffersEXT } " to handle the common case of generating a single render buffer ID." } ; -HELP: gen-buffer +HELP: gen-gl-buffer { $values { "id" integer } } { $description "Wrapper for " { $link glGenBuffers } " to handle the common case of generating a single buffer ID." } ; @@ -81,14 +81,14 @@ HELP: delete-renderbuffer { $values { "id" integer } } { $description "Wrapper for " { $link glDeleteRenderbuffersEXT } " to handle the common case of deleting a single render buffer ID." } ; -HELP: delete-buffer +HELP: delete-gl-buffer { $values { "id" integer } } { $description "Wrapper for " { $link glDeleteBuffers } " to handle the common case of deleting a single buffer ID." } ; { gen-texture delete-texture } related-words { gen-framebuffer delete-framebuffer } related-words { gen-renderbuffer delete-renderbuffer } related-words -{ gen-buffer delete-buffer } related-words +{ gen-gl-buffer delete-gl-buffer } related-words HELP: framebuffer-incomplete? { $values { "status/f" "The framebuffer error code, or " { $snippet "f" } " if the framebuffer is render-complete." } } diff --git a/extra/opengl/opengl.factor b/extra/opengl/opengl.factor index 2c1d4de75c..a6aecf1b77 100755 --- a/extra/opengl/opengl.factor +++ b/extra/opengl/opengl.factor @@ -30,6 +30,13 @@ IN: opengl : do-enabled ( what quot -- ) over glEnable dip glDisable ; inline +: do-enabled-client-state ( what quot -- ) + over glEnableClientState dip glDisableClientState ; inline + +: all-enabled ( seq quot -- ) + over [ glEnable ] each dip [ glDisable ] each ; inline +: all-enabled-client-state ( seq quot -- ) + over [ glEnableClientState ] each dip [ glDisableClientState ] each ; inline : do-matrix ( mode quot -- ) swap [ glMatrixMode glPushMatrix call ] keep @@ -103,7 +110,7 @@ IN: opengl [ glGenFramebuffersEXT ] (gen-gl-object) ; : gen-renderbuffer ( -- id ) [ glGenRenderbuffersEXT ] (gen-gl-object) ; -: gen-buffer ( -- id ) +: gen-gl-buffer ( -- id ) [ glGenBuffers ] (gen-gl-object) ; : (delete-gl-object) ( id quot -- ) @@ -114,9 +121,26 @@ IN: opengl [ glDeleteFramebuffersEXT ] (delete-gl-object) ; : delete-renderbuffer ( id -- ) [ glDeleteRenderbuffersEXT ] (delete-gl-object) ; -: delete-buffer ( id -- ) +: delete-gl-buffer ( id -- ) [ glDeleteBuffers ] (delete-gl-object) ; +: with-gl-buffer ( binding id quot -- ) + -rot dupd glBindBuffer + [ slip ] [ 0 glBindBuffer ] [ ] cleanup ; inline + +: with-array-element-buffers ( array-buffer element-buffer quot -- ) + -rot GL_ELEMENT_ARRAY_BUFFER swap [ + swap GL_ARRAY_BUFFER -rot with-gl-buffer + ] with-gl-buffer ; inline + +: ( target data hint -- id ) + pick gen-gl-buffer [ [ + >r dup byte-length swap r> glBufferData + ] with-gl-buffer ] keep ; + +: buffer-offset ( int -- alien ) + ; inline + : framebuffer-incomplete? ( -- status/f ) GL_FRAMEBUFFER_EXT glCheckFramebufferStatusEXT dup GL_FRAMEBUFFER_COMPLETE_EXT = f rot ? ; From e37f2101c6d355e437d4ca9654e59f6354473748 Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Fri, 1 Feb 2008 14:45:29 -0500 Subject: [PATCH 007/269] Solution to Project Euler problem 38 --- extra/project-euler/032/032.factor | 5 +-- extra/project-euler/038/038.factor | 55 ++++++++++++++++++++++++ extra/project-euler/common/common.factor | 6 ++- extra/project-euler/project-euler.factor | 4 +- 4 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 extra/project-euler/038/038.factor diff --git a/extra/project-euler/032/032.factor b/extra/project-euler/032/032.factor index d10326a076..2baa6f8714 100644 --- a/extra/project-euler/032/032.factor +++ b/extra/project-euler/032/032.factor @@ -1,7 +1,7 @@ ! Copyright (c) 2008 Aaron Schaefer. ! See http://factorcode.org/license.txt for BSD license. USING: combinators.lib hashtables kernel math math.combinatorics math.parser - math.ranges project-euler.common sequences sorting ; + math.ranges project-euler.common sequences ; IN: project-euler.032 ! http://projecteuler.net/index.php?section=problems&id=32 @@ -63,9 +63,6 @@ PRIVATE> : source-032a ( -- seq ) 50 [1,b] 2000 [1,b] cartesian-product ; -: pandigital? ( n -- ? ) - number>string natural-sort "123456789" = ; - ! multiplicand/multiplier/product : mmp ( pair -- n ) first2 2dup * [ number>string ] 3apply 3append 10 string>integer ; diff --git a/extra/project-euler/038/038.factor b/extra/project-euler/038/038.factor new file mode 100644 index 0000000000..cbe6f2363c --- /dev/null +++ b/extra/project-euler/038/038.factor @@ -0,0 +1,55 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel math math.parser math.ranges project-euler.common sequences ; +IN: project-euler.038 + +! http://projecteuler.net/index.php?section=problems&id=38 + +! DESCRIPTION +! ----------- + +! Take the number 192 and multiply it by each of 1, 2, and 3: + +! 192 × 1 = 192 +! 192 × 2 = 384 +! 192 × 3 = 576 + +! By concatenating each product we get the 1 to 9 pandigital, 192384576. We +! will call 192384576 the concatenated product of 192 and (1,2,3) + +! The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, +! and 5, giving the pandigital, 918273645, which is the concatenated product of +! 9 and (1,2,3,4,5). + +! What is the largest 1 to 9 pandigital 9-digit number that can be formed as +! the concatenated product of an integer with (1,2, ... , n) where n > 1? + + +! SOLUTION +! -------- + +! Only need to search 4-digit numbers starting with 9 since a 2-digit number +! starting with 9 would produce 8 or 11 digits, and a 3-digit number starting +! with 9 would produce 7 or 11 digits. + + [ + 2drop 10 swap digits>integer + ] [ + [ * number>digits over push-all ] 2keep 1+ (concat-product) + ] if ; + +: concat-product ( n -- m ) + V{ } clone swap 1 (concat-product) ; + +PRIVATE> + +: euler038 ( -- answer ) + 9123 9876 [a,b] [ concat-product ] map [ pandigital? ] subset supremum ; + +! [ euler038 ] 100 ave-time +! 37 ms run / 1 ms GC ave time - 100 trials + +MAIN: euler038 diff --git a/extra/project-euler/common/common.factor b/extra/project-euler/common/common.factor index 2e718ab5a2..609492c724 100644 --- a/extra/project-euler/common/common.factor +++ b/extra/project-euler/common/common.factor @@ -1,5 +1,5 @@ USING: arrays combinators.lib kernel math math.functions math.miller-rabin - math.parser math.primes.factors math.ranges namespaces sequences ; + math.parser math.primes.factors math.ranges namespaces sequences sorting ; IN: project-euler.common ! A collection of words used by more than one Project Euler solution @@ -12,6 +12,7 @@ IN: project-euler.common ! log10 - #25, #134 ! max-path - #18, #67 ! number>digits - #16, #20, #30, #34 +! pandigital? - #32, #38 ! propagate-all - #18, #67 ! sum-proper-divisors - #21 ! tau* - #12 @@ -67,6 +68,9 @@ PRIVATE> : number>digits ( n -- seq ) number>string string>digits ; +: pandigital? ( n -- ? ) + number>string natural-sort "123456789" = ; + ! Not strictly needed, but it is nice to be able to dump the triangle after the ! propagation : propagate-all ( triangle -- newtriangle ) diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index fbb62961a9..0037e4462f 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -11,8 +11,8 @@ USING: definitions io io.files kernel math.parser sequences vocabs project-euler.025 project-euler.026 project-euler.027 project-euler.028 project-euler.029 project-euler.030 project-euler.031 project-euler.032 project-euler.033 project-euler.034 project-euler.035 project-euler.036 - project-euler.037 project-euler.067 project-euler.134 project-euler.169 - project-euler.173 project-euler.175 ; + project-euler.037 project-euler.038 project-euler.067 project-euler.134 + project-euler.169 project-euler.173 project-euler.175 ; IN: project-euler Date: Fri, 1 Feb 2008 17:43:44 -0600 Subject: [PATCH 008/269] first commit of db stuff --- extra/db/db.factor | 96 ++++++ extra/db/postgresql/authors.txt | 1 + extra/db/postgresql/ffi/ffi.factor | 360 ++++++++++++++++++++ extra/db/postgresql/lib/lib.factor | 72 ++++ extra/db/postgresql/postgresql-tests.factor | 54 +++ extra/db/postgresql/postgresql.factor | 87 +++++ extra/db/sqlite/authors.txt | 2 + extra/db/sqlite/ffi/ffi.factor | 131 +++++++ extra/db/sqlite/lib/lib.factor | 103 ++++++ extra/db/sqlite/sqlite-tests.factor | 99 ++++++ extra/db/sqlite/sqlite.factor | 70 ++++ extra/db/sqlite/test.txt | 3 + 12 files changed, 1078 insertions(+) create mode 100644 extra/db/db.factor create mode 100644 extra/db/postgresql/authors.txt create mode 100644 extra/db/postgresql/ffi/ffi.factor create mode 100644 extra/db/postgresql/lib/lib.factor create mode 100644 extra/db/postgresql/postgresql-tests.factor create mode 100644 extra/db/postgresql/postgresql.factor create mode 100644 extra/db/sqlite/authors.txt create mode 100644 extra/db/sqlite/ffi/ffi.factor create mode 100644 extra/db/sqlite/lib/lib.factor create mode 100644 extra/db/sqlite/sqlite-tests.factor create mode 100644 extra/db/sqlite/sqlite.factor create mode 100644 extra/db/sqlite/test.txt diff --git a/extra/db/db.factor b/extra/db/db.factor new file mode 100644 index 0000000000..597ac1f0f3 --- /dev/null +++ b/extra/db/db.factor @@ -0,0 +1,96 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: arrays assocs classes continuations kernel math +namespaces sequences sequences.lib tuples words ; +IN: db + +TUPLE: db handle ; +C: db ( handle -- obj ) + +! HOOK: db-create db ( str -- ) +! HOOK: db-drop db ( str -- ) +GENERIC: db-open ( db -- ) +GENERIC: db-close ( db -- ) + +TUPLE: statement sql params handle bound? n max ; + +TUPLE: simple-statement ; +TUPLE: bound-statement ; +TUPLE: prepared-statement ; +TUPLE: prepared-bound-statement ; + +HOOK: db ( str -- statement ) +HOOK: db ( str obj -- statement ) +HOOK: db ( str -- statement ) +HOOK: db ( str obj -- statement ) + +! TUPLE: result sql params handle n max ; + +GENERIC: #rows ( statement -- n ) +GENERIC: #columns ( statement -- n ) +GENERIC# row-column 1 ( statement n -- obj ) +GENERIC: advance-row ( statement -- ? ) + +GENERIC: prepare-statement ( statement -- ) +GENERIC: reset-statement ( statement -- ) +GENERIC: bind-statement* ( obj statement -- ) +GENERIC: rebind-statement ( obj statement -- ) + +: bind-statement ( obj statement -- ) + 2dup dup statement-bound? [ + rebind-statement + ] [ + bind-statement* + ] if + tuck set-statement-params + t swap set-statement-bound? ; + +: sql-row ( statement -- seq ) + dup #columns [ row-column ] with map ; + +: query-each ( statement quot -- ) + over advance-row [ + 2drop + ] [ + [ call ] 2keep query-each + ] if ; inline + +: query-map ( statement quot -- seq ) + accumulator >r query-each r> { } like ; inline + +: with-db ( db quot -- ) + [ + over db-open + [ db swap with-variable ] curry with-disposal + ] with-scope ; + +: do-statement ( statement -- ) + [ advance-row drop ] with-disposal ; + +: do-query ( query -- rows ) + [ [ sql-row ] query-map ] with-disposal ; + +: do-simple-query ( sql -- rows ) + do-query ; + +: do-bound-query ( sql obj -- rows ) + do-query ; + +: do-simple-command ( sql -- ) + do-statement ; + +: do-bound-command ( sql obj -- ) + do-statement ; + +SYMBOL: in-transaction +HOOK: begin-transaction db ( -- ) +HOOK: commit-transaction db ( -- ) +HOOK: rollback-transaction db ( -- ) + +: in-transaction? ( -- ? ) in-transaction get ; + +: with-transaction ( quot -- ) + t in-transaction [ + begin-transaction + [ ] [ rollback-transaction ] cleanup commit-transaction + ] with-variable ; diff --git a/extra/db/postgresql/authors.txt b/extra/db/postgresql/authors.txt new file mode 100644 index 0000000000..7c1b2f2279 --- /dev/null +++ b/extra/db/postgresql/authors.txt @@ -0,0 +1 @@ +Doug Coleman diff --git a/extra/db/postgresql/ffi/ffi.factor b/extra/db/postgresql/ffi/ffi.factor new file mode 100644 index 0000000000..6d3cdfc468 --- /dev/null +++ b/extra/db/postgresql/ffi/ffi.factor @@ -0,0 +1,360 @@ +! Copyright (C) 2007 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. + +! adapted from libpq-fe.h version 7.4.7 +! tested on debian linux with postgresql 7.4.7 +! Updated to 8.1 + +USING: alien alien.syntax combinators system ; +IN: db.postgresql.ffi + +<< +"postgresql" { + { [ win32? ] [ "libpq.dll" ] } + { [ macosx? ] [ "/opt/local/lib/postgresql81/libpq.dylib" ] } + { [ unix? ] [ "libpq.so" ] } +} cond "cdecl" add-library +>> + +! ConnSatusType +: CONNECTION_OK HEX: 0 ; inline +: CONNECTION_BAD HEX: 1 ; inline +: CONNECTION_STARTED HEX: 2 ; inline +: CONNECTION_MADE HEX: 3 ; inline +: CONNECTION_AWAITING_RESPONSE HEX: 4 ; inline +: CONNECTION_AUTH_OK HEX: 5 ; inline +: CONNECTION_SETENV HEX: 6 ; inline +: CONNECTION_SSL_STARTUP HEX: 7 ; inline +: CONNECTION_NEEDED HEX: 8 ; inline + +! PostgresPollingStatusType +: PGRES_POLLING_FAILED HEX: 0 ; inline +: PGRES_POLLING_READING HEX: 1 ; inline +: PGRES_POLLING_WRITING HEX: 2 ; inline +: PGRES_POLLING_OK HEX: 3 ; inline +: PGRES_POLLING_ACTIVE HEX: 4 ; inline + +! ExecStatusType; +: PGRES_EMPTY_QUERY HEX: 0 ; inline +: PGRES_COMMAND_OK HEX: 1 ; inline +: PGRES_TUPLES_OK HEX: 2 ; inline +: PGRES_COPY_OUT HEX: 3 ; inline +: PGRES_COPY_IN HEX: 4 ; inline +: PGRES_BAD_RESPONSE HEX: 5 ; inline +: PGRES_NONFATAL_ERROR HEX: 6 ; inline +: PGRES_FATAL_ERROR HEX: 7 ; inline + +! PGTransactionStatusType; +: PQTRANS_IDLE HEX: 0 ; inline +: PQTRANS_ACTIVE HEX: 1 ; inline +: PQTRANS_INTRANS HEX: 2 ; inline +: PQTRANS_INERROR HEX: 3 ; inline +: PQTRANS_UNKNOWN HEX: 4 ; inline + +! PGVerbosity; +: PQERRORS_TERSE HEX: 0 ; inline +: PQERRORS_DEFAULT HEX: 1 ; inline +: PQERRORS_VERBOSE HEX: 2 ; inline + + +TYPEDEF: int size_t +TYPEDEF: int ConnStatusType +TYPEDEF: int ExecStatusType +TYPEDEF: int PostgresPollingStatusType +TYPEDEF: int PGTransactionStatusType +TYPEDEF: int PGVerbosity + +TYPEDEF: void* PGconn* +TYPEDEF: void* PGresult* +TYPEDEF: void* PGcancel* +TYPEDEF: uint Oid +TYPEDEF: uint* Oid* +TYPEDEF: char pqbool +TYPEDEF: void* PQconninfoOption* +TYPEDEF: void* PGnotify* +TYPEDEF: void* PQArgBlock* +TYPEDEF: void* PQprintOpt* +TYPEDEF: void* FILE* +TYPEDEF: void* SSL* + +LIBRARY: postgresql + + +! Exported functions of libpq +! === in fe-connect.c === + +! make a new client connection to the backend +! Asynchronous (non-blocking) +FUNCTION: PGconn* PQconnectStart ( char* conninfo ) ; +FUNCTION: PostgresPollingStatusType PQconnectPoll ( PGconn* conn ) ; + +! Synchronous (blocking) +FUNCTION: PGconn* PQconnectdb ( char* conninfo ) ; +FUNCTION: PGconn* PQsetdbLogin ( char* pghost, char* pgport, + char* pgoptions, char* pgtty, + char* dbName, + char* login, char* pwd ) ; + +: PQsetdb ( M_PGHOST M_PGPORT M_PGOPT M_PGTTY M_DBNAME -- PGconn* ) + f f PQsetdbLogin ; + +! close the current connection and free the PGconn data structure +FUNCTION: void PQfinish ( PGconn* conn ) ; + +! get info about connection options known to PQconnectdb +FUNCTION: PQconninfoOption* PQconndefaults ( ) ; + +! free the data structure returned by PQconndefaults() +FUNCTION: void PQconninfoFree ( PQconninfoOption* connOptions ) ; + +! +! close the current connection and restablish a new one with the same +! parameters +! +! Asynchronous (non-blocking) +FUNCTION: int PQresetStart ( PGconn* conn ) ; +FUNCTION: PostgresPollingStatusType PQresetPoll ( PGconn* conn ) ; + +! Synchronous (blocking) +FUNCTION: void PQreset ( PGconn* conn ) ; + +! request a cancel structure +FUNCTION: PGcancel* PQgetCancel ( PGconn* conn ) ; + +! free a cancel structure +FUNCTION: void PQfreeCancel ( PGcancel* cancel ) ; + +! issue a cancel request +FUNCTION: int PQrequestCancel ( PGconn* conn ) ; + +! Accessor functions for PGconn objects +FUNCTION: char* PQdb ( PGconn* conn ) ; +FUNCTION: char* PQuser ( PGconn* conn ) ; +FUNCTION: char* PQpass ( PGconn* conn ) ; +FUNCTION: char* PQhost ( PGconn* conn ) ; +FUNCTION: char* PQport ( PGconn* conn ) ; +FUNCTION: char* PQtty ( PGconn* conn ) ; +FUNCTION: char* PQoptions ( PGconn* conn ) ; +FUNCTION: ConnStatusType PQstatus ( PGconn* conn ) ; +FUNCTION: PGTransactionStatusType PQtransactionStatus ( PGconn* conn ) ; +FUNCTION: char* PQparameterStatus ( PGconn* conn, + char* paramName ) ; +FUNCTION: int PQprotocolVersion ( PGconn* conn ) ; +! FUNCTION: int PQServerVersion ( PGconn* conn ) ; +FUNCTION: char* PQerrorMessage ( PGconn* conn ) ; +FUNCTION: int PQsocket ( PGconn* conn ) ; +FUNCTION: int PQbackendPID ( PGconn* conn ) ; +FUNCTION: int PQclientEncoding ( PGconn* conn ) ; +FUNCTION: int PQsetClientEncoding ( PGconn* conn, char* encoding ) ; + +! May not be compiled into libpq +! Get the SSL structure associated with a connection +FUNCTION: SSL* PQgetssl ( PGconn* conn ) ; + +! Tell libpq whether it needs to initialize OpenSSL +FUNCTION: void PQinitSSL ( int do_init ) ; + +! Set verbosity for PQerrorMessage and PQresultErrorMessage +FUNCTION: PGVerbosity PQsetErrorVerbosity ( PGconn* conn, + PGVerbosity verbosity ) ; + +! Enable/disable tracing +FUNCTION: void PQtrace ( PGconn* conn, FILE* debug_port ) ; +FUNCTION: void PQuntrace ( PGconn* conn ) ; + +! BROKEN +! Function types for notice-handling callbacks +! typedef void (*PQnoticeReceiver) (void *arg, PGresult *res); +! typedef void (*PQnoticeProcessor) (void *arg, char* message); +! ALIAS: void* PQnoticeReceiver +! ALIAS: void* PQnoticeProcessor + +! Override default notice handling routines +! FUNCTION: PQnoticeReceiver PQsetNoticeReceiver ( PGconn* conn, + ! PQnoticeReceiver proc, + ! void* arg ) ; +! FUNCTION: PQnoticeProcessor PQsetNoticeProcessor ( PGconn* conn, + ! PQnoticeProcessor proc, + ! void* arg ) ; +! END BROKEN + +! === in fe-exec.c === + +! Simple synchronous query +FUNCTION: PGresult* PQexec ( PGconn* conn, char* query ) ; +FUNCTION: PGresult* PQexecParams ( PGconn* conn, + char* command, + int nParams, + Oid* paramTypes, + char** paramValues, + int* paramLengths, + int* paramFormats, + int resultFormat ) ; +FUNCTION: PGresult* PQprepare ( PGconn* conn, char* stmtName, + char* query, int nParams, + Oid* paramTypes ) ; +FUNCTION: PGresult* PQexecPrepared ( PGconn* conn, + char* stmtName, + int nParams, + char** paramValues, + int* paramLengths, + int* paramFormats, + int resultFormat ) ; + +! Interface for multiple-result or asynchronous queries +FUNCTION: int PQsendQuery ( PGconn* conn, char* query ) ; +FUNCTION: int PQsendQueryParams ( PGconn* conn, + char* command, + int nParams, + Oid* paramTypes, + char** paramValues, + int* paramLengths, + int* paramFormats, + int resultFormat ) ; +FUNCTION: PGresult* PQsendPrepare ( PGconn* conn, char* stmtName, + char* query, int nParams, + Oid* paramTypes ) ; +FUNCTION: int PQsendQueryPrepared ( PGconn* conn, + char* stmtName, + int nParams, + char** paramValues, + int *paramLengths, + int *paramFormats, + int resultFormat ) ; +FUNCTION: PGresult* PQgetResult ( PGconn* conn ) ; + +! Routines for managing an asynchronous query +FUNCTION: int PQisBusy ( PGconn* conn ) ; +FUNCTION: int PQconsumeInput ( PGconn* conn ) ; + +! LISTEN/NOTIFY support +FUNCTION: PGnotify* PQnotifies ( PGconn* conn ) ; + +! Routines for copy in/out +FUNCTION: int PQputCopyData ( PGconn* conn, char* buffer, int nbytes ) ; +FUNCTION: int PQputCopyEnd ( PGconn* conn, char* errormsg ) ; +FUNCTION: int PQgetCopyData ( PGconn* conn, char** buffer, int async ) ; + +! Deprecated routines for copy in/out +FUNCTION: int PQgetline ( PGconn* conn, char* string, int length ) ; +FUNCTION: int PQputline ( PGconn* conn, char* string ) ; +FUNCTION: int PQgetlineAsync ( PGconn* conn, char* buffer, int bufsize ) ; +FUNCTION: int PQputnbytes ( PGconn* conn, char* buffer, int nbytes ) ; +FUNCTION: int PQendcopy ( PGconn* conn ) ; + +! Set blocking/nonblocking connection to the backend +FUNCTION: int PQsetnonblocking ( PGconn* conn, int arg ) ; +FUNCTION: int PQisnonblocking ( PGconn* conn ) ; + +! Force the write buffer to be written (or at least try) +FUNCTION: int PQflush ( PGconn* conn ) ; + +! +! * "Fast path" interface --- not really recommended for application +! * use +! +FUNCTION: PGresult* PQfn ( PGconn* conn, + int fnid, + int* result_buf, + int* result_len, + int result_is_int, + PQArgBlock* args, + int nargs ) ; + +! Accessor functions for PGresult objects +FUNCTION: ExecStatusType PQresultStatus ( PGresult* res ) ; +FUNCTION: char* PQresStatus ( ExecStatusType status ) ; +FUNCTION: char* PQresultErrorMessage ( PGresult* res ) ; +FUNCTION: char* PQresultErrorField ( PGresult* res, int fieldcode ) ; +FUNCTION: int PQntuples ( PGresult* res ) ; +FUNCTION: int PQnfields ( PGresult* res ) ; +FUNCTION: int PQbinaryTuples ( PGresult* res ) ; +FUNCTION: char* PQfname ( PGresult* res, int field_num ) ; +FUNCTION: int PQfnumber ( PGresult* res, char* field_name ) ; +FUNCTION: Oid PQftable ( PGresult* res, int field_num ) ; +FUNCTION: int PQftablecol ( PGresult* res, int field_num ) ; +FUNCTION: int PQfformat ( PGresult* res, int field_num ) ; +FUNCTION: Oid PQftype ( PGresult* res, int field_num ) ; +FUNCTION: int PQfsize ( PGresult* res, int field_num ) ; +FUNCTION: int PQfmod ( PGresult* res, int field_num ) ; +FUNCTION: char* PQcmdStatus ( PGresult* res ) ; +FUNCTION: char* PQoidStatus ( PGresult* res ) ; +FUNCTION: Oid PQoidValue ( PGresult* res ) ; +FUNCTION: char* PQcmdTuples ( PGresult* res ) ; +FUNCTION: char* PQgetvalue ( PGresult* res, int tup_num, int field_num ) ; +FUNCTION: int PQgetlength ( PGresult* res, int tup_num, int field_num ) ; +FUNCTION: int PQgetisnull ( PGresult* res, int tup_num, int field_num ) ; + +! Delete a PGresult +FUNCTION: void PQclear ( PGresult* res ) ; + +! For freeing other alloc'd results, such as PGnotify structs +FUNCTION: void PQfreemem ( void* ptr ) ; + +! Exists for backward compatibility. +: PQfreeNotify PQfreemem ; + +! +! Make an empty PGresult with given status (some apps find this +! useful). If conn is not NULL and status indicates an error, the +! conn's errorMessage is copied. +! +FUNCTION: PGresult* PQmakeEmptyPGresult ( PGconn* conn, ExecStatusType status ) ; + +! Quoting strings before inclusion in queries. +FUNCTION: size_t PQescapeStringConn ( PGconn* conn, + char* to, char* from, size_t length, + int* error ) ; +FUNCTION: uchar* PQescapeByteaConn ( PGconn* conn, + char* from, size_t length, + size_t* to_length ) ; +FUNCTION: uchar* PQunescapeBytea ( uchar* strtext, + size_t* retbuflen ) ; +! These forms are deprecated! +FUNCTION: size_t PQescapeString ( void* to, char* from, size_t length ) ; +FUNCTION: uchar* PQescapeBytea ( uchar* bintext, size_t binlen, + size_t* bytealen ) ; + +! === in fe-print.c === + +FUNCTION: void PQprint ( FILE* fout, PGresult* res, PQprintOpt* ps ) ; + +! really old printing routines +FUNCTION: void PQdisplayTuples ( PGresult* res, + FILE* fp, + int fillAlign, + char* fieldSep, + int printHeader, + int quiet ) ; + +FUNCTION: void PQprintTuples ( PGresult* res, + FILE* fout, + int printAttName, + int terseOutput, + int width ) ; + +! === in fe-lobj.c === + +! Large-object access routines +FUNCTION: int lo_open ( PGconn* conn, Oid lobjId, int mode ) ; +FUNCTION: int lo_close ( PGconn* conn, int fd ) ; +FUNCTION: int lo_read ( PGconn* conn, int fd, char* buf, size_t len ) ; +FUNCTION: int lo_write ( PGconn* conn, int fd, char* buf, size_t len ) ; +FUNCTION: int lo_lseek ( PGconn* conn, int fd, int offset, int whence ) ; +FUNCTION: Oid lo_creat ( PGconn* conn, int mode ) ; +! FUNCTION: Oid lo_creat ( PGconn* conn, Oid lobjId ) ; +FUNCTION: int lo_tell ( PGconn* conn, int fd ) ; +FUNCTION: int lo_unlink ( PGconn* conn, Oid lobjId ) ; +FUNCTION: Oid lo_import ( PGconn* conn, char* filename ) ; +FUNCTION: int lo_export ( PGconn* conn, Oid lobjId, char* filename ) ; + +! === in fe-misc.c === + +! Determine length of multibyte encoded char at *s +FUNCTION: int PQmblen ( uchar* s, int encoding ) ; + +! Determine display length of multibyte encoded char at *s +FUNCTION: int PQdsplen ( uchar* s, int encoding ) ; + +! Get encoding id from environment variable PGCLIENTENCODING +FUNCTION: int PQenv2encoding ( ) ; diff --git a/extra/db/postgresql/lib/lib.factor b/extra/db/postgresql/lib/lib.factor new file mode 100644 index 0000000000..4b362f9931 --- /dev/null +++ b/extra/db/postgresql/lib/lib.factor @@ -0,0 +1,72 @@ +USING: arrays continuations db io kernel math namespaces +quotations sequences db.postgresql.ffi ; +IN: db.postgresql.lib + +SYMBOL: query-res + +: connect-postgres ( host port pgopts pgtty db user pass -- conn ) + PQsetdbLogin + dup PQstatus zero? [ "couldn't connect to database" throw ] unless ; + +: postgresql-result-error-message ( res -- str/f ) + dup zero? [ + drop f + ] [ + PQresultErrorMessage [ CHAR: \n = ] right-trim + ] if ; + +: postgres-result-error ( res -- ) + postgresql-result-error-message [ throw ] when* ; + +: postgresql-error-message ( -- str ) + db get db-handle PQerrorMessage [ CHAR: \n = ] right-trim ; + +: postgresql-error ( res -- res ) + dup [ postgresql-error-message throw ] unless ; + +: postgresql-result-ok? ( n -- ? ) + PQresultStatus + PGRES_COMMAND_OK PGRES_TUPLES_OK 2array member? ; + +: do-postgresql-statement ( statement -- res ) + db get db-handle swap statement-sql PQexec dup postgresql-result-ok? [ + dup postgresql-result-error-message swap PQclear throw + ] unless ; + +! : do-command ( str -- ) + ! 1quotation \ (do-command) add db get swap call ; + +! : prepare ( str quot word -- conn quot ) + ! rot 1quotation swap append swap append db get swap ; + +! : do-query ( str quot -- ) + ! [ (do-query) query-res set ] prepare catch + ! [ rethrow ] [ query-res get PQclear ] if* ; + +! : result>seq ( -- seq ) + ! query-res get [ PQnfields ] keep PQntuples + ! [ swap [ query-res get -rot PQgetvalue ] with map ] with map ; +! +! : print-table ( seq -- ) + ! [ [ write bl ] each "\n" write ] each ; + + + +! select * from animal where name = 'Simba' +! select * from animal where name = $1 + +! : (do-query) ( PGconn query -- PGresult* ) + ! ! For queries that do not return rows, PQexec() returns PGRES_COMMAND_OK + ! ! For queries that return rows, PQexec() returns PGRES_TUPLES_OK + ! PQexec dup postgresql-result-ok? [ + ! dup postgresql-error-message swap PQclear throw + ! ] unless ; + +! : (do-command) ( PGconn query -- PGresult* ) + ! [ (do-query) ] catch + ! [ + ! swap + ! "non-fatal error: " print + ! "\tQuery: " write "'" write write "'" print + ! "\t" write print + ! ] when* drop ; diff --git a/extra/db/postgresql/postgresql-tests.factor b/extra/db/postgresql/postgresql-tests.factor new file mode 100644 index 0000000000..438a80e2d8 --- /dev/null +++ b/extra/db/postgresql/postgresql-tests.factor @@ -0,0 +1,54 @@ +! You will need to run 'createdb factor-test' to create the database. +! Set username and password in the 'connect' word. + +USING: kernel db.postgresql alien continuations io prettyprint +sequences namespaces tools.test ; +IN: temporary + +: test-connection ( host port pgopts pgtty db user pass -- bool ) + [ [ ] with-postgres ] catch "Error connecting!" "Connected!" ? print ; + +[ ] [ "localhost" "" "" "" "factor-test" "postgres" "" test-connection ] unit-test + +[ ] [ "localhost" "postgres" "" "factor-test" [ ] with-db ] unit-test + +! just a basic demo + +"localhost" "postgres" "" "factor-test" [ + [ ] [ "drop table animal" do-command ] unit-test + + [ ] [ "create table animal (id serial not null primary key, species varchar(256), name varchar(256), age integer)" do-command ] unit-test + + [ ] [ "insert into animal (species, name, age) values ('lion', 'Mufasa', 5)" + do-command ] unit-test + + [ ] [ "select * from animal where name = 'Mufasa'" [ ] do-query ] unit-test + [ ] [ "select * from animal where name = 'Mufasa'" [ + result>seq length 1 = [ + "...there can only be one Mufasa..." throw + ] unless + ] do-query + ] unit-test + + [ ] [ "insert into animal (species, name, age) values ('lion', 'Simba', 1)" + do-command ] unit-test + + [ ] [ + "select * from animal" + [ + "Animal table:" print + result>seq print-table + ] do-query + ] unit-test + + ! intentional errors + ! [ "select asdf from animal" + ! [ ] do-query ] catch [ "caught: " write print ] when* + ! "select asdf from animal" [ ] do-query + ! "aofijweafew" do-command +] with-db + + +"localhost" "postgres" "" "factor-test" [ + [ ] [ "drop table animal" do-command ] unit-test +] with-db diff --git a/extra/db/postgresql/postgresql.factor b/extra/db/postgresql/postgresql.factor new file mode 100644 index 0000000000..cd2c34682e --- /dev/null +++ b/extra/db/postgresql/postgresql.factor @@ -0,0 +1,87 @@ +! Copyright (C) 2007 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +! adapted from libpq-fe.h version 7.4.7 +! tested on debian linux with postgresql 7.4.7 + +USING: arrays assocs alien alien.syntax continuations io +kernel math namespaces prettyprint quotations +sequences debugger db db.postgresql.lib db.postgresql.ffi ; +IN: db.postgresql + +TUPLE: postgresql-db host port pgopts pgtty db user pass ; +TUPLE: postgresql-statement ; +: ( statement -- postgresql-statement ) + postgresql-statement construct-delegate ; + +: ( host user pass db -- obj ) + { + set-postgresql-db-host + set-postgresql-db-user + set-postgresql-db-pass + set-postgresql-db-db + } postgresql-db construct ; + +M: postgresql-db db-open ( db -- ) + dup { + postgresql-db-host + postgresql-db-port + postgresql-db-pgopts + postgresql-db-pgtty + postgresql-db-db + postgresql-db-user + postgresql-db-pass + } get-slots connect-postgres swap set-delegate ; + +M: postgresql-db dispose ( db -- ) + db-handle PQfinish ; + +: with-postgresql ( host ust pass db quot -- ) + >r r> with-disposal ; + +M: postgresql-statement #rows ( statement -- n ) + statement-handle PQntuples ; + +M: postgresql-statement #columns ( statement -- n ) + statement-handle PQnfields ; + +M: postgresql-statement row-column ( statement n -- obj ) + >r dup statement-handle swap statement-n r> PQgetvalue ; + +: init-statement ( statement -- ) + dup statement-max [ + dup do-postgresql-statement over set-statement-handle + dup #rows over set-statement-max + -1 over set-statement-n + ] unless drop ; + +: increment-n ( statement -- n ) + dup statement-n 1+ dup rot set-statement-n ; + +M: postgresql-statement advance-row ( statement -- ? ) + dup init-statement + dup increment-n swap statement-max >= ; + +M: postgresql-statement dispose ( query -- ) + dup statement-handle PQclear + 0 0 rot { set-statement-n set-statement-max } set-slots ; + +M: postgresql-statement prepare-statement ( statement -- ) + [ + >r db get db-handle "" r> + dup statement-sql swap statement-params + dup assoc-size swap PQprepare postgresql-error + ] keep set-statement-handle ; + +M: postgresql-db ( sql -- statement ) + { set-statement-sql } statement construct + ; + +M: postgresql-db ( sql array -- statement ) + { set-statement-sql set-statement-params } statement construct + ; + +M: postgresql-db ( sql -- statement ) + ; + +M: postgresql-db ( sql seq -- statement ) + ; diff --git a/extra/db/sqlite/authors.txt b/extra/db/sqlite/authors.txt new file mode 100644 index 0000000000..26093b451b --- /dev/null +++ b/extra/db/sqlite/authors.txt @@ -0,0 +1,2 @@ +Chris Double +Doug Coleman diff --git a/extra/db/sqlite/ffi/ffi.factor b/extra/db/sqlite/ffi/ffi.factor new file mode 100644 index 0000000000..77a86a8a2d --- /dev/null +++ b/extra/db/sqlite/ffi/ffi.factor @@ -0,0 +1,131 @@ +! Copyright (C) 2005 Chris Double, Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +! +! An interface to the sqlite database. Tested against sqlite v3.1.3. + +! Not all functions have been wrapped yet. Only those directly involving +! executing SQL calls and obtaining results. + +USING: alien compiler kernel math namespaces sequences strings alien.syntax + system combinators ; +IN: db.sqlite.ffi + +<< + "sqlite" { + { [ winnt? ] [ "sqlite3.dll" ] } + { [ macosx? ] [ "/usr/lib/libsqlite3.dylib" ] } + { [ unix? ] [ "libsqlite3.so" ] } + } cond "cdecl" add-library >> + +! Return values from sqlite functions +: SQLITE_OK 0 ; inline ! Successful result +: SQLITE_ERROR 1 ; inline ! SQL error or missing database +: SQLITE_INTERNAL 2 ; inline ! An internal logic error in SQLite +: SQLITE_PERM 3 ; inline ! Access permission denied +: SQLITE_ABORT 4 ; inline ! Callback routine requested an abort +: SQLITE_BUSY 5 ; inline ! The database file is locked +: SQLITE_LOCKED 6 ; inline ! A table in the database is locked +: SQLITE_NOMEM 7 ; inline ! A malloc() failed +: SQLITE_READONLY 8 ; inline ! Attempt to write a readonly database +: SQLITE_INTERRUPT 9 ; inline ! Operation terminated by sqlite_interrupt() +: SQLITE_IOERR 10 ; inline ! Some kind of disk I/O error occurred +: SQLITE_CORRUPT 11 ; inline ! The database disk image is malformed +: SQLITE_NOTFOUND 12 ; inline ! (Internal Only) Table or record not found +: SQLITE_FULL 13 ; inline ! Insertion failed because database is full +: SQLITE_CANTOPEN 14 ; inline ! Unable to open the database file +: SQLITE_PROTOCOL 15 ; inline ! Database lock protocol error +: SQLITE_EMPTY 16 ; inline ! (Internal Only) Database table is empty +: SQLITE_SCHEMA 17 ; inline ! The database schema changed +: SQLITE_TOOBIG 18 ; inline ! Too much data for one row of a table +: SQLITE_CONSTRAINT 19 ; inline ! Abort due to contraint violation +: SQLITE_MISMATCH 20 ; inline ! Data type mismatch +: SQLITE_MISUSE 21 ; inline ! Library used incorrectly +: SQLITE_NOLFS 22 ; inline ! Uses OS features not supported on host +: SQLITE_AUTH 23 ; inline ! Authorization denied +: SQLITE_FORMAT 24 ; inline ! Auxiliary database format error +: SQLITE_RANGE 25 ; inline ! 2nd parameter to sqlite3_bind out of range +: SQLITE_NOTADB 26 ; inline ! File opened that is not a database file + +: sqlite-error-messages ( -- seq ) { + "Successful result" + "SQL error or missing database" + "An internal logic error in SQLite" + "Access permission denied" + "Callback routine requested an abort" + "The database file is locked" + "A table in the database is locked" + "A malloc() failed" + "Attempt to write a readonly database" + "Operation terminated by sqlite_interrupt()" + "Some kind of disk I/O error occurred" + "The database disk image is malformed" + "(Internal Only) Table or record not found" + "Insertion failed because database is full" + "Unable to open the database file" + "Database lock protocol error" + "(Internal Only) Database table is empty" + "The database schema changed" + "Too much data for one row of a table" + "Abort due to contraint violation" + "Data type mismatch" + "Library used incorrectly" + "Uses OS features not supported on host" + "Authorization denied" + "Auxiliary database format error" + "2nd parameter to sqlite3_bind out of range" + "File opened that is not a database file" +} ; + +: SQLITE_ROW 100 ; inline ! sqlite_step() has another row ready +: SQLITE_DONE 101 ; inline ! sqlite_step() has finished executing + +! Return values from the sqlite3_column_type function +: SQLITE_INTEGER 1 ; inline +: SQLITE_FLOAT 2 ; inline +: SQLITE_TEXT 3 ; inline +: SQLITE_BLOB 4 ; inline +: SQLITE_NULL 5 ; inline + +! Values for the 'destructor' parameter of the 'bind' routines. +: SQLITE_STATIC 0 ; inline +: SQLITE_TRANSIENT -1 ; inline + +: SQLITE_OPEN_READONLY HEX: 00000001 ; inline +: SQLITE_OPEN_READWRITE HEX: 00000002 ; inline +: SQLITE_OPEN_CREATE HEX: 00000004 ; inline +: SQLITE_OPEN_DELETEONCLOSE HEX: 00000008 ; inline +: SQLITE_OPEN_EXCLUSIVE HEX: 00000010 ; inline +: SQLITE_OPEN_MAIN_DB HEX: 00000100 ; inline +: SQLITE_OPEN_TEMP_DB HEX: 00000200 ; inline +: SQLITE_OPEN_TRANSIENT_DB HEX: 00000400 ; inline +: SQLITE_OPEN_MAIN_JOURNAL HEX: 00000800 ; inline +: SQLITE_OPEN_TEMP_JOURNAL HEX: 00001000 ; inline +: SQLITE_OPEN_SUBJOURNAL HEX: 00002000 ; inline +: SQLITE_OPEN_MASTER_JOURNAL HEX: 00004000 ; inline + + +TYPEDEF: void sqlite3 +TYPEDEF: void sqlite3_stmt + +LIBRARY: sqlite +FUNCTION: int sqlite3_open ( char* filename, void* ppDb ) ; +FUNCTION: int sqlite3_open_v2 ( char* filename, void* ppDb, int flags, char* zVfs ) ; +FUNCTION: int sqlite3_close ( sqlite3* pDb ) ; +FUNCTION: int sqlite3_prepare ( sqlite3* pDb, char* zSql, int nBytes, void* ppStmt, void* pzTail ) ; +FUNCTION: int sqlite3_finalize ( sqlite3_stmt* pStmt ) ; +FUNCTION: int sqlite3_reset ( sqlite3_stmt* pStmt ) ; +FUNCTION: int sqlite3_step ( sqlite3_stmt* pStmt ) ; +FUNCTION: int sqlite3_last_insert_rowid ( sqlite3* pStmt ) ; +FUNCTION: int sqlite3_bind_blob ( sqlite3_stmt* pStmt, int index, void* ptr, int len, int destructor ) ; +FUNCTION: int sqlite3_bind_int ( sqlite3_stmt* pStmt, int index, int n ) ; +FUNCTION: int sqlite3_bind_null ( sqlite3_stmt* pStmt, int n ) ; +FUNCTION: int sqlite3_bind_text ( sqlite3_stmt* pStmt, int index, char* text, int len, int destructor ) ; +FUNCTION: int sqlite3_bind_parameter_index ( sqlite3_stmt* pStmt, char* name ) ; +FUNCTION: int sqlite3_column_count ( sqlite3_stmt* pStmt ) ; +FUNCTION: void* sqlite3_column_blob ( sqlite3_stmt* pStmt, int col ) ; +FUNCTION: int sqlite3_column_bytes ( sqlite3_stmt* pStmt, int col ) ; +FUNCTION: char* sqlite3_column_decltype ( sqlite3_stmt* pStmt, int col ) ; +FUNCTION: int sqlite3_column_int ( sqlite3_stmt* pStmt, int col ) ; +FUNCTION: int sqlite3_column_name ( sqlite3_stmt* pStmt, int col ) ; +FUNCTION: char* sqlite3_column_text ( sqlite3_stmt* pStmt, int col ) ; +FUNCTION: int sqlite3_column_type ( sqlite3_stmt* pStmt, int col ) ; diff --git a/extra/db/sqlite/lib/lib.factor b/extra/db/sqlite/lib/lib.factor new file mode 100644 index 0000000000..99cd9c1b9f --- /dev/null +++ b/extra/db/sqlite/lib/lib.factor @@ -0,0 +1,103 @@ +USING: alien.c-types assocs kernel math math.parser sequences +db.sqlite.ffi ; +IN: db.sqlite.lib + +TUPLE: sqlite-error n message ; + +: sqlite-check-result ( result -- ) + dup SQLITE_OK = [ + drop + ] [ + dup sqlite-error-messages nth + sqlite-error construct-boa throw + ] if ; + +: sqlite-open ( filename -- db ) + "void*" + [ sqlite3_open sqlite-check-result ] keep *void* ; + +: sqlite-close ( db -- ) + sqlite3_close sqlite-check-result ; + +: sqlite-last-insert-rowid ( db -- rowid ) + sqlite3_last_insert_rowid ; + +: sqlite-prepare ( db sql -- statement ) + #! TODO: Support multiple statements in the SQL string. + dup length "void*" "void*" + [ sqlite3_prepare sqlite-check-result ] 2keep + drop *void* ; + +: sqlite-bind-text ( statement index text -- ) + dup number? [ number>string ] when + dup length SQLITE_TRANSIENT sqlite3_bind_text sqlite-check-result ; + +: sqlite-bind-parameter-index ( statement name -- index ) + sqlite3_bind_parameter_index ; + +: sqlite-bind-text-by-name ( statement name text -- ) + >r dupd sqlite-bind-parameter-index r> sqlite-bind-text ; + +: sqlite-bind-assoc ( statement assoc -- ) + swap [ + -rot sqlite-bind-text-by-name + ] curry assoc-each ; + +: sqlite-finalize ( statement -- ) + sqlite3_finalize sqlite-check-result ; + +: sqlite-reset ( statement -- ) + sqlite3_reset sqlite-check-result ; + +: sqlite-#columns ( query -- int ) + sqlite3_column_count ; + +: sqlite-column ( statement index -- string ) + sqlite3_column_text ; + +: sqlite-row ( statement -- seq ) + dup sqlite-#columns [ sqlite-column ] with map ; + +! 2dup sqlite3_column_type . +! SQLITE_INTEGER 1 +! SQLITE_FLOAT 2 +! SQLITE_TEXT 3 +! SQLITE_BLOB 4 +! SQLITE_NULL 5 + + +: step-complete? ( step-result -- bool ) + dup SQLITE_ROW = [ + drop f + ] [ + dup SQLITE_DONE = [ drop t ] [ sqlite-check-result t ] if + ] if ; + +: sqlite-step ( prepared -- ) + dup sqlite3_step step-complete? [ + drop + ] [ + sqlite-step + ] if ; + +: sqlite-next ( prepared -- ) + sqlite3_step step-complete? ; + +: sqlite-each ( statement quot -- ) + over sqlite3_step step-complete? [ + 2drop + ] [ + [ call ] 2keep sqlite-each + ] if ; inline + +DEFER: (sqlite-map) + +: (sqlite-map) ( statement quot seq -- ) + pick sqlite3_step step-complete? [ + 2nip + ] [ + >r 2dup call r> swap add (sqlite-map) + ] if ; + +: sqlite-map ( statement quot -- seq ) + { } (sqlite-map) ; diff --git a/extra/db/sqlite/sqlite-tests.factor b/extra/db/sqlite/sqlite-tests.factor new file mode 100644 index 0000000000..79e967de24 --- /dev/null +++ b/extra/db/sqlite/sqlite-tests.factor @@ -0,0 +1,99 @@ +USING: io io.files io.launcher kernel namespaces +prettyprint tools.test db.sqlite db db.sql sequences +continuations ; +IN: temporary + +! "sqlite3 -init test.txt test.db" + +: test.db "extra/db/sqlite/test.db" resource-path ; + +: (create-db) ( -- str ) + [ + "sqlite3 -init " % + "extra/db/sqlite/test.txt" resource-path % + " " % + test.db % + ] "" make ; + +: create-db ( -- ) (create-db) run-process drop ; + +[ ] [ test.db delete-file ] unit-test + +[ ] [ create-db ] unit-test + +[ + { + { "John" "America" } + { "Jane" "New Zealand" } + } +] [ test.db [ "select * from person" do-simple-query ] with-sqlite ] unit-test + +[ + { { "John" "America" } } +] [ + test.db [ + "select * from person where name = :name and country = :country" + { { ":name" "Jane" } { ":country" "New Zealand" } } + dup [ sql-row ] query-map + + { { "Jane" "New Zealand" } } = [ "test fails" throw ] unless + { { ":name" "John" } { ":country" "America" } } over bind-statement + + dup [ sql-row ] query-map swap dispose + ] with-sqlite +] unit-test + +[ + { + { "1" "John" "America" } + { "2" "Jane" "New Zealand" } + } +] [ test.db [ "select rowid, * from person" do-simple-query ] with-sqlite ] unit-test + +[ +] [ + "extra/db/sqlite/test.db" resource-path [ + "insert into person(name, country) values('Jimmy', 'Canada')" + do-simple-command + ] with-sqlite +] unit-test + +[ + { + { "1" "John" "America" } + { "2" "Jane" "New Zealand" } + { "3" "Jimmy" "Canada" } + } +] [ test.db [ "select rowid, * from person" do-simple-query ] with-sqlite ] unit-test + +[ + "extra/db/sqlite/test.db" resource-path [ + [ + "insert into person(name, country) values('Jose', 'Mexico')" do-simple-command + "insert into person(name, country) values('Jose', 'Mexico')" do-simple-command + "oops" throw + ] with-transaction + ] with-sqlite +] unit-test-fails + +[ 3 ] [ + "extra/db/sqlite/test.db" resource-path [ + "select * from person" do-simple-query length + ] with-sqlite +] unit-test + +[ +] [ + "extra/db/sqlite/test.db" resource-path [ + [ + "insert into person(name, country) values('Jose', 'Mexico')" do-simple-command + "insert into person(name, country) values('Jose', 'Mexico')" do-simple-command + ] with-transaction + ] with-sqlite +] unit-test + +[ 5 ] [ + "extra/db/sqlite/test.db" resource-path [ + "select * from person" do-simple-query length + ] with-sqlite +] unit-test diff --git a/extra/db/sqlite/sqlite.factor b/extra/db/sqlite/sqlite.factor new file mode 100644 index 0000000000..c5964ed599 --- /dev/null +++ b/extra/db/sqlite/sqlite.factor @@ -0,0 +1,70 @@ +! Copyright (C) 2005, 2008 Chris Double, Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: alien arrays assocs classes compiler db db.sql hashtables +io.files kernel math math.parser namespaces prettyprint sequences +strings sqlite.lib tuples alien.c-types continuations +db.sqlite.lib db.sqlite.ffi ; +IN: db.sqlite + +TUPLE: sqlite-db path ; +C: sqlite-db + +M: sqlite-db db-open ( db -- ) + dup sqlite-db-path sqlite-open + swap set-delegate ; + +M: sqlite-db dispose ( obj -- ) + dup db-handle sqlite-close + f over set-db-handle + f swap set-delegate ; + +: with-sqlite ( path quot -- ) + >r r> with-db ; inline + +TUPLE: sqlite-statement ; +C: sqlite-statement + +M: sqlite-db ( str -- obj ) + ; + +M: sqlite-db ( str -- obj ) + ; + +M: sqlite-db ( str -- obj ) + db get db-handle over sqlite-prepare + { set-statement-sql set-statement-handle } statement construct + [ set-delegate ] keep ; + +M: sqlite-db ( str assoc -- obj ) + swap tuck bind-statement ; + +M: sqlite-statement dispose ( statement -- ) + statement-handle sqlite-finalize ; + +M: sqlite-statement bind-statement* ( assoc statement -- ) + statement-handle swap sqlite-bind-assoc ; + +M: sqlite-statement rebind-statement ( assoc statement -- ) + dup reset-statement + statement-handle swap sqlite-bind-assoc ; + +M: sqlite-statement #columns ( statement -- n ) + statement-handle sqlite-#columns ; + +M: sqlite-statement row-column ( statement n -- obj ) + >r statement-handle r> sqlite-column ; + +M: sqlite-statement advance-row ( statement -- ? ) + statement-handle sqlite-next ; + +M: sqlite-statement reset-statement ( statement -- ) + statement-handle sqlite-reset ; + +M: sqlite-db begin-transaction ( -- ) + "BEGIN" do-simple-command ; + +M: sqlite-db commit-transaction ( -- ) + "COMMIT" do-simple-command ; + +M: sqlite-db rollback-transaction ( -- ) + "ROLLBACK" do-simple-command ; diff --git a/extra/db/sqlite/test.txt b/extra/db/sqlite/test.txt new file mode 100644 index 0000000000..e4487d30f9 --- /dev/null +++ b/extra/db/sqlite/test.txt @@ -0,0 +1,3 @@ +create table person (name varchar(30), country varchar(30)); +insert into person values('John', 'America'); +insert into person values('Jane', 'New Zealand'); From 822e859f9430cd5bc63263fe9630e156ed88b884 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 1 Feb 2008 17:44:15 -0600 Subject: [PATCH 009/269] remove old postgresql --- extra/postgresql/authors.txt | 1 - extra/postgresql/libpq/libpq.factor | 361 ----------------------- extra/postgresql/postgresql-tests.factor | 42 --- extra/postgresql/postgresql.factor | 61 ---- 4 files changed, 465 deletions(-) delete mode 100644 extra/postgresql/authors.txt delete mode 100644 extra/postgresql/libpq/libpq.factor delete mode 100644 extra/postgresql/postgresql-tests.factor delete mode 100644 extra/postgresql/postgresql.factor diff --git a/extra/postgresql/authors.txt b/extra/postgresql/authors.txt deleted file mode 100644 index 7c1b2f2279..0000000000 --- a/extra/postgresql/authors.txt +++ /dev/null @@ -1 +0,0 @@ -Doug Coleman diff --git a/extra/postgresql/libpq/libpq.factor b/extra/postgresql/libpq/libpq.factor deleted file mode 100644 index 3b21fd8203..0000000000 --- a/extra/postgresql/libpq/libpq.factor +++ /dev/null @@ -1,361 +0,0 @@ -! Copyright (C) 2007 Doug Coleman. -! See http://factorcode.org/license.txt for BSD license. - -! adapted from libpq-fe.h version 7.4.7 -! tested on debian linux with postgresql 7.4.7 -! Updated to 8.1 - -USING: alien alien.syntax combinators system ; -IN: postgresql.libpq - -<< -"postgresql" { - { [ win32? ] [ "libpq.dll" ] } - { [ macosx? ] [ "/opt/local/lib/postgresql81/libpq.dylib" ] } - { [ unix? ] [ "libpq.so" ] } -} cond "cdecl" add-library ->> - -! ConnSatusType -: CONNECTION_OK HEX: 0 ; inline -: CONNECTION_BAD HEX: 1 ; inline -: CONNECTION_STARTED HEX: 2 ; inline -: CONNECTION_MADE HEX: 3 ; inline -: CONNECTION_AWAITING_RESPONSE HEX: 4 ; inline -: CONNECTION_AUTH_OK HEX: 5 ; inline -: CONNECTION_SETENV HEX: 6 ; inline -: CONNECTION_SSL_STARTUP HEX: 7 ; inline -: CONNECTION_NEEDED HEX: 8 ; inline - -! PostgresPollingStatusType -: PGRES_POLLING_FAILED HEX: 0 ; inline -: PGRES_POLLING_READING HEX: 1 ; inline -: PGRES_POLLING_WRITING HEX: 2 ; inline -: PGRES_POLLING_OK HEX: 3 ; inline -: PGRES_POLLING_ACTIVE HEX: 4 ; inline - -! ExecStatusType; -: PGRES_EMPTY_QUERY HEX: 0 ; inline -: PGRES_COMMAND_OK HEX: 1 ; inline -: PGRES_TUPLES_OK HEX: 2 ; inline -: PGRES_COPY_OUT HEX: 3 ; inline -: PGRES_COPY_IN HEX: 4 ; inline -: PGRES_BAD_RESPONSE HEX: 5 ; inline -: PGRES_NONFATAL_ERROR HEX: 6 ; inline -: PGRES_FATAL_ERROR HEX: 7 ; inline - -! PGTransactionStatusType; -: PQTRANS_IDLE HEX: 0 ; inline -: PQTRANS_ACTIVE HEX: 1 ; inline -: PQTRANS_INTRANS HEX: 2 ; inline -: PQTRANS_INERROR HEX: 3 ; inline -: PQTRANS_UNKNOWN HEX: 4 ; inline - -! PGVerbosity; -: PQERRORS_TERSE HEX: 0 ; inline -: PQERRORS_DEFAULT HEX: 1 ; inline -: PQERRORS_VERBOSE HEX: 2 ; inline - - -TYPEDEF: int size_t -TYPEDEF: int ConnStatusType -TYPEDEF: int ExecStatusType -TYPEDEF: int PostgresPollingStatusType -TYPEDEF: int PGTransactionStatusType -TYPEDEF: int PGVerbosity - -TYPEDEF: void* PGconn* -TYPEDEF: void* PGresult* -TYPEDEF: void* PGcancel* -TYPEDEF: uint Oid -TYPEDEF: uint* Oid* -TYPEDEF: char pqbool -TYPEDEF: void* PQconninfoOption* -TYPEDEF: void* PGnotify* -TYPEDEF: void* PQArgBlock* -TYPEDEF: void* PQprintOpt* -TYPEDEF: void* FILE* -TYPEDEF: void* SSL* - -LIBRARY: postgresql - - -! Exported functions of libpq -! === in fe-connect.c === - -! make a new client connection to the backend -! Asynchronous (non-blocking) -FUNCTION: PGconn* PQconnectStart ( char* conninfo ) ; -FUNCTION: PostgresPollingStatusType PQconnectPoll ( PGconn* conn ) ; - -! Synchronous (blocking) -FUNCTION: PGconn* PQconnectdb ( char* conninfo ) ; -FUNCTION: PGconn* PQsetdbLogin ( char* pghost, char* pgport, - char* pgoptions, char* pgtty, - char* dbName, - char* login, char* pwd ) ; - -: PQsetdb ( M_PGHOST M_PGPORT M_PGOPT M_PGTTY M_DBNAME -- PGconn* ) - f f PQsetdbLogin ; - -! close the current connection and free the PGconn data structure -FUNCTION: void PQfinish ( PGconn* conn ) ; - -! get info about connection options known to PQconnectdb -FUNCTION: PQconninfoOption* PQconndefaults ( ) ; - -! free the data structure returned by PQconndefaults() -FUNCTION: void PQconninfoFree ( PQconninfoOption* connOptions ) ; - -! -! close the current connection and restablish a new one with the same -! parameters -! -! Asynchronous (non-blocking) -FUNCTION: int PQresetStart ( PGconn* conn ) ; -FUNCTION: PostgresPollingStatusType PQresetPoll ( PGconn* conn ) ; - -! Synchronous (blocking) -FUNCTION: void PQreset ( PGconn* conn ) ; - -! request a cancel structure -FUNCTION: PGcancel* PQgetCancel ( PGconn* conn ) ; - -! free a cancel structure -FUNCTION: void PQfreeCancel ( PGcancel* cancel ) ; - -! issue a cancel request -FUNCTION: int PQrequestCancel ( PGconn* conn ) ; - -! Accessor functions for PGconn objects -FUNCTION: char* PQdb ( PGconn* conn ) ; -FUNCTION: char* PQuser ( PGconn* conn ) ; -FUNCTION: char* PQpass ( PGconn* conn ) ; -FUNCTION: char* PQhost ( PGconn* conn ) ; -FUNCTION: char* PQport ( PGconn* conn ) ; -FUNCTION: char* PQtty ( PGconn* conn ) ; -FUNCTION: char* PQoptions ( PGconn* conn ) ; -FUNCTION: ConnStatusType PQstatus ( PGconn* conn ) ; -FUNCTION: PGTransactionStatusType PQtransactionStatus ( PGconn* conn ) ; -FUNCTION: char* PQparameterStatus ( PGconn* conn, - char* paramName ) ; -FUNCTION: int PQprotocolVersion ( PGconn* conn ) ; -FUNCTION: int PQServerVersion ( PGconn* conn ) ; -FUNCTION: char* PQerrorMessage ( PGconn* conn ) ; -FUNCTION: int PQsocket ( PGconn* conn ) ; -FUNCTION: int PQbackendPID ( PGconn* conn ) ; -FUNCTION: int PQclientEncoding ( PGconn* conn ) ; -FUNCTION: int PQsetClientEncoding ( PGconn* conn, char* encoding ) ; - -! May not be compiled into libpq -! Get the SSL structure associated with a connection -FUNCTION: SSL* PQgetssl ( PGconn* conn ) ; - -! Tell libpq whether it needs to initialize OpenSSL -FUNCTION: void PQinitSSL ( int do_init ) ; - -! Set verbosity for PQerrorMessage and PQresultErrorMessage -FUNCTION: PGVerbosity PQsetErrorVerbosity ( PGconn* conn, - PGVerbosity verbosity ) ; - -! Enable/disable tracing -FUNCTION: void PQtrace ( PGconn* conn, FILE* debug_port ) ; -FUNCTION: void PQuntrace ( PGconn* conn ) ; - -! BROKEN -! Function types for notice-handling callbacks -! typedef void (*PQnoticeReceiver) (void *arg, PGresult *res); -! typedef void (*PQnoticeProcessor) (void *arg, char* message); -! ALIAS: void* PQnoticeReceiver -! ALIAS: void* PQnoticeProcessor - -! Override default notice handling routines -! FUNCTION: PQnoticeReceiver PQsetNoticeReceiver ( PGconn* conn, - ! PQnoticeReceiver proc, - ! void* arg ) ; -! FUNCTION: PQnoticeProcessor PQsetNoticeProcessor ( PGconn* conn, - ! PQnoticeProcessor proc, - ! void* arg ) ; -! END BROKEN - -! === in fe-exec.c === - -! Simple synchronous query -FUNCTION: PGresult* PQexec ( PGconn* conn, char* query ) ; -FUNCTION: PGresult* PQexecParams ( PGconn* conn, - char* command, - int nParams, - Oid* paramTypes, - char** paramValues, - int* paramLengths, - int* paramFormats, - int resultFormat ) ; -FUNCTION: PGresult* PQprepare ( PGconn* conn, char* stmtName, - char* query, int nParams, - Oid* paramTypes ) ; -FUNCTION: PGresult* PQexecPrepared ( PGconn* conn, - char* stmtName, - int nParams, - char** paramValues, - int* paramLengths, - int* paramFormats, - int resultFormat ) ; - -! Interface for multiple-result or asynchronous queries -FUNCTION: int PQsendQuery ( PGconn* conn, char* query ) ; -FUNCTION: int PQsendQueryParams ( PGconn* conn, - char* command, - int nParams, - Oid* paramTypes, - char** paramValues, - int* paramLengths, - int* paramFormats, - int resultFormat ) ; -FUNCTION: PGresult* PQsendPrepare ( PGconn* conn, char* stmtName, - char* query, int nParams, - Oid* paramTypes ) ; -FUNCTION: int PQsendQueryPrepared ( PGconn* conn, - char* stmtName, - int nParams, - char** paramValues, - int *paramLengths, - int *paramFormats, - int resultFormat ) ; -FUNCTION: PGresult* PQgetResult ( PGconn* conn ) ; - -! Routines for managing an asynchronous query -FUNCTION: int PQisBusy ( PGconn* conn ) ; -FUNCTION: int PQconsumeInput ( PGconn* conn ) ; - -! LISTEN/NOTIFY support -FUNCTION: PGnotify* PQnotifies ( PGconn* conn ) ; - -! Routines for copy in/out -FUNCTION: int PQputCopyData ( PGconn* conn, char* buffer, int nbytes ) ; -FUNCTION: int PQputCopyEnd ( PGconn* conn, char* errormsg ) ; -FUNCTION: int PQgetCopyData ( PGconn* conn, char** buffer, int async ) ; - -! Deprecated routines for copy in/out -FUNCTION: int PQgetline ( PGconn* conn, char* string, int length ) ; -FUNCTION: int PQputline ( PGconn* conn, char* string ) ; -FUNCTION: int PQgetlineAsync ( PGconn* conn, char* buffer, int bufsize ) ; -FUNCTION: int PQputnbytes ( PGconn* conn, char* buffer, int nbytes ) ; -FUNCTION: int PQendcopy ( PGconn* conn ) ; - -! Set blocking/nonblocking connection to the backend -FUNCTION: int PQsetnonblocking ( PGconn* conn, int arg ) ; -FUNCTION: int PQisnonblocking ( PGconn* conn ) ; - -! Force the write buffer to be written (or at least try) -FUNCTION: int PQflush ( PGconn* conn ) ; - -! -! * "Fast path" interface --- not really recommended for application -! * use -! -FUNCTION: PGresult* PQfn ( PGconn* conn, - int fnid, - int* result_buf, - int* result_len, - int result_is_int, - PQArgBlock* args, - int nargs ) ; - -! Accessor functions for PGresult objects -FUNCTION: ExecStatusType PQresultStatus ( PGresult* res ) ; -FUNCTION: char* PQresStatus ( ExecStatusType status ) ; -FUNCTION: char* PQresultErrorMessage ( PGresult* res ) ; -FUNCTION: char* PQresultErrorField ( PGresult* res, int fieldcode ) ; -FUNCTION: int PQntuples ( PGresult* res ) ; -FUNCTION: int PQnfields ( PGresult* res ) ; -FUNCTION: int PQbinaryTuples ( PGresult* res ) ; -FUNCTION: char* PQfname ( PGresult* res, int field_num ) ; -FUNCTION: int PQfnumber ( PGresult* res, char* field_name ) ; -FUNCTION: Oid PQftable ( PGresult* res, int field_num ) ; -FUNCTION: int PQftablecol ( PGresult* res, int field_num ) ; -FUNCTION: int PQfformat ( PGresult* res, int field_num ) ; -FUNCTION: Oid PQftype ( PGresult* res, int field_num ) ; -FUNCTION: int PQfsize ( PGresult* res, int field_num ) ; -FUNCTION: int PQfmod ( PGresult* res, int field_num ) ; -FUNCTION: char* PQcmdStatus ( PGresult* res ) ; -FUNCTION: char* PQoidStatus ( PGresult* res ) ; -FUNCTION: Oid PQoidValue ( PGresult* res ) ; -FUNCTION: char* PQcmdTuples ( PGresult* res ) ; -FUNCTION: char* PQgetvalue ( PGresult* res, int tup_num, int field_num ) ; -FUNCTION: int PQgetlength ( PGresult* res, int tup_num, int field_num ) ; -FUNCTION: int PQgetisnull ( PGresult* res, int tup_num, int field_num ) ; - -! Delete a PGresult -FUNCTION: void PQclear ( PGresult* res ) ; - -! For freeing other alloc'd results, such as PGnotify structs -FUNCTION: void PQfreemem ( void* ptr ) ; - -! Exists for backward compatibility. -: PQfreeNotify PQfreemem ; - -! -! Make an empty PGresult with given status (some apps find this -! useful). If conn is not NULL and status indicates an error, the -! conn's errorMessage is copied. -! -FUNCTION: PGresult* PQmakeEmptyPGresult ( PGconn* conn, ExecStatusType status ) ; - -! Quoting strings before inclusion in queries. -FUNCTION: size_t PQescapeStringConn ( PGconn* conn, - char* to, char* from, size_t length, - int* error ) ; -FUNCTION: uchar* PQescapeByteaConn ( PGconn* conn, - char* from, size_t length, - size_t* to_length ) ; -FUNCTION: uchar* PQunescapeBytea ( uchar* strtext, - size_t* retbuflen ) ; -! These forms are deprecated! -FUNCTION: size_t PQescapeString ( void* to, char* from, size_t length ) ; -FUNCTION: uchar* PQescapeBytea ( uchar* bintext, size_t binlen, - size_t* bytealen ) ; - -! === in fe-print.c === - -FUNCTION: void PQprint ( FILE* fout, PGresult* res, PQprintOpt* ps ) ; - -! really old printing routines -FUNCTION: void PQdisplayTuples ( PGresult* res, - FILE* fp, - int fillAlign, - char* fieldSep, - int printHeader, - int quiet ) ; - -FUNCTION: void PQprintTuples ( PGresult* res, - FILE* fout, - int printAttName, - int terseOutput, - int width ) ; - -! === in fe-lobj.c === - -! Large-object access routines -FUNCTION: int lo_open ( PGconn* conn, Oid lobjId, int mode ) ; -FUNCTION: int lo_close ( PGconn* conn, int fd ) ; -FUNCTION: int lo_read ( PGconn* conn, int fd, char* buf, size_t len ) ; -FUNCTION: int lo_write ( PGconn* conn, int fd, char* buf, size_t len ) ; -FUNCTION: int lo_lseek ( PGconn* conn, int fd, int offset, int whence ) ; -FUNCTION: Oid lo_creat ( PGconn* conn, int mode ) ; -! FUNCTION: Oid lo_creat ( PGconn* conn, Oid lobjId ) ; -FUNCTION: int lo_tell ( PGconn* conn, int fd ) ; -FUNCTION: int lo_unlink ( PGconn* conn, Oid lobjId ) ; -FUNCTION: Oid lo_import ( PGconn* conn, char* filename ) ; -FUNCTION: int lo_export ( PGconn* conn, Oid lobjId, char* filename ) ; - -! === in fe-misc.c === - -! Determine length of multibyte encoded char at *s -FUNCTION: int PQmblen ( uchar* s, int encoding ) ; - -! Determine display length of multibyte encoded char at *s -FUNCTION: int PQdsplen ( uchar* s, int encoding ) ; - -! Get encoding id from environment variable PGCLIENTENCODING -FUNCTION: int PQenv2encoding ( ) ; - diff --git a/extra/postgresql/postgresql-tests.factor b/extra/postgresql/postgresql-tests.factor deleted file mode 100644 index c725882b67..0000000000 --- a/extra/postgresql/postgresql-tests.factor +++ /dev/null @@ -1,42 +0,0 @@ -! You will need to run 'createdb factor-test' to create the database. -! Set username and password in the 'connect' word. - -IN: postgresql-test -USING: kernel postgresql alien continuations io prettyprint -sequences namespaces ; - - -: test-connection ( host port pgopts pgtty db user pass -- bool ) - [ [ ] with-postgres ] catch "Error connecting!" "Connected!" ? print ; - -! just a basic demo - -"localhost" "" "" "" "test" "postgres" "" [ - "drop table animal" do-command - - "create table animal (id serial not null primary key, species varchar(256), name varchar(256), age integer)" do-command - "insert into animal (species, name, age) values ('lion', 'Mufasa', 5)" - do-command - - "select * from animal where name = 'Mufasa'" [ ] do-query - "select * from animal where name = 'Mufasa'" - [ - result>seq length 1 = [ "...there can only be one Mufasa..." throw ] unless - ] do-query - - "insert into animal (species, name, age) values ('lion', 'Simba', 1)" - do-command - - "select * from animal" - [ - "Animal table:" print - result>seq print-table - ] do-query - - ! intentional errors - ! [ "select asdf from animal" - ! [ ] do-query ] catch [ "caught: " write print ] when* - ! "select asdf from animal" [ ] do-query - ! "aofijweafew" do-command -] with-postgres - diff --git a/extra/postgresql/postgresql.factor b/extra/postgresql/postgresql.factor deleted file mode 100644 index 9d85b6a77e..0000000000 --- a/extra/postgresql/postgresql.factor +++ /dev/null @@ -1,61 +0,0 @@ -! Copyright (C) 2007 Doug Coleman. -! See http://factorcode.org/license.txt for BSD license. - -! adapted from libpq-fe.h version 7.4.7 -! tested on debian linux with postgresql 7.4.7 - -USING: arrays alien alien.syntax continuations io -kernel math namespaces postgresql.libpq prettyprint -quotations sequences debugger ; -IN: postgresql - -SYMBOL: db -SYMBOL: query-res - -: connect-postgres ( host port pgopts pgtty db user pass -- conn ) - PQsetdbLogin - dup PQstatus zero? [ "couldn't connect to database" throw ] unless ; - -: with-postgres ( host port pgopts pgtty db user pass quot -- ) - [ >r connect-postgres db set r> - [ db get PQfinish ] [ ] cleanup ] with-scope ; inline - -: postgres-error ( ret -- ret ) - dup zero? [ PQresultErrorMessage throw ] when ; - -: (do-query) ( PGconn query -- PGresult* ) - ! For queries that do not return rows, PQexec() returns PGRES_COMMAND_OK - ! For queries that return rows, PQexec() returns PGRES_TUPLES_OK - PQexec - dup PQresultStatus PGRES_COMMAND_OK = - over PQresultStatus PGRES_TUPLES_OK = - or [ - [ PQresultErrorMessage CHAR: \n swap remove ] keep PQclear throw - ] unless ; - -: (do-command) ( PGconn query -- PGresult* ) - [ (do-query) ] catch - [ - swap - "non-fatal error: " print - "\tQuery: " write "'" write write "'" print - "\t" write print - ] when* drop ; - -: do-command ( str -- ) - 1quotation \ (do-command) add db get swap call ; - -: prepare ( str quot word -- conn quot ) - rot 1quotation swap append swap append db get swap ; - -: do-query ( str quot -- ) - [ (do-query) query-res set ] prepare catch - [ rethrow ] [ query-res get PQclear ] if* ; - -: result>seq ( -- seq ) - query-res get [ PQnfields ] keep PQntuples - [ swap [ query-res get -rot PQgetvalue ] with map ] with map ; - -: print-table ( seq -- ) - [ [ write bl ] each "\n" write ] each ; - From 161c3ec1560dbcc32f3006bac38b49a8a62a0338 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 1 Feb 2008 17:45:34 -0600 Subject: [PATCH 010/269] remove sqlite and tupledb for now --- extra/sqlite/authors.txt | 1 - extra/sqlite/lib/authors.txt | 1 - extra/sqlite/lib/lib.factor | 120 --------- extra/sqlite/sqlite-docs.factor | 87 ------- extra/sqlite/sqlite-tests.factor | 69 ----- extra/sqlite/sqlite.factor | 127 --------- extra/sqlite/test.txt | 3 - extra/sqlite/tuple-db/authors.txt | 1 - extra/sqlite/tuple-db/tuple-db-docs.factor | 131 ---------- extra/sqlite/tuple-db/tuple-db-tests.factor | 39 --- extra/sqlite/tuple-db/tuple-db.factor | 270 -------------------- 11 files changed, 849 deletions(-) delete mode 100755 extra/sqlite/authors.txt delete mode 100755 extra/sqlite/lib/authors.txt delete mode 100644 extra/sqlite/lib/lib.factor delete mode 100644 extra/sqlite/sqlite-docs.factor delete mode 100644 extra/sqlite/sqlite-tests.factor delete mode 100644 extra/sqlite/sqlite.factor delete mode 100644 extra/sqlite/test.txt delete mode 100755 extra/sqlite/tuple-db/authors.txt delete mode 100644 extra/sqlite/tuple-db/tuple-db-docs.factor delete mode 100644 extra/sqlite/tuple-db/tuple-db-tests.factor delete mode 100644 extra/sqlite/tuple-db/tuple-db.factor diff --git a/extra/sqlite/authors.txt b/extra/sqlite/authors.txt deleted file mode 100755 index 44b06f94bc..0000000000 --- a/extra/sqlite/authors.txt +++ /dev/null @@ -1 +0,0 @@ -Chris Double diff --git a/extra/sqlite/lib/authors.txt b/extra/sqlite/lib/authors.txt deleted file mode 100755 index 44b06f94bc..0000000000 --- a/extra/sqlite/lib/authors.txt +++ /dev/null @@ -1 +0,0 @@ -Chris Double diff --git a/extra/sqlite/lib/lib.factor b/extra/sqlite/lib/lib.factor deleted file mode 100644 index 438f22a80f..0000000000 --- a/extra/sqlite/lib/lib.factor +++ /dev/null @@ -1,120 +0,0 @@ -! Copyright (C) 2005 Chris Double, Doug Coleman. -! See http://factorcode.org/license.txt for BSD license. -! -! An interface to the sqlite database. Tested against sqlite v3.1.3. -! Remeber to pass the following to factor: -! -libraries:sqlite=libsqlite3.so -! -! Not all functions have been wrapped yet. Only those directly involving -! executing SQL calls and obtaining results. -! -IN: sqlite.lib -USING: alien compiler kernel math namespaces sequences strings alien.syntax - system combinators ; - -<< -"sqlite" { - { [ win32? ] [ "sqlite3.dll" ] } - { [ macosx? ] [ "/usr/lib/libsqlite3.dylib" ] } - { [ unix? ] [ "libsqlite3.so" ] } -} cond "cdecl" add-library ->> - -! Return values from sqlite functions -: SQLITE_OK 0 ; inline ! Successful result -: SQLITE_ERROR 1 ; inline ! SQL error or missing database -: SQLITE_INTERNAL 2 ; inline ! An internal logic error in SQLite -: SQLITE_PERM 3 ; inline ! Access permission denied -: SQLITE_ABORT 4 ; inline ! Callback routine requested an abort -: SQLITE_BUSY 5 ; inline ! The database file is locked -: SQLITE_LOCKED 6 ; inline ! A table in the database is locked -: SQLITE_NOMEM 7 ; inline ! A malloc() failed -: SQLITE_READONLY 8 ; inline ! Attempt to write a readonly database -: SQLITE_INTERRUPT 9 ; inline ! Operation terminated by sqlite_interrupt() -: SQLITE_IOERR 10 ; inline ! Some kind of disk I/O error occurred -: SQLITE_CORRUPT 11 ; inline ! The database disk image is malformed -: SQLITE_NOTFOUND 12 ; inline ! (Internal Only) Table or record not found -: SQLITE_FULL 13 ; inline ! Insertion failed because database is full -: SQLITE_CANTOPEN 14 ; inline ! Unable to open the database file -: SQLITE_PROTOCOL 15 ; inline ! Database lock protocol error -: SQLITE_EMPTY 16 ; inline ! (Internal Only) Database table is empty -: SQLITE_SCHEMA 17 ; inline ! The database schema changed -: SQLITE_TOOBIG 18 ; inline ! Too much data for one row of a table -: SQLITE_CONSTRAINT 19 ; inline ! Abort due to contraint violation -: SQLITE_MISMATCH 20 ; inline ! Data type mismatch -: SQLITE_MISUSE 21 ; inline ! Library used incorrectly -: SQLITE_NOLFS 22 ; inline ! Uses OS features not supported on host -: SQLITE_AUTH 23 ; inline ! Authorization denied -: SQLITE_FORMAT 24 ; inline ! Auxiliary database format error -: SQLITE_RANGE 25 ; inline ! 2nd parameter to sqlite3_bind out of range -: SQLITE_NOTADB 26 ; inline ! File opened that is not a database file - -: sqlite-error-messages ( -- seq ) { - "Successful result" - "SQL error or missing database" - "An internal logic error in SQLite" - "Access permission denied" - "Callback routine requested an abort" - "The database file is locked" - "A table in the database is locked" - "A malloc() failed" - "Attempt to write a readonly database" - "Operation terminated by sqlite_interrupt()" - "Some kind of disk I/O error occurred" - "The database disk image is malformed" - "(Internal Only) Table or record not found" - "Insertion failed because database is full" - "Unable to open the database file" - "Database lock protocol error" - "(Internal Only) Database table is empty" - "The database schema changed" - "Too much data for one row of a table" - "Abort due to contraint violation" - "Data type mismatch" - "Library used incorrectly" - "Uses OS features not supported on host" - "Authorization denied" - "Auxiliary database format error" - "2nd parameter to sqlite3_bind out of range" - "File opened that is not a database file" -} ; - -: SQLITE_ROW 100 ; inline ! sqlite_step() has another row ready -: SQLITE_DONE 101 ; inline ! sqlite_step() has finished executing - -! Return values from the sqlite3_column_type function -: SQLITE_INTEGER 1 ; inline -: SQLITE_FLOAT 2 ; inline -: SQLITE_TEXT 3 ; inline -: SQLITE_BLOB 4 ; inline -: SQLITE_NULL 5 ; inline - -! Values for the 'destructor' parameter of the 'bind' routines. -: SQLITE_STATIC 0 ; inline -: SQLITE_TRANSIENT -1 ; inline - -TYPEDEF: void sqlite3 -TYPEDEF: void sqlite3_stmt - -LIBRARY: sqlite -FUNCTION: int sqlite3_open ( char* filename, void* ppDb ) ; -FUNCTION: int sqlite3_close ( sqlite3* pDb ) ; -FUNCTION: int sqlite3_prepare ( sqlite3* pDb, char* zSql, int nBytes, void* ppStmt, void* pzTail ) ; -FUNCTION: int sqlite3_finalize ( sqlite3_stmt* pStmt ) ; -FUNCTION: int sqlite3_reset ( sqlite3_stmt* pStmt ) ; -FUNCTION: int sqlite3_step ( sqlite3_stmt* pStmt ) ; -FUNCTION: int sqlite3_last_insert_rowid ( sqlite3* pStmt ) ; -FUNCTION: int sqlite3_bind_blob ( sqlite3_stmt* pStmt, int index, void* ptr, int len, int destructor ) ; -FUNCTION: int sqlite3_bind_int ( sqlite3_stmt* pStmt, int index, int n ) ; -FUNCTION: int sqlite3_bind_null ( sqlite3_stmt* pStmt, int n ) ; -FUNCTION: int sqlite3_bind_text ( sqlite3_stmt* pStmt, int index, char* text, int len, int destructor ) ; -FUNCTION: int sqlite3_bind_parameter_index ( sqlite3_stmt* pStmt, char* name ) ; -FUNCTION: int sqlite3_column_count ( sqlite3_stmt* pStmt ) ; -FUNCTION: void* sqlite3_column_blob ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: int sqlite3_column_bytes ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: char* sqlite3_column_decltype ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: int sqlite3_column_int ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: int sqlite3_column_name ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: char* sqlite3_column_text ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: int sqlite3_column_type ( sqlite3_stmt* pStmt, int col ) ; - diff --git a/extra/sqlite/sqlite-docs.factor b/extra/sqlite/sqlite-docs.factor deleted file mode 100644 index d58b553f11..0000000000 --- a/extra/sqlite/sqlite-docs.factor +++ /dev/null @@ -1,87 +0,0 @@ -! Copyright (C) 2006 Chris Double. -! See http://factorcode.org/license.txt for BSD license. -USING: help help.syntax help.markup ; -IN: sqlite - -HELP: sqlite-open -{ $values { "filename" "path to sqlite database" } - { "db" "the database object" } -} -{ $description "Opens the sqlite3 database." } -{ $see-also sqlite-close sqlite-last-insert-rowid } ; - -HELP: sqlite-close -{ $values { "db" "the database object" } -} -{ $description "Closes the sqlite3 database." } -{ $see-also sqlite-open sqlite-last-insert-rowid } ; - -HELP: sqlite-last-insert-rowid -{ $values { "db" "the database object" } - { "rowid" "the row number of the last insert" } -} -{ $description "Returns the number of the row of the last statement inserted into the database." } -{ $see-also sqlite-open sqlite-close } ; - -HELP: sqlite-prepare -{ $values { "db" "the database object" } - { "sql" "the SQL statement as a string" } - { "statement" "the prepared SQL statement" } -} -{ $description "Internally compiles the SQL statement ready to be run by sqlite. The statement is executed and the results iterated over using " { $link sqlite-each } " and " { $link sqlite-map } ". The SQL statement can use named parameters which are later bound to values using " { $link sqlite-bind-text } " and " { $link sqlite-bind-text-by-name } "." } -{ $see-also sqlite-open sqlite-close } ; - -HELP: sqlite-bind-text -{ $values { "statement" "a prepared SQL statement" } - { "index" "the index of the bound parameter in the SQL statement" } - { "text" "the string value to bind to that column" } - -} -{ $description "Binds the text to a parameter in the SQL statement. The parameter to be bound is identified by the index given and the indexes start from one." } -{ $examples { $code "\"people.db\" sqlite-open\n\"select * from people where name=?\" sqlite-prepare\n1 \"chris\" sqlite-bind-text" } } -{ $see-also sqlite-bind-text-by-name } ; - -HELP: sqlite-bind-text-by-name -{ $values { "statement" "a prepared SQL statement" } - { "name" "the name of the bound parameter in the SQL statement" } - { "text" "the string value to bind to that column" } - -} -{ $description "Binds the text to a parameter in the SQL statement. The parameter to be bound is identified by the given name." } -{ $examples { $code "\"people.db\" sqlite-open\n\"select * from people where name=:name\" sqlite-prepare\n\"name\" \"chris\" sqlite-bind-text" } } -{ $see-also sqlite-bind-text } ; - -HELP: sqlite-finalize -{ $values { "statement" "a prepared SQL statement" } -} -{ $description "Clean up all resources related to a statement. Once called the statement cannot be used again. All statements must be finalized before closing the database." } -{ $see-also sqlite-close sqlite-prepare } ; - -HELP: sqlite-reset -{ $values { "statement" "a prepared SQL statement" } -} -{ $description "Reset a statement so it can be called again, possibly with different bound parameters." } -{ $see-also sqlite-bind-text sqlite-bind-text-by-name } ; - -HELP: column-count -{ $values { "statement" "a prepared SQL statement" } { "int" "the number of columns" } } -{ $description "Return the number of columns in each row of the result set of the given statement." } -{ $see-also column-text sqlite-each sqlite-map } ; - -HELP: column-text -{ $values { "statement" "a prepared SQL statement" } { "index" "column number indexed from zero" } { "string" "column value" } -} -{ $description "Return the value of the given column, indexed from zero, as a string." } -{ $see-also column-count sqlite-each sqlite-map } ; - -HELP: sqlite-each -{ $values { "statement" "a prepared SQL statement" } { "quot" "A quotation with stack effect ( statement -- )" } -} -{ $description "Executes the SQL statement and for each returned row calls the qutotation passing the statement on the stack. The quotation can use " { $link column-text } " to get result values for that row." } -{ $see-also column-count column-text sqlite-map } ; - -HELP: sqlite-map -{ $values { "statement" "a prepared SQL statement" } { "quot" "A quotation with stack effect ( statement -- value )" } { "seq" "a new sequence" } -} -{ $description "Executes the SQL statement and for each returned row calls the qutotation passing the statement on the stack. The quotation can use " { $link column-text } " to get result values for that row. The quotation should leave a value on the stack which gets collected and returned in the resulting sequence." } -{ $see-also column-count column-text sqlite-each } ; diff --git a/extra/sqlite/sqlite-tests.factor b/extra/sqlite/sqlite-tests.factor deleted file mode 100644 index 5eecbec369..0000000000 --- a/extra/sqlite/sqlite-tests.factor +++ /dev/null @@ -1,69 +0,0 @@ -! Copyright (C) 2005 Chris Double. -! -! Redistribution and use in source and binary forms, with or without -! modification, are permitted provided that the following conditions are met: -! -! 1. Redistributions of source code must retain the above copyright notice, -! this list of conditions and the following disclaimer. -! -! 2. Redistributions in binary form must reproduce the above copyright notice, -! this list of conditions and the following disclaimer in the documentation -! and/or other materials provided with the distribution. -! -! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, -! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -! -! Test the sqlite interface -! -! Create a test database like follows: -! -! sqlite3 test.db < test.txt -! -! Then run this file. -USE: sqlite -USE: kernel -USE: io -USE: io.files -USE: prettyprint - -: test.db "libs/sqlite/test.db" resource-path ; - -: show-people ( statement -- ) - dup 0 column-text write " from " write 1 column-text . ; - -: run-test ( -- ) - test.db sqlite-open - dup "select * from test" sqlite-prepare - dup [ show-people ] sqlite-each - sqlite-finalize - sqlite-close ; - -: find-person ( name -- ) - test.db sqlite-open ! name db - dup "select * from test where name=?" sqlite-prepare ! name db stmt - [ rot 1 swap sqlite-bind-text ] keep ! db stmt - [ [ 1 column-text . ] sqlite-each ] keep - sqlite-finalize - sqlite-close ; - -: find-all ( -- ) - test.db sqlite-open ! db - dup "select * from test" sqlite-prepare ! db stmt - [ [ [ 0 column-text ] keep 1 column-text curry ] sqlite-map ] keep - sqlite-finalize - swap sqlite-close ; - -: run-test2 ( -- ) - test.db sqlite-open - dup "select * from test" sqlite-prepare - dup [ show-people ] ; - -run-test diff --git a/extra/sqlite/sqlite.factor b/extra/sqlite/sqlite.factor deleted file mode 100644 index d651ad916c..0000000000 --- a/extra/sqlite/sqlite.factor +++ /dev/null @@ -1,127 +0,0 @@ -! Copyright (C) 2005 Chris Double. -! See http://factorcode.org/license.txt for BSD license. -! -! An interface to the sqlite database. Tested against sqlite v3.0.8. -! -! Not all functions have been wrapped yet. Only those directly involving -! executing SQL calls and obtaining results. -! -IN: sqlite -USING: alien compiler kernel namespaces sequences strings sqlite.lib - alien.c-types continuations ; - -TUPLE: sqlite-error n message ; -SYMBOL: db - -! High level sqlite routines -: sqlite-check-result ( result -- ) - #! Check the result from a sqlite call is ok. If it is - #! return, otherwise throw an error. - dup SQLITE_OK = [ - drop - ] [ - dup sqlite-error-messages nth - \ sqlite-error construct-boa throw - ] if ; - -: sqlite-open ( filename -- db ) - #! Open the database referenced by the filename and return - #! a handle to that database. An error is thrown if the database - #! failed to open. - "void*" [ sqlite3_open sqlite-check-result ] keep *void* ; - -: sqlite-close ( db -- ) - #! Close the given database - sqlite3_close sqlite-check-result ; - -: sqlite-last-insert-rowid ( db -- rowid ) - #! Return the rowid of the last insert - sqlite3_last_insert_rowid ; - -: sqlite-prepare ( db sql -- statement ) - #! Prepare a SQL statement. Returns the statement which - #! can have values bound to parameters or simply executed. - #! TODO: Support multiple statements in the SQL string. - dup length "void*" "void*" - [ sqlite3_prepare sqlite-check-result ] 2keep - drop *void* ; - -: sqlite-bind-text ( statement index text -- ) - #! Bind the text to the parameterized value in the statement. - dup length SQLITE_TRANSIENT sqlite3_bind_text sqlite-check-result ; - -: sqlite-bind-parameter-index ( statement name -- index ) - sqlite3_bind_parameter_index ; - -: sqlite-bind-text-by-name ( statement name text -- ) - >r dupd sqlite-bind-parameter-index r> sqlite-bind-text ; - -: sqlite-finalize ( statement -- ) - #! Clean up all resources related to a statement. Once called - #! the statement cannot be used. All statements must be finalized - #! before closing the database. - sqlite3_finalize sqlite-check-result ; - -: sqlite-reset ( statement -- ) - #! Reset a statement so it can be called again, possibly with - #! different parameters. - sqlite3_reset sqlite-check-result ; - -: column-count ( statement -- int ) - #! Given a prepared statement, return the number of - #! columns in each row of the result set of that statement. - sqlite3_column_count ; - -: column-text ( statement index -- string ) - #! Return the value of the given column, indexed - #! from zero, as a string. - sqlite3_column_text ; - -: step-complete? ( step-result -- bool ) - #! Return true if the result of a sqlite3_step is - #! such that the iteration has completed (ie. it is - #! SQLITE_DONE). Throw an error if an error occurs. - dup SQLITE_ROW = [ - drop f - ] [ - dup SQLITE_DONE = [ - drop t - ] [ - sqlite-check-result t - ] if - ] if ; - -: sqlite-each ( statement quot -- ) - #! Execute the SQL statement, and call the quotation for - #! each row returned from executing the statement with the - #! statement on the top of the stack. - over sqlite3_step step-complete? [ - 2drop - ] [ - [ call ] 2keep sqlite-each - ] if ; inline - -! For comparison, here is the linrec implementation of sqlite-each -! [ drop sqlite3_step step-complete? ] -! [ 2drop ] -! [ 2dup 2slip ] -! [ ] linrec ; - -DEFER: (sqlite-map) - -: (sqlite-map) ( statement quot seq -- ) - pick sqlite3_step step-complete? [ - 2nip - ] [ - >r 2dup call r> swap add (sqlite-map) - ] if ; - -: sqlite-map ( statement quot -- seq ) - { } (sqlite-map) ; - -: with-sqlite ( path quot -- ) - [ - >r sqlite-open db set r> - [ db get sqlite-close ] [ ] cleanup - ] with-scope ; - diff --git a/extra/sqlite/test.txt b/extra/sqlite/test.txt deleted file mode 100644 index 5c7ae2b52a..0000000000 --- a/extra/sqlite/test.txt +++ /dev/null @@ -1,3 +0,0 @@ -create table test (name varchar(30), address varchar(30)); -insert into test values('John', 'America'); -insert into test values('Jane', 'New Zealand'); diff --git a/extra/sqlite/tuple-db/authors.txt b/extra/sqlite/tuple-db/authors.txt deleted file mode 100755 index 44b06f94bc..0000000000 --- a/extra/sqlite/tuple-db/authors.txt +++ /dev/null @@ -1 +0,0 @@ -Chris Double diff --git a/extra/sqlite/tuple-db/tuple-db-docs.factor b/extra/sqlite/tuple-db/tuple-db-docs.factor deleted file mode 100644 index 3c6df0eaa6..0000000000 --- a/extra/sqlite/tuple-db/tuple-db-docs.factor +++ /dev/null @@ -1,131 +0,0 @@ -! Copyright (C) 2006 Chris Double. -! See http://factorcode.org/license.txt for BSD license. -USING: help sqlite help.syntax help.markup ; -IN: sqlite.tuple-db - -ARTICLE: { "sqlite" "tuple-db-loading" } "Loading" -"The quickest way to get up and running with this library is to use the vocabulary:" -{ $code "USING: sqlite sqlite.tuple-db ;\n" } -"Some simple tests can be run to check that everything is working ok:" -{ $code "\"libs/sqlite\" test-module" } ; - -ARTICLE: { "sqlite" "tuple-db-usage" } "Basic Usage" -"This library can be used for storing simple Factor tuples in a sqlite database. In its current form the tuples must not contain references to other tuples and should not have a delegate set." -$nl -"This document will use the following tuple for demonstration purposes:" -{ $code "TUPLE: person name surname phone ;" } -"The sqlite database to store tuples must be created, or an existing one opened. This is done using the " { $link sqlite-open } " word. If the database does not exist then it is created. The examples in this document store the database pointer in a variable called 'db':" -{ $code "SYMBOL: db\n\"example.db\" sqlite-open db set-global" } ; - -ARTICLE: { "sqlite" "tuple-db-mappings" } "Tuple Mappings" -"Each tuple has a 'mapping' tuple associated with it. The 'mapping' stores information about what table the tuple will be stored in, the datatypes of the tuple slots, etc. A mapping must be created before a tuple can be stored in a database. A default mapping is easily created using " { $link default-mapping } ". Given the tuple class, this will use reflection to get the slots of it, assume that all slots are of database type 'text', and store the tuple objects in a table with the same name as the tuple." -$nl -"The following shows how to create the default mapping for the 'person' tuple, and how to register that mapping so the 'tuple-db' system can know how to handle 'person' instances:" -{ $code "person default-mapping set-mapping" } ; - -ARTICLE: { "sqlite" "tuple-db-create" } "Creating the table" -"The table used to store tuple instances may need to be created. This can be done manually using the external sqlite program or via " { $link create-tuple-table } ":" -{ $code "db get person create-tuple-table" } -"The SQL used to create the table is produced internally by " { $link create-sql } ". This is a generic word dispatched on the mapping object, and could be specialised if needed. If you wish to see the SQL used to create the table, use the following code:" -{ $code "person get-mapping create-sql .\n => \"create table person (name text,surname text,phone text);\"" } ; - -ARTICLE: { "sqlite" "tuple-db-insert" } "Inserting instances" -"The " { $link insert-tuple } " word will store instances of a tuple into the database table defined by its mapping object:" -{ $code "db get \"John\" \"Smith\" \"123-456-789\" insert-tuple" } -{ $link insert-tuple } " internally uses the " { $link insert-sql } " word to produce the SQL used to store the tuple. Like " { $link create-sql } ", it is a generic word specialized on the mapping object. You can call it directly to see what SQL is generated:" -{ $code "person get-mapping insert-sql .\n => \"insert into person values(:name,:surname,:phone);\"" } -"Notice that the SQL uses named parameters. These parameters are bound to the values stored in the tuple object when the SQL is compiled. This helps prevent SQL injection techniques." -$nl -"When " { $link insert-sql } " is run, it adds a delegate to the tuple being stored. The delegate is of type 'persistent' and holds the row id of the tuple in its 'key' slot. This way the exact record can be updated or retrieved later. The following demonstates this fact:" -{ $code "\"Mandy\" \"Jones\" \"987-654-321\" dup .\n => T{ person f \"Mandy\" \"Jones\" \"987-654-321\" }\ndb get over insert-tuple .\n => T{ person T{ persistent ... 2 } \"Mandy\" \"Jones\" \"987-654-321\" }" } -"The '2' in the above example is the row id of the record inserted. We can go into the 'sqlite' command and view this record:" -{ $code " $ sqlite3 example.db\n SQLite version 3.0.8\n Enter \".help\" for instructions\n sqlite> select ROWID,* from person;\n 1|John|Smith|123-456-789\n 2|Mandy|Jones|987-654-321\n sqlite>" } ; - -ARTICLE: { "sqlite" "tuple-db-finding" } "Finding instances" -"The " { $link find-tuples } " word is used to return tuples populated with data already existing in the database. As well as the database objcet, it takes a tuple that should be populated only with the fields that should be matched in the database. All fields you do not wish to match against should be set to 'f':" -{ $code "db get f \"Smith\" f find-tuples .\n => { T{ person # \"John\" \"Smith\" \"123-456-789\" } }\ndb get \"Mandy\" f f find-tuples .\n => { T{ person # \"Mandy\" \"Jones\" \"987-654-321\" } }\ndb get \"Joe\" f f find-tuples .\n => { }" } -"Notice that if no matching tuples are found then an empty sequence is returned. The returned tuples also have their delegate set to 'persistent' with the correct row id set as the key. This can be used to later update the tuples with new information and store them in the database." ; - -ARTICLE: { "sqlite" "tuple-db-updating" } "Updating instances" -"Given a tuple that has the 'persistent' delegate with the row id set as the key, you can update this specific record using " { $link update-tuple } ":" -{ $code "db get f \"Smith\" f find-tuples dup .\n => { T{ person # \"John\" \"Smith\" \"123-456-789\" } }\nfirst { \"999-999-999\" swap set-person-phone ] keep dup .\n => T{ person T{ persistent f # \"1\" } \"John\" \"Smith\" \"999-999-999\" ...\n db get swap update-tuple" } -"Using the 'sqlite' command from the system shell you can see the record was updated:" -{ $code " $ sqlite3 example.db\n SQLite version 3.0.8\n Enter \".help\" for instructions\n sqlite> select ROWID,* from person;\n 1|John|Smith|999-999-999\n 2|Mandy|Jones|987-654-321\n sqlite>" } ; - -ARTICLE: { "sqlite" "tuple-db-inserting-or-updating" } "Inserting or Updating instances" -"The " { $link save-tuple } " word can be used to insert a tuple if it has not already been stored in the database, or update it if it already exists. Whether to insert or update is decided by the existance of the 'persistent' delegate:" -{ $code "\"Mary\" \"Smith\" \"111-111-111\" dup .\n => T{ person f \"Mary\" \"Smith\" \"111-111-111\" }\n! This will insert the tuple\ndb get over save-tuple dup .\n => T{ person T{ persistent f # \"3\" } \"Mary\" \"Smith\" \"111-111-111\" ...\n[ \"222-222-222\" swap set-person-phone ] keep dup .\n => T{ person T{ persistent f # \"3\" } \"Mary\" \"Smith\" \"222-222-222\" ...\n! This will update the tuple\ndb get over save-tuple .\n => T{ person T{ persistent f # \"3\" } \"Mary\" \"Smith\" \"222-222-222\" ..." } ; - -ARTICLE: { "sqlite" "tuple-db-deleting" } "Deleting instances" -"Given a tuple with the delegate set to 'persistent' (ie. One already stored in the database) you can delete it from the database with " { $link delete-tuple } ":" -{ $code "db get f \"Smith\" f find-tuples [ db get swap delete-tuple ] each" } ; - -ARTICLE: { "sqlite" "tuple-db-closing" } "Closing the database" -"It's important to close the sqlite database when you've finished using it. The word for this is " { $link sqlite-close } ":" -{ $code "db get sqlite-close" } ; - -ARTICLE: { "sqlite" "tuple-db" } "Tuple Database Library" -"The version of sqlite required by this library is version 3 or greater. This library allows storing Factor tuples in a sqlite database. It provides words to create, read update and delete these entries as well as simple searching." -$nl -"The library is in a very early state and is likely to change quite a bit in the near future. Its most notable omission is it cannot currently handle relationships between tuples." -{ $subsection { "sqlite" "tuple-db-loading" } } -{ $subsection { "sqlite" "tuple-db-usage" } } -{ $subsection { "sqlite" "tuple-db-mappings" } } -{ $subsection { "sqlite" "tuple-db-create" } } -{ $subsection { "sqlite" "tuple-db-insert" } } -{ $subsection { "sqlite" "tuple-db-finding" } } -{ $subsection { "sqlite" "tuple-db-updating" } } -{ $subsection { "sqlite" "tuple-db-inserting-or-updating" } } -{ $subsection { "sqlite" "tuple-db-deleting" } } -{ $subsection { "sqlite" "tuple-db-closing" } } -; - -HELP: default-mapping -{ $values { "class" "symbol for the tuple class" } - { "mapping" "a mapping object" } -} -{ $description "Given a tuple class, create a default mappings object. This is used to associate field names in the tuple with SQL statement field names, etc." } -{ $see-also { "sqlite" "tuple-db" } set-mapping } ; - -HELP: set-mapping -{ $values { "mapping" "a mapping object" } -} -{ $description "Store a database mapping so that the tuple-db system knows how to store instances of the tuple in the database." } -{ $see-also { "sqlite" "tuple-db" } default-mapping } ; - -HELP: create-tuple-table -{ $values { "db" "a database object" } { "class" "symbol for the tuple class" } -} -{ $description "Create the database table to store intances of the given tuple." } -{ $see-also { "sqlite" "tuple-db" } default-mapping get-mapping } ; - -HELP: insert-tuple -{ $values { "db" "a database object" } { "tuple" "an instance of a tuple" } -} -{ $description "Insert the tuple instance into the database. It is assumed that this tuple does not currently exist in the database." } -{ $see-also { "sqlite" "tuple-db" } insert-tuple update-tuple find-tuples delete-tuple save-tuple } ; - -HELP: find-tuples -{ $values { "db" "a database object" } { "tuple" "an instance of a tuple" } { "seq" "a sequence of tuples" } } -{ $description "Return a sequence of all tuples in the database that match the tuple provided as a template. All fields in the tuple must match the entries in the database, except for those set to 'f'." } -{ $see-also { "sqlite" "tuple-db" } insert-tuple update-tuple find-tuples delete-tuple save-tuple } ; - -HELP: update-tuple -{ $values { "db" "a database object" } { "tuple" "an instance of a tuple" } -} -{ $description "Update the database record for this tuple instance. The tuple must have previously been obtained from the database, or inserted into it. It must have a delegate of 'persistent' with the key field set (which is done by the find and insert operations)." } -{ $see-also { "sqlite" "tuple-db" } insert-tuple update-tuple find-tuples delete-tuple save-tuple } ; - -HELP: save-tuple -{ $values { "db" "a database object" } { "tuple" "an instance of a tuple" } -} -{ $description "Insert or Update the tuple instance depending on whether it has a persistent delegate." } -{ $see-also { "sqlite" "tuple-db" } insert-tuple update-tuple find-tuples delete-tuple save-tuple } ; - -HELP: delete-tuple -{ $values { "db" "a database object" } { "tuple" "an instance of a tuple" } -} -{ $description "Delete this tuple instance from the database. The tuple must have previously been obtained from the database, or inserted into it. It must have a delegate of 'persistent' with the key field set (which is done by the find and insert operations)." } -{ $see-also { "sqlite" "tuple-db" } insert-tuple update-tuple find-tuples delete-tuple save-tuple } ; - -ABOUT: { "sqlite" "tuple-db" } \ No newline at end of file diff --git a/extra/sqlite/tuple-db/tuple-db-tests.factor b/extra/sqlite/tuple-db/tuple-db-tests.factor deleted file mode 100644 index 8ed2631b45..0000000000 --- a/extra/sqlite/tuple-db/tuple-db-tests.factor +++ /dev/null @@ -1,39 +0,0 @@ -! Copyright (C) 2005 Chris Double. -! See http://factorcode.org/license.txt for BSD license. - -IN: temporary -USING: io io.files kernel sequences namespaces -hashtables sqlite sqlite.tuple-db math words tools.test ; - -TUPLE: testdata one two ; - -C: testdata - -testdata default-mapping set-mapping - -"libs/sqlite/test.db" resource-path [ - - db get testdata create-tuple-table - - [ "two" { } ] [ - db get "one" "two" insert-tuple - db get "one" f find-tuples - first [ testdata-two ] keep - db get swap delete-tuple - db get "one" f find-tuples - ] unit-test - - [ "junk" ] [ - db get "one" "two" insert-tuple - db get "one" f find-tuples - first - "junk" over set-testdata-two - db get swap update-tuple - db get "one" f find-tuples - first [ testdata-two ] keep - db get swap delete-tuple - ] unit-test - - db get testdata drop-tuple-table -] with-sqlite - diff --git a/extra/sqlite/tuple-db/tuple-db.factor b/extra/sqlite/tuple-db/tuple-db.factor deleted file mode 100644 index c37a49d2b6..0000000000 --- a/extra/sqlite/tuple-db/tuple-db.factor +++ /dev/null @@ -1,270 +0,0 @@ -! Copyright (C) 2005 Chris Double. -! -! A tuple that is persistent has its delegate set as 'persistent'. -! 'persistent' holds the numeric rowid for that tuple in its table. -IN: sqlite.tuple-db -USING: io kernel sequences namespaces slots classes slots.private -assocs math words generic sqlite math.parser ; - -! Each slot in a tuple that is storable in the database has -! an instance of a db-field object the gives the name of the -! database table and slot number in the tuple object of that field. -TUPLE: db-field name bind-name slot type ; - -C: db-field - -! The mapping tuple holds information on how the slots of -! a tuple are mapped to the fields of a sqlite database. -TUPLE: mapping tuple table fields one-to-one one-to-many ; - -C: mapping - -: sanitize ( string -- string ) - #! Convert a string so it can be used as a table or field name. - clone - H{ { CHAR: - CHAR: _ } { CHAR: ? CHAR: p } } - over substitute ; - -: tuple-fields ( class -- seq ) - #! Given a tuple class return a list of the fields - #! within that tuple. Ignores the delegate field. - "slots" word-prop 1 tail [ - [ slot-spec-name sanitize dup ":" swap append ] keep - slot-spec-offset - "text" - - ] map ; - -: default-mapping ( class -- mapping ) - #! Given a tuple class, create a default mappings object. It assumes - #! there are no one-to-one or one-to-many relationships. - dup [ word-name sanitize ] keep tuple-fields f f ; - -! The mappings variable holds a hashtable mapping the tuple symbol -! to the mapping object, describing how that tuple is stored -! in the database. -SYMBOL: mappings - -: init-mappings ( -- ) - H{ } mappings set-global ; - -: get-mappings ( -- hashtable ) - mappings get-global ; - -: set-mapping ( mapping -- ) - #! Store a database mapping so that the persistence system - #! knows how to store instances of the relevant tuple in the database. - dup mapping-tuple get-mappings set-at ; - -: get-mapping ( class -- mapping ) - #! Return the database mapping for the given tuple class. - get-mappings at ; - -! The 'persistent' tuple will be set to the delegate of any tuple -! instance stored in the database. It contains the database key -! of the row in the database table for the instance or 'f' if it has -! not yet been stored in the database. It also contains the 'mapping' -! object used to translate the fields of the tuple to the database fields. -TUPLE: persistent mapping key ; -: ( tuple -- persistent ) - persistent construct-empty - >r class get-mapping r> - [ set-persistent-mapping ] keep ; - -: make-persistent ( tuple -- tuple ) - #! Convert the tuple into something that can be stored - #! into a database by setting its delegate to 'persistent'. - [ ] keep - [ set-delegate ] keep ; - - -: comma-fields ( mapping quot -- string ) - #! Given a mapping, call quot on each field in - #! the mapping. The contents of quot should call ',' or '%' - #! to generate output. The output of each quot call - #! seperated by commas is returned as a string. 'quot' should be - #! stack effect ( field -- ). - >r mapping-fields r> [ "" make ] curry map "," join ; inline - -GENERIC: create-sql ( mapping -- string ) -M: mapping create-sql ( mapping -- string ) - #! Return the SQL used to create a table for storing this type of tuple. - [ - "create table " % dup mapping-table % - " (" % - [ dup db-field-name % " " % db-field-type % ] comma-fields % - ");" % - ] "" make ; - -GENERIC: drop-sql ( mapping -- string ) -M: mapping drop-sql ( mapping -- string ) - #! Return the SQL used to drop the table for storing this type of tuple. - [ - "drop table " % mapping-table % ";" % - ] "" make ; - -GENERIC: insert-sql ( mapping -- string ) -M: mapping insert-sql ( mapping -- string ) - #! Return the SQL used to insert a tuple into a table - [ - "insert into " % dup mapping-table % - " values(" % - [ db-field-bind-name % ] comma-fields % - ");" % - ] "" make ; - -GENERIC: delete-sql ( mapping -- string ) -M: mapping delete-sql ( mapping -- string ) - #! Return the SQL used to delete a tuple from a table - [ - "delete from " % mapping-table % - " where ROWID=:rowid;" % - ] "" make ; - -GENERIC: update-sql ( mapping -- string ) -M: mapping update-sql ( mapping -- string ) - #! Return the SQL used to update the tuple - [ - "update " % dup mapping-table % - " set " % - [ dup db-field-name % "=" % db-field-bind-name % ] comma-fields % - " where ROWID=:rowid;" % - ] "" make ; - -GENERIC: select-sql ( tuple mapping -- select ) -M: mapping select-sql ( tuple mapping -- select ) - #! Return the SQL used to select a series of tuples from the database. It - #! will select based on only the filled in fields of the tuple (ie. all non-f). - [ - "select ROWID,* from " % dup mapping-table % - mapping-fields [ ! tuple field - swap over db-field-slot slot ! field value - [ - [ dup db-field-name % "=" % db-field-bind-name % ] "" make - ] [ - drop f - ] if - ] with map [ ] subset dup length 0 > [ - " where " % - " and " join % - ] [ - drop - ] if - ";" % - ] "" make ; - -: execute-update-sql ( db string -- ) - #! Execute the SQL, which should contain a database update - #! statement (update, insert, create, etc). Ignore the result. - sqlite-prepare dup [ drop ] sqlite-each sqlite-finalize ; - -: create-tuple-table ( db class -- ) - #! Create the table for the tuple class. - get-mapping create-sql execute-update-sql ; - -: drop-tuple-table ( db class -- ) - #! Create the table for the tuple class. - get-mapping drop-sql execute-update-sql ; - -: bind-for-insert ( statement tuple -- ) - #! Bind the fields in the tuple to the fields in the - #! prepared insert statement. - dup class get-mapping mapping-fields [ ! statement tuple field - [ db-field-slot slot ] keep ! statement value field - db-field-bind-name swap ! statement name value - >r dupd r> sqlite-bind-text-by-name - ] with each drop ; - -: bind-for-select ( statement tuple -- ) - #! Bind the fields in the tuple to the fields in the - #! prepared select statement. - dup class get-mapping mapping-fields [ ! statement tuple field - [ db-field-slot slot ] keep ! statement value field - over [ - db-field-bind-name swap ! statement name value - >r dupd r> sqlite-bind-text-by-name - ] [ - 2drop - ] if - ] with each drop ; - -: bind-for-update ( statement tuple -- ) - #! Bind the fields in the tuple to the fields in the - #! prepared update statement. - 2dup bind-for-insert - >r ":rowid" r> persistent-key sqlite-bind-text-by-name ; - -: bind-for-delete ( statement tuple -- ) - #! Bind the fields in the tuple to the fields in the - #! prepared delete statement. - >r ":rowid" r> persistent-key sqlite-bind-text-by-name ; - -: (insert-tuple) ( db tuple -- ) - #! Insert this tuple instance into the database. Note that - #! it inserts only this instance, and not any one-to-one or - #! one-to-many fields. - dup class get-mapping insert-sql ! db tuple sql - swapd sqlite-prepare swap ! statement tuple - dupd bind-for-insert ! statement - dup [ drop ] sqlite-each - sqlite-finalize ; - -: insert-tuple ( db tuple -- ) - #! Insert this tuple instance into the database and - #! update the rowid of the insert in the tuple. - [ (insert-tuple) ] 2keep - >r sqlite-last-insert-rowid number>string r> make-persistent set-persistent-key ; - -: update-tuple ( db tuple -- ) - #! Update this tuple instance in the database. The tuple should have - #! a delegate of 'persistent' with the key field set. - dup class get-mapping update-sql ! db tuple sql - swapd sqlite-prepare swap ! statement tuple - dupd bind-for-update ! statement - dup [ drop ] sqlite-each - sqlite-finalize ; - -: save-tuple ( db tuple -- ) - #! Insert or Update the tuple instance depending on whether it - #! has a persistent delegate. - dup delegate [ update-tuple ] [ insert-tuple ] if ; - -: delete-tuple ( db tuple -- ) - #! Delete this tuple instance from the database. The tuple should have - #! a delegate of 'persistent' with the key field set. - dup class get-mapping delete-sql ! db tuple sql - swapd sqlite-prepare swap ! statement tuple - dupd bind-for-delete ! statement - dup [ drop ] sqlite-each - sqlite-finalize ; - -: restore-tuple ( statement tuple -- tuple ) - #! Using 'tuple' as a template, clone it and - #! return the clone with fields set to the values from the - #! database. - clone dup class get-mapping mapping-fields 1 swap - [ ! statement tuple index field ) - over 1+ >r ! statement tuple index field r: index+1 - db-field-slot >r ! statement tuple index r: index+1 slot - pick swap column-text ! statement tuple value r: index+1 slot - over r> set-slot r> ! statement tuple index+1 - ] each ! statement tuple index - drop make-persistent swap 0 column-text swap [ set-persistent-key ] keep ; - -: find-tuples ( db tuple -- seq ) - #! Return a sequence of all tuples in the database that - #! match the tuple provided as a template. All fields in the - #! tuple must match the entries in the database, except for - #! those set to 'f'. - dup class get-mapping dupd select-sql ! db tuple sql - swapd sqlite-prepare swap ! statement tuple - 2dup bind-for-select ! statement tuple - [ - over [ ! tuple statement - over restore-tuple , - ] sqlite-each - ] { } make nip ! statement tuple accum - swap sqlite-finalize ; - - -get-mappings [ init-mappings ] unless From 6f2b91d4a4a9acdd83161a0d1ae884b423c59ae4 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Fri, 1 Feb 2008 18:10:32 -0800 Subject: [PATCH 011/269] Move byte-length generic and methods into alien.c-types --- core/alien/alien-docs.factor | 4 ---- core/alien/alien.factor | 2 -- core/alien/c-types/c-types-docs.factor | 4 ++++ core/alien/c-types/c-types.factor | 8 +++++++- core/byte-arrays/byte-arrays.factor | 2 -- core/float-arrays/float-arrays.factor | 3 +-- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/core/alien/alien-docs.factor b/core/alien/alien-docs.factor index 8ae89ed5b1..8fee0e8c3e 100755 --- a/core/alien/alien-docs.factor +++ b/core/alien/alien-docs.factor @@ -34,10 +34,6 @@ HELP: { $description "Creates an alien object, wrapping a raw memory address." } { $notes "Alien objects are invalidated between image saves and loads." } ; -HELP: byte-length -{ $values { "seq" "A byte array or float array" } { "n" "a non-negative integer" } } -{ $contract "Outputs the size of the byte array or float array data in bytes as presented to the C library interface." } ; - HELP: c-ptr { $class-description "Class of objects consisting of aliens, byte arrays and " { $link f } ". These objects can convert to pointer C types, which are all aliases of " { $snippet "void*" } "." } ; diff --git a/core/alien/alien.factor b/core/alien/alien.factor index 4b899a15e4..1c8163e2fa 100755 --- a/core/alien/alien.factor +++ b/core/alien/alien.factor @@ -28,8 +28,6 @@ PREDICATE: alien pinned-alien UNION: pinned-c-ptr pinned-alien POSTPONE: f ; -GENERIC: byte-length ( seq -- n ) flushable - M: f expired? drop t ; : ( address -- alien ) diff --git a/core/alien/c-types/c-types-docs.factor b/core/alien/c-types/c-types-docs.factor index f6418295f7..f4aa297a3a 100755 --- a/core/alien/c-types/c-types-docs.factor +++ b/core/alien/c-types/c-types-docs.factor @@ -34,6 +34,10 @@ HELP: stack-size { $description "Outputs the number of bytes to reserve on the C stack by a value of this C type. In most cases this is equal to " { $link heap-size } ", except on some platforms where C structs are passed by invisible reference, in which case a C struct type only uses as much space as a pointer on the C stack." } { $errors "Throws a " { $link no-c-type } " error if the type does not exist." } ; +HELP: byte-length +{ $values { "seq" "A byte array or float array" } { "n" "a non-negative integer" } } +{ $contract "Outputs the size of the byte array or float array data in bytes as presented to the C library interface." } ; + HELP: c-getter { $values { "name" string } { "quot" "a quotation with stack effect " { $snippet "( c-ptr n -- obj )" } } } { $description "Outputs a quotation which reads values of this C type from a C structure." } diff --git a/core/alien/c-types/c-types.factor b/core/alien/c-types/c-types.factor index 1ecfa37ee6..47f9fd0326 100755 --- a/core/alien/c-types/c-types.factor +++ b/core/alien/c-types/c-types.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2004, 2007 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: byte-arrays arrays generator.registers assocs +USING: byte-arrays float-arrays arrays generator.registers assocs kernel kernel.private libc math namespaces parser sequences strings words assocs splitting math.parser cpu.architecture alien quotations system compiler.units ; @@ -107,6 +107,12 @@ M: string stack-size c-type stack-size ; M: c-type stack-size c-type-size ; +GENERIC: byte-length ( seq -- n ) flushable + +M: float-array byte-length length "float" heap-size * ; + +M: byte-array byte-length length ; + : c-getter ( name -- quot ) c-type c-type-getter [ [ "Cannot read struct fields with type" throw ] diff --git a/core/byte-arrays/byte-arrays.factor b/core/byte-arrays/byte-arrays.factor index 295c6c4384..401b151ad0 100755 --- a/core/byte-arrays/byte-arrays.factor +++ b/core/byte-arrays/byte-arrays.factor @@ -15,8 +15,6 @@ M: byte-array new drop ; M: byte-array equal? over byte-array? [ sequence= ] [ 2drop f ] if ; -M: byte-array byte-length - length ; M: byte-array resize resize-byte-array ; diff --git a/core/float-arrays/float-arrays.factor b/core/float-arrays/float-arrays.factor index 948e41ef7a..445edd550a 100755 --- a/core/float-arrays/float-arrays.factor +++ b/core/float-arrays/float-arrays.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2007, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: kernel kernel.private alien alien.c-types sequences +USING: kernel kernel.private alien sequences sequences.private math math.private ; IN: float-arrays @@ -12,7 +12,6 @@ PRIVATE> M: float-array clone (clone) ; M: float-array length array-capacity ; -M: float-array byte-length array-capacity "float" heap-size * ; M: float-array nth-unsafe float-array@ alien-double ; From db3ac4d75ff5835d49bd9821cd303d724f330a6d Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 1 Feb 2008 22:46:03 -0600 Subject: [PATCH 012/269] intermediate work on cookies --- extra/http/http.factor | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/extra/http/http.factor b/extra/http/http.factor index 9e5d34fa36..a71e003433 100755 --- a/extra/http/http.factor +++ b/extra/http/http.factor @@ -1,18 +1,18 @@ ! Copyright (C) 2003, 2007 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: hashtables io kernel math namespaces math.parser assocs -sequences strings splitting ; +sequences strings splitting assocs.lib ; IN: http : header-line ( line -- ) - ": " split1 dup [ swap set ] [ 2drop ] if ; + ": " split1 dup [ swap >lower set ] [ 2drop ] if ; : (read-header) ( -- ) readln dup empty? [ drop ] [ header-line (read-header) ] if ; : read-header ( -- hash ) - [ (read-header) ] H{ } make-assoc ; + [ (read-header) ] VH{ } make-assoc ; : url-quotable? ( ch -- ? ) #! In a URL, can this character be used without @@ -74,4 +74,3 @@ IN: http hash>query % ] if ] "" make ; - From 004dd0dc5e97f60bf917b2af99673c3fa2bbe754 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 1 Feb 2008 22:46:32 -0600 Subject: [PATCH 013/269] add accumulator --- extra/sequences/lib/lib.factor | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extra/sequences/lib/lib.factor b/extra/sequences/lib/lib.factor index e46ce3b107..9aac0a50bd 100755 --- a/extra/sequences/lib/lib.factor +++ b/extra/sequences/lib/lib.factor @@ -140,3 +140,6 @@ PRIVATE> : ?second ( seq -- second/f ) 1 swap ?nth ; inline : ?third ( seq -- third/f ) 2 swap ?nth ; inline : ?fourth ( seq -- fourth/f ) 3 swap ?nth ; inline + +: accumulator ( quot -- quot vec ) + V{ } clone [ [ push ] curry compose ] keep ; From 9e9c71b6d0925d5929bbb10a20807fd3d75cfb6c Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 1 Feb 2008 23:46:44 -0600 Subject: [PATCH 014/269] make multi-assocs work for http headers --- extra/http/client/client.factor | 4 +-- extra/http/http.factor | 7 +++-- .../http/server/responders/responders.factor | 28 +++++++++++-------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/extra/http/client/client.factor b/extra/http/client/client.factor index 7eb84fba4c..8e6d8257a4 100755 --- a/extra/http/client/client.factor +++ b/extra/http/client/client.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: assocs http kernel math math.parser namespaces sequences io io.sockets io.streams.string io.files strings splitting -continuations ; +continuations assocs.lib ; IN: http.client : parse-host ( url -- host port ) @@ -44,7 +44,7 @@ DEFER: http-get-stream #! Should this support Location: headers that are #! relative URLs? pick 100 /i 3 = [ - dispose "location" swap header-single nip http-get-stream + dispose "location" swap peek-at nip http-get-stream ] when ; : http-get-stream ( url -- code headers stream ) diff --git a/extra/http/http.factor b/extra/http/http.factor index 4999559324..755f36a538 100755 --- a/extra/http/http.factor +++ b/extra/http/http.factor @@ -1,18 +1,19 @@ ! Copyright (C) 2003, 2007 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: hashtables io kernel math namespaces math.parser assocs -sequences strings splitting ascii io.utf8 assocs.lib ; +sequences strings splitting ascii io.utf8 assocs.lib +namespaces unicode.case ; IN: http : header-line ( line -- ) - ": " split1 dup [ swap >lower set ] [ 2drop ] if ; + ": " split1 dup [ swap >lower insert ] [ 2drop ] if ; : (read-header) ( -- ) readln dup empty? [ drop ] [ header-line (read-header) ] if ; : read-header ( -- hash ) - [ (read-header) ] VH{ } make-assoc ; + [ (read-header) ] H{ } make-assoc ; : url-quotable? ( ch -- ? ) #! In a URL, can this character be used without diff --git a/extra/http/server/responders/responders.factor b/extra/http/server/responders/responders.factor index 8dcaa7223d..a507a95a14 100644 --- a/extra/http/server/responders/responders.factor +++ b/extra/http/server/responders/responders.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: arrays assocs hashtables html html.elements splitting http io kernel math math.parser namespaces parser sequences -strings io.server ; +strings io.server vectors vector-hash strings.lib ; IN: http.server.responders @@ -10,8 +10,11 @@ IN: http.server.responders SYMBOL: vhosts SYMBOL: responders +: >header ( value key -- vector-hash ) + VH{ } clone [ set-at ] keep ; + : print-header ( alist -- ) - [ swap write ": " write print ] assoc-each nl ; + [ swap >Upper-dashes write ": " write print ] vector-hash-each nl ; : response ( msg -- ) "HTTP/1.0 " write print ; @@ -20,7 +23,7 @@ SYMBOL: responders : error-head ( error -- ) dup log-error response - H{ { "Content-Type" "text/html" } } print-header nl ; + VH{ { "Content-Type" "text/html" } } print-header nl ; : httpd-error ( error -- ) #! This must be run from handle-request @@ -36,7 +39,7 @@ SYMBOL: responders : serving-content ( mime -- ) "200 Document follows" response - "Content-Type" associate print-header ; + "Content-Type" >header print-header ; : serving-html "text/html" serving-content ; @@ -46,7 +49,7 @@ SYMBOL: responders : serving-text "text/plain" serving-content ; : redirect ( to response -- ) - response "Location" associate print-header ; + response "Location" >header print-header ; : permanent-redirect ( to -- ) "301 Moved Permanently" redirect ; @@ -84,14 +87,14 @@ SYMBOL: max-post-request : log-headers ( hash -- ) [ drop { - "User-Agent" - "Referer" - "X-Forwarded-For" - "Host" + "user-agent" + "referer" + "x-forwarded-for" + "host" } member? ] assoc-subset [ ": " swap 3append log-message - ] assoc-each ; + ] vector-hash-each ; : prepare-url ( url -- url ) #! This is executed in the with-request namespace. @@ -122,7 +125,8 @@ SYMBOL: max-post-request : query-param ( key -- value ) "query" get at ; -: header-param ( key -- value ) "header" get at ; +: header-param ( key -- value ) + "header" get peek-at ; : host ( -- string ) #! The host the current responder was called from. @@ -130,7 +134,7 @@ SYMBOL: max-post-request : add-responder ( responder -- ) #! Add a responder object to the list. - "responder" over at responders get set-at ; + "responder" over at responders get set-at ; : make-responder ( quot -- ) #! quot has stack effect ( url -- ) From 22eb97778e04197530130d280959832db727111d Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 1 Feb 2008 23:47:37 -0600 Subject: [PATCH 015/269] add multi-assocs --- extra/assocs/lib/lib.factor | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/extra/assocs/lib/lib.factor b/extra/assocs/lib/lib.factor index 849f88023f..182f04a367 100755 --- a/extra/assocs/lib/lib.factor +++ b/extra/assocs/lib/lib.factor @@ -1,9 +1,6 @@ -USING: assocs kernel vectors sequences ; +USING: assocs kernel vectors sequences namespaces ; IN: assocs.lib -: insert-at ( value key assoc -- ) - [ ?push ] change-at ; - : >set ( seq -- hash ) [ dup ] H{ } map>assoc ; @@ -19,5 +16,19 @@ IN: assocs.lib : at-default ( key assoc -- value/key ) dupd at [ nip ] when* ; -: at-peek ( key assoc -- value ? ) - at* dup >r [ peek ] when r> ; +: insert-at ( value key assoc -- ) + [ ?push ] change-at ; + +: peek-at* ( key assoc -- obj ? ) + at* dup [ >r peek r> ] when ; + +: peek-at ( key assoc -- obj ) + peek-at* drop ; + +: >multi-assoc ( assoc -- new-assoc ) + [ 1vector ] assoc-map ; + +: multi-assoc-each ( assoc quot -- ) + [ with each ] curry assoc-each ; inline + +: insert ( value variable -- ) namespace insert-at ; From 698f4180bbd580c73f1cf3204cc83626a6245a7b Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 1 Feb 2008 23:47:54 -0600 Subject: [PATCH 016/269] add a wget-bootstrap option --- misc/factor.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/misc/factor.sh b/misc/factor.sh index 39a15f93dc..032b0b3184 100755 --- a/misc/factor.sh +++ b/misc/factor.sh @@ -289,7 +289,7 @@ install_libraries() { } usage() { - echo "usage: $0 install|install-x11|self-update|quick-update|update|bootstrap" + echo "usage: $0 install|install-x11|self-update|quick-update|update|bootstrap|wget-bootstrap" } case "$1" in @@ -299,5 +299,6 @@ case "$1" in quick-update) update; refresh_image ;; update) update; update_bootstrap ;; bootstrap) get_config_info; bootstrap ;; + wget-bootstrap) get_config_info; delete_boot_images; get_boot_image; bootstrap ;; *) usage ;; esac From 16e206b3b8dccdeda5fb29fb155ab9ecd6e53766 Mon Sep 17 00:00:00 2001 From: sheeple Date: Sat, 2 Feb 2008 06:58:28 -0600 Subject: [PATCH 017/269] Add flags to math.bitfields --- core/inference/transforms/transforms.factor | 2 ++ core/math/bitfields/bitfields.factor | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/core/inference/transforms/transforms.factor b/core/inference/transforms/transforms.factor index fd15b7da98..ad2bacc789 100755 --- a/core/inference/transforms/transforms.factor +++ b/core/inference/transforms/transforms.factor @@ -54,6 +54,8 @@ M: pair (bitfield-quot) ( spec -- quot ) \ bitfield [ bitfield-quot ] 1 define-transform +\ flags [ flags [ ] curry ] 1 define-transform + ! Tuple operations : [get-slots] ( slots -- quot ) [ [ 1quotation , \ keep , ] each \ drop , ] [ ] make ; diff --git a/core/math/bitfields/bitfields.factor b/core/math/bitfields/bitfields.factor index f6a3419784..29c3329f3d 100644 --- a/core/math/bitfields/bitfields.factor +++ b/core/math/bitfields/bitfields.factor @@ -1,4 +1,4 @@ -! Copyright (C) 2007 Slava Pestov. +! Copyright (C) 2007, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: arrays kernel math sequences words ; IN: math.bitfields @@ -13,3 +13,6 @@ M: pair (bitfield) ( value accum pair -- newaccum ) : bitfield ( values... bitspec -- n ) 0 [ (bitfield) ] reduce ; + +: flags ( values -- n ) + 0 [ execute bitor ] reduce ; From b22a40f90602b6ac9461a49c3782aa0249512c7a Mon Sep 17 00:00:00 2001 From: sheeple Date: Sat, 2 Feb 2008 07:02:32 -0600 Subject: [PATCH 018/269] inotify bindings --- extra/unix/linux/inotify/inotify.factor | 50 +++++++++++++++++++++++++ vm/os-linux.c | 15 ++++++++ vm/os-linux.h | 4 ++ 3 files changed, 69 insertions(+) create mode 100644 extra/unix/linux/inotify/inotify.factor diff --git a/extra/unix/linux/inotify/inotify.factor b/extra/unix/linux/inotify/inotify.factor new file mode 100644 index 0000000000..14840b380a --- /dev/null +++ b/extra/unix/linux/inotify/inotify.factor @@ -0,0 +1,50 @@ +! Copyright (C) 2008 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: alien.syntax ; +IN: unix.linux.inotify + +C-STRUCT: inotify-event + { "int" "wd" } ! watch descriptor + { "uint" "mask" } ! watch mask + { "uint" "cookie" } ! cookie to synchronize two events + { "uint" "len" } ! length (including nulls) of name + { "char[1]" "name" } ! stub for possible name + ; + +: IN_ACCESS HEX: 1 ; inline ! File was accessed +: IN_MODIFY HEX: 2 ; inline ! File was modified +: IN_ATTRIB HEX: 4 ; inline ! Metadata changed +: IN_CLOSE_WRITE HEX: 8 ; inline ! Writtable file was closed +: IN_CLOSE_NOWRITE HEX: 10 ; inline ! Unwrittable file closed +: IN_OPEN HEX: 20 ; inline ! File was opened +: IN_MOVED_FROM HEX: 40 ; inline ! File was moved from X +: IN_MOVED_TO HEX: 80 ; inline ! File was moved to Y +: IN_CREATE HEX: 100 ; inline ! Subfile was created +: IN_DELETE HEX: 200 ; inline ! Subfile was deleted +: IN_DELETE_SELF HEX: 400 ; inline ! Self was deleted +: IN_MOVE_SELF HEX: 800 ; inline ! Self was moved + +: IN_UNMOUNT HEX: 2000 ; inline ! Backing fs was unmounted +: IN_Q_OVERFLOW HEX: 4000 ; inline ! Event queued overflowed +: IN_IGNORED HEX: 8000 ; inline ! File was ignored + +: IN_CLOSE IN_CLOSE_WRITE IN_CLOSE_NOWRITE bitor ; inline ! close +: IN_MOVE IN_MOVED_FROM IN_MOVED_TO bitor ; inline ! moves + +: IN_ONLYDIR HEX: 1000000 ; inline ! only watch the path if it is a directory +: IN_DONT_FOLLOW HEX: 2000000 ; inline ! don't follow a sym link +: IN_MASK_ADD HEX: 20000000 ; inline ! add to the mask of an already existing watch +: IN_ISDIR HEX: 40000000 ; inline ! event occurred against dir +: IN_ONESHOT HEX: 80000000 ; inline ! only send event once + +: IN_ALL_EVENTS + { + IN_ACCESS IN_MODIFY IN_ATTRIB IN_CLOSE_WRITE + IN_CLOSE_NOWRITE IN_OPEN IN_MOVED_FROM + IN_MOVED_TO IN_DELETE IN_CREATE IN_DELETE_SELF + IN_MOVE_SELF + } flags ; foldable + +FUNCTION: int inotify_init ( void ) ; +FUNCTION: int inotify_add_watch ( int fd, char* name, u32 mask ) ; +FUNCTION: int inotify_rm_watch ( int fd, u32 wd ) ; diff --git a/vm/os-linux.c b/vm/os-linux.c index 8f3f8408f3..935add6714 100644 --- a/vm/os-linux.c +++ b/vm/os-linux.c @@ -17,3 +17,18 @@ const char *vm_executable_path(void) return safe_strdup(path); } } + +int inotify_init(void) +{ + return syscall(SYS_inotify_init); +} + +int inotify_add_watch(int fd, const char *name, u32 mask) +{ + return syscall(SYS_inotify_add_watch, fd, name, mask); +} + +int inotify_rm_watch(int fd, u32 wd) +{ + return syscall(SYS_inotify_rm_watch, fd, wd); +} diff --git a/vm/os-linux.h b/vm/os-linux.h index 21e34c98f8..2b5371ff1b 100644 --- a/vm/os-linux.h +++ b/vm/os-linux.h @@ -4,3 +4,7 @@ #ifndef environ extern char **environ; #endif + +int inotify_init(void); +int inotify_add_watch(int fd, const char *name, u32 mask); +int inotify_rm_watch(int fd, u32 wd); From a05c18152b59073c49aa313ba685516310ec74a8 Mon Sep 17 00:00:00 2001 From: sheeple Date: Sat, 2 Feb 2008 07:05:15 -0600 Subject: [PATCH 019/269] flags now works with numbers --- core/math/bitfields/bitfields.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/math/bitfields/bitfields.factor b/core/math/bitfields/bitfields.factor index 29c3329f3d..77cc40180e 100644 --- a/core/math/bitfields/bitfields.factor +++ b/core/math/bitfields/bitfields.factor @@ -15,4 +15,4 @@ M: pair (bitfield) ( value accum pair -- newaccum ) 0 [ (bitfield) ] reduce ; : flags ( values -- n ) - 0 [ execute bitor ] reduce ; + 0 [ dup word? [ execute ] when bitor ] reduce ; From ff4051316513f0eb56a07051887491067ed89802 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 2 Feb 2008 16:23:04 -0600 Subject: [PATCH 020/269] Cleaning up monitors in preparation for Linux inotify --- extra/io/monitor/monitor.factor | 32 ++++++++++++++- extra/io/unix/backend/backend.factor | 8 ++-- extra/io/windows/nt/monitor/monitor.factor | 48 +++++++++------------- 3 files changed, 53 insertions(+), 35 deletions(-) diff --git a/extra/io/monitor/monitor.factor b/extra/io/monitor/monitor.factor index 4dc5081513..fe33045e01 100755 --- a/extra/io/monitor/monitor.factor +++ b/extra/io/monitor/monitor.factor @@ -1,11 +1,39 @@ ! Copyright (C) 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: io.backend kernel continuations ; +USING: io.backend kernel continuations namespaces sequences +assocs hashtables sorting arrays ; IN: io.monitor +array ; + +PRIVATE> + HOOK: io-backend ( path recursive? -- monitor ) -HOOK: next-change io-backend ( monitor -- path changes ) +: next-change ( monitor -- path changed ) + dup check-monitor + dup monitor-queue dup assoc-empty? [ + drop dup fill-queue over set-monitor-queue next-change + ] [ nip dequeue-change ] if ; SYMBOL: +add-file+ SYMBOL: +remove-file+ diff --git a/extra/io/unix/backend/backend.factor b/extra/io/unix/backend/backend.factor index 1b66c0332e..7112c48551 100755 --- a/extra/io/unix/backend/backend.factor +++ b/extra/io/unix/backend/backend.factor @@ -14,9 +14,9 @@ TUPLE: io-task port callbacks ; : io-task-fd io-task-port port-handle ; -: ( port continuation class -- task ) - >r 1vector io-task construct-boa r> construct-delegate ; - inline +: ( port continuation/f class -- task ) + >r [ 1vector ] [ V{ } clone ] if* io-task construct-boa + r> construct-delegate ; inline TUPLE: input-task ; @@ -194,7 +194,7 @@ TUPLE: mx-port mx ; TUPLE: mx-task ; : ( port -- task ) - f io-task construct-boa mx-task construct-delegate ; + f mx-task ; M: mx-task do-io-task io-task-port mx-port-mx 0 swap wait-for-events f ; diff --git a/extra/io/windows/nt/monitor/monitor.factor b/extra/io/windows/nt/monitor/monitor.factor index 8e0e63923d..1be91263c4 100755 --- a/extra/io/windows/nt/monitor/monitor.factor +++ b/extra/io/windows/nt/monitor/monitor.factor @@ -3,12 +3,10 @@ USING: alien.c-types destructors io.windows io.windows.nt.backend kernel math windows windows.kernel32 windows.types libc assocs alien namespaces continuations -io.monitor io.nonblocking io.buffers io.files io sequences -hashtables sorting arrays combinators ; +io.monitor io.monitor.private io.nonblocking io.buffers io.files +io sequences hashtables sorting arrays combinators ; IN: io.windows.nt.monitor -TUPLE: monitor path recursive? queue closed? ; - : open-directory ( path -- handle ) FILE_LIST_DIRECTORY share-mode @@ -22,23 +20,26 @@ TUPLE: monitor path recursive? queue closed? ; dup add-completion f ; +TUPLE: win32-monitor path recursive? ; + +: ( path recursive? port -- monitor ) + (monitor) { + set-win32-monitor-path + set-win32-monitor-recursive? + set-delegate + } win32-monitor construct ; + M: windows-nt-io ( path recursive? -- monitor ) [ - >r dup open-directory monitor r> { - set-monitor-path - set-delegate - set-monitor-recursive? - } monitor construct + over open-directory win32-monitor + ] with-destructors ; -: check-closed ( monitor -- ) - port-type closed eq? [ "Monitor closed" throw ] when ; - : begin-reading-changes ( monitor -- overlapped ) dup port-handle win32-file-handle over buffer-ptr pick buffer-size - roll monitor-recursive? 1 0 ? + roll win32-monitor-recursive? 1 0 ? FILE_NOTIFY_CHANGE_ALL 0 (make-overlapped) @@ -49,6 +50,7 @@ M: windows-nt-io ( path recursive? -- monitor ) [ dup begin-reading-changes swap [ save-callback ] 2keep + dup check-monitor ! we may have closed it... get-overlapped-result ] with-port-timeout ] with-destructors ; @@ -63,7 +65,7 @@ M: windows-nt-io ( path recursive? -- monitor ) { [ t ] [ +modify-file+ ] } } cond nip ; -: changed-file ( directory buffer -- changed path ) +: parse-file-notify ( directory buffer -- changed path ) { FILE_NOTIFY_INFORMATION-FileName FILE_NOTIFY_INFORMATION-FileNameLength @@ -71,22 +73,10 @@ M: windows-nt-io ( path recursive? -- monitor ) } get-slots >r memory>u16-string path+ r> parse-action swap ; : (changed-files) ( directory buffer -- ) - 2dup changed-file namespace [ swap add ] change-at + 2dup parse-file-notify changed-file dup FILE_NOTIFY_INFORMATION-NextEntryOffset dup zero? [ 3drop ] [ swap (changed-files) ] if ; -: changed-files ( directory buffer len -- assoc ) +M: windows-nt-io fill-queue ( monitor -- assoc ) + dup win32-monitor-path over buffer-ptr rot read-changes [ zero? [ 2drop ] [ (changed-files) ] if ] H{ } make-assoc ; - -: fill-queue ( monitor -- ) - dup monitor-path over buffer-ptr pick read-changes - changed-files - swap set-monitor-queue ; - -M: windows-nt-io next-change ( monitor -- path changes ) - dup check-closed - dup monitor-queue dup assoc-empty? [ - drop dup fill-queue next-change - ] [ - nip delete-any prune natural-sort >array - ] if ; From ff46bfaa9610a95299a3afb0bac745c9825f7852 Mon Sep 17 00:00:00 2001 From: sheeple Date: Sat, 2 Feb 2008 11:51:16 -0600 Subject: [PATCH 021/269] Linux inotify support work in progress --- extra/io/unix/linux/linux.factor | 127 +++++++++++++++++++++++- extra/unix/linux/inotify/inotify.factor | 17 +++- vm/os-linux.h | 2 + 3 files changed, 138 insertions(+), 8 deletions(-) diff --git a/extra/io/unix/linux/linux.factor b/extra/io/unix/linux/linux.factor index 6d55decb5a..01d6159e45 100755 --- a/extra/io/unix/linux/linux.factor +++ b/extra/io/unix/linux/linux.factor @@ -1,15 +1,136 @@ ! Copyright (C) 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. +USING: kernel io.backend io.monitor io.monitor.private io.files +io.buffers io.nonblocking io.unix.backend io.unix.select +io.unix.launcher unix.linux.inotify assocs namespaces threads +continuations init math alien.c-types alien ; IN: io.unix.linux -USING: io.backend io.unix.backend io.unix.launcher io.unix.select -namespaces kernel assocs unix.process init ; TUPLE: linux-io ; INSTANCE: linux-io unix-io +TUPLE: linux-monitor path wd callback ; + +: ( path wd -- monitor ) + f (monitor) { + set-linux-monitor-path + set-linux-monitor-wd + set-delegate + } linux-monitor construct ; + +TUPLE: inotify watches ; + +: wd>path ( wd -- path ) + inotify get-global inotify-watches at linux-monitor-path ; + +: ( -- port ) + H{ } clone + inotify_init dup io-error inotify + { set-inotify-watches set-delegate } inotify construct ; + +: inotify-fd inotify get-global port-handle ; + +: watches inotify get-global inotify-watches ; + +: (add-watch) ( path mask -- wd ) + inotify-fd -rot inotify_add_watch dup io-error ; + +: check-existing ( wd -- ) + watches key? [ + "Cannot open multiple monitors for the same file" throw + ] when ; + +: add-watch ( path mask -- monitor ) + dupd (add-watch) + dup check-existing + [ dup ] keep watches set-at ; + +: remove-watch ( monitor -- ) + dup linux-monitor-wd watches delete-at + linux-monitor-wd inotify-fd swap inotify_rm_watch io-error ; + +M: linux-io ( path recursive? -- monitor ) + drop IN_CHANGE_EVENTS add-watch ; + +: notify-callback ( assoc monitor -- ) + linux-monitor-callback dup + [ schedule-thread-with ] [ 2drop ] if ; + +M: linux-io fill-queue ( monitor -- assoc ) + dup linux-monitor-callback [ + "Cannot wait for changes on the same file from multiple threads" throw + ] when + [ swap set-linux-monitor-callback stop ] callcc1 + swap check-monitor ; + +M: linux-monitor dispose ( monitor -- ) + dup check-monitor + t over set-monitor-closed? + H{ } over notify-callback + remove-watch ; + +: ?flag ( n mask symbol -- n ) + pick rot bitand 0 > [ , ] [ drop ] if ; + +: parse-action ( mask -- changed ) + [ + IN_CREATE +add-file+ ?flag + IN_DELETE +remove-file+ ?flag + IN_DELETE_SELF +remove-file+ ?flag + IN_MODIFY +modify-file+ ?flag + IN_ATTRIB +modify-file+ ?flag + IN_MOVED_FROM +rename-file+ ?flag + IN_MOVED_TO +rename-file+ ?flag + IN_MOVE_SELF +rename-file+ ?flag + drop + ] { } make ; + +: parse-file-notify ( buffer -- changed path ) + { + inotify-event-wd + inotify-event-name + inotify-event-mask + } get-slots + parse-action -rot alien>char-string >r wd>path r> path+ ; + +: events-exhausted? ( i buffer -- ? ) + buffer-fill >= ; + +: inotify-event@ ( i buffer -- alien ) + buffer-ptr ; + +: next-event ( i buffer -- i buffer ) + 2dup inotify-event@ + inotify-event-len "inotify-event" heap-size + + swap >r + r> ; + +: parse-file-notifications ( i buffer -- ) + 2dup events-exhausted? [ 2drop ] [ + 2dup inotify-event@ parse-file-notify changed-file + next-event parse-file-notifications + ] if ; + +: read-notifications ( port -- ) + dup refill drop + 0 over parse-file-notifications + 0 swap buffer-reset ; + +TUPLE: inotify-task ; + +: ( port -- task ) + f inotify-task ; + +: init-inotify ( mx -- ) + + dup inotify set-global + swap register-io-task ; + +M: inotify-task do-io-task ( task -- ) + io-task-port read-notifications f ; + M: linux-io init-io ( -- ) - mx set-global ; + mx set-global ; ! init-inotify ; T{ linux-io } set-io-backend diff --git a/extra/unix/linux/inotify/inotify.factor b/extra/unix/linux/inotify/inotify.factor index 14840b380a..b7b721efc7 100644 --- a/extra/unix/linux/inotify/inotify.factor +++ b/extra/unix/linux/inotify/inotify.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.syntax ; +USING: alien.syntax math math.bitfields ; IN: unix.linux.inotify C-STRUCT: inotify-event @@ -8,7 +8,7 @@ C-STRUCT: inotify-event { "uint" "mask" } ! watch mask { "uint" "cookie" } ! cookie to synchronize two events { "uint" "len" } ! length (including nulls) of name - { "char[1]" "name" } ! stub for possible name + { "char[0]" "name" } ! stub for possible name ; : IN_ACCESS HEX: 1 ; inline ! File was accessed @@ -37,6 +37,13 @@ C-STRUCT: inotify-event : IN_ISDIR HEX: 40000000 ; inline ! event occurred against dir : IN_ONESHOT HEX: 80000000 ; inline ! only send event once +: IN_CHANGE_EVENTS + { + IN_MODIFY IN_ATTRIB IN_MOVED_FROM + IN_MOVED_TO IN_DELETE IN_CREATE IN_DELETE_SELF + IN_MOVE_SELF + } flags ; foldable + : IN_ALL_EVENTS { IN_ACCESS IN_MODIFY IN_ATTRIB IN_CLOSE_WRITE @@ -45,6 +52,6 @@ C-STRUCT: inotify-event IN_MOVE_SELF } flags ; foldable -FUNCTION: int inotify_init ( void ) ; -FUNCTION: int inotify_add_watch ( int fd, char* name, u32 mask ) ; -FUNCTION: int inotify_rm_watch ( int fd, u32 wd ) ; +FUNCTION: int inotify_init ( ) ; +FUNCTION: int inotify_add_watch ( int fd, char* name, uint mask ) ; +FUNCTION: int inotify_rm_watch ( int fd, uint wd ) ; diff --git a/vm/os-linux.h b/vm/os-linux.h index 2b5371ff1b..1a1e088359 100644 --- a/vm/os-linux.h +++ b/vm/os-linux.h @@ -1,3 +1,5 @@ +#include + #define UNKNOWN_TYPE_P(file) ((file)->d_type == DT_UNKNOWN) #define DIRECTORY_P(file) ((file)->d_type == DT_DIR) From a0dad18f4f96ffc0a93a07f90a7c42453c5e6ebb Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Sat, 2 Feb 2008 13:37:53 -0500 Subject: [PATCH 022/269] Solution to Project Euler problem 39 --- extra/project-euler/039/039.factor | 76 ++++++++++++++++++++++++ extra/project-euler/project-euler.factor | 8 +-- 2 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 extra/project-euler/039/039.factor diff --git a/extra/project-euler/039/039.factor b/extra/project-euler/039/039.factor new file mode 100644 index 0000000000..4df7ba610a --- /dev/null +++ b/extra/project-euler/039/039.factor @@ -0,0 +1,76 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: arrays combinators.lib kernel math math.matrices math.ranges namespaces + sequences ; +IN: project-euler.039 + +! http://projecteuler.net/index.php?section=problems&id=39 + +! DESCRIPTION +! ----------- + +! If p is the perimeter of a right angle triangle with integral length sides, +! {a,b,c}, there are exactly three solutions for p = 120. + +! {20,48,52}, {24,45,51}, {30,40,50} + +! For which value of p < 1000, is the number of solutions maximised? + + +! SOLUTION +! -------- + +! Algorithm adapted from http://mathworld.wolfram.com/PythagoreanTriple.html + +! Basically, this makes an array of 1000 zeros, recursively creates primitive +! triples using the three transforms and then increments the array at index +! [a+b+c] by one for each triple's sum AND its multiples under 1000 (to account +! for non-primitive triples). The answer is just the index that has the highest +! number. + +SYMBOL: p-count + + p-count get + [ [ 1+ ] change-nth ] curry each ; + +: transform ( triple matrix -- new-triple ) + [ 1array ] dip m. first ; + +: u-transform ( triple -- new-triple ) + { { 1 2 2 } { -2 -1 -2 } { 2 2 3 } } transform ; + +: a-transform ( triple -- new-triple ) + { { 1 2 2 } { 2 1 2 } { 2 2 3 } } transform ; + +: d-transform ( triple -- new-triple ) + { { -1 -2 -2 } { 2 1 2 } { 2 2 3 } } transform ; + +: (count-perimeters) ( seq -- ) + dup sum max-p < [ + dup sum adjust-p-count + [ u-transform ] keep [ a-transform ] keep d-transform + [ (count-perimeters) ] 3apply + ] [ + drop + ] if ; + +: count-perimeters ( n -- ) + 0 p-count set { 3 4 5 } (count-perimeters) ; + +PRIVATE> + +: euler039 ( -- answer ) + [ + 1000 count-perimeters p-count get [ supremum ] keep index + ] with-scope ; + +! [ euler039 ] 100 ave-time +! 2 ms run / 0 ms GC ave time - 100 trials + +MAIN: euler039 diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index 0037e4462f..86dff7a192 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -1,7 +1,7 @@ ! Copyright (c) 2007, 2008 Aaron Schaefer, Samuel Tardieu. ! See http://factorcode.org/license.txt for BSD license. -USING: definitions io io.files kernel math.parser sequences vocabs - vocabs.loader project-euler.ave-time project-euler.common math +USING: definitions io io.files kernel math math.parser project-euler.ave-time + sequences vocabs vocabs.loader project-euler.001 project-euler.002 project-euler.003 project-euler.004 project-euler.005 project-euler.006 project-euler.007 project-euler.008 project-euler.009 project-euler.010 project-euler.011 project-euler.012 @@ -11,8 +11,8 @@ USING: definitions io io.files kernel math.parser sequences vocabs project-euler.025 project-euler.026 project-euler.027 project-euler.028 project-euler.029 project-euler.030 project-euler.031 project-euler.032 project-euler.033 project-euler.034 project-euler.035 project-euler.036 - project-euler.037 project-euler.038 project-euler.067 project-euler.134 - project-euler.169 project-euler.173 project-euler.175 ; + project-euler.037 project-euler.038 project-euler.039 project-euler.067 + project-euler.134 project-euler.169 project-euler.173 project-euler.175 ; IN: project-euler Date: Sat, 2 Feb 2008 13:09:23 -0600 Subject: [PATCH 023/269] Removed obsolete vocabs --- extra/macros/zoo/authors.txt | 1 - extra/macros/zoo/zoo.factor | 38 ----------------------------------- extra/strings/lib/authors.txt | 1 - extra/strings/lib/lib.factor | 14 ------------- extra/strings/lib/tags.txt | 1 - 5 files changed, 55 deletions(-) delete mode 100755 extra/macros/zoo/authors.txt delete mode 100644 extra/macros/zoo/zoo.factor delete mode 100755 extra/strings/lib/authors.txt delete mode 100644 extra/strings/lib/lib.factor delete mode 100644 extra/strings/lib/tags.txt diff --git a/extra/macros/zoo/authors.txt b/extra/macros/zoo/authors.txt deleted file mode 100755 index 6cfd5da273..0000000000 --- a/extra/macros/zoo/authors.txt +++ /dev/null @@ -1 +0,0 @@ -Eduardo Cavazos diff --git a/extra/macros/zoo/zoo.factor b/extra/macros/zoo/zoo.factor deleted file mode 100644 index 21edc39f19..0000000000 --- a/extra/macros/zoo/zoo.factor +++ /dev/null @@ -1,38 +0,0 @@ - -USING: kernel quotations arrays sequences sequences.private macros ; - -IN: macros.zoo - -! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -! MACRO: narray ( n -- quot ) -! dup [ f ] curry -! swap [ -! [ swap [ set-nth-unsafe ] keep ] curry -! ] map concat append ; - -! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -! MACRO: map-call-with ( quots -- ) -! [ [ [ keep ] curry ] map concat ] keep length [ nip narray ] curry compose ; - -! MACRO: map-call-with2 ( quots -- ) -! dup >r -! [ [ 2dup >r >r ] swap append [ r> r> ] append ] map concat -! [ 2drop ] append -! r> length [ narray ] curry append ; - -! MACRO: map-exec-with ( words -- ) [ 1quotation ] map [ map-call-with ] curry ; - -! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -! Conceptual implementation: - -! : pcall ( seq quots -- seq ) [ call ] 2map ; - -! MACRO: pcall ( quots -- ) -! [ [ unclip ] swap append ] map -! [ [ r> swap add >r ] append ] map -! concat -! [ { } >r ] swap append ! pre -! [ drop r> ] append ; ! post diff --git a/extra/strings/lib/authors.txt b/extra/strings/lib/authors.txt deleted file mode 100755 index 6cfd5da273..0000000000 --- a/extra/strings/lib/authors.txt +++ /dev/null @@ -1 +0,0 @@ -Eduardo Cavazos diff --git a/extra/strings/lib/lib.factor b/extra/strings/lib/lib.factor deleted file mode 100644 index 223fdb2090..0000000000 --- a/extra/strings/lib/lib.factor +++ /dev/null @@ -1,14 +0,0 @@ -USING: math arrays sequences kernel splitting strings ; -IN: strings.lib - -: char>digit ( c -- i ) 48 - ; - -: string>digits ( s -- seq ) [ char>digit ] { } map-as ; - -: >Upper ( str -- str ) - dup empty? [ - unclip ch>upper 1string swap append - ] unless ; - -: >Upper-dashes ( str -- str ) - "-" split [ >Upper ] map "-" join ; diff --git a/extra/strings/lib/tags.txt b/extra/strings/lib/tags.txt deleted file mode 100644 index 42d711b32b..0000000000 --- a/extra/strings/lib/tags.txt +++ /dev/null @@ -1 +0,0 @@ -collections From 8575bc62e3e8c1575e000b44aa07f1fe7ed45f45 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 2 Feb 2008 13:29:09 -0600 Subject: [PATCH 024/269] Updating extra/ to use flags --- extra/cocoa/windows/windows.factor | 10 +++++--- extra/io/unix/files/files.factor | 4 +-- extra/io/windows/windows.factor | 7 ++++-- extra/ui/windows/windows.factor | 4 +-- extra/unix/linux/ifreq/ifreq.factor | 8 +----- extra/windows/advapi32/advapi32.factor | 34 +++++++++++++++----------- extra/windows/opengl32/opengl32.factor | 2 +- extra/windows/user32/user32.factor | 18 ++++++++++---- extra/windows/winsock/winsock.factor | 2 +- extra/x/widgets/wm/frame/frame.factor | 18 ++++++++------ extra/x11/windows/windows.factor | 27 ++++++++++---------- extra/x11/xim/xim.factor | 0 extra/x11/xlib/xlib.factor | 4 +-- 13 files changed, 77 insertions(+), 61 deletions(-) mode change 100644 => 100755 extra/cocoa/windows/windows.factor mode change 100644 => 100755 extra/unix/linux/ifreq/ifreq.factor mode change 100644 => 100755 extra/windows/advapi32/advapi32.factor mode change 100644 => 100755 extra/windows/user32/user32.factor mode change 100644 => 100755 extra/x/widgets/wm/frame/frame.factor mode change 100644 => 100755 extra/x11/windows/windows.factor mode change 100644 => 100755 extra/x11/xim/xim.factor mode change 100644 => 100755 extra/x11/xlib/xlib.factor diff --git a/extra/cocoa/windows/windows.factor b/extra/cocoa/windows/windows.factor old mode 100644 new mode 100755 index f1c66f5e58..caf5f713b7 --- a/extra/cocoa/windows/windows.factor +++ b/extra/cocoa/windows/windows.factor @@ -15,10 +15,12 @@ IN: cocoa.windows : NSBackingStoreBuffered 2 ; inline : standard-window-type - NSTitledWindowMask - NSClosableWindowMask bitor - NSMiniaturizableWindowMask bitor - NSResizableWindowMask bitor ; inline + { + NSTitledWindowMask + NSClosableWindowMask + NSMiniaturizableWindowMask + NSResizableWindowMask + } flags ; inline : ( rect -- window ) NSWindow -> alloc swap diff --git a/extra/io/unix/files/files.factor b/extra/io/unix/files/files.factor index b56e62d3c4..8b32b19e1b 100755 --- a/extra/io/unix/files/files.factor +++ b/extra/io/unix/files/files.factor @@ -12,7 +12,7 @@ IN: io.unix.files M: unix-io ( path -- stream ) open-read ; -: write-flags O_WRONLY O_CREAT O_TRUNC bitor bitor ; inline +: write-flags { O_WRONLY O_CREAT O_TRUNC } flags ; inline : open-write ( path -- fd ) write-flags file-mode open dup io-error ; @@ -20,7 +20,7 @@ M: unix-io ( path -- stream ) M: unix-io ( path -- stream ) open-write ; -: append-flags O_WRONLY O_APPEND O_CREAT bitor bitor ; inline +: append-flags { O_WRONLY O_APPEND O_CREAT } flags ; inline : open-append ( path -- fd ) append-flags file-mode open dup io-error diff --git a/extra/io/windows/windows.factor b/extra/io/windows/windows.factor index 419864b624..3cf40fedf7 100755 --- a/extra/io/windows/windows.factor +++ b/extra/io/windows/windows.factor @@ -31,8 +31,11 @@ M: windows-io normalize-directory ( string -- string ) "\\" ?tail drop "\\*" append ; : share-mode ( -- fixnum ) - FILE_SHARE_READ FILE_SHARE_WRITE bitor - FILE_SHARE_DELETE bitor ; foldable + { + FILE_SHARE_READ + FILE_SHARE_WRITE + FILE_SHARE_DELETE + } flags ; foldable : default-security-attributes ( -- obj ) "SECURITY_ATTRIBUTES" diff --git a/extra/ui/windows/windows.factor b/extra/ui/windows/windows.factor index c3ef328b29..3ee339209c 100755 --- a/extra/ui/windows/windows.factor +++ b/extra/ui/windows/windows.factor @@ -370,7 +370,7 @@ M: windows-ui-backend (close-window) class-name-ptr get-global pick GetClassInfoEx zero? [ "WNDCLASSEX" heap-size over set-WNDCLASSEX-cbSize - CS_HREDRAW CS_VREDRAW bitor CS_OWNDC bitor over set-WNDCLASSEX-style + { CS_HREDRAW CS_VREDRAW CS_OWNDC } flags over set-WNDCLASSEX-style ui-wndproc over set-WNDCLASSEX-lpfnWndProc 0 over set-WNDCLASSEX-cbClsExtra 0 over set-WNDCLASSEX-cbWndExtra @@ -387,7 +387,7 @@ M: windows-ui-backend (close-window) make-adjusted-RECT >r class-name-ptr get-global f r> >r >r >r ex-style r> r> - WS_CLIPSIBLINGS WS_CLIPCHILDREN bitor style bitor + { WS_CLIPSIBLINGS WS_CLIPCHILDREN style } flags CW_USEDEFAULT dup r> get-RECT-dimensions f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ; diff --git a/extra/unix/linux/ifreq/ifreq.factor b/extra/unix/linux/ifreq/ifreq.factor old mode 100644 new mode 100755 index c75ee9a5e4..31adc5c237 --- a/extra/unix/linux/ifreq/ifreq.factor +++ b/extra/unix/linux/ifreq/ifreq.factor @@ -58,10 +58,4 @@ IN: unix.linux.ifreq rot string>char-alien over set-struct-ifreq-ifr-ifrn swap over set-struct-ifreq-ifr-ifru - AF_INET SOCK_DGRAM 0 socket SIOCSIFMETRIC rot ioctl drop ; - -! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -USING: words quotations sequences math macros ; - -MACRO: flags ( seq -- ) 0 swap [ execute bitor ] each 1quotation ; \ No newline at end of file + AF_INET SOCK_DGRAM 0 socket SIOCSIFMETRIC rot ioctl drop ; \ No newline at end of file diff --git a/extra/windows/advapi32/advapi32.factor b/extra/windows/advapi32/advapi32.factor old mode 100644 new mode 100755 index a749fcb52b..e755d4707f --- a/extra/windows/advapi32/advapi32.factor +++ b/extra/windows/advapi32/advapi32.factor @@ -483,20 +483,26 @@ FUNCTION: BOOL LookupPrivilegeValueW ( LPCTSTR lpSystemName, : TOKEN_QUERY_SOURCE HEX: 0010 ; inline : TOKEN_ADJUST_DEFAULT HEX: 0080 ; inline : TOKEN_READ STANDARD_RIGHTS_READ TOKEN_QUERY bitor ; -: TOKEN_WRITE STANDARD_RIGHTS_WRITE - TOKEN_ADJUST_PRIVILEGES bitor - TOKEN_ADJUST_GROUPS bitor - TOKEN_ADJUST_DEFAULT bitor ; foldable -: TOKEN_ALL_ACCESS STANDARD_RIGHTS_REQUIRED - TOKEN_ASSIGN_PRIMARY bitor - TOKEN_DUPLICATE bitor - TOKEN_IMPERSONATE bitor - TOKEN_QUERY bitor - TOKEN_QUERY_SOURCE bitor - TOKEN_ADJUST_PRIVILEGES bitor - TOKEN_ADJUST_GROUPS bitor - TOKEN_ADJUST_SESSIONID bitor - TOKEN_ADJUST_DEFAULT bitor ; foldable +: TOKEN_WRITE + { + STANDARD_RIGHTS_WRITE + TOKEN_ADJUST_PRIVILEGES + TOKEN_ADJUST_GROUPS + TOKEN_ADJUST_DEFAULT + } flags ; foldable +: TOKEN_ALL_ACCESS + { + STANDARD_RIGHTS_REQUIRED + TOKEN_ASSIGN_PRIMARY + TOKEN_DUPLICATE + TOKEN_IMPERSONATE + TOKEN_QUERY + TOKEN_QUERY_SOURCE + TOKEN_ADJUST_PRIVILEGES + TOKEN_ADJUST_GROUPS + TOKEN_ADJUST_SESSIONID + TOKEN_ADJUST_DEFAULT + } flags ; foldable FUNCTION: BOOL OpenProcessToken ( HANDLE ProcessHandle, DWORD DesiredAccess, diff --git a/extra/windows/opengl32/opengl32.factor b/extra/windows/opengl32/opengl32.factor index a8d8ad8153..e4254d779b 100755 --- a/extra/windows/opengl32/opengl32.factor +++ b/extra/windows/opengl32/opengl32.factor @@ -73,7 +73,7 @@ IN: windows.opengl32 : pfd-dwFlags - PFD_DRAW_TO_WINDOW PFD_SUPPORT_OPENGL bitor PFD_DOUBLEBUFFER bitor ; + { PFD_DRAW_TO_WINDOW PFD_SUPPORT_OPENGL PFD_DOUBLEBUFFER } flags ; ! TODO: compare to http://www.nullterminator.net/opengl32.html : make-pfd ( bits -- pfd ) diff --git a/extra/windows/user32/user32.factor b/extra/windows/user32/user32.factor old mode 100644 new mode 100755 index c8f6a82fb5..18d1956bda --- a/extra/windows/user32/user32.factor +++ b/extra/windows/user32/user32.factor @@ -32,9 +32,18 @@ IN: windows.user32 : WS_MAXIMIZEBOX HEX: 00010000 ; inline ! Common window styles -: WS_OVERLAPPEDWINDOW WS_OVERLAPPED WS_CAPTION WS_SYSMENU WS_THICKFRAME WS_MINIMIZEBOX WS_MAXIMIZEBOX bitor bitor bitor bitor bitor ; foldable inline +: WS_OVERLAPPEDWINDOW + { + WS_OVERLAPPED + WS_CAPTION + WS_SYSMENU + WS_THICKFRAME + WS_MINIMIZEBOX + WS_MAXIMIZEBOX + } flags ; foldable -: WS_POPUPWINDOW WS_POPUP WS_BORDER WS_SYSMENU bitor bitor ; foldable inline +: WS_POPUPWINDOW + { WS_POPUP WS_BORDER WS_SYSMENU } flags ; foldable : WS_CHILDWINDOW WS_CHILD ; inline @@ -66,10 +75,9 @@ IN: windows.user32 : WS_EX_STATICEDGE HEX: 00020000 ; inline : WS_EX_APPWINDOW HEX: 00040000 ; inline : WS_EX_OVERLAPPEDWINDOW ( -- n ) - WS_EX_WINDOWEDGE WS_EX_CLIENTEDGE bitor ; foldable inline + WS_EX_WINDOWEDGE WS_EX_CLIENTEDGE bitor ; foldable : WS_EX_PALETTEWINDOW ( -- n ) - WS_EX_WINDOWEDGE WS_EX_TOOLWINDOW bitor - WS_EX_TOPMOST bitor ; foldable inline + { WS_EX_WINDOWEDGE WS_EX_TOOLWINDOW WS_EX_TOPMOST } flags ; foldable : CS_VREDRAW HEX: 0001 ; inline : CS_HREDRAW HEX: 0002 ; inline diff --git a/extra/windows/winsock/winsock.factor b/extra/windows/winsock/winsock.factor index ffab6786b5..197a16ea31 100755 --- a/extra/windows/winsock/winsock.factor +++ b/extra/windows/winsock/winsock.factor @@ -74,7 +74,7 @@ TYPEDEF: void* SOCKET : AI_PASSIVE 1 ; inline : AI_CANONNAME 2 ; inline : AI_NUMERICHOST 4 ; inline -: AI_MASK AI_PASSIVE AI_CANONNAME bitor AI_NUMERICHOST bitor ; +: AI_MASK { AI_PASSIVE AI_CANONNAME AI_NUMERICHOST } flags ; : NI_NUMERICHOST 1 ; : NI_NUMERICSERV 2 ; diff --git a/extra/x/widgets/wm/frame/frame.factor b/extra/x/widgets/wm/frame/frame.factor old mode 100644 new mode 100755 index d8f08d8772..36b4fa1160 --- a/extra/x/widgets/wm/frame/frame.factor +++ b/extra/x/widgets/wm/frame/frame.factor @@ -21,14 +21,16 @@ SYMBOL: swap new* >>child new* "white" <-- set-foreground >>gc - SubstructureRedirectMask - ExposureMask bitor - ButtonPressMask bitor - ButtonReleaseMask bitor - ButtonMotionMask bitor - EnterWindowMask bitor - ! experimental masks - SubstructureNotifyMask bitor + { + SubstructureRedirectMask + ExposureMask + ButtonPressMask + ButtonReleaseMask + ButtonMotionMask + EnterWindowMask + ! experimental masks + SubstructureNotifyMask + } flags >>mask <- init-widget diff --git a/extra/x11/windows/windows.factor b/extra/x11/windows/windows.factor old mode 100644 new mode 100755 index 1f44460026..586acc1210 --- a/extra/x11/windows/windows.factor +++ b/extra/x11/windows/windows.factor @@ -5,25 +5,26 @@ namespaces sequences x11.xlib x11.constants x11.glx ; IN: x11.windows : create-window-mask ( -- n ) - CWBackPixel CWBorderPixel bitor - CWColormap bitor CWEventMask bitor ; + { CWBackPixel CWBorderPixel CWColormap CWEventMask } flags ; : create-colormap ( visinfo -- colormap ) dpy get root get rot XVisualInfo-visual AllocNone XCreateColormap ; : event-mask ( -- n ) - ExposureMask - StructureNotifyMask bitor - KeyPressMask bitor - KeyReleaseMask bitor - ButtonPressMask bitor - ButtonReleaseMask bitor - PointerMotionMask bitor - FocusChangeMask bitor - EnterWindowMask bitor - LeaveWindowMask bitor - PropertyChangeMask bitor ; + { + ExposureMask + StructureNotifyMask + KeyPressMask + KeyReleaseMask + ButtonPressMask + ButtonReleaseMask + PointerMotionMask + FocusChangeMask + EnterWindowMask + LeaveWindowMask + PropertyChangeMask + } flags ; : window-attributes ( visinfo -- attributes ) "XSetWindowAttributes" diff --git a/extra/x11/xim/xim.factor b/extra/x11/xim/xim.factor old mode 100644 new mode 100755 diff --git a/extra/x11/xlib/xlib.factor b/extra/x11/xlib/xlib.factor old mode 100644 new mode 100755 index 730c4cf7cd..a13b553975 --- a/extra/x11/xlib/xlib.factor +++ b/extra/x11/xlib/xlib.factor @@ -1088,8 +1088,8 @@ FUNCTION: Status XWithdrawWindow ( : PAspect 1 7 shift ; inline : PBaseSize 1 8 shift ; inline : PWinGravity 1 9 shift ; inline -: PAllHints [ PPosition PSize PMinSize PMaxSize PResizeInc PAspect ] -0 [ execute bitor ] reduce ; inline +: PAllHints + { PPosition PSize PMinSize PMaxSize PResizeInc PAspect } flags ; foldable C-STRUCT: XSizeHints { "long" "flags" } From 61a9adb2bb28cb290dcb46a5b094a4ae64ca480b Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 2 Feb 2008 14:59:36 -0600 Subject: [PATCH 025/269] Remove a tab --- core/compiler/test/alien.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/compiler/test/alien.factor b/core/compiler/test/alien.factor index e737a76e1e..acb9a4a4fa 100755 --- a/core/compiler/test/alien.factor +++ b/core/compiler/test/alien.factor @@ -132,8 +132,8 @@ FUNCTION: int ffi_test_10 int a int b double c int d float e int f int g int h ; [ -34 ] [ 1 2 3.0 4 5.0 6 7 8 ffi_test_10 ] unit-test FUNCTION: void ffi_test_20 double x1, double x2, double x3, - double y1, double y2, double y3, - double z1, double z2, double z3 ; + double y1, double y2, double y3, + double z1, double z2, double z3 ; [ ] [ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ffi_test_20 ] unit-test From b381c123dd0a24cad6c0f0d776c84bee458e5bf6 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 2 Feb 2008 15:00:05 -0600 Subject: [PATCH 026/269] Test fixes --- core/prettyprint/prettyprint-tests.factor | 6 +++--- core/strings/strings-tests.factor | 6 ++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/core/prettyprint/prettyprint-tests.factor b/core/prettyprint/prettyprint-tests.factor index 7f7d946347..5907c22686 100755 --- a/core/prettyprint/prettyprint-tests.factor +++ b/core/prettyprint/prettyprint-tests.factor @@ -21,9 +21,9 @@ IN: temporary [ "hello\\backslash" unparse ] unit-test -[ "\"\\u123456\"" ] -[ "\u123456" unparse ] -unit-test +! [ "\"\\u123456\"" ] +! [ "\u123456" unparse ] +! unit-test [ "\"\\e\"" ] [ "\e" unparse ] diff --git a/core/strings/strings-tests.factor b/core/strings/strings-tests.factor index 459ec7b153..985c025827 100755 --- a/core/strings/strings-tests.factor +++ b/core/strings/strings-tests.factor @@ -88,8 +88,6 @@ unit-test ! Make sure aux vector is not shared [ "\udeadbe" ] [ - "\udeadbe" clone - CHAR: \u123456 over clone set-first + "\udeadbe" clone + CHAR: \u123456 over clone set-first ] unit-test - - From 70b685fad883132d6da87a1d4ae8fa0141fc1d8a Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 2 Feb 2008 15:00:16 -0600 Subject: [PATCH 027/269] Load fixes, FreeType memory usage fix --- extra/io/windows/windows.factor | 2 +- extra/opengl/opengl.factor | 7 ++++--- extra/ui/freetype/freetype.factor | 12 ++++++------ extra/ui/windows/windows.factor | 3 ++- extra/windows/advapi32/advapi32.factor | 2 +- extra/windows/opengl32/opengl32.factor | 5 ++--- extra/windows/user32/user32.factor | 2 +- extra/windows/winsock/winsock.factor | 6 +++--- 8 files changed, 20 insertions(+), 19 deletions(-) diff --git a/extra/io/windows/windows.factor b/extra/io/windows/windows.factor index 3cf40fedf7..ee3f744bb0 100755 --- a/extra/io/windows/windows.factor +++ b/extra/io/windows/windows.factor @@ -5,7 +5,7 @@ io.buffers io.files io.nonblocking io.sockets io.binary io.sockets.impl windows.errors strings io.streams.duplex kernel math namespaces sequences windows windows.kernel32 windows.shell32 windows.types windows.winsock splitting -continuations ; +continuations math.bitfields ; IN: io.windows TUPLE: windows-nt-io ; diff --git a/extra/opengl/opengl.factor b/extra/opengl/opengl.factor index 4ea91b867b..22bf657637 100755 --- a/extra/opengl/opengl.factor +++ b/extra/opengl/opengl.factor @@ -1,11 +1,11 @@ -! Copyright (C) 2005, 2007 Slava Pestov. +! Copyright (C) 2005, 2008 Slava Pestov. ! Portions copyright (C) 2007 Eduardo Cavazos. ! Portions copyright (C) 2008 Joe Groff. ! See http://factorcode.org/license.txt for BSD license. USING: alien alien.c-types continuations kernel libc math macros namespaces math.vectors math.constants math.functions math.parser opengl.gl opengl.glu combinators arrays sequences -splitting words byte-arrays ; +splitting words byte-arrays assocs ; IN: opengl : coordinates [ first2 ] 2apply ; @@ -233,7 +233,8 @@ TUPLE: sprite loc dim dim2 dlist texture ; dup sprite-dlist delete-dlist sprite-texture delete-texture ; -: free-sprites ( sprites -- ) [ [ free-sprite ] when* ] each ; +: free-sprites ( sprites -- ) + [ nip [ free-sprite ] when* ] assoc-each ; : with-translation ( loc quot -- ) GL_MODELVIEW [ >r gl-translate r> call ] do-matrix ; inline diff --git a/extra/ui/freetype/freetype.factor b/extra/ui/freetype/freetype.factor index 0d7522332f..2dade0f58e 100755 --- a/extra/ui/freetype/freetype.factor +++ b/extra/ui/freetype/freetype.factor @@ -36,13 +36,13 @@ M: font hashcode* drop font hashcode* ; : close-freetype ( -- ) global [ - open-fonts [ values [ close-font ] each f ] change + open-fonts [ [ drop close-font ] assoc-each f ] change freetype [ FT_Done_FreeType f ] change ] bind ; M: freetype-renderer free-fonts ( world -- ) dup world-handle select-gl-context - world-fonts values [ second free-sprites ] each ; + world-fonts [ nip second free-sprites ] assoc-each ; : ttf-name ( font style -- name ) 2array H{ @@ -100,7 +100,7 @@ SYMBOL: dpi swap set-font-height ; : ( handle -- font ) - V{ } clone + H{ } clone { set-font-handle set-font-widths } font construct dup init-font ; @@ -119,7 +119,7 @@ M: freetype-renderer open-font ( font -- open-font ) : char-width ( open-font char -- w ) over font-widths [ dupd load-glyph glyph-hori-advance ft-ceil - ] cache-nth nip ; + ] cache nip ; M: freetype-renderer string-width ( open-font string -- w ) 0 -rot [ char-width + ] with each ; @@ -175,7 +175,7 @@ M: freetype-renderer string-height ( open-font string -- h ) [ bitmap>texture ] keep [ init-sprite ] keep ; : draw-char ( open-font char sprites -- ) - [ dupd ] cache-nth nip + [ dupd ] cache nip sprite-dlist glCallList ; : (draw-string) ( open-font sprites string loc -- ) @@ -186,7 +186,7 @@ M: freetype-renderer string-height ( open-font string -- h ) ] do-enabled ; : font-sprites ( open-font world -- pair ) - world-fonts [ open-font V{ } clone 2array ] cache ; + world-fonts [ open-font H{ } clone 2array ] cache ; M: freetype-renderer draw-string ( font string loc -- ) >r >r world get font-sprites first2 r> r> (draw-string) ; diff --git a/extra/ui/windows/windows.factor b/extra/ui/windows/windows.factor index 3ee339209c..c831a959d0 100755 --- a/extra/ui/windows/windows.factor +++ b/extra/ui/windows/windows.factor @@ -6,7 +6,8 @@ math math.vectors namespaces prettyprint sequences strings vectors words windows.kernel32 windows.gdi32 windows.user32 windows.opengl32 windows.messages windows.types windows.nt windows threads timers libc combinators continuations -command-line shuffle opengl ui.render unicode.case ascii ; +command-line shuffle opengl ui.render unicode.case ascii +math.bitfields ; IN: ui.windows TUPLE: windows-ui-backend ; diff --git a/extra/windows/advapi32/advapi32.factor b/extra/windows/advapi32/advapi32.factor index 3f62082047..d3413b5695 100755 --- a/extra/windows/advapi32/advapi32.factor +++ b/extra/windows/advapi32/advapi32.factor @@ -1,4 +1,4 @@ -USING: alien.syntax kernel math windows.types ; +USING: alien.syntax kernel math windows.types math.bitfields ; IN: windows.advapi32 LIBRARY: advapi32 diff --git a/extra/windows/opengl32/opengl32.factor b/extra/windows/opengl32/opengl32.factor index e4254d779b..c38579c95e 100755 --- a/extra/windows/opengl32/opengl32.factor +++ b/extra/windows/opengl32/opengl32.factor @@ -1,7 +1,8 @@ ! Copyright (C) 2005, 2006 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: alien alien.c-types alien.syntax parser namespaces kernel -math windows.types windows.types init assocs sequences libc ; +math math.bitfields windows.types windows.types init assocs +sequences libc ; IN: windows.opengl32 ! PIXELFORMATDESCRIPTOR flags @@ -70,8 +71,6 @@ IN: windows.opengl32 : WGL_SWAP_UNDERLAY14 HEX: 20000000 ; inline : WGL_SWAP_UNDERLAY15 HEX: 40000000 ; inline - - : pfd-dwFlags { PFD_DRAW_TO_WINDOW PFD_SUPPORT_OPENGL PFD_DOUBLEBUFFER } flags ; diff --git a/extra/windows/user32/user32.factor b/extra/windows/user32/user32.factor index 18d1956bda..39879bf91d 100755 --- a/extra/windows/user32/user32.factor +++ b/extra/windows/user32/user32.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2005, 2006 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: alien alien.syntax parser namespaces kernel math -windows.types shuffle ; +windows.types shuffle math.bitfields ; IN: windows.user32 ! HKL for ActivateKeyboardLayout diff --git a/extra/windows/winsock/winsock.factor b/extra/windows/winsock/winsock.factor index 197a16ea31..cc19cdc2a3 100755 --- a/extra/windows/winsock/winsock.factor +++ b/extra/windows/winsock/winsock.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2006 Mackenzie Straight, Doug Coleman. -USING: alien alien.c-types alien.syntax arrays byte-arrays kernel -math sequences windows.types windows.kernel32 windows.errors structs -windows ; +USING: alien alien.c-types alien.syntax arrays byte-arrays +kernel math sequences windows.types windows.kernel32 +windows.errors structs windows math.bitfields ; IN: windows.winsock USE: libc From 9667afcb816c042e4c396a0b048cbebdbd9b75f0 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sat, 2 Feb 2008 13:14:22 -0800 Subject: [PATCH 028/269] cel-shading, line-art, and bunny touch their magic rings together and become Super Bunny Demo --- core/alien/c-types/c-types.factor | 2 +- extra/bunny/bunny.factor | 158 ++++------- extra/cel-shading/cel-shading.factor | 115 -------- extra/line-art/line-art.factor | 254 ------------------ .../demo-support}/authors.txt | 0 .../demo-support/demo-support.factor} | 2 +- .../demo-support}/summary.txt | 0 .../demo-support}/tags.txt | 0 extra/opengl/opengl.factor | 11 +- 9 files changed, 63 insertions(+), 479 deletions(-) delete mode 100644 extra/cel-shading/cel-shading.factor delete mode 100644 extra/line-art/line-art.factor rename extra/{opengl-demo-support => opengl/demo-support}/authors.txt (100%) rename extra/{opengl-demo-support/opengl-demo-support.factor => opengl/demo-support/demo-support.factor} (99%) rename extra/{opengl-demo-support => opengl/demo-support}/summary.txt (100%) rename extra/{opengl-demo-support => opengl/demo-support}/tags.txt (100%) diff --git a/core/alien/c-types/c-types.factor b/core/alien/c-types/c-types.factor index d260eb9b8f..8ab703eb7e 100755 --- a/core/alien/c-types/c-types.factor +++ b/core/alien/c-types/c-types.factor @@ -109,7 +109,7 @@ M: c-type stack-size c-type-size ; GENERIC: byte-length ( seq -- n ) flushable -M: float-array byte-length length "float" heap-size * ; +M: float-array byte-length length "double" heap-size * ; M: byte-array byte-length length ; diff --git a/extra/bunny/bunny.factor b/extra/bunny/bunny.factor index 479d9cb39b..efebefcef3 100755 --- a/extra/bunny/bunny.factor +++ b/extra/bunny/bunny.factor @@ -1,123 +1,73 @@ -! From http://www.ffconsultancy.com/ocaml/bunny/index.html USING: alien alien.c-types arrays sequences math math.vectors math.matrices math.parser io io.files kernel opengl opengl.gl opengl.glu shuffle http.client vectors timers namespaces ui.gadgets ui.gadgets.canvas ui.render ui splitting combinators tools.time system combinators.lib combinators.cleave -float-arrays ; +float-arrays continuations opengl.demo-support multiline +ui.gestures +bunny.fixed-pipeline bunny.cel-shaded bunny.outlined bunny.model ; IN: bunny -: numbers ( str -- seq ) - " " split [ string>number ] map [ ] subset ; + -: parse-model ( stream -- vs is ) - [ - 100000 100000 (parse-model) - ] with-stream - [ - over length # " vertices, " % - dup length # " triangles" % - ] "" make print ; +TUPLE: bunny-gadget model geom draw-seq draw-n ; -: n ( vs triple -- n ) - swap [ nth ] curry map - dup third over first v- >r dup second swap first v- r> cross - vneg normalize ; +: ( -- bunny-gadget ) + 0.0 0.0 0.375 + maybe-download read-model { + set-delegate + set-bunny-gadget-model + } bunny-gadget construct ; -: normal ( ns vs triple -- ) - [ n ] keep [ rot [ v+ ] change-nth ] each-with2 ; +: bunny-gadget-draw ( gadget -- draw ) + { bunny-gadget-draw-n bunny-gadget-draw-seq } + get-slots nth ; -: normals ( vs is -- ns ) - over length { 0.0 0.0 0.0 } -rot - [ >r 2dup r> normal ] each drop - [ normalize ] map ; +: bunny-gadget-next-draw ( gadget -- ) + dup { bunny-gadget-draw-seq bunny-gadget-draw-n } + get-slots + 1+ swap length mod + swap [ set-bunny-gadget-draw-n ] keep relayout-1 ; -: read-model ( stream -- model ) - "Reading model" print flush [ - parse-model [ normals ] 2keep 3array - ] time ; - -: make-vertex-buffers ( model -- array element-array ) - [ - [ first concat ] [ second concat ] bi - append >float-array - GL_ARRAY_BUFFER swap GL_STATIC_DRAW - ] [ - third concat >c-uint-array - GL_ELEMENT_ARRAY_BUFFER swap GL_STATIC_DRAW - ] bi ; - -: model-path "bun_zipper.ply" ; - -: model-url "http://factorcode.org/bun_zipper.ply" ; - -: maybe-download ( -- path ) - model-path resource-path dup exists? [ - "Downloading bunny from " write - model-url dup print flush - over download-to - ] unless ; - -: draw-triangle ( ns vs triple -- ) - [ dup roll nth gl-normal swap nth gl-vertex ] each-with2 ; - -: draw-bunny ( ns vs is -- ) - GL_TRIANGLES [ [ draw-triangle ] each-with2 ] do-state ; - -TUPLE: bunny-gadget model ; - -: ( model -- gadget ) - - { set-bunny-gadget-model set-delegate } - bunny-gadget construct ; - -M: bunny-gadget graft* 10 10 add-timer ; - -M: bunny-gadget ungraft* dup delegate ungraft* remove-timer ; - -M: bunny-gadget tick relayout-1 ; - -: aspect ( gadget -- x ) rect-dim first2 /f ; - -M: bunny-gadget draw-gadget* +M: bunny-gadget graft* ( gadget -- ) GL_DEPTH_TEST glEnable - GL_SCISSOR_TEST glDisable - 1.0 glClearDepth - 0.0 0.0 0.0 1.0 glClearColor - GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear - GL_PROJECTION glMatrixMode - glLoadIdentity - 45.0 over aspect 0.1 1.0 gluPerspective - 0.0 0.12 -0.25 0.0 0.1 0.0 0.0 1.0 0.0 gluLookAt - GL_MODELVIEW glMatrixMode - glLoadIdentity - GL_LEQUAL glDepthFunc - GL_LIGHTING glEnable - GL_LIGHT0 glEnable - GL_COLOR_MATERIAL glEnable - GL_LIGHT0 GL_POSITION { 1.0 -1.0 1.0 1.0 } >c-float-array glLightfv - millis 24000 mod 0.015 * 0.0 1.0 0.0 glRotated - GL_FRONT_AND_BACK GL_SHININESS 100.0 glMaterialf - GL_FRONT_AND_BACK GL_SPECULAR glColorMaterial - GL_FRONT_AND_BACK GL_AMBIENT_AND_DIFFUSE glColorMaterial - 0.6 0.5 0.5 1.0 glColor4d - [ bunny-gadget-model first3 draw-bunny ] draw-canvas ; + dup bunny-gadget-model + over { + [ ] + [ ] + [ ] + } map-call-with [ ] subset + 0 + roll { + set-bunny-gadget-geom + set-bunny-gadget-draw-seq + set-bunny-gadget-draw-n + } set-slots ; -M: bunny-gadget pref-dim* drop { 400 300 } ; +M: bunny-gadget ungraft* ( gadget -- ) + { bunny-gadget-geom bunny-gadget-draw-seq } get-slots + [ [ dispose ] when* ] each + [ dispose ] when* ; + +M: bunny-gadget draw-gadget* ( gadget -- ) + 0.15 0.15 0.15 1.0 glClearColor + GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear + dup demo-gadget-set-matrices + GL_MODELVIEW glMatrixMode + 0.0 -0.12 0.0 glTranslatef + { bunny-gadget-geom bunny-gadget-draw } get-slots + draw-bunny ; + +M: bunny-gadget pref-dim* ( gadget -- dim ) + drop { 640 480 } ; + +bunny-gadget H{ + { T{ key-down f f "TAB" } [ bunny-gadget-next-draw ] } +} set-gestures : bunny-window ( -- ) - [ - maybe-download read-model - "Bunny" open-window - ] with-ui ; + [ "Bunny" open-window ] with-ui ; MAIN: bunny-window diff --git a/extra/cel-shading/cel-shading.factor b/extra/cel-shading/cel-shading.factor deleted file mode 100644 index 992fd9655d..0000000000 --- a/extra/cel-shading/cel-shading.factor +++ /dev/null @@ -1,115 +0,0 @@ -USING: arrays bunny combinators.lib io io.files kernel - math math.functions multiline continuations debugger - opengl opengl.gl opengl-demo-support - sequences ui ui.gadgets ui.render ; -IN: cel-shading - -TUPLE: cel-shading-gadget model program vertices elements ; - -: ( -- cel-shading-gadget ) - 0.0 0.0 0.375 - maybe-download read-model { - set-delegate - set-cel-shading-gadget-model - } cel-shading-gadget construct ; - -STRING: cel-shading-vertex-shader-source -varying vec3 position, normal; - -void -main() -{ - gl_Position = ftransform(); - - position = gl_Vertex.xyz; - normal = gl_Normal; -} - -; - -STRING: cel-shading-fragment-shader-source -varying vec3 position, normal; -uniform vec3 light_direction; -uniform vec4 color; -uniform vec4 ambient, diffuse; - -float -smooth_modulate(vec3 direction, vec3 normal) -{ - return clamp(dot(direction, normal), 0.0, 1.0); -} - -float -modulate(vec3 direction, vec3 normal) -{ - float m = smooth_modulate(direction, normal); - return smoothstep(0.0, 0.01, m) * 0.4 + smoothstep(0.49, 0.5, m) * 0.5; -} - -void -main() -{ - vec3 direction = normalize(light_direction - position); - gl_FragColor = ambient + diffuse * color * vec4(vec3(modulate(direction, normal)), 1); -} - -; - -: make-cel-shading-program ( -- program ) - cel-shading-vertex-shader-source cel-shading-fragment-shader-source - ; - -M: cel-shading-gadget graft* ( gadget -- ) - "2.0" { - "GL_ARB_shader_objects" - "GL_ARB_vertex_buffer_object" - } require-gl-version-or-extensions - 0.0 0.0 0.0 1.0 glClearColor - GL_CULL_FACE glEnable - GL_DEPTH_TEST glEnable - dup cel-shading-gadget-model make-vertex-buffers - make-cel-shading-program roll { - set-cel-shading-gadget-vertices - set-cel-shading-gadget-elements - set-cel-shading-gadget-program - } set-slots ; - -M: cel-shading-gadget ungraft* ( gadget -- ) - { - [ cel-shading-gadget-program [ delete-gl-program ] when* ] - [ cel-shading-gadget-elements [ delete-gl-buffer ] when* ] - [ cel-shading-gadget-vertices [ delete-gl-buffer ] when* ] - } call-with ; - -: cel-shading-draw-setup ( gadget -- gadget ) - [ demo-gadget-set-matrices ] keep - [ cel-shading-gadget-program { - [ "light_direction" glGetUniformLocation -25.0 45.0 80.0 glUniform3f ] - [ "color" glGetUniformLocation 0.6 0.5 0.5 1.0 glUniform4f ] - [ "ambient" glGetUniformLocation 0.2 0.2 0.2 0.2 glUniform4f ] - [ "diffuse" glGetUniformLocation 0.8 0.8 0.8 0.8 glUniform4f ] - } call-with ] keep ; - -M: cel-shading-gadget draw-gadget* ( gadget -- ) - dup cel-shading-gadget-program [ - cel-shading-draw-setup - 0.0 -0.12 0.0 glTranslatef - dup { - cel-shading-gadget-vertices - cel-shading-gadget-elements - } get-slots [ - GL_VERTEX_ARRAY GL_NORMAL_ARRAY 2array [ - GL_FLOAT 0 0 buffer-offset glNormalPointer - cel-shading-gadget-model dup - first length 3 * 4 * buffer-offset - 3 GL_FLOAT 0 roll glVertexPointer - third length 3 * - GL_TRIANGLES swap GL_UNSIGNED_INT 0 buffer-offset glDrawElements - ] all-enabled-client-state - ] with-array-element-buffers - ] with-gl-program ; - -: cel-shading-window ( -- ) - [ "Cel Shading" open-window ] with-ui ; - -MAIN: cel-shading-window diff --git a/extra/line-art/line-art.factor b/extra/line-art/line-art.factor deleted file mode 100644 index d78ea8a4ee..0000000000 --- a/extra/line-art/line-art.factor +++ /dev/null @@ -1,254 +0,0 @@ -USING: arrays bunny combinators.lib continuations io io.files kernel - math math.functions math.vectors multiline - namespaces debugger - opengl opengl.gl opengl-demo-support - prettyprint - sequences ui ui.gadgets ui.gestures ui.render ; -IN: line-art - -TUPLE: line-art-gadget - model step1-program step2-program - framebuffer color-texture normal-texture depth-texture framebuffer-dim ; - -: ( -- line-art-gadget ) - 40.0 -5.0 0.275 - maybe-download read-model - { set-delegate set-line-art-gadget-model } line-art-gadget construct ; - -STRING: line-art-step1-vertex-shader-source -varying vec3 normal; - -void -main() -{ - gl_Position = ftransform(); - normal = gl_Normal; -} - -; - -STRING: line-art-step1-fragment-shader-source -varying vec3 normal; -uniform vec4 color; - -void -main() -{ - gl_FragData[0] = color; - gl_FragData[1] = vec4(normal, 1); -} - -; - -STRING: line-art-step2-vertex-shader-source -varying vec2 coord; - -void -main() -{ - gl_Position = ftransform(); - coord = (gl_Vertex * vec4(0.5) + vec4(0.5)).xy; -} - -; - -STRING: line-art-step2-fragment-shader-source -uniform sampler2D colormap, normalmap, depthmap; -uniform vec4 line_color; -varying vec2 coord; - -const float DEPTH_RATIO_THRESHOLD = 1.001, SAMPLE_SPREAD = 1.0/512.0; - -float -depth_sample(vec2 c) -{ - return texture2D(depthmap, c).x; -} -bool -are_depths_border(vec3 depths) -{ - return any(lessThan(depths, vec3(1.0/DEPTH_RATIO_THRESHOLD))) - || any(greaterThan(depths, vec3(DEPTH_RATIO_THRESHOLD))); -} - -vec3 -normal_sample(vec2 c) -{ - return texture2D(normalmap, c).xyz; -} - -float -min6(float a, float b, float c, float d, float e, float f) -{ - return min(min(min(min(min(a, b), c), d), e), f); -} - -float -border_factor(vec2 c) -{ - vec2 coord1 = c + vec2(-SAMPLE_SPREAD, -SAMPLE_SPREAD), - coord2 = c + vec2( SAMPLE_SPREAD, -SAMPLE_SPREAD), - coord3 = c + vec2(-SAMPLE_SPREAD, SAMPLE_SPREAD), - coord4 = c + vec2( SAMPLE_SPREAD, SAMPLE_SPREAD); - - vec3 normal1 = normal_sample(coord1), - normal2 = normal_sample(coord2), - normal3 = normal_sample(coord3), - normal4 = normal_sample(coord4); - - if (dot(normal1, normal1) < 0.5 - && dot(normal2, normal2) < 0.5 - && dot(normal3, normal3) < 0.5 - && dot(normal4, normal4) < 0.5) { - return 0.0; - } else { - vec4 depths = vec4(depth_sample(coord1), - depth_sample(coord2), - depth_sample(coord3), - depth_sample(coord4)); - - vec3 ratios1 = depths.xxx/depths.yzw, ratios2 = depths.yyz/depths.zww; - - if (are_depths_border(ratios1) || are_depths_border(ratios2)) { - return 1.0; - } else { - float normal_border = 1.0 - min6( - dot(normal1, normal2), - dot(normal1, normal3), - dot(normal1, normal4), - dot(normal2, normal3), - dot(normal2, normal4), - dot(normal3, normal4) - ); - - return normal_border; - } - } -} - -void -main() -{ - gl_FragColor = mix(texture2D(colormap, coord), line_color, border_factor(coord)); -} - -; - -: (line-art-step1-program) ( -- step1 ) - line-art-step1-vertex-shader-source line-art-step1-fragment-shader-source - ; -: (line-art-step2-program) ( -- step2 ) - line-art-step2-vertex-shader-source line-art-step2-fragment-shader-source - ; - -: (line-art-framebuffer-texture) ( dim iformat xformat -- texture ) - swapd >r >r >r - GL_TEXTURE0 glActiveTexture - gen-texture GL_TEXTURE_2D over glBindTexture - GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP glTexParameteri - GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP glTexParameteri - GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST glTexParameteri - GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST glTexParameteri - GL_TEXTURE_2D 0 r> r> first2 0 r> GL_UNSIGNED_BYTE f glTexImage2D ; - -: (line-art-color-texture) ( dim -- texture ) - GL_RGBA16F_ARB GL_RGBA (line-art-framebuffer-texture) ; - -: (line-art-normal-texture) ( dim -- texture ) - GL_RGBA16F_ARB GL_RGBA (line-art-framebuffer-texture) ; - -: (line-art-depth-texture) ( dim -- texture ) - GL_DEPTH_COMPONENT32 GL_DEPTH_COMPONENT (line-art-framebuffer-texture) ; - -: (attach-framebuffer-texture) ( texture attachment -- ) - swap >r >r GL_FRAMEBUFFER_EXT r> GL_TEXTURE_2D r> 0 glFramebufferTexture2DEXT gl-error ; - -: (line-art-framebuffer) ( color-texture normal-texture depth-texture -- framebuffer ) - 3array gen-framebuffer dup [ - swap GL_COLOR_ATTACHMENT0_EXT - GL_COLOR_ATTACHMENT1_EXT - GL_DEPTH_ATTACHMENT_EXT 3array [ (attach-framebuffer-texture) ] 2each - check-framebuffer - ] with-framebuffer ; - -: line-art-remake-framebuffer-if-needed ( gadget -- ) - dup { rect-dim rect-dim line-art-gadget-framebuffer-dim } get-slots = [ 2drop ] [ - swap >r - dup (line-art-color-texture) gl-error - swap dup (line-art-normal-texture) gl-error - swap dup (line-art-depth-texture) gl-error - swap >r - [ (line-art-framebuffer) ] 3keep - r> r> { set-line-art-gadget-framebuffer - set-line-art-gadget-color-texture - set-line-art-gadget-normal-texture - set-line-art-gadget-depth-texture - set-line-art-gadget-framebuffer-dim } set-slots - ] if ; - -M: line-art-gadget graft* ( gadget -- ) - "2.0" { "GL_ARB_draw_buffers" - "GL_ARB_shader_objects" - "GL_ARB_multitexture" - "GL_ARB_texture_float" } - require-gl-version-or-extensions - { "GL_EXT_framebuffer_object" } require-gl-extensions - GL_CULL_FACE glEnable - GL_DEPTH_TEST glEnable - (line-art-step1-program) over set-line-art-gadget-step1-program - (line-art-step2-program) swap set-line-art-gadget-step2-program ; - -M: line-art-gadget ungraft* ( gadget -- ) - dup line-art-gadget-framebuffer [ - { [ line-art-gadget-step1-program [ delete-gl-program ] when* ] - [ line-art-gadget-step2-program [ delete-gl-program ] when* ] - [ line-art-gadget-framebuffer [ delete-framebuffer ] when* ] - [ line-art-gadget-color-texture [ delete-texture ] when* ] - [ line-art-gadget-normal-texture [ delete-texture ] when* ] - [ line-art-gadget-depth-texture [ delete-texture ] when* ] - [ f swap set-line-art-gadget-framebuffer-dim ] - [ f swap set-line-art-gadget-framebuffer ] } call-with - ] [ drop ] if ; - -: line-art-draw-setup ( gadget -- gadget ) - 0.0 0.0 0.0 1.0 glClearColor - GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear - dup demo-gadget-set-matrices - dup line-art-remake-framebuffer-if-needed - gl-error ; - -: line-art-clear-framebuffer ( -- ) - GL_COLOR_ATTACHMENT0_EXT glDrawBuffer - 0.2 0.2 0.2 1.0 glClearColor - GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear - GL_COLOR_ATTACHMENT1_EXT glDrawBuffer - 0.0 0.0 0.0 0.0 glClearColor - GL_COLOR_BUFFER_BIT glClear ; - -M: line-art-gadget draw-gadget* ( gadget -- ) - line-art-draw-setup - dup line-art-gadget-framebuffer [ - line-art-clear-framebuffer - { GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT1_EXT } set-draw-buffers - dup line-art-gadget-step1-program dup [ - "color" glGetUniformLocation 0.6 0.5 0.5 1.0 glUniform4f - 0.0 -0.12 0.0 glTranslatef - dup line-art-gadget-model first3 draw-bunny - ] with-gl-program - ] with-framebuffer - init-matrices - dup line-art-gadget-color-texture GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit - dup line-art-gadget-normal-texture GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit - dup line-art-gadget-depth-texture GL_TEXTURE_2D GL_TEXTURE2 bind-texture-unit - line-art-gadget-step2-program dup [ - { [ "colormap" glGetUniformLocation 0 glUniform1i ] - [ "normalmap" glGetUniformLocation 1 glUniform1i ] - [ "depthmap" glGetUniformLocation 2 glUniform1i ] - [ "line_color" glGetUniformLocation 0.2 0.0 0.0 1.0 glUniform4f ] } call-with - { -1.0 -1.0 } { 1.0 1.0 } rect-vertices - ] with-gl-program ; - -: line-art-window ( -- ) - [ "Line Art" open-window ] with-ui ; - -MAIN: line-art-window diff --git a/extra/opengl-demo-support/authors.txt b/extra/opengl/demo-support/authors.txt similarity index 100% rename from extra/opengl-demo-support/authors.txt rename to extra/opengl/demo-support/authors.txt diff --git a/extra/opengl-demo-support/opengl-demo-support.factor b/extra/opengl/demo-support/demo-support.factor similarity index 99% rename from extra/opengl-demo-support/opengl-demo-support.factor rename to extra/opengl/demo-support/demo-support.factor index ecc6458d41..59b7a3bcc3 100644 --- a/extra/opengl-demo-support/opengl-demo-support.factor +++ b/extra/opengl/demo-support/demo-support.factor @@ -1,6 +1,6 @@ USING: arrays combinators.lib kernel math math.functions math.vectors namespaces opengl opengl.gl sequences ui ui.gadgets ui.gestures ui.render ; -IN: opengl-demo-support +IN: opengl.demo-support : NEAR-PLANE 1.0 64.0 / ; inline : FAR-PLANE 4.0 ; inline diff --git a/extra/opengl-demo-support/summary.txt b/extra/opengl/demo-support/summary.txt similarity index 100% rename from extra/opengl-demo-support/summary.txt rename to extra/opengl/demo-support/summary.txt diff --git a/extra/opengl-demo-support/tags.txt b/extra/opengl/demo-support/tags.txt similarity index 100% rename from extra/opengl-demo-support/tags.txt rename to extra/opengl/demo-support/tags.txt diff --git a/extra/opengl/opengl.factor b/extra/opengl/opengl.factor index a6aecf1b77..9b26662cef 100755 --- a/extra/opengl/opengl.factor +++ b/extra/opengl/opengl.factor @@ -444,8 +444,11 @@ PREDICATE: integer gl-program (gl-program?) ; [ "Required GLSL version " % % " not supported (" % glsl-version % " available)" % ] (require-gl) ; +: has-gl-version-or-extensions? ( version extensions -- ? ) + has-gl-extensions? swap has-gl-version? or ; + : require-gl-version-or-extensions ( version extensions -- ) - 2array [ first2 has-gl-extensions? swap has-gl-version? or ] - [ dup first (make-gl-version-error) "\n" % - second (make-gl-extensions-error) "\n" % ] - (require-gl) ; + 2array [ first2 has-gl-version-or-extensions? ] [ + dup first (make-gl-version-error) "\n" % + second (make-gl-extensions-error) "\n" % + ] (require-gl) ; From ba6660cabe1c155884ec8388e38bac51a2a378c0 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 2 Feb 2008 15:44:43 -0600 Subject: [PATCH 029/269] Fix bootstrap --- extra/cocoa/windows/windows.factor | 2 +- extra/io/unix/files/files.factor | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/cocoa/windows/windows.factor b/extra/cocoa/windows/windows.factor index caf5f713b7..b45acaf852 100755 --- a/extra/cocoa/windows/windows.factor +++ b/extra/cocoa/windows/windows.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2006, 2007 Slava Pestov ! See http://factorcode.org/license.txt for BSD license. USING: arrays kernel math cocoa cocoa.messages cocoa.classes -sequences ; +sequences math.bitfields ; IN: cocoa.windows : NSBorderlessWindowMask 0 ; inline diff --git a/extra/io/unix/files/files.factor b/extra/io/unix/files/files.factor index 8b32b19e1b..edee598435 100755 --- a/extra/io/unix/files/files.factor +++ b/extra/io/unix/files/files.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2005, 2007 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: io.backend io.nonblocking io.unix.backend io.files io -unix kernel math continuations ; +unix kernel math continuations math.bitfields ; IN: io.unix.files : read-flags O_RDONLY ; inline From 8b207d1f48891d201387da7930419e9287745308 Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Sat, 2 Feb 2008 17:22:20 -0500 Subject: [PATCH 030/269] Solution to Project Euler problem 75 --- extra/project-euler/039/039.factor | 17 +----- extra/project-euler/075/075.factor | 78 ++++++++++++++++++++++++ extra/project-euler/common/common.factor | 16 ++++- extra/project-euler/project-euler.factor | 3 +- 4 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 extra/project-euler/075/075.factor diff --git a/extra/project-euler/039/039.factor b/extra/project-euler/039/039.factor index 4df7ba610a..67578dc5f2 100644 --- a/extra/project-euler/039/039.factor +++ b/extra/project-euler/039/039.factor @@ -1,7 +1,7 @@ ! Copyright (c) 2008 Aaron Schaefer. ! See http://factorcode.org/license.txt for BSD license. -USING: arrays combinators.lib kernel math math.matrices math.ranges namespaces - sequences ; +USING: arrays combinators.lib kernel math math.ranges namespaces + project-euler.common sequences ; IN: project-euler.039 ! http://projecteuler.net/index.php?section=problems&id=39 @@ -21,6 +21,7 @@ IN: project-euler.039 ! -------- ! Algorithm adapted from http://mathworld.wolfram.com/PythagoreanTriple.html +! Identical implementation as problem #75 ! Basically, this makes an array of 1000 zeros, recursively creates primitive ! triples using the three transforms and then increments the array at index @@ -39,18 +40,6 @@ SYMBOL: p-count max-p 1- over p-count get [ [ 1+ ] change-nth ] curry each ; -: transform ( triple matrix -- new-triple ) - [ 1array ] dip m. first ; - -: u-transform ( triple -- new-triple ) - { { 1 2 2 } { -2 -1 -2 } { 2 2 3 } } transform ; - -: a-transform ( triple -- new-triple ) - { { 1 2 2 } { 2 1 2 } { 2 2 3 } } transform ; - -: d-transform ( triple -- new-triple ) - { { -1 -2 -2 } { 2 1 2 } { 2 2 3 } } transform ; - : (count-perimeters) ( seq -- ) dup sum max-p < [ dup sum adjust-p-count diff --git a/extra/project-euler/075/075.factor b/extra/project-euler/075/075.factor new file mode 100644 index 0000000000..f8ee9d50db --- /dev/null +++ b/extra/project-euler/075/075.factor @@ -0,0 +1,78 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: arrays combinators.lib kernel math math.ranges namespaces + project-euler.common sequences ; +IN: project-euler.075 + +! http://projecteuler.net/index.php?section=problems&id=75 + +! DESCRIPTION +! ----------- + +! It turns out that 12 cm is the smallest length of wire can be bent to form a +! right angle triangle in exactly one way, but there are many more examples. + +! 12 cm: (3,4,5) +! 24 cm: (6,8,10) +! 30 cm: (5,12,13) +! 36 cm: (9,12,15) +! 40 cm: (8,15,17) +! 48 cm: (12,16,20) + +! In contrast, some lengths of wire, like 20 cm, cannot be bent to form a right +! angle triangle, and other lengths allow more than one solution to be found; +! for example, using 120 cm it is possible to form exactly three different +! right angle triangles. + +! 120 cm: (30,40,50), (20,48,52), (24,45,51) + +! Given that L is the length of the wire, for how many values of L ≤ 1,000,000 +! can exactly one right angle triangle be formed? + + +! SOLUTION +! -------- + +! Algorithm adapted from http://mathworld.wolfram.com/PythagoreanTriple.html +! Identical implementation as problem #39 + +! Basically, this makes an array of 1000000 zeros, recursively creates +! primitive triples using the three transforms and then increments the array at +! index [a+b+c] by one for each triple's sum AND its multiples under 1000000 +! (to account for non-primitive triples). The answer is just the number of +! indexes that equal one. + +SYMBOL: p-count + + p-count get + [ [ 1+ ] change-nth ] curry each ; + +: (count-perimeters) ( seq -- ) + dup sum max-p < [ + dup sum adjust-p-count + [ u-transform ] keep [ a-transform ] keep d-transform + [ (count-perimeters) ] 3apply + ] [ + drop + ] if ; + +: count-perimeters ( n -- ) + 0 p-count set { 3 4 5 } (count-perimeters) ; + +PRIVATE> + +: euler075 ( -- answer ) + [ + 1000000 count-perimeters p-count get [ 1 = ] count + ] with-scope ; + +! [ euler075 ] 100 ave-time +! 1873 ms run / 123 ms GC ave time - 100 trials + +MAIN: euler075 diff --git a/extra/project-euler/common/common.factor b/extra/project-euler/common/common.factor index 609492c724..50adbe4953 100644 --- a/extra/project-euler/common/common.factor +++ b/extra/project-euler/common/common.factor @@ -1,5 +1,6 @@ USING: arrays combinators.lib kernel math math.functions math.miller-rabin - math.parser math.primes.factors math.ranges namespaces sequences sorting ; + math.matrices math.parser math.primes.factors math.ranges namespaces + sequences sorting ; IN: project-euler.common ! A collection of words used by more than one Project Euler solution @@ -16,6 +17,7 @@ IN: project-euler.common ! propagate-all - #18, #67 ! sum-proper-divisors - #21 ! tau* - #12 +! [uad]-transform - #39, #75 : nth-pair ( n seq -- nth next ) @@ -45,6 +47,9 @@ IN: project-euler.common dup perfect-square? [ sqrt >fixnum neg , ] [ drop ] if ] { } make sum ; +: transform ( triple matrix -- new-triple ) + [ 1array ] dip m. first ; + PRIVATE> : cartesian-product ( seq1 seq2 -- seq1xseq2 ) @@ -101,3 +106,12 @@ PRIVATE> dup sqrt >fixnum [1,b] [ dupd mod zero? [ [ 2 + ] dip ] when ] each drop * ; + +! These transforms are for generating primitive Pythagorean triples +: u-transform ( triple -- new-triple ) + { { 1 2 2 } { -2 -1 -2 } { 2 2 3 } } transform ; +: a-transform ( triple -- new-triple ) + { { 1 2 2 } { 2 1 2 } { 2 2 3 } } transform ; +: d-transform ( triple -- new-triple ) + { { -1 -2 -2 } { 2 1 2 } { 2 2 3 } } transform ; + diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index 86dff7a192..f5766536ef 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -12,7 +12,8 @@ USING: definitions io io.files kernel math math.parser project-euler.ave-time project-euler.029 project-euler.030 project-euler.031 project-euler.032 project-euler.033 project-euler.034 project-euler.035 project-euler.036 project-euler.037 project-euler.038 project-euler.039 project-euler.067 - project-euler.134 project-euler.169 project-euler.173 project-euler.175 ; + project-euler.075 project-euler.134 project-euler.169 project-euler.173 + project-euler.175 ; IN: project-euler Date: Sat, 2 Feb 2008 14:24:03 -0800 Subject: [PATCH 031/269] Fix has-gl-extensions? when requested extensions are not contiguous in the gl-extensions string --- extra/opengl/opengl.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/opengl/opengl.factor b/extra/opengl/opengl.factor index e000a3103e..d26c2c7685 100755 --- a/extra/opengl/opengl.factor +++ b/extra/opengl/opengl.factor @@ -401,7 +401,7 @@ PREDICATE: integer gl-program (gl-program?) ; : gl-extensions ( -- seq ) GL_EXTENSIONS glGetString " " split ; : has-gl-extensions? ( extensions -- ? ) - gl-extensions subseq? ; + gl-extensions swap [ over member? ] all? nip ; : (make-gl-extensions-error) ( required-extensions -- ) gl-extensions swap seq-diff "Required OpenGL extensions not supported:\n" % From 7da1da5fff0e539fefd5772b891977f283639d65 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sat, 2 Feb 2008 15:33:05 -0800 Subject: [PATCH 032/269] Modularize the new bunny demo, and adjust the bikeshed parameters a bit --- extra/bunny/cel-shaded/cel-shaded.factor | 95 +++++++ .../fixed-pipeline/fixed-pipeline.factor | 25 ++ extra/bunny/model/model.factor | 114 +++++++++ extra/bunny/outlined/outlined.factor | 235 ++++++++++++++++++ 4 files changed, 469 insertions(+) create mode 100644 extra/bunny/cel-shaded/cel-shaded.factor create mode 100644 extra/bunny/fixed-pipeline/fixed-pipeline.factor create mode 100644 extra/bunny/model/model.factor create mode 100644 extra/bunny/outlined/outlined.factor diff --git a/extra/bunny/cel-shaded/cel-shaded.factor b/extra/bunny/cel-shaded/cel-shaded.factor new file mode 100644 index 0000000000..eb0924f50e --- /dev/null +++ b/extra/bunny/cel-shaded/cel-shaded.factor @@ -0,0 +1,95 @@ +USING: arrays bunny.model combinators.lib continuations +kernel multiline opengl opengl.gl sequences ; +IN: bunny.cel-shaded + +STRING: vertex-shader-source +varying vec3 position, normal, viewer; + +void +main() +{ + gl_Position = ftransform(); + + position = gl_Vertex.xyz; + normal = gl_Normal; + viewer = vec3(0, 0, 1) * gl_NormalMatrix; +} + +; + +STRING: cel-shaded-fragment-shader-lib-source +varying vec3 position, normal, viewer; +uniform vec3 light_direction; +uniform vec4 color; +uniform vec4 ambient, diffuse; +uniform float shininess; + +float +modulate(vec3 direction, vec3 normal) +{ + return dot(direction, normal) * 0.5 + 0.5; +} + +float +cel(float m) +{ + return smoothstep(0.25, 0.255, m) * 0.4 + smoothstep(0.695, 0.70, m) * 0.5; +} + +vec4 +cel_light() +{ + vec3 direction = normalize(light_direction - position); + vec3 reflection = reflect(direction, normal); + vec4 ad = (ambient + diffuse * vec4(vec3(cel(modulate(direction, normal))), 1)); + float s = cel(pow(max(dot(-reflection, viewer), 0.0), shininess)); + return ad * color + vec4(vec3(s), 0); +} + +; + +STRING: cel-shaded-fragment-shader-main-source +vec4 cel_light(); + +void +main() +{ + gl_FragColor = cel_light(); +} + +; + +TUPLE: bunny-cel-shaded program ; + +: cel-shading-supported? ( -- ? ) + "2.0" { "GL_ARB_shader_objects" } + has-gl-version-or-extensions? ; + +: ( gadget -- draw ) + drop + cel-shading-supported? [ + vertex-shader-source check-gl-shader + cel-shaded-fragment-shader-lib-source check-gl-shader + cel-shaded-fragment-shader-main-source check-gl-shader + 3array check-gl-program + { set-bunny-cel-shaded-program } bunny-cel-shaded construct + ] [ f ] if ; + +: (draw-cel-shaded-bunny) ( geom program -- ) + dup [ + { + [ "light_direction" glGetUniformLocation 1.0 -1.0 1.0 glUniform3f ] + [ "color" glGetUniformLocation 0.6 0.5 0.5 1.0 glUniform4f ] + [ "ambient" glGetUniformLocation 0.2 0.2 0.2 0.2 glUniform4f ] + [ "diffuse" glGetUniformLocation 0.8 0.8 0.8 0.8 glUniform4f ] + [ "shininess" glGetUniformLocation 100.0 glUniform1f ] + } call-with + bunny-geom + ] with-gl-program ; + +M: bunny-cel-shaded draw-bunny + bunny-cel-shaded-program (draw-cel-shaded-bunny) ; + +M: bunny-cel-shaded dispose + bunny-cel-shaded-program delete-gl-program ; + diff --git a/extra/bunny/fixed-pipeline/fixed-pipeline.factor b/extra/bunny/fixed-pipeline/fixed-pipeline.factor new file mode 100644 index 0000000000..f3fb68e515 --- /dev/null +++ b/extra/bunny/fixed-pipeline/fixed-pipeline.factor @@ -0,0 +1,25 @@ +USING: alien.c-types continuations kernel +opengl opengl.gl bunny.model ; +IN: bunny.fixed-pipeline + +TUPLE: bunny-fixed-pipeline ; + +: ( gadget -- draw ) + drop + { } bunny-fixed-pipeline construct ; + +M: bunny-fixed-pipeline draw-bunny + drop + GL_LIGHTING glEnable + GL_LIGHT0 glEnable + GL_COLOR_MATERIAL glEnable + GL_LIGHT0 GL_POSITION { 1.0 -1.0 1.0 1.0 } >c-float-array glLightfv + GL_FRONT_AND_BACK GL_SHININESS 100.0 glMaterialf + GL_FRONT_AND_BACK GL_SPECULAR glColorMaterial + GL_FRONT_AND_BACK GL_AMBIENT_AND_DIFFUSE glColorMaterial + 0.6 0.5 0.5 1.0 glColor4f + bunny-geom ; + +M: bunny-fixed-pipeline dispose + drop ; + diff --git a/extra/bunny/model/model.factor b/extra/bunny/model/model.factor new file mode 100644 index 0000000000..a19adcb782 --- /dev/null +++ b/extra/bunny/model/model.factor @@ -0,0 +1,114 @@ +USING: alien alien.c-types arrays sequences math +math.vectors math.matrices math.parser io io.files kernel opengl +opengl.gl opengl.glu shuffle http.client vectors splitting +tools.time system combinators combinators.lib combinators.cleave +float-arrays continuations namespaces ; +IN: bunny.model + +: numbers ( str -- seq ) + " " split [ string>number ] map [ ] subset ; + +: (parse-model) ( vs is -- vs is ) + readln [ + numbers { + { [ dup length 5 = ] [ 3 head pick push ] } + { [ dup first 3 = ] [ 1 tail over push ] } + { [ t ] [ drop ] } + } cond (parse-model) + ] when* ; + +: parse-model ( stream -- vs is ) + [ + 100000 100000 (parse-model) + ] with-stream + [ + over length # " vertices, " % + dup length # " triangles" % + ] "" make print ; + +: n ( vs triple -- n ) + swap [ nth ] curry map + dup third over first v- >r dup second swap first v- r> cross + vneg normalize ; + +: normal ( ns vs triple -- ) + [ n ] keep [ rot [ v+ ] change-nth ] each-with2 ; + +: normals ( vs is -- ns ) + over length { 0.0 0.0 0.0 } -rot + [ >r 2dup r> normal ] each drop + [ normalize ] map ; + +: read-model ( stream -- model ) + "Reading model" print flush [ + parse-model [ normals ] 2keep 3array + ] time ; + +: model-path "bun_zipper.ply" ; + +: model-url "http://factorcode.org/bun_zipper.ply" ; + +: maybe-download ( -- path ) + model-path resource-path dup exists? [ + "Downloading bunny from " write + model-url dup print flush + over download-to + ] unless ; + +: (draw-triangle) ( ns vs triple -- ) + [ dup roll nth gl-normal swap nth gl-vertex ] each-with2 ; + +: draw-triangles ( ns vs is -- ) + GL_TRIANGLES [ [ (draw-triangle) ] each-with2 ] do-state ; + +TUPLE: bunny-dlist list ; +TUPLE: bunny-buffers array element-array nv ni ; + +: ( model -- geom ) + GL_COMPILE [ first3 draw-triangles ] make-dlist + bunny-dlist construct-boa ; + +: ( model -- geom ) + [ + [ first concat ] [ second concat ] bi + append >float-array + GL_ARRAY_BUFFER swap GL_STATIC_DRAW + ] [ + third concat >c-uint-array + GL_ELEMENT_ARRAY_BUFFER swap GL_STATIC_DRAW + ] + [ first length 3 * ] [ third length 3 * ] tetra + bunny-buffers construct-boa ; + +GENERIC: bunny-geom ( geom -- ) +GENERIC: draw-bunny ( geom draw -- ) + +M: bunny-dlist bunny-geom + bunny-dlist-list glCallList ; + +M: bunny-buffers bunny-geom + dup { + bunny-buffers-array + bunny-buffers-element-array + } get-slots [ + GL_VERTEX_ARRAY GL_NORMAL_ARRAY 2array [ + GL_DOUBLE 0 0 buffer-offset glNormalPointer + dup bunny-buffers-nv "double" heap-size * buffer-offset + 3 GL_DOUBLE 0 roll glVertexPointer + bunny-buffers-ni + GL_TRIANGLES swap GL_UNSIGNED_INT 0 buffer-offset glDrawElements + ] all-enabled-client-state + ] with-array-element-buffers ; + +M: bunny-dlist dispose + bunny-dlist-list delete-dlist ; + +M: bunny-buffers dispose + { bunny-buffers-array bunny-buffers-element-array } get-slots + delete-gl-buffer delete-gl-buffer ; + +: ( model -- geom ) + "1.5" { "GL_ARB_vertex_buffer_object" } + has-gl-version-or-extensions? + [ ] [ ] if ; + diff --git a/extra/bunny/outlined/outlined.factor b/extra/bunny/outlined/outlined.factor new file mode 100644 index 0000000000..021ac6b4d8 --- /dev/null +++ b/extra/bunny/outlined/outlined.factor @@ -0,0 +1,235 @@ +USING: arrays bunny.model bunny.cel-shaded +combinators.lib continuations kernel math multiline +opengl opengl.gl sequences ui.gadgets ; +IN: bunny.outlined + +STRING: outlined-pass1-fragment-shader-main-source +varying vec3 normal; +vec4 cel_light(); + +void +main() +{ + gl_FragData[0] = cel_light(); + gl_FragData[1] = vec4(normal, 1); +} + +; + +STRING: outlined-pass2-vertex-shader-source +varying vec2 coord; + +void +main() +{ + gl_Position = ftransform(); + coord = (gl_Vertex * vec4(0.5) + vec4(0.5)).xy; +} + +; + +STRING: outlined-pass2-fragment-shader-source +uniform sampler2D colormap, normalmap, depthmap; +uniform vec4 line_color; +varying vec2 coord; + +const float DEPTH_RATIO_THRESHOLD = 1.001, SAMPLE_SPREAD = 1.0/512.0; + +float +depth_sample(vec2 c) +{ + return texture2D(depthmap, c).x; +} +bool +are_depths_border(vec3 depths) +{ + return any(lessThan(depths, vec3(1.0/DEPTH_RATIO_THRESHOLD))) + || any(greaterThan(depths, vec3(DEPTH_RATIO_THRESHOLD))); +} + +vec3 +normal_sample(vec2 c) +{ + return texture2D(normalmap, c).xyz; +} + +float +min6(float a, float b, float c, float d, float e, float f) +{ + return min(min(min(min(min(a, b), c), d), e), f); +} + +float +border_factor(vec2 c) +{ + vec2 coord1 = c + vec2(-SAMPLE_SPREAD, -SAMPLE_SPREAD), + coord2 = c + vec2( SAMPLE_SPREAD, -SAMPLE_SPREAD), + coord3 = c + vec2(-SAMPLE_SPREAD, SAMPLE_SPREAD), + coord4 = c + vec2( SAMPLE_SPREAD, SAMPLE_SPREAD); + + vec3 normal1 = normal_sample(coord1), + normal2 = normal_sample(coord2), + normal3 = normal_sample(coord3), + normal4 = normal_sample(coord4); + + if (dot(normal1, normal1) < 0.5 + && dot(normal2, normal2) < 0.5 + && dot(normal3, normal3) < 0.5 + && dot(normal4, normal4) < 0.5) { + return 0.0; + } else { + vec4 depths = vec4(depth_sample(coord1), + depth_sample(coord2), + depth_sample(coord3), + depth_sample(coord4)); + + vec3 ratios1 = depths.xxx/depths.yzw, ratios2 = depths.yyz/depths.zww; + + if (are_depths_border(ratios1) || are_depths_border(ratios2)) { + return 1.0; + } else { + float normal_border = 1.0 - min6( + dot(normal1, normal2), + dot(normal1, normal3), + dot(normal1, normal4), + dot(normal2, normal3), + dot(normal2, normal4), + dot(normal3, normal4) + ); + + return normal_border; + } + } +} + +void +main() +{ + gl_FragColor = mix(texture2D(colormap, coord), line_color, border_factor(coord)); +} + +; + +TUPLE: bunny-outlined + gadget + pass1-program pass2-program + color-texture normal-texture depth-texture + framebuffer framebuffer-dim ; + +: outlining-supported? ( -- ? ) + "2.0" { + "GL_ARB_shading_objects" + "GL_ARB_draw_buffers" + "GL_ARB_multitexture" + } has-gl-version-or-extensions? { + "GL_EXT_framebuffer_object" + "GL_ARB_texture_float" + } has-gl-extensions? and ; + +: pass1-program ( -- program ) + vertex-shader-source check-gl-shader + cel-shaded-fragment-shader-lib-source check-gl-shader + outlined-pass1-fragment-shader-main-source check-gl-shader + 3array check-gl-program ; + +: pass2-program ( -- program ) + outlined-pass2-vertex-shader-source + outlined-pass2-fragment-shader-source ; + +: ( gadget -- draw ) + outlining-supported? [ + pass1-program pass2-program { + set-bunny-outlined-gadget + set-bunny-outlined-pass1-program + set-bunny-outlined-pass2-program + } bunny-outlined construct + ] [ drop f ] if ; + +: (framebuffer-texture) ( dim iformat xformat -- texture ) + swapd >r >r >r + GL_TEXTURE0 glActiveTexture + gen-texture GL_TEXTURE_2D over glBindTexture + GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP glTexParameteri + GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP glTexParameteri + GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST glTexParameteri + GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST glTexParameteri + GL_TEXTURE_2D 0 r> r> first2 0 r> GL_UNSIGNED_BYTE f glTexImage2D ; + +: (attach-framebuffer-texture) ( texture attachment -- ) + swap >r >r + GL_FRAMEBUFFER_EXT r> GL_TEXTURE_2D r> 0 glFramebufferTexture2DEXT + gl-error ; + +: (make-framebuffer) ( color-texture normal-texture depth-texture -- framebuffer ) + 3array gen-framebuffer dup [ + swap GL_COLOR_ATTACHMENT0_EXT + GL_COLOR_ATTACHMENT1_EXT + GL_DEPTH_ATTACHMENT_EXT 3array [ (attach-framebuffer-texture) ] 2each + check-framebuffer + ] with-framebuffer ; + +: remake-framebuffer-if-needed ( draw -- ) + dup bunny-outlined-gadget rect-dim + over bunny-outlined-framebuffer-dim + over = + [ 2drop ] + [ + swap >r + dup GL_RGBA16F_ARB GL_RGBA (framebuffer-texture) + swap dup GL_RGBA16F_ARB GL_RGBA (framebuffer-texture) + swap dup GL_DEPTH_COMPONENT32 GL_DEPTH_COMPONENT (framebuffer-texture) + swap >r + [ (make-framebuffer) ] 3keep + r> r> { + set-bunny-outlined-framebuffer + set-bunny-outlined-color-texture + set-bunny-outlined-normal-texture + set-bunny-outlined-depth-texture + set-bunny-outlined-framebuffer-dim + } set-slots + ] if ; + +: clear-framebuffer ( -- ) + GL_COLOR_ATTACHMENT0_EXT glDrawBuffer + 0.15 0.15 0.15 1.0 glClearColor + GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear + GL_COLOR_ATTACHMENT1_EXT glDrawBuffer + 0.0 0.0 0.0 0.0 glClearColor + GL_COLOR_BUFFER_BIT glClear ; + +: (pass1) ( geom draw -- ) + dup bunny-outlined-framebuffer [ + clear-framebuffer + { GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT1_EXT } set-draw-buffers + bunny-outlined-pass1-program (draw-cel-shaded-bunny) + ] with-framebuffer ; + +: (pass2) ( draw -- ) + init-matrices + dup bunny-outlined-color-texture GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit + dup bunny-outlined-normal-texture GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit + dup bunny-outlined-depth-texture GL_TEXTURE_2D GL_TEXTURE2 bind-texture-unit + bunny-outlined-pass2-program dup [ + { + [ "colormap" glGetUniformLocation 0 glUniform1i ] + [ "normalmap" glGetUniformLocation 1 glUniform1i ] + [ "depthmap" glGetUniformLocation 2 glUniform1i ] + [ "line_color" glGetUniformLocation 0.1 0.0 0.1 1.0 glUniform4f ] + } call-with + { -1.0 -1.0 } { 1.0 1.0 } rect-vertices + ] with-gl-program ; + +M: bunny-outlined draw-bunny + dup remake-framebuffer-if-needed + [ (pass1) ] keep (pass2) ; + +M: bunny-outlined dispose + { + [ bunny-outlined-pass1-program [ delete-gl-program ] when* ] + [ bunny-outlined-pass2-program [ delete-gl-program ] when* ] + [ bunny-outlined-framebuffer [ delete-framebuffer ] when* ] + [ bunny-outlined-color-texture [ delete-texture ] when* ] + [ bunny-outlined-normal-texture [ delete-texture ] when* ] + [ bunny-outlined-depth-texture [ delete-texture ] when* ] + [ f swap set-bunny-outlined-framebuffer-dim ] + } call-with ; From 557bde6206e5243140307bff964a0b73e204d139 Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Sat, 2 Feb 2008 18:53:32 -0500 Subject: [PATCH 033/269] Solution to Project Euler problem 40 --- extra/project-euler/040/040.factor | 51 ++++++++++++++++++++++++ extra/project-euler/075/075.factor | 4 +- extra/project-euler/project-euler.factor | 6 +-- 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 extra/project-euler/040/040.factor diff --git a/extra/project-euler/040/040.factor b/extra/project-euler/040/040.factor new file mode 100644 index 0000000000..8984559265 --- /dev/null +++ b/extra/project-euler/040/040.factor @@ -0,0 +1,51 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel math math.parser sequences strings ; +IN: project-euler.040 + +! http://projecteuler.net/index.php?section=problems&id=40 + +! DESCRIPTION +! ----------- + +! An irrational decimal fraction is created by concatenating the positive +! integers: + +! 0.123456789101112131415161718192021... + +! It can be seen that the 12th digit of the fractional part is 1. + +! If dn represents the nth digit of the fractional part, find the value of the +! following expression. + +! d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000 + + +! SOLUTION +! -------- + + [ + pick number>string over push-all rot 1+ -rot (concat-upto) + ] [ + 2nip + ] if ; + +: concat-upto ( n -- str ) + SBUF" " clone 1 -rot (concat-upto) ; + +: nth-integer ( n str -- m ) + [ 1- ] dip nth 1string 10 string>integer ; + +PRIVATE> + +: euler040 ( -- answer ) + 1000000 concat-upto { 1 10 100 1000 10000 100000 1000000 } + [ swap nth-integer ] with map product ; + +! [ euler040 ] 100 ave-time +! 1002 ms run / 43 ms GC ave time - 100 trials + +MAIN: euler040 diff --git a/extra/project-euler/075/075.factor b/extra/project-euler/075/075.factor index f8ee9d50db..8399235c0d 100644 --- a/extra/project-euler/075/075.factor +++ b/extra/project-euler/075/075.factor @@ -39,8 +39,8 @@ IN: project-euler.075 ! Basically, this makes an array of 1000000 zeros, recursively creates ! primitive triples using the three transforms and then increments the array at ! index [a+b+c] by one for each triple's sum AND its multiples under 1000000 -! (to account for non-primitive triples). The answer is just the number of -! indexes that equal one. +! (to account for non-primitive triples). The answer is just the total number +! of indexes that are equal to one. SYMBOL: p-count diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index f5766536ef..eb9d7d1300 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -11,9 +11,9 @@ USING: definitions io io.files kernel math math.parser project-euler.ave-time project-euler.025 project-euler.026 project-euler.027 project-euler.028 project-euler.029 project-euler.030 project-euler.031 project-euler.032 project-euler.033 project-euler.034 project-euler.035 project-euler.036 - project-euler.037 project-euler.038 project-euler.039 project-euler.067 - project-euler.075 project-euler.134 project-euler.169 project-euler.173 - project-euler.175 ; + project-euler.037 project-euler.038 project-euler.039 project-euler.040 + project-euler.067 project-euler.075 project-euler.134 project-euler.169 + project-euler.173 project-euler.175 ; IN: project-euler Date: Sat, 2 Feb 2008 18:14:26 -0600 Subject: [PATCH 034/269] Monitors work in progress --- extra/io/buffers/buffers.factor | 2 +- extra/io/monitor/monitor.factor | 2 +- extra/io/windows/nt/monitor/monitor.factor | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/extra/io/buffers/buffers.factor b/extra/io/buffers/buffers.factor index f26fe50d79..ef12543d52 100755 --- a/extra/io/buffers/buffers.factor +++ b/extra/io/buffers/buffers.factor @@ -14,7 +14,7 @@ TUPLE: buffer size ptr fill pos ; dup buffer-ptr free f swap set-buffer-ptr ; : buffer-reset ( n buffer -- ) - [ set-buffer-fill ] keep 0 swap set-buffer-pos ; + 0 swap { set-buffer-fill set-buffer-pos } set-slots ; : buffer-consume ( n buffer -- ) [ buffer-pos + ] keep diff --git a/extra/io/monitor/monitor.factor b/extra/io/monitor/monitor.factor index fe33045e01..11d1b6ecf9 100755 --- a/extra/io/monitor/monitor.factor +++ b/extra/io/monitor/monitor.factor @@ -20,7 +20,7 @@ TUPLE: monitor queue closed? ; HOOK: fill-queue io-backend ( monitor -- assoc ) : changed-file ( changed path -- ) - namespace [ swap add ] change-at ; + namespace [ append ] change-at ; : dequeue-change ( assoc -- path changes ) delete-any prune natural-sort >array ; diff --git a/extra/io/windows/nt/monitor/monitor.factor b/extra/io/windows/nt/monitor/monitor.factor index 1be91263c4..f2cc4ef92a 100755 --- a/extra/io/windows/nt/monitor/monitor.factor +++ b/extra/io/windows/nt/monitor/monitor.factor @@ -70,7 +70,8 @@ M: windows-nt-io ( path recursive? -- monitor ) FILE_NOTIFY_INFORMATION-FileName FILE_NOTIFY_INFORMATION-FileNameLength FILE_NOTIFY_INFORMATION-Action - } get-slots >r memory>u16-string path+ r> parse-action swap ; + } get-slots parse-action 1array -rot + memory>u16-string path+ ; : (changed-files) ( directory buffer -- ) 2dup parse-file-notify changed-file From ac10c4067a5401a6088ba6ba95f371e57af5714b Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Sat, 2 Feb 2008 19:31:55 -0500 Subject: [PATCH 035/269] Better method for getting last digits of an integer --- extra/project-euler/032/032.factor | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/extra/project-euler/032/032.factor b/extra/project-euler/032/032.factor index 2baa6f8714..e03ab6f89b 100644 --- a/extra/project-euler/032/032.factor +++ b/extra/project-euler/032/032.factor @@ -1,7 +1,7 @@ ! Copyright (c) 2008 Aaron Schaefer. ! See http://factorcode.org/license.txt for BSD license. -USING: combinators.lib hashtables kernel math math.combinatorics math.parser - math.ranges project-euler.common sequences ; +USING: combinators.lib hashtables kernel math math.combinatorics math.functions + math.parser math.ranges project-euler.common sequences ; IN: project-euler.032 ! http://projecteuler.net/index.php?section=problems&id=32 @@ -41,7 +41,7 @@ IN: project-euler.032 dup 1and4 swap 2and3 or ; : products ( seq -- m ) - [ number>string 4 tail* 10 string>integer ] map ; + [ 10 4 ^ mod ] map ; PRIVATE> @@ -49,7 +49,7 @@ PRIVATE> source-032 [ valid? ] subset products prune sum ; ! [ euler032 ] 10 ave-time -! 27609 ms run / 2484 ms GC ave time - 10 trials +! 23922 ms run / 1505 ms GC ave time - 10 trials ! ALTERNATE SOLUTIONS From 1954114d85e75a13b2274a093af8d4f94372f024 Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Sat, 2 Feb 2008 19:42:47 -0500 Subject: [PATCH 036/269] Solution to Project Euler problem 48 --- extra/project-euler/048/048.factor | 25 ++++++++++++++++++++++++ extra/project-euler/project-euler.factor | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 extra/project-euler/048/048.factor diff --git a/extra/project-euler/048/048.factor b/extra/project-euler/048/048.factor new file mode 100644 index 0000000000..ba58792987 --- /dev/null +++ b/extra/project-euler/048/048.factor @@ -0,0 +1,25 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: combinators.lib kernel math math.functions ; +IN: project-euler.048 + +! http://projecteuler.net/index.php?section=problems&id=48 + +! DESCRIPTION +! ----------- + +! The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. + +! Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. + + +! SOLUTION +! -------- + +: euler048 ( -- answer ) + 1000 [ 1+ dup ^ ] sigma 10 10 ^ mod ; + +! [ euler048 ] 100 ave-time +! 276 ms run / 1 ms GC ave time - 100 trials + +MAIN: euler048 diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index eb9d7d1300..d89453eb14 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -12,8 +12,8 @@ USING: definitions io io.files kernel math math.parser project-euler.ave-time project-euler.029 project-euler.030 project-euler.031 project-euler.032 project-euler.033 project-euler.034 project-euler.035 project-euler.036 project-euler.037 project-euler.038 project-euler.039 project-euler.040 - project-euler.067 project-euler.075 project-euler.134 project-euler.169 - project-euler.173 project-euler.175 ; + project-euler.048 project-euler.067 project-euler.075 project-euler.134 + project-euler.169 project-euler.173 project-euler.175 ; IN: project-euler Date: Sat, 2 Feb 2008 18:15:22 -0800 Subject: [PATCH 037/269] Make setting up shader uniform parameters nicer from with-gl-program --- extra/bunny/bunny.factor | 4 --- extra/bunny/cel-shaded/cel-shaded.factor | 17 +++++------- extra/bunny/outlined/outlined.factor | 34 +++++++++++++----------- extra/opengl/opengl-docs.factor | 25 +++++++++++++---- extra/opengl/opengl.factor | 18 +++++++++++-- 5 files changed, 62 insertions(+), 36 deletions(-) diff --git a/extra/bunny/bunny.factor b/extra/bunny/bunny.factor index efebefcef3..38f8e32fb6 100755 --- a/extra/bunny/bunny.factor +++ b/extra/bunny/bunny.factor @@ -8,10 +8,6 @@ ui.gestures bunny.fixed-pipeline bunny.cel-shaded bunny.outlined bunny.model ; IN: bunny - - TUPLE: bunny-gadget model geom draw-seq draw-n ; : ( -- bunny-gadget ) diff --git a/extra/bunny/cel-shaded/cel-shaded.factor b/extra/bunny/cel-shaded/cel-shaded.factor index eb0924f50e..fc42ca971e 100644 --- a/extra/bunny/cel-shaded/cel-shaded.factor +++ b/extra/bunny/cel-shaded/cel-shaded.factor @@ -76,16 +76,13 @@ TUPLE: bunny-cel-shaded program ; ] [ f ] if ; : (draw-cel-shaded-bunny) ( geom program -- ) - dup [ - { - [ "light_direction" glGetUniformLocation 1.0 -1.0 1.0 glUniform3f ] - [ "color" glGetUniformLocation 0.6 0.5 0.5 1.0 glUniform4f ] - [ "ambient" glGetUniformLocation 0.2 0.2 0.2 0.2 glUniform4f ] - [ "diffuse" glGetUniformLocation 0.8 0.8 0.8 0.8 glUniform4f ] - [ "shininess" glGetUniformLocation 100.0 glUniform1f ] - } call-with - bunny-geom - ] with-gl-program ; + { + { "light_direction" [ 1.0 -1.0 1.0 glUniform3f ] } + { "color" [ 0.6 0.5 0.5 1.0 glUniform4f ] } + { "ambient" [ 0.2 0.2 0.2 0.2 glUniform4f ] } + { "diffuse" [ 0.8 0.8 0.8 0.8 glUniform4f ] } + { "shininess" [ 100.0 glUniform1f ] } + } [ bunny-geom ] with-gl-program ; M: bunny-cel-shaded draw-bunny bunny-cel-shaded-program (draw-cel-shaded-bunny) ; diff --git a/extra/bunny/outlined/outlined.factor b/extra/bunny/outlined/outlined.factor index 021ac6b4d8..9de341561c 100644 --- a/extra/bunny/outlined/outlined.factor +++ b/extra/bunny/outlined/outlined.factor @@ -168,13 +168,24 @@ TUPLE: bunny-outlined check-framebuffer ] with-framebuffer ; +: dispose-framebuffer ( draw -- ) + dup bunny-outlined-framebuffer-dim [ + { + [ bunny-outlined-framebuffer [ delete-framebuffer ] when* ] + [ bunny-outlined-color-texture [ delete-texture ] when* ] + [ bunny-outlined-normal-texture [ delete-texture ] when* ] + [ bunny-outlined-depth-texture [ delete-texture ] when* ] + [ f swap set-bunny-outlined-framebuffer-dim ] + } call-with + ] [ drop ] if ; + : remake-framebuffer-if-needed ( draw -- ) dup bunny-outlined-gadget rect-dim over bunny-outlined-framebuffer-dim over = [ 2drop ] [ - swap >r + swap dup dispose-framebuffer >r dup GL_RGBA16F_ARB GL_RGBA (framebuffer-texture) swap dup GL_RGBA16F_ARB GL_RGBA (framebuffer-texture) swap dup GL_DEPTH_COMPONENT32 GL_DEPTH_COMPONENT (framebuffer-texture) @@ -209,15 +220,12 @@ TUPLE: bunny-outlined dup bunny-outlined-color-texture GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit dup bunny-outlined-normal-texture GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit dup bunny-outlined-depth-texture GL_TEXTURE_2D GL_TEXTURE2 bind-texture-unit - bunny-outlined-pass2-program dup [ - { - [ "colormap" glGetUniformLocation 0 glUniform1i ] - [ "normalmap" glGetUniformLocation 1 glUniform1i ] - [ "depthmap" glGetUniformLocation 2 glUniform1i ] - [ "line_color" glGetUniformLocation 0.1 0.0 0.1 1.0 glUniform4f ] - } call-with - { -1.0 -1.0 } { 1.0 1.0 } rect-vertices - ] with-gl-program ; + bunny-outlined-pass2-program { + { "colormap" [ 0 glUniform1i ] } + { "normalmap" [ 1 glUniform1i ] } + { "depthmap" [ 2 glUniform1i ] } + { "line_color" [ 0.1 0.0 0.1 1.0 glUniform4f ] } + } [ { -1.0 -1.0 } { 1.0 1.0 } rect-vertices ] with-gl-program ; M: bunny-outlined draw-bunny dup remake-framebuffer-if-needed @@ -227,9 +235,5 @@ M: bunny-outlined dispose { [ bunny-outlined-pass1-program [ delete-gl-program ] when* ] [ bunny-outlined-pass2-program [ delete-gl-program ] when* ] - [ bunny-outlined-framebuffer [ delete-framebuffer ] when* ] - [ bunny-outlined-color-texture [ delete-texture ] when* ] - [ bunny-outlined-normal-texture [ delete-texture ] when* ] - [ bunny-outlined-depth-texture [ delete-texture ] when* ] - [ f swap set-bunny-outlined-framebuffer-dim ] + [ dispose-framebuffer ] } call-with ; diff --git a/extra/opengl/opengl-docs.factor b/extra/opengl/opengl-docs.factor index 63875e91a8..cb0c9e884f 100644 --- a/extra/opengl/opengl-docs.factor +++ b/extra/opengl/opengl-docs.factor @@ -1,5 +1,5 @@ USING: help.markup help.syntax io kernel math quotations -opengl.gl ; +opengl.gl multiline assocs ; IN: opengl HELP: gl-color @@ -241,8 +241,19 @@ HELP: delete-gl-program { $description "Deletes the program object, invalidating it and releasing any resources allocated for it by the OpenGL implementation. Any attached " { $link gl-shader } "s are also deleted.\n\nIf the shader objects should be preserved, they should each be detached using " { $link detach-gl-program-shader } ". The program object can then be destroyed alone using " { $link delete-gl-program-only } "." } ; HELP: with-gl-program -{ $values { "program" "A " { $link gl-program } " object" } { "quot" "A quotation" } } -{ $description "Enables " { $snippet "program" } " for all OpenGL calls made in the dynamic extent of " { $snippet "quot" } ". The fixed-function pipeline is restored at the end of " { $snippet "quot" } "." } ; +{ $values { "program" "A " { $link gl-program } " object" } { "uniforms" "An " { $link assoc } " between uniform parameter names and quotations with effect " { $snippet "( uniform-location -- )" } } { "quot" "A quotation" } } +{ $description "Enables " { $snippet "program" } " for all OpenGL calls made in the dynamic extent of " { $snippet "quot" } ". The fixed-function pipeline is restored at the end of " { $snippet "quot" } ". Before calling " { $snippet "quot" } ", calls " { $link glGetUniformLocation } " on each key of " { $snippet "uniforms" } " to get the address of the uniform parameter, which is then placed on top of the stack for the associated quotation.\n\nExample:" } +{ $code <" +! From bunny.cel-shaded +: (draw-cel-shaded-bunny) ( geom program -- ) + { + { "light_direction" [ 1.0 -1.0 1.0 glUniform3f ] } + { "color" [ 0.6 0.5 0.5 1.0 glUniform4f ] } + { "ambient" [ 0.2 0.2 0.2 0.2 glUniform4f ] } + { "diffuse" [ 0.8 0.8 0.8 0.8 glUniform4f ] } + { "shininess" [ 100.0 glUniform1f ] } + } [ bunny-geom ] with-gl-program ; +"> } ; HELP: gl-version { $values { "version" "The version string from the OpenGL implementation" } } @@ -284,15 +295,19 @@ HELP: has-gl-extensions? { $values { "extensions" "A sequence of extension name strings" } { "?" "A boolean value" } } { $description "Returns true if the set of " { $snippet "extensions" } " is a subset of the implementation-supported extensions returned by " { $link gl-extensions } "." } ; +HELP: has-gl-version-or-extensions? +{ $values { "version" "A version string" } { "extensions" "A sequence of extension name strings" } } +{ $description "Returns true if either " { $link has-gl-version? } " or " { $link has-gl-extensions? } " returns true for " { $snippet "version" } " or " { $snippet "extensions" } ", respectively. Intended for use when required OpenGL functionality can be verified either by a minimum version or a set of equivalent extensions." } ; + HELP: require-gl-extensions { $values { "extensions" "A sequence of extension name strings" } } { $description "Throws an exception if " { $link has-gl-extensions? } " returns false for " { $snippet "extensions" } "." } ; HELP: require-gl-version-or-extensions { $values { "version" "A version string" } { "extensions" "A sequence of extension name strings" } } -{ $description "Throws an exception if neither " { $link has-gl-version? } " nor " { $link has-gl-extensions? } " returns true for " { $snippet "version" } " or " { $snippet "extensions" } ", respectively. Intended for use when required OpenGL functionality can be verified either by a minimum version, or a set of equivalent extensions." } ; +{ $description "Throws an exception if neither " { $link has-gl-version? } " nor " { $link has-gl-extensions? } " returns true for " { $snippet "version" } " or " { $snippet "extensions" } ", respectively. Intended for use when required OpenGL functionality can be verified either by a minimum version or a set of equivalent extensions." } ; -{ require-gl-version require-glsl-version require-gl-extensions require-gl-version-or-extensions has-gl-version? has-glsl-version? has-gl-extensions? gl-version glsl-version gl-extensions } related-words +{ require-gl-version require-glsl-version require-gl-extensions require-gl-version-or-extensions has-gl-version? has-glsl-version? has-gl-extensions? has-gl-version-or-extensions? gl-version glsl-version gl-extensions } related-words ARTICLE: "gl-utilities" "OpenGL utility words" "In addition to the full OpenGL API, the " { $vocab-link "opengl" } " vocabulary includes some utility words to give OpenGL a more Factor-like feel." diff --git a/extra/opengl/opengl.factor b/extra/opengl/opengl.factor index d26c2c7685..071f85fe12 100755 --- a/extra/opengl/opengl.factor +++ b/extra/opengl/opengl.factor @@ -5,7 +5,7 @@ USING: alien alien.c-types continuations kernel libc math macros namespaces math.vectors math.constants math.functions math.parser opengl.gl opengl.glu combinators arrays sequences -splitting words byte-arrays assocs ; +splitting words byte-arrays assocs combinators.lib ; IN: opengl : coordinates [ first2 ] 2apply ; @@ -382,9 +382,23 @@ PREDICATE: gl-shader fragment-shader (fragment-shader?) ; 2dup detach-gl-program-shader delete-gl-shader ] each delete-gl-program-only ; -: with-gl-program ( program quot -- ) +: (with-gl-program) ( program quot -- ) swap glUseProgram [ 0 glUseProgram ] [ ] cleanup ; inline +: (with-gl-program-uniforms) ( uniforms -- quot ) + [ [ swap , \ glGetUniformLocation , % ] [ ] make ] + { } assoc>map ; +: (make-with-gl-program) ( uniforms quot -- q ) + [ + \ dup , + [ swap (with-gl-program-uniforms) , \ call-with , % ] + [ ] make , + \ (with-gl-program) , + ] [ ] make ; + +MACRO: with-gl-program ( uniforms quot -- ) + (make-with-gl-program) ; + PREDICATE: integer gl-program (gl-program?) ; : ( vertex-shader-source fragment-shader-source -- program ) From 7ad7a89a2bb47412252d4bcb47c9b4b7e31e3df2 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 2 Feb 2008 23:27:27 -0600 Subject: [PATCH 038/269] move >Upper and >Upper-dashes to unicode.case --- extra/strings/lib/lib.factor | 8 -------- extra/unicode/case/case.factor | 11 ++++++++++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/extra/strings/lib/lib.factor b/extra/strings/lib/lib.factor index 719881b768..d0a34c8d28 100644 --- a/extra/strings/lib/lib.factor +++ b/extra/strings/lib/lib.factor @@ -4,11 +4,3 @@ IN: strings.lib ! : char>digit ( c -- i ) 48 - ; ! : string>digits ( s -- seq ) [ char>digit ] { } map-as ; - -! : >Upper ( str -- str ) -! dup empty? [ -! unclip ch>upper 1string swap append -! ] unless ; - -! : >Upper-dashes ( str -- str ) -! "-" split [ >Upper ] map "-" join ; diff --git a/extra/unicode/case/case.factor b/extra/unicode/case/case.factor index ee9e2a0381..f244192a32 100755 --- a/extra/unicode/case/case.factor +++ b/extra/unicode/case/case.factor @@ -1,6 +1,6 @@ USING: kernel unicode.data sequences sequences.next namespaces assocs.lib unicode.normalize math unicode.categories combinators -assocs ; +assocs strings splitting ; IN: unicode.case : ch>lower ( ch -- lower ) simple-lower at-default ; @@ -110,3 +110,12 @@ SYMBOL: locale ! Just casing locale, or overall? dup >title = ; : case-fold? ( string -- ? ) dup >case-fold = ; + + +: >Upper ( str -- str ) + dup empty? [ + unclip ch>upper 1string swap append + ] unless ; + +: >Upper-dashes ( str -- str ) + "-" split [ >Upper ] map "-" join ; From 7954bc33bfec8694e0d619b59c07215b98548a70 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 2 Feb 2008 23:27:44 -0600 Subject: [PATCH 039/269] fix server responders --- extra/http/server/responders/responders.factor | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/extra/http/server/responders/responders.factor b/extra/http/server/responders/responders.factor index a507a95a14..6df52997e1 100644 --- a/extra/http/server/responders/responders.factor +++ b/extra/http/server/responders/responders.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: arrays assocs hashtables html html.elements splitting http io kernel math math.parser namespaces parser sequences -strings io.server vectors vector-hash strings.lib ; +strings io.server vectors assocs.lib unicode.case ; IN: http.server.responders @@ -10,11 +10,11 @@ IN: http.server.responders SYMBOL: vhosts SYMBOL: responders -: >header ( value key -- vector-hash ) - VH{ } clone [ set-at ] keep ; +: >header ( value key -- multi-hash ) + H{ } clone [ insert-at ] keep ; : print-header ( alist -- ) - [ swap >Upper-dashes write ": " write print ] vector-hash-each nl ; + [ swap >Upper-dashes write ": " write print ] multi-assoc-each nl ; : response ( msg -- ) "HTTP/1.0 " write print ; @@ -23,7 +23,7 @@ SYMBOL: responders : error-head ( error -- ) dup log-error response - VH{ { "Content-Type" "text/html" } } print-header nl ; + H{ { "Content-Type" V{ "text/html" } } } print-header nl ; : httpd-error ( error -- ) #! This must be run from handle-request @@ -94,7 +94,7 @@ SYMBOL: max-post-request } member? ] assoc-subset [ ": " swap 3append log-message - ] vector-hash-each ; + ] multi-assoc-each ; : prepare-url ( url -- url ) #! This is executed in the with-request namespace. From 2c1bad2254b67b11b4780e537a832580dcdd1660 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 2 Feb 2008 23:28:33 -0600 Subject: [PATCH 040/269] improve the db protocol and update sqlite to use it --- extra/db/db.factor | 58 ++++++++++++++------------- extra/db/postgresql/postgresql.factor | 47 ++++++++++++---------- extra/db/sqlite/sqlite-tests.factor | 41 +++++++++++-------- extra/db/sqlite/sqlite.factor | 49 ++++++++++++---------- 4 files changed, 108 insertions(+), 87 deletions(-) diff --git a/extra/db/db.factor b/extra/db/db.factor index 597ac1f0f3..813ce901ff 100644 --- a/extra/db/db.factor +++ b/extra/db/db.factor @@ -12,30 +12,20 @@ C: db ( handle -- obj ) GENERIC: db-open ( db -- ) GENERIC: db-close ( db -- ) -TUPLE: statement sql params handle bound? n max ; +TUPLE: statement sql params handle bound? ; TUPLE: simple-statement ; -TUPLE: bound-statement ; TUPLE: prepared-statement ; -TUPLE: prepared-bound-statement ; HOOK: db ( str -- statement ) -HOOK: db ( str obj -- statement ) HOOK: db ( str -- statement ) -HOOK: db ( str obj -- statement ) - -! TUPLE: result sql params handle n max ; - -GENERIC: #rows ( statement -- n ) -GENERIC: #columns ( statement -- n ) -GENERIC# row-column 1 ( statement n -- obj ) -GENERIC: advance-row ( statement -- ? ) GENERIC: prepare-statement ( statement -- ) -GENERIC: reset-statement ( statement -- ) GENERIC: bind-statement* ( obj statement -- ) GENERIC: rebind-statement ( obj statement -- ) +GENERIC: execute-statement ( statement -- ) + : bind-statement ( obj statement -- ) 2dup dup statement-bound? [ rebind-statement @@ -45,7 +35,24 @@ GENERIC: rebind-statement ( obj statement -- ) tuck set-statement-params t swap set-statement-bound? ; -: sql-row ( statement -- seq ) +TUPLE: result-set sql params handle n max ; + +GENERIC: query-results ( query -- result-set ) + +GENERIC: #rows ( result-set -- n ) +GENERIC: #columns ( result-set -- n ) +GENERIC# row-column 1 ( result-set n -- obj ) +GENERIC: advance-row ( result-set -- ? ) + +: ( query handle tuple -- result-set ) + >r >r { statement-sql statement-params } get-slots r> + { + set-result-set-sql + set-result-set-params + set-result-set-handle + } result-set construct r> construct-delegate ; + +: sql-row ( result-set -- seq ) dup #columns [ row-column ] with map ; : query-each ( statement quot -- ) @@ -64,23 +71,20 @@ GENERIC: rebind-statement ( obj statement -- ) [ db swap with-variable ] curry with-disposal ] with-scope ; -: do-statement ( statement -- ) - [ advance-row drop ] with-disposal ; +: do-query ( query -- result-set ) + query-results [ [ sql-row ] query-map ] with-disposal ; -: do-query ( query -- rows ) - [ [ sql-row ] query-map ] with-disposal ; +: do-bound-query ( obj query -- rows ) + [ bind-statement ] keep do-query ; -: do-simple-query ( sql -- rows ) - do-query ; +: do-bound-command ( obj query -- rows ) + [ bind-statement ] keep execute-statement ; -: do-bound-query ( sql obj -- rows ) - do-query ; +: sql-query ( sql -- rows ) + [ do-query ] with-disposal ; -: do-simple-command ( sql -- ) - do-statement ; - -: do-bound-command ( sql obj -- ) - do-statement ; +: sql-command ( sql -- ) + [ execute-statement ] with-disposal ; SYMBOL: in-transaction HOOK: begin-transaction db ( -- ) diff --git a/extra/db/postgresql/postgresql.factor b/extra/db/postgresql/postgresql.factor index cd2c34682e..2ea1b3a1dc 100644 --- a/extra/db/postgresql/postgresql.factor +++ b/extra/db/postgresql/postgresql.factor @@ -38,32 +38,41 @@ M: postgresql-db dispose ( db -- ) : with-postgresql ( host ust pass db quot -- ) >r r> with-disposal ; -M: postgresql-statement #rows ( statement -- n ) + +M: postgresql-result-set #rows ( statement -- n ) statement-handle PQntuples ; -M: postgresql-statement #columns ( statement -- n ) +M: postgresql-result-set #columns ( statement -- n ) statement-handle PQnfields ; -M: postgresql-statement row-column ( statement n -- obj ) +M: postgresql-result-set row-column ( statement n -- obj ) >r dup statement-handle swap statement-n r> PQgetvalue ; -: init-statement ( statement -- ) - dup statement-max [ - dup do-postgresql-statement over set-statement-handle - dup #rows over set-statement-max - -1 over set-statement-n + +: init-result-set ( result-set -- ) + dup result-set-max [ + dup do-postgresql-statement over set-result-set-handle + dup #rows over set-result-set-max + -1 over set-result-set-n ] unless drop ; -: increment-n ( statement -- n ) - dup statement-n 1+ dup rot set-statement-n ; +: increment-n ( result-set -- n ) + dup result-set-n 1+ dup rot set-result-set-n ; + +M: postgresql-result-set advance-row ( result-set -- ? ) + dup init-result-set + dup increment-n swap result-set-max >= ; -M: postgresql-statement advance-row ( statement -- ? ) - dup init-statement - dup increment-n swap statement-max >= ; M: postgresql-statement dispose ( query -- ) dup statement-handle PQclear - 0 0 rot { set-statement-n set-statement-max } set-slots ; + f swap set-statement-handle ; + +M: postgresql-result-set dispose ( result-set -- ) + dup result-set-handle PQclear + 0 0 f roll { + set-statement-n set-statement-max set-statement-handle + } set-slots ; M: postgresql-statement prepare-statement ( statement -- ) [ @@ -76,12 +85,6 @@ M: postgresql-db ( sql -- statement ) { set-statement-sql } statement construct ; -M: postgresql-db ( sql array -- statement ) - { set-statement-sql set-statement-params } statement construct - ; - M: postgresql-db ( sql -- statement ) - ; - -M: postgresql-db ( sql seq -- statement ) - ; + { set-statement-sql } statement construct + ; diff --git a/extra/db/sqlite/sqlite-tests.factor b/extra/db/sqlite/sqlite-tests.factor index 79e967de24..ef1bbfc262 100644 --- a/extra/db/sqlite/sqlite-tests.factor +++ b/extra/db/sqlite/sqlite-tests.factor @@ -26,20 +26,27 @@ IN: temporary { "John" "America" } { "Jane" "New Zealand" } } -] [ test.db [ "select * from person" do-simple-query ] with-sqlite ] unit-test +] [ + "extra/db/sqlite/test.db" resource-path [ + "select * from person" sql-query + ] with-sqlite +] unit-test [ { { "John" "America" } } ] [ - test.db [ + "extra/db/sqlite/test.db" resource-path [ "select * from person where name = :name and country = :country" - { { ":name" "Jane" } { ":country" "New Zealand" } } - dup [ sql-row ] query-map + [ + { { ":name" "Jane" } { ":country" "New Zealand" } } + over do-bound-query - { { "Jane" "New Zealand" } } = [ "test fails" throw ] unless - { { ":name" "John" } { ":country" "America" } } over bind-statement + { { "Jane" "New Zealand" } } = + [ "test fails" throw ] unless - dup [ sql-row ] query-map swap dispose + { { ":name" "John" } { ":country" "America" } } + swap do-bound-query + ] with-disposal ] with-sqlite ] unit-test @@ -48,13 +55,13 @@ IN: temporary { "1" "John" "America" } { "2" "Jane" "New Zealand" } } -] [ test.db [ "select rowid, * from person" do-simple-query ] with-sqlite ] unit-test +] [ test.db [ "select rowid, * from person" sql-query ] with-sqlite ] unit-test [ ] [ "extra/db/sqlite/test.db" resource-path [ "insert into person(name, country) values('Jimmy', 'Canada')" - do-simple-command + sql-command ] with-sqlite ] unit-test @@ -64,13 +71,13 @@ IN: temporary { "2" "Jane" "New Zealand" } { "3" "Jimmy" "Canada" } } -] [ test.db [ "select rowid, * from person" do-simple-query ] with-sqlite ] unit-test +] [ test.db [ "select rowid, * from person" sql-query ] with-sqlite ] unit-test [ "extra/db/sqlite/test.db" resource-path [ [ - "insert into person(name, country) values('Jose', 'Mexico')" do-simple-command - "insert into person(name, country) values('Jose', 'Mexico')" do-simple-command + "insert into person(name, country) values('Jose', 'Mexico')" sql-command + "insert into person(name, country) values('Jose', 'Mexico')" sql-command "oops" throw ] with-transaction ] with-sqlite @@ -78,7 +85,7 @@ IN: temporary [ 3 ] [ "extra/db/sqlite/test.db" resource-path [ - "select * from person" do-simple-query length + "select * from person" sql-query length ] with-sqlite ] unit-test @@ -86,14 +93,16 @@ IN: temporary ] [ "extra/db/sqlite/test.db" resource-path [ [ - "insert into person(name, country) values('Jose', 'Mexico')" do-simple-command - "insert into person(name, country) values('Jose', 'Mexico')" do-simple-command + "insert into person(name, country) values('Jose', 'Mexico')" + sql-command + "insert into person(name, country) values('Jose', 'Mexico')" + sql-command ] with-transaction ] with-sqlite ] unit-test [ 5 ] [ "extra/db/sqlite/test.db" resource-path [ - "select * from person" do-simple-query length + "select * from person" sql-query length ] with-sqlite ] unit-test diff --git a/extra/db/sqlite/sqlite.factor b/extra/db/sqlite/sqlite.factor index c5964ed599..8352d2e11f 100644 --- a/extra/db/sqlite/sqlite.factor +++ b/extra/db/sqlite/sqlite.factor @@ -1,9 +1,9 @@ ! Copyright (C) 2005, 2008 Chris Double, Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien arrays assocs classes compiler db db.sql hashtables -io.files kernel math math.parser namespaces prettyprint sequences -strings sqlite.lib tuples alien.c-types continuations -db.sqlite.lib db.sqlite.ffi ; +USING: alien arrays assocs classes compiler db db.sql +hashtables io.files kernel math math.parser namespaces +prettyprint sequences strings tuples alien.c-types +continuations db.sqlite.lib db.sqlite.ffi ; IN: db.sqlite TUPLE: sqlite-db path ; @@ -24,47 +24,52 @@ M: sqlite-db dispose ( obj -- ) TUPLE: sqlite-statement ; C: sqlite-statement +TUPLE: sqlite-result-set ; +: ( query -- sqlite-result-set ) + dup statement-handle sqlite-result-set ; + M: sqlite-db ( str -- obj ) ; -M: sqlite-db ( str -- obj ) - ; - M: sqlite-db ( str -- obj ) db get db-handle over sqlite-prepare { set-statement-sql set-statement-handle } statement construct [ set-delegate ] keep ; -M: sqlite-db ( str assoc -- obj ) - swap tuck bind-statement ; - M: sqlite-statement dispose ( statement -- ) statement-handle sqlite-finalize ; +M: sqlite-result-set dispose ( result-set -- ) + f swap set-result-set-handle ; + M: sqlite-statement bind-statement* ( assoc statement -- ) statement-handle swap sqlite-bind-assoc ; M: sqlite-statement rebind-statement ( assoc statement -- ) - dup reset-statement + dup statement-handle sqlite-reset statement-handle swap sqlite-bind-assoc ; -M: sqlite-statement #columns ( statement -- n ) - statement-handle sqlite-#columns ; +M: sqlite-statement execute-statement ( statement -- ) + statement-handle sqlite-next drop ; -M: sqlite-statement row-column ( statement n -- obj ) - >r statement-handle r> sqlite-column ; +M: sqlite-result-set #columns ( result-set -- n ) + result-set-handle sqlite-#columns ; -M: sqlite-statement advance-row ( statement -- ? ) - statement-handle sqlite-next ; +M: sqlite-result-set row-column ( result-set n -- obj ) + >r result-set-handle r> sqlite-column ; + +M: sqlite-result-set advance-row ( result-set -- handle ? ) + result-set-handle sqlite-next ; + +M: sqlite-statement query-results ( query -- result-set ) + dup statement-handle sqlite-result-set ; -M: sqlite-statement reset-statement ( statement -- ) - statement-handle sqlite-reset ; M: sqlite-db begin-transaction ( -- ) - "BEGIN" do-simple-command ; + "BEGIN" sql-command ; M: sqlite-db commit-transaction ( -- ) - "COMMIT" do-simple-command ; + "COMMIT" sql-command ; M: sqlite-db rollback-transaction ( -- ) - "ROLLBACK" do-simple-command ; + "ROLLBACK" sql-command ; From 55cfd30543091c74889b1c8a0ae9a3838377f783 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 2 Feb 2008 23:46:56 -0600 Subject: [PATCH 041/269] remove strings.lib from automata --- extra/automata/automata.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/automata/automata.factor b/extra/automata/automata.factor index 732033fb75..cd799d477e 100644 --- a/extra/automata/automata.factor +++ b/extra/automata/automata.factor @@ -1,6 +1,6 @@ USING: kernel math math.parser random arrays hashtables assocs sequences - vars strings.lib ; + vars ; IN: automata @@ -108,4 +108,4 @@ last-line> height> [ drop step-capped-line dup ] map >bitmap >last-line ; ! : start-loop ( -- ) t >loop-flag [ loop ] in-thread ; -! : stop-loop ( -- ) f >loop-flag ; \ No newline at end of file +! : stop-loop ( -- ) f >loop-flag ; From 1b03538caa28f37c4e56986c9e22eae9fcf4d966 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 3 Feb 2008 00:14:27 -0600 Subject: [PATCH 042/269] fix compile errors in sqlite --- extra/db/db.factor | 2 +- extra/db/sqlite/ffi/ffi.factor | 1 - extra/db/sqlite/lib/lib.factor | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/extra/db/db.factor b/extra/db/db.factor index 813ce901ff..81d79eb695 100644 --- a/extra/db/db.factor +++ b/extra/db/db.factor @@ -77,7 +77,7 @@ GENERIC: advance-row ( result-set -- ? ) : do-bound-query ( obj query -- rows ) [ bind-statement ] keep do-query ; -: do-bound-command ( obj query -- rows ) +: do-bound-command ( obj query -- ) [ bind-statement ] keep execute-statement ; : sql-query ( sql -- rows ) diff --git a/extra/db/sqlite/ffi/ffi.factor b/extra/db/sqlite/ffi/ffi.factor index 77a86a8a2d..609c597b35 100644 --- a/extra/db/sqlite/ffi/ffi.factor +++ b/extra/db/sqlite/ffi/ffi.factor @@ -109,7 +109,6 @@ TYPEDEF: void sqlite3_stmt LIBRARY: sqlite FUNCTION: int sqlite3_open ( char* filename, void* ppDb ) ; -FUNCTION: int sqlite3_open_v2 ( char* filename, void* ppDb, int flags, char* zVfs ) ; FUNCTION: int sqlite3_close ( sqlite3* pDb ) ; FUNCTION: int sqlite3_prepare ( sqlite3* pDb, char* zSql, int nBytes, void* ppStmt, void* pzTail ) ; FUNCTION: int sqlite3_finalize ( sqlite3_stmt* pStmt ) ; diff --git a/extra/db/sqlite/lib/lib.factor b/extra/db/sqlite/lib/lib.factor index 99cd9c1b9f..4e4f2ca508 100644 --- a/extra/db/sqlite/lib/lib.factor +++ b/extra/db/sqlite/lib/lib.factor @@ -80,7 +80,7 @@ TUPLE: sqlite-error n message ; sqlite-step ] if ; -: sqlite-next ( prepared -- ) +: sqlite-next ( prepared -- ? ) sqlite3_step step-complete? ; : sqlite-each ( statement quot -- ) From 303cb0edc2efaedfab5d8e38cf7ab18a5a975d65 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Sun, 3 Feb 2008 03:48:08 -0600 Subject: [PATCH 043/269] Fix missing math.bitfields --- extra/x11/windows/windows.factor | 16 +--------------- extra/x11/xlib/xlib.factor | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/extra/x11/windows/windows.factor b/extra/x11/windows/windows.factor index b3220d44bd..f9158c2956 100755 --- a/extra/x11/windows/windows.factor +++ b/extra/x11/windows/windows.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2005, 2006 Eduardo Cavazos and Slava Pestov ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.c-types hashtables kernel math math.vectors +USING: alien alien.c-types hashtables kernel math math.vectors math.bitfields namespaces sequences x11.xlib x11.constants x11.glx ; IN: x11.windows @@ -12,7 +12,6 @@ IN: x11.windows XCreateColormap ; : event-mask ( -- n ) -<<<<<<< HEAD:extra/x11/windows/windows.factor { ExposureMask StructureNotifyMask @@ -26,19 +25,6 @@ IN: x11.windows LeaveWindowMask PropertyChangeMask } flags ; -======= - ExposureMask - StructureNotifyMask bitor - KeyPressMask bitor - KeyReleaseMask bitor - ButtonPressMask bitor - ButtonReleaseMask bitor - PointerMotionMask bitor - FocusChangeMask bitor - EnterWindowMask bitor - LeaveWindowMask bitor - PropertyChangeMask bitor ; ->>>>>>> a05c18152b59073c49aa313ba685516310ec74a8:extra/x11/windows/windows.factor : window-attributes ( visinfo -- attributes ) "XSetWindowAttributes" diff --git a/extra/x11/xlib/xlib.factor b/extra/x11/xlib/xlib.factor index 230b24c6d0..70006c9f64 100755 --- a/extra/x11/xlib/xlib.factor +++ b/extra/x11/xlib/xlib.factor @@ -12,7 +12,7 @@ ! and note the section. USING: kernel arrays alien alien.c-types alien.syntax -math words sequences namespaces continuations ; +math math.bitfields words sequences namespaces continuations ; IN: x11.xlib LIBRARY: xlib @@ -1078,16 +1078,16 @@ FUNCTION: Status XWithdrawWindow ( ! 17.1.7 - Setting and Reading the WM_NORMAL_HINTS Property -: USPosition 1 0 shift ; inline -: USSize 1 1 shift ; inline -: PPosition 1 2 shift ; inline -: PSize 1 3 shift ; inline -: PMinSize 1 4 shift ; inline -: PMaxSize 1 5 shift ; inline -: PResizeInc 1 6 shift ; inline -: PAspect 1 7 shift ; inline -: PBaseSize 1 8 shift ; inline -: PWinGravity 1 9 shift ; inline +: USPosition 1 0 shift ; inline +: USSize 1 1 shift ; inline +: PPosition 1 2 shift ; inline +: PSize 1 3 shift ; inline +: PMinSize 1 4 shift ; inline +: PMaxSize 1 5 shift ; inline +: PResizeInc 1 6 shift ; inline +: PAspect 1 7 shift ; inline +: PBaseSize 1 8 shift ; inline +: PWinGravity 1 9 shift ; inline : PAllHints { PPosition PSize PMinSize PMaxSize PResizeInc PAspect } flags ; foldable From 7a5d48cadb7c67c0859cd71879a68b7c58e355c7 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Sun, 3 Feb 2008 03:48:29 -0600 Subject: [PATCH 044/269] shuffle: add nrev --- extra/shuffle/shuffle.factor | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/extra/shuffle/shuffle.factor b/extra/shuffle/shuffle.factor index f9f8b030a8..f139a4864e 100644 --- a/extra/shuffle/shuffle.factor +++ b/extra/shuffle/shuffle.factor @@ -30,3 +30,8 @@ MACRO: ntuck ( n -- ) 2 + [ dup , -nrot ] bake ; : 4drop ( a b c d -- ) 3drop drop ; inline : tuckd ( x y z -- z x y z ) 2 ntuck ; inline + +MACRO: nrev ( n -- quot ) + [ 1+ ] map + reverse + [ [ -nrot ] curry ] map concat ; From 8cfc644893a61e4cd807da2dea6a6019f7c175de Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Sun, 3 Feb 2008 03:48:58 -0600 Subject: [PATCH 045/269] sequences.lib: indices --- extra/sequences/lib/lib.factor | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/extra/sequences/lib/lib.factor b/extra/sequences/lib/lib.factor index d784726754..65b0d1beb0 100755 --- a/extra/sequences/lib/lib.factor +++ b/extra/sequences/lib/lib.factor @@ -140,3 +140,13 @@ PRIVATE> : ?second ( seq -- second/f ) 1 swap ?nth ; inline : ?third ( seq -- third/f ) 2 swap ?nth ; inline : ?fourth ( seq -- fourth/f ) 3 swap ?nth ; inline + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +! List the positions of obj in seq + +: indices ( seq obj -- seq ) + >r dup length swap r> + [ = [ ] [ drop f ] if ] curry + 2map + [ ] subset ; From 1de4896c248f6f95767c4d75df3bd7ffc3130c32 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Sun, 3 Feb 2008 03:49:19 -0600 Subject: [PATCH 046/269] Add partial-apply --- extra/partial-apply/partial-apply.factor | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 extra/partial-apply/partial-apply.factor diff --git a/extra/partial-apply/partial-apply.factor b/extra/partial-apply/partial-apply.factor new file mode 100644 index 0000000000..0340e53025 --- /dev/null +++ b/extra/partial-apply/partial-apply.factor @@ -0,0 +1,26 @@ + +USING: kernel sequences quotations math parser + shuffle combinators.cleave combinators.lib sequences.lib ; + +IN: partial-apply + +! Basic conceptual implementation. Todo: get it to compile. + +: apply-n ( obj quot i -- quot ) 1+ [ -nrot ] curry swap compose curry ; + +SYMBOL: _ + +SYMBOL: ~ + +: blank-positions ( quot -- seq ) + [ length 2 - ] [ _ indices ] bi [ - ] map-with ; + +: partial-apply ( pattern -- quot ) + [ blank-positions length nrev ] + [ peek 1quotation ] + [ blank-positions ] + tri + [ apply-n ] each ; + +: $[ \ ] [ >quotation ] parse-literal \ partial-apply parsed ; parsing + From c60338df7e82a1ac8ae1df3f7fd35470a48cdc86 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sun, 3 Feb 2008 10:24:28 -0800 Subject: [PATCH 047/269] Fix bug in TextMate bundle when using the see or help commands on the first word on a line --- extra/bunny/model/model.factor | 1 - misc/Factor.tmbundle/Support/lib/tm_factor.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/extra/bunny/model/model.factor b/extra/bunny/model/model.factor index a19adcb782..e3df6bb26c 100644 --- a/extra/bunny/model/model.factor +++ b/extra/bunny/model/model.factor @@ -111,4 +111,3 @@ M: bunny-buffers dispose "1.5" { "GL_ARB_vertex_buffer_object" } has-gl-version-or-extensions? [ ] [ ] if ; - diff --git a/misc/Factor.tmbundle/Support/lib/tm_factor.rb b/misc/Factor.tmbundle/Support/lib/tm_factor.rb index 54272e5e36..2775a12ae9 100644 --- a/misc/Factor.tmbundle/Support/lib/tm_factor.rb +++ b/misc/Factor.tmbundle/Support/lib/tm_factor.rb @@ -33,6 +33,6 @@ def doc_using_statements(document) end def line_current_word(line, point) - left = line.rindex(/\s|^/, point - 1) + 1; right = line.index(/\s|$/, point) - 1 + left = line.rindex(/\s/, point - 1) || 0; right = line.index(/\s/, point) || line.length line[left..right] end From 1dbd54293c7775ebf866c9d415603b8d15a17eaf Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 3 Feb 2008 14:19:07 -0600 Subject: [PATCH 048/269] Clean up generic words a little bit --- core/definitions/definitions-tests.factor | 2 +- core/generic/generic-docs.factor | 4 ++-- core/generic/generic.factor | 14 +++++--------- core/generic/math/math.factor | 4 ++-- core/slots/slots.factor | 2 +- core/syntax/syntax.factor | 2 +- 6 files changed, 12 insertions(+), 16 deletions(-) mode change 100644 => 100755 core/generic/math/math.factor diff --git a/core/definitions/definitions-tests.factor b/core/definitions/definitions-tests.factor index 13172c0ada..a4cb4de902 100755 --- a/core/definitions/definitions-tests.factor +++ b/core/definitions/definitions-tests.factor @@ -11,7 +11,7 @@ SYMBOL: generic-1 [ generic-1 T{ combination-1 } define-generic - [ ] object \ generic-1 define-method + [ ] object \ generic-1 define-method ] with-compilation-unit [ ] [ diff --git a/core/generic/generic-docs.factor b/core/generic/generic-docs.factor index 9dfc40a869..f1cdae1c91 100755 --- a/core/generic/generic-docs.factor +++ b/core/generic/generic-docs.factor @@ -1,6 +1,6 @@ USING: help.markup help.syntax generic.math generic.standard words classes definitions kernel alien combinators sequences -math ; +math quotations ; IN: generic ARTICLE: "method-order" "Method precedence" @@ -154,7 +154,7 @@ HELP: with-methods $low-level-note ; HELP: define-method -{ $values { "method" "an instance of " { $link method } } { "class" class } { "generic" generic } } +{ $values { "method" quotation } { "class" class } { "generic" generic } } { $description "Defines a method. This is the runtime equivalent of " { $link POSTPONE: M: } "." } ; HELP: implementors diff --git a/core/generic/generic.factor b/core/generic/generic.factor index bde5fd31af..c75dd41d74 100755 --- a/core/generic/generic.factor +++ b/core/generic/generic.factor @@ -39,11 +39,6 @@ TUPLE: method loc def ; : ( def -- method ) { set-method-def } \ method construct ; -M: f method-def ; -M: f method-loc ; -M: quotation method-def ; -M: quotation method-loc drop f ; - : method ( class generic -- method/f ) "methods" word-prop at ; @@ -55,7 +50,7 @@ PREDICATE: pair method-spec : sort-methods ( assoc -- newassoc ) [ keys sort-classes ] keep - [ dupd at method-def 2array ] curry map ; + [ dupd at method-def ] curry { } map>assoc ; : methods ( word -- assoc ) "methods" word-prop sort-methods ; @@ -72,18 +67,19 @@ TUPLE: check-method class generic ; inline : define-method ( method class generic -- ) - >r bootstrap-word r> check-method + >r >r r> bootstrap-word r> check-method [ set-at ] with-methods ; ! Definition protocol M: method-spec where - dup first2 method method-loc [ ] [ second where ] ?if ; + dup first2 method [ method-loc ] [ second where ] ?if ; M: method-spec set-where first2 method set-method-loc ; M: method-spec definer drop \ M: \ ; ; -M: method-spec definition first2 method method-def ; +M: method-spec definition + first2 method dup [ method-def ] when ; : forget-method ( class generic -- ) check-method [ delete-at ] with-methods ; diff --git a/core/generic/math/math.factor b/core/generic/math/math.factor old mode 100644 new mode 100755 index 912ece3a30..d5079c5dfb --- a/core/generic/math/math.factor +++ b/core/generic/math/math.factor @@ -39,8 +39,8 @@ TUPLE: no-math-method left right generic ; \ no-math-method construct-boa throw ; : applicable-method ( generic class -- quot ) - over method method-def - [ ] [ [ no-math-method ] curry [ ] like ] ?if ; + over method + [ method-def ] [ [ no-math-method ] curry [ ] like ] ?if ; : object-method ( generic -- quot ) object bootstrap-word applicable-method ; diff --git a/core/slots/slots.factor b/core/slots/slots.factor index cd523b05c1..40f0dd3da1 100755 --- a/core/slots/slots.factor +++ b/core/slots/slots.factor @@ -10,7 +10,7 @@ TUPLE: slot-spec type name offset reader writer ; C: slot-spec : define-typecheck ( class generic quot -- ) - over define-simple-generic -rot define-method ; + over define-simple-generic -rot define-method ; : define-slot-word ( class slot word quot -- ) rot >fixnum add* define-typecheck ; diff --git a/core/syntax/syntax.factor b/core/syntax/syntax.factor index 006f1a225f..67799b92ea 100755 --- a/core/syntax/syntax.factor +++ b/core/syntax/syntax.factor @@ -126,7 +126,7 @@ IN: bootstrap.syntax f set-word location >r scan-word bootstrap-word scan-word - [ parse-definition -rot define-method ] 2keep + [ parse-definition -rot define-method ] 2keep 2array r> remember-definition ] define-syntax From d92361286da46186e4dd961dd03dde288e0b38c0 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 3 Feb 2008 14:23:14 -0600 Subject: [PATCH 049/269] Add kill-process and flesh out inotify --- extra/io/launcher/launcher-docs.factor | 11 +++++++++++ extra/io/launcher/launcher.factor | 5 +++++ extra/io/unix/launcher/launcher.factor | 6 +++++- extra/io/unix/linux/linux.factor | 15 ++++++++++----- extra/io/windows/launcher/launcher.factor | 8 ++++++-- extra/unix/unix.factor | 7 ++++--- extra/windows/kernel32/kernel32.factor | 2 +- 7 files changed, 42 insertions(+), 12 deletions(-) diff --git a/extra/io/launcher/launcher-docs.factor b/extra/io/launcher/launcher-docs.factor index 072cfcf959..c30516a83f 100755 --- a/extra/io/launcher/launcher-docs.factor +++ b/extra/io/launcher/launcher-docs.factor @@ -116,6 +116,15 @@ HELP: run-detached "The output value can be passed to " { $link wait-for-process } " to get an exit code." } ; +HELP: kill-process +{ $values { "process" process } } +{ $description "Kills a running process. Does nothing if the process has already exited." } ; + +HELP: kill-process* +{ $values { "handle" "a process handle" } } +{ $contract "Kills a running process." } +{ $notes "User code should call " { $link kill-process } " intead." } ; + HELP: process { $class-description "A class representing an active or finished process." $nl @@ -166,6 +175,8 @@ $nl "The following words are used to launch processes:" { $subsection run-process } { $subsection run-detached } +"Stopping processes:" +{ $subsection kill-process } "Redirecting standard input and output to a pipe:" { $subsection } { $subsection with-process-stream } diff --git a/extra/io/launcher/launcher.factor b/extra/io/launcher/launcher.factor index 9fb24fb51a..09a77fe985 100755 --- a/extra/io/launcher/launcher.factor +++ b/extra/io/launcher/launcher.factor @@ -84,6 +84,11 @@ HOOK: run-process* io-backend ( desc -- handle ) : run-detached ( desc -- process ) >descriptor H{ { +detached+ t } } union run-process ; +HOOK: kill-process* io-backend ( handle -- ) + +: kill-process ( process -- ) + process-handle [ kill-process* ] when* ; + HOOK: process-stream* io-backend ( desc -- stream process ) TUPLE: process-stream process ; diff --git a/extra/io/unix/launcher/launcher.factor b/extra/io/unix/launcher/launcher.factor index 0135b55a7e..030583dbe8 100755 --- a/extra/io/unix/launcher/launcher.factor +++ b/extra/io/unix/launcher/launcher.factor @@ -57,7 +57,8 @@ MEMO: 'arguments' ( -- parser ) : setup-redirection ( -- ) +stdin+ get read-flags 0 redirect +stdout+ get write-flags 1 redirect - +stderr+ get write-flags 2 redirect ; + +stderr+ get dup +stdout+ get eq? + [ 1 2 dup2 ] [ write-flags 2 redirect ] if ; : spawn-process ( -- ) [ @@ -74,6 +75,9 @@ M: unix-io run-process* ( desc -- pid ) [ spawn-process ] [ ] with-fork ] with-descriptor ; +M: unix-io kill-process* ( pid -- ) + SIGTERM kill io-error ; + : open-pipe ( -- pair ) 2 "int" dup pipe zero? [ 2 c-int-array> ] [ drop f ] if ; diff --git a/extra/io/unix/linux/linux.factor b/extra/io/unix/linux/linux.factor index 01d6159e45..9751cefe91 100755 --- a/extra/io/unix/linux/linux.factor +++ b/extra/io/unix/linux/linux.factor @@ -21,8 +21,11 @@ TUPLE: linux-monitor path wd callback ; TUPLE: inotify watches ; -: wd>path ( wd -- path ) - inotify get-global inotify-watches at linux-monitor-path ; +: watches ( -- assoc ) inotify get-global inotify-watches ; + +: wd>monitor ( wd -- monitor ) watches at ; + +: wd>path ( wd -- path ) wd>monitor linux-monitor-path ; : ( -- port ) H{ } clone @@ -31,8 +34,6 @@ TUPLE: inotify watches ; : inotify-fd inotify get-global port-handle ; -: watches inotify get-global inotify-watches ; - : (add-watch) ( path mask -- wd ) inotify-fd -rot inotify_add_watch dup io-error ; @@ -105,9 +106,13 @@ M: linux-monitor dispose ( monitor -- ) inotify-event-len "inotify-event" heap-size + swap >r + r> ; +: wd>queue ( wd -- queue ) + inotify-event-wd wd>monitor monitor-queue ; + : parse-file-notifications ( i buffer -- ) 2dup events-exhausted? [ 2drop ] [ - 2dup inotify-event@ parse-file-notify changed-file + 2dup inotify-event@ dup inotify-event-wd wd>queue + [ parse-file-notify changed-file ] bind next-event parse-file-notifications ] if ; diff --git a/extra/io/windows/launcher/launcher.factor b/extra/io/windows/launcher/launcher.factor index ec53d9152c..ad84be0825 100755 --- a/extra/io/windows/launcher/launcher.factor +++ b/extra/io/windows/launcher/launcher.factor @@ -48,10 +48,10 @@ TUPLE: CreateProcess-args } get-slots CreateProcess win32-error=0/f ; : escape-argument ( str -- newstr ) - [ [ dup CHAR: " = [ CHAR: \\ , ] when , ] each ] "" make ; + CHAR: \s over member? [ "\"" swap "\"" 3append ] when ; : join-arguments ( args -- cmd-line ) - " " join ; + [ escape-argument ] map " " join ; : app-name/cmd-line ( -- app-name cmd-line ) +command+ get [ @@ -162,6 +162,10 @@ M: windows-io run-process* ( desc -- handle ) ] with-descriptor ] with-destructors ; +M: windows-io kill-process* ( handle -- ) + PROCESS_INFORMATION-hProcess + 255 TerminateProcess win32-error=0/f ; + : dispose-process ( process-information -- ) #! From MSDN: "Handles in PROCESS_INFORMATION must be closed #! with CloseHandle when they are no longer needed." diff --git a/extra/unix/unix.factor b/extra/unix/unix.factor index f5c484568e..bcfbb3a214 100755 --- a/extra/unix/unix.factor +++ b/extra/unix/unix.factor @@ -168,9 +168,10 @@ FUNCTION: time_t time ( time_t* t ) ; FUNCTION: int unlink ( char* path ) ; FUNCTION: int utimes ( char* path, timeval[2] times ) ; -! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -! wait and waitpid -! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +: SIGKILL 9 ; inline +: SIGTERM 15 ; inline + +FUNCTION: int kill ( pid_t pid, int sig ) ; ! Flags for waitpid diff --git a/extra/windows/kernel32/kernel32.factor b/extra/windows/kernel32/kernel32.factor index 77c7666bfd..b0c2d85598 100755 --- a/extra/windows/kernel32/kernel32.factor +++ b/extra/windows/kernel32/kernel32.factor @@ -1453,7 +1453,7 @@ FUNCTION: DWORD SleepEx ( DWORD dwMilliSeconds, BOOL bAlertable ) ; FUNCTION: BOOL SystemTimeToFileTime ( SYSTEMTIME* lpSystemTime, LPFILETIME lpFileTime ) ; ! FUNCTION: SystemTimeToTzSpecificLocalTime ! FUNCTION: TerminateJobObject -! FUNCTION: TerminateProcess +FUNCTION: BOOL TerminateProcess ( HANDLE hProcess, DWORD uExit ) ; ! FUNCTION: TerminateThread ! FUNCTION: TermsrvAppInstallMode ! FUNCTION: Thread32First From 9d0d371efc1159aa26f5350a305108968aad4a87 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 3 Feb 2008 14:47:44 -0600 Subject: [PATCH 050/269] Minor fix for Windows +stderr+ = +stdout+ --- extra/io/windows/launcher/launcher.factor | 13 ++++++++++++- extra/windows/kernel32/kernel32.factor | 14 +++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/extra/io/windows/launcher/launcher.factor b/extra/io/windows/launcher/launcher.factor index ad84be0825..3d0c2feac1 100755 --- a/extra/io/windows/launcher/launcher.factor +++ b/extra/io/windows/launcher/launcher.factor @@ -118,11 +118,22 @@ TUPLE: CreateProcess-args : inherited-stderr ( args -- handle ) drop STD_ERROR_HANDLE GetStdHandle ; +: duplicate-handle ( handle -- handle ) + GetCurrentProcess + swap + GetCurrentProcess + f [ + 0 + TRUE + DUPLICATE_SAME_ACCESS + DuplicateHandle win32-error=0/f + ] keep *void* ; + : redirect-stderr ( args -- handle ) +stderr+ get dup +stdout+ eq? [ drop - CreateProcess-args-lpStartupInfo + CreateProcess-args-lpStartupInfo duplicate-handle STARTUPINFO-hStdOutput ] [ GENERIC_WRITE CREATE_ALWAYS redirect diff --git a/extra/windows/kernel32/kernel32.factor b/extra/windows/kernel32/kernel32.factor index b0c2d85598..45bd6bfae9 100755 --- a/extra/windows/kernel32/kernel32.factor +++ b/extra/windows/kernel32/kernel32.factor @@ -707,7 +707,19 @@ FUNCTION: BOOL DeleteFileW ( LPCTSTR lpFileName ) ; ! FUNCTION: DosPathToSessionPathA ! FUNCTION: DosPathToSessionPathW ! FUNCTION: DuplicateConsoleHandle -! FUNCTION: DuplicateHandle + +FUNCTION: BOOL DuplicateHandle ( + HANDLE hSourceProcessHandle, + HANDLE hSourceHandle, + HANDLE hTargetProcessHandle, + LPHANDLE lpTargetHandle, + DWORD dwDesiredAccess, + BOOL bInheritHandle, + DWORD dwOptions ) ; + +: DUPLICATE_CLOSE_SOURCE 1 ; +: DUPLICATE_SAME_ACCESS 2 ; + ! FUNCTION: EncodePointer ! FUNCTION: EncodeSystemPointer ! FUNCTION: EndUpdateResourceA From 62bbb0597ee1f9fd621f5eb6b34aa7af4f60e67c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 3 Feb 2008 14:51:35 -0600 Subject: [PATCH 051/269] Fix dodgy memory management --- vm/os-genunix.c | 3 ++- vm/utilities.c | 7 ------- vm/utilities.h | 1 - 3 files changed, 2 insertions(+), 9 deletions(-) mode change 100644 => 100755 vm/os-genunix.c mode change 100644 => 100755 vm/utilities.c mode change 100644 => 100755 vm/utilities.h diff --git a/vm/os-genunix.c b/vm/os-genunix.c old mode 100644 new mode 100755 index 92598eec41..a0bd3e05ae --- a/vm/os-genunix.c +++ b/vm/os-genunix.c @@ -21,7 +21,8 @@ const char *default_image_path(void) if(!path) return "factor.image"; - char *new_path = safe_realloc(path,PATH_MAX + strlen(SUFFIX) + 1); + char *new_path = safe_malloc(PATH_MAX + strlen(SUFFIX) + 1); + memcpy(new_path,path,strlen(path) + 1); strcat(new_path,SUFFIX); return new_path; } diff --git a/vm/utilities.c b/vm/utilities.c old mode 100644 new mode 100755 index 60a4ecb268..ebc8e87977 --- a/vm/utilities.c +++ b/vm/utilities.c @@ -8,13 +8,6 @@ void *safe_malloc(size_t size) return ptr; } -void *safe_realloc(const void *ptr, size_t size) -{ - void *new_ptr = realloc((void *)ptr,size); - if(!new_ptr) fatal_error("Out of memory in safe_realloc", 0); - return new_ptr; -} - F_CHAR *safe_strdup(const F_CHAR *str) { F_CHAR *ptr = STRDUP(str); diff --git a/vm/utilities.h b/vm/utilities.h old mode 100644 new mode 100755 index 483e395345..89a8ba57a3 --- a/vm/utilities.h +++ b/vm/utilities.h @@ -1,3 +1,2 @@ void *safe_malloc(size_t size); -void *safe_realloc(const void *ptr, size_t size); F_CHAR *safe_strdup(const F_CHAR *str); From bb1e06dd8d812db71bb802b0faa9d5fae70b0571 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 3 Feb 2008 15:06:31 -0600 Subject: [PATCH 052/269] add copyright notices update postgresql for new db protocol make unit tests pass --- extra/db/db.factor | 4 + extra/db/postgresql/ffi/ffi.factor | 4 +- extra/db/postgresql/lib/lib.factor | 60 +++------ extra/db/postgresql/postgresql-tests.factor | 128 ++++++++++++++------ extra/db/postgresql/postgresql.factor | 57 +++++---- extra/db/sqlite/lib/lib.factor | 22 +--- extra/db/sqlite/sqlite-tests.factor | 18 +-- extra/db/sqlite/sqlite.factor | 1 - 8 files changed, 161 insertions(+), 133 deletions(-) diff --git a/extra/db/db.factor b/extra/db/db.factor index 81d79eb695..b765924cd6 100644 --- a/extra/db/db.factor +++ b/extra/db/db.factor @@ -44,6 +44,10 @@ GENERIC: #columns ( result-set -- n ) GENERIC# row-column 1 ( result-set n -- obj ) GENERIC: advance-row ( result-set -- ? ) +: init-result-set ( result-set -- ) + dup #rows over set-result-set-max + -1 swap set-result-set-n ; + : ( query handle tuple -- result-set ) >r >r { statement-sql statement-params } get-slots r> { diff --git a/extra/db/postgresql/ffi/ffi.factor b/extra/db/postgresql/ffi/ffi.factor index 368e2fbe77..dbaa70c625 100644 --- a/extra/db/postgresql/ffi/ffi.factor +++ b/extra/db/postgresql/ffi/ffi.factor @@ -1,9 +1,7 @@ ! Copyright (C) 2007 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. - ! adapted from libpq-fe.h version 7.4.7 -! tested on debian linux with postgresql 7.4.7 -! Updated to 8.1 +! tested on debian linux with postgresql 8.1 USING: alien alien.syntax combinators system ; IN: db.postgresql.ffi diff --git a/extra/db/postgresql/lib/lib.factor b/extra/db/postgresql/lib/lib.factor index 4b362f9931..a940a42ae4 100644 --- a/extra/db/postgresql/lib/lib.factor +++ b/extra/db/postgresql/lib/lib.factor @@ -1,13 +1,9 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. USING: arrays continuations db io kernel math namespaces -quotations sequences db.postgresql.ffi ; +quotations sequences db.postgresql.ffi alien alien.c-types ; IN: db.postgresql.lib -SYMBOL: query-res - -: connect-postgres ( host port pgopts pgtty db user pass -- conn ) - PQsetdbLogin - dup PQstatus zero? [ "couldn't connect to database" throw ] unless ; - : postgresql-result-error-message ( res -- str/f ) dup zero? [ drop f @@ -28,45 +24,21 @@ SYMBOL: query-res PQresultStatus PGRES_COMMAND_OK PGRES_TUPLES_OK 2array member? ; +: connect-postgres ( host port pgopts pgtty db user pass -- conn ) + PQsetdbLogin + dup PQstatus zero? [ postgresql-error-message throw ] unless ; + : do-postgresql-statement ( statement -- res ) db get db-handle swap statement-sql PQexec dup postgresql-result-ok? [ dup postgresql-result-error-message swap PQclear throw ] unless ; -! : do-command ( str -- ) - ! 1quotation \ (do-command) add db get swap call ; - -! : prepare ( str quot word -- conn quot ) - ! rot 1quotation swap append swap append db get swap ; - -! : do-query ( str quot -- ) - ! [ (do-query) query-res set ] prepare catch - ! [ rethrow ] [ query-res get PQclear ] if* ; - -! : result>seq ( -- seq ) - ! query-res get [ PQnfields ] keep PQntuples - ! [ swap [ query-res get -rot PQgetvalue ] with map ] with map ; -! -! : print-table ( seq -- ) - ! [ [ write bl ] each "\n" write ] each ; - - - -! select * from animal where name = 'Simba' -! select * from animal where name = $1 - -! : (do-query) ( PGconn query -- PGresult* ) - ! ! For queries that do not return rows, PQexec() returns PGRES_COMMAND_OK - ! ! For queries that return rows, PQexec() returns PGRES_TUPLES_OK - ! PQexec dup postgresql-result-ok? [ - ! dup postgresql-error-message swap PQclear throw - ! ] unless ; - -! : (do-command) ( PGconn query -- PGresult* ) - ! [ (do-query) ] catch - ! [ - ! swap - ! "non-fatal error: " print - ! "\tQuery: " write "'" write write "'" print - ! "\t" write print - ! ] when* drop ; +: do-postgresql-bound-statement ( statement -- res ) + >r db get db-handle r> + [ statement-sql ] keep + [ statement-params length f ] keep + statement-params [ malloc-char-string ] map >c-void*-array + f f 0 PQexecParams + dup postgresql-result-ok? [ + dup postgresql-result-error-message swap PQclear throw + ] unless ; diff --git a/extra/db/postgresql/postgresql-tests.factor b/extra/db/postgresql/postgresql-tests.factor index 438a80e2d8..c5a5155d12 100644 --- a/extra/db/postgresql/postgresql-tests.factor +++ b/extra/db/postgresql/postgresql-tests.factor @@ -2,53 +2,109 @@ ! Set username and password in the 'connect' word. USING: kernel db.postgresql alien continuations io prettyprint -sequences namespaces tools.test ; +sequences namespaces tools.test db ; IN: temporary -: test-connection ( host port pgopts pgtty db user pass -- bool ) - [ [ ] with-postgres ] catch "Error connecting!" "Connected!" ? print ; +IN: scratchpad +: test-db ( -- postgresql-db ) + "localhost" "postgres" "" "factor-test" ; +IN: temporary -[ ] [ "localhost" "" "" "" "factor-test" "postgres" "" test-connection ] unit-test +[ ] [ test-db [ ] with-db ] unit-test -[ ] [ "localhost" "postgres" "" "factor-test" [ ] with-db ] unit-test +[ ] [ + test-db [ + [ "drop table person;" sql-command ] catch drop + "create table person (name varchar(30), country varchar(30));" + sql-command -! just a basic demo + "insert into person values('John', 'America');" sql-command + "insert into person values('Jane', 'New Zealand');" sql-command + ] with-db +] unit-test -"localhost" "postgres" "" "factor-test" [ - [ ] [ "drop table animal" do-command ] unit-test +[ + { + { "John" "America" } + { "Jane" "New Zealand" } + } +] [ + test-db [ + "select * from person" sql-query + ] with-db +] unit-test - [ ] [ "create table animal (id serial not null primary key, species varchar(256), name varchar(256), age integer)" do-command ] unit-test - - [ ] [ "insert into animal (species, name, age) values ('lion', 'Mufasa', 5)" - do-command ] unit-test +[ + { { "John" "America" } } +] [ + test-db [ + "select * from person where name = $1 and country = $2" + [ + { "Jane" "New Zealand" } + over do-bound-query - [ ] [ "select * from animal where name = 'Mufasa'" [ ] do-query ] unit-test - [ ] [ "select * from animal where name = 'Mufasa'" [ - result>seq length 1 = [ - "...there can only be one Mufasa..." throw - ] unless - ] do-query - ] unit-test + { { "Jane" "New Zealand" } } = + [ "test fails" throw ] unless - [ ] [ "insert into animal (species, name, age) values ('lion', 'Simba', 1)" - do-command ] unit-test + { "John" "America" } + swap do-bound-query + ] with-disposal + ] with-db +] unit-test - [ ] [ - "select * from animal" +[ + { + { "John" "America" } + { "Jane" "New Zealand" } + } +] [ test-db [ "select * from person" sql-query ] with-db ] unit-test + +[ +] [ + test-db [ + "insert into person(name, country) values('Jimmy', 'Canada')" + sql-command + ] with-db +] unit-test + +[ + { + { "John" "America" } + { "Jane" "New Zealand" } + { "Jimmy" "Canada" } + } +] [ test-db [ "select * from person" sql-query ] with-db ] unit-test + +[ + test-db [ [ - "Animal table:" print - result>seq print-table - ] do-query - ] unit-test + "insert into person(name, country) values('Jose', 'Mexico')" sql-command + "insert into person(name, country) values('Jose', 'Mexico')" sql-command + "oops" throw + ] with-transaction + ] with-db +] unit-test-fails - ! intentional errors - ! [ "select asdf from animal" - ! [ ] do-query ] catch [ "caught: " write print ] when* - ! "select asdf from animal" [ ] do-query - ! "aofijweafew" do-command -] with-db +[ 3 ] [ + test-db [ + "select * from person" sql-query length + ] with-db +] unit-test +[ +] [ + test-db [ + [ + "insert into person(name, country) values('Jose', 'Mexico')" + sql-command + "insert into person(name, country) values('Jose', 'Mexico')" + sql-command + ] with-transaction + ] with-db +] unit-test -"localhost" "postgres" "" "factor-test" [ - [ ] [ "drop table animal" do-command ] unit-test -] with-db +[ 5 ] [ + test-db [ + "select * from person" sql-query length + ] with-db +] unit-test diff --git a/extra/db/postgresql/postgresql.factor b/extra/db/postgresql/postgresql.factor index 2ea1b3a1dc..df778cc80d 100644 --- a/extra/db/postgresql/postgresql.factor +++ b/extra/db/postgresql/postgresql.factor @@ -1,8 +1,5 @@ -! Copyright (C) 2007 Doug Coleman. +! Copyright (C) 2007, 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -! adapted from libpq-fe.h version 7.4.7 -! tested on debian linux with postgresql 7.4.7 - USING: arrays assocs alien alien.syntax continuations io kernel math namespaces prettyprint quotations sequences debugger db db.postgresql.lib db.postgresql.ffi ; @@ -10,6 +7,7 @@ IN: db.postgresql TUPLE: postgresql-db host port pgopts pgtty db user pass ; TUPLE: postgresql-statement ; +TUPLE: postgresql-result-set ; : ( statement -- postgresql-statement ) postgresql-statement construct-delegate ; @@ -38,31 +36,39 @@ M: postgresql-db dispose ( db -- ) : with-postgresql ( host ust pass db quot -- ) >r r> with-disposal ; +M: postgresql-statement bind-statement* ( seq statement -- ) + set-statement-params ; -M: postgresql-result-set #rows ( statement -- n ) - statement-handle PQntuples ; +M: postgresql-statement rebind-statement ( seq statement -- ) + bind-statement* ; -M: postgresql-result-set #columns ( statement -- n ) - statement-handle PQnfields ; +M: postgresql-result-set #rows ( result-set -- n ) + result-set-handle PQntuples ; -M: postgresql-result-set row-column ( statement n -- obj ) - >r dup statement-handle swap statement-n r> PQgetvalue ; +M: postgresql-result-set #columns ( result-set -- n ) + result-set-handle PQnfields ; +M: postgresql-result-set row-column ( result-set n -- obj ) + >r dup result-set-handle swap result-set-n r> PQgetvalue ; -: init-result-set ( result-set -- ) - dup result-set-max [ - dup do-postgresql-statement over set-result-set-handle - dup #rows over set-result-set-max - -1 over set-result-set-n - ] unless drop ; +M: postgresql-statement execute-statement ( statement -- ) + query-results dispose ; : increment-n ( result-set -- n ) dup result-set-n 1+ dup rot set-result-set-n ; -M: postgresql-result-set advance-row ( result-set -- ? ) - dup init-result-set - dup increment-n swap result-set-max >= ; +M: postgresql-statement query-results ( query -- result-set ) + dup statement-params [ + over [ bind-statement ] keep + do-postgresql-bound-statement + ] [ + dup do-postgresql-statement + ] if* + postgresql-result-set + dup init-result-set ; +M: postgresql-result-set advance-row ( result-set -- ? ) + dup increment-n swap result-set-max >= ; M: postgresql-statement dispose ( query -- ) dup statement-handle PQclear @@ -71,14 +77,14 @@ M: postgresql-statement dispose ( query -- ) M: postgresql-result-set dispose ( result-set -- ) dup result-set-handle PQclear 0 0 f roll { - set-statement-n set-statement-max set-statement-handle + set-result-set-n set-result-set-max set-result-set-handle } set-slots ; M: postgresql-statement prepare-statement ( statement -- ) [ >r db get db-handle "" r> dup statement-sql swap statement-params - dup assoc-size swap PQprepare postgresql-error + length f PQprepare postgresql-error ] keep set-statement-handle ; M: postgresql-db ( sql -- statement ) @@ -88,3 +94,12 @@ M: postgresql-db ( sql -- statement ) M: postgresql-db ( sql -- statement ) { set-statement-sql } statement construct ; + +M: postgresql-db begin-transaction ( -- ) + "BEGIN" sql-command ; + +M: postgresql-db commit-transaction ( -- ) + "COMMIT" sql-command ; + +M: postgresql-db rollback-transaction ( -- ) + "ROLLBACK" sql-command ; diff --git a/extra/db/sqlite/lib/lib.factor b/extra/db/sqlite/lib/lib.factor index 4e4f2ca508..e5f8425d92 100644 --- a/extra/db/sqlite/lib/lib.factor +++ b/extra/db/sqlite/lib/lib.factor @@ -1,3 +1,5 @@ +! Copyright (C) 2008 Chris Double, Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. USING: alien.c-types assocs kernel math math.parser sequences db.sqlite.ffi ; IN: db.sqlite.lib @@ -65,7 +67,6 @@ TUPLE: sqlite-error n message ; ! SQLITE_BLOB 4 ! SQLITE_NULL 5 - : step-complete? ( step-result -- bool ) dup SQLITE_ROW = [ drop f @@ -82,22 +83,3 @@ TUPLE: sqlite-error n message ; : sqlite-next ( prepared -- ? ) sqlite3_step step-complete? ; - -: sqlite-each ( statement quot -- ) - over sqlite3_step step-complete? [ - 2drop - ] [ - [ call ] 2keep sqlite-each - ] if ; inline - -DEFER: (sqlite-map) - -: (sqlite-map) ( statement quot seq -- ) - pick sqlite3_step step-complete? [ - 2nip - ] [ - >r 2dup call r> swap add (sqlite-map) - ] if ; - -: sqlite-map ( statement quot -- seq ) - { } (sqlite-map) ; diff --git a/extra/db/sqlite/sqlite-tests.factor b/extra/db/sqlite/sqlite-tests.factor index ef1bbfc262..f64b8d1104 100644 --- a/extra/db/sqlite/sqlite-tests.factor +++ b/extra/db/sqlite/sqlite-tests.factor @@ -5,12 +5,14 @@ IN: temporary ! "sqlite3 -init test.txt test.db" +IN: scratchpad : test.db "extra/db/sqlite/test.db" resource-path ; +IN: temporary : (create-db) ( -- str ) [ "sqlite3 -init " % - "extra/db/sqlite/test.txt" resource-path % + test.db % " " % test.db % ] "" make ; @@ -27,7 +29,7 @@ IN: temporary { "Jane" "New Zealand" } } ] [ - "extra/db/sqlite/test.db" resource-path [ + test.db [ "select * from person" sql-query ] with-sqlite ] unit-test @@ -35,7 +37,7 @@ IN: temporary [ { { "John" "America" } } ] [ - "extra/db/sqlite/test.db" resource-path [ + test.db [ "select * from person where name = :name and country = :country" [ { { ":name" "Jane" } { ":country" "New Zealand" } } @@ -59,7 +61,7 @@ IN: temporary [ ] [ - "extra/db/sqlite/test.db" resource-path [ + test.db [ "insert into person(name, country) values('Jimmy', 'Canada')" sql-command ] with-sqlite @@ -74,7 +76,7 @@ IN: temporary ] [ test.db [ "select rowid, * from person" sql-query ] with-sqlite ] unit-test [ - "extra/db/sqlite/test.db" resource-path [ + test.db [ [ "insert into person(name, country) values('Jose', 'Mexico')" sql-command "insert into person(name, country) values('Jose', 'Mexico')" sql-command @@ -84,14 +86,14 @@ IN: temporary ] unit-test-fails [ 3 ] [ - "extra/db/sqlite/test.db" resource-path [ + test.db [ "select * from person" sql-query length ] with-sqlite ] unit-test [ ] [ - "extra/db/sqlite/test.db" resource-path [ + test.db [ [ "insert into person(name, country) values('Jose', 'Mexico')" sql-command @@ -102,7 +104,7 @@ IN: temporary ] unit-test [ 5 ] [ - "extra/db/sqlite/test.db" resource-path [ + test.db [ "select * from person" sql-query length ] with-sqlite ] unit-test diff --git a/extra/db/sqlite/sqlite.factor b/extra/db/sqlite/sqlite.factor index 8352d2e11f..49462dcc50 100644 --- a/extra/db/sqlite/sqlite.factor +++ b/extra/db/sqlite/sqlite.factor @@ -64,7 +64,6 @@ M: sqlite-result-set advance-row ( result-set -- handle ? ) M: sqlite-statement query-results ( query -- result-set ) dup statement-handle sqlite-result-set ; - M: sqlite-db begin-transaction ( -- ) "BEGIN" sql-command ; From bae79b80e32cc2658dbd7c0f804f5c9ae0f2ec95 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 3 Feb 2008 15:14:48 -0600 Subject: [PATCH 053/269] Undo handle duplication --- extra/io/windows/launcher/launcher.factor | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/extra/io/windows/launcher/launcher.factor b/extra/io/windows/launcher/launcher.factor index 3d0c2feac1..f3f78fbb88 100755 --- a/extra/io/windows/launcher/launcher.factor +++ b/extra/io/windows/launcher/launcher.factor @@ -118,23 +118,11 @@ TUPLE: CreateProcess-args : inherited-stderr ( args -- handle ) drop STD_ERROR_HANDLE GetStdHandle ; -: duplicate-handle ( handle -- handle ) - GetCurrentProcess - swap - GetCurrentProcess - f [ - 0 - TRUE - DUPLICATE_SAME_ACCESS - DuplicateHandle win32-error=0/f - ] keep *void* ; - : redirect-stderr ( args -- handle ) +stderr+ get dup +stdout+ eq? [ drop - CreateProcess-args-lpStartupInfo duplicate-handle - STARTUPINFO-hStdOutput + CreateProcess-args-lpStartupInfo STARTUPINFO-hStdOutput ] [ GENERIC_WRITE CREATE_ALWAYS redirect swap inherited-stderr ?closed From 4a0bb3b03278b0e4348875d4778c2ca6a82cf6d8 Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Sun, 3 Feb 2008 16:15:03 -0500 Subject: [PATCH 054/269] Solution to Project Euler problem 41 --- extra/project-euler/041/041.factor | 40 ++++++++++++++++++++++++ extra/project-euler/project-euler.factor | 4 +-- 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 extra/project-euler/041/041.factor diff --git a/extra/project-euler/041/041.factor b/extra/project-euler/041/041.factor new file mode 100644 index 0000000000..60017f39a1 --- /dev/null +++ b/extra/project-euler/041/041.factor @@ -0,0 +1,40 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel math.combinatorics math.parser math.primes sequences ; +IN: project-euler.041 + +! http://projecteuler.net/index.php?section=problems&id=41 + +! DESCRIPTION +! ----------- + +! We shall say that an n-digit number is pandigital if it makes use of all the +! digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is +! also prime. + +! What is the largest n-digit pandigital prime that exists? + + +! SOLUTION +! -------- + +! Check 7-digit pandigitals because if the sum of the digits in any number add +! up to a multiple of three, then it is a multiple of three and can't be prime. +! I assumed there would be a 7-digit answer, but technically a higher 4-digit +! pandigital than the one given in the description was also possible. + +! 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45 +! 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36 +! 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28 *** not divisible by 3 *** +! 1 + 2 + 3 + 4 + 5 + 6 = 21 +! 1 + 2 + 3 + 4 + 5 = 15 +! 1 + 2 + 3 + 4 = 10 *** not divisible by 3 *** + +: euler041 ( -- answer ) + { 7 6 5 4 3 2 1 } all-permutations + [ 10 swap digits>integer ] map [ prime? ] find nip ; + +! [ euler041 ] 100 ave-time +! 107 ms run / 7 ms GC ave time - 100 trials + +MAIN: euler041 diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index d89453eb14..3433fe7154 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -12,8 +12,8 @@ USING: definitions io io.files kernel math math.parser project-euler.ave-time project-euler.029 project-euler.030 project-euler.031 project-euler.032 project-euler.033 project-euler.034 project-euler.035 project-euler.036 project-euler.037 project-euler.038 project-euler.039 project-euler.040 - project-euler.048 project-euler.067 project-euler.075 project-euler.134 - project-euler.169 project-euler.173 project-euler.175 ; + project-euler.041 project-euler.048 project-euler.067 project-euler.075 + project-euler.134 project-euler.169 project-euler.173 project-euler.175 ; IN: project-euler Date: Sun, 3 Feb 2008 15:55:59 -0600 Subject: [PATCH 055/269] Fix Unix launcher --- extra/io/unix/launcher/launcher.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/io/unix/launcher/launcher.factor b/extra/io/unix/launcher/launcher.factor index 030583dbe8..b44ac80159 100755 --- a/extra/io/unix/launcher/launcher.factor +++ b/extra/io/unix/launcher/launcher.factor @@ -57,8 +57,8 @@ MEMO: 'arguments' ( -- parser ) : setup-redirection ( -- ) +stdin+ get read-flags 0 redirect +stdout+ get write-flags 1 redirect - +stderr+ get dup +stdout+ get eq? - [ 1 2 dup2 ] [ write-flags 2 redirect ] if ; + +stderr+ get dup +stdout+ eq? + [ drop 1 2 dup2 io-error ] [ write-flags 2 redirect ] if ; : spawn-process ( -- ) [ From 793c3ceb1f627d578fa510f272786cb3e10cc70f Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 3 Feb 2008 16:06:57 -0600 Subject: [PATCH 056/269] byte-length for bit-arrays --- core/alien/c-types/c-types.factor | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/alien/c-types/c-types.factor b/core/alien/c-types/c-types.factor index 8ab703eb7e..1f0f6b121e 100755 --- a/core/alien/c-types/c-types.factor +++ b/core/alien/c-types/c-types.factor @@ -1,9 +1,10 @@ -! Copyright (C) 2004, 2007 Slava Pestov. +! Copyright (C) 2004, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: byte-arrays float-arrays arrays generator.registers assocs -kernel kernel.private libc math namespaces parser sequences -strings words assocs splitting math.parser cpu.architecture -alien alien.accessors quotations system compiler.units ; +USING: bit-arrays byte-arrays float-arrays arrays +generator.registers assocs kernel kernel.private libc math +namespaces parser sequences strings words assocs splitting +math.parser cpu.architecture alien alien.accessors quotations +system compiler.units ; IN: alien.c-types TUPLE: c-type @@ -109,10 +110,12 @@ M: c-type stack-size c-type-size ; GENERIC: byte-length ( seq -- n ) flushable -M: float-array byte-length length "double" heap-size * ; +M: bit-array byte-length length 7 + -3 shift ; M: byte-array byte-length length ; +M: float-array byte-length length "double" heap-size * ; + : c-getter ( name -- quot ) c-type c-type-getter [ [ "Cannot read struct fields with type" throw ] From d6185e224ad77ec30490086c101536bcdd4eed7e Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 3 Feb 2008 16:13:57 -0600 Subject: [PATCH 057/269] Undo funny stuff --- extra/http/server/responders/responders.factor | 4 ++-- extra/unicode/case/case.factor | 9 --------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/extra/http/server/responders/responders.factor b/extra/http/server/responders/responders.factor index 6df52997e1..70503236f6 100644 --- a/extra/http/server/responders/responders.factor +++ b/extra/http/server/responders/responders.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: arrays assocs hashtables html html.elements splitting http io kernel math math.parser namespaces parser sequences -strings io.server vectors assocs.lib unicode.case ; +strings io.server vectors assocs.lib ; IN: http.server.responders @@ -14,7 +14,7 @@ SYMBOL: responders H{ } clone [ insert-at ] keep ; : print-header ( alist -- ) - [ swap >Upper-dashes write ": " write print ] multi-assoc-each nl ; + [ swap write ": " write print ] multi-assoc-each nl ; : response ( msg -- ) "HTTP/1.0 " write print ; diff --git a/extra/unicode/case/case.factor b/extra/unicode/case/case.factor index f244192a32..8129ec17f8 100755 --- a/extra/unicode/case/case.factor +++ b/extra/unicode/case/case.factor @@ -110,12 +110,3 @@ SYMBOL: locale ! Just casing locale, or overall? dup >title = ; : case-fold? ( string -- ? ) dup >case-fold = ; - - -: >Upper ( str -- str ) - dup empty? [ - unclip ch>upper 1string swap append - ] unless ; - -: >Upper-dashes ( str -- str ) - "-" split [ >Upper ] map "-" join ; From 170d8d8c51142f936a6ce6d2e39f9b0e9b965070 Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Sun, 3 Feb 2008 17:18:10 -0500 Subject: [PATCH 058/269] Fix common Project Euler word alpha-num --- extra/project-euler/022/022.factor | 10 +++------- extra/project-euler/common/common.factor | 6 +++++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/extra/project-euler/022/022.factor b/extra/project-euler/022/022.factor index e9b0b5fbcf..9c8866b736 100644 --- a/extra/project-euler/022/022.factor +++ b/extra/project-euler/022/022.factor @@ -1,7 +1,6 @@ ! Copyright (c) 2007 Aaron Schaefer. ! See http://factorcode.org/license.txt for BSD license. -USING: io.files kernel math math.parser namespaces sequences sorting splitting - strings system vocabs ascii ; +USING: ascii io.files kernel math project-euler.common sequences sorting splitting ; IN: project-euler.022 ! http://projecteuler.net/index.php?section=problems&id=22 @@ -31,9 +30,6 @@ IN: project-euler.022 "extra/project-euler/022/names.txt" resource-path file-contents [ quotable? ] subset "," split ; -: alpha-value ( str -- n ) - [ string>digits sum ] keep length 9 * - ; - : name-scores ( seq -- seq ) dup length [ 1+ swap alpha-value * ] 2map ; @@ -43,9 +39,9 @@ PRIVATE> source-022 natural-sort name-scores sum ; ! [ euler022 ] 100 ave-time -! 59 ms run / 1 ms GC ave time - 100 trials +! 123 ms run / 4 ms GC ave time - 100 trials ! source-022 [ natural-sort name-scores sum ] curry 100 ave-time -! 45 ms run / 1 ms GC ave time - 100 trials +! 93 ms run / 2 ms GC ave time - 100 trials MAIN: euler022 diff --git a/extra/project-euler/common/common.factor b/extra/project-euler/common/common.factor index 50adbe4953..99bb3169c4 100644 --- a/extra/project-euler/common/common.factor +++ b/extra/project-euler/common/common.factor @@ -1,6 +1,6 @@ USING: arrays combinators.lib kernel math math.functions math.miller-rabin math.matrices math.parser math.primes.factors math.ranges namespaces - sequences sorting ; + sequences sorting unicode.case ; IN: project-euler.common ! A collection of words used by more than one Project Euler solution @@ -8,6 +8,7 @@ IN: project-euler.common ! Problems using each public word ! ------------------------------- +! alpha-value - #22, #42 ! cartesian-product - #4, #27, #29, #32, #33 ! collect-consecutive - #8, #11 ! log10 - #25, #134 @@ -52,6 +53,9 @@ IN: project-euler.common PRIVATE> +: alpha-value ( str -- n ) + >lower [ CHAR: a - 1+ ] sigma ; + : cartesian-product ( seq1 seq2 -- seq1xseq2 ) swap [ swap [ 2array ] map-with ] map-with concat ; From e7722c02b75252c9ba8456c1390c0db6b2d98860 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 3 Feb 2008 17:28:57 -0600 Subject: [PATCH 059/269] Add unit test for float alignment --- core/compiler/test/alien.factor | 10 ++++++++++ vm/ffi_test.c | 5 +++++ vm/ffi_test.h | 4 ++++ 3 files changed, 19 insertions(+) mode change 100644 => 100755 vm/ffi_test.c mode change 100644 => 100755 vm/ffi_test.h diff --git a/core/compiler/test/alien.factor b/core/compiler/test/alien.factor index acb9a4a4fa..9416fd1415 100755 --- a/core/compiler/test/alien.factor +++ b/core/compiler/test/alien.factor @@ -270,6 +270,16 @@ FUNCTION: double ffi_test_35 test-struct-11 x int y ; 3 ffi_test_35 ] unit-test +C-STRUCT: test-struct-12 { "int" "a" } { "double" "x" } ; + +: make-struct-12 + "test-struct-12" + [ set-test-struct-12-x ] keep ; + +FUNCTION: double ffi_test_36 ( test-struct-12 x ) ; + +[ 1.23456 ] [ 1.23456 make-struct-12 ffi_test_36 ] unit-test + ! Test callbacks : callback-1 "void" { } "cdecl" [ ] alien-callback ; diff --git a/vm/ffi_test.c b/vm/ffi_test.c old mode 100644 new mode 100755 index f6e70fd6ac..9cec5ccbad --- a/vm/ffi_test.c +++ b/vm/ffi_test.c @@ -245,3 +245,8 @@ double ffi_test_35(struct test_struct_11 x, int y) { return (x.x + x.y) * y; } + +double ffi_test_36(struct test_struct_12 x) +{ + return x.x; +} diff --git a/vm/ffi_test.h b/vm/ffi_test.h old mode 100644 new mode 100755 index 27e402b74f..aac5d32f93 --- a/vm/ffi_test.h +++ b/vm/ffi_test.h @@ -57,3 +57,7 @@ struct test_struct_10 { float x; int y; }; DLLEXPORT double ffi_test_34(struct test_struct_10 x, int y); struct test_struct_11 { int x; int y; }; DLLEXPORT double ffi_test_35(struct test_struct_11 x, int y); + +struct test_struct_12 { int a; double x; }; + +DLLEXPORT double ffi_test_36(struct test_struct_12 x); From c64fe3d07bfe6f91d22dcc586a81a289a82c255f Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Sun, 3 Feb 2008 18:42:45 -0500 Subject: [PATCH 060/269] Solution to Project Euler problem 42 --- extra/project-euler/012/012.factor | 3 - extra/project-euler/022/022.factor | 3 - extra/project-euler/042/042.factor | 74 ++++++++++++++++++++++++ extra/project-euler/042/words.txt | 1 + extra/project-euler/067/067.factor | 3 - extra/project-euler/common/common.factor | 4 ++ extra/project-euler/project-euler.factor | 5 +- 7 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 extra/project-euler/042/042.factor create mode 100644 extra/project-euler/042/words.txt diff --git a/extra/project-euler/012/012.factor b/extra/project-euler/012/012.factor index 3d59549e69..583bad8f72 100644 --- a/extra/project-euler/012/012.factor +++ b/extra/project-euler/012/012.factor @@ -33,9 +33,6 @@ IN: project-euler.012 ! SOLUTION ! -------- -: nth-triangle ( n -- n ) - dup 1+ * 2 / ; - : euler012 ( -- answer ) 8 [ dup nth-triangle tau* 500 < ] [ 1+ ] [ ] while nth-triangle ; diff --git a/extra/project-euler/022/022.factor b/extra/project-euler/022/022.factor index 9c8866b736..5bd1797272 100644 --- a/extra/project-euler/022/022.factor +++ b/extra/project-euler/022/022.factor @@ -41,7 +41,4 @@ PRIVATE> ! [ euler022 ] 100 ave-time ! 123 ms run / 4 ms GC ave time - 100 trials -! source-022 [ natural-sort name-scores sum ] curry 100 ave-time -! 93 ms run / 2 ms GC ave time - 100 trials - MAIN: euler022 diff --git a/extra/project-euler/042/042.factor b/extra/project-euler/042/042.factor new file mode 100644 index 0000000000..3d5f271374 --- /dev/null +++ b/extra/project-euler/042/042.factor @@ -0,0 +1,74 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: ascii combinators.lib io.files kernel math namespaces + project-euler.common sequences splitting ; +IN: project-euler.042 + +! http://projecteuler.net/index.php?section=problems&id=42 + +! DESCRIPTION +! ----------- + +! The nth term of the sequence of triangle numbers is given by, +! tn = n * (n + 1) / 2; so the first ten triangle numbers are: + +! 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... + +! By converting each letter in a word to a number corresponding to its +! alphabetical position and adding these values we form a word value. For +! example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value +! is a triangle number then we shall call the word a triangle word. + +! Using words.txt (right click and 'Save Link/Target As...'), a 16K text file +! containing nearly two-thousand common English words, how many are triangle +! words? + + +! SOLUTION +! -------- + + [ + dup nth-triangle , 1+ (triangle-upto) + ] [ + 2drop + ] if ; + +: triangle-upto ( n -- seq ) + [ 1 (triangle-upto) ] { } make ; + +PRIVATE> + +: euler042 ( -- answer ) + source-042 [ alpha-value ] map dup supremum + triangle-upto [ member? ] curry count ; + +! [ euler042 ] 100 ave-time +! 27 ms run / 1 ms GC ave time - 100 trials + + +! ALTERNATE SOLUTIONS +! ------------------- + +! Use the inverse function of n * (n + 1) / 2 and test if the result is an integer + + + +: euler042a ( -- answer ) + source-042 [ alpha-value ] map [ triangle? ] count ; + +! [ euler042a ] 100 ave-time +! 25 ms run / 1 ms GC ave time - 100 trials + +MAIN: euler042a diff --git a/extra/project-euler/042/words.txt b/extra/project-euler/042/words.txt new file mode 100644 index 0000000000..7177624d41 --- /dev/null +++ b/extra/project-euler/042/words.txt @@ -0,0 +1 @@ +"A","ABILITY","ABLE","ABOUT","ABOVE","ABSENCE","ABSOLUTELY","ACADEMIC","ACCEPT","ACCESS","ACCIDENT","ACCOMPANY","ACCORDING","ACCOUNT","ACHIEVE","ACHIEVEMENT","ACID","ACQUIRE","ACROSS","ACT","ACTION","ACTIVE","ACTIVITY","ACTUAL","ACTUALLY","ADD","ADDITION","ADDITIONAL","ADDRESS","ADMINISTRATION","ADMIT","ADOPT","ADULT","ADVANCE","ADVANTAGE","ADVICE","ADVISE","AFFAIR","AFFECT","AFFORD","AFRAID","AFTER","AFTERNOON","AFTERWARDS","AGAIN","AGAINST","AGE","AGENCY","AGENT","AGO","AGREE","AGREEMENT","AHEAD","AID","AIM","AIR","AIRCRAFT","ALL","ALLOW","ALMOST","ALONE","ALONG","ALREADY","ALRIGHT","ALSO","ALTERNATIVE","ALTHOUGH","ALWAYS","AMONG","AMONGST","AMOUNT","AN","ANALYSIS","ANCIENT","AND","ANIMAL","ANNOUNCE","ANNUAL","ANOTHER","ANSWER","ANY","ANYBODY","ANYONE","ANYTHING","ANYWAY","APART","APPARENT","APPARENTLY","APPEAL","APPEAR","APPEARANCE","APPLICATION","APPLY","APPOINT","APPOINTMENT","APPROACH","APPROPRIATE","APPROVE","AREA","ARGUE","ARGUMENT","ARISE","ARM","ARMY","AROUND","ARRANGE","ARRANGEMENT","ARRIVE","ART","ARTICLE","ARTIST","AS","ASK","ASPECT","ASSEMBLY","ASSESS","ASSESSMENT","ASSET","ASSOCIATE","ASSOCIATION","ASSUME","ASSUMPTION","AT","ATMOSPHERE","ATTACH","ATTACK","ATTEMPT","ATTEND","ATTENTION","ATTITUDE","ATTRACT","ATTRACTIVE","AUDIENCE","AUTHOR","AUTHORITY","AVAILABLE","AVERAGE","AVOID","AWARD","AWARE","AWAY","AYE","BABY","BACK","BACKGROUND","BAD","BAG","BALANCE","BALL","BAND","BANK","BAR","BASE","BASIC","BASIS","BATTLE","BE","BEAR","BEAT","BEAUTIFUL","BECAUSE","BECOME","BED","BEDROOM","BEFORE","BEGIN","BEGINNING","BEHAVIOUR","BEHIND","BELIEF","BELIEVE","BELONG","BELOW","BENEATH","BENEFIT","BESIDE","BEST","BETTER","BETWEEN","BEYOND","BIG","BILL","BIND","BIRD","BIRTH","BIT","BLACK","BLOCK","BLOOD","BLOODY","BLOW","BLUE","BOARD","BOAT","BODY","BONE","BOOK","BORDER","BOTH","BOTTLE","BOTTOM","BOX","BOY","BRAIN","BRANCH","BREAK","BREATH","BRIDGE","BRIEF","BRIGHT","BRING","BROAD","BROTHER","BUDGET","BUILD","BUILDING","BURN","BUS","BUSINESS","BUSY","BUT","BUY","BY","CABINET","CALL","CAMPAIGN","CAN","CANDIDATE","CAPABLE","CAPACITY","CAPITAL","CAR","CARD","CARE","CAREER","CAREFUL","CAREFULLY","CARRY","CASE","CASH","CAT","CATCH","CATEGORY","CAUSE","CELL","CENTRAL","CENTRE","CENTURY","CERTAIN","CERTAINLY","CHAIN","CHAIR","CHAIRMAN","CHALLENGE","CHANCE","CHANGE","CHANNEL","CHAPTER","CHARACTER","CHARACTERISTIC","CHARGE","CHEAP","CHECK","CHEMICAL","CHIEF","CHILD","CHOICE","CHOOSE","CHURCH","CIRCLE","CIRCUMSTANCE","CITIZEN","CITY","CIVIL","CLAIM","CLASS","CLEAN","CLEAR","CLEARLY","CLIENT","CLIMB","CLOSE","CLOSELY","CLOTHES","CLUB","COAL","CODE","COFFEE","COLD","COLLEAGUE","COLLECT","COLLECTION","COLLEGE","COLOUR","COMBINATION","COMBINE","COME","COMMENT","COMMERCIAL","COMMISSION","COMMIT","COMMITMENT","COMMITTEE","COMMON","COMMUNICATION","COMMUNITY","COMPANY","COMPARE","COMPARISON","COMPETITION","COMPLETE","COMPLETELY","COMPLEX","COMPONENT","COMPUTER","CONCENTRATE","CONCENTRATION","CONCEPT","CONCERN","CONCERNED","CONCLUDE","CONCLUSION","CONDITION","CONDUCT","CONFERENCE","CONFIDENCE","CONFIRM","CONFLICT","CONGRESS","CONNECT","CONNECTION","CONSEQUENCE","CONSERVATIVE","CONSIDER","CONSIDERABLE","CONSIDERATION","CONSIST","CONSTANT","CONSTRUCTION","CONSUMER","CONTACT","CONTAIN","CONTENT","CONTEXT","CONTINUE","CONTRACT","CONTRAST","CONTRIBUTE","CONTRIBUTION","CONTROL","CONVENTION","CONVERSATION","COPY","CORNER","CORPORATE","CORRECT","COS","COST","COULD","COUNCIL","COUNT","COUNTRY","COUNTY","COUPLE","COURSE","COURT","COVER","CREATE","CREATION","CREDIT","CRIME","CRIMINAL","CRISIS","CRITERION","CRITICAL","CRITICISM","CROSS","CROWD","CRY","CULTURAL","CULTURE","CUP","CURRENT","CURRENTLY","CURRICULUM","CUSTOMER","CUT","DAMAGE","DANGER","DANGEROUS","DARK","DATA","DATE","DAUGHTER","DAY","DEAD","DEAL","DEATH","DEBATE","DEBT","DECADE","DECIDE","DECISION","DECLARE","DEEP","DEFENCE","DEFENDANT","DEFINE","DEFINITION","DEGREE","DELIVER","DEMAND","DEMOCRATIC","DEMONSTRATE","DENY","DEPARTMENT","DEPEND","DEPUTY","DERIVE","DESCRIBE","DESCRIPTION","DESIGN","DESIRE","DESK","DESPITE","DESTROY","DETAIL","DETAILED","DETERMINE","DEVELOP","DEVELOPMENT","DEVICE","DIE","DIFFERENCE","DIFFERENT","DIFFICULT","DIFFICULTY","DINNER","DIRECT","DIRECTION","DIRECTLY","DIRECTOR","DISAPPEAR","DISCIPLINE","DISCOVER","DISCUSS","DISCUSSION","DISEASE","DISPLAY","DISTANCE","DISTINCTION","DISTRIBUTION","DISTRICT","DIVIDE","DIVISION","DO","DOCTOR","DOCUMENT","DOG","DOMESTIC","DOOR","DOUBLE","DOUBT","DOWN","DRAW","DRAWING","DREAM","DRESS","DRINK","DRIVE","DRIVER","DROP","DRUG","DRY","DUE","DURING","DUTY","EACH","EAR","EARLY","EARN","EARTH","EASILY","EAST","EASY","EAT","ECONOMIC","ECONOMY","EDGE","EDITOR","EDUCATION","EDUCATIONAL","EFFECT","EFFECTIVE","EFFECTIVELY","EFFORT","EGG","EITHER","ELDERLY","ELECTION","ELEMENT","ELSE","ELSEWHERE","EMERGE","EMPHASIS","EMPLOY","EMPLOYEE","EMPLOYER","EMPLOYMENT","EMPTY","ENABLE","ENCOURAGE","END","ENEMY","ENERGY","ENGINE","ENGINEERING","ENJOY","ENOUGH","ENSURE","ENTER","ENTERPRISE","ENTIRE","ENTIRELY","ENTITLE","ENTRY","ENVIRONMENT","ENVIRONMENTAL","EQUAL","EQUALLY","EQUIPMENT","ERROR","ESCAPE","ESPECIALLY","ESSENTIAL","ESTABLISH","ESTABLISHMENT","ESTATE","ESTIMATE","EVEN","EVENING","EVENT","EVENTUALLY","EVER","EVERY","EVERYBODY","EVERYONE","EVERYTHING","EVIDENCE","EXACTLY","EXAMINATION","EXAMINE","EXAMPLE","EXCELLENT","EXCEPT","EXCHANGE","EXECUTIVE","EXERCISE","EXHIBITION","EXIST","EXISTENCE","EXISTING","EXPECT","EXPECTATION","EXPENDITURE","EXPENSE","EXPENSIVE","EXPERIENCE","EXPERIMENT","EXPERT","EXPLAIN","EXPLANATION","EXPLORE","EXPRESS","EXPRESSION","EXTEND","EXTENT","EXTERNAL","EXTRA","EXTREMELY","EYE","FACE","FACILITY","FACT","FACTOR","FACTORY","FAIL","FAILURE","FAIR","FAIRLY","FAITH","FALL","FAMILIAR","FAMILY","FAMOUS","FAR","FARM","FARMER","FASHION","FAST","FATHER","FAVOUR","FEAR","FEATURE","FEE","FEEL","FEELING","FEMALE","FEW","FIELD","FIGHT","FIGURE","FILE","FILL","FILM","FINAL","FINALLY","FINANCE","FINANCIAL","FIND","FINDING","FINE","FINGER","FINISH","FIRE","FIRM","FIRST","FISH","FIT","FIX","FLAT","FLIGHT","FLOOR","FLOW","FLOWER","FLY","FOCUS","FOLLOW","FOLLOWING","FOOD","FOOT","FOOTBALL","FOR","FORCE","FOREIGN","FOREST","FORGET","FORM","FORMAL","FORMER","FORWARD","FOUNDATION","FREE","FREEDOM","FREQUENTLY","FRESH","FRIEND","FROM","FRONT","FRUIT","FUEL","FULL","FULLY","FUNCTION","FUND","FUNNY","FURTHER","FUTURE","GAIN","GAME","GARDEN","GAS","GATE","GATHER","GENERAL","GENERALLY","GENERATE","GENERATION","GENTLEMAN","GET","GIRL","GIVE","GLASS","GO","GOAL","GOD","GOLD","GOOD","GOVERNMENT","GRANT","GREAT","GREEN","GREY","GROUND","GROUP","GROW","GROWING","GROWTH","GUEST","GUIDE","GUN","HAIR","HALF","HALL","HAND","HANDLE","HANG","HAPPEN","HAPPY","HARD","HARDLY","HATE","HAVE","HE","HEAD","HEALTH","HEAR","HEART","HEAT","HEAVY","HELL","HELP","HENCE","HER","HERE","HERSELF","HIDE","HIGH","HIGHLY","HILL","HIM","HIMSELF","HIS","HISTORICAL","HISTORY","HIT","HOLD","HOLE","HOLIDAY","HOME","HOPE","HORSE","HOSPITAL","HOT","HOTEL","HOUR","HOUSE","HOUSEHOLD","HOUSING","HOW","HOWEVER","HUGE","HUMAN","HURT","HUSBAND","I","IDEA","IDENTIFY","IF","IGNORE","ILLUSTRATE","IMAGE","IMAGINE","IMMEDIATE","IMMEDIATELY","IMPACT","IMPLICATION","IMPLY","IMPORTANCE","IMPORTANT","IMPOSE","IMPOSSIBLE","IMPRESSION","IMPROVE","IMPROVEMENT","IN","INCIDENT","INCLUDE","INCLUDING","INCOME","INCREASE","INCREASED","INCREASINGLY","INDEED","INDEPENDENT","INDEX","INDICATE","INDIVIDUAL","INDUSTRIAL","INDUSTRY","INFLUENCE","INFORM","INFORMATION","INITIAL","INITIATIVE","INJURY","INSIDE","INSIST","INSTANCE","INSTEAD","INSTITUTE","INSTITUTION","INSTRUCTION","INSTRUMENT","INSURANCE","INTEND","INTENTION","INTEREST","INTERESTED","INTERESTING","INTERNAL","INTERNATIONAL","INTERPRETATION","INTERVIEW","INTO","INTRODUCE","INTRODUCTION","INVESTIGATE","INVESTIGATION","INVESTMENT","INVITE","INVOLVE","IRON","IS","ISLAND","ISSUE","IT","ITEM","ITS","ITSELF","JOB","JOIN","JOINT","JOURNEY","JUDGE","JUMP","JUST","JUSTICE","KEEP","KEY","KID","KILL","KIND","KING","KITCHEN","KNEE","KNOW","KNOWLEDGE","LABOUR","LACK","LADY","LAND","LANGUAGE","LARGE","LARGELY","LAST","LATE","LATER","LATTER","LAUGH","LAUNCH","LAW","LAWYER","LAY","LEAD","LEADER","LEADERSHIP","LEADING","LEAF","LEAGUE","LEAN","LEARN","LEAST","LEAVE","LEFT","LEG","LEGAL","LEGISLATION","LENGTH","LESS","LET","LETTER","LEVEL","LIABILITY","LIBERAL","LIBRARY","LIE","LIFE","LIFT","LIGHT","LIKE","LIKELY","LIMIT","LIMITED","LINE","LINK","LIP","LIST","LISTEN","LITERATURE","LITTLE","LIVE","LIVING","LOAN","LOCAL","LOCATION","LONG","LOOK","LORD","LOSE","LOSS","LOT","LOVE","LOVELY","LOW","LUNCH","MACHINE","MAGAZINE","MAIN","MAINLY","MAINTAIN","MAJOR","MAJORITY","MAKE","MALE","MAN","MANAGE","MANAGEMENT","MANAGER","MANNER","MANY","MAP","MARK","MARKET","MARRIAGE","MARRIED","MARRY","MASS","MASTER","MATCH","MATERIAL","MATTER","MAY","MAYBE","ME","MEAL","MEAN","MEANING","MEANS","MEANWHILE","MEASURE","MECHANISM","MEDIA","MEDICAL","MEET","MEETING","MEMBER","MEMBERSHIP","MEMORY","MENTAL","MENTION","MERELY","MESSAGE","METAL","METHOD","MIDDLE","MIGHT","MILE","MILITARY","MILK","MIND","MINE","MINISTER","MINISTRY","MINUTE","MISS","MISTAKE","MODEL","MODERN","MODULE","MOMENT","MONEY","MONTH","MORE","MORNING","MOST","MOTHER","MOTION","MOTOR","MOUNTAIN","MOUTH","MOVE","MOVEMENT","MUCH","MURDER","MUSEUM","MUSIC","MUST","MY","MYSELF","NAME","NARROW","NATION","NATIONAL","NATURAL","NATURE","NEAR","NEARLY","NECESSARILY","NECESSARY","NECK","NEED","NEGOTIATION","NEIGHBOUR","NEITHER","NETWORK","NEVER","NEVERTHELESS","NEW","NEWS","NEWSPAPER","NEXT","NICE","NIGHT","NO","NOBODY","NOD","NOISE","NONE","NOR","NORMAL","NORMALLY","NORTH","NORTHERN","NOSE","NOT","NOTE","NOTHING","NOTICE","NOTION","NOW","NUCLEAR","NUMBER","NURSE","OBJECT","OBJECTIVE","OBSERVATION","OBSERVE","OBTAIN","OBVIOUS","OBVIOUSLY","OCCASION","OCCUR","ODD","OF","OFF","OFFENCE","OFFER","OFFICE","OFFICER","OFFICIAL","OFTEN","OIL","OKAY","OLD","ON","ONCE","ONE","ONLY","ONTO","OPEN","OPERATE","OPERATION","OPINION","OPPORTUNITY","OPPOSITION","OPTION","OR","ORDER","ORDINARY","ORGANISATION","ORGANISE","ORGANIZATION","ORIGIN","ORIGINAL","OTHER","OTHERWISE","OUGHT","OUR","OURSELVES","OUT","OUTCOME","OUTPUT","OUTSIDE","OVER","OVERALL","OWN","OWNER","PACKAGE","PAGE","PAIN","PAINT","PAINTING","PAIR","PANEL","PAPER","PARENT","PARK","PARLIAMENT","PART","PARTICULAR","PARTICULARLY","PARTLY","PARTNER","PARTY","PASS","PASSAGE","PAST","PATH","PATIENT","PATTERN","PAY","PAYMENT","PEACE","PENSION","PEOPLE","PER","PERCENT","PERFECT","PERFORM","PERFORMANCE","PERHAPS","PERIOD","PERMANENT","PERSON","PERSONAL","PERSUADE","PHASE","PHONE","PHOTOGRAPH","PHYSICAL","PICK","PICTURE","PIECE","PLACE","PLAN","PLANNING","PLANT","PLASTIC","PLATE","PLAY","PLAYER","PLEASE","PLEASURE","PLENTY","PLUS","POCKET","POINT","POLICE","POLICY","POLITICAL","POLITICS","POOL","POOR","POPULAR","POPULATION","POSITION","POSITIVE","POSSIBILITY","POSSIBLE","POSSIBLY","POST","POTENTIAL","POUND","POWER","POWERFUL","PRACTICAL","PRACTICE","PREFER","PREPARE","PRESENCE","PRESENT","PRESIDENT","PRESS","PRESSURE","PRETTY","PREVENT","PREVIOUS","PREVIOUSLY","PRICE","PRIMARY","PRIME","PRINCIPLE","PRIORITY","PRISON","PRISONER","PRIVATE","PROBABLY","PROBLEM","PROCEDURE","PROCESS","PRODUCE","PRODUCT","PRODUCTION","PROFESSIONAL","PROFIT","PROGRAM","PROGRAMME","PROGRESS","PROJECT","PROMISE","PROMOTE","PROPER","PROPERLY","PROPERTY","PROPORTION","PROPOSE","PROPOSAL","PROSPECT","PROTECT","PROTECTION","PROVE","PROVIDE","PROVIDED","PROVISION","PUB","PUBLIC","PUBLICATION","PUBLISH","PULL","PUPIL","PURPOSE","PUSH","PUT","QUALITY","QUARTER","QUESTION","QUICK","QUICKLY","QUIET","QUITE","RACE","RADIO","RAILWAY","RAIN","RAISE","RANGE","RAPIDLY","RARE","RATE","RATHER","REACH","REACTION","READ","READER","READING","READY","REAL","REALISE","REALITY","REALIZE","REALLY","REASON","REASONABLE","RECALL","RECEIVE","RECENT","RECENTLY","RECOGNISE","RECOGNITION","RECOGNIZE","RECOMMEND","RECORD","RECOVER","RED","REDUCE","REDUCTION","REFER","REFERENCE","REFLECT","REFORM","REFUSE","REGARD","REGION","REGIONAL","REGULAR","REGULATION","REJECT","RELATE","RELATION","RELATIONSHIP","RELATIVE","RELATIVELY","RELEASE","RELEVANT","RELIEF","RELIGION","RELIGIOUS","RELY","REMAIN","REMEMBER","REMIND","REMOVE","REPEAT","REPLACE","REPLY","REPORT","REPRESENT","REPRESENTATION","REPRESENTATIVE","REQUEST","REQUIRE","REQUIREMENT","RESEARCH","RESOURCE","RESPECT","RESPOND","RESPONSE","RESPONSIBILITY","RESPONSIBLE","REST","RESTAURANT","RESULT","RETAIN","RETURN","REVEAL","REVENUE","REVIEW","REVOLUTION","RICH","RIDE","RIGHT","RING","RISE","RISK","RIVER","ROAD","ROCK","ROLE","ROLL","ROOF","ROOM","ROUND","ROUTE","ROW","ROYAL","RULE","RUN","RURAL","SAFE","SAFETY","SALE","SAME","SAMPLE","SATISFY","SAVE","SAY","SCALE","SCENE","SCHEME","SCHOOL","SCIENCE","SCIENTIFIC","SCIENTIST","SCORE","SCREEN","SEA","SEARCH","SEASON","SEAT","SECOND","SECONDARY","SECRETARY","SECTION","SECTOR","SECURE","SECURITY","SEE","SEEK","SEEM","SELECT","SELECTION","SELL","SEND","SENIOR","SENSE","SENTENCE","SEPARATE","SEQUENCE","SERIES","SERIOUS","SERIOUSLY","SERVANT","SERVE","SERVICE","SESSION","SET","SETTLE","SETTLEMENT","SEVERAL","SEVERE","SEX","SEXUAL","SHAKE","SHALL","SHAPE","SHARE","SHE","SHEET","SHIP","SHOE","SHOOT","SHOP","SHORT","SHOT","SHOULD","SHOULDER","SHOUT","SHOW","SHUT","SIDE","SIGHT","SIGN","SIGNAL","SIGNIFICANCE","SIGNIFICANT","SILENCE","SIMILAR","SIMPLE","SIMPLY","SINCE","SING","SINGLE","SIR","SISTER","SIT","SITE","SITUATION","SIZE","SKILL","SKIN","SKY","SLEEP","SLIGHTLY","SLIP","SLOW","SLOWLY","SMALL","SMILE","SO","SOCIAL","SOCIETY","SOFT","SOFTWARE","SOIL","SOLDIER","SOLICITOR","SOLUTION","SOME","SOMEBODY","SOMEONE","SOMETHING","SOMETIMES","SOMEWHAT","SOMEWHERE","SON","SONG","SOON","SORRY","SORT","SOUND","SOURCE","SOUTH","SOUTHERN","SPACE","SPEAK","SPEAKER","SPECIAL","SPECIES","SPECIFIC","SPEECH","SPEED","SPEND","SPIRIT","SPORT","SPOT","SPREAD","SPRING","STAFF","STAGE","STAND","STANDARD","STAR","START","STATE","STATEMENT","STATION","STATUS","STAY","STEAL","STEP","STICK","STILL","STOCK","STONE","STOP","STORE","STORY","STRAIGHT","STRANGE","STRATEGY","STREET","STRENGTH","STRIKE","STRONG","STRONGLY","STRUCTURE","STUDENT","STUDIO","STUDY","STUFF","STYLE","SUBJECT","SUBSTANTIAL","SUCCEED","SUCCESS","SUCCESSFUL","SUCH","SUDDENLY","SUFFER","SUFFICIENT","SUGGEST","SUGGESTION","SUITABLE","SUM","SUMMER","SUN","SUPPLY","SUPPORT","SUPPOSE","SURE","SURELY","SURFACE","SURPRISE","SURROUND","SURVEY","SURVIVE","SWITCH","SYSTEM","TABLE","TAKE","TALK","TALL","TAPE","TARGET","TASK","TAX","TEA","TEACH","TEACHER","TEACHING","TEAM","TEAR","TECHNICAL","TECHNIQUE","TECHNOLOGY","TELEPHONE","TELEVISION","TELL","TEMPERATURE","TEND","TERM","TERMS","TERRIBLE","TEST","TEXT","THAN","THANK","THANKS","THAT","THE","THEATRE","THEIR","THEM","THEME","THEMSELVES","THEN","THEORY","THERE","THEREFORE","THESE","THEY","THIN","THING","THINK","THIS","THOSE","THOUGH","THOUGHT","THREAT","THREATEN","THROUGH","THROUGHOUT","THROW","THUS","TICKET","TIME","TINY","TITLE","TO","TODAY","TOGETHER","TOMORROW","TONE","TONIGHT","TOO","TOOL","TOOTH","TOP","TOTAL","TOTALLY","TOUCH","TOUR","TOWARDS","TOWN","TRACK","TRADE","TRADITION","TRADITIONAL","TRAFFIC","TRAIN","TRAINING","TRANSFER","TRANSPORT","TRAVEL","TREAT","TREATMENT","TREATY","TREE","TREND","TRIAL","TRIP","TROOP","TROUBLE","TRUE","TRUST","TRUTH","TRY","TURN","TWICE","TYPE","TYPICAL","UNABLE","UNDER","UNDERSTAND","UNDERSTANDING","UNDERTAKE","UNEMPLOYMENT","UNFORTUNATELY","UNION","UNIT","UNITED","UNIVERSITY","UNLESS","UNLIKELY","UNTIL","UP","UPON","UPPER","URBAN","US","USE","USED","USEFUL","USER","USUAL","USUALLY","VALUE","VARIATION","VARIETY","VARIOUS","VARY","VAST","VEHICLE","VERSION","VERY","VIA","VICTIM","VICTORY","VIDEO","VIEW","VILLAGE","VIOLENCE","VISION","VISIT","VISITOR","VITAL","VOICE","VOLUME","VOTE","WAGE","WAIT","WALK","WALL","WANT","WAR","WARM","WARN","WASH","WATCH","WATER","WAVE","WAY","WE","WEAK","WEAPON","WEAR","WEATHER","WEEK","WEEKEND","WEIGHT","WELCOME","WELFARE","WELL","WEST","WESTERN","WHAT","WHATEVER","WHEN","WHERE","WHEREAS","WHETHER","WHICH","WHILE","WHILST","WHITE","WHO","WHOLE","WHOM","WHOSE","WHY","WIDE","WIDELY","WIFE","WILD","WILL","WIN","WIND","WINDOW","WINE","WING","WINNER","WINTER","WISH","WITH","WITHDRAW","WITHIN","WITHOUT","WOMAN","WONDER","WONDERFUL","WOOD","WORD","WORK","WORKER","WORKING","WORKS","WORLD","WORRY","WORTH","WOULD","WRITE","WRITER","WRITING","WRONG","YARD","YEAH","YEAR","YES","YESTERDAY","YET","YOU","YOUNG","YOUR","YOURSELF","YOUTH" \ No newline at end of file diff --git a/extra/project-euler/067/067.factor b/extra/project-euler/067/067.factor index 5df516f2f4..f206f59472 100644 --- a/extra/project-euler/067/067.factor +++ b/extra/project-euler/067/067.factor @@ -58,7 +58,4 @@ PRIVATE> ! [ euler067a ] 100 ave-time ! 14 ms run / 0 ms GC ave time - 100 trials -! source-067 [ max-path ] curry 100 ave-time -! 3 ms run / 0 ms GC ave time - 100 trials - MAIN: euler067a diff --git a/extra/project-euler/common/common.factor b/extra/project-euler/common/common.factor index 99bb3169c4..0910cbcb7b 100644 --- a/extra/project-euler/common/common.factor +++ b/extra/project-euler/common/common.factor @@ -13,6 +13,7 @@ IN: project-euler.common ! collect-consecutive - #8, #11 ! log10 - #25, #134 ! max-path - #18, #67 +! nth-triangle - #12, #42 ! number>digits - #16, #20, #30, #34 ! pandigital? - #32, #38 ! propagate-all - #18, #67 @@ -77,6 +78,9 @@ PRIVATE> : number>digits ( n -- seq ) number>string string>digits ; +: nth-triangle ( n -- n ) + dup 1+ * 2 / ; + : pandigital? ( n -- ? ) number>string natural-sort "123456789" = ; diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index 3433fe7154..226c47b0a3 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -12,8 +12,9 @@ USING: definitions io io.files kernel math math.parser project-euler.ave-time project-euler.029 project-euler.030 project-euler.031 project-euler.032 project-euler.033 project-euler.034 project-euler.035 project-euler.036 project-euler.037 project-euler.038 project-euler.039 project-euler.040 - project-euler.041 project-euler.048 project-euler.067 project-euler.075 - project-euler.134 project-euler.169 project-euler.173 project-euler.175 ; + project-euler.041 project-euler.042 project-euler.048 project-euler.067 + project-euler.075 project-euler.134 project-euler.169 project-euler.173 + project-euler.175 ; IN: project-euler Date: Sun, 3 Feb 2008 15:59:47 -0800 Subject: [PATCH 061/269] Take the fattening opengl vocab and hack it up into smaller, mouth-sized morsels --- core/alien/c-types/c-types.factor | 3 + extra/bunny/authors.txt | 1 + extra/bunny/cel-shaded/cel-shaded.factor | 3 +- extra/bunny/model/model.factor | 3 +- extra/bunny/outlined/outlined.factor | 3 +- extra/bunny/tags.txt | 1 + extra/opengl/authors.txt | 1 + extra/opengl/capabilities/authors.txt | 1 + .../capabilities/capabilities-docs.factor | 59 +++++ extra/opengl/capabilities/capabilities.factor | 67 +++++ extra/opengl/capabilities/summary.txt | 1 + extra/opengl/capabilities/tags.txt | 2 + extra/opengl/framebuffers/authors.txt | 1 + .../framebuffers/framebuffer-docs.factor | 35 +++ extra/opengl/framebuffers/framebuffers.factor | 43 ++++ extra/opengl/framebuffers/summary.txt | 1 + extra/opengl/framebuffers/tags.txt | 2 + extra/opengl/opengl-docs.factor | 202 +-------------- extra/opengl/opengl.factor | 241 +----------------- extra/opengl/shaders/authors.txt | 1 + extra/opengl/shaders/shaders-docs.factor | 112 ++++++++ extra/opengl/shaders/shaders.factor | 134 ++++++++++ extra/opengl/shaders/summary.txt | 1 + extra/opengl/shaders/tags.txt | 3 + 24 files changed, 487 insertions(+), 434 deletions(-) create mode 100644 extra/opengl/capabilities/authors.txt create mode 100644 extra/opengl/capabilities/capabilities-docs.factor create mode 100644 extra/opengl/capabilities/capabilities.factor create mode 100644 extra/opengl/capabilities/summary.txt create mode 100644 extra/opengl/capabilities/tags.txt create mode 100644 extra/opengl/framebuffers/authors.txt create mode 100644 extra/opengl/framebuffers/framebuffer-docs.factor create mode 100644 extra/opengl/framebuffers/framebuffers.factor create mode 100644 extra/opengl/framebuffers/summary.txt create mode 100644 extra/opengl/framebuffers/tags.txt create mode 100644 extra/opengl/shaders/authors.txt create mode 100644 extra/opengl/shaders/shaders-docs.factor create mode 100644 extra/opengl/shaders/shaders.factor create mode 100644 extra/opengl/shaders/summary.txt create mode 100644 extra/opengl/shaders/tags.txt diff --git a/core/alien/c-types/c-types.factor b/core/alien/c-types/c-types.factor index 8ab703eb7e..9bbd24351c 100755 --- a/core/alien/c-types/c-types.factor +++ b/core/alien/c-types/c-types.factor @@ -211,6 +211,9 @@ M: long-long-type box-return ( type -- ) over [ tuck 0 ] over c-setter append swap >r >r constructor-word r> r> add* define-inline ; +: c-bool> ( int -- ? ) + zero? not ; + : >c-array ( seq type word -- ) >r >r dup length dup r> dup -roll r> [ execute ] 2curry 2each ; inline diff --git a/extra/bunny/authors.txt b/extra/bunny/authors.txt index 1901f27a24..580f882c8d 100644 --- a/extra/bunny/authors.txt +++ b/extra/bunny/authors.txt @@ -1 +1,2 @@ Slava Pestov +Joe Groff diff --git a/extra/bunny/cel-shaded/cel-shaded.factor b/extra/bunny/cel-shaded/cel-shaded.factor index fc42ca971e..37343a23fb 100644 --- a/extra/bunny/cel-shaded/cel-shaded.factor +++ b/extra/bunny/cel-shaded/cel-shaded.factor @@ -1,5 +1,6 @@ USING: arrays bunny.model combinators.lib continuations -kernel multiline opengl opengl.gl sequences ; +kernel multiline opengl opengl.shaders opengl.capabilities +opengl.gl sequences ; IN: bunny.cel-shaded STRING: vertex-shader-source diff --git a/extra/bunny/model/model.factor b/extra/bunny/model/model.factor index e3df6bb26c..f2c93eac3e 100644 --- a/extra/bunny/model/model.factor +++ b/extra/bunny/model/model.factor @@ -1,6 +1,7 @@ USING: alien alien.c-types arrays sequences math math.vectors math.matrices math.parser io io.files kernel opengl -opengl.gl opengl.glu shuffle http.client vectors splitting +opengl.gl opengl.glu opengl.capabilities shuffle http.client +vectors splitting tools.time system combinators combinators.lib combinators.cleave float-arrays continuations namespaces ; IN: bunny.model diff --git a/extra/bunny/outlined/outlined.factor b/extra/bunny/outlined/outlined.factor index 9de341561c..d7064ebdde 100644 --- a/extra/bunny/outlined/outlined.factor +++ b/extra/bunny/outlined/outlined.factor @@ -1,6 +1,7 @@ USING: arrays bunny.model bunny.cel-shaded combinators.lib continuations kernel math multiline -opengl opengl.gl sequences ui.gadgets ; +opengl opengl.shaders opengl.framebuffers opengl.gl +opengl.capabilities sequences ui.gadgets ; IN: bunny.outlined STRING: outlined-pass1-fragment-shader-main-source diff --git a/extra/bunny/tags.txt b/extra/bunny/tags.txt index cb5fc203e1..339115d3c7 100644 --- a/extra/bunny/tags.txt +++ b/extra/bunny/tags.txt @@ -1 +1,2 @@ demos +opengl diff --git a/extra/opengl/authors.txt b/extra/opengl/authors.txt index e1907c6d91..55ac3c728e 100644 --- a/extra/opengl/authors.txt +++ b/extra/opengl/authors.txt @@ -1,2 +1,3 @@ Slava Pestov Eduardo Cavazos +Joe Groff diff --git a/extra/opengl/capabilities/authors.txt b/extra/opengl/capabilities/authors.txt new file mode 100644 index 0000000000..6a0dc7293a --- /dev/null +++ b/extra/opengl/capabilities/authors.txt @@ -0,0 +1 @@ +Joe Groff \ No newline at end of file diff --git a/extra/opengl/capabilities/capabilities-docs.factor b/extra/opengl/capabilities/capabilities-docs.factor new file mode 100644 index 0000000000..e73b7a3f0b --- /dev/null +++ b/extra/opengl/capabilities/capabilities-docs.factor @@ -0,0 +1,59 @@ +USING: help.markup help.syntax io kernel math quotations +opengl.gl multiline assocs ; +IN: opengl.capabilities + +HELP: gl-version +{ $values { "version" "The version string from the OpenGL implementation" } } +{ $description "Wrapper for " { $snippet "GL_VERSION glGetString" } " that removes the vendor-specific information from the version string." } ; + +HELP: gl-vendor-version +{ $values { "version" "The vendor-specific version information from the OpenGL implementation" } } +{ $description "Wrapper for " { $snippet "GL_VERSION glGetString" } " that returns only the vendor-specific information from the version string." } ; + +HELP: has-gl-version? +{ $values { "version" "A version string" } { "?" "A boolean value" } } +{ $description "Compares the version string returned by " { $link gl-version } " to " { $snippet "version" } ". Returns true if the implementation version meets or exceeds " { $snippet "version" } "." } ; + +HELP: require-gl-version +{ $values { "version" "A version string" } } +{ $description "Throws an exception if " { $link has-gl-version? } " returns false for " { $snippet "version" } "." } ; + +HELP: glsl-version +{ $values { "version" "The GLSL version string from the OpenGL implementation" } } +{ $description "Wrapper for " { $snippet "GL_SHADING_LANGUAGE_VERSION glGetString" } " that removes the vendor-specific information from the version string." } ; + +HELP: glsl-vendor-version +{ $values { "version" "The vendor-specific GLSL version information from the OpenGL implementation" } } +{ $description "Wrapper for " { $snippet "GL_SHADING_LANGUAGE_VERSION glGetString" } " that returns only the vendor-specific information from the version string." } ; + +HELP: has-glsl-version? +{ $values { "version" "A version string" } { "?" "A boolean value" } } +{ $description "Compares the version string returned by " { $link glsl-version } " to " { $snippet "version" } ". Returns true if the implementation version meets or exceeds " { $snippet "version" } "." } ; + +HELP: require-glsl-version +{ $values { "version" "A version string" } } +{ $description "Throws an exception if " { $link has-glsl-version? } " returns false for " { $snippet "version" } "." } ; + +HELP: gl-extensions +{ $values { "seq" "A sequence of strings naming the implementation-supported OpenGL extensions" } } +{ $description "Wrapper for " { $snippet "GL_EXTENSIONS glGetString" } " that returns a sequence of extension names supported by the OpenGL implementation." } ; + +HELP: has-gl-extensions? +{ $values { "extensions" "A sequence of extension name strings" } { "?" "A boolean value" } } +{ $description "Returns true if the set of " { $snippet "extensions" } " is a subset of the implementation-supported extensions returned by " { $link gl-extensions } "." } ; + +HELP: has-gl-version-or-extensions? +{ $values { "version" "A version string" } { "extensions" "A sequence of extension name strings" } } +{ $description "Returns true if either " { $link has-gl-version? } " or " { $link has-gl-extensions? } " returns true for " { $snippet "version" } " or " { $snippet "extensions" } ", respectively. Intended for use when required OpenGL functionality can be verified either by a minimum version or a set of equivalent extensions." } ; + +HELP: require-gl-extensions +{ $values { "extensions" "A sequence of extension name strings" } } +{ $description "Throws an exception if " { $link has-gl-extensions? } " returns false for " { $snippet "extensions" } "." } ; + +HELP: require-gl-version-or-extensions +{ $values { "version" "A version string" } { "extensions" "A sequence of extension name strings" } } +{ $description "Throws an exception if neither " { $link has-gl-version? } " nor " { $link has-gl-extensions? } " returns true for " { $snippet "version" } " or " { $snippet "extensions" } ", respectively. Intended for use when required OpenGL functionality can be verified either by a minimum version or a set of equivalent extensions." } ; + +{ require-gl-version require-glsl-version require-gl-extensions require-gl-version-or-extensions has-gl-version? has-glsl-version? has-gl-extensions? has-gl-version-or-extensions? gl-version glsl-version gl-extensions } related-words + +ABOUT: "gl-utilities" diff --git a/extra/opengl/capabilities/capabilities.factor b/extra/opengl/capabilities/capabilities.factor new file mode 100644 index 0000000000..d9eb6fd679 --- /dev/null +++ b/extra/opengl/capabilities/capabilities.factor @@ -0,0 +1,67 @@ +! Copyright (C) 2008 Joe Groff. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel namespaces sequences splitting opengl.gl +continuations math.parser math arrays ; +IN: opengl.capabilities + +: (require-gl) ( thing require-quot make-error-quot -- ) + >r dupd call + [ r> 2drop ] + [ r> " " make throw ] + if ; inline + +: gl-extensions ( -- seq ) + GL_EXTENSIONS glGetString " " split ; +: has-gl-extensions? ( extensions -- ? ) + gl-extensions swap [ over member? ] all? nip ; +: (make-gl-extensions-error) ( required-extensions -- ) + gl-extensions swap seq-diff + "Required OpenGL extensions not supported:\n" % + [ " " % % "\n" % ] each ; +: require-gl-extensions ( extensions -- ) + [ has-gl-extensions? ] + [ (make-gl-extensions-error) ] + (require-gl) ; + +: version-seq ( version-string -- version-seq ) + "." split [ string>number ] map ; + +: version<=> ( version1 version2 -- n ) + swap version-seq swap version-seq <=> ; + +: (gl-version) ( -- version vendor ) + GL_VERSION glGetString " " split1 ; +: gl-version ( -- version ) + (gl-version) drop ; +: gl-vendor-version ( -- version ) + (gl-version) nip ; +: has-gl-version? ( version -- ? ) + gl-version version<=> 0 <= ; +: (make-gl-version-error) ( required-version -- ) + "Required OpenGL version " % % " not supported (" % gl-version % " available)" % ; +: require-gl-version ( version -- ) + [ has-gl-version? ] + [ (make-gl-version-error) ] + (require-gl) ; + +: (glsl-version) ( -- version vendor ) + GL_SHADING_LANGUAGE_VERSION glGetString " " split1 ; +: glsl-version ( -- version ) + (glsl-version) drop ; +: glsl-vendor-version ( -- version ) + (glsl-version) nip ; +: has-glsl-version? ( version -- ? ) + glsl-version version<=> 0 <= ; +: require-glsl-version ( version -- ) + [ has-glsl-version? ] + [ "Required GLSL version " % % " not supported (" % glsl-version % " available)" % ] + (require-gl) ; + +: has-gl-version-or-extensions? ( version extensions -- ? ) + has-gl-extensions? swap has-gl-version? or ; + +: require-gl-version-or-extensions ( version extensions -- ) + 2array [ first2 has-gl-version-or-extensions? ] [ + dup first (make-gl-version-error) "\n" % + second (make-gl-extensions-error) "\n" % + ] (require-gl) ; diff --git a/extra/opengl/capabilities/summary.txt b/extra/opengl/capabilities/summary.txt new file mode 100644 index 0000000000..d31b63b8d4 --- /dev/null +++ b/extra/opengl/capabilities/summary.txt @@ -0,0 +1 @@ +Testing for OpenGL versions and extensions \ No newline at end of file diff --git a/extra/opengl/capabilities/tags.txt b/extra/opengl/capabilities/tags.txt new file mode 100644 index 0000000000..77282be3a9 --- /dev/null +++ b/extra/opengl/capabilities/tags.txt @@ -0,0 +1,2 @@ +opengl +bindings diff --git a/extra/opengl/framebuffers/authors.txt b/extra/opengl/framebuffers/authors.txt new file mode 100644 index 0000000000..6a0dc7293a --- /dev/null +++ b/extra/opengl/framebuffers/authors.txt @@ -0,0 +1 @@ +Joe Groff \ No newline at end of file diff --git a/extra/opengl/framebuffers/framebuffer-docs.factor b/extra/opengl/framebuffers/framebuffer-docs.factor new file mode 100644 index 0000000000..c5507dcce1 --- /dev/null +++ b/extra/opengl/framebuffers/framebuffer-docs.factor @@ -0,0 +1,35 @@ +USING: help.markup help.syntax io kernel math quotations +opengl.gl multiline assocs ; +IN: opengl.framebuffers + +HELP: gen-framebuffer +{ $values { "id" integer } } +{ $description "Wrapper for " { $link glGenFramebuffersEXT } " to handle the common case of generating a single framebuffer ID." } ; + +HELP: gen-renderbuffer +{ $values { "id" integer } } +{ $description "Wrapper for " { $link glGenRenderbuffersEXT } " to handle the common case of generating a single render buffer ID." } ; + +HELP: delete-framebuffer +{ $values { "id" integer } } +{ $description "Wrapper for " { $link glDeleteFramebuffersEXT } " to handle the common case of deleting a single framebuffer ID." } ; + +HELP: delete-renderbuffer +{ $values { "id" integer } } +{ $description "Wrapper for " { $link glDeleteRenderbuffersEXT } " to handle the common case of deleting a single render buffer ID." } ; + +{ gen-framebuffer delete-framebuffer } related-words +{ gen-renderbuffer delete-renderbuffer } related-words + +HELP: framebuffer-incomplete? +{ $values { "status/f" "The framebuffer error code, or " { $snippet "f" } " if the framebuffer is render-complete." } } +{ $description "Checks the framebuffer currently bound by " { $link glBindFramebufferEXT } " or " { $link with-framebuffer } " to see if it is incomplete, i.e., it is not ready to be rendered to." } ; + +HELP: check-framebuffer +{ $description "Checks the framebuffer currently bound by " { $link glBindFramebufferEXT } " or " { $link with-framebuffer } " with " { $link framebuffer-incomplete? } ", and throws a descriptive error if the framebuffer is incomplete." } ; + +HELP: with-framebuffer +{ $values { "id" "The id of a framebuffer object." } { "quot" "a quotation" } } +{ $description "Binds framebuffer " { $snippet "id" } " in the dynamic extent of " { $snippet "quot" } ", restoring the window framebuffer when finished." } ; + +ABOUT: "gl-utilities" \ No newline at end of file diff --git a/extra/opengl/framebuffers/framebuffers.factor b/extra/opengl/framebuffers/framebuffers.factor new file mode 100644 index 0000000000..346789e1c5 --- /dev/null +++ b/extra/opengl/framebuffers/framebuffers.factor @@ -0,0 +1,43 @@ +! Copyright (C) 2008 Joe Groff. +! See http://factorcode.org/license.txt for BSD license. +USING: opengl opengl.gl combinators continuations kernel +alien.c-types ; +IN: opengl.framebuffers + +: gen-framebuffer ( -- id ) + [ glGenFramebuffersEXT ] (gen-gl-object) ; +: gen-renderbuffer ( -- id ) + [ glGenRenderbuffersEXT ] (gen-gl-object) ; + +: delete-framebuffer ( id -- ) + [ glDeleteFramebuffersEXT ] (delete-gl-object) ; +: delete-renderbuffer ( id -- ) + [ glDeleteRenderbuffersEXT ] (delete-gl-object) ; + +: framebuffer-incomplete? ( -- status/f ) + GL_FRAMEBUFFER_EXT glCheckFramebufferStatusEXT + dup GL_FRAMEBUFFER_COMPLETE_EXT = f rot ? ; + +: framebuffer-error ( status -- * ) + { + { GL_FRAMEBUFFER_COMPLETE_EXT [ "framebuffer complete" ] } + { GL_FRAMEBUFFER_UNSUPPORTED_EXT [ "framebuffer configuration unsupported" ] } + { GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT [ "framebuffer incomplete (incomplete attachment)" ] } + { GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT [ "framebuffer incomplete (missing attachment)" ] } + { GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT [ "framebuffer incomplete (dimension mismatch)" ] } + { GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT [ "framebuffer incomplete (format mismatch)" ] } + { GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT [ "framebuffer incomplete (draw buffer(s) have no attachment)" ] } + { GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT [ "framebuffer incomplete (read buffer has no attachment)" ] } + [ drop gl-error "unknown framebuffer error" ] + } case throw ; + +: check-framebuffer ( -- ) + framebuffer-incomplete? [ framebuffer-error ] when* ; + +: with-framebuffer ( id quot -- ) + GL_FRAMEBUFFER_EXT rot glBindFramebufferEXT + [ GL_FRAMEBUFFER_EXT 0 glBindFramebufferEXT ] [ ] cleanup ; inline + +: framebuffer-attachment ( attachment -- id ) + GL_FRAMEBUFFER_EXT swap GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT + 0 [ glGetFramebufferAttachmentParameterivEXT ] keep *uint ; diff --git a/extra/opengl/framebuffers/summary.txt b/extra/opengl/framebuffers/summary.txt new file mode 100644 index 0000000000..3ef713ac13 --- /dev/null +++ b/extra/opengl/framebuffers/summary.txt @@ -0,0 +1 @@ +Rendering to offscreen textures using the GL_EXT_framebuffer_object extension \ No newline at end of file diff --git a/extra/opengl/framebuffers/tags.txt b/extra/opengl/framebuffers/tags.txt new file mode 100644 index 0000000000..77282be3a9 --- /dev/null +++ b/extra/opengl/framebuffers/tags.txt @@ -0,0 +1,2 @@ +opengl +bindings diff --git a/extra/opengl/opengl-docs.factor b/extra/opengl/opengl-docs.factor index cb0c9e884f..97120237ec 100644 --- a/extra/opengl/opengl-docs.factor +++ b/extra/opengl/opengl-docs.factor @@ -1,5 +1,5 @@ USING: help.markup help.syntax io kernel math quotations -opengl.gl multiline assocs ; +opengl.gl multiline assocs vocabs.loader sequences ; IN: opengl HELP: gl-color @@ -57,14 +57,6 @@ HELP: gen-texture { $values { "id" integer } } { $description "Wrapper for " { $link glGenTextures } " to handle the common case of generating a single texture ID." } ; -HELP: gen-framebuffer -{ $values { "id" integer } } -{ $description "Wrapper for " { $link glGenFramebuffersEXT } " to handle the common case of generating a single framebuffer ID." } ; - -HELP: gen-renderbuffer -{ $values { "id" integer } } -{ $description "Wrapper for " { $link glGenRenderbuffersEXT } " to handle the common case of generating a single render buffer ID." } ; - HELP: gen-gl-buffer { $values { "id" integer } } { $description "Wrapper for " { $link glGenBuffers } " to handle the common case of generating a single buffer ID." } ; @@ -73,34 +65,13 @@ HELP: delete-texture { $values { "id" integer } } { $description "Wrapper for " { $link glDeleteTextures } " to handle the common case of deleting a single texture ID." } ; -HELP: delete-framebuffer -{ $values { "id" integer } } -{ $description "Wrapper for " { $link glDeleteFramebuffersEXT } " to handle the common case of deleting a single framebuffer ID." } ; - -HELP: delete-renderbuffer -{ $values { "id" integer } } -{ $description "Wrapper for " { $link glDeleteRenderbuffersEXT } " to handle the common case of deleting a single render buffer ID." } ; - HELP: delete-gl-buffer { $values { "id" integer } } { $description "Wrapper for " { $link glDeleteBuffers } " to handle the common case of deleting a single buffer ID." } ; { gen-texture delete-texture } related-words -{ gen-framebuffer delete-framebuffer } related-words -{ gen-renderbuffer delete-renderbuffer } related-words { gen-gl-buffer delete-gl-buffer } related-words -HELP: framebuffer-incomplete? -{ $values { "status/f" "The framebuffer error code, or " { $snippet "f" } " if the framebuffer is render-complete." } } -{ $description "Checks the framebuffer currently bound by " { $link glBindFramebufferEXT } " or " { $link with-framebuffer } " to see if it is incomplete, i.e., it is not ready to be rendered to." } ; - -HELP: check-framebuffer -{ $description "Checks the framebuffer currently bound by " { $link glBindFramebufferEXT } " or " { $link with-framebuffer } " with " { $link framebuffer-incomplete? } ", and throws a descriptive error if the framebuffer is incomplete." } ; - -HELP: with-framebuffer -{ $values { "id" "The id of a framebuffer object." } { "quot" "a quotation" } } -{ $description "Binds framebuffer " { $snippet "id" } " in the dynamic extent of " { $snippet "quot" } ", restoring the window framebuffer when finished." } ; - HELP: bind-texture-unit { $values { "id" "The id of a texture object." } { "target" "The texture target (e.g., " { $snippet "GL_TEXTURE_2D" } ")" } { "unit" "The texture unit to bind (e.g., " { $snippet "GL_TEXTURE0" } ")" } } { $description "Binds texture " { $snippet "id" } " to texture target " { $snippet "target" } " of texture unit " { $snippet "unit" } ". Equivalent to " { $snippet "unit glActiveTexture target id glBindTexture" } "." } ; @@ -148,175 +119,9 @@ HELP: with-translation { $values { "loc" "a pair of integers" } { "quot" quotation } } { $description "Calls the quotation with a translation by " { $snippet "loc" } " pixels applied to the current " { $link GL_MODELVIEW } " matrix, restoring the matrix when the quotation is done." } ; -HELP: gl-shader -{ $class-description { $snippet "gl-shader" } " is a predicate class comprising values returned by OpenGL to represent shader objects. The following words are provided for creating and manipulating these objects:" - { $list - { { $link } " - Compile GLSL code into a shader object" } - { { $link gl-shader-ok? } " - Check whether a shader object compiled successfully" } - { { $link check-gl-shader } " - Throw an error unless a shader object compiled successfully" } - { { $link gl-shader-info-log } " - Retrieve the info log of messages generated by the GLSL compiler" } - { { $link delete-gl-shader } " - Invalidate a shader object" } - } - "The derived predicate classes " { $link vertex-shader } " and " { $link fragment-shader } " are also defined for the two standard kinds of shader defined by the OpenGL specification." } ; - -HELP: vertex-shader -{ $class-description { $snippet "vertex-shader" } " is the predicate class of " { $link gl-shader } " objects that refer to shaders of type " { $snippet "GL_VERTEX_SHADER" } ". In addition to the " { $snippet "gl-shader" } " words, the following vertex shader-specific functions are defined:" - { $list - { { $link } " - Compile GLSL code into a vertex shader object "} - } -} ; - -HELP: fragment-shader -{ $class-description { $snippet "fragment-shader" } " is the predicate class of " { $link gl-shader } " objects that refer to shaders of type " { $snippet "GL_FRAGMENT_SHADER" } ". In addition to the " { $snippet "gl-shader" } " words, the following fragment shader-specific functions are defined:" - { $list - { { $link } " - Compile GLSL code into a fragment shader object "} - } -} ; - -HELP: -{ $values { "source" "The GLSL source code to compile" } { "kind" "The kind of shader to compile, such as " { $snippet "GL_VERTEX_SHADER" } " or " { $snippet "GL_FRAGMENT_SHADER" } } } -{ $description "Tries to compile the given GLSL source into a shader object. The returned object can be checked for validity by " { $link check-gl-shader } " or " { $link gl-shader-ok? } ". Errors and warnings generated by the GLSL compiler will be collected in the info log, available from " { $link gl-shader-info-log } ".\n\nWhen the shader object is no longer needed, it should be deleted using " { $link delete-gl-shader } " or else be attached to a " { $link gl-program } " object deleted using " { $link delete-gl-program } "." } ; - -HELP: -{ $values { "source" "The GLSL source code to compile" } } -{ $description "Tries to compile the given GLSL source into a vertex shader object. Equivalent to " { $snippet "GL_VERTEX_SHADER " } "." } ; - -HELP: -{ $values { "source" "The GLSL source code to compile" } } -{ $description "Tries to compile the given GLSL source into a fragment shader object. Equivalent to " { $snippet "GL_FRAGMENT_SHADER " } "." } ; - -HELP: gl-shader-ok? -{ $values { "shader" "A " { $link gl-shader } " object" } } -{ $description "Returns a boolean value indicating whether the given shader object compiled successfully. Compilation errors and warnings are available in the shader's info log, which can be gotten using " { $link gl-shader-info-log } "." } ; - -HELP: check-gl-shader -{ $values { "shader" "A " { $link gl-shader } " object" } } -{ $description "Throws an error containing the " { $link gl-shader-info-log } " for the shader object if it failed to compile. Otherwise, the shader object is left on the stack." } ; - -HELP: delete-gl-shader -{ $values { "shader" "A " { $link gl-shader } " object" } } -{ $description "Deletes the shader object, invalidating it and releasing any resources allocated for it by the OpenGL implementation." } ; - -HELP: gl-shader-info-log -{ $values { "shader" "A " { $link gl-shader } " object" } } -{ $description "Retrieves the info log for " { $snippet "shader" } ", including any errors or warnings generated in compiling the shader object." } ; - -HELP: gl-program -{ $class-description { $snippet "gl-program" } " is a predicate class comprising values returned by OpenGL to represent proram objects. The following words are provided for creating and manipulating these objects:" - { $list - { { $link } ", " { $link } " - Link a set of shaders into a GLSL program" } - { { $link gl-program-ok? } " - Check whether a program object linked successfully" } - { { $link check-gl-program } " - Throw an error unless a program object linked successfully" } - { { $link gl-program-info-log } " - Retrieve the info log of messages generated by the GLSL linker" } - { { $link gl-program-shaders } " - Retrieve the set of shader objects composing the GLSL program" } - { { $link delete-gl-program } " - Invalidate a program object and all its attached shaders" } - { { $link with-gl-program } " - Use a program object" } - } -} ; - -HELP: -{ $values { "shaders" "A sequence of " { $link gl-shader } " objects." } } -{ $description "Creates a new GLSL program object, attaches all the shader objects in the " { $snippet "shaders" } " sequence, and attempts to link them. The returned object can be checked for validity by " { $link check-gl-program } " or " { $link gl-program-ok? } ". Errors and warnings generated by the GLSL linker will be collected in the info log, available from " { $link gl-program-info-log } ".\n\nWhen the program object and its attached shaders are no longer needed, it should be deleted using " { $link delete-gl-program } "." } ; - -HELP: -{ $values { "vertex-shader-source" "A string containing GLSL vertex shader source" } { "fragment-shader-source" "A string containing GLSL fragment shader source" } } -{ $description "Wrapper for " { $link } " for the simple case of compiling a single vertex shader and fragment shader and linking them into a GLSL program. Throws an exception if compiling or linking fails." } ; - -{ } related-words - -HELP: gl-program-ok? -{ $values { "program" "A " { $link gl-program } " object" } } -{ $description "Returns a boolean value indicating whether the given program object linked successfully. Link errors and warnings are available in the program's info log, which can be gotten using " { $link gl-program-info-log } "." } ; - -HELP: check-gl-program -{ $values { "program" "A " { $link gl-program } " object" } } -{ $description "Throws an error containing the " { $link gl-program-info-log } " for the program object if it failed to link. Otherwise, the program object is left on the stack." } ; - -HELP: gl-program-info-log -{ $values { "program" "A " { $link gl-program } " object" } } -{ $description "Retrieves the info log for " { $snippet "program" } ", including any errors or warnings generated in linking the program object." } ; - -HELP: delete-gl-program -{ $values { "program" "A " { $link gl-program } " object" } } -{ $description "Deletes the program object, invalidating it and releasing any resources allocated for it by the OpenGL implementation. Any attached " { $link gl-shader } "s are also deleted.\n\nIf the shader objects should be preserved, they should each be detached using " { $link detach-gl-program-shader } ". The program object can then be destroyed alone using " { $link delete-gl-program-only } "." } ; - -HELP: with-gl-program -{ $values { "program" "A " { $link gl-program } " object" } { "uniforms" "An " { $link assoc } " between uniform parameter names and quotations with effect " { $snippet "( uniform-location -- )" } } { "quot" "A quotation" } } -{ $description "Enables " { $snippet "program" } " for all OpenGL calls made in the dynamic extent of " { $snippet "quot" } ". The fixed-function pipeline is restored at the end of " { $snippet "quot" } ". Before calling " { $snippet "quot" } ", calls " { $link glGetUniformLocation } " on each key of " { $snippet "uniforms" } " to get the address of the uniform parameter, which is then placed on top of the stack for the associated quotation.\n\nExample:" } -{ $code <" -! From bunny.cel-shaded -: (draw-cel-shaded-bunny) ( geom program -- ) - { - { "light_direction" [ 1.0 -1.0 1.0 glUniform3f ] } - { "color" [ 0.6 0.5 0.5 1.0 glUniform4f ] } - { "ambient" [ 0.2 0.2 0.2 0.2 glUniform4f ] } - { "diffuse" [ 0.8 0.8 0.8 0.8 glUniform4f ] } - { "shininess" [ 100.0 glUniform1f ] } - } [ bunny-geom ] with-gl-program ; -"> } ; - -HELP: gl-version -{ $values { "version" "The version string from the OpenGL implementation" } } -{ $description "Wrapper for " { $snippet "GL_VERSION glGetString" } " that removes the vendor-specific information from the version string." } ; - -HELP: gl-vendor-version -{ $values { "version" "The vendor-specific version information from the OpenGL implementation" } } -{ $description "Wrapper for " { $snippet "GL_VERSION glGetString" } " that returns only the vendor-specific information from the version string." } ; - -HELP: has-gl-version? -{ $values { "version" "A version string" } { "?" "A boolean value" } } -{ $description "Compares the version string returned by " { $link gl-version } " to " { $snippet "version" } ". Returns true if the implementation version meets or exceeds " { $snippet "version" } "." } ; - -HELP: require-gl-version -{ $values { "version" "A version string" } } -{ $description "Throws an exception if " { $link has-gl-version? } " returns false for " { $snippet "version" } "." } ; - -HELP: glsl-version -{ $values { "version" "The GLSL version string from the OpenGL implementation" } } -{ $description "Wrapper for " { $snippet "GL_SHADING_LANGUAGE_VERSION glGetString" } " that removes the vendor-specific information from the version string." } ; - -HELP: glsl-vendor-version -{ $values { "version" "The vendor-specific GLSL version information from the OpenGL implementation" } } -{ $description "Wrapper for " { $snippet "GL_SHADING_LANGUAGE_VERSION glGetString" } " that returns only the vendor-specific information from the version string." } ; - -HELP: has-glsl-version? -{ $values { "version" "A version string" } { "?" "A boolean value" } } -{ $description "Compares the version string returned by " { $link glsl-version } " to " { $snippet "version" } ". Returns true if the implementation version meets or exceeds " { $snippet "version" } "." } ; - -HELP: require-glsl-version -{ $values { "version" "A version string" } } -{ $description "Throws an exception if " { $link has-glsl-version? } " returns false for " { $snippet "version" } "." } ; - -HELP: gl-extensions -{ $values { "seq" "A sequence of strings naming the implementation-supported OpenGL extensions" } } -{ $description "Wrapper for " { $snippet "GL_EXTENSIONS glGetString" } " that returns a sequence of extension names supported by the OpenGL implementation." } ; - -HELP: has-gl-extensions? -{ $values { "extensions" "A sequence of extension name strings" } { "?" "A boolean value" } } -{ $description "Returns true if the set of " { $snippet "extensions" } " is a subset of the implementation-supported extensions returned by " { $link gl-extensions } "." } ; - -HELP: has-gl-version-or-extensions? -{ $values { "version" "A version string" } { "extensions" "A sequence of extension name strings" } } -{ $description "Returns true if either " { $link has-gl-version? } " or " { $link has-gl-extensions? } " returns true for " { $snippet "version" } " or " { $snippet "extensions" } ", respectively. Intended for use when required OpenGL functionality can be verified either by a minimum version or a set of equivalent extensions." } ; - -HELP: require-gl-extensions -{ $values { "extensions" "A sequence of extension name strings" } } -{ $description "Throws an exception if " { $link has-gl-extensions? } " returns false for " { $snippet "extensions" } "." } ; - -HELP: require-gl-version-or-extensions -{ $values { "version" "A version string" } { "extensions" "A sequence of extension name strings" } } -{ $description "Throws an exception if neither " { $link has-gl-version? } " nor " { $link has-gl-extensions? } " returns true for " { $snippet "version" } " or " { $snippet "extensions" } ", respectively. Intended for use when required OpenGL functionality can be verified either by a minimum version or a set of equivalent extensions." } ; - -{ require-gl-version require-glsl-version require-gl-extensions require-gl-version-or-extensions has-gl-version? has-glsl-version? has-gl-extensions? has-gl-version-or-extensions? gl-version glsl-version gl-extensions } related-words - ARTICLE: "gl-utilities" "OpenGL utility words" "In addition to the full OpenGL API, the " { $vocab-link "opengl" } " vocabulary includes some utility words to give OpenGL a more Factor-like feel." $nl -"Checking implementation capabilities:" -{ $subsection require-gl-version } -{ $subsection require-gl-extensions } -{ $subsection require-glsl-version } -{ $subsection require-gl-version-or-extensions } "Wrappers:" { $subsection gl-color } { $subsection gl-vertex } @@ -329,8 +134,6 @@ $nl { $subsection do-attribs } { $subsection do-matrix } { $subsection with-translation } -{ $subsection with-framebuffer } -{ $subsection with-gl-program } { $subsection make-dlist } "Rendering geometric shapes:" { $subsection gl-line } @@ -339,9 +142,6 @@ $nl { $subsection gl-fill-poly } { $subsection gl-poly } { $subsection gl-gradient } -"Compiling, linking, and using GLSL programs:" -{ $subsection gl-shader } -{ $subsection gl-program } ; ABOUT: "gl-utilities" diff --git a/extra/opengl/opengl.factor b/extra/opengl/opengl.factor index 071f85fe12..5afb6ef070 100755 --- a/extra/opengl/opengl.factor +++ b/extra/opengl/opengl.factor @@ -33,11 +33,19 @@ IN: opengl : do-enabled-client-state ( what quot -- ) over glEnableClientState dip glDisableClientState ; inline -: all-enabled ( seq quot -- ) +: words>values ( word/value-seq -- value-seq ) + [ dup word? [ execute ] [ ] if ] map ; + +: (all-enabled) ( seq quot -- ) over [ glEnable ] each dip [ glDisable ] each ; inline -: all-enabled-client-state ( seq quot -- ) +: (all-enabled-client-state) ( seq quot -- ) over [ glEnableClientState ] each dip [ glDisableClientState ] each ; inline +MACRO: all-enabled ( seq quot -- ) + >r words>values r> [ (all-enabled) ] 2curry ; +MACRO: all-enabled-client-state ( seq quot -- ) + >r words>values r> [ (all-enabled-client-state) ] 2curry ; + : do-matrix ( mode quot -- ) swap [ glMatrixMode glPushMatrix call ] keep glMatrixMode glPopMatrix ; inline @@ -106,10 +114,6 @@ IN: opengl >r 1 0 r> keep *uint ; inline : gen-texture ( -- id ) [ glGenTextures ] (gen-gl-object) ; -: gen-framebuffer ( -- id ) - [ glGenFramebuffersEXT ] (gen-gl-object) ; -: gen-renderbuffer ( -- id ) - [ glGenRenderbuffersEXT ] (gen-gl-object) ; : gen-gl-buffer ( -- id ) [ glGenBuffers ] (gen-gl-object) ; @@ -117,10 +121,6 @@ IN: opengl >r 1 swap r> call ; inline : delete-texture ( id -- ) [ glDeleteTextures ] (delete-gl-object) ; -: delete-framebuffer ( id -- ) - [ glDeleteFramebuffersEXT ] (delete-gl-object) ; -: delete-renderbuffer ( id -- ) - [ glDeleteRenderbuffersEXT ] (delete-gl-object) ; : delete-gl-buffer ( id -- ) [ glDeleteBuffers ] (delete-gl-object) ; @@ -141,40 +141,14 @@ IN: opengl : buffer-offset ( int -- alien ) ; inline -: framebuffer-incomplete? ( -- status/f ) - GL_FRAMEBUFFER_EXT glCheckFramebufferStatusEXT - dup GL_FRAMEBUFFER_COMPLETE_EXT = f rot ? ; - -: framebuffer-error ( status -- * ) - { { GL_FRAMEBUFFER_COMPLETE_EXT [ "framebuffer complete" ] } - { GL_FRAMEBUFFER_UNSUPPORTED_EXT [ "framebuffer configuration unsupported" ] } - { GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT [ "framebuffer incomplete (incomplete attachment)" ] } - { GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT [ "framebuffer incomplete (missing attachment)" ] } - { GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT [ "framebuffer incomplete (dimension mismatch)" ] } - { GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT [ "framebuffer incomplete (format mismatch)" ] } - { GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT [ "framebuffer incomplete (draw buffer(s) have no attachment)" ] } - { GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT [ "framebuffer incomplete (read buffer has no attachment)" ] } - [ drop gl-error "unknown framebuffer error" ] } case throw ; - -: check-framebuffer ( -- ) - framebuffer-incomplete? [ framebuffer-error ] when* ; - -: with-framebuffer ( id quot -- ) - GL_FRAMEBUFFER_EXT rot glBindFramebufferEXT - [ GL_FRAMEBUFFER_EXT 0 glBindFramebufferEXT ] [ ] cleanup ; inline - : bind-texture-unit ( id target unit -- ) glActiveTexture swap glBindTexture gl-error ; -: framebuffer-attachment ( attachment -- id ) - GL_FRAMEBUFFER_EXT swap GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT - 0 [ glGetFramebufferAttachmentParameterivEXT ] keep *uint ; - : (set-draw-buffers) ( buffers -- ) dup length swap >c-uint-array glDrawBuffers ; MACRO: set-draw-buffers ( buffers -- ) - [ dup word? [ execute ] [ ] if ] map [ (set-draw-buffers) ] curry ; + words>values [ (set-draw-buffers) ] curry ; : do-attribs ( bits quot -- ) swap glPushAttrib call glPopAttrib ; inline @@ -274,196 +248,3 @@ TUPLE: sprite loc dim dim2 dlist texture ; glLoadIdentity GL_MODELVIEW glMatrixMode glLoadIdentity ; - -! Shaders - -: c-true? ( int -- ? ) zero? not ; inline - -: with-gl-shader-source-ptr ( string quot -- ) - swap string>char-alien malloc-byte-array [ - swap call - ] keep free ; inline - -: ( source kind -- shader ) - glCreateShader dup rot - [ 1 swap f glShaderSource ] with-gl-shader-source-ptr - [ glCompileShader ] keep - gl-error ; - -: (gl-shader?) ( object -- ? ) - dup integer? [ glIsShader c-true? ] [ drop f ] if ; - -: gl-shader-get-int ( shader enum -- value ) - 0 [ glGetShaderiv ] keep *int ; - -: gl-shader-ok? ( shader -- ? ) - GL_COMPILE_STATUS gl-shader-get-int c-true? ; - -: ( source -- vertex-shader ) - GL_VERTEX_SHADER ; inline - -: (vertex-shader?) ( object -- ? ) - dup (gl-shader?) - [ GL_SHADER_TYPE gl-shader-get-int GL_VERTEX_SHADER = ] - [ drop f ] if ; - -: ( source -- fragment-shader ) - GL_FRAGMENT_SHADER ; inline - -: (fragment-shader?) ( object -- ? ) - dup (gl-shader?) - [ GL_SHADER_TYPE gl-shader-get-int GL_FRAGMENT_SHADER = ] - [ drop f ] if ; - -: gl-shader-info-log-length ( shader -- log-length ) - GL_INFO_LOG_LENGTH gl-shader-get-int ; inline - -: gl-shader-info-log ( shader -- log ) - dup gl-shader-info-log-length dup [ - [ 0 swap glGetShaderInfoLog ] keep - alien>char-string - ] with-malloc ; - -: check-gl-shader ( shader -- shader* ) - dup gl-shader-ok? [ dup gl-shader-info-log throw ] unless ; - -: delete-gl-shader ( shader -- ) glDeleteShader ; inline - -PREDICATE: integer gl-shader (gl-shader?) ; -PREDICATE: gl-shader vertex-shader (vertex-shader?) ; -PREDICATE: gl-shader fragment-shader (fragment-shader?) ; - -! Programs - -: ( shaders -- program ) - glCreateProgram swap - [ dupd glAttachShader ] each - [ glLinkProgram ] keep - gl-error ; - -: (gl-program?) ( object -- ? ) - dup integer? [ glIsProgram c-true? ] [ drop f ] if ; - -: gl-program-get-int ( program enum -- value ) - 0 [ glGetProgramiv ] keep *int ; - -: gl-program-ok? ( program -- ? ) - GL_LINK_STATUS gl-program-get-int c-true? ; - -: gl-program-info-log-length ( program -- log-length ) - GL_INFO_LOG_LENGTH gl-program-get-int ; inline - -: gl-program-info-log ( program -- log ) - dup gl-program-info-log-length dup [ - [ 0 swap glGetProgramInfoLog ] keep - alien>char-string - ] with-malloc ; - -: check-gl-program ( program -- program* ) - dup gl-program-ok? [ dup gl-program-info-log throw ] unless ; - -: gl-program-shaders-length ( program -- shaders-length ) - GL_ATTACHED_SHADERS gl-program-get-int ; inline - -: gl-program-shaders ( program -- shaders ) - dup gl-program-shaders-length [ - dup "GLuint" - [ 0 swap glGetAttachedShaders ] keep - ] keep c-uint-array> ; - -: delete-gl-program-only ( program -- ) - glDeleteProgram ; inline - -: detach-gl-program-shader ( program shader -- ) - glDetachShader ; inline - -: delete-gl-program ( program -- ) - dup gl-program-shaders [ - 2dup detach-gl-program-shader delete-gl-shader - ] each delete-gl-program-only ; - -: (with-gl-program) ( program quot -- ) - swap glUseProgram [ 0 glUseProgram ] [ ] cleanup ; inline - -: (with-gl-program-uniforms) ( uniforms -- quot ) - [ [ swap , \ glGetUniformLocation , % ] [ ] make ] - { } assoc>map ; -: (make-with-gl-program) ( uniforms quot -- q ) - [ - \ dup , - [ swap (with-gl-program-uniforms) , \ call-with , % ] - [ ] make , - \ (with-gl-program) , - ] [ ] make ; - -MACRO: with-gl-program ( uniforms quot -- ) - (make-with-gl-program) ; - -PREDICATE: integer gl-program (gl-program?) ; - -: ( vertex-shader-source fragment-shader-source -- program ) - >r check-gl-shader - r> check-gl-shader - 2array check-gl-program ; - -: (require-gl) ( thing require-quot make-error-quot -- ) - >r dupd call - [ r> 2drop ] - [ r> " " make throw ] - if ; inline - -: gl-extensions ( -- seq ) - GL_EXTENSIONS glGetString " " split ; -: has-gl-extensions? ( extensions -- ? ) - gl-extensions swap [ over member? ] all? nip ; -: (make-gl-extensions-error) ( required-extensions -- ) - gl-extensions swap seq-diff - "Required OpenGL extensions not supported:\n" % - [ " " % % "\n" % ] each ; -: require-gl-extensions ( extensions -- ) - [ has-gl-extensions? ] - [ (make-gl-extensions-error) ] - (require-gl) ; - -: version-seq ( version-string -- version-seq ) - "." split [ string>number ] map ; - -: version<=> ( version1 version2 -- n ) - swap version-seq swap version-seq <=> ; - -: (gl-version) ( -- version vendor ) - GL_VERSION glGetString " " split1 ; -: gl-version ( -- version ) - (gl-version) drop ; -: gl-vendor-version ( -- version ) - (gl-version) nip ; -: has-gl-version? ( version -- ? ) - gl-version version<=> 0 <= ; -: (make-gl-version-error) ( required-version -- ) - "Required OpenGL version " % % " not supported (" % gl-version % " available)" % ; -: require-gl-version ( version -- ) - [ has-gl-version? ] - [ (make-gl-version-error) ] - (require-gl) ; - -: (glsl-version) ( -- version vendor ) - GL_SHADING_LANGUAGE_VERSION glGetString " " split1 ; -: glsl-version ( -- version ) - (glsl-version) drop ; -: glsl-vendor-version ( -- version ) - (glsl-version) nip ; -: has-glsl-version? ( version -- ? ) - glsl-version version<=> 0 <= ; -: require-glsl-version ( version -- ) - [ has-glsl-version? ] - [ "Required GLSL version " % % " not supported (" % glsl-version % " available)" % ] - (require-gl) ; - -: has-gl-version-or-extensions? ( version extensions -- ? ) - has-gl-extensions? swap has-gl-version? or ; - -: require-gl-version-or-extensions ( version extensions -- ) - 2array [ first2 has-gl-version-or-extensions? ] [ - dup first (make-gl-version-error) "\n" % - second (make-gl-extensions-error) "\n" % - ] (require-gl) ; diff --git a/extra/opengl/shaders/authors.txt b/extra/opengl/shaders/authors.txt new file mode 100644 index 0000000000..6a0dc7293a --- /dev/null +++ b/extra/opengl/shaders/authors.txt @@ -0,0 +1 @@ +Joe Groff \ No newline at end of file diff --git a/extra/opengl/shaders/shaders-docs.factor b/extra/opengl/shaders/shaders-docs.factor new file mode 100644 index 0000000000..e065367323 --- /dev/null +++ b/extra/opengl/shaders/shaders-docs.factor @@ -0,0 +1,112 @@ +USING: help.markup help.syntax io kernel math quotations +opengl.gl multiline assocs ; +IN: opengl.shaders + +HELP: gl-shader +{ $class-description { $snippet "gl-shader" } " is a predicate class comprising values returned by OpenGL to represent shader objects. The following words are provided for creating and manipulating these objects:" + { $list + { { $link } " - Compile GLSL code into a shader object" } + { { $link gl-shader-ok? } " - Check whether a shader object compiled successfully" } + { { $link check-gl-shader } " - Throw an error unless a shader object compiled successfully" } + { { $link gl-shader-info-log } " - Retrieve the info log of messages generated by the GLSL compiler" } + { { $link delete-gl-shader } " - Invalidate a shader object" } + } + "The derived predicate classes " { $link vertex-shader } " and " { $link fragment-shader } " are also defined for the two standard kinds of shader defined by the OpenGL specification." } ; + +HELP: vertex-shader +{ $class-description { $snippet "vertex-shader" } " is the predicate class of " { $link gl-shader } " objects that refer to shaders of type " { $snippet "GL_VERTEX_SHADER" } ". In addition to the " { $snippet "gl-shader" } " words, the following vertex shader-specific functions are defined:" + { $list + { { $link } " - Compile GLSL code into a vertex shader object "} + } +} ; + +HELP: fragment-shader +{ $class-description { $snippet "fragment-shader" } " is the predicate class of " { $link gl-shader } " objects that refer to shaders of type " { $snippet "GL_FRAGMENT_SHADER" } ". In addition to the " { $snippet "gl-shader" } " words, the following fragment shader-specific functions are defined:" + { $list + { { $link } " - Compile GLSL code into a fragment shader object "} + } +} ; + +HELP: +{ $values { "source" "The GLSL source code to compile" } { "kind" "The kind of shader to compile, such as " { $snippet "GL_VERTEX_SHADER" } " or " { $snippet "GL_FRAGMENT_SHADER" } } } +{ $description "Tries to compile the given GLSL source into a shader object. The returned object can be checked for validity by " { $link check-gl-shader } " or " { $link gl-shader-ok? } ". Errors and warnings generated by the GLSL compiler will be collected in the info log, available from " { $link gl-shader-info-log } ".\n\nWhen the shader object is no longer needed, it should be deleted using " { $link delete-gl-shader } " or else be attached to a " { $link gl-program } " object deleted using " { $link delete-gl-program } "." } ; + +HELP: +{ $values { "source" "The GLSL source code to compile" } } +{ $description "Tries to compile the given GLSL source into a vertex shader object. Equivalent to " { $snippet "GL_VERTEX_SHADER " } "." } ; + +HELP: +{ $values { "source" "The GLSL source code to compile" } } +{ $description "Tries to compile the given GLSL source into a fragment shader object. Equivalent to " { $snippet "GL_FRAGMENT_SHADER " } "." } ; + +HELP: gl-shader-ok? +{ $values { "shader" "A " { $link gl-shader } " object" } } +{ $description "Returns a boolean value indicating whether the given shader object compiled successfully. Compilation errors and warnings are available in the shader's info log, which can be gotten using " { $link gl-shader-info-log } "." } ; + +HELP: check-gl-shader +{ $values { "shader" "A " { $link gl-shader } " object" } } +{ $description "Throws an error containing the " { $link gl-shader-info-log } " for the shader object if it failed to compile. Otherwise, the shader object is left on the stack." } ; + +HELP: delete-gl-shader +{ $values { "shader" "A " { $link gl-shader } " object" } } +{ $description "Deletes the shader object, invalidating it and releasing any resources allocated for it by the OpenGL implementation." } ; + +HELP: gl-shader-info-log +{ $values { "shader" "A " { $link gl-shader } " object" } } +{ $description "Retrieves the info log for " { $snippet "shader" } ", including any errors or warnings generated in compiling the shader object." } ; + +HELP: gl-program +{ $class-description { $snippet "gl-program" } " is a predicate class comprising values returned by OpenGL to represent proram objects. The following words are provided for creating and manipulating these objects:" + { $list + { { $link } ", " { $link } " - Link a set of shaders into a GLSL program" } + { { $link gl-program-ok? } " - Check whether a program object linked successfully" } + { { $link check-gl-program } " - Throw an error unless a program object linked successfully" } + { { $link gl-program-info-log } " - Retrieve the info log of messages generated by the GLSL linker" } + { { $link gl-program-shaders } " - Retrieve the set of shader objects composing the GLSL program" } + { { $link delete-gl-program } " - Invalidate a program object and all its attached shaders" } + { { $link with-gl-program } " - Use a program object" } + } +} ; + +HELP: +{ $values { "shaders" "A sequence of " { $link gl-shader } " objects." } } +{ $description "Creates a new GLSL program object, attaches all the shader objects in the " { $snippet "shaders" } " sequence, and attempts to link them. The returned object can be checked for validity by " { $link check-gl-program } " or " { $link gl-program-ok? } ". Errors and warnings generated by the GLSL linker will be collected in the info log, available from " { $link gl-program-info-log } ".\n\nWhen the program object and its attached shaders are no longer needed, it should be deleted using " { $link delete-gl-program } "." } ; + +HELP: +{ $values { "vertex-shader-source" "A string containing GLSL vertex shader source" } { "fragment-shader-source" "A string containing GLSL fragment shader source" } } +{ $description "Wrapper for " { $link } " for the simple case of compiling a single vertex shader and fragment shader and linking them into a GLSL program. Throws an exception if compiling or linking fails." } ; + +{ } related-words + +HELP: gl-program-ok? +{ $values { "program" "A " { $link gl-program } " object" } } +{ $description "Returns a boolean value indicating whether the given program object linked successfully. Link errors and warnings are available in the program's info log, which can be gotten using " { $link gl-program-info-log } "." } ; + +HELP: check-gl-program +{ $values { "program" "A " { $link gl-program } " object" } } +{ $description "Throws an error containing the " { $link gl-program-info-log } " for the program object if it failed to link. Otherwise, the program object is left on the stack." } ; + +HELP: gl-program-info-log +{ $values { "program" "A " { $link gl-program } " object" } } +{ $description "Retrieves the info log for " { $snippet "program" } ", including any errors or warnings generated in linking the program object." } ; + +HELP: delete-gl-program +{ $values { "program" "A " { $link gl-program } " object" } } +{ $description "Deletes the program object, invalidating it and releasing any resources allocated for it by the OpenGL implementation. Any attached " { $link gl-shader } "s are also deleted.\n\nIf the shader objects should be preserved, they should each be detached using " { $link detach-gl-program-shader } ". The program object can then be destroyed alone using " { $link delete-gl-program-only } "." } ; + +HELP: with-gl-program +{ $values { "program" "A " { $link gl-program } " object" } { "uniforms" "An " { $link assoc } " between uniform parameter names and quotations with effect " { $snippet "( uniform-location -- )" } } { "quot" "A quotation" } } +{ $description "Enables " { $snippet "program" } " for all OpenGL calls made in the dynamic extent of " { $snippet "quot" } ". The fixed-function pipeline is restored at the end of " { $snippet "quot" } ". Before calling " { $snippet "quot" } ", calls " { $link glGetUniformLocation } " on each key of " { $snippet "uniforms" } " to get the address of the uniform parameter, which is then placed on top of the stack as the associated quotation is called.\n\nExample:" } +{ $code <" +! From bunny.cel-shaded +: (draw-cel-shaded-bunny) ( geom program -- ) + { + { "light_direction" [ 1.0 -1.0 1.0 glUniform3f ] } + { "color" [ 0.6 0.5 0.5 1.0 glUniform4f ] } + { "ambient" [ 0.2 0.2 0.2 0.2 glUniform4f ] } + { "diffuse" [ 0.8 0.8 0.8 0.8 glUniform4f ] } + { "shininess" [ 100.0 glUniform1f ] } + } [ bunny-geom ] with-gl-program ; +"> } ; + +ABOUT: "gl-utilities" diff --git a/extra/opengl/shaders/shaders.factor b/extra/opengl/shaders/shaders.factor new file mode 100644 index 0000000000..0ff708d6d4 --- /dev/null +++ b/extra/opengl/shaders/shaders.factor @@ -0,0 +1,134 @@ +! Copyright (C) 2008 Joe Groff. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel opengl.gl alien.c-types continuations namespaces +assocs alien libc opengl math sequences combinators.lib +macros arrays ; +IN: opengl.shaders + +: with-gl-shader-source-ptr ( string quot -- ) + swap string>char-alien malloc-byte-array [ + swap call + ] keep free ; inline + +: ( source kind -- shader ) + glCreateShader dup rot + [ 1 swap f glShaderSource ] with-gl-shader-source-ptr + [ glCompileShader ] keep + gl-error ; + +: (gl-shader?) ( object -- ? ) + dup integer? [ glIsShader c-bool> ] [ drop f ] if ; + +: gl-shader-get-int ( shader enum -- value ) + 0 [ glGetShaderiv ] keep *int ; + +: gl-shader-ok? ( shader -- ? ) + GL_COMPILE_STATUS gl-shader-get-int c-bool> ; + +: ( source -- vertex-shader ) + GL_VERTEX_SHADER ; inline + +: (vertex-shader?) ( object -- ? ) + dup (gl-shader?) + [ GL_SHADER_TYPE gl-shader-get-int GL_VERTEX_SHADER = ] + [ drop f ] if ; + +: ( source -- fragment-shader ) + GL_FRAGMENT_SHADER ; inline + +: (fragment-shader?) ( object -- ? ) + dup (gl-shader?) + [ GL_SHADER_TYPE gl-shader-get-int GL_FRAGMENT_SHADER = ] + [ drop f ] if ; + +: gl-shader-info-log-length ( shader -- log-length ) + GL_INFO_LOG_LENGTH gl-shader-get-int ; inline + +: gl-shader-info-log ( shader -- log ) + dup gl-shader-info-log-length dup [ + [ 0 swap glGetShaderInfoLog ] keep + alien>char-string + ] with-malloc ; + +: check-gl-shader ( shader -- shader* ) + dup gl-shader-ok? [ dup gl-shader-info-log throw ] unless ; + +: delete-gl-shader ( shader -- ) glDeleteShader ; inline + +PREDICATE: integer gl-shader (gl-shader?) ; +PREDICATE: gl-shader vertex-shader (vertex-shader?) ; +PREDICATE: gl-shader fragment-shader (fragment-shader?) ; + +! Programs + +: ( shaders -- program ) + glCreateProgram swap + [ dupd glAttachShader ] each + [ glLinkProgram ] keep + gl-error ; + +: (gl-program?) ( object -- ? ) + dup integer? [ glIsProgram c-bool> ] [ drop f ] if ; + +: gl-program-get-int ( program enum -- value ) + 0 [ glGetProgramiv ] keep *int ; + +: gl-program-ok? ( program -- ? ) + GL_LINK_STATUS gl-program-get-int c-bool> ; + +: gl-program-info-log-length ( program -- log-length ) + GL_INFO_LOG_LENGTH gl-program-get-int ; inline + +: gl-program-info-log ( program -- log ) + dup gl-program-info-log-length dup [ + [ 0 swap glGetProgramInfoLog ] keep + alien>char-string + ] with-malloc ; + +: check-gl-program ( program -- program* ) + dup gl-program-ok? [ dup gl-program-info-log throw ] unless ; + +: gl-program-shaders-length ( program -- shaders-length ) + GL_ATTACHED_SHADERS gl-program-get-int ; inline + +: gl-program-shaders ( program -- shaders ) + dup gl-program-shaders-length [ + dup "GLuint" + [ 0 swap glGetAttachedShaders ] keep + ] keep c-uint-array> ; + +: delete-gl-program-only ( program -- ) + glDeleteProgram ; inline + +: detach-gl-program-shader ( program shader -- ) + glDetachShader ; inline + +: delete-gl-program ( program -- ) + dup gl-program-shaders [ + 2dup detach-gl-program-shader delete-gl-shader + ] each delete-gl-program-only ; + +: (with-gl-program) ( program quot -- ) + swap glUseProgram [ 0 glUseProgram ] [ ] cleanup ; inline + +: (with-gl-program-uniforms) ( uniforms -- quot ) + [ [ swap , \ glGetUniformLocation , % ] [ ] make ] + { } assoc>map ; +: (make-with-gl-program) ( uniforms quot -- q ) + [ + \ dup , + [ swap (with-gl-program-uniforms) , \ call-with , % ] + [ ] make , + \ (with-gl-program) , + ] [ ] make ; + +MACRO: with-gl-program ( uniforms quot -- ) + (make-with-gl-program) ; + +PREDICATE: integer gl-program (gl-program?) ; + +: ( vertex-shader-source fragment-shader-source -- program ) + >r check-gl-shader + r> check-gl-shader + 2array check-gl-program ; + diff --git a/extra/opengl/shaders/summary.txt b/extra/opengl/shaders/summary.txt new file mode 100644 index 0000000000..c55f76668f --- /dev/null +++ b/extra/opengl/shaders/summary.txt @@ -0,0 +1 @@ +OpenGL Shading Language (GLSL) support \ No newline at end of file diff --git a/extra/opengl/shaders/tags.txt b/extra/opengl/shaders/tags.txt new file mode 100644 index 0000000000..ce0345edc9 --- /dev/null +++ b/extra/opengl/shaders/tags.txt @@ -0,0 +1,3 @@ +opengl +glsl +bindings \ No newline at end of file From 3bc9790b740dd8d989b9b9146f01d472418c4116 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sun, 3 Feb 2008 16:19:05 -0800 Subject: [PATCH 062/269] Adjust the bunny position to be better centered --- extra/bunny/bunny.factor | 2 +- extra/bunny/model/model.factor | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/bunny/bunny.factor b/extra/bunny/bunny.factor index 38f8e32fb6..7cf6132925 100755 --- a/extra/bunny/bunny.factor +++ b/extra/bunny/bunny.factor @@ -52,7 +52,7 @@ M: bunny-gadget draw-gadget* ( gadget -- ) GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear dup demo-gadget-set-matrices GL_MODELVIEW glMatrixMode - 0.0 -0.12 0.0 glTranslatef + 0.02 -0.105 0.0 glTranslatef { bunny-gadget-geom bunny-gadget-draw } get-slots draw-bunny ; diff --git a/extra/bunny/model/model.factor b/extra/bunny/model/model.factor index f2c93eac3e..b238bd8b99 100644 --- a/extra/bunny/model/model.factor +++ b/extra/bunny/model/model.factor @@ -92,7 +92,7 @@ M: bunny-buffers bunny-geom bunny-buffers-array bunny-buffers-element-array } get-slots [ - GL_VERTEX_ARRAY GL_NORMAL_ARRAY 2array [ + { GL_VERTEX_ARRAY GL_NORMAL_ARRAY } [ GL_DOUBLE 0 0 buffer-offset glNormalPointer dup bunny-buffers-nv "double" heap-size * buffer-offset 3 GL_DOUBLE 0 roll glVertexPointer From d0e5b238e2c056500e4bab055b404eac04ad7a52 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 3 Feb 2008 20:36:04 -0600 Subject: [PATCH 063/269] Use new feature --- extra/tools/deploy/backend/backend.factor | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/extra/tools/deploy/backend/backend.factor b/extra/tools/deploy/backend/backend.factor index f2bd03475f..d768b6a334 100755 --- a/extra/tools/deploy/backend/backend.factor +++ b/extra/tools/deploy/backend/backend.factor @@ -16,8 +16,11 @@ IN: tools.deploy.backend : copy-lines ( stream -- ) [ (copy-lines) ] with-disposal ; -: run-with-output ( descriptor -- ) - +: run-with-output ( arguments -- ) + [ + +arguments+ set + +stdout+ +stderr+ set + ] H{ } make-assoc dup duplex-stream-out dispose copy-lines ; From e36bddd91c8929a238ed0b9679e49d9cb83ad584 Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Sun, 3 Feb 2008 22:11:31 -0500 Subject: [PATCH 064/269] Solution to Project Euler problem 52 --- extra/project-euler/052/052.factor | 50 ++++++++++++++++++++++++ extra/project-euler/project-euler.factor | 6 +-- 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 extra/project-euler/052/052.factor diff --git a/extra/project-euler/052/052.factor b/extra/project-euler/052/052.factor new file mode 100644 index 0000000000..3f6487fb3e --- /dev/null +++ b/extra/project-euler/052/052.factor @@ -0,0 +1,50 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: combinators.lib kernel math project-euler.common sequences sorting ; +IN: project-euler.052 + +! http://projecteuler.net/index.php?section=problems&id=52 + +! DESCRIPTION +! ----------- + +! It can be seen that the number, 125874, and its double, 251748, contain +! exactly the same digits, but in a different order. + +! Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, +! contain the same digits. + + +! SOLUTION +! -------- + +! Analysis shows the number must be odd, divisible by 3, and larger than 123456 + +digits natural-sort ] map all-equal? ; + +: candidate? ( n -- ? ) + { [ dup odd? ] [ dup 3 mod zero? ] } && nip ; + +: next-all-same ( x n -- n ) + dup candidate? [ + 2dup swap map-nx all-same-digits? + [ nip ] [ 1+ next-all-same ] if + ] [ + 1+ next-all-same + ] if ; + +PRIVATE> + +: euler052 ( -- answer ) + 6 123456 next-all-same ; + +! [ euler052 ] 100 ave-time +! 403 ms run / 7 ms GC ave time - 100 trials + +MAIN: euler052 diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index 226c47b0a3..2f8a3184bb 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -12,9 +12,9 @@ USING: definitions io io.files kernel math math.parser project-euler.ave-time project-euler.029 project-euler.030 project-euler.031 project-euler.032 project-euler.033 project-euler.034 project-euler.035 project-euler.036 project-euler.037 project-euler.038 project-euler.039 project-euler.040 - project-euler.041 project-euler.042 project-euler.048 project-euler.067 - project-euler.075 project-euler.134 project-euler.169 project-euler.173 - project-euler.175 ; + project-euler.041 project-euler.042 project-euler.048 project-euler.052 + project-euler.067 project-euler.075 project-euler.134 project-euler.169 + project-euler.173 project-euler.175 ; IN: project-euler Date: Mon, 4 Feb 2008 01:40:47 -0500 Subject: [PATCH 065/269] Solution to Project Euler problem 97 --- extra/project-euler/097/097.factor | 31 ++++++++++++++++++++++++ extra/project-euler/project-euler.factor | 4 +-- 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 extra/project-euler/097/097.factor diff --git a/extra/project-euler/097/097.factor b/extra/project-euler/097/097.factor new file mode 100644 index 0000000000..50e7af563d --- /dev/null +++ b/extra/project-euler/097/097.factor @@ -0,0 +1,31 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: math math.functions ; +IN: project-euler.097 + +! http://projecteuler.net/index.php?section=problems&id=97 + +! DESCRIPTION +! ----------- + +! The first known prime found to exceed one million digits was discovered in +! 1999, and is a Mersenne prime of the form 2^6972593 − 1; it contains exactly +! 2,098,960 digits. Subsequently other Mersenne primes, of the form 2p − 1, +! have been found which contain more digits. + +! However, in 2004 there was found a massive non-Mersenne prime which contains +! 2,357,207 digits: 28433 * 2^7830457 + 1. + +! Find the last ten digits of this prime number. + + +! SOLUTION +! -------- + +: euler097 ( -- answer ) + 2 7830457 10 10 ^ ^mod 28433 * 10 10 ^ mod 1+ ; + +! [ euler097 ] 100 ave-time +! 0 ms run / 0 ms GC ave time - 100 trials + +MAIN: euler097 diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index 2f8a3184bb..0be0b456ad 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -13,8 +13,8 @@ USING: definitions io io.files kernel math math.parser project-euler.ave-time project-euler.033 project-euler.034 project-euler.035 project-euler.036 project-euler.037 project-euler.038 project-euler.039 project-euler.040 project-euler.041 project-euler.042 project-euler.048 project-euler.052 - project-euler.067 project-euler.075 project-euler.134 project-euler.169 - project-euler.173 project-euler.175 ; + project-euler.067 project-euler.075 project-euler.097 project-euler.134 + project-euler.169 project-euler.173 project-euler.175 ; IN: project-euler Date: Mon, 4 Feb 2008 01:49:31 -0500 Subject: [PATCH 066/269] Add missing dependency for Project Euler 42 --- extra/project-euler/042/042.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/project-euler/042/042.factor b/extra/project-euler/042/042.factor index 3d5f271374..95b3062e95 100644 --- a/extra/project-euler/042/042.factor +++ b/extra/project-euler/042/042.factor @@ -1,6 +1,6 @@ ! Copyright (c) 2008 Aaron Schaefer. ! See http://factorcode.org/license.txt for BSD license. -USING: ascii combinators.lib io.files kernel math namespaces +USING: ascii combinators.lib io.files kernel math math.functions namespaces project-euler.common sequences splitting ; IN: project-euler.042 From e2c20d23a4856580e1d8eabf2b57a8d6b5d78d0d Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 11:06:11 -0600 Subject: [PATCH 067/269] add missing use fix dll path on windows --- extra/ogg/theora/theora.factor | 2 +- extra/ogg/vorbis/vorbis.factor | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/ogg/theora/theora.factor b/extra/ogg/theora/theora.factor index 0d9748a6f3..48b61b41a3 100644 --- a/extra/ogg/theora/theora.factor +++ b/extra/ogg/theora/theora.factor @@ -6,7 +6,7 @@ IN: ogg.theora << "theora" { - { [ win32? ] [ "libtheora.dll" ] } + { [ win32? ] [ "theora.dll" ] } { [ macosx? ] [ "libtheora.0.dylib" ] } { [ unix? ] [ "libtheora.so" ] } } cond "cdecl" add-library diff --git a/extra/ogg/vorbis/vorbis.factor b/extra/ogg/vorbis/vorbis.factor index 26e917ebf4..170d0ea6ef 100644 --- a/extra/ogg/vorbis/vorbis.factor +++ b/extra/ogg/vorbis/vorbis.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2007 Chris Double. ! See http://factorcode.org/license.txt for BSD license. ! -USING: kernel system combinators alien alien.syntax ; +USING: kernel system combinators alien alien.syntax ogg ; IN: ogg.vorbis << From bc3bf6b2b4ede72aa4332dd3f7b98cd85f836756 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 11:45:53 -0600 Subject: [PATCH 068/269] make factor compile on win64 --- Makefile | 6 +++++- vm/Config.windows.nt.x86.32 | 1 + vm/Config.windows.nt.x86.64 | 6 ++++-- vm/os-windows-nt.32.h | 2 ++ vm/os-windows-nt.64.h | 2 ++ vm/os-windows-nt.c | 10 +++++----- vm/platform.h | 9 +++++++-- 7 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 vm/os-windows-nt.32.h create mode 100644 vm/os-windows-nt.64.h diff --git a/Makefile b/Makefile index aad7fe90eb..bd1bf16c74 100755 --- a/Makefile +++ b/Makefile @@ -65,6 +65,7 @@ default: @echo "solaris-x86-64" @echo "windows-ce-arm" @echo "windows-nt-x86-32" + @echo "windows-nt-x86-64" @echo "" @echo "Additional modifiers:" @echo "" @@ -125,6 +126,9 @@ solaris-x86-64: windows-nt-x86-32: $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.32 +windows-nt-x86-64: + $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.64 + windows-ce-arm: $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.ce.arm @@ -151,7 +155,7 @@ clean: rm -f factor*.dll libfactor*.* vm/resources.o: - windres vm/factor.rs vm/resources.o + $(WINDRES) vm/factor.rs vm/resources.o .c.o: $(CC) -c $(CFLAGS) -o $@ $< diff --git a/vm/Config.windows.nt.x86.32 b/vm/Config.windows.nt.x86.32 index 9a020a7bc1..603a7200ae 100644 --- a/vm/Config.windows.nt.x86.32 +++ b/vm/Config.windows.nt.x86.32 @@ -1,3 +1,4 @@ WINDRES=windres include vm/Config.windows.nt include vm/Config.x86.32 +#error "lolllll" diff --git a/vm/Config.windows.nt.x86.64 b/vm/Config.windows.nt.x86.64 index 1c30e64096..6d3865c2f4 100644 --- a/vm/Config.windows.nt.x86.64 +++ b/vm/Config.windows.nt.x86.64 @@ -1,4 +1,6 @@ -CC=/k/target/bin/x86_64-pc-mingw32-gcc +#WIN64_PATH=/k/MinGW/win64/bin +WIN64_PATH=/cygdrive/k/MinGW/win64/bin/x86_64-pc-mingw32 +CC=$(WIN64_PATH)-gcc.exe +WINDRES=$(WIN64_PATH)-windres.exe include vm/Config.windows.nt include vm/Config.x86.64 -WINDRES = /k/target/bin/windres diff --git a/vm/os-windows-nt.32.h b/vm/os-windows-nt.32.h new file mode 100644 index 0000000000..9b10671ba0 --- /dev/null +++ b/vm/os-windows-nt.32.h @@ -0,0 +1,2 @@ +#define ESP Esp +#define EIP Eip diff --git a/vm/os-windows-nt.64.h b/vm/os-windows-nt.64.h new file mode 100644 index 0000000000..1f61c2335f --- /dev/null +++ b/vm/os-windows-nt.64.h @@ -0,0 +1,2 @@ +#define ESP Rsp +#define EIP Rip diff --git a/vm/os-windows-nt.c b/vm/os-windows-nt.c index e356c2f674..3995b6a35a 100755 --- a/vm/os-windows-nt.c +++ b/vm/os-windows-nt.c @@ -57,26 +57,26 @@ long exception_handler(PEXCEPTION_POINTERS pe) PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord; CONTEXT *c = (CONTEXT*)pe->ContextRecord; - if(in_code_heap_p(c->Eip)) - signal_callstack_top = (void *)c->Esp; + if(in_code_heap_p(c->EIP)) + signal_callstack_top = (void *)c->ESP; else signal_callstack_top = NULL; if(e->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { signal_fault_addr = e->ExceptionInformation[1]; - c->Eip = (CELL)memory_signal_handler_impl; + c->EIP = (CELL)memory_signal_handler_impl; } else if(e->ExceptionCode == EXCEPTION_FLT_DIVIDE_BY_ZERO || e->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO) { signal_number = ERROR_DIVIDE_BY_ZERO; - c->Eip = (CELL)divide_by_zero_signal_handler_impl; + c->EIP = (CELL)divide_by_zero_signal_handler_impl; } else { signal_number = 11; - c->Eip = (CELL)misc_signal_handler_impl; + c->EIP = (CELL)misc_signal_handler_impl; } return EXCEPTION_CONTINUE_EXECUTION; diff --git a/vm/platform.h b/vm/platform.h index b0641176bc..66f22bbf96 100644 --- a/vm/platform.h +++ b/vm/platform.h @@ -1,11 +1,11 @@ #if defined(__arm__) #define FACTOR_ARM +#elif defined(__amd64__) || defined(__x86_64__) + #define FACTOR_AMD64 #elif defined(i386) || defined(__i386) || defined(__i386__) || defined(WIN32) #define FACTOR_X86 #elif defined(__POWERPC__) || defined(__ppc__) || defined(_ARCH_PPC) #define FACTOR_PPC -#elif defined(__amd64__) || defined(__x86_64__) - #define FACTOR_AMD64 #else #error "Unsupported architecture" #endif @@ -18,6 +18,11 @@ #endif #include "os-windows.h" + #if defined(FACTOR_AMD64) + #include "os-windows-nt.64.h" + #elif defined(FACTOR_X86) + #include "os-windows-nt.32.h" + #endif #else #include "os-unix.h" From 46e02fa30d45f23fe98aed2ff4233fa0eba26415 Mon Sep 17 00:00:00 2001 From: sheeple Date: Mon, 4 Feb 2008 11:50:02 -0600 Subject: [PATCH 069/269] Linux inotify works --- extra/io/monitor/monitor.factor | 4 ++-- extra/io/unix/linux/linux.factor | 27 +++++++++++----------- extra/io/windows/nt/monitor/monitor.factor | 7 +++--- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/extra/io/monitor/monitor.factor b/extra/io/monitor/monitor.factor index 11d1b6ecf9..1d8499b392 100755 --- a/extra/io/monitor/monitor.factor +++ b/extra/io/monitor/monitor.factor @@ -17,7 +17,7 @@ TUPLE: monitor queue closed? ; set-monitor-queue } monitor construct ; -HOOK: fill-queue io-backend ( monitor -- assoc ) +HOOK: fill-queue io-backend ( monitor -- ) : changed-file ( changed path -- ) namespace [ append ] change-at ; @@ -32,7 +32,7 @@ HOOK: io-backend ( path recursive? -- monitor ) : next-change ( monitor -- path changed ) dup check-monitor dup monitor-queue dup assoc-empty? [ - drop dup fill-queue over set-monitor-queue next-change + drop dup fill-queue next-change ] [ nip dequeue-change ] if ; SYMBOL: +add-file+ diff --git a/extra/io/unix/linux/linux.factor b/extra/io/unix/linux/linux.factor index 9751cefe91..1707ac9546 100755 --- a/extra/io/unix/linux/linux.factor +++ b/extra/io/unix/linux/linux.factor @@ -54,21 +54,22 @@ TUPLE: inotify watches ; M: linux-io ( path recursive? -- monitor ) drop IN_CHANGE_EVENTS add-watch ; -: notify-callback ( assoc monitor -- ) - linux-monitor-callback dup - [ schedule-thread-with ] [ 2drop ] if ; +: notify-callback ( monitor -- ) + dup linux-monitor-callback + f rot set-linux-monitor-callback + [ schedule-thread ] when* ; -M: linux-io fill-queue ( monitor -- assoc ) +M: linux-io fill-queue ( monitor -- ) dup linux-monitor-callback [ "Cannot wait for changes on the same file from multiple threads" throw ] when - [ swap set-linux-monitor-callback stop ] callcc1 - swap check-monitor ; + [ swap set-linux-monitor-callback stop ] callcc0 + check-monitor ; M: linux-monitor dispose ( monitor -- ) dup check-monitor t over set-monitor-closed? - H{ } over notify-callback + dup notify-callback remove-watch ; : ?flag ( n mask symbol -- n ) @@ -106,13 +107,13 @@ M: linux-monitor dispose ( monitor -- ) inotify-event-len "inotify-event" heap-size + swap >r + r> ; -: wd>queue ( wd -- queue ) - inotify-event-wd wd>monitor monitor-queue ; - : parse-file-notifications ( i buffer -- ) 2dup events-exhausted? [ 2drop ] [ - 2dup inotify-event@ dup inotify-event-wd wd>queue - [ parse-file-notify changed-file ] bind + 2dup inotify-event@ dup inotify-event-wd wd>monitor [ + monitor-queue [ + parse-file-notify changed-file + ] bind + ] keep notify-callback next-event parse-file-notifications ] if ; @@ -135,7 +136,7 @@ M: inotify-task do-io-task ( task -- ) io-task-port read-notifications f ; M: linux-io init-io ( -- ) - mx set-global ; ! init-inotify ; + dup mx set-global init-inotify ; T{ linux-io } set-io-backend diff --git a/extra/io/windows/nt/monitor/monitor.factor b/extra/io/windows/nt/monitor/monitor.factor index f2cc4ef92a..d418dff270 100755 --- a/extra/io/windows/nt/monitor/monitor.factor +++ b/extra/io/windows/nt/monitor/monitor.factor @@ -78,6 +78,7 @@ M: windows-nt-io ( path recursive? -- monitor ) dup FILE_NOTIFY_INFORMATION-NextEntryOffset dup zero? [ 3drop ] [ swap (changed-files) ] if ; -M: windows-nt-io fill-queue ( monitor -- assoc ) - dup win32-monitor-path over buffer-ptr rot read-changes - [ zero? [ 2drop ] [ (changed-files) ] if ] H{ } make-assoc ; +M: windows-nt-io fill-queue ( monitor -- ) + dup win32-monitor-path over buffer-ptr pick read-changes + [ zero? [ 2drop ] [ (changed-files) ] if ] H{ } make-assoc + swap set-monitor-queue ; From f2af000ed040468ed6377ad526c461fbff66b6af Mon Sep 17 00:00:00 2001 From: sheeple Date: Mon, 4 Feb 2008 11:50:20 -0600 Subject: [PATCH 070/269] refresh-all fix, new show word for debugging --- core/io/crc32/crc32-docs.factor | 10 +++++----- core/io/crc32/crc32.factor | 2 -- core/io/streams/c/c.factor | 7 +++++++ core/source-files/source-files.factor | 2 +- extra/tools/deploy/shaker/shaker.factor | 5 ----- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/core/io/crc32/crc32-docs.factor b/core/io/crc32/crc32-docs.factor index 020f2668b0..3855c77cd8 100644 --- a/core/io/crc32/crc32-docs.factor +++ b/core/io/crc32/crc32-docs.factor @@ -2,16 +2,16 @@ USING: help.markup help.syntax math ; IN: io.crc32 HELP: crc32 -{ $values { "seq" "a sequence" } { "n" integer } } +{ $values { "seq" "a sequence of bytes" } { "n" integer } } { $description "Computes the CRC32 checksum of a sequence of bytes." } ; -HELP: file-crc32 -{ $values { "path" "a pathname string" } { "n" integer } } -{ $description "Computes the CRC32 checksum of a file's contents." } ; +HELP: lines-crc32 +{ $values { "lines" "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 file-crc32 } ; +{ $subsection lines-crc32 } ; ABOUT: "io.crc32" diff --git a/core/io/crc32/crc32.factor b/core/io/crc32/crc32.factor index b83943df48..afe7e4bfb7 100755 --- a/core/io/crc32/crc32.factor +++ b/core/io/crc32/crc32.factor @@ -23,8 +23,6 @@ IN: io.crc32 : crc32 ( seq -- n ) >r HEX: ffffffff dup r> [ (crc32) ] each bitxor ; -: file-crc32 ( path -- n ) file-contents crc32 ; - : lines-crc32 ( seq -- n ) HEX: ffffffff tuck [ [ (crc32) ] each CHAR: \n (crc32) diff --git a/core/io/streams/c/c.factor b/core/io/streams/c/c.factor index b02c3367d4..288ab212d1 100755 --- a/core/io/streams/c/c.factor +++ b/core/io/streams/c/c.factor @@ -74,3 +74,10 @@ M: object M: object "ab" fopen ; + +: show ( msg -- ) + #! A word which directly calls primitives. It is used to + #! print stuff from contexts where the I/O system would + #! otherwise not work (tools.deploy.shaker, the I/O + #! multiplexer thread). + "\r\n" append stdout-handle fwrite stdout-handle fflush ; diff --git a/core/source-files/source-files.factor b/core/source-files/source-files.factor index 8bbf329491..c974145928 100755 --- a/core/source-files/source-files.factor +++ b/core/source-files/source-files.factor @@ -17,7 +17,7 @@ uses definitions ; : (source-modified?) ( path modified checksum -- ? ) pick file-modified rot [ 0 or ] 2apply > - [ swap file-crc32 number= not ] [ 2drop f ] if ; + [ swap file-lines lines-crc32 = not ] [ 2drop f ] if ; : source-modified? ( path -- ? ) dup source-files get at [ diff --git a/extra/tools/deploy/shaker/shaker.factor b/extra/tools/deploy/shaker/shaker.factor index f2b951ad16..16507232ae 100755 --- a/extra/tools/deploy/shaker/shaker.factor +++ b/extra/tools/deploy/shaker/shaker.factor @@ -8,11 +8,6 @@ debugger io.streams.c io.streams.duplex io.files io.backend quotations words.private tools.deploy.config compiler.units ; IN: tools.deploy.shaker -: show ( msg -- ) - #! Use primitives directly so that we can print stuff even - #! after most of the image has been stripped away - "\r\n" append stdout-handle fwrite stdout-handle fflush ; - : strip-init-hooks ( -- ) "Stripping startup hooks" show "command-line" init-hooks get delete-at From 53cb45c9ff3a53ea1cce2679e9a772ea94d3b24a Mon Sep 17 00:00:00 2001 From: sheeple Date: Mon, 4 Feb 2008 12:03:48 -0600 Subject: [PATCH 071/269] Fix TYPEDEF: issue --- extra/unix/linux/linux.factor | 4 +--- extra/unix/solaris/solaris.factor | 2 -- extra/unix/unix.factor | 4 +++- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/extra/unix/linux/linux.factor b/extra/unix/linux/linux.factor index d25ff71d65..0a3eb7ee5f 100644 --- a/extra/unix/linux/linux.factor +++ b/extra/unix/linux/linux.factor @@ -1,10 +1,8 @@ -! Copyright (C) 2005 Slava Pestov. +! Copyright (C) 2005, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. IN: unix USING: alien.syntax ; -TYPEDEF: ulong off_t - ! Linux. : O_RDONLY HEX: 0000 ; inline diff --git a/extra/unix/solaris/solaris.factor b/extra/unix/solaris/solaris.factor index b4aa8285eb..2bca20c6b6 100644 --- a/extra/unix/solaris/solaris.factor +++ b/extra/unix/solaris/solaris.factor @@ -3,8 +3,6 @@ IN: unix USING: alien.syntax system kernel ; -TYPEDEF: ulong off_t - ! Solaris. : O_RDONLY HEX: 0000 ; inline diff --git a/extra/unix/unix.factor b/extra/unix/unix.factor index bcfbb3a214..7c3467b052 100755 --- a/extra/unix/unix.factor +++ b/extra/unix/unix.factor @@ -19,11 +19,13 @@ TYPEDEF: uint time_t TYPEDEF: uint uid_t TYPEDEF: ulong size_t TYPEDEF: ulong u_long -TYPEDEF: ulonglong off_t TYPEDEF: ushort mode_t TYPEDEF: ushort nlink_t TYPEDEF: void* caddr_t +TYPEDEF: ulong off_t +TYPEDEF-IF: bsd? ulonglong off_t + C-STRUCT: tm { "int" "sec" } ! Seconds: 0-59 (K&R says 0-61?) { "int" "min" } ! Minutes: 0-59 From 87d44252c59f0a7d967157b634f10dc83acce442 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 12:30:23 -0600 Subject: [PATCH 072/269] add more dlls to script --- misc/factor.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/misc/factor.sh b/misc/factor.sh index 032b0b3184..02f4c4a542 100755 --- a/misc/factor.sh +++ b/misc/factor.sh @@ -233,6 +233,16 @@ maybe_download_dlls() { check_ret wget wget http://factorcode.org/dlls/zlib1.dll check_ret wget + wget http://factorcode.org/dlls/OpenAL32.dll + check_ret wget + wget http://factorcode.org/dlls/alut.dll + check_ret wget + wget http://factorcode.org/dlls/ogg.dll + check_ret wget + wget http://factorcode.org/dlls/theora.dll + check_ret wget + wget http://factorcode.org/dlls/vorbis.dll + check_ret wget chmod 777 *.dll check_ret chmod fi From a75afb18d71e6ffb2fdedf3787e6190e94b86ef2 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 12:58:38 -0600 Subject: [PATCH 073/269] Fix GCC error --- vm/os-genunix.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/vm/os-genunix.c b/vm/os-genunix.c index a0bd3e05ae..f582483ce7 100755 --- a/vm/os-genunix.c +++ b/vm/os-genunix.c @@ -13,6 +13,7 @@ void init_signals(void) void early_init(void) { } #define SUFFIX ".image" +#define SUFFIX_LEN 6 const char *default_image_path(void) { @@ -21,8 +22,14 @@ const char *default_image_path(void) if(!path) return "factor.image"; - char *new_path = safe_malloc(PATH_MAX + strlen(SUFFIX) + 1); - memcpy(new_path,path,strlen(path) + 1); - strcat(new_path,SUFFIX); + /* We can't call strlen() here because with gcc 4.1.2 this + causes an internal compiler error. */ + int len = 0; + const char *iter = path; + while(*iter) { len++; iter++; } + + char *new_path = safe_malloc(PATH_MAX + SUFFIX_LEN + 1); + memcpy(new_path,path,len + 1); + memcpy(new_path + len,SUFFIX,SUFFIX_LEN + 1); return new_path; } From 0311c0a842380aebcf53c026b4215529758637cd Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 13:07:34 -0600 Subject: [PATCH 074/269] Remove broken optimization --- vm/types.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/vm/types.c b/vm/types.c index 24b5e7ff07..11e92ec754 100755 --- a/vm/types.c +++ b/vm/types.c @@ -471,8 +471,6 @@ F_STRING* allot_string_internal(CELL capacity) string->hashcode = F; string->aux = F; - set_string_nth(string,capacity,0); - return string; } @@ -645,14 +643,7 @@ F_BYTE_ARRAY *allot_c_string(CELL capacity, CELL size) } \ type *to_##type##_string(F_STRING *s, bool check) \ { \ - if(sizeof(type) == sizeof(char)) \ - { \ - if(check && !check_string(s,sizeof(type))) \ - general_error(ERROR_C_STRING,tag_object(s),F,NULL); \ - return (type*)(s + 1); \ - } \ - else \ - return (type*)(string_to_##type##_alien(s,check) + 1); \ + return (type*)(string_to_##type##_alien(s,check) + 1); \ } \ type *unbox_##type##_string(void) \ { \ From c9a7f35e9ccb21e4e08ece6182c110defdb6d490 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 13:32:27 -0600 Subject: [PATCH 075/269] remove spurious db.sql --- extra/db/db.factor | 1 - extra/db/sqlite/sqlite-tests.factor | 2 +- extra/db/sqlite/sqlite.factor | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/extra/db/db.factor b/extra/db/db.factor index b765924cd6..1c287cd871 100644 --- a/extra/db/db.factor +++ b/extra/db/db.factor @@ -10,7 +10,6 @@ C: db ( handle -- obj ) ! HOOK: db-create db ( str -- ) ! HOOK: db-drop db ( str -- ) GENERIC: db-open ( db -- ) -GENERIC: db-close ( db -- ) TUPLE: statement sql params handle bound? ; diff --git a/extra/db/sqlite/sqlite-tests.factor b/extra/db/sqlite/sqlite-tests.factor index f64b8d1104..aa7168530b 100644 --- a/extra/db/sqlite/sqlite-tests.factor +++ b/extra/db/sqlite/sqlite-tests.factor @@ -1,5 +1,5 @@ USING: io io.files io.launcher kernel namespaces -prettyprint tools.test db.sqlite db db.sql sequences +prettyprint tools.test db.sqlite db sequences continuations ; IN: temporary diff --git a/extra/db/sqlite/sqlite.factor b/extra/db/sqlite/sqlite.factor index 49462dcc50..73b93d404b 100644 --- a/extra/db/sqlite/sqlite.factor +++ b/extra/db/sqlite/sqlite.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2005, 2008 Chris Double, Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien arrays assocs classes compiler db db.sql +USING: alien arrays assocs classes compiler db hashtables io.files kernel math math.parser namespaces prettyprint sequences strings tuples alien.c-types continuations db.sqlite.lib db.sqlite.ffi ; From 4066e1ca6b68512726bf66a9a4526a222ce770fe Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 13:34:01 -0600 Subject: [PATCH 076/269] start mysql --- extra/db/mysql/ffi/ffi.factor | 25 ++++++++++ extra/db/mysql/lib/lib.factor | 94 +++++++++++++++++++++++++++++++++++ extra/db/mysql/mysql.factor | 15 ++++++ 3 files changed, 134 insertions(+) create mode 100644 extra/db/mysql/ffi/ffi.factor create mode 100644 extra/db/mysql/lib/lib.factor create mode 100644 extra/db/mysql/mysql.factor diff --git a/extra/db/mysql/ffi/ffi.factor b/extra/db/mysql/ffi/ffi.factor new file mode 100644 index 0000000000..845381a23c --- /dev/null +++ b/extra/db/mysql/ffi/ffi.factor @@ -0,0 +1,25 @@ +! Copyright (C) 2007 Berlin Brown, 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +! Adapted from mysql.h and mysql.c +! Tested with MySQL version - 5.0.24a +USING: alien alien.syntax combinators kernel system ; +IN: db.mysql.ffi + +<< "mysql" { + { [ win32? ] [ "libmySQL.dll" "stdcall" ] } + { [ macosx? ] [ "libmysqlclient.14.dylib" "cdecl" ] } + { [ unix? ] [ "libmysqlclient.so.14" "cdecl" ] } +} cond add-library >> + +LIBRARY: mysql + +FUNCTION: void* mysql_init ( void* mysql ) ; +FUNCTION: char* mysql_error ( void* mysql ) ; +FUNCTION: void* mysql_real_connect ( void* mysql, char* host, char* user, char* passwd, char* db, int port, char* unixsocket, long clientflag ) ; +FUNCTION: void mysql_close ( void* sock ) ; +FUNCTION: int mysql_query ( void* mysql, char* q ) ; +FUNCTION: void* mysql_use_result ( void* mysql ) ; +FUNCTION: void mysql_free_result ( void* result ) ; +FUNCTION: char** mysql_fetch_row ( void* result ) ; +FUNCTION: int mysql_num_fields ( void* result ) ; +FUNCTION: ulong mysql_affected_rows ( void* mysql ) ; diff --git a/extra/db/mysql/lib/lib.factor b/extra/db/mysql/lib/lib.factor new file mode 100644 index 0000000000..7d5c2d55dc --- /dev/null +++ b/extra/db/mysql/lib/lib.factor @@ -0,0 +1,94 @@ +! Copyright (C) 2007 Berlin Brown, 2008 Doug Coleman. +! See http://factorcode.org/license.txt for license. +! Adapted from mysql.h and mysql.c +! Tested with MySQL version - 5.0.24a +USING: kernel alien io prettyprint sequences +namespaces arrays math db.mysql.ffi system ; +IN: db.mysql.lib + +SYMBOL: my-conn + +TUPLE: mysql-db handle host user password db port ; +TUPLE: mysql-statement ; +TUPLE: mysql-result-set ; + +: new-mysql ( -- conn ) + f mysql_init ; + +: mysql-error-string ( mysql-connection -- str ) + mysql-db-handle mysql_error ; + +: mysql-error ( mysql -- ) + mysql-error-string throw ; + +: mysql-connect ( mysql-connection -- ) + init-mysql swap + [ set-mysql-connection-mysqlconn ] 2keep + [ mysql-connection-host ] keep + [ mysql-connection-user ] keep + [ mysql-connection-password ] keep + [ mysql-connection-db ] keep + [ mysql-connection-port f 0 mysql_real_connect ] keep + [ set-mysql-connection-handle ] keep + dup mysql-connection-handle + [ connect-error-msg throw ] unless ; + +! ========================================================= +! Low level mysql utility definitions +! ========================================================= + +: (mysql-query) ( mysql-connection query -- ret ) + >r mysql-connection-mysqlconn r> mysql_query ; + +: (mysql-result) ( mysql-connection -- ret ) + [ mysql-connection-mysqlconn mysql_use_result ] keep + [ set-mysql-connection-resulthandle ] keep ; + +: (mysql-affected-rows) ( mysql-connection -- n ) + mysql-connection-mysqlconn mysql_affected_rows ; + +: (mysql-free-result) ( mysql-connection -- ) + mysql-connection-resulthandle drop ; + +: (mysql-row) ( mysql-connection -- row ) + mysql-connection-resulthandle mysql_fetch_row ; + +: (mysql-num-cols) ( mysql-connection -- n ) + mysql-connection-resulthandle mysql_num_fields ; + +: mysql-char*-nth ( index object -- str ) + #! Utility based on 'char*-nth' to perform an additional sanity check on the value + #! extracted from the array of strings. + void*-nth [ alien>char-string ] [ "" ] if* ; + +: mysql-row>seq ( object n -- seq ) + [ swap mysql-char*-nth ] map-with ; + +: (mysql-result>seq) ( seq -- seq ) + my-conn get (mysql-row) dup [ + my-conn get (mysql-num-cols) mysql-row>seq + over push + (mysql-result>seq) + ] [ drop ] if + ! Perform needed cleanup on fetched results + my-conn get (mysql-free-result) ; + +! ========================================================= +! Public Word Definitions +! ========================================================= + + +: mysql-query ( query -- ret ) + >r my-conn get r> (mysql-query) drop + my-conn get (mysql-result) ; + +: mysql-command ( query -- n ) + mysql-query drop + my-conn get (mysql-affected-rows) ; + +: with-mysql ( host user password db port quot -- ) + [ + >r my-conn set + my-conn get mysql-connect drop r> + [ my-conn get mysql-close ] cleanup + ] with-scope ; inline diff --git a/extra/db/mysql/mysql.factor b/extra/db/mysql/mysql.factor new file mode 100644 index 0000000000..8043bc2782 --- /dev/null +++ b/extra/db/mysql/mysql.factor @@ -0,0 +1,15 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for license. +USING: alien continuations io kernel prettyprint sequences +db ; +IN: db.mysql + +TUPLE: mysql-db handle host user password db port ; + +M: mysql-db db-open ( mysql-db -- ) + ; + +M: mysql-db dispose ( mysql-db -- ) + mysql-db-handle mysql_close ; + + From 13338b04f6f44499b700714bd07adc86ef666931 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 13:34:32 -0600 Subject: [PATCH 077/269] remove old mysql --- unmaintained/mysql/libmysql.factor | 35 ------ unmaintained/mysql/load.factor | 11 -- unmaintained/mysql/mysql.factor | 124 ------------------- unmaintained/mysql/test/create_database.sql | 17 --- unmaintained/mysql/test/mysql-example.factor | 57 --------- 5 files changed, 244 deletions(-) delete mode 100644 unmaintained/mysql/libmysql.factor delete mode 100644 unmaintained/mysql/load.factor delete mode 100644 unmaintained/mysql/mysql.factor delete mode 100644 unmaintained/mysql/test/create_database.sql delete mode 100644 unmaintained/mysql/test/mysql-example.factor diff --git a/unmaintained/mysql/libmysql.factor b/unmaintained/mysql/libmysql.factor deleted file mode 100644 index 064c7bffbc..0000000000 --- a/unmaintained/mysql/libmysql.factor +++ /dev/null @@ -1,35 +0,0 @@ -! See http://factorcode.org/license.txt -! Copyright (C) 2007 Berlin Brown -! Date: 1/17/2007 -! -! libs/mysql/libmysql.factor -! -! Adapted from mysql.h and mysql.c -! Tested with MySQL version - 5.0.24a - -IN: mysql -USING: alien kernel ; - -"mysql" { - { [ win32? ] [ "libmySQL.dll" "stdcall" ] } - { [ macosx? ] [ "libmysqlclient.14.dylib" "cdecl" ] } - { [ unix? ] [ "libmysqlclient.so.14" "cdecl" ] } -} cond add-library - -LIBRARY: mysql - -! =============================================== -! mysql.c -! =============================================== - -FUNCTION: void* mysql_init ( void* mysql ) ; -FUNCTION: char* mysql_error ( void* mysql ) ; -FUNCTION: void* mysql_real_connect ( void* mysql, char* host, char* user, char* passwd, char* db, int port, char* unixsocket, long clientflag ) ; -FUNCTION: void mysql_close ( void* sock ) ; -FUNCTION: int mysql_query ( void* mysql, char* q ) ; -FUNCTION: void* mysql_use_result ( void* mysql ) ; -FUNCTION: void mysql_free_result ( void* result ) ; -FUNCTION: char** mysql_fetch_row ( void* result ) ; -FUNCTION: int mysql_num_fields ( void* result ) ; -FUNCTION: ulong mysql_affected_rows ( void* mysql ) ; - diff --git a/unmaintained/mysql/load.factor b/unmaintained/mysql/load.factor deleted file mode 100644 index b3872d6259..0000000000 --- a/unmaintained/mysql/load.factor +++ /dev/null @@ -1,11 +0,0 @@ -! License: See http://factor.sf.net/license.txt for BSD license. -! Berlin Brown -! Date: 1/17/2007 -! -! Adapted from mysql.h and mysql.c -! Tested with MySQL version - 5.0.24a -PROVIDE: libs/mysql -{ +files+ { - "libmysql.factor" - "mysql.factor" -} } ; \ No newline at end of file diff --git a/unmaintained/mysql/mysql.factor b/unmaintained/mysql/mysql.factor deleted file mode 100644 index 22a6bc9248..0000000000 --- a/unmaintained/mysql/mysql.factor +++ /dev/null @@ -1,124 +0,0 @@ -! See http://factorcode.org/license.txt for license. -! Copyright (C) 2007 Berlin Brown -! Date: 1/17/2007 -! -! libs/mysql/mysql.factor -! -! Adapted from mysql.h and mysql.c -! Tested with MySQL version - 5.0.24a - -IN: mysql -USING: kernel alien errors io prettyprint - sequences namespaces arrays math tools generic ; - -SYMBOL: my-conn - -TUPLE: mysql-connection mysqlconn host user password db port handle resulthandle ; - -: init-mysql ( -- conn ) - f mysql_init ; - -C: mysql-connection ( host user password db port -- mysql-connection ) - [ set-mysql-connection-port ] keep - [ set-mysql-connection-db ] keep - [ set-mysql-connection-password ] keep - [ set-mysql-connection-user ] keep - [ set-mysql-connection-host ] keep ; - -: (mysql-error) ( mysql-connection -- str ) - mysql-connection-mysqlconn mysql_error ; - -: connect-error-msg ( mysql-connection -- s ) - mysql-connection-mysqlconn mysql_error - [ - "Couldn't connect to mysql database.\n" % - "Message: " % % - ] "" make ; - -: mysql-connect ( mysql-connection -- ) - init-mysql swap - [ set-mysql-connection-mysqlconn ] 2keep - [ mysql-connection-host ] keep - [ mysql-connection-user ] keep - [ mysql-connection-password ] keep - [ mysql-connection-db ] keep - [ mysql-connection-port f 0 mysql_real_connect ] keep - [ set-mysql-connection-handle ] keep - dup mysql-connection-handle - [ connect-error-msg throw ] unless ; - -! ========================================================= -! Low level mysql utility definitions -! ========================================================= - -: (mysql-query) ( mysql-connection query -- ret ) - >r mysql-connection-mysqlconn r> mysql_query ; - -: (mysql-result) ( mysql-connection -- ret ) - [ mysql-connection-mysqlconn mysql_use_result ] keep - [ set-mysql-connection-resulthandle ] keep ; - -: (mysql-affected-rows) ( mysql-connection -- n ) - mysql-connection-mysqlconn mysql_affected_rows ; - -: (mysql-free-result) ( mysql-connection -- ) - mysql-connection-resulthandle drop ; - -: (mysql-row) ( mysql-connection -- row ) - mysql-connection-resulthandle mysql_fetch_row ; - -: (mysql-num-cols) ( mysql-connection -- n ) - mysql-connection-resulthandle mysql_num_fields ; - -: mysql-char*-nth ( index object -- str ) - #! Utility based on 'char*-nth' to perform an additional sanity check on the value - #! extracted from the array of strings. - void*-nth [ alien>char-string ] [ "" ] if* ; - -: mysql-row>seq ( object n -- seq ) - [ swap mysql-char*-nth ] map-with ; - -: (mysql-result>seq) ( seq -- seq ) - my-conn get (mysql-row) dup [ - my-conn get (mysql-num-cols) mysql-row>seq - over push - (mysql-result>seq) - ] [ drop ] if - ! Perform needed cleanup on fetched results - my-conn get (mysql-free-result) ; - -! ========================================================= -! Public Word Definitions -! ========================================================= - -: mysql-close ( mysql-connection -- ) - mysql-connection-mysqlconn mysql_close ; - -: mysql-print-table ( seq -- ) - [ [ write bl ] each "\n" write ] each ; - -: mysql-query ( query -- ret ) - >r my-conn get r> (mysql-query) drop - my-conn get (mysql-result) ; - -: mysql-command ( query -- n ) - mysql-query drop - my-conn get (mysql-affected-rows) ; - -: mysql-error ( -- s ) - #! Get the last mysql error - my-conn get (mysql-error) ; - -: mysql-result>seq ( -- seq ) - V{ } clone (mysql-result>seq) ; - -: with-mysql ( host user password db port quot -- ) - [ - >r my-conn set - my-conn get mysql-connect drop r> - [ my-conn get mysql-close ] cleanup - ] with-scope ; inline - -: with-mysql-catch ( host user password db port quot -- ) - [ with-mysql ] catch [ "Caught: " write print ] when* ; - \ No newline at end of file diff --git a/unmaintained/mysql/test/create_database.sql b/unmaintained/mysql/test/create_database.sql deleted file mode 100644 index 00fd323046..0000000000 --- a/unmaintained/mysql/test/create_database.sql +++ /dev/null @@ -1,17 +0,0 @@ --- --- Create three databases (development / test / production) --- with prefix 'factordb_' -create database factordb_development; -create database factordb_test; -create database factordb_production; - -grant all on factordb_development.* to 'factoruser'@'localhost' identified by 'mysqlfactor'; -grant all on factordb_test.* to 'factoruser'@'localhost' identified by 'mysqlfactor'; -grant all on factordb_production.* to 'factoruser'@'localhost' identified by 'mysqlfactor'; - -grant all on factordb_development.* to 'factoruser'@'*' identified by 'mysqlfactor'; -grant all on factordb_test.* to 'factoruser'@'*' identified by 'mysqlfactor'; -grant all on factordb_production.* to 'factoruser'@'*' identified by 'mysqlfactor'; - --- End of the Script - diff --git a/unmaintained/mysql/test/mysql-example.factor b/unmaintained/mysql/test/mysql-example.factor deleted file mode 100644 index 2476153c8a..0000000000 --- a/unmaintained/mysql/test/mysql-example.factor +++ /dev/null @@ -1,57 +0,0 @@ -! See http://factorcode.org/license.txt for license. -! Simple test for mysql library -! libs/mysql/test/mysql-example.factor - -IN: mysql-example -REQUIRES: libs/mysql ; -USING: sequences mysql modules prettyprint kernel io math tools namespaces test ; - -"Testing..." print nl - -: get-drop-table ( -- s ) - "DROP TABLE if exists DISCUSSION_FORUM" ; - -: get-insert-table ( -- s ) - { - "INSERT INTO DISCUSSION_FORUM(category, full_name, email, title, main_url, keywords, message) " - "VALUES('none', 'John Doe', 'johndoe@test.com', 'The Message', NULL, NULL, 'Testing')" - } "" join ; - -: get-update-table ( -- s ) - "UPDATE DISCUSSION_FORUM set category = 'my-new-category'" ; - -: get-delete-table ( -- s ) - "DELETE FROM DISCUSSION_FORUM where id = 2" ; - -: get-create-table ( -- s ) - { - "create table DISCUSSION_FORUM(" - "id int(11) NOT NULL auto_increment," - "category varchar(128)," - "full_name varchar(128) NOT NULL," - "email varchar(128) NOT NULL," - "title varchar(255) NOT NULL," - "main_url varchar(255)," - "keywords varchar(255)," - "message text NOT NULL," - "created_on DATETIME NOT NULL DEFAULT '0000-00-0000:00:00'," - "PRIMARY KEY (id));" - } "" join ; - -[ "localhost" "factoruser" "mysqlfactor" "factordb_development" 0 [ - get-drop-table mysql-command drop - get-create-table mysql-command drop - get-update-table mysql-command drop - get-delete-table mysql-command drop - - ! Insert multiple records - 20 [ - get-insert-table mysql-command 2drop - ] each - - "select * from discussion_forum order by created_on" mysql-query drop - mysql-result>seq mysql-print-table - -] with-mysql ] time - -"Done" print \ No newline at end of file From eda2c710d450352314ebf9df616ebaa0e7d390dd Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 13:38:59 -0600 Subject: [PATCH 078/269] add dll to script --- misc/factor.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/misc/factor.sh b/misc/factor.sh index 02f4c4a542..fa8cdcd5b1 100755 --- a/misc/factor.sh +++ b/misc/factor.sh @@ -243,6 +243,8 @@ maybe_download_dlls() { check_ret wget wget http://factorcode.org/dlls/vorbis.dll check_ret wget + wget http://factorcode.org/dlls/sqlite3.dll + check_ret wget chmod 777 *.dll check_ret chmod fi From 354d85342e11f5465432e43662809fc5763d2af0 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 13:57:22 -0600 Subject: [PATCH 079/269] remove dependency on sqlite3 binary --- extra/db/sqlite/sqlite-tests.factor | 45 +++++++++-------------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/extra/db/sqlite/sqlite-tests.factor b/extra/db/sqlite/sqlite-tests.factor index aa7168530b..c6576dcd62 100644 --- a/extra/db/sqlite/sqlite-tests.factor +++ b/extra/db/sqlite/sqlite-tests.factor @@ -3,40 +3,26 @@ prettyprint tools.test db.sqlite db sequences continuations ; IN: temporary -! "sqlite3 -init test.txt test.db" - -IN: scratchpad : test.db "extra/db/sqlite/test.db" resource-path ; -IN: temporary -: (create-db) ( -- str ) - [ - "sqlite3 -init " % - test.db % - " " % - test.db % - ] "" make ; +[ ] [ [ test.db delete-file ] catch drop ] unit-test -: create-db ( -- ) (create-db) run-process drop ; +[ ] [ + test.db [ + "create table person (name varchar(30), country varchar(30))" sql-command + "insert into person values('John', 'America')" sql-command + "insert into person values('Jane', 'New Zealand')" sql-command + ] with-sqlite +] unit-test -[ ] [ test.db delete-file ] unit-test -[ ] [ create-db ] unit-test - -[ - { - { "John" "America" } - { "Jane" "New Zealand" } - } -] [ +[ { { "John" "America" } { "Jane" "New Zealand" } } ] [ test.db [ "select * from person" sql-query ] with-sqlite ] unit-test -[ - { { "John" "America" } } -] [ +[ { { "John" "America" } } ] [ test.db [ "select * from person where name = :name and country = :country" [ @@ -52,15 +38,10 @@ IN: temporary ] with-sqlite ] unit-test -[ - { - { "1" "John" "America" } - { "2" "Jane" "New Zealand" } - } -] [ test.db [ "select rowid, * from person" sql-query ] with-sqlite ] unit-test +[ { { "1" "John" "America" } { "2" "Jane" "New Zealand" } } ] +[ test.db [ "select rowid, * from person" sql-query ] with-sqlite ] unit-test -[ -] [ +[ ] [ test.db [ "insert into person(name, country) values('Jimmy', 'Canada')" sql-command From bc2ce8a77b3f2994bdb07623ea71e942ac77856e Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 14:05:31 -0600 Subject: [PATCH 080/269] Space one byte per string --- core/bootstrap/image/image.factor | 2 +- vm/types.c | 4 ---- vm/types.h | 2 +- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/core/bootstrap/image/image.factor b/core/bootstrap/image/image.factor index e9ee569fd6..4995d0b572 100755 --- a/core/bootstrap/image/image.factor +++ b/core/bootstrap/image/image.factor @@ -248,7 +248,7 @@ M: wrapper ' emit-seq ; : pack-string ( string -- newstr ) - dup length 1+ bootstrap-cell align 0 pad-right ; + dup length bootstrap-cell align 0 pad-right ; : emit-string ( string -- ptr ) string type-number object tag-number [ diff --git a/vm/types.c b/vm/types.c index 11e92ec754..78e74535b8 100755 --- a/vm/types.c +++ b/vm/types.c @@ -463,10 +463,6 @@ F_STRING* allot_string_internal(CELL capacity) { F_STRING *string = allot_object(STRING_TYPE,string_size(capacity)); - /* strings are null-terminated in memory, even though they also - have a length field. The null termination allows us to add - the sizeof(F_STRING) to a Factor string to get a C-style - char* string for C library calls. */ string->length = tag_fixnum(capacity); string->hashcode = F; string->aux = F; diff --git a/vm/types.h b/vm/types.h index e5003ea069..62b2e06dd0 100755 --- a/vm/types.h +++ b/vm/types.h @@ -11,7 +11,7 @@ INLINE CELL string_capacity(F_STRING* str) INLINE CELL string_size(CELL size) { - return sizeof(F_STRING) + size + 1; + return sizeof(F_STRING) + size; } DEFINE_UNTAG(F_BYTE_ARRAY,BYTE_ARRAY_TYPE,byte_array) From dee25cda136cb01c3960946839e43f845e2ec0e7 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 16:20:07 -0600 Subject: [PATCH 081/269] New generic word implementation reduces compile time --- core/bootstrap/image/image.factor | 53 ++++----------- core/bootstrap/primitives.factor | 10 +-- core/bootstrap/stage1.factor | 1 + core/bootstrap/stage2.factor | 2 +- core/classes/classes.factor | 2 +- core/classes/union/union.factor | 29 ++++++-- core/compiler/compiler.factor | 2 +- core/effects/effects.factor | 16 +++-- core/generator/generator.factor | 10 ++- core/generic/generic-docs.factor | 6 +- core/generic/generic.factor | 77 ++++++++++++++-------- core/generic/math/math.factor | 9 ++- core/generic/standard/standard.factor | 53 +++++++++------ core/inference/backend/backend.factor | 10 ++- core/optimizer/backend/backend.factor | 10 ++- core/words/words.factor | 3 +- extra/benchmark/dispatch5/dispatch5.factor | 77 ++++++++++++++++++++++ extra/tools/crossref/crossref.factor | 3 +- 18 files changed, 254 insertions(+), 119 deletions(-) mode change 100644 => 100755 core/effects/effects.factor mode change 100644 => 100755 core/optimizer/backend/backend.factor create mode 100755 extra/benchmark/dispatch5/dispatch5.factor mode change 100644 => 100755 extra/tools/crossref/crossref.factor diff --git a/core/bootstrap/image/image.factor b/core/bootstrap/image/image.factor index e9ee569fd6..10715d2c5c 100755 --- a/core/bootstrap/image/image.factor +++ b/core/bootstrap/image/image.factor @@ -203,7 +203,14 @@ M: f ' ! Words +DEFER: emit-word + +: emit-generic ( generic -- ) + dup "default-method" word-prop method-word emit-word + "methods" word-prop [ nip method-word emit-word ] assoc-each ; + : emit-word ( word -- ) + dup generic? [ dup emit-generic ] when [ dup hashcode ' , dup word-name ' , @@ -224,7 +231,7 @@ M: f ' [ % dup word-vocabulary % " " % word-name % ] "" make throw ; : transfer-word ( word -- word ) - dup target-word [ ] [ word-name no-word ] ?if ; + dup target-word swap or ; : fixup-word ( word -- offset ) transfer-word dup objects get at @@ -285,17 +292,20 @@ M: float-array ' float-array emit-dummy-array ; ] emit-object ; : emit-tuple ( obj -- pointer ) - objects get [ + [ [ tuple>array unclip transfer-word , % ] { } make tuple type-number dup emit-array - ] cache ; inline + ] + ! Hack + over class word-name "tombstone" = + [ objects get swap cache ] [ call ] if ; M: tuple ' emit-tuple ; M: tombstone ' delegate "((tombstone))" "((empty))" ? "hashtables.private" lookup - word-def first emit-tuple ; + word-def first objects get [ emit-tuple ] cache ; M: array ' array type-number object tag-number emit-array ; @@ -313,41 +323,6 @@ M: quotation ' ] emit-object ] cache ; -! Vectors and sbufs - -M: vector ' - dup length swap underlying ' - tuple type-number tuple tag-number [ - 4 emit-fixnum - vector ' emit - f ' emit - emit ! array ptr - emit-fixnum ! length - ] emit-object ; - -M: sbuf ' - dup length swap underlying ' - tuple type-number tuple tag-number [ - 4 emit-fixnum - sbuf ' emit - f ' emit - emit ! array ptr - emit-fixnum ! length - ] emit-object ; - -! Hashes - -M: hashtable ' - [ hash-array ' ] keep - tuple type-number tuple tag-number [ - 5 emit-fixnum - hashtable ' emit - f ' emit - dup hash-count emit-fixnum - hash-deleted emit-fixnum - emit ! array ptr - ] emit-object ; - ! Curries M: curry ' diff --git a/core/bootstrap/primitives.factor b/core/bootstrap/primitives.factor index 545d904c9c..550aac71b0 100755 --- a/core/bootstrap/primitives.factor +++ b/core/bootstrap/primitives.factor @@ -118,11 +118,11 @@ H{ } clone update-map set H{ } clone typemap set num-types get f builtins set -! These symbols are needed by the code that executes below -{ - { "object" "kernel" } - { "null" "kernel" } -} [ create drop ] assoc-each +! Forward definitions +"object" "kernel" create t "class" set-word-prop +"object" "kernel" create union-class "metaclass" set-word-prop + +"null" "kernel" create drop "fixnum" "math" create "fixnum?" "math" create { } define-builtin "fixnum" "math" create ">fixnum" "math" create 1quotation "coercer" set-word-prop diff --git a/core/bootstrap/stage1.factor b/core/bootstrap/stage1.factor index 8af1bfdec9..cc328e9760 100755 --- a/core/bootstrap/stage1.factor +++ b/core/bootstrap/stage1.factor @@ -32,6 +32,7 @@ vocabs.loader system ; "io.streams.c" require "vocabs.loader" require + "syntax" require "bootstrap.layouts" require diff --git a/core/bootstrap/stage2.factor b/core/bootstrap/stage2.factor index 5a5a8d1c67..7a0fab8a99 100755 --- a/core/bootstrap/stage2.factor +++ b/core/bootstrap/stage2.factor @@ -15,7 +15,7 @@ IN: bootstrap.stage2 vm file-name windows? [ "." split1 drop ] when ".image" append "output-image" set-global - "math tools help compiler ui ui.tools io" "include" set-global + "math help compiler tools ui ui.tools io" "include" set-global "" "exclude" set-global parse-command-line diff --git a/core/classes/classes.factor b/core/classes/classes.factor index a6a1db7045..151429bf69 100755 --- a/core/classes/classes.factor +++ b/core/classes/classes.factor @@ -1,4 +1,4 @@ -! Copyright (C) 2004, 2007 Slava Pestov. +! Copyright (C) 2004, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. IN: classes USING: arrays definitions assocs kernel diff --git a/core/classes/union/union.factor b/core/classes/union/union.factor index 0adbdc080d..332903d36b 100755 --- a/core/classes/union/union.factor +++ b/core/classes/union/union.factor @@ -1,19 +1,34 @@ -! Copyright (C) 2004, 2007 Slava Pestov. +! Copyright (C) 2004, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: words sequences kernel assocs combinators classes -generic.standard namespaces arrays ; +generic.standard namespaces arrays math quotations ; IN: classes.union PREDICATE: class union-class "metaclass" word-prop union-class eq? ; ! Union classes for dispatch on multiple classes. +: small-union-predicate-quot ( members -- quot ) + dup empty? [ + drop [ drop f ] + ] [ + unclip first "predicate" word-prop swap + [ >r "predicate" word-prop [ dup ] swap append r> ] + assoc-map alist>quot + ] if ; + +: big-union-predicate-quot ( members -- quot ) + [ small-union-predicate-quot ] [ dup ] + class-hash-dispatch-quot ; + : union-predicate-quot ( members -- quot ) - 0 (dispatch#) [ - [ [ drop t ] ] { } map>assoc - object bootstrap-word [ drop f ] 2array add* - single-combination - ] with-variable ; + [ [ drop t ] ] { } map>assoc + dup length 4 <= [ + small-union-predicate-quot + ] [ + flatten-methods + big-union-predicate-quot + ] if ; : define-union-predicate ( class -- ) dup predicate-word diff --git a/core/compiler/compiler.factor b/core/compiler/compiler.factor index 1e6d4f8a17..631c2e4f53 100755 --- a/core/compiler/compiler.factor +++ b/core/compiler/compiler.factor @@ -26,7 +26,7 @@ IN: compiler >r dupd save-effect r> f pick compiler-error over compiled-unxref - over word-vocabulary [ compiled-xref ] [ 2drop ] if ; + compiled-xref ; : compile-succeeded ( word -- effect dependencies ) [ diff --git a/core/effects/effects.factor b/core/effects/effects.factor old mode 100644 new mode 100755 index ee929507c8..10ebca6dea --- a/core/effects/effects.factor +++ b/core/effects/effects.factor @@ -42,12 +42,16 @@ M: integer (stack-picture) drop "object" ; ] "" make ; : stack-effect ( word -- effect/f ) - dup symbol? [ - drop 0 1 - ] [ - { "declared-effect" "inferred-effect" } - swap word-props [ at ] curry map [ ] find nip - ] if ; + { + { [ dup symbol? ] [ drop 0 1 ] } + { [ dup "parent-generic" word-prop ] [ + "parent-generic" word-prop stack-effect + ] } + { [ t ] [ + { "declared-effect" "inferred-effect" } + swap word-props [ at ] curry map [ ] find nip + ] } + } cond ; M: effect clone [ effect-in clone ] keep effect-out clone ; diff --git a/core/generator/generator.factor b/core/generator/generator.factor index de80872b73..3d66241bc3 100755 --- a/core/generator/generator.factor +++ b/core/generator/generator.factor @@ -154,9 +154,17 @@ M: #if generate-node ] generate-1 ] keep ; +: tail-dispatch? ( node -- ? ) + #! Is the dispatch a jump to a tail call to a word? + dup #call? swap node-successor #return? and ; + : dispatch-branches ( node -- ) node-children [ - compiling-word get dispatch-branch %dispatch-label + dup tail-dispatch? [ + node-param + ] [ + compiling-word get dispatch-branch + ] if %dispatch-label ] each ; M: #dispatch generate-node diff --git a/core/generic/generic-docs.factor b/core/generic/generic-docs.factor index f1cdae1c91..f4da9575e9 100755 --- a/core/generic/generic-docs.factor +++ b/core/generic/generic-docs.factor @@ -125,16 +125,12 @@ HELP: method { $description "Looks up a method definition." } { $class-description "Instances of this class are methods. A method consists of a quotation together with a source location where it was defined." } ; -{ method method-def method-loc define-method POSTPONE: M: } related-words +{ method define-method POSTPONE: M: } related-words HELP: { $values { "def" "a quotation" } { "method" "a new method definition" } } { $description "Creates a new "{ $link method } " instance." } ; -HELP: sort-methods -{ $values { "assoc" "an assoc mapping classes to methods" } { "newassoc" "an association list mapping classes to quotations" } } -{ $description "Outputs a sequence of pairs, where the first element of each pair is a class and the second element is the corresponding method quotation. The methods are sorted by class order; see " { $link sort-classes } "." } ; - HELP: methods { $values { "word" generic } { "assoc" "an association list mapping classes to quotations" } } { $description "Outputs a sequence of pairs, where the first element of each pair is a class and the second element is the corresponding method quotation. The methods are sorted by class order; see " { $link sort-classes } "." } ; diff --git a/core/generic/generic.factor b/core/generic/generic.factor index c75dd41d74..951813dbcd 100755 --- a/core/generic/generic.factor +++ b/core/generic/generic.factor @@ -5,12 +5,7 @@ definitions kernel.private classes classes.private quotations arrays vocabs ; IN: generic -PREDICATE: word generic "combination" word-prop >boolean ; - -M: generic definer drop f f ; - -M: generic definition drop f ; - +! Method combination protocol GENERIC: perform-combination ( word combination -- quot ) M: object perform-combination @@ -22,22 +17,22 @@ M: object perform-combination #! the method will throw an error. We don't want that. nip [ "Invalid method combination" throw ] curry [ ] like ; +GENERIC: method-prologue ( class combination -- quot ) + +M: object method-prologue 2drop [ ] ; + +GENERIC: make-default-method ( generic combination -- method ) + +PREDICATE: word generic "combination" word-prop >boolean ; + +M: generic definer drop f f ; + +M: generic definition drop f ; + : make-generic ( word -- ) dup dup "combination" word-prop perform-combination define ; -: init-methods ( word -- ) - dup "methods" word-prop - H{ } assoc-like - "methods" set-word-prop ; - -: define-generic ( word combination -- ) - dupd "combination" set-word-prop - dup init-methods make-generic ; - -TUPLE: method loc def ; - -: ( def -- method ) - { set-method-def } \ method construct ; +TUPLE: method word def specializer generic loc ; : method ( class generic -- method/f ) "methods" word-prop at ; @@ -48,12 +43,10 @@ PREDICATE: pair method-spec : order ( generic -- seq ) "methods" word-prop keys sort-classes ; -: sort-methods ( assoc -- newassoc ) - [ keys sort-classes ] keep - [ dupd at method-def ] curry { } map>assoc ; - : methods ( word -- assoc ) - "methods" word-prop sort-methods ; + "methods" word-prop + [ keys sort-classes ] keep + [ dupd at method-word ] curry { } map>assoc ; TUPLE: check-method class generic ; @@ -66,10 +59,31 @@ TUPLE: check-method class generic ; swap [ "methods" word-prop swap call ] keep make-generic ; inline -: define-method ( method class generic -- ) - >r >r r> bootstrap-word r> check-method +: method-word-name ( class word -- string ) + word-name "/" rot word-name 3append ; + +: make-method-def ( quot word combination -- quot ) + "combination" word-prop method-prologue swap append ; + +: ( quot class generic -- word ) + [ make-method-def ] 2keep + [ method-word-name f dup ] keep + "parent-generic" set-word-prop + dup rot define ; + +: ( quot class generic -- method ) + check-method + [ ] 3keep f \ method construct-boa ; + +: define-method ( quot class generic -- ) + >r bootstrap-word r> + [ ] 2keep [ set-at ] with-methods ; +: define-default-method ( generic combination -- ) + dupd make-default-method object bootstrap-word pick + "default-method" set-word-prop ; + ! Definition protocol M: method-spec where dup first2 method [ method-loc ] [ second where ] ?if ; @@ -105,3 +119,14 @@ M: class forget* ( class -- ) M: assoc update-methods ( assoc -- ) implementors* [ make-generic ] each ; + +: init-methods ( word -- ) + dup "methods" word-prop + H{ } assoc-like + "methods" set-word-prop ; + +: define-generic ( word combination -- ) + 2dup "combination" set-word-prop + dupd define-default-method + dup init-methods + make-generic ; diff --git a/core/generic/math/math.factor b/core/generic/math/math.factor index d5079c5dfb..8cf83b0ba7 100755 --- a/core/generic/math/math.factor +++ b/core/generic/math/math.factor @@ -38,9 +38,13 @@ TUPLE: no-math-method left right generic ; : no-math-method ( left right generic -- * ) \ no-math-method construct-boa throw ; +: default-math-method ( generic -- quot ) + [ no-math-method ] curry [ ] like ; + : applicable-method ( generic class -- quot ) over method - [ method-def ] [ [ no-math-method ] curry [ ] like ] ?if ; + [ method-word word-def ] + [ default-math-method ] ?if ; : object-method ( generic -- quot ) object bootstrap-word applicable-method ; @@ -66,6 +70,9 @@ TUPLE: no-math-method left right generic ; TUPLE: math-combination ; +M: math-combination make-default-method + drop default-math-method ; + M: math-combination perform-combination drop \ over [ diff --git a/core/generic/standard/standard.factor b/core/generic/standard/standard.factor index 6cc7f7f3e8..94ac82a0e4 100755 --- a/core/generic/standard/standard.factor +++ b/core/generic/standard/standard.factor @@ -8,6 +8,10 @@ IN: generic.standard TUPLE: standard-combination # ; +M: standard-combination method-prologue + standard-combination-# object + swap add [ declare ] curry ; + C: standard-combination SYMBOL: (dispatch#) @@ -31,10 +35,10 @@ TUPLE: no-method object generic ; : no-method ( object generic -- * ) \ no-method construct-boa throw ; -: error-method ( word -- method ) +: error-method ( word -- quot ) picker swap [ no-method ] curry append ; -: empty-method ( word -- method ) +: empty-method ( word -- quot ) [ picker % [ delegate dup ] % unpicker over add , @@ -65,13 +69,15 @@ TUPLE: no-method object generic ; ] if ; : default-method ( word -- pair ) - empty-method object bootstrap-word swap 2array ; + "default-method" word-prop method-word + object bootstrap-word swap 2array ; : method-alist>quot ( alist base-class -- quot ) bootstrap-word swap simplify-alist class-predicates alist>quot ; : small-generic ( methods -- def ) + [ 1quotation ] assoc-map object method-alist>quot ; : hash-methods ( methods -- buckets ) @@ -83,9 +89,12 @@ TUPLE: no-method object generic ; ] if ] distribute-buckets ; +: class-hash-dispatch-quot ( methods quot picker -- quot ) + >r >r hash-methods r> map + hash-dispatch-quot r> [ class-hash ] rot 3append ; + : big-generic ( methods -- quot ) - hash-methods [ small-generic ] map - hash-dispatch-quot picker [ class-hash ] rot 3append ; + [ small-generic ] picker class-hash-dispatch-quot ; : vtable-class ( n -- class ) type>class [ hi-tag bootstrap-word ] unless* ; @@ -100,7 +109,8 @@ TUPLE: no-method object generic ; : build-type-vtable ( alist-seq -- alist-seq ) dup length [ - vtable-class swap simplify-alist + vtable-class + swap [ word-def ] assoc-map simplify-alist class-predicates alist>quot ] 2map ; @@ -137,30 +147,35 @@ TUPLE: no-method object generic ; : standard-methods ( word -- alist ) dup methods swap default-method add* ; +M: standard-combination make-default-method + standard-combination-# (dispatch#) + [ empty-method ] with-variable ; + M: standard-combination perform-combination standard-combination-# (dispatch#) [ [ standard-methods ] keep "inline" word-prop [ small-generic ] [ single-combination ] if ] with-variable ; -: default-hook-method ( word -- pair ) - error-method object bootstrap-word swap 2array ; - -: hook-methods ( word -- methods ) - dup methods [ [ drop ] swap append ] assoc-map - swap default-hook-method add* ; - TUPLE: hook-combination var ; C: hook-combination -M: hook-combination perform-combination +M: hook-combination method-prologue + 2drop [ drop ] ; + +: with-hook ( combination quot -- quot' ) 0 (dispatch#) [ - [ - hook-combination-var [ get ] curry % - hook-methods single-combination % - ] [ ] make - ] with-variable ; + swap slip + hook-combination-var [ get ] curry + swap append + ] with-variable ; inline + +M: hook-combination make-default-method + [ error-method ] with-hook ; + +M: hook-combination perform-combination + [ standard-methods single-combination ] with-hook ; : define-simple-generic ( word -- ) T{ standard-combination f 0 } define-generic ; diff --git a/core/inference/backend/backend.factor b/core/inference/backend/backend.factor index 121c555d29..34179bbf32 100755 --- a/core/inference/backend/backend.factor +++ b/core/inference/backend/backend.factor @@ -9,9 +9,13 @@ IN: inference.backend : recursive-label ( word -- label/f ) recursive-state get at ; +: inline? ( word -- ? ) + dup "parent-generic" word-prop + [ inline? ] [ "inline" word-prop ] ?if ; + : local-recursive-state ( -- assoc ) recursive-state get dup keys - [ dup word? [ "inline" word-prop ] when not ] find drop + [ dup word? [ inline? ] when not ] find drop [ head-slice ] when* ; : inline-recursive-label ( word -- label/f ) @@ -157,7 +161,7 @@ TUPLE: too-many-r> ; meta-d get push-all ; : if-inline ( word true false -- ) - >r >r dup "inline" word-prop r> r> if ; inline + >r >r dup inline? r> r> if ; inline : consume/produce ( effect node -- ) over effect-in over consume-values @@ -331,7 +335,7 @@ TUPLE: unbalanced-branches-error quots in out ; #merge node, ; inline : make-call-node ( word effect -- ) - swap dup "inline" word-prop + swap dup inline? over dup recursive-label eq? not and [ meta-d get clone -rot recursive-label #call-label [ consume/produce ] keep diff --git a/core/optimizer/backend/backend.factor b/core/optimizer/backend/backend.factor old mode 100644 new mode 100755 index 4843a9ff26..27b1b1e0ec --- a/core/optimizer/backend/backend.factor +++ b/core/optimizer/backend/backend.factor @@ -245,11 +245,19 @@ M: #dispatch optimize-node* : dispatching-class ( node word -- class ) [ dispatch# node-class# ] keep specific-method ; +: flat-length ( seq -- n ) + [ + dup quotation? over array? or + [ flat-length ] [ drop 1 ] if + ] map sum ; + : will-inline-method ( node word -- method-spec/t quot/t ) #! t indicates failure tuck dispatching-class dup [ swap [ 2array ] 2keep - method method-def + method method-word + dup word-def flat-length 5 >= + [ 1quotation ] [ word-def ] if ] [ 2drop t t ] if ; diff --git a/core/words/words.factor b/core/words/words.factor index 5dc89212a8..b4062d8f02 100755 --- a/core/words/words.factor +++ b/core/words/words.factor @@ -154,7 +154,8 @@ SYMBOL: changed-words } reset-props ; : reset-generic ( word -- ) - dup reset-word { "methods" "combination" } reset-props ; + dup reset-word + { "methods" "combination" "default-method" } reset-props ; : gensym ( -- word ) "G:" \ gensym counter number>string append f ; diff --git a/extra/benchmark/dispatch5/dispatch5.factor b/extra/benchmark/dispatch5/dispatch5.factor new file mode 100755 index 0000000000..34df715f89 --- /dev/null +++ b/extra/benchmark/dispatch5/dispatch5.factor @@ -0,0 +1,77 @@ +USING: classes kernel sequences vocabs math ; +IN: benchmark.dispatch5 + +MIXIN: g + +TUPLE: x1 ; +INSTANCE: x1 g +TUPLE: x2 ; +INSTANCE: x2 g +TUPLE: x3 ; +INSTANCE: x3 g +TUPLE: x4 ; +INSTANCE: x4 g +TUPLE: x5 ; +INSTANCE: x5 g +TUPLE: x6 ; +INSTANCE: x6 g +TUPLE: x7 ; +INSTANCE: x7 g +TUPLE: x8 ; +INSTANCE: x8 g +TUPLE: x9 ; +INSTANCE: x9 g +TUPLE: x10 ; +INSTANCE: x10 g +TUPLE: x11 ; +INSTANCE: x11 g +TUPLE: x12 ; +INSTANCE: x12 g +TUPLE: x13 ; +INSTANCE: x13 g +TUPLE: x14 ; +INSTANCE: x14 g +TUPLE: x15 ; +INSTANCE: x15 g +TUPLE: x16 ; +INSTANCE: x16 g +TUPLE: x17 ; +INSTANCE: x17 g +TUPLE: x18 ; +INSTANCE: x18 g +TUPLE: x19 ; +INSTANCE: x19 g +TUPLE: x20 ; +INSTANCE: x20 g +TUPLE: x21 ; +INSTANCE: x21 g +TUPLE: x22 ; +INSTANCE: x22 g +TUPLE: x23 ; +INSTANCE: x23 g +TUPLE: x24 ; +INSTANCE: x24 g +TUPLE: x25 ; +INSTANCE: x25 g +TUPLE: x26 ; +INSTANCE: x26 g +TUPLE: x27 ; +INSTANCE: x27 g +TUPLE: x28 ; +INSTANCE: x28 g +TUPLE: x29 ; +INSTANCE: x29 g +TUPLE: x30 ; +INSTANCE: x30 g + +: my-classes ( -- seq ) + "benchmark.dispatch5" words [ tuple-class? ] subset ; + +: a-bunch-of-objects ( -- seq ) + my-classes [ construct-empty ] map ; + +: dispatch-benchmark ( -- ) + 1000000 a-bunch-of-objects + [ f [ g? or ] reduce drop ] curry times ; + +MAIN: dispatch-benchmark diff --git a/extra/tools/crossref/crossref.factor b/extra/tools/crossref/crossref.factor old mode 100644 new mode 100755 index dfb421c8f8..663df61926 --- a/extra/tools/crossref/crossref.factor +++ b/extra/tools/crossref/crossref.factor @@ -14,8 +14,7 @@ IN: tools.crossref : (method-usage) ( word generic -- methods ) tuck methods - [ second quot-uses key? ] with subset - 0 + [ second uses member? ] with subset keys swap [ 2array ] curry map ; : method-usage ( word seq -- methods ) From aff818a07d82a013eb5a9963eeb4397bb0deb3f7 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 16:40:14 -0600 Subject: [PATCH 082/269] add using --- extra/x/widgets/wm/frame/frame.factor | 1 + 1 file changed, 1 insertion(+) diff --git a/extra/x/widgets/wm/frame/frame.factor b/extra/x/widgets/wm/frame/frame.factor index 4e3b4e7c93..b75671fa3c 100755 --- a/extra/x/widgets/wm/frame/frame.factor +++ b/extra/x/widgets/wm/frame/frame.factor @@ -4,6 +4,7 @@ USING: kernel io combinators namespaces quotations arrays sequences x11.xlib x11.constants mortar mortar.sugar slot-accessors geom.rect + math.bitfields x x.gc x.widgets x.widgets.button x.widgets.wm.child From 37bb75b19b1ce245e2522d4d6511b4e7e05dbc3d Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 16:50:15 -0600 Subject: [PATCH 083/269] Fix extra/delegate --- extra/delegate/delegate.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/delegate/delegate.factor b/extra/delegate/delegate.factor index 4cd25baeb9..c0da9c51bc 100755 --- a/extra/delegate/delegate.factor +++ b/extra/delegate/delegate.factor @@ -27,7 +27,7 @@ M: tuple-class group-words swap [ slot-spec-writer ] map append ; : define-consult-method ( word class quot -- ) - pick add spin define-method ; + pick add spin define-method ; : define-consult ( class group quot -- ) >r group-words r> From c0c08985c5c46c877ebefcceb034751e6143bd94 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 17:10:49 -0600 Subject: [PATCH 084/269] make hardware-info load on windows --- extra/hardware-info/hardware-info.factor | 7 +++--- .../windows/backend/backend.factor | 6 ----- extra/hardware-info/windows/ce/ce.factor | 4 ++-- extra/hardware-info/windows/nt/nt.factor | 24 +++++++++---------- extra/hardware-info/windows/windows.factor | 7 +++--- 5 files changed, 21 insertions(+), 27 deletions(-) delete mode 100644 extra/hardware-info/windows/backend/backend.factor diff --git a/extra/hardware-info/hardware-info.factor b/extra/hardware-info/hardware-info.factor index 0515646a5f..69b8678749 100755 --- a/extra/hardware-info/hardware-info.factor +++ b/extra/hardware-info/hardware-info.factor @@ -1,12 +1,13 @@ -USING: alien.syntax kernel math prettyprint system -combinators vocabs.loader hardware-info.backend ; +USING: alien.syntax kernel math prettyprint +combinators vocabs.loader hardware-info.backend system ; IN: hardware-info : kb. ( x -- ) 10 2^ /f . ; : megs. ( x -- ) 20 2^ /f . ; : gigs. ( x -- ) 30 2^ /f . ; -<< { +<< +{ { [ windows? ] [ "hardware-info.windows" ] } { [ linux? ] [ "hardware-info.linux" ] } { [ macosx? ] [ "hardware-info.macosx" ] } diff --git a/extra/hardware-info/windows/backend/backend.factor b/extra/hardware-info/windows/backend/backend.factor deleted file mode 100644 index 516603c441..0000000000 --- a/extra/hardware-info/windows/backend/backend.factor +++ /dev/null @@ -1,6 +0,0 @@ -IN: hardware-info.windows.backend - -TUPLE: wince ; -TUPLE: winnt ; -UNION: windows wince winnt ; - diff --git a/extra/hardware-info/windows/ce/ce.factor b/extra/hardware-info/windows/ce/ce.factor index 1592bad14c..8923d86b03 100755 --- a/extra/hardware-info/windows/ce/ce.factor +++ b/extra/hardware-info/windows/ce/ce.factor @@ -2,8 +2,8 @@ USING: alien.c-types hardware-info kernel math namespaces windows windows.kernel32 hardware-info.backend ; IN: hardware-info.windows.ce -TUPLE: wince ; -T{ wince } os set-global +TUPLE: wince-os ; +T{ wince-os } os set-global : memory-status ( -- MEMORYSTATUS ) "MEMORYSTATUS" diff --git a/extra/hardware-info/windows/nt/nt.factor b/extra/hardware-info/windows/nt/nt.factor index 827b32c2f2..8bdb75fe6a 100755 --- a/extra/hardware-info/windows/nt/nt.factor +++ b/extra/hardware-info/windows/nt/nt.factor @@ -1,16 +1,15 @@ -USING: alien alien.c-types hardware-info.windows.backend +USING: alien alien.c-types kernel libc math namespaces hardware-info.backend windows windows.advapi32 windows.kernel32 ; IN: hardware-info.windows.nt -TUPLE: winnt ; - -T{ winnt } os set-global +TUPLE: winnt-os ; +T{ winnt-os } os set-global : system-info ( -- SYSTEM_INFO ) "SYSTEM_INFO" [ GetSystemInfo ] keep ; -M: winnt cpus ( -- n ) +M: winnt-os cpus ( -- n ) system-info SYSTEM_INFO-dwNumberOfProcessors ; : memory-status ( -- MEMORYSTATUSEX ) @@ -18,25 +17,25 @@ M: winnt cpus ( -- n ) "MEMORYSTATUSEX" heap-size over set-MEMORYSTATUSEX-dwLength [ GlobalMemoryStatusEx ] keep swap zero? [ win32-error ] when ; -M: winnt memory-load ( -- n ) +M: winnt-os memory-load ( -- n ) memory-status MEMORYSTATUSEX-dwMemoryLoad ; -M: winnt physical-mem ( -- n ) +M: winnt-os physical-mem ( -- n ) memory-status MEMORYSTATUSEX-ullTotalPhys ; -M: winnt available-mem ( -- n ) +M: winnt-os available-mem ( -- n ) memory-status MEMORYSTATUSEX-ullAvailPhys ; -M: winnt total-page-file ( -- n ) +M: winnt-os total-page-file ( -- n ) memory-status MEMORYSTATUSEX-ullTotalPageFile ; -M: winnt available-page-file ( -- n ) +M: winnt-os available-page-file ( -- n ) memory-status MEMORYSTATUSEX-ullAvailPageFile ; -M: winnt total-virtual-mem ( -- n ) +M: winnt-os total-virtual-mem ( -- n ) memory-status MEMORYSTATUSEX-ullTotalVirtual ; -M: winnt available-virtual-mem ( -- n ) +M: winnt-os available-virtual-mem ( -- n ) memory-status MEMORYSTATUSEX-ullAvailVirtual ; : computer-name ( -- string ) @@ -54,4 +53,3 @@ M: winnt available-virtual-mem ( -- n ) ] [ [ alien>u16-string ] keep free ] if ; - diff --git a/extra/hardware-info/windows/windows.factor b/extra/hardware-info/windows/windows.factor index 67d13fc50f..f3a1eb33f5 100755 --- a/extra/hardware-info/windows/windows.factor +++ b/extra/hardware-info/windows/windows.factor @@ -1,7 +1,7 @@ USING: alien alien.c-types kernel libc math namespaces windows windows.kernel32 windows.advapi32 -hardware-info.windows.backend -words combinators vocabs.loader hardware-info.backend ; +words combinators vocabs.loader hardware-info.backend +system ; IN: hardware-info.windows : system-info ( -- SYSTEM_INFO ) @@ -63,7 +63,8 @@ IN: hardware-info.windows : system-windows-directory ( -- str ) \ GetSystemWindowsDirectory get-directory ; +<< { { [ wince? ] [ "hardware-info.windows.ce" ] } { [ winnt? ] [ "hardware-info.windows.nt" ] } -} cond [ require ] when* +} cond [ require ] when* >> From 5c21b08606848c3c776534fa9c7a8432bb2eb234 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 17:11:55 -0600 Subject: [PATCH 085/269] remove a line of comments --- extra/db/postgresql/ffi/ffi.factor | 1 - 1 file changed, 1 deletion(-) diff --git a/extra/db/postgresql/ffi/ffi.factor b/extra/db/postgresql/ffi/ffi.factor index dbaa70c625..23368164a1 100644 --- a/extra/db/postgresql/ffi/ffi.factor +++ b/extra/db/postgresql/ffi/ffi.factor @@ -1,6 +1,5 @@ ! Copyright (C) 2007 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -! adapted from libpq-fe.h version 7.4.7 ! tested on debian linux with postgresql 8.1 USING: alien alien.syntax combinators system ; From 123aabc730b17e49f8ba27804514f4159db1fe43 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 17:33:59 -0600 Subject: [PATCH 086/269] Fix Mac Intel alignment issue --- core/cpu/x86/32/32.factor | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/cpu/x86/32/32.factor b/core/cpu/x86/32/32.factor index d3e33c46bd..4ed186d769 100755 --- a/core/cpu/x86/32/32.factor +++ b/core/cpu/x86/32/32.factor @@ -261,6 +261,10 @@ windows? [ cell "ulonglong" c-type set-c-type-align ] unless +macosx? [ + cell "double" c-type set-c-type-align +] when + T{ x86-backend f 4 } compiler-backend set-global : sse2? "Intrinsic" throw ; From 1ae14bbacfcc5c4a58d904779d286a745979a750 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 17:53:04 -0600 Subject: [PATCH 087/269] skeletonize mysql --- extra/db/mysql/lib/lib.factor | 102 ++++++++++++++-------------------- extra/db/mysql/mysql.factor | 45 ++++++++++++++- 2 files changed, 87 insertions(+), 60 deletions(-) diff --git a/extra/db/mysql/lib/lib.factor b/extra/db/mysql/lib/lib.factor index 7d5c2d55dc..59d1b6ff3d 100644 --- a/extra/db/mysql/lib/lib.factor +++ b/extra/db/mysql/lib/lib.factor @@ -14,81 +14,65 @@ TUPLE: mysql-result-set ; : new-mysql ( -- conn ) f mysql_init ; - -: mysql-error-string ( mysql-connection -- str ) - mysql-db-handle mysql_error ; : mysql-error ( mysql -- ) - mysql-error-string throw ; + [ mysql_error throw ] when* ; : mysql-connect ( mysql-connection -- ) - init-mysql swap - [ set-mysql-connection-mysqlconn ] 2keep - [ mysql-connection-host ] keep - [ mysql-connection-user ] keep - [ mysql-connection-password ] keep - [ mysql-connection-db ] keep - [ mysql-connection-port f 0 mysql_real_connect ] keep - [ set-mysql-connection-handle ] keep - dup mysql-connection-handle - [ connect-error-msg throw ] unless ; + new-mysql over set-mysql-db-handle + dup { + mysql-db-handle + mysql-db-host + mysql-db-user + mysql-db-password + mysql-db-db + mysql-db-port + } get-slots f 0 mysql_real_connect mysql-error ; ! ========================================================= ! Low level mysql utility definitions ! ========================================================= : (mysql-query) ( mysql-connection query -- ret ) - >r mysql-connection-mysqlconn r> mysql_query ; + >r mysql-db-handle r> mysql_query ; -: (mysql-result) ( mysql-connection -- ret ) - [ mysql-connection-mysqlconn mysql_use_result ] keep - [ set-mysql-connection-resulthandle ] keep ; - -: (mysql-affected-rows) ( mysql-connection -- n ) - mysql-connection-mysqlconn mysql_affected_rows ; +! : (mysql-result) ( mysql-connection -- ret ) + ! [ mysql-db-handle mysql_use_result ] keep + ! [ set-mysql-connection-resulthandle ] keep ; -: (mysql-free-result) ( mysql-connection -- ) - mysql-connection-resulthandle drop ; +! : (mysql-affected-rows) ( mysql-connection -- n ) + ! mysql-connection-mysqlconn mysql_affected_rows ; -: (mysql-row) ( mysql-connection -- row ) - mysql-connection-resulthandle mysql_fetch_row ; +! : (mysql-free-result) ( mysql-connection -- ) + ! mysql-connection-resulthandle drop ; -: (mysql-num-cols) ( mysql-connection -- n ) - mysql-connection-resulthandle mysql_num_fields ; +! : (mysql-row) ( mysql-connection -- row ) + ! mysql-connection-resulthandle mysql_fetch_row ; + +! : (mysql-num-cols) ( mysql-connection -- n ) + ! mysql-connection-resulthandle mysql_num_fields ; -: mysql-char*-nth ( index object -- str ) - #! Utility based on 'char*-nth' to perform an additional sanity check on the value - #! extracted from the array of strings. - void*-nth [ alien>char-string ] [ "" ] if* ; - -: mysql-row>seq ( object n -- seq ) - [ swap mysql-char*-nth ] map-with ; - -: (mysql-result>seq) ( seq -- seq ) - my-conn get (mysql-row) dup [ - my-conn get (mysql-num-cols) mysql-row>seq - over push - (mysql-result>seq) - ] [ drop ] if - ! Perform needed cleanup on fetched results - my-conn get (mysql-free-result) ; - -! ========================================================= -! Public Word Definitions -! ========================================================= +! : mysql-char*-nth ( index object -- str ) + ! #! Utility based on 'char*-nth' to perform an additional sanity check on the value + ! #! extracted from the array of strings. + ! void*-nth [ alien>char-string ] [ "" ] if* ; +! : mysql-row>seq ( object n -- seq ) + ! [ swap mysql-char*-nth ] map-with ; -: mysql-query ( query -- ret ) - >r my-conn get r> (mysql-query) drop - my-conn get (mysql-result) ; +! : (mysql-result>seq) ( seq -- seq ) + ! my-conn get (mysql-row) dup [ + ! my-conn get (mysql-num-cols) mysql-row>seq + ! over push + ! (mysql-result>seq) + ! ] [ drop ] if + ! ! Perform needed cleanup on fetched results + ! my-conn get (mysql-free-result) ; -: mysql-command ( query -- n ) - mysql-query drop - my-conn get (mysql-affected-rows) ; +! : mysql-query ( query -- ret ) + ! >r my-conn get r> (mysql-query) drop + ! my-conn get (mysql-result) ; -: with-mysql ( host user password db port quot -- ) - [ - >r my-conn set - my-conn get mysql-connect drop r> - [ my-conn get mysql-close ] cleanup - ] with-scope ; inline +! : mysql-command ( query -- n ) + ! mysql-query drop + ! my-conn get (mysql-affected-rows) ; diff --git a/extra/db/mysql/mysql.factor b/extra/db/mysql/mysql.factor index 8043bc2782..941c25e1fa 100644 --- a/extra/db/mysql/mysql.factor +++ b/extra/db/mysql/mysql.factor @@ -1,10 +1,12 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for license. USING: alien continuations io kernel prettyprint sequences -db ; +db db.mysql.ffi ; IN: db.mysql TUPLE: mysql-db handle host user password db port ; +TUPLE: mysql-statement ; +TUPLE: mysql-result-set ; M: mysql-db db-open ( mysql-db -- ) ; @@ -13,3 +15,44 @@ M: mysql-db dispose ( mysql-db -- ) mysql-db-handle mysql_close ; +M: mysql-db ( str -- statement ) + ; + +M: mysql-db ( str -- statement ) + ; + +M: mysql-statement prepare-statement ( statement -- ) + ; + +M: mysql-statement bind-statement* ( statement -- ) + ; + +M: mysql-statement rebind-statement ( statement -- ) + ; + +M: mysql-statement execute-statement ( statement -- ) + ; + +M: mysql-statement query-results ( query -- result-set ) + ; + +M: mysql-result-set #rows ( result-set -- n ) + ; + +M: mysql-result-set #columns ( result-set -- n ) + ; + +M: mysql-result-set row-column ( result-set n -- obj ) + ; + +M: mysql-result-set advance-row ( result-set -- ? ) + ; + +M: mysql-db begin-transaction ( -- ) + ; + +M: mysql-db commit-transaction ( -- ) + ; + +M: mysql-db rollback-transaction ( -- ) + ; From 21183af0ceb70821d6de9b6c0dcc5b8f824522ff Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 17:56:00 -0600 Subject: [PATCH 088/269] remove sudo requirement --- misc/factor.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/misc/factor.sh b/misc/factor.sh index fa8cdcd5b1..d1ef738cd9 100755 --- a/misc/factor.sh +++ b/misc/factor.sh @@ -45,7 +45,6 @@ check_gcc_version() { } check_installed_programs() { - ensure_program_installed sudo ensure_program_installed chmod ensure_program_installed uname ensure_program_installed git From e9b5a6b9d30a1ad21d46978731c4ffc202df8b43 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 19:38:19 -0600 Subject: [PATCH 089/269] with-process-stream waits for process exit --- extra/io/launcher/launcher-docs.factor | 4 ++-- extra/io/launcher/launcher.factor | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/extra/io/launcher/launcher-docs.factor b/extra/io/launcher/launcher-docs.factor index c30516a83f..e372f7a41e 100755 --- a/extra/io/launcher/launcher-docs.factor +++ b/extra/io/launcher/launcher-docs.factor @@ -146,8 +146,8 @@ HELP: with-process-stream { $values { "desc" "a launch descriptor" } { "quot" quotation } - { "process" process } } -{ $description "Calls " { $snippet "quot" } " in a dynamic scope where " { $link stdio } " is rebound to a " { $link process-stream } ". When the quotation returns, the " { $link process } " instance is output." } ; + { "status" "an exit code" } } +{ $description "Calls " { $snippet "quot" } " in a dynamic scope where " { $link stdio } " is rebound to a " { $link process-stream } ". After the quotation returns, waits for the process to end and outputs the exit code." } ; HELP: wait-for-process { $values { "process" process } { "status" integer } } diff --git a/extra/io/launcher/launcher.factor b/extra/io/launcher/launcher.factor index 09a77fe985..9be90d28de 100755 --- a/extra/io/launcher/launcher.factor +++ b/extra/io/launcher/launcher.factor @@ -98,10 +98,10 @@ TUPLE: process-stream process ; { set-delegate set-process-stream-process } process-stream construct ; -: with-process-stream ( desc quot -- process ) +: with-process-stream ( desc quot -- status ) swap [ swap with-stream ] keep - process-stream-process ; inline + process-stream-process wait-for-process ; inline : notify-exit ( status process -- ) [ set-process-status ] keep From 2872bc9d306c553b1546a46983d660d36e6dcafd Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 19:38:31 -0600 Subject: [PATCH 090/269] More method cleanups --- core/compiler/compiler.factor | 2 +- core/effects/effects.factor | 20 +++++++---------- core/generic/generic-docs.factor | 4 ---- core/generic/generic-tests.factor | 3 +++ core/generic/generic.factor | 32 +++++++++++++++------------ core/generic/standard/standard.factor | 2 +- core/inference/backend/backend.factor | 4 ++-- core/words/words.factor | 5 ++++- 8 files changed, 37 insertions(+), 35 deletions(-) diff --git a/core/compiler/compiler.factor b/core/compiler/compiler.factor index 631c2e4f53..2674734483 100755 --- a/core/compiler/compiler.factor +++ b/core/compiler/compiler.factor @@ -26,7 +26,7 @@ IN: compiler >r dupd save-effect r> f pick compiler-error over compiled-unxref - compiled-xref ; + over crossref? [ compiled-xref ] [ 2drop ] if ; : compile-succeeded ( word -- effect dependencies ) [ diff --git a/core/effects/effects.factor b/core/effects/effects.factor index 10ebca6dea..23e8daf122 100755 --- a/core/effects/effects.factor +++ b/core/effects/effects.factor @@ -1,4 +1,4 @@ -! Copyright (C) 2006, 2007 Slava Pestov. +! Copyright (C) 2006, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: kernel math namespaces sequences strings words assocs combinators ; @@ -41,17 +41,13 @@ M: integer (stack-picture) drop "object" ; ")" % ] "" make ; -: stack-effect ( word -- effect/f ) - { - { [ dup symbol? ] [ drop 0 1 ] } - { [ dup "parent-generic" word-prop ] [ - "parent-generic" word-prop stack-effect - ] } - { [ t ] [ - { "declared-effect" "inferred-effect" } - swap word-props [ at ] curry map [ ] find nip - ] } - } cond ; +GENERIC: stack-effect ( word -- effect/f ) + +M: symbol stack-effect drop 0 1 ; + +M: word stack-effect + { "declared-effect" "inferred-effect" } + swap word-props [ at ] curry map [ ] find nip ; M: effect clone [ effect-in clone ] keep effect-out clone ; diff --git a/core/generic/generic-docs.factor b/core/generic/generic-docs.factor index f4da9575e9..631aa7e62d 100755 --- a/core/generic/generic-docs.factor +++ b/core/generic/generic-docs.factor @@ -107,10 +107,6 @@ HELP: make-generic { $description "Regenerates the definition of a generic word by applying the method combination to the set of defined methods." } $low-level-note ; -HELP: init-methods -{ $values { "word" word } } -{ $description "Prepare to define a generic word." } ; - HELP: define-generic { $values { "word" word } { "combination" "a method combination" } } { $description "Defines a generic word. A method combination is an object which responds to the " { $link perform-combination } " generic word." } diff --git a/core/generic/generic-tests.factor b/core/generic/generic-tests.factor index dc888ec30c..f0d5bf3063 100755 --- a/core/generic/generic-tests.factor +++ b/core/generic/generic-tests.factor @@ -176,6 +176,9 @@ M: f tag-and-f 4 ; ! define-class hashing issue TUPLE: debug-combination ; +M: debug-combination make-default-method + 2drop [ "Oops" throw ] when ; + M: debug-combination perform-combination drop order [ dup class-hashes ] { } map>assoc sort-keys diff --git a/core/generic/generic.factor b/core/generic/generic.factor index 951813dbcd..78577eaed4 100755 --- a/core/generic/generic.factor +++ b/core/generic/generic.factor @@ -1,8 +1,8 @@ -! Copyright (C) 2006, 2007 Slava Pestov. +! Copyright (C) 2006, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: words kernel sequences namespaces assocs hashtables definitions kernel.private classes classes.private -quotations arrays vocabs ; +quotations arrays vocabs effects ; IN: generic ! Method combination protocol @@ -65,15 +65,20 @@ TUPLE: check-method class generic ; : make-method-def ( quot word combination -- quot ) "combination" word-prop method-prologue swap append ; +PREDICATE: word method-body "method" word-prop >boolean ; + +M: method-body stack-effect + "method" word-prop method-generic stack-effect ; + : ( quot class generic -- word ) [ make-method-def ] 2keep - [ method-word-name f dup ] keep - "parent-generic" set-word-prop + method-word-name f dup rot define ; : ( quot class generic -- method ) check-method - [ ] 3keep f \ method construct-boa ; + [ ] 3keep f \ method construct-boa + dup method-word over "method" set-word-prop ; : define-method ( quot class generic -- ) >r bootstrap-word r> @@ -120,13 +125,12 @@ M: class forget* ( class -- ) M: assoc update-methods ( assoc -- ) implementors* [ make-generic ] each ; -: init-methods ( word -- ) - dup "methods" word-prop - H{ } assoc-like - "methods" set-word-prop ; - : define-generic ( word combination -- ) - 2dup "combination" set-word-prop - dupd define-default-method - dup init-methods - make-generic ; + over "combination" word-prop over = [ + 2drop + ] [ + 2dup "combination" set-word-prop + over H{ } clone "methods" set-word-prop + dupd define-default-method + make-generic + ] if ; diff --git a/core/generic/standard/standard.factor b/core/generic/standard/standard.factor index 94ac82a0e4..d52208ccbf 100755 --- a/core/generic/standard/standard.factor +++ b/core/generic/standard/standard.factor @@ -10,7 +10,7 @@ TUPLE: standard-combination # ; M: standard-combination method-prologue standard-combination-# object - swap add [ declare ] curry ; + swap add* [ declare ] curry ; C: standard-combination diff --git a/core/inference/backend/backend.factor b/core/inference/backend/backend.factor index 34179bbf32..b839b047d6 100755 --- a/core/inference/backend/backend.factor +++ b/core/inference/backend/backend.factor @@ -10,8 +10,8 @@ IN: inference.backend recursive-state get at ; : inline? ( word -- ? ) - dup "parent-generic" word-prop - [ inline? ] [ "inline" word-prop ] ?if ; + dup "method" word-prop + [ method-generic inline? ] [ "inline" word-prop ] ?if ; : local-recursive-state ( -- assoc ) recursive-state get dup keys diff --git a/core/words/words.factor b/core/words/words.factor index b4062d8f02..93b1185335 100755 --- a/core/words/words.factor +++ b/core/words/words.factor @@ -116,13 +116,16 @@ SYMBOL: changed-words [ no-compilation-unit ] unless* set-at ; +: crossref? ( word -- ? ) + dup word-vocabulary swap "method" word-prop or ; + : define ( word def -- ) [ ] like over unxref over redefined over set-word-def dup changed-word - dup word-vocabulary [ dup xref ] when drop ; + dup crossref? [ dup xref ] when drop ; : define-declared ( word def effect -- ) pick swap "declared-effect" set-word-prop From 77a2a2136a0d4837c6f00e66d784fce9bf8d8a97 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 19:43:10 -0600 Subject: [PATCH 091/269] Better method usages work in progres --- core/generic/generic.factor | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/generic/generic.factor b/core/generic/generic.factor index 78577eaed4..2100f49423 100755 --- a/core/generic/generic.factor +++ b/core/generic/generic.factor @@ -134,3 +134,13 @@ M: assoc update-methods ( assoc -- ) dupd define-default-method make-generic ] if ; + +: subwords ( generic -- seq ) + dup "methods" word-prop values + swap "default-method" word-prop add + [ method-word ] map ; + +: xref-generics ( -- ) + all-words + [ generic? ] subset + [ subwords [ xref ] each ] each ; From 3433adefbe9e8397e5a0f84b4275b50d4da100f0 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 19:58:07 -0600 Subject: [PATCH 092/269] Fix wait-for-pid --- extra/unix/process/process.factor | 2 +- extra/unix/unix.factor | 36 +++++++++++++++++++------------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/extra/unix/process/process.factor b/extra/unix/process/process.factor index fb4271ea23..8b7144b979 100755 --- a/extra/unix/process/process.factor +++ b/extra/unix/process/process.factor @@ -32,4 +32,4 @@ IN: unix.process fork dup zero? -roll swap curry if ; inline : wait-for-pid ( pid -- status ) - 0 [ 0 waitpid drop ] keep *int ; \ No newline at end of file + 0 [ 0 waitpid drop ] keep *int WEXITSTATUS ; \ No newline at end of file diff --git a/extra/unix/unix.factor b/extra/unix/unix.factor index 7c3467b052..750a4b5044 100755 --- a/extra/unix/unix.factor +++ b/extra/unix/unix.factor @@ -177,31 +177,39 @@ FUNCTION: int kill ( pid_t pid, int sig ) ; ! Flags for waitpid -: WNOHANG 1 ; -: WUNTRACED 2 ; +: WNOHANG 1 ; inline +: WUNTRACED 2 ; inline -: WSTOPPED 2 ; -: WEXITED 4 ; -: WCONTINUED 8 ; -: WNOWAIT HEX: 1000000 ; +: WSTOPPED 2 ; inline +: WEXITED 4 ; inline +: WCONTINUED 8 ; inline +: WNOWAIT HEX: 1000000 ; inline ! Examining status -: WTERMSIG ( status -- value ) HEX: 7f bitand ; +: WTERMSIG ( status -- value ) + HEX: 7f bitand ; inline -: WIFEXITED ( status -- ? ) WTERMSIG zero? ; +: WIFEXITED ( status -- ? ) + WTERMSIG zero? ; inline -: WEXITSTATUS ( status -- value ) HEX: ff00 bitand -8 shift ; +: WEXITSTATUS ( status -- value ) + HEX: ff00 bitand -8 shift ; inline -: WIFSIGNALED ( status -- ? ) HEX: 7f bitand 1+ -1 shift 0 > ; +: WIFSIGNALED ( status -- ? ) + HEX: 7f bitand 1+ -1 shift 0 > ; inline -: WCOREFLAG ( -- value ) HEX: 80 ; +: WCOREFLAG ( -- value ) + HEX: 80 ; inline -: WCOREDUMP ( status -- ? ) WCOREFLAG bitand zero? not ; +: WCOREDUMP ( status -- ? ) + WCOREFLAG bitand zero? not ; inline -: WIFSTOPPED ( status -- ? ) HEX: ff bitand HEX: 7f = ; +: WIFSTOPPED ( status -- ? ) + HEX: ff bitand HEX: 7f = ; inline -: WSTOPSIG ( status -- value ) WEXITSTATUS ; +: WSTOPSIG ( status -- value ) + WEXITSTATUS ; inline FUNCTION: pid_t wait ( int* status ) ; FUNCTION: pid_t waitpid ( pid_t wpid, int* status, int options ) ; From f1989fc8c6142671cc27d5f0d14041b05a104d50 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 20:10:00 -0600 Subject: [PATCH 093/269] Fix io.launcher again --- extra/io/unix/launcher/launcher.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/io/unix/launcher/launcher.factor b/extra/io/unix/launcher/launcher.factor index b44ac80159..93278e2b1a 100755 --- a/extra/io/unix/launcher/launcher.factor +++ b/extra/io/unix/launcher/launcher.factor @@ -111,7 +111,7 @@ M: unix-io process-stream* 2drop t ] [ find-process dup [ - >r *uint r> notify-exit f + >r *int WEXITSTATUS r> notify-exit f ] [ 2drop f ] if From 6aabef8e3213d0a92fff3688142ae30b5b5e066b Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 20:49:40 -0600 Subject: [PATCH 094/269] git pull to master delete staging.*.image --- misc/factor.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/misc/factor.sh b/misc/factor.sh index d1ef738cd9..c8e0456b3a 100755 --- a/misc/factor.sh +++ b/misc/factor.sh @@ -196,7 +196,7 @@ git_clone() { git_pull_factorcode() { echo "Updating the git repository from factorcode.org..." - git pull git://factorcode.org/git/factor.git + git pull git://factorcode.org/git/factor.git master check_ret git } @@ -219,6 +219,7 @@ delete_boot_images() { echo "Deleting old images..." rm $BOOT_IMAGE > /dev/null 2>&1 rm $BOOT_IMAGE.* > /dev/null 2>&1 + rm staging.*.image > /dev/null 2>&1 } get_boot_image() { From b2cd79ebddb28c312dd1f9bce7bdd756cf6a0bbf Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 20:49:59 -0600 Subject: [PATCH 095/269] Fix deploy --- extra/tools/deploy/backend/backend.factor | 1 + 1 file changed, 1 insertion(+) diff --git a/extra/tools/deploy/backend/backend.factor b/extra/tools/deploy/backend/backend.factor index d768b6a334..95d19712c0 100755 --- a/extra/tools/deploy/backend/backend.factor +++ b/extra/tools/deploy/backend/backend.factor @@ -80,6 +80,7 @@ IN: tools.deploy.backend ] { } make ; : make-deploy-image ( vm image vocab config -- ) + make-boot-image dup staging-image-name exists? [ >r pick r> tuck make-staging-image ] unless From 1f66e8173f955a28416560be41f28707b68bba31 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Mon, 4 Feb 2008 21:26:59 -0600 Subject: [PATCH 096/269] builder: convert to io.launcher --- extra/builder/builder.factor | 151 +++++++++++++++++++---------------- 1 file changed, 82 insertions(+), 69 deletions(-) diff --git a/extra/builder/builder.factor b/extra/builder/builder.factor index 38570ae46f..cb0720d0a9 100644 --- a/extra/builder/builder.factor +++ b/extra/builder/builder.factor @@ -1,7 +1,7 @@ -USING: kernel io io.files io.launcher tools.deploy.backend - system namespaces sequences splitting math.parser - unix prettyprint tools.time calendar bake vars ; +USING: kernel io io.files io.launcher hashtables tools.deploy.backend + system continuations namespaces sequences splitting math.parser + prettyprint tools.time calendar bake vars http.client ; IN: builder @@ -19,16 +19,20 @@ IN: builder SYMBOL: builder-recipients -: quote ( str -- str ) "'" swap "'" 3append ; - : email-file ( subject file -- ) `{ - "cat" , - "| mutt -s" ,[ quote ] - "-x" %[ builder-recipients get ] - } - " " join system drop ; - + { +stdin+ , } + { +arguments+ { "mutt" "-s" , %[ builder-recipients get ] } } + } + >hashtable run-process drop ; + +: email-string ( subject -- ) + `{ "mutt" "-s" , %[ builder-recipients get ] } + + dup + dispose + process-stream-process wait-for-process drop ; + ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! : target ( -- target ) `{ ,[ os ] %[ cpu "." split ] } "-" join ; @@ -41,74 +45,83 @@ VAR: stamp : build ( -- ) -datestamp >stamp + datestamp >stamp -"/builds/factor" cd -"git pull git://factorcode.org/git/factor.git" system -0 = -[ ] -[ - "builder: git pull" "/dev/null" email-file - "builder: git pull" throw -] -if + "/builds/factor" cd + + { "git" "pull" "--no-summary" "git://factorcode.org/git/factor.git" } + run-process process-status + 0 = + [ ] + [ + "builder: git pull" email-string + "builder: git pull" throw + ] + if -"/builds/" stamp> append make-directory -"/builds/" stamp> append cd -"git clone /builds/factor" system drop + "/builds/" stamp> append make-directory + "/builds/" stamp> append cd -"factor" cd + { "git" "clone" "/builds/factor" } run-process drop -{ "git" "show" } -[ readln ] with-stream -" " split second -"../git-id" [ print ] with-stream + "factor" cd -"make clean" system drop + { "git" "show" } + [ readln ] with-stream + " " split second + "../git-id" [ print ] with-stream -"make " target " > ../compile-log" 3append system -0 = -[ ] -[ - "builder: vm compile" "../compile-log" email-file - "builder: vm compile" throw -] if + { "make" "clean" } run-process drop -"wget http://factorcode.org/images/latest/" boot-image-name append system -0 = -[ ] -[ - "builder: image download" "/dev/null" email-file - "builder: image download" throw -] if + `{ + { +arguments+ { "make" ,[ target ] } } + { +stdout+ "../compile-log" } + { +stderr+ +stdout+ } + } + >hashtable run-process process-status + 0 = + [ ] + [ + "builder: vm compile" "../compile-log" email-file + "builder: vm compile" throw + ] if -[ - "./factor -i=" boot-image-name " -no-user-init > ../boot-log" - 3append - system -] -benchmark nip -"../boot-time" [ . ] with-stream -0 = -[ ] -[ - "builder: bootstrap" "../boot-log" email-file - "builder: bootstrap" throw -] if + [ "http://factorcode.org/images/latest/" boot-image-name append download ] + [ "builder: image download" email-string ] + recover -[ - "./factor -e='USE: tools.browser load-everything' > ../load-everything-log" - system -] benchmark nip -"../load-everything-time" [ . ] with-stream -0 = -[ ] -[ - "builder: load-everything" "../load-everything-log" email-file - "builder: load-everything" throw -] if + `{ + { +arguments+ { + "./factor" + ,[ "-i=" boot-image-name append ] + "-no-user-init" + } } + { +stdout+ "../boot-log" } + { +stderr+ +stdout+ } + } + >hashtable + [ run-process process-status ] + benchmark nip "../boot-time" [ . ] with-stream + 0 = + [ ] + [ + "builder: bootstrap" "../boot-log" email-file + "builder: bootstrap" throw + ] if -; + `{ + { +arguments+ { "./factor" "-e=USE: tools.browser load-everything" } } + { +stdout+ "../load-everything-log" } + { +stderr+ +stdout+ } + } + >hashtable [ run-process process-status ] benchmark nip + "../load-everything-time" [ . ] with-stream + 0 = + [ ] + [ + "builder: load-everything" "../load-everything-log" email-file + "builder: load-everything" throw + ] if ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! From bd2226d89e09fa14a600238277166a490be96984 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Mon, 4 Feb 2008 21:58:57 -0600 Subject: [PATCH 097/269] builder: add factor-binary word --- extra/builder/builder.factor | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/extra/builder/builder.factor b/extra/builder/builder.factor index cb0720d0a9..d20b5b8e5b 100644 --- a/extra/builder/builder.factor +++ b/extra/builder/builder.factor @@ -1,7 +1,8 @@ USING: kernel io io.files io.launcher hashtables tools.deploy.backend system continuations namespaces sequences splitting math.parser - prettyprint tools.time calendar bake vars http.client ; + prettyprint tools.time calendar bake vars http.client + combinators ; IN: builder @@ -39,6 +40,15 @@ SYMBOL: builder-recipients ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +: factor-binary ( -- name ) + os + { { "macosx" [ "./Factor.app/Contents/MacOS/factor" ] } + { "windows" [ "./factor-nt.exe" ] } + [ drop "./factor" ] } + case ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + VAR: stamp ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -92,7 +102,7 @@ VAR: stamp `{ { +arguments+ { - "./factor" + ,[ factor-binary ] ,[ "-i=" boot-image-name append ] "-no-user-init" } } @@ -110,7 +120,8 @@ VAR: stamp ] if `{ - { +arguments+ { "./factor" "-e=USE: tools.browser load-everything" } } + { +arguments+ + { ,[ factor-binary ] "-e=USE: tools.browser load-everything" } } { +stdout+ "../load-everything-log" } { +stderr+ +stdout+ } } From 659b6d8f3c3e2ca0f5deed100e8ace971dd7e4c7 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 23:30:38 -0600 Subject: [PATCH 098/269] Better assert-depth error --- core/debugger/debugger.factor | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/core/debugger/debugger.factor b/core/debugger/debugger.factor index 77c6da38e9..53f3387d85 100755 --- a/core/debugger/debugger.factor +++ b/core/debugger/debugger.factor @@ -87,7 +87,32 @@ TUPLE: assert got expect ; : depth ( -- n ) datastack length ; -: assert-depth ( quot -- ) depth slip depth swap assert= ; +: trim-datastacks ( seq1 seq2 -- seq1' seq2' ) + 2dup [ length ] 2apply min tuck tail >r tail r> ; + +TUPLE: relative-underflow stack ; + +: relative-underflow ( before after -- * ) + trim-datastacks nip \ relative-underflow construct-boa throw ; + +M: relative-underflow summary + drop "Too many items removed from data stack" ; + +TUPLE: relative-overflow stack ; + +M: relative-overflow summary + drop "Superfluous items pushed to data stack" ; + +: relative-overflow ( before after -- * ) + trim-datastacks drop \ relative-overflow construct-boa throw ; + +: assert-depth ( quot -- ) + >r datastack r> swap slip >r datastack r> + 2dup [ length ] compare sgn { + { -1 [ relative-underflow ] } + { 0 [ 2drop ] } + { 1 [ relative-overflow ] } + } case ; inline : expired-error. ( obj -- ) "Object did not survive image save/load: " write third . ; @@ -222,9 +247,6 @@ M: redefine-error error. "Re-definition of " write redefine-error-def . ; -M: forward-error error. - "Forward reference to " write forward-error-word . ; - M: undefined summary drop "Calling a deferred word before it has been defined" ; From 87887a11654619d03ca37e7d63a87196c5506a7e Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 23:30:49 -0600 Subject: [PATCH 099/269] Monitors tweak --- extra/io/unix/linux/linux.factor | 10 ++-------- extra/io/windows/nt/monitor/monitor.factor | 16 ++++++++-------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/extra/io/unix/linux/linux.factor b/extra/io/unix/linux/linux.factor index 1707ac9546..dcf1beabf9 100755 --- a/extra/io/unix/linux/linux.factor +++ b/extra/io/unix/linux/linux.factor @@ -25,8 +25,6 @@ TUPLE: inotify watches ; : wd>monitor ( wd -- monitor ) watches at ; -: wd>path ( wd -- path ) wd>monitor linux-monitor-path ; - : ( -- port ) H{ } clone inotify_init dup io-error inotify @@ -89,12 +87,8 @@ M: linux-monitor dispose ( monitor -- ) ] { } make ; : parse-file-notify ( buffer -- changed path ) - { - inotify-event-wd - inotify-event-name - inotify-event-mask - } get-slots - parse-action -rot alien>char-string >r wd>path r> path+ ; + { inotify-event-name inotify-event-mask } get-slots + parse-action swap alien>char-string ; : events-exhausted? ( i buffer -- ? ) buffer-fill >= ; diff --git a/extra/io/windows/nt/monitor/monitor.factor b/extra/io/windows/nt/monitor/monitor.factor index d418dff270..6f956760a8 100755 --- a/extra/io/windows/nt/monitor/monitor.factor +++ b/extra/io/windows/nt/monitor/monitor.factor @@ -65,20 +65,20 @@ M: windows-nt-io ( path recursive? -- monitor ) { [ t ] [ +modify-file+ ] } } cond nip ; -: parse-file-notify ( directory buffer -- changed path ) +: parse-file-notify ( buffer -- changed path ) { FILE_NOTIFY_INFORMATION-FileName FILE_NOTIFY_INFORMATION-FileNameLength FILE_NOTIFY_INFORMATION-Action - } get-slots parse-action 1array -rot - memory>u16-string path+ ; + } get-slots parse-action 1array swap + memory>u16-string ; -: (changed-files) ( directory buffer -- ) - 2dup parse-file-notify changed-file +: (changed-files) ( buffer -- ) + dup parse-file-notify changed-file dup FILE_NOTIFY_INFORMATION-NextEntryOffset dup zero? - [ 3drop ] [ swap (changed-files) ] if ; + [ 2drop ] [ swap (changed-files) ] if ; M: windows-nt-io fill-queue ( monitor -- ) - dup win32-monitor-path over buffer-ptr pick read-changes - [ zero? [ 2drop ] [ (changed-files) ] if ] H{ } make-assoc + dup buffer-ptr over read-changes + [ zero? [ drop ] [ (changed-files) ] if ] H{ } make-assoc swap set-monitor-queue ; From 2d3298d611ab2fd1dcdfa2b7577928299d8de9bf Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 23:30:59 -0600 Subject: [PATCH 100/269] Method usages cleanup --- core/bootstrap/image/image.factor | 8 +------- core/bootstrap/stage2.factor | 1 + core/compiler/units/units-docs.factor | 9 +-------- core/compiler/units/units.factor | 5 ----- core/definitions/definitions-docs.factor | 4 +--- core/definitions/definitions-tests.factor | 4 +++- core/generic/generic-tests.factor | 2 +- core/generic/generic.factor | 13 ++++++++----- core/generic/standard/standard.factor | 2 +- core/inference/inference.factor | 11 +++++++---- core/optimizer/backend/backend.factor | 2 +- core/parser/parser-docs.factor | 4 +--- core/parser/parser-tests.factor | 4 ++-- core/parser/parser.factor | 10 +++------- core/source-files/source-files.factor | 14 ++++++++++++++ core/vocabs/loader/loader-tests.factor | 2 +- core/words/words-tests.factor | 3 ++- extra/tools/browser/browser.factor | 2 +- extra/tools/crossref/crossref.factor | 17 +---------------- 19 files changed, 50 insertions(+), 67 deletions(-) mode change 100644 => 100755 core/compiler/units/units-docs.factor diff --git a/core/bootstrap/image/image.factor b/core/bootstrap/image/image.factor index 60e73cb249..3dadee5193 100755 --- a/core/bootstrap/image/image.factor +++ b/core/bootstrap/image/image.factor @@ -203,14 +203,8 @@ M: f ' ! Words -DEFER: emit-word - -: emit-generic ( generic -- ) - dup "default-method" word-prop method-word emit-word - "methods" word-prop [ nip method-word emit-word ] assoc-each ; - : emit-word ( word -- ) - dup generic? [ dup emit-generic ] when + dup subwords [ emit-word ] each [ dup hashcode ' , dup word-name ' , diff --git a/core/bootstrap/stage2.factor b/core/bootstrap/stage2.factor index 7a0fab8a99..f3483add57 100755 --- a/core/bootstrap/stage2.factor +++ b/core/bootstrap/stage2.factor @@ -24,6 +24,7 @@ IN: bootstrap.stage2 "Cross-referencing..." print flush H{ } clone crossref set-global xref-words + xref-generics xref-sources ] unless diff --git a/core/compiler/units/units-docs.factor b/core/compiler/units/units-docs.factor old mode 100644 new mode 100755 index 363b5b5014..99124d40ae --- a/core/compiler/units/units-docs.factor +++ b/core/compiler/units/units-docs.factor @@ -28,9 +28,7 @@ HELP: redefine-error HELP: remember-definition { $values { "definition" "a definition specifier" } { "loc" "a " { $snippet "{ path line# }" } " pair" } } -{ $description "Saves the location of a definition and associates this definition with the current source file." -$nl -"This is the book-keeping required to detect " { $link redefine-error } " and " { $link forward-error } "." } ; +{ $description "Saves the location of a definition and associates this definition with the current source file." } ; HELP: old-definitions { $var-description "Stores an assoc where the keys form the set of definitions which were defined by " { $link file } " the most recent time it was loaded." } ; @@ -38,11 +36,6 @@ HELP: old-definitions HELP: new-definitions { $var-description "Stores an assoc where the keys form the set of definitions which were defined so far by the current parsing of " { $link file } "." } ; -HELP: forward-error -{ $values { "word" word } } -{ $description "Throws a " { $link forward-error } "." } -{ $description "Indicates a word is being referenced prior to the location of its most recent definition. This can only happen if a source file is loaded, and subsequently edited such that two dependent definitions are reversed." } ; - HELP: with-compilation-unit { $values { "quot" quotation } } { $description "Calls a quotation in a new compilation unit. The quotation can define new words and classes, as well as forget words. When the quotation returns, any changed words are recompiled, and changes are applied atomically." } diff --git a/core/compiler/units/units.factor b/core/compiler/units/units.factor index 68e1a79185..242ed9854a 100755 --- a/core/compiler/units/units.factor +++ b/core/compiler/units/units.factor @@ -26,11 +26,6 @@ TUPLE: redefine-error def ; over new-definitions get first key? [ dup redefine-error ] when new-definitions get second (remember-definition) ; -TUPLE: forward-error word ; - -: forward-error ( word -- ) - \ forward-error construct-boa throw ; - : forward-reference? ( word -- ? ) dup old-definitions get assoc-stack [ new-definitions get assoc-stack not ] diff --git a/core/definitions/definitions-docs.factor b/core/definitions/definitions-docs.factor index eec88bba0c..d855a14be9 100755 --- a/core/definitions/definitions-docs.factor +++ b/core/definitions/definitions-docs.factor @@ -52,9 +52,7 @@ $nl $nl "If the parser did not have special checks for this case, then the modified source file would still load, because when the definition of " { $snippet "hello-world" } " on line 4 is being parsed, the " { $snippet "world" } " word is already present in the dictionary from an earlier run. The developer would then not discover this mistake until attempting to load the source file into a fresh image." $nl -"Since this is undesirable, the parser explicitly raises an error if a source file refers to a word which is in the dictionary, but defined after it is used." -{ $subsection forward-error } -"If a source file raises a " { $link forward-error } " when loaded into a development image, then it would have raised a " { $link no-word } " error when loaded into a fresh image." +"Since this is undesirable, the parser explicitly raises a " { $link no-word } " error if a source file refers to a word which is in the dictionary, but defined after it is used." $nl "The parser also catches duplicate definitions. If an artifact is defined twice in the same source file, the earlier definition will never be accessible, and this is almost always a mistake, perhaps due to a bad choice of word names, or a copy and paste error. The parser raises an error in this case." { $subsection redefine-error } ; diff --git a/core/definitions/definitions-tests.factor b/core/definitions/definitions-tests.factor index a4cb4de902..f0b0888052 100755 --- a/core/definitions/definitions-tests.factor +++ b/core/definitions/definitions-tests.factor @@ -6,6 +6,8 @@ TUPLE: combination-1 ; M: combination-1 perform-combination 2drop { } [ ] each [ ] ; +M: combination-1 make-default-method 2drop [ "No method" throw ] ; + SYMBOL: generic-1 [ @@ -20,7 +22,7 @@ SYMBOL: generic-1 ] with-compilation-unit ] unit-test -GENERIC: some-generic +GENERIC: some-generic ( a -- b ) USE: arrays diff --git a/core/generic/generic-tests.factor b/core/generic/generic-tests.factor index f0d5bf3063..f1e1ebd6d2 100755 --- a/core/generic/generic-tests.factor +++ b/core/generic/generic-tests.factor @@ -177,7 +177,7 @@ M: f tag-and-f 4 ; TUPLE: debug-combination ; M: debug-combination make-default-method - 2drop [ "Oops" throw ] when ; + 2drop [ "Oops" throw ] ; M: debug-combination perform-combination drop diff --git a/core/generic/generic.factor b/core/generic/generic.factor index 2100f49423..453d72effb 100755 --- a/core/generic/generic.factor +++ b/core/generic/generic.factor @@ -73,7 +73,8 @@ M: method-body stack-effect : ( quot class generic -- word ) [ make-method-def ] 2keep method-word-name f - dup rot define ; + dup rot define + dup xref ; : ( quot class generic -- method ) check-method @@ -135,12 +136,14 @@ M: assoc update-methods ( assoc -- ) make-generic ] if ; -: subwords ( generic -- seq ) +GENERIC: subwords ( word -- seq ) + +M: word subwords drop f ; + +M: generic subwords dup "methods" word-prop values swap "default-method" word-prop add [ method-word ] map ; : xref-generics ( -- ) - all-words - [ generic? ] subset - [ subwords [ xref ] each ] each ; + all-words [ subwords [ xref ] each ] each ; diff --git a/core/generic/standard/standard.factor b/core/generic/standard/standard.factor index d52208ccbf..88f6a05bc2 100755 --- a/core/generic/standard/standard.factor +++ b/core/generic/standard/standard.factor @@ -91,7 +91,7 @@ TUPLE: no-method object generic ; : class-hash-dispatch-quot ( methods quot picker -- quot ) >r >r hash-methods r> map - hash-dispatch-quot r> [ class-hash ] rot 3append ; + hash-dispatch-quot r> [ class-hash ] rot 3append ; inline : big-generic ( methods -- quot ) [ small-generic ] picker class-hash-dispatch-quot ; diff --git a/core/inference/inference.factor b/core/inference/inference.factor index 0fc344dd85..3f52eaadf4 100755 --- a/core/inference/inference.factor +++ b/core/inference/inference.factor @@ -1,9 +1,9 @@ -! Copyright (C) 2004, 2007 Slava Pestov. +! Copyright (C) 2004, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: inference.backend inference.state inference.dataflow inference.known-words inference.transforms inference.errors -sequences prettyprint io effects kernel namespaces quotations -words vocabs ; +kernel io effects namespaces sequences quotations vocabs +generic words ; IN: inference GENERIC: infer ( quot -- effect ) @@ -28,4 +28,7 @@ M: callable dataflow-with ] with-infer nip ; : forget-errors ( -- ) - all-words [ f "no-effect" set-word-prop ] each ; + all-words [ + dup subwords [ f "no-effect" set-word-prop ] each + f "no-effect" set-word-prop + ] each ; diff --git a/core/optimizer/backend/backend.factor b/core/optimizer/backend/backend.factor index 27b1b1e0ec..9d75346091 100755 --- a/core/optimizer/backend/backend.factor +++ b/core/optimizer/backend/backend.factor @@ -256,7 +256,7 @@ M: #dispatch optimize-node* tuck dispatching-class dup [ swap [ 2array ] 2keep method method-word - dup word-def flat-length 5 >= + dup word-def flat-length 6 >= [ 1quotation ] [ word-def ] if ] [ 2drop t t diff --git a/core/parser/parser-docs.factor b/core/parser/parser-docs.factor index 30e259c033..d8d6c9b7bc 100755 --- a/core/parser/parser-docs.factor +++ b/core/parser/parser-docs.factor @@ -202,9 +202,7 @@ HELP: location HELP: save-location { $values { "definition" "a definition specifier" } } -{ $description "Saves the location of a definition and associates this definition with the current source file." -$nl -"This is the book-keeping required to detect " { $link redefine-error } " and " { $link forward-error } "." } ; +{ $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." } ; diff --git a/core/parser/parser-tests.factor b/core/parser/parser-tests.factor index b00e8e26b4..f503528a24 100755 --- a/core/parser/parser-tests.factor +++ b/core/parser/parser-tests.factor @@ -342,7 +342,7 @@ IN: temporary [ "IN: temporary \\ class-fwd-test" "redefining-a-class-3" parse-stream drop - ] catch [ forward-error? ] is? + ] catch [ no-word? ] is? ] unit-test [ ] [ @@ -354,7 +354,7 @@ IN: temporary [ "IN: temporary \\ class-fwd-test" "redefining-a-class-3" parse-stream drop - ] catch [ forward-error? ] is? + ] catch [ no-word? ] is? ] unit-test [ t ] [ diff --git a/core/parser/parser.factor b/core/parser/parser.factor index ffecf9493e..6d7ad47843 100755 --- a/core/parser/parser.factor +++ b/core/parser/parser.factor @@ -235,7 +235,8 @@ M: no-word summary : no-word ( name -- newword ) dup \ no-word construct-boa - swap words-named word-restarts throw-restarts + swap words-named [ forward-reference? not ] subset + word-restarts throw-restarts dup word-vocabulary (use+) ; : check-forward ( str word -- word ) @@ -244,7 +245,7 @@ M: no-word summary dup use get [ at ] with map [ ] subset [ forward-reference? not ] find nip - [ ] [ forward-error ] ?if + [ ] [ no-word ] ?if ] [ nip ] if ; @@ -415,11 +416,6 @@ SYMBOL: interactive-vocabs over stack. ] when 2drop ; -: outside-usages ( seq -- usages ) - dup [ - over usage [ pathname? not ] subset seq-diff - ] curry { } map>assoc ; - : filter-moved ( assoc -- newassoc ) [ drop where dup [ first ] when diff --git a/core/source-files/source-files.factor b/core/source-files/source-files.factor index c974145928..64ae2e376e 100755 --- a/core/source-files/source-files.factor +++ b/core/source-files/source-files.factor @@ -96,3 +96,17 @@ SYMBOL: file source-file-definitions old-definitions set [ ] [ file get rollback-source-file ] cleanup ] with-scope ; inline + +: smart-usage ( word -- definitions ) + \ f or usage [ + dup method-body? [ + "method" word-prop + { method-specializer method-generic } get-slots + 2array + ] when + ] map ; + +: outside-usages ( seq -- usages ) + dup [ + over smart-usage [ pathname? not ] subset seq-diff + ] curry { } map>assoc ; diff --git a/core/vocabs/loader/loader-tests.factor b/core/vocabs/loader/loader-tests.factor index f38276d318..560affa566 100755 --- a/core/vocabs/loader/loader-tests.factor +++ b/core/vocabs/loader/loader-tests.factor @@ -79,7 +79,7 @@ IN: temporary "resource:core/vocabs/loader/test/a/a.factor" parse-stream - ] catch [ forward-error? ] is? + ] catch [ no-word? ] is? ] unit-test 0 "count-me" set-global diff --git a/core/words/words-tests.factor b/core/words/words-tests.factor index 2455250dc9..35a2421e71 100755 --- a/core/words/words-tests.factor +++ b/core/words/words-tests.factor @@ -87,7 +87,8 @@ FORGET: foe ] unit-test [ t ] [ - \ * usage [ word? ] subset [ interned? not ] subset empty? + \ * usage [ word? ] subset + [ dup interned? swap method-body? or ] all? ] unit-test DEFER: calls-a-gensym diff --git a/extra/tools/browser/browser.factor b/extra/tools/browser/browser.factor index 370e55eb97..dabc37e5de 100755 --- a/extra/tools/browser/browser.factor +++ b/extra/tools/browser/browser.factor @@ -238,7 +238,7 @@ C: vocab-author : vocab-xref ( vocab quot -- vocabs ) >r dup vocab-name swap words r> map [ [ word? ] subset [ word-vocabulary ] map ] map>set - remove [ vocab ] map ; inline + remove [ ] subset [ vocab ] map ; inline : vocab-uses ( vocab -- vocabs ) [ uses ] vocab-xref ; diff --git a/extra/tools/crossref/crossref.factor b/extra/tools/crossref/crossref.factor index 663df61926..f6561e9f26 100755 --- a/extra/tools/crossref/crossref.factor +++ b/extra/tools/crossref/crossref.factor @@ -3,7 +3,7 @@ USING: arrays definitions assocs io kernel math namespaces prettyprint sequences strings io.styles words generic tools.completion quotations parser inspector -sorting hashtables vocabs ; +sorting hashtables vocabs parser source-files ; IN: tools.crossref : synopsis-alist ( definitions -- alist ) @@ -12,21 +12,6 @@ IN: tools.crossref : definitions. ( alist -- ) [ write-object nl ] assoc-each ; -: (method-usage) ( word generic -- methods ) - tuck methods - [ second uses member? ] with subset keys - swap [ 2array ] curry map ; - -: method-usage ( word seq -- methods ) - [ generic? ] subset [ (method-usage) ] with map concat ; - -: compound-usage ( words -- seq ) - [ generic? not ] subset ; - -: smart-usage ( word -- definitions ) - \ f or - dup usage dup compound-usage -rot method-usage append ; - : usage. ( word -- ) smart-usage synopsis-alist sort-keys definitions. ; From 751a1da3d2fb1ee36d4d5e01238307ff371c4a2f Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 4 Feb 2008 23:48:18 -0600 Subject: [PATCH 101/269] Builder tweak --- extra/builder/builder.factor | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) mode change 100644 => 100755 extra/builder/builder.factor diff --git a/extra/builder/builder.factor b/extra/builder/builder.factor old mode 100644 new mode 100755 index d20b5b8e5b..3216105d47 --- a/extra/builder/builder.factor +++ b/extra/builder/builder.factor @@ -14,7 +14,7 @@ IN: builder ,[ dup timestamp-day ] ,[ dup timestamp-hour ] ,[ timestamp-minute ] } - [ number>string 2 CHAR: 0 pad-left ] map "-" join ; + [ pad-00 ] map "-" join ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -29,10 +29,7 @@ SYMBOL: builder-recipients : email-string ( subject -- ) `{ "mutt" "-s" , %[ builder-recipients get ] } - - dup - dispose - process-stream-process wait-for-process drop ; + [ ] with-process-stream drop ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! From ba1a958a321efdec8be27cdb4c7b0edcffd13468 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 13:11:36 -0600 Subject: [PATCH 102/269] Move cd and cwd primitives to native I/O, fix Windows normalize-pathname --- core/bootstrap/primitives.factor | 2 - core/bootstrap/stage2.factor | 6 +-- core/io/files/files-docs.factor | 4 +- core/io/files/files.factor | 6 ++- extra/io/unix/files/files.factor | 9 +++- extra/io/windows/nt/backend/backend.factor | 37 +------------ extra/io/windows/nt/files/files.factor | 62 ++++++++++++++++++++-- extra/io/windows/nt/nt-tests.factor | 6 ++- extra/unix/bsd/bsd.factor | 2 + extra/unix/linux/linux.factor | 2 + extra/unix/unix.factor | 1 + extra/windows/kernel32/kernel32.factor | 6 ++- vm/io.h | 2 - vm/os-unix.c | 13 ----- vm/os-windows-ce.c | 10 ---- vm/os-windows-nt.c | 15 ------ vm/os-windows.h | 1 + vm/primitives.c | 2 - 18 files changed, 93 insertions(+), 93 deletions(-) mode change 100644 => 100755 extra/unix/bsd/bsd.factor mode change 100644 => 100755 extra/unix/linux/linux.factor mode change 100644 => 100755 vm/io.h diff --git a/core/bootstrap/primitives.factor b/core/bootstrap/primitives.factor index 550aac71b0..967840a3dc 100755 --- a/core/bootstrap/primitives.factor +++ b/core/bootstrap/primitives.factor @@ -553,8 +553,6 @@ builtins get num-tags get tail f union-class define-class { "millis" "system" } { "type" "kernel.private" } { "tag" "kernel.private" } - { "cwd" "io.files" } - { "cd" "io.files" } { "modify-code-heap" "compiler.units" } { "dlopen" "alien" } { "dlsym" "alien" } diff --git a/core/bootstrap/stage2.factor b/core/bootstrap/stage2.factor index f3483add57..c601ba7671 100755 --- a/core/bootstrap/stage2.factor +++ b/core/bootstrap/stage2.factor @@ -1,11 +1,11 @@ -! Copyright (C) 2004, 2007 Slava Pestov. +! Copyright (C) 2004, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: init command-line namespaces words debugger io kernel.private math memory continuations kernel io.files io.backend system parser vocabs sequences prettyprint vocabs.loader combinators splitting source-files strings definitions assocs compiler.errors compiler.units -math.parser ; +math.parser generic ; IN: bootstrap.stage2 ! Wrap everything in a catch which starts a listener so @@ -88,5 +88,5 @@ IN: bootstrap.stage2 "output-image" get resource-path save-image-and-exit ] if ] [ - print-error :c "listener" vocab-main execute + print-error :c "listener" vocab-main execute 1 exit ] recover diff --git a/core/io/files/files-docs.factor b/core/io/files/files-docs.factor index 3a23c8f6ef..0b9a748eb8 100755 --- a/core/io/files/files-docs.factor +++ b/core/io/files/files-docs.factor @@ -52,12 +52,12 @@ HELP: { $description "Outputs an output stream for writing to the specified pathname. The stream begins writing at the end of the file." } { $errors "Throws an error if the file cannot be opened for writing." } ; -HELP: cwd ( -- path ) +HELP: cwd { $values { "path" "a pathname string" } } { $description "Outputs the current working directory of the Factor process." } { $errors "Windows CE has no concept of ``current directory'', so this word throws an error there." } ; -HELP: cd ( path -- ) +HELP: cd { $values { "path" "a pathname string" } } { $description "Changes the current working directory of the Factor process." } { $errors "Windows CE has no concept of ``current directory'', so this word throws an error there." } ; diff --git a/core/io/files/files.factor b/core/io/files/files.factor index 6e4648b590..9952e6387b 100755 --- a/core/io/files/files.factor +++ b/core/io/files/files.factor @@ -1,10 +1,14 @@ -! Copyright (C) 2004, 2007 Slava Pestov. +! Copyright (C) 2004, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. IN: io.files USING: io.backend io.files.private io hashtables kernel math memory namespaces sequences strings assocs arrays definitions system combinators splitting sbufs ; +HOOK: cd io-backend ( path -- ) + +HOOK: cwd io-backend ( -- path ) + HOOK: io-backend ( path -- stream ) HOOK: io-backend ( path -- stream ) diff --git a/extra/io/unix/files/files.factor b/extra/io/unix/files/files.factor index edee598435..3201c29c45 100755 --- a/extra/io/unix/files/files.factor +++ b/extra/io/unix/files/files.factor @@ -1,9 +1,16 @@ -! Copyright (C) 2005, 2007 Slava Pestov. +! Copyright (C) 2005, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: io.backend io.nonblocking io.unix.backend io.files io unix kernel math continuations math.bitfields ; IN: io.unix.files +M: unix-io cwd + MAXPATHLEN dup getcwd + [ alien>char-string ] [ (io-error) ] if* ; + +M: unix-io cd + chdir io-error ; + : read-flags O_RDONLY ; inline : open-read ( path -- fd ) diff --git a/extra/io/windows/nt/backend/backend.factor b/extra/io/windows/nt/backend/backend.factor index 88e7cdf84a..760bcec457 100755 --- a/extra/io/windows/nt/backend/backend.factor +++ b/extra/io/windows/nt/backend/backend.factor @@ -2,45 +2,10 @@ USING: alien alien.c-types arrays assocs combinators continuations destructors io io.backend io.nonblocking io.windows libc kernel math namespaces sequences threads tuples.lib windows windows.errors windows.kernel32 -strings splitting io.files qualified ascii ; +strings splitting io.files qualified ascii combinators.lib ; QUALIFIED: windows.winsock IN: io.windows.nt.backend -: unicode-prefix ( -- seq ) - "\\\\?\\" ; inline - -M: windows-nt-io root-directory? ( path -- ? ) - dup length 2 = [ - dup first Letter? - swap second CHAR: : = and - ] [ - drop f - ] if ; - -M: windows-nt-io normalize-pathname ( string -- string ) - dup string? [ "pathname must be a string" throw ] unless - "/" split "\\" join - { - ! empty - { [ dup empty? ] [ "empty path" throw ] } - ! .\\foo - { [ dup ".\\" head? ] [ - >r unicode-prefix cwd r> 1 tail 3append - ] } - ! c:\\foo - { [ dup 1 tail ":" head? ] [ >r unicode-prefix r> append ] } - ! \\\\?\\c:\\foo - { [ dup unicode-prefix head? ] [ ] } - ! foo.txt ..\\foo.txt - { [ t ] [ - [ - unicode-prefix % cwd % - dup first CHAR: \\ = [ CHAR: \\ , ] unless % - ] "" make - ] } - } cond [ "/\\." member? ] right-trim - dup peek CHAR: : = [ "\\" append ] when ; - SYMBOL: io-hash TUPLE: io-callback port continuation ; diff --git a/extra/io/windows/nt/files/files.factor b/extra/io/windows/nt/files/files.factor index 4a304e5ac9..43686707a2 100755 --- a/extra/io/windows/nt/files/files.factor +++ b/extra/io/windows/nt/files/files.factor @@ -1,8 +1,64 @@ -USING: continuations destructors io.buffers io.nonblocking -io.windows io.windows.nt.backend kernel libc math threads -windows windows.kernel32 ; +USING: continuations destructors io.buffers io.files io.backend +io.nonblocking io.windows io.windows.nt.backend kernel libc math +threads windows windows.kernel32 alien.c-types alien.arrays +sequences combinators combinators.lib ascii splitting alien +strings ; IN: io.windows.nt.files +M: windows-nt-io cwd + MAX_UNICODE_PATH dup "ushort" + [ GetCurrentDirectory win32-error=0/f ] keep + alien>u16-string ; + +M: windows-nt-io cd + SetCurrentDirectory win32-error=0/f ; + +: unicode-prefix ( -- seq ) + "\\\\?\\" ; inline + +M: windows-nt-io root-directory? ( path -- ? ) + dup length 2 = [ + dup first Letter? + swap second CHAR: : = and + ] [ + drop f + ] if ; + +: root-directory ( string -- string' ) + { + [ dup length 2 >= ] + [ dup second CHAR: : = ] + [ dup first Letter? ] + } && [ 2 head ] [ "Not an absolute path" throw ] if ; + +: prepend-prefix ( string -- string' ) + unicode-prefix swap append ; + +: windows-path+ ( cwd path -- newpath ) + { + ! empty + { [ dup empty? ] [ "empty path" throw ] } + ! \\\\?\\c:\\foo + { [ dup unicode-prefix head? ] [ nip ] } + ! ..\\foo + { [ dup "..\\" head? ] [ >r parent-directory r> 2 tail windows-path+ ] } + ! .\\foo + { [ dup ".\\" head? ] [ 1 tail append prepend-prefix ] } + ! \\foo + { [ dup "\\" head? ] [ >r root-directory r> append prepend-prefix ] } + ! c:\\foo + { [ dup second CHAR: : = ] [ nip prepend-prefix ] } + ! foo.txt + { [ t ] [ [ first CHAR: \\ = "" "\\" ? ] keep 3append prepend-prefix ] } + } cond ; + +M: windows-nt-io normalize-pathname ( string -- string ) + dup string? [ "pathname must be a string" throw ] unless + "/" split "\\" join + cwd swap windows-path+ + [ "/\\." member? ] right-trim + dup peek CHAR: : = [ "\\" append ] when ; + M: windows-nt-io CreateFile-flags ( DWORD -- DWORD ) FILE_FLAG_OVERLAPPED bitor ; diff --git a/extra/io/windows/nt/nt-tests.factor b/extra/io/windows/nt/nt-tests.factor index 9dfef6796d..ad409fb083 100755 --- a/extra/io/windows/nt/nt-tests.factor +++ b/extra/io/windows/nt/nt-tests.factor @@ -1,4 +1,4 @@ -USING: io.files kernel tools.test ; +USING: io.files kernel tools.test io.backend splitting ; IN: temporary [ "c:\\foo\\" ] [ "c:\\foo\\bar" parent-directory ] unit-test @@ -14,3 +14,7 @@ IN: temporary [ f ] [ "c:\\foo" root-directory? ] unit-test [ f ] [ "." root-directory? ] unit-test [ f ] [ ".." root-directory? ] unit-test + +[ ] [ "" resource-path cd ] unit-test + +[ "\\foo\\bar" ] [ "/foo/bar" normalize-pathname ":" split1 nip ] unit-test diff --git a/extra/unix/bsd/bsd.factor b/extra/unix/bsd/bsd.factor old mode 100644 new mode 100755 index 0a5aa1080e..e652f1b9f9 --- a/extra/unix/bsd/bsd.factor +++ b/extra/unix/bsd/bsd.factor @@ -5,6 +5,8 @@ USING: alien.syntax ; ! FreeBSD +: MAXPATHLEN 1024 ; inline + : O_RDONLY HEX: 0000 ; inline : O_WRONLY HEX: 0001 ; inline : O_RDWR HEX: 0002 ; inline diff --git a/extra/unix/linux/linux.factor b/extra/unix/linux/linux.factor old mode 100644 new mode 100755 index 0a3eb7ee5f..11db6cc862 --- a/extra/unix/linux/linux.factor +++ b/extra/unix/linux/linux.factor @@ -5,6 +5,8 @@ USING: alien.syntax ; ! Linux. +: MAXPATHLEN 1024 ; inline + : O_RDONLY HEX: 0000 ; inline : O_WRONLY HEX: 0001 ; inline : O_RDWR HEX: 0002 ; inline diff --git a/extra/unix/unix.factor b/extra/unix/unix.factor index 750a4b5044..d32fc25eab 100755 --- a/extra/unix/unix.factor +++ b/extra/unix/unix.factor @@ -124,6 +124,7 @@ FUNCTION: void freeaddrinfo ( addrinfo* ai ) ; FUNCTION: int futimes ( int id, timeval[2] times ) ; FUNCTION: char* gai_strerror ( int ecode ) ; FUNCTION: int getaddrinfo ( char* hostname, char* servname, addrinfo* hints, addrinfo** res ) ; +FUNCTION: char* getcwd ( char* buf, size_t size ) ; FUNCTION: int getdtablesize ; FUNCTION: gid_t getegid ; FUNCTION: uid_t geteuid ; diff --git a/extra/windows/kernel32/kernel32.factor b/extra/windows/kernel32/kernel32.factor index 45bd6bfae9..b8928c5820 100755 --- a/extra/windows/kernel32/kernel32.factor +++ b/extra/windows/kernel32/kernel32.factor @@ -892,7 +892,8 @@ FUNCTION: DWORD GetConsoleTitleW ( LPWSTR lpConsoleTitle, DWORD nSize ) ; ! FUNCTION: GetCurrentActCtx ! FUNCTION: GetCurrentConsoleFont ! FUNCTION: GetCurrentDirectoryA -! FUNCTION: GetCurrentDirectoryW +FUNCTION: BOOL GetCurrentDirectoryW ( DWORD len, LPTSTR buf ) ; +: GetCurrentDirectory GetCurrentDirectoryW ; inline FUNCTION: HANDLE GetCurrentProcess ( ) ; ! FUNCTION: GetCurrentProcessId FUNCTION: HANDLE GetCurrentThread ( ) ; @@ -1387,7 +1388,8 @@ FUNCTION: BOOL SetConsoleTitleW ( LPCWSTR lpConsoleTitle ) ; ! FUNCTION: SetCPGlobal ! FUNCTION: SetCriticalSectionSpinCount ! FUNCTION: SetCurrentDirectoryA -! FUNCTION: SetCurrentDirectoryW +FUNCTION: BOOL SetCurrentDirectoryW ( LPCWSTR lpDirectory ) ; +: SetCurrentDirectory SetCurrentDirectoryW ; inline ! FUNCTION: SetDefaultCommConfigA ! FUNCTION: SetDefaultCommConfigW ! FUNCTION: SetDllDirectoryA diff --git a/vm/io.h b/vm/io.h old mode 100644 new mode 100755 index d8cc2a0578..39e7390c3e --- a/vm/io.h +++ b/vm/io.h @@ -13,5 +13,3 @@ DECLARE_PRIMITIVE(fread); DECLARE_PRIMITIVE(open_file); DECLARE_PRIMITIVE(stat); DECLARE_PRIMITIVE(read_dir); -DECLARE_PRIMITIVE(cwd); -DECLARE_PRIMITIVE(cd); diff --git a/vm/os-unix.c b/vm/os-unix.c index 41dbe9cabf..92028dfc43 100755 --- a/vm/os-unix.c +++ b/vm/os-unix.c @@ -115,19 +115,6 @@ DEFINE_PRIMITIVE(read_dir) dpush(result); } -DEFINE_PRIMITIVE(cwd) -{ - char wd[MAXPATHLEN]; - if(getcwd(wd,MAXPATHLEN) == NULL) - io_error(); - box_char_string(wd); -} - -DEFINE_PRIMITIVE(cd) -{ - chdir(unbox_char_string()); -} - DEFINE_PRIMITIVE(os_envs) { GROWABLE_ARRAY(result); diff --git a/vm/os-windows-ce.c b/vm/os-windows-ce.c index e68a6385ae..9b73692aa0 100755 --- a/vm/os-windows-ce.c +++ b/vm/os-windows-ce.c @@ -10,16 +10,6 @@ s64 current_millis(void) | (s64)ft.dwHighDateTime<<32) - EPOCH_OFFSET) / 10000; } -DEFINE_PRIMITIVE(cwd) -{ - not_implemented_error(); -} - -DEFINE_PRIMITIVE(cd) -{ - not_implemented_error(); -} - char *strerror(int err) { /* strerror() is not defined on WinCE */ diff --git a/vm/os-windows-nt.c b/vm/os-windows-nt.c index e356c2f674..99ac21f62f 100755 --- a/vm/os-windows-nt.c +++ b/vm/os-windows-nt.c @@ -8,21 +8,6 @@ s64 current_millis(void) - EPOCH_OFFSET) / 10000; } -DEFINE_PRIMITIVE(cwd) -{ - F_CHAR buf[MAX_UNICODE_PATH]; - - if(!GetCurrentDirectory(MAX_UNICODE_PATH, buf)) - io_error(); - - box_u16_string(buf); -} - -DEFINE_PRIMITIVE(cd) -{ - SetCurrentDirectory(unbox_u16_string()); -} - DEFINE_PRIMITIVE(os_envs) { GROWABLE_ARRAY(result); diff --git a/vm/os-windows.h b/vm/os-windows.h index f252c214af..a22252fde8 100755 --- a/vm/os-windows.h +++ b/vm/os-windows.h @@ -30,6 +30,7 @@ typedef wchar_t F_CHAR; F_STRING *get_error_message(void); DLLEXPORT F_CHAR *error_message(DWORD id); +void windows_error(void); void init_ffi(void); void ffi_dlopen(F_DLL *dll, bool error); diff --git a/vm/primitives.c b/vm/primitives.c index f2f8ccf18d..dc7333c667 100755 --- a/vm/primitives.c +++ b/vm/primitives.c @@ -109,8 +109,6 @@ void *primitives[] = { primitive_millis, primitive_type, primitive_tag, - primitive_cwd, - primitive_cd, primitive_modify_code_heap, primitive_dlopen, primitive_dlsym, From c68e70877d47bd1239f6a1402edc767a7b6a3dfe Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Tue, 5 Feb 2008 16:42:50 -0500 Subject: [PATCH 103/269] Solution to Project Euler problem 43 --- extra/project-euler/043/043.factor | 97 ++++++++++++++++++++++++ extra/project-euler/project-euler.factor | 6 +- 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 extra/project-euler/043/043.factor diff --git a/extra/project-euler/043/043.factor b/extra/project-euler/043/043.factor new file mode 100644 index 0000000000..abe455e273 --- /dev/null +++ b/extra/project-euler/043/043.factor @@ -0,0 +1,97 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: combinators.lib hashtables kernel math math.combinatorics math.parser + math.ranges project-euler.common sequences sorting ; +IN: project-euler.043 + +! http://projecteuler.net/index.php?section=problems&id=43 + +! DESCRIPTION +! ----------- + +! The number, 1406357289, is a 0 to 9 pandigital number because it is made up +! of each of the digits 0 to 9 in some order, but it also has a rather +! interesting sub-string divisibility property. + +! Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note +! the following: + +! * d2d3d4 = 406 is divisible by 2 +! * d3d4d5 = 063 is divisible by 3 +! * d4d5d6 = 635 is divisible by 5 +! * d5d6d7 = 357 is divisible by 7 +! * d6d7d8 = 572 is divisible by 11 +! * d7d8d9 = 728 is divisible by 13 +! * d8d9d10 = 289 is divisible by 17 + +! Find the sum of all 0 to 9 pandigital numbers with this property. + + +! SOLUTION +! -------- + +! Brute force generating all the pandigitals then checking 3-digit divisiblity +! properties...this is very slow! + +integer swap mod zero? ; + +: interesting? ( seq -- ? ) + { + [ 17 8 pick subseq-divisible? ] + [ 13 7 pick subseq-divisible? ] + [ 11 6 pick subseq-divisible? ] + [ 7 5 pick subseq-divisible? ] + [ 5 4 pick subseq-divisible? ] + [ 3 3 pick subseq-divisible? ] + [ 2 2 pick subseq-divisible? ] + } && nip ; + +PRIVATE> + +: euler043 ( -- answer ) + 1234567890 number>digits all-permutations + [ interesting? ] subset [ 10 swap digits>integer ] map sum ; + +! [ euler043 ] time +! 125196 ms run / 19548 ms GC time + + +! ALTERNATE SOLUTIONS +! ------------------- + +! Build the number from right to left, generating the next 3-digits according +! to the divisiblity rules and combining them with the previous digits if they +! overlap and still have all unique digits. When done with that, add whatever +! missing digit is needed to make the number pandigital. + + [ number>digits 3 0 pad-left ] map [ all-unique? ] subset ; + +: overlap? ( seq -- ? ) + dup first 2 tail* swap second 2 head = ; + +: clean ( seq -- seq ) + [ unclip 1 head add* concat ] map [ all-unique? ] subset ; + +: add-missing-digit ( seq -- seq ) + dup natural-sort 10 seq-diff first add* ; + +: interesting-pandigitals ( -- seq ) + 17 candidates { 13 11 7 5 3 2 } [ + candidates swap cartesian-product [ overlap? ] subset clean + ] each [ add-missing-digit ] map ; + +PRIVATE> + +: euler043a ( -- answer ) + interesting-pandigitals [ 10 swap digits>integer ] sigma ; + +! [ euler043a ] 100 ave-time +! 19 ms run / 1 ms GC ave time - 100 trials + +MAIN: euler043a diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index 0be0b456ad..ef28cf8778 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -12,9 +12,9 @@ USING: definitions io io.files kernel math math.parser project-euler.ave-time project-euler.029 project-euler.030 project-euler.031 project-euler.032 project-euler.033 project-euler.034 project-euler.035 project-euler.036 project-euler.037 project-euler.038 project-euler.039 project-euler.040 - project-euler.041 project-euler.042 project-euler.048 project-euler.052 - project-euler.067 project-euler.075 project-euler.097 project-euler.134 - project-euler.169 project-euler.173 project-euler.175 ; + project-euler.041 project-euler.042 project-euler.043 project-euler.048 + project-euler.052 project-euler.067 project-euler.075 project-euler.097 + project-euler.134 project-euler.169 project-euler.173 project-euler.175 ; IN: project-euler Date: Tue, 5 Feb 2008 16:35:42 -0600 Subject: [PATCH 104/269] Fix MIMIC: --- extra/delegate/delegate.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/delegate/delegate.factor b/extra/delegate/delegate.factor index c0da9c51bc..667805dcc3 100755 --- a/extra/delegate/delegate.factor +++ b/extra/delegate/delegate.factor @@ -39,7 +39,7 @@ M: tuple-class group-words : define-mimic ( group mimicker mimicked -- ) >r >r group-words r> r> [ pick "methods" word-prop at dup - [ method-def spin define-method ] [ 3drop ] if + [ method-def spin define-method ] [ 3drop ] if ] 2curry each ; : MIMIC: From 2b9f977912d1472bd909ad58432aa98fd2403e32 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 16:35:57 -0600 Subject: [PATCH 105/269] Fix Windows normalize-pathname --- extra/io/windows/nt/files/files.factor | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extra/io/windows/nt/files/files.factor b/extra/io/windows/nt/files/files.factor index 43686707a2..5cbcd063bd 100755 --- a/extra/io/windows/nt/files/files.factor +++ b/extra/io/windows/nt/files/files.factor @@ -1,8 +1,8 @@ USING: continuations destructors io.buffers io.files io.backend io.nonblocking io.windows io.windows.nt.backend kernel libc math threads windows windows.kernel32 alien.c-types alien.arrays -sequences combinators combinators.lib ascii splitting alien -strings ; +sequences combinators combinators.lib sequences.lib ascii +splitting alien strings ; IN: io.windows.nt.files M: windows-nt-io cwd @@ -47,7 +47,7 @@ M: windows-nt-io root-directory? ( path -- ? ) ! \\foo { [ dup "\\" head? ] [ >r root-directory r> append prepend-prefix ] } ! c:\\foo - { [ dup second CHAR: : = ] [ nip prepend-prefix ] } + { [ dup ?second CHAR: : = ] [ nip prepend-prefix ] } ! foo.txt { [ t ] [ [ first CHAR: \\ = "" "\\" ? ] keep 3append prepend-prefix ] } } cond ; From 4297777e19bf43a735419f2e898edcfaaa9655eb Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 16:36:11 -0600 Subject: [PATCH 106/269] better logging for webapps.planet --- extra/io/server/server.factor | 17 +++++++++-------- extra/webapps/planet/planet.factor | 20 +++++++++++++------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/extra/io/server/server.factor b/extra/io/server/server.factor index 408fd29714..3c3d2c20f5 100755 --- a/extra/io/server/server.factor +++ b/extra/io/server/server.factor @@ -1,4 +1,4 @@ -! Copyright (C) 2003, 2007 Slava Pestov. +! Copyright (C) 2003, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: io io.sockets io.files continuations kernel math math.parser namespaces parser sequences strings @@ -9,11 +9,14 @@ IN: io.server SYMBOL: log-stream +: with-log-stream ( quot -- ) + log-stream get swap with-stream* ; inline + : log-message ( str -- ) - log-stream get [ + [ "[" write now timestamp>string write "] " write print flush - ] with-stream* ; + ] with-log-stream ; : log-error ( str -- ) "Error: " swap append log-message ; @@ -24,15 +27,13 @@ SYMBOL: log-stream : log-file ( service -- path ) ".log" append resource-path ; -: with-log-stream ( stream quot -- ) - log-stream swap with-variable ; inline - : with-log-file ( file quot -- ) >r r> - [ with-log-stream ] curry with-disposal ; inline + [ log-stream swap with-variable ] curry + with-disposal ; inline : with-log-stdio ( quot -- ) - stdio get swap with-log-stream ; + stdio get log-stream rot with-variable ; inline : with-logging ( service quot -- ) over [ diff --git a/extra/webapps/planet/planet.factor b/extra/webapps/planet/planet.factor index e9105ee459..ede0c579de 100755 --- a/extra/webapps/planet/planet.factor +++ b/extra/webapps/planet/planet.factor @@ -2,7 +2,7 @@ USING: sequences rss arrays concurrency kernel sorting html.elements io assocs namespaces math threads vocabs html furnace http.server.templating calendar math.parser splitting continuations debugger system http.server.responders -xml.writer prettyprint ; +xml.writer prettyprint io.server ; IN: webapps.planet : print-posting-summary ( posting -- ) @@ -75,13 +75,11 @@ SYMBOL: cached-postings SYMBOL: last-update -: diagnostic write print flush ; - : fetch-feed ( triple -- feed ) second - dup "Fetching " diagnostic + "Fetching " over append log-message dup download-feed feed-entries - swap "Done fetching " diagnostic ; + "Done fetching " swap append log-message ; : ( author entry -- entry' ) clone @@ -89,7 +87,11 @@ SYMBOL: last-update [ set-entry-title ] keep ; : ?fetch-feed ( triple -- feed/f ) - [ fetch-feed ] [ swap . error. f ] recover ; + [ + fetch-feed + ] [ + swap [ . error. ] with-log-stream f + ] recover ; : fetch-blogroll ( blogroll -- entries ) dup 0 @@ -111,7 +113,11 @@ SYMBOL: last-update update-thread ; : start-update-thread ( -- ) - [ update-thread ] in-thread ; + [ + "webapps.planet" [ + update-thread + ] with-logging + ] in-thread ; "planet" "planet-factor" "extra/webapps/planet" web-app From be39d64ef8e3f7aec8300883ab5a0903f7362b67 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 17:07:37 -0600 Subject: [PATCH 107/269] Check fork() error code --- extra/unix/process/process.factor | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/extra/unix/process/process.factor b/extra/unix/process/process.factor index 8b7144b979..c315d10d7f 100755 --- a/extra/unix/process/process.factor +++ b/extra/unix/process/process.factor @@ -8,7 +8,8 @@ IN: unix.process ! to implement io.launcher on Unix. User code should use ! io.launcher instead. -: >argv ( seq -- alien ) [ malloc-char-string ] map f add >c-void*-array ; +: >argv ( seq -- alien ) + [ malloc-char-string ] map f add >c-void*-array ; : exec ( pathname argv -- int ) [ malloc-char-string ] [ >argv ] bi* execv ; @@ -29,7 +30,7 @@ IN: unix.process >r [ first ] [ ] bi r> exec-with-env ; : with-fork ( child parent -- ) - fork dup zero? -roll swap curry if ; inline + fork dup io-error dup zero? -roll swap curry if ; inline : wait-for-pid ( pid -- status ) 0 [ 0 waitpid drop ] keep *int WEXITSTATUS ; \ No newline at end of file From acf236342c8fd42d1ebc8bac81835e20eaa2e0bd Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Tue, 5 Feb 2008 17:15:41 -0600 Subject: [PATCH 108/269] Fixing XML's whitespace handling --- extra/state-parser/state-parser.factor | 2 +- extra/xml/xml.factor | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/state-parser/state-parser.factor b/extra/state-parser/state-parser.factor index 19a4af44cc..3f51a52e1b 100644 --- a/extra/state-parser/state-parser.factor +++ b/extra/state-parser/state-parser.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2005, 2006 Daniel Ehrenberg ! See http://factorcode.org/license.txt for BSD license. USING: io io.streams.string kernel math namespaces sequences -strings circular prettyprint debugger unicode.categories ; +strings circular prettyprint debugger ascii ; IN: state-parser ! * Basic underlying words diff --git a/extra/xml/xml.factor b/extra/xml/xml.factor index 65a8e28dea..ec3e24b99d 100644 --- a/extra/xml/xml.factor +++ b/extra/xml/xml.factor @@ -3,7 +3,7 @@ USING: io io.streams.string io.files kernel math namespaces prettyprint sequences arrays generic strings vectors xml.char-classes xml.data xml.errors xml.tokenize xml.writer -xml.utilities state-parser assocs unicode.categories ; +xml.utilities state-parser assocs ascii ; IN: xml ! -- Overall parser with data tree From ede3254f0ab9ac092177481af3c5e994a18eb65c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 17:27:29 -0600 Subject: [PATCH 109/269] Bootstrap prints restarts --- core/bootstrap/stage2.factor | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/bootstrap/stage2.factor b/core/bootstrap/stage2.factor index c601ba7671..1a9bdd599a 100755 --- a/core/bootstrap/stage2.factor +++ b/core/bootstrap/stage2.factor @@ -88,5 +88,7 @@ IN: bootstrap.stage2 "output-image" get resource-path save-image-and-exit ] if ] [ - print-error :c "listener" vocab-main execute 1 exit + print-error :c restarts. + "listener" vocab-main execute + 1 exit ] recover From 898770f774005f701301146aaa421fba934b0286 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 17:31:27 -0600 Subject: [PATCH 110/269] Bootstrap fixes --- extra/io/unix/files/files.factor | 3 ++- extra/unix/process/process.factor | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/extra/io/unix/files/files.factor b/extra/io/unix/files/files.factor index 3201c29c45..a70f7339d2 100755 --- a/extra/io/unix/files/files.factor +++ b/extra/io/unix/files/files.factor @@ -1,7 +1,8 @@ ! Copyright (C) 2005, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: io.backend io.nonblocking io.unix.backend io.files io -unix kernel math continuations math.bitfields ; +unix kernel math continuations math.bitfields byte-arrays +alien ; IN: io.unix.files M: unix-io cwd diff --git a/extra/unix/process/process.factor b/extra/unix/process/process.factor index c315d10d7f..6fdc8e358b 100755 --- a/extra/unix/process/process.factor +++ b/extra/unix/process/process.factor @@ -1,6 +1,6 @@ USING: kernel alien.c-types sequences math unix combinators.cleave vectors kernel namespaces continuations -threads assocs vectors ; +threads assocs vectors io.unix.backend ; IN: unix.process From 9804d9462de31b3edbaa57dbe355ce0a2a674d22 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 17:33:36 -0600 Subject: [PATCH 111/269] Rename symbols to be consistent --- extra/io/launcher/launcher-docs.factor | 14 +++++++------- extra/io/launcher/launcher.factor | 16 ++++++++-------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/extra/io/launcher/launcher-docs.factor b/extra/io/launcher/launcher-docs.factor index e372f7a41e..4979f135ac 100755 --- a/extra/io/launcher/launcher-docs.factor +++ b/extra/io/launcher/launcher-docs.factor @@ -24,11 +24,11 @@ $nl HELP: +environment-mode+ { $description "Launch descriptor key. Must equal of the following:" { $list - { $link prepend-environment } - { $link replace-environment } - { $link append-environment } + { $link +prepend-environment+ } + { $link +replace-environment+ } + { $link +append-environment+ } } -"Default value is " { $link append-environment } "." +"Default value is " { $link +append-environment+ } "." } ; HELP: +stdin+ @@ -61,17 +61,17 @@ HELP: +stderr+ HELP: +closed+ { $description "Possible value for " { $link +stdin+ } ", " { $link +stdout+ } ", and " { $link +stderr+ } " launch descriptors." } ; -HELP: prepend-environment +HELP: +prepend-environment+ { $description "Possible value of " { $link +environment-mode+ } " launch descriptor key. The child process environment consists of the value of the " { $link +environment+ } " key together with the current environment, with entries from the current environment taking precedence." $nl "This is used in situations where you want to spawn a child process with some default environment variables set, but allowing the user to override these defaults by changing the environment before launching Factor." } ; -HELP: replace-environment +HELP: +replace-environment+ { $description "Possible value of " { $link +environment-mode+ } " launch descriptor key. The child process environment consists of the value of the " { $link +environment+ } " key." $nl "This is used in situations where you want full control over a child process environment, perhaps for security or testing." } ; -HELP: append-environment +HELP: +append-environment+ { $description "Possible value of " { $link +environment-mode+ } " launch descriptor key. The child process environment consists of the current environment together with the value of the " { $link +environment+ } " key, with entries from the " { $link +environment+ } " key taking precedence." $nl "This is used in situations where you want a spawn child process with some overridden environment variables." } ; diff --git a/extra/io/launcher/launcher.factor b/extra/io/launcher/launcher.factor index 9be90d28de..f2ed59a591 100755 --- a/extra/io/launcher/launcher.factor +++ b/extra/io/launcher/launcher.factor @@ -35,9 +35,9 @@ SYMBOL: +stdout+ SYMBOL: +stderr+ SYMBOL: +closed+ -SYMBOL: prepend-environment -SYMBOL: replace-environment -SYMBOL: append-environment +SYMBOL: +prepend-environment+ +SYMBOL: +replace-environment+ +SYMBOL: +append-environment+ : default-descriptor H{ @@ -45,7 +45,7 @@ SYMBOL: append-environment { +arguments+ f } { +detached+ f } { +environment+ H{ } } - { +environment-mode+ append-environment } + { +environment-mode+ +append-environment+ } } ; : with-descriptor ( desc quot -- ) @@ -53,14 +53,14 @@ SYMBOL: append-environment : pass-environment? ( -- ? ) +environment+ get assoc-empty? not - +environment-mode+ get replace-environment eq? or ; + +environment-mode+ get +replace-environment+ eq? or ; : get-environment ( -- env ) +environment+ get +environment-mode+ get { - { prepend-environment [ os-envs union ] } - { append-environment [ os-envs swap union ] } - { replace-environment [ ] } + { +prepend-environment+ [ os-envs union ] } + { +append-environment+ [ os-envs swap union ] } + { +replace-environment+ [ ] } } case ; GENERIC: >descriptor ( desc -- desc ) From f8df69d9a119967ea723a6924829da6a44dba210 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 18:00:24 -0600 Subject: [PATCH 112/269] Rename io.monitor to io.monitors; add log-viewer demo --- extra/help/handbook/handbook.factor | 4 ++-- extra/io/{monitor => monitors}/authors.txt | 0 .../monitors-docs.factor} | 14 +++++++------- .../monitor.factor => monitors/monitors.factor} | 2 +- extra/io/{monitor => monitors}/summary.txt | 0 extra/io/unix/linux/linux.factor | 2 +- .../windows/nt/{monitor => monitors}/authors.txt | 0 .../monitor.factor => monitors/monitors.factor} | 7 +++---- extra/io/windows/nt/nt.factor | 2 +- extra/log-viewer/authors.txt | 1 + extra/log-viewer/log-viewer.factor | 14 ++++++++++++++ extra/log-viewer/summary.txt | 1 + extra/log-viewer/tags.txt | 1 + 13 files changed, 32 insertions(+), 16 deletions(-) rename extra/io/{monitor => monitors}/authors.txt (100%) rename extra/io/{monitor/monitor-docs.factor => monitors/monitors-docs.factor} (87%) rename extra/io/{monitor/monitor.factor => monitors/monitors.factor} (94%) rename extra/io/{monitor => monitors}/summary.txt (100%) rename extra/io/windows/nt/{monitor => monitors}/authors.txt (100%) rename extra/io/windows/nt/{monitor/monitor.factor => monitors/monitors.factor} (94%) create mode 100755 extra/log-viewer/authors.txt create mode 100755 extra/log-viewer/log-viewer.factor create mode 100755 extra/log-viewer/summary.txt create mode 100755 extra/log-viewer/tags.txt diff --git a/extra/help/handbook/handbook.factor b/extra/help/handbook/handbook.factor index 234e7891d7..81e4bea7b3 100755 --- a/extra/help/handbook/handbook.factor +++ b/extra/help/handbook/handbook.factor @@ -137,7 +137,7 @@ ARTICLE: "collections" "Collections" { $subsection "graphs" } { $subsection "buffers" } ; -USING: io.sockets io.launcher io.mmap io.monitor ; +USING: io.sockets io.launcher io.mmap io.monitors ; ARTICLE: "io" "Input and output" { $subsection "streams" } @@ -155,7 +155,7 @@ ARTICLE: "io" "Input and output" "Advanced features:" { $subsection "io.launcher" } { $subsection "io.mmap" } -{ $subsection "io.monitor" } ; +{ $subsection "io.monitors" } ; ARTICLE: "tools" "Developer tools" { $subsection "tools.annotations" } diff --git a/extra/io/monitor/authors.txt b/extra/io/monitors/authors.txt similarity index 100% rename from extra/io/monitor/authors.txt rename to extra/io/monitors/authors.txt diff --git a/extra/io/monitor/monitor-docs.factor b/extra/io/monitors/monitors-docs.factor similarity index 87% rename from extra/io/monitor/monitor-docs.factor rename to extra/io/monitors/monitors-docs.factor index de649f48e7..9d985ff3fb 100755 --- a/extra/io/monitor/monitor-docs.factor +++ b/extra/io/monitors/monitors-docs.factor @@ -1,4 +1,4 @@ -IN: io.monitor +IN: io.monitors USING: help.markup help.syntax continuations ; HELP: @@ -9,7 +9,7 @@ $nl HELP: next-change { $values { "monitor" "a monitor" } { "path" "a pathname string" } { "changes" "a change descriptor" } } -{ $description "Waits for file system changes and outputs the pathname of the first changed file. The change descriptor is aq sequence of symbols documented in " { $link "io.monitor.descriptors" } "." } ; +{ $description "Waits for file system changes and outputs the pathname of the first changed file. The change descriptor is aq sequence of symbols documented in " { $link "io.monitors.descriptors" } "." } ; HELP: with-monitor { $values { "path" "a pathname string" } { "recursive?" "a boolean" } { "quot" "a quotation with stack effect " { $snippet "( monitor -- )" } } } @@ -27,7 +27,7 @@ HELP: +modify-file+ HELP: +rename-file+ { $description "Indicates that file has been renamed." } ; -ARTICLE: "io.monitor.descriptors" "File system change descriptors" +ARTICLE: "io.monitors.descriptors" "File system change descriptors" "Change descriptors output by " { $link next-change } ":" { $subsection +add-file+ } { $subsection +remove-file+ } @@ -35,24 +35,24 @@ ARTICLE: "io.monitor.descriptors" "File system change descriptors" { $subsection +rename-file+ } { $subsection +add-file+ } ; -ARTICLE: "io.monitor" "File system change monitors" +ARTICLE: "io.monitors" "File system change monitors" "File system change monitors listen for changes to file names, attributes and contents under a specified directory. They can optionally be recursive, in which case subdirectories are also monitored." $nl "Creating a file system change monitor and listening for changes:" { $subsection } { $subsection next-change } -{ $subsection "io.monitor.descriptors" } +{ $subsection "io.monitors.descriptors" } "Monitors are closed by calling " { $link dispose } " or " { $link with-disposal } "." $nl "A utility combinator which opens a monitor and cleans it up after:" { $subsection with-monitor } "An example which watches the Factor directory for changes:" { $code - "USE: io.monitor" + "USE: io.monitors" ": watch-loop ( monitor -- )" " dup next-change . . nl nl flush watch-loop ;" "" "\"\" resource-path f [ watch-loop ] with-monitor" } ; -ABOUT: "io.monitor" +ABOUT: "io.monitors" diff --git a/extra/io/monitor/monitor.factor b/extra/io/monitors/monitors.factor similarity index 94% rename from extra/io/monitor/monitor.factor rename to extra/io/monitors/monitors.factor index 1d8499b392..d652f34f1e 100755 --- a/extra/io/monitor/monitor.factor +++ b/extra/io/monitors/monitors.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: io.backend kernel continuations namespaces sequences assocs hashtables sorting arrays ; -IN: io.monitor +IN: io.monitors ( path recursive? -- monitor ) FILE_NOTIFY_INFORMATION-FileName FILE_NOTIFY_INFORMATION-FileNameLength FILE_NOTIFY_INFORMATION-Action - } get-slots parse-action 1array swap - memory>u16-string ; + } get-slots parse-action 1array -rot memory>u16-string ; : (changed-files) ( buffer -- ) dup parse-file-notify changed-file diff --git a/extra/io/windows/nt/nt.factor b/extra/io/windows/nt/nt.factor index 5bdefd7713..b957aa2fca 100755 --- a/extra/io/windows/nt/nt.factor +++ b/extra/io/windows/nt/nt.factor @@ -5,7 +5,7 @@ USE: io.windows USE: io.windows.nt.backend USE: io.windows.nt.files USE: io.windows.nt.launcher -USE: io.windows.nt.monitor +USE: io.windows.nt.monitors USE: io.windows.nt.sockets USE: io.windows.mmap USE: io.backend diff --git a/extra/log-viewer/authors.txt b/extra/log-viewer/authors.txt new file mode 100755 index 0000000000..1901f27a24 --- /dev/null +++ b/extra/log-viewer/authors.txt @@ -0,0 +1 @@ +Slava Pestov diff --git a/extra/log-viewer/log-viewer.factor b/extra/log-viewer/log-viewer.factor new file mode 100755 index 0000000000..0f139d184e --- /dev/null +++ b/extra/log-viewer/log-viewer.factor @@ -0,0 +1,14 @@ +USING: kernel io io.files io.monitors ; +IN: log-viewer + +: read-lines ( stream -- ) + dup stream-readln dup + [ print read-lines ] [ 2drop flush ] if ; + +: tail-file-loop ( stream monitor -- ) + dup next-change 2drop over read-lines tail-file-loop ; + +: tail-file ( file -- ) + dup dup read-lines + swap parent-directory f + tail-file-loop ; diff --git a/extra/log-viewer/summary.txt b/extra/log-viewer/summary.txt new file mode 100755 index 0000000000..5eb102447a --- /dev/null +++ b/extra/log-viewer/summary.txt @@ -0,0 +1 @@ +Simple log file watcher demo using io.monitors diff --git a/extra/log-viewer/tags.txt b/extra/log-viewer/tags.txt new file mode 100755 index 0000000000..cb5fc203e1 --- /dev/null +++ b/extra/log-viewer/tags.txt @@ -0,0 +1 @@ +demos From 53810cd17b49a9de41a80977e9c0e03b58be176a Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Tue, 5 Feb 2008 18:28:05 -0600 Subject: [PATCH 113/269] builder: update target --- extra/builder/builder.factor | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/extra/builder/builder.factor b/extra/builder/builder.factor index 3216105d47..832b89a7dc 100755 --- a/extra/builder/builder.factor +++ b/extra/builder/builder.factor @@ -33,7 +33,12 @@ SYMBOL: builder-recipients ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -: target ( -- target ) `{ ,[ os ] %[ cpu "." split ] } "-" join ; +! : target ( -- target ) `{ ,[ os ] %[ cpu "." split ] } "-" join ; + +: target ( -- target ) + { { [ os "windows" = ] [ "windows-nt-x86-32" ] } + { [ t ] [ `{ ,[ os ] %[ cpu "." split ] } "-" join ] } } + cond ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! From 20e4fcecda6d3b2a2d20756ae002fa85c19a1b34 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 18:48:38 -0600 Subject: [PATCH 114/269] Make OS name more consistent for extra/builder --- Makefile | 8 ++++---- core/system/system-docs.factor | 3 ++- core/system/system.factor | 2 +- vm/os-windows-nt.h | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) mode change 100644 => 100755 core/system/system-docs.factor mode change 100644 => 100755 core/system/system.factor diff --git a/Makefile b/Makefile index aad7fe90eb..5e1a9d6220 100755 --- a/Makefile +++ b/Makefile @@ -63,8 +63,8 @@ default: @echo "macosx-ppc" @echo "solaris-x86-32" @echo "solaris-x86-64" - @echo "windows-ce-arm" - @echo "windows-nt-x86-32" + @echo "wince-arm" + @echo "winnt-x86-32" @echo "" @echo "Additional modifiers:" @echo "" @@ -122,10 +122,10 @@ solaris-x86-32: solaris-x86-64: $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.solaris.x86.64 -windows-nt-x86-32: +winnt-x86-32: $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.32 -windows-ce-arm: +wince-arm: $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.ce.arm macosx.app: factor diff --git a/core/system/system-docs.factor b/core/system/system-docs.factor old mode 100644 new mode 100755 index d80cfa9ceb..bdd04307df --- a/core/system/system-docs.factor +++ b/core/system/system-docs.factor @@ -51,7 +51,8 @@ HELP: os "openbsd" "netbsd" "solaris" - "windows" + "wince" + "winnt" } } ; diff --git a/core/system/system.factor b/core/system/system.factor old mode 100644 new mode 100755 index 4983260a36..4500720058 --- a/core/system/system.factor +++ b/core/system/system.factor @@ -22,7 +22,7 @@ splitting assocs ; os "wince" = ; foldable : winnt? ( -- ? ) - os "windows" = ; foldable + os "winnt" = ; foldable : windows? ( -- ? ) wince? winnt? or ; foldable diff --git a/vm/os-windows-nt.h b/vm/os-windows-nt.h index 9e451f0301..e289b6617d 100755 --- a/vm/os-windows-nt.h +++ b/vm/os-windows-nt.h @@ -12,7 +12,7 @@ typedef char F_SYMBOL; #define unbox_symbol_string unbox_char_string #define from_symbol_string from_char_string -#define FACTOR_OS_STRING "windows" +#define FACTOR_OS_STRING "winnt" #define FACTOR_DLL L"factor-nt.dll" #define FACTOR_DLL_NAME "factor-nt.dll" From cf99e405fe22f35900160fc054d401894f101d69 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 18:50:24 -0600 Subject: [PATCH 115/269] More intuitive error message for about --- extra/help/help.factor | 3 +++ 1 file changed, 3 insertions(+) mode change 100644 => 100755 extra/help/help.factor diff --git a/extra/help/help.factor b/extra/help/help.factor old mode 100644 new mode 100755 index 87bc0a4b7f..aefbf2aba2 --- a/extra/help/help.factor +++ b/extra/help/help.factor @@ -96,6 +96,9 @@ M: word set-article-parent swap "help-parent" set-word-prop ; article-content print-content nl ; : about ( vocab -- ) + dup vocab [ ] [ + "No such vocabulary: " swap append throw + ] ?if dup vocab-help [ help ] [ From 551b3a42a130eaf0e0ea77e1b9ba873c5e5628db Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 18:52:16 -0600 Subject: [PATCH 116/269] New reset-memoized word --- extra/memoize/memoize.factor | 3 +++ extra/xmode/catalog/catalog.factor | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) mode change 100644 => 100755 extra/xmode/catalog/catalog.factor diff --git a/extra/memoize/memoize.factor b/extra/memoize/memoize.factor index 5fa112921c..3b0b8fd29f 100755 --- a/extra/memoize/memoize.factor +++ b/extra/memoize/memoize.factor @@ -50,3 +50,6 @@ M: memoized definition "memo-quot" word-prop ; : memoize-quot ( quot effect -- memo-quot ) gensym swap dupd "declared-effect" set-word-prop dup rot define-memoized 1quotation ; + +: reset-memoized ( word -- ) + "memoize" word-prop clear-assoc ; diff --git a/extra/xmode/catalog/catalog.factor b/extra/xmode/catalog/catalog.factor old mode 100644 new mode 100755 index 9c7e6a1ee7..d6402603fa --- a/extra/xmode/catalog/catalog.factor +++ b/extra/xmode/catalog/catalog.factor @@ -99,7 +99,7 @@ SYMBOL: rule-sets (load-mode) dup finalize-mode ; : reset-modes ( -- ) - \ (load-mode) "memoize" word-prop clear-assoc ; + \ (load-mode) reset-memoized ; : ?glob-matches ( string glob/f -- ? ) dup [ glob-matches? ] [ 2drop f ] if ; From 18403d15faf04ade5a159672585fdb4f68a12bff Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 18:55:10 -0600 Subject: [PATCH 117/269] tools.browser now uses io.monitor --- extra/tools/browser/browser-docs.factor | 22 ++++++++++-- extra/tools/browser/browser.factor | 47 ++++++++++++++++++------- extra/vocabs/monitor/authors.txt | 1 + extra/vocabs/monitor/monitor.factor | 14 ++++++++ extra/vocabs/monitor/summary.txt | 1 + 5 files changed, 71 insertions(+), 14 deletions(-) mode change 100644 => 100755 extra/tools/browser/browser-docs.factor create mode 100644 extra/vocabs/monitor/authors.txt create mode 100755 extra/vocabs/monitor/monitor.factor create mode 100644 extra/vocabs/monitor/summary.txt diff --git a/extra/tools/browser/browser-docs.factor b/extra/tools/browser/browser-docs.factor old mode 100644 new mode 100755 index db0e5942f5..28bef58a8a --- a/extra/tools/browser/browser-docs.factor +++ b/extra/tools/browser/browser-docs.factor @@ -2,16 +2,34 @@ USING: help.markup help.syntax io strings ; IN: tools.browser ARTICLE: "vocab-index" "Vocabulary index" -{ $tags,authors } +{ $tags } +{ $authors } { $describe-vocab "" } ; ARTICLE: "tools.browser" "Vocabulary browser" "Getting and setting vocabulary meta-data:" +{ $subsection vocab-file-contents } +{ $subsection set-vocab-file-contents } { $subsection vocab-summary } { $subsection set-vocab-summary } { $subsection vocab-tags } { $subsection set-vocab-tags } -{ $subsection add-vocab-tags } ; +{ $subsection add-vocab-tags } +"Global meta-data:" +{ $subsection all-vocabs } +{ $subsection all-vocabs-seq } +{ $subsection all-tags } +{ $subsection all-authors } +"Because loading the above data is expensive, it is cached. The cache is flushed by the " { $vocab-link "vocabs.monitor" } " vocabulary. It can also be flushed manually when file system change monitors are not available:" +{ $subsection reset-cache } ; + +HELP: vocab-file-contents +{ $values { "vocab" "a vocabulary specifier" } { "name" string } { "seq" "a sequence of lines, or " { $link f } } } +{ $description "Outputs the contents of the file named " { $snippet "name" } " from the vocabulary's directory, or " { $link f } " if the file does not exist." } ; + +HELP: set-vocab-file-contents +{ $values { "seq" "a sequence of lines" } { "vocab" "a vocabulary specifier" } { "name" string } } +{ $description "Stores a sequence of lines to the file named " { $snippet "name" } " from the vocabulary's directory." } ; HELP: vocab-summary { $values { "vocab" "a vocabulary specifier" } { "summary" "a string or " { $link f } } } diff --git a/extra/tools/browser/browser.factor b/extra/tools/browser/browser.factor index dabc37e5de..7aefbc8aaa 100755 --- a/extra/tools/browser/browser.factor +++ b/extra/tools/browser/browser.factor @@ -1,13 +1,30 @@ -! Copyright (C) 2007 Slava Pestov. +! Copyright (C) 2007, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: namespaces splitting sequences io.files kernel assocs words vocabs vocabs.loader definitions parser continuations inspector debugger io io.styles io.streams.lines hashtables sorting prettyprint source-files arrays combinators strings system math.parser help.markup help.topics help.syntax -help.stylesheet ; +help.stylesheet memoize ; IN: tools.browser +MEMO: (vocab-file-contents) ( path -- lines ) + ?resource-path dup exists? + [ lines ] [ drop f ] if ; + +: vocab-file-contents ( vocab name -- seq ) + vocab-path+ dup [ (vocab-file-contents) ] when ; + +: set-vocab-file-contents ( seq vocab name -- ) + dupd vocab-path+ [ + ?resource-path + [ [ print ] each ] with-stream + ] [ + "The " swap vocab-name + " vocabulary was not loaded from the file system" + 3append throw + ] ?if ; + : vocab-summary-path ( vocab -- string ) vocab-dir "summary.txt" path+ ; @@ -86,7 +103,7 @@ M: vocab-link summary vocab-summary ; dup [ "" vocabs-in-dir ] { } make ] { } map>assoc ; -: all-vocabs-seq ( -- seq ) +MEMO: all-vocabs-seq ( -- seq ) all-vocabs values concat ; : dangerous? ( name -- ? ) @@ -288,20 +305,20 @@ C: vocab-author : $tagged-vocabs ( element -- ) first tagged vocabs. ; -: all-tags ( vocabs -- seq ) [ vocab-tags ] map>set ; +MEMO: all-tags ( -- seq ) + all-vocabs-seq [ vocab-tags ] map>set ; : $authored-vocabs ( element -- ) first authored vocabs. ; -: all-authors ( vocabs -- seq ) [ vocab-authors ] map>set ; +MEMO: all-authors ( -- seq ) + all-vocabs-seq [ vocab-authors ] map>set ; -: $tags,authors ( element -- ) - drop - all-vocabs-seq - "Tags" $heading - dup all-tags tags. - "Authors" $heading - all-authors authors. ; +: $tags ( element -- ) + drop "Tags" $heading all-tags tags. ; + +: $authors ( element -- ) + drop "Authors" $heading all-authors authors. ; M: vocab-spec article-title vocab-name " vocabulary" append ; @@ -339,3 +356,9 @@ M: vocab-author article-content M: vocab-author article-parent drop "vocab-index" ; M: vocab-author summary article-title ; + +: reset-cache ( -- ) + \ (vocab-file-contents) reset-memoized + \ all-vocabs-seq reset-memoized + \ all-authors reset-memoized + \ all-tags reset-memoized ; diff --git a/extra/vocabs/monitor/authors.txt b/extra/vocabs/monitor/authors.txt new file mode 100644 index 0000000000..1901f27a24 --- /dev/null +++ b/extra/vocabs/monitor/authors.txt @@ -0,0 +1 @@ +Slava Pestov diff --git a/extra/vocabs/monitor/monitor.factor b/extra/vocabs/monitor/monitor.factor new file mode 100755 index 0000000000..24aa8b1d99 --- /dev/null +++ b/extra/vocabs/monitor/monitor.factor @@ -0,0 +1,14 @@ +USING: threads io.files io.monitors init kernel tools.browser ; +IN: vocabs.monitor + +! Use file system change monitoring to flush the tags/authors +! cache +: update-thread ( monitor -- ) + dup next-change 2drop reset-cache update-thread ; + +: start-update-thread + [ + "" resource-path t update-thread + ] in-thread ; + +[ start-update-thread ] "tools.browser" add-init-hook diff --git a/extra/vocabs/monitor/summary.txt b/extra/vocabs/monitor/summary.txt new file mode 100644 index 0000000000..27c0d3867a --- /dev/null +++ b/extra/vocabs/monitor/summary.txt @@ -0,0 +1 @@ +Use io.monitors to clear tools.browser authors/tags/summary cache From c87bd84635ed8c984f2cd9d87ef0e14b6711adef Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 18:55:20 -0600 Subject: [PATCH 118/269] Fix opengl tags --- extra/opengl/tags.txt | 3 --- 1 file changed, 3 deletions(-) mode change 100644 => 100755 extra/opengl/tags.txt diff --git a/extra/opengl/tags.txt b/extra/opengl/tags.txt old mode 100644 new mode 100755 index 5e477dbcb3..bb863cf9a0 --- a/extra/opengl/tags.txt +++ b/extra/opengl/tags.txt @@ -1,4 +1 @@ -opengl.glu -opengl.gl -opengl bindings From 687cd7860321ac07a36f0c6d96b1c1cd946099b6 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 18:55:28 -0600 Subject: [PATCH 119/269] Word moved --- extra/tools/deploy/config/config.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/tools/deploy/config/config.factor b/extra/tools/deploy/config/config.factor index e6d03c2233..1f34e68f29 100755 --- a/extra/tools/deploy/config/config.factor +++ b/extra/tools/deploy/config/config.factor @@ -1,8 +1,8 @@ -! Copyright (C) 2007 Slava Pestov. +! Copyright (C) 2007, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: vocabs.loader io.files io kernel sequences assocs splitting parser prettyprint namespaces math vocabs -hashtables ; +hashtables tools.browser ; IN: tools.deploy.config SYMBOL: deploy-name From 038578939f998bcdce47e47980cf019e3971105b Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 19:01:19 -0600 Subject: [PATCH 120/269] Change require-all for Ed --- core/vocabs/loader/loader-docs.factor | 13 +++---- core/vocabs/loader/loader.factor | 50 ++++++++++++--------------- extra/bootstrap/io/io.factor | 2 ++ 3 files changed, 30 insertions(+), 35 deletions(-) diff --git a/core/vocabs/loader/loader-docs.factor b/core/vocabs/loader/loader-docs.factor index 899d50407f..bc88661530 100755 --- a/core/vocabs/loader/loader-docs.factor +++ b/core/vocabs/loader/loader-docs.factor @@ -124,15 +124,12 @@ HELP: refresh { $values { "prefix" string } } { $description "Reloads source files and documentation belonging to loaded vocabularies whose names are prefixed by " { $snippet "prefix" } " which have been modified on disk." } ; +HELP: refresh-all-error +{ $values { "vocabs" "a sequence of vocabularies" } } +{ $description "Throws a " { $link require-all-error } "." } +{ $error-description "Thrown by " { $link require-all } " if one or more vocabulary failed to load." } ; + HELP: refresh-all { $description "Reloads source files and documentation for all loaded vocabularies which have been modified on disk." } ; { refresh refresh-all } related-words - -HELP: vocab-file-contents -{ $values { "vocab" "a vocabulary specifier" } { "name" string } { "seq" "a sequence of lines, or " { $link f } } } -{ $description "Outputs the contents of the file named " { $snippet "name" } " from the vocabulary's directory, or " { $link f } " if the file does not exist." } ; - -HELP: set-vocab-file-contents -{ $values { "seq" "a sequence of lines" } { "vocab" "a vocabulary specifier" } { "name" string } } -{ $description "Stores a sequence of lines to the file named " { $snippet "name" } " from the vocabulary's directory." } ; diff --git a/core/vocabs/loader/loader.factor b/core/vocabs/loader/loader.factor index f2c5b2a012..6e6d1923e0 100755 --- a/core/vocabs/loader/loader.factor +++ b/core/vocabs/loader/loader.factor @@ -148,16 +148,31 @@ SYMBOL: load-help? dup update-roots dup modified-sources swap modified-docs ; -: require-restart { { "Ignore this vocabulary" t } } ; +: load-error. ( vocab error -- ) + "While loading " swap dup >vocab-link write-object ":" print + print-error ; -: require-all ( seq -- ) - [ +TUPLE: require-all-error vocabs ; + +: require-all-error ( vocabs -- ) + \ require-all-error construct-boa throw ; + +M: require-all-error summary + drop "The require-all operation failed" ; + +: require-all ( vocabs -- ) + dup length 1 = [ first require ] [ [ - [ require ] - [ require-restart rethrow-restarts 2drop ] - recover - ] each - ] with-compiler-errors ; + [ + [ [ require ] [ 2array , ] recover ] each + ] { } make + dup empty? [ drop ] [ + "==== LOAD ERRORS:" print + dup [ nl load-error. ] assoc-each + keys require-all-error + ] if + ] with-compiler-errors + ] if ; : do-refresh ( modified-sources modified-docs -- ) 2dup @@ -190,22 +205,3 @@ load-vocab-hook set-global M: vocab where vocab-where ; M: vocab-link where vocab-where ; - -: vocab-file-contents ( vocab name -- seq ) - vocab-path+ dup [ - ?resource-path dup exists? [ - lines - ] [ - drop f - ] if - ] when ; - -: set-vocab-file-contents ( seq vocab name -- ) - dupd vocab-path+ [ - ?resource-path - [ [ print ] each ] with-stream - ] [ - "The " swap vocab-name - " vocabulary was not loaded from the file system" - 3append throw - ] ?if ; diff --git a/extra/bootstrap/io/io.factor b/extra/bootstrap/io/io.factor index 065f7dd5c4..4d5440e546 100755 --- a/extra/bootstrap/io/io.factor +++ b/extra/bootstrap/io/io.factor @@ -10,3 +10,5 @@ IN: bootstrap.io { [ wince? ] [ "windows.ce" ] } } cond append require ] when + +"vocabs.monitor" require From 1eda70f1ad1f0d744ed846ce8c975a1cd4b28fb6 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 19:16:22 -0600 Subject: [PATCH 121/269] Bug fixes --- core/io/files/files.factor | 13 ++++++++----- core/vocabs/loader/loader-docs.factor | 2 +- core/vocabs/loader/loader.factor | 2 +- extra/io/windows/nt/files/files.factor | 12 +++++++++--- extra/io/windows/nt/nt-tests.factor | 22 +++++++++++++++++++--- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/core/io/files/files.factor b/core/io/files/files.factor index 9952e6387b..9a99090699 100755 --- a/core/io/files/files.factor +++ b/core/io/files/files.factor @@ -29,12 +29,15 @@ HOOK: root-directory? io-backend ( path -- ? ) M: object root-directory? ( path -- ? ) path-separator? ; -: trim-path-separators ( str -- newstr ) +: right-trim-separators ( str -- newstr ) [ path-separator? ] right-trim ; +: left-trim-separators ( str -- newstr ) + [ path-separator? ] left-trim ; + : path+ ( str1 str2 -- str ) - >r trim-path-separators "/" r> - [ path-separator? ] left-trim 3append ; + >r right-trim-separators "/" r> + left-trim-separators 3append ; : stat ( path -- directory? permissions length modified ) normalize-pathname (stat) ; @@ -69,7 +72,7 @@ TUPLE: no-parent-directory path ; \ no-parent-directory construct-boa throw ; : parent-directory ( path -- parent ) - trim-path-separators { + right-trim-separators { { [ dup empty? ] [ drop "/" ] } { [ dup root-directory? ] [ ] } { [ dup [ path-separator? ] contains? not ] [ drop "." ] } @@ -90,7 +93,7 @@ TUPLE: no-parent-directory path ; "resource:" ?head [ resource-path ] when ; : make-directories ( path -- ) - normalize-pathname trim-path-separators { + normalize-pathname right-trim-separators { { [ dup "." = ] [ ] } { [ dup root-directory? ] [ ] } { [ dup empty? ] [ ] } diff --git a/core/vocabs/loader/loader-docs.factor b/core/vocabs/loader/loader-docs.factor index bc88661530..f8626f3370 100755 --- a/core/vocabs/loader/loader-docs.factor +++ b/core/vocabs/loader/loader-docs.factor @@ -124,7 +124,7 @@ HELP: refresh { $values { "prefix" string } } { $description "Reloads source files and documentation belonging to loaded vocabularies whose names are prefixed by " { $snippet "prefix" } " which have been modified on disk." } ; -HELP: refresh-all-error +HELP: require-all-error { $values { "vocabs" "a sequence of vocabularies" } } { $description "Throws a " { $link require-all-error } "." } { $error-description "Thrown by " { $link require-all } " if one or more vocabulary failed to load." } ; diff --git a/core/vocabs/loader/loader.factor b/core/vocabs/loader/loader.factor index 6e6d1923e0..64372fe4b7 100755 --- a/core/vocabs/loader/loader.factor +++ b/core/vocabs/loader/loader.factor @@ -149,7 +149,7 @@ SYMBOL: load-help? dup modified-sources swap modified-docs ; : load-error. ( vocab error -- ) - "While loading " swap dup >vocab-link write-object ":" print + "While loading " rot dup >vocab-link write-object ":" print print-error ; TUPLE: require-all-error vocabs ; diff --git a/extra/io/windows/nt/files/files.factor b/extra/io/windows/nt/files/files.factor index 5cbcd063bd..a1c331816c 100755 --- a/extra/io/windows/nt/files/files.factor +++ b/extra/io/windows/nt/files/files.factor @@ -37,11 +37,13 @@ M: windows-nt-io root-directory? ( path -- ? ) : windows-path+ ( cwd path -- newpath ) { ! empty - { [ dup empty? ] [ "empty path" throw ] } + { [ dup empty? ] [ drop ] } + ! .. + { [ dup ".." = ] [ drop parent-directory prepend-prefix ] } ! \\\\?\\c:\\foo { [ dup unicode-prefix head? ] [ nip ] } ! ..\\foo - { [ dup "..\\" head? ] [ >r parent-directory r> 2 tail windows-path+ ] } + { [ dup "..\\" head? ] [ >r parent-directory r> 3 tail windows-path+ ] } ! .\\foo { [ dup ".\\" head? ] [ 1 tail append prepend-prefix ] } ! \\foo @@ -49,7 +51,11 @@ M: windows-nt-io root-directory? ( path -- ? ) ! c:\\foo { [ dup ?second CHAR: : = ] [ nip prepend-prefix ] } ! foo.txt - { [ t ] [ [ first CHAR: \\ = "" "\\" ? ] keep 3append prepend-prefix ] } + { [ t ] [ + >r right-trim-separators "\\" r> + left-trim-separators + 3append prepend-prefix + ] } } cond ; M: windows-nt-io normalize-pathname ( string -- string ) diff --git a/extra/io/windows/nt/nt-tests.factor b/extra/io/windows/nt/nt-tests.factor index ad409fb083..e4ebe3dd37 100755 --- a/extra/io/windows/nt/nt-tests.factor +++ b/extra/io/windows/nt/nt-tests.factor @@ -1,4 +1,5 @@ -USING: io.files kernel tools.test io.backend splitting ; +USING: io.files kernel tools.test io.backend +io.windows.nt.files splitting ; IN: temporary [ "c:\\foo\\" ] [ "c:\\foo\\bar" parent-directory ] unit-test @@ -9,8 +10,8 @@ IN: temporary [ "Z:" ] [ "Z:\\" parent-directory ] unit-test [ "c:" ] [ "c:" parent-directory ] unit-test [ "Z:" ] [ "Z:" parent-directory ] unit-test -[ t ] [ "c:\\" trim-path-separators root-directory? ] unit-test -[ t ] [ "Z:\\" trim-path-separators root-directory? ] unit-test +[ t ] [ "c:\\" right-trim-separators root-directory? ] unit-test +[ t ] [ "Z:\\" right-trim-separators root-directory? ] unit-test [ f ] [ "c:\\foo" root-directory? ] unit-test [ f ] [ "." root-directory? ] unit-test [ f ] [ ".." root-directory? ] unit-test @@ -18,3 +19,18 @@ IN: temporary [ ] [ "" resource-path cd ] unit-test [ "\\foo\\bar" ] [ "/foo/bar" normalize-pathname ":" split1 nip ] unit-test + +[ "\\\\?\\C:\\builds\\factor\\log.txt" ] [ + "C:\\builds\\factor\\12345\\" + "..\\log.txt" windows-path+ +] unit-test + +[ "\\\\?\\C:\\builds\\" ] [ + "C:\\builds\\factor\\12345\\" + "..\\.." windows-path+ +] unit-test + +[ "\\\\?\\C:\\builds\\" ] [ + "C:\\builds\\factor\\12345\\" + "..\\.." windows-path+ +] unit-test From 3f9e4bcf0025c03e5a1f3ad0630e8a85f9d3410a Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 20:11:35 -0600 Subject: [PATCH 122/269] More efficient specializers --- core/generator/generator.factor | 5 ++- core/optimizer/backend/backend.factor | 6 +-- core/optimizer/known-words/known-words.factor | 16 +++---- core/optimizer/optimizer-docs.factor | 29 ------------- core/optimizer/optimizer.factor | 43 +------------------ .../specializers/specializers-docs.factor | 26 +++++++++++ .../specializers/specializers.factor | 41 ++++++++++++++++++ extra/benchmark/recursive/recursive.factor | 6 --- extra/math/vectors/vectors.factor | 30 ++++++------- 9 files changed, 98 insertions(+), 104 deletions(-) mode change 100644 => 100755 core/optimizer/optimizer-docs.factor mode change 100644 => 100755 core/optimizer/optimizer.factor create mode 100755 core/optimizer/specializers/specializers-docs.factor create mode 100755 core/optimizer/specializers/specializers.factor mode change 100644 => 100755 extra/benchmark/recursive/recursive.factor diff --git a/core/generator/generator.factor b/core/generator/generator.factor index 3d66241bc3..3883fb6e35 100755 --- a/core/generator/generator.factor +++ b/core/generator/generator.factor @@ -3,8 +3,9 @@ USING: arrays assocs classes combinators cpu.architecture effects generator.fixup generator.registers generic hashtables inference inference.backend inference.dataflow io kernel -kernel.private layouts math namespaces optimizer prettyprint -quotations sequences system threads words vectors ; +kernel.private layouts math namespaces optimizer +optimizer.specializers prettyprint quotations sequences system +threads words vectors ; IN: generator SYMBOL: compile-queue diff --git a/core/optimizer/backend/backend.factor b/core/optimizer/backend/backend.factor index 9d75346091..e73200b861 100755 --- a/core/optimizer/backend/backend.factor +++ b/core/optimizer/backend/backend.factor @@ -4,7 +4,7 @@ USING: arrays generic assocs inference inference.class inference.dataflow inference.backend inference.state io kernel math namespaces sequences vectors words quotations hashtables combinators classes generic.math continuations optimizer.def-use -optimizer.pattern-match generic.standard ; +optimizer.pattern-match generic.standard optimizer.specializers ; IN: optimizer.backend SYMBOL: class-substitutions @@ -256,7 +256,7 @@ M: #dispatch optimize-node* tuck dispatching-class dup [ swap [ 2array ] 2keep method method-word - dup word-def flat-length 6 >= + dup word-def flat-length 5 >= [ 1quotation ] [ word-def ] if ] [ 2drop t t @@ -363,7 +363,7 @@ M: #dispatch optimize-node* : optimistic-inline? ( #call -- ? ) dup node-param "specializer" word-prop dup [ - >r node-input-classes r> length tail* + >r node-input-classes r> specialized-length tail* [ types length 1 = ] all? ] [ 2drop f diff --git a/core/optimizer/known-words/known-words.factor b/core/optimizer/known-words/known-words.factor index 6828a0948c..5820d8f5b2 100755 --- a/core/optimizer/known-words/known-words.factor +++ b/core/optimizer/known-words/known-words.factor @@ -124,19 +124,19 @@ float-arrays combinators.private combinators ; ] each \ push-all -{ { string array } { sbuf vector } } +{ { string sbuf } { array vector } } "specializer" set-word-prop \ append -{ { string array } { string array } } +{ { string string } { array array } } "specializer" set-word-prop \ subseq -{ fixnum fixnum { string array } } +{ { fixnum fixnum string } { fixnum fixnum array } } "specializer" set-word-prop \ reverse-here -{ { string array } } +{ { string } { array } } "specializer" set-word-prop \ mismatch @@ -147,9 +147,9 @@ float-arrays combinators.private combinators ; \ >string { sbuf } "specializer" set-word-prop -\ >array { { string vector } } "specializer" set-word-prop +\ >array { { string } { vector } } "specializer" set-word-prop -\ >vector { { array vector } } "specializer" set-word-prop +\ >vector { { array } { vector } } "specializer" set-word-prop \ >sbuf { string } "specializer" set-word-prop @@ -163,6 +163,6 @@ float-arrays combinators.private combinators ; \ assoc-stack { vector } "specializer" set-word-prop -\ >le { { fixnum bignum } fixnum } "specializer" set-word-prop +\ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop -\ >be { { fixnum bignum } fixnum } "specializer" set-word-prop +\ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop diff --git a/core/optimizer/optimizer-docs.factor b/core/optimizer/optimizer-docs.factor old mode 100644 new mode 100755 index ff694650bc..4be1176cda --- a/core/optimizer/optimizer-docs.factor +++ b/core/optimizer/optimizer-docs.factor @@ -2,31 +2,6 @@ USING: help.markup help.syntax quotations words math sequences ; IN: optimizer -ARTICLE: "specializers" "Word specializers" -"The optimizer can be passed hints as to the classes of parameters a word is expected to be called with. The optimizer will then generate multiple versions of word when compiling, specialized to each class." -$nl -"Specialization hints are stored in the " { $snippet "\"specializer\"" } " word property. The value of this property is a sequence having the same number of elements as the word has inputs; each element takes one of the following forms and gives the compiler a hint about the corresponding parameter:" -{ $table - { { $snippet { $emphasis "class" } } { "a class word indicates that this parameter is expected to be an instance of the class most of the time." } } - { { $snippet "{ " { $emphasis "classes..." } " }" } { "a sequence of class words indicates that this parameter is expected to be an instance of one of these classes most of the time." } } - { { $snippet "number" } { "the " { $link number } " class word has a special behavior. It will result in a version of the word being generated for every primitive numeric type, where this parameter is assumed to have that type. A fast jump table will then determine which version is chosen at run time." } } - { { $snippet "*" } { "indicates no specialization should be performed on this parameter." } } -} -"Specialization can help in the case where a word calls a lot of generic words on the same object - perhaps in a loop - and in most cases, it is anticipated that this object is of a certain class. Using specialization hints, the compiler can be instructed to compile a branch at the beginning of the word; if the branch is taken, the input object has the assumed class, and inlining of generic methods can take place." -$nl -"Specialization hints are not declarations; if the inputs do not match what is specified, the word will still run, possibly slower if the compiled code cannot inline methods because of insufficient static type information." -$nl -"In some cases, specialization will not help at all, and can make generated code slower from the increase in code size. The compiler is capable of inferring enough static type information to generate efficient code in many cases without explicit help from the programmer. Specializers should be used as a last resort, after profiling shows that a critical loop makes a lot of repeated calls to generic words which dispatch on the same class." -$nl -"For example, the " { $link append } " word has a specializer for the very common case where two strings or two arrays are appended:" -{ $code -"\\ append" -"{ { string array } { string array } }" -"\"specializer\" set-word-prop" -} -"The specialized version of a word which will be compiled by the compiler can be inspected:" -{ $subsection specialized-def } ; - ARTICLE: "optimizer" "Optimizer" "The words in the " { $vocab-link "optimizer" } " vocabulary are internal to the compiler and user code has no reason to call them." $nl @@ -43,7 +18,3 @@ HELP: optimize-1 HELP: optimize { $values { "node" "a dataflow graph" } { "newnode" "a dataflow graph" } } { $description "Continues to optimize a dataflow graph until a fixed point is reached." } ; - -HELP: specialized-def -{ $values { "word" word } { "quot" quotation } } -{ $description "Outputs the definition of a word after it has been split into specialized branches. This is the definition which will actually be compiled by the compiler." } ; diff --git a/core/optimizer/optimizer.factor b/core/optimizer/optimizer.factor old mode 100644 new mode 100755 index 66e4ac9220..219b27197f --- a/core/optimizer/optimizer.factor +++ b/core/optimizer/optimizer.factor @@ -1,10 +1,7 @@ ! Copyright (C) 2006, 2007 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: arrays generic hashtables kernel kernel.private math -namespaces sequences vectors words strings layouts combinators -combinators.private classes optimizer.backend optimizer.def-use -optimizer.known-words optimizer.math inference.class -generic.standard ; +USING: kernel namespaces optimizer.backend optimizer.def-use +optimizer.known-words optimizer.math inference.class ; IN: optimizer : optimize-1 ( node -- newnode ? ) @@ -22,39 +19,3 @@ IN: optimizer : optimize ( node -- newnode ) optimize-1 [ optimize ] when ; - -: simple-specializer ( quot dispatch# classes -- quot ) - swap (dispatch#) [ - object add* swap [ 2array ] curry map - object method-alist>quot - ] with-variable ; - -: dispatch-specializer ( quot dispatch# symbol dispatcher -- quot ) - rot (dispatch#) [ - [ - picker % - , - get swap , - \ dispatch , - ] [ ] make - ] with-variable ; - -: tag-specializer ( quot dispatch# -- quot ) - num-tags \ tag dispatch-specializer ; - -: type-specializer ( quot dispatch# -- quot ) - num-types \ type dispatch-specializer ; - -: make-specializer ( quot dispatch# spec -- quot ) - { - { [ dup number eq? ] [ drop tag-specializer ] } - { [ dup object eq? ] [ drop type-specializer ] } - { [ dup \ * eq? ] [ 2drop ] } - { [ dup array? ] [ simple-specializer ] } - { [ t ] [ 1array simple-specializer ] } - } cond ; - -: specialized-def ( word -- quot ) - dup word-def swap "specializer" word-prop [ - [ length ] keep [ make-specializer ] 2each - ] when* ; diff --git a/core/optimizer/specializers/specializers-docs.factor b/core/optimizer/specializers/specializers-docs.factor new file mode 100755 index 0000000000..de5d5d7a1f --- /dev/null +++ b/core/optimizer/specializers/specializers-docs.factor @@ -0,0 +1,26 @@ +IN: optimizer.specializers +USING: help.markup help.syntax sequences words quotations ; + +ARTICLE: "specializers" "Word specializers" +"The optimizer can be passed hints as to the classes of parameters a word is expected to be called with. The optimizer will then generate multiple versions of word when compiling, specialized to each class." +$nl +"Specialization hints are stored in the " { $snippet "\"specializer\"" } " word property. The value of this property is either a sequence of classes, or a sequence of sequences of classes. Each element in the sequence (or the sequence itself, in the former case) is a specialization hint." +$nl +"Specialization can help in the case where a word calls a lot of generic words on the same object - perhaps in a loop - and in most cases, it is anticipated that this object is of a certain class. Using specialization hints, the compiler can be instructed to compile a branch at the beginning of the word; if the branch is taken, the input object has the assumed class, and inlining of generic methods can take place." +$nl +"Specialization hints are not declarations; if the inputs do not match what is specified, the word will still run, possibly slower if the compiled code cannot inline methods because of insufficient static type information." +$nl +"In some cases, specialization will not help at all, and can make generated code slower from the increase in code size. The compiler is capable of inferring enough static type information to generate efficient code in many cases without explicit help from the programmer. Specializers should be used as a last resort, after profiling shows that a critical loop makes a lot of repeated calls to generic words which dispatch on the same class." +$nl +"For example, the " { $link append } " word has a specializer for the very common case where two strings or two arrays are appended:" +{ $code +"\\ append" +"{ { string string } { array array } }" +"\"specializer\" set-word-prop" +} +"The specialized version of a word which will be compiled by the compiler can be inspected:" +{ $subsection specialized-def } ; + +HELP: specialized-def +{ $values { "word" word } { "quot" quotation } } +{ $description "Outputs the definition of a word after it has been split into specialized branches. This is the definition which will actually be compiled by the compiler." } ; diff --git a/core/optimizer/specializers/specializers.factor b/core/optimizer/specializers/specializers.factor new file mode 100755 index 0000000000..223ce18117 --- /dev/null +++ b/core/optimizer/specializers/specializers.factor @@ -0,0 +1,41 @@ +! Copyright (C) 2006, 2008 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: arrays generic hashtables kernel kernel.private math +namespaces sequences vectors words strings layouts combinators +combinators.private classes generic.standard assocs ; +IN: optimizer.specializers + +: (make-specializer) ( class picker -- quot ) + swap "predicate" word-prop append ; + +: make-specializer ( classes -- quot ) + dup length + [ (picker) 2array ] 2map + [ drop object eq? not ] assoc-subset + dup empty? [ drop [ t ] ] [ + [ (make-specializer) ] { } assoc>map + unclip [ swap [ f ] \ if 3array append [ ] like ] reduce + ] if ; + +: tag-specializer ( quot -- newquot ) + [ + [ dup tag ] % + num-tags get swap , + \ dispatch , + ] [ ] make ; + +: specialized-def ( word -- quot ) + dup word-def swap "specializer" word-prop [ + dup { number } = [ + drop tag-specializer + ] [ + dup [ array? ] all? [ 1array ] unless [ + [ make-specializer ] keep + [ declare ] curry pick append + ] { } map>assoc + alist>quot + ] if + ] when* ; + +: specialized-length ( specializer -- n ) + dup [ array? ] all? [ first ] when length ; diff --git a/extra/benchmark/recursive/recursive.factor b/extra/benchmark/recursive/recursive.factor old mode 100644 new mode 100755 index 79c6dfbaca..6e3c201cf0 --- a/extra/benchmark/recursive/recursive.factor +++ b/extra/benchmark/recursive/recursive.factor @@ -4,8 +4,6 @@ USING: math kernel hints prettyprint io ; : fib ( m -- n ) dup 2 < [ drop 1 ] [ dup 1 - fib swap 2 - fib + ] if ; -! HINTS: fib { fixnum float } ; -! : ack ( m n -- x ) over zero? [ nip 1+ @@ -17,8 +15,6 @@ USING: math kernel hints prettyprint io ; ] if ] if ; -! HINTS: ack fixnum fixnum ; - : tak ( x y z -- t ) pick pick swap < [ [ rot 1- -rot tak ] 3keep @@ -29,8 +25,6 @@ USING: math kernel hints prettyprint io ; 2nip ] if ; -! HINTS: tak { fixnum float } { fixnum float } { fixnum float } ; - : recursive ( n -- ) 3 over ack . flush dup 27.0 + fib . flush diff --git a/extra/math/vectors/vectors.factor b/extra/math/vectors/vectors.factor index b2a8995df0..2be9cf7f58 100755 --- a/extra/math/vectors/vectors.factor +++ b/extra/math/vectors/vectors.factor @@ -27,20 +27,20 @@ IN: math.vectors : set-axis ( u v axis -- w ) dup length [ >r zero? pick pick ? r> swap nth ] 2map 2nip ; -HINTS: vneg { float-array array } ; -HINTS: norm-sq { float-array array } ; -HINTS: norm { float-array array } ; -HINTS: normalize { float-array array } ; +HINTS: vneg { float-array } { array } ; +HINTS: norm-sq { float-array } { array } ; +HINTS: norm { float-array } { array } ; +HINTS: normalize { float-array } { array } ; -HINTS: n*v * { float-array array } ; -HINTS: v*n { float-array array } * ; -HINTS: n/v * { float-array array } ; -HINTS: v/n { float-array array } * ; +HINTS: n*v { object float-array } { object array } ; +HINTS: v*n { float-array object } { array object } ; +HINTS: n/v { object float-array } { array } ; +HINTS: v/n { float-array object } { array object } ; -HINTS: v+ { float-array array } { float-array array } ; -HINTS: v- { float-array array } { float-array array } ; -HINTS: v* { float-array array } { float-array array } ; -HINTS: v/ { float-array array } { float-array array } ; -HINTS: vmax { float-array array } { float-array array } ; -HINTS: vmin { float-array array } { float-array array } ; -HINTS: v. { float-array array } { float-array array } ; +HINTS: v+ { float-array float-array } { array array } ; +HINTS: v- { float-array float-array } { array array } ; +HINTS: v* { float-array float-array } { array array } ; +HINTS: v/ { float-array float-array } { array array } ; +HINTS: vmax { float-array float-array } { array array } ; +HINTS: vmin { float-array float-array } { array array } ; +HINTS: v. { float-array float-array } { array array } ; From 95651daef07cea2485add25be8791f957b67dc86 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 5 Feb 2008 22:36:10 -0600 Subject: [PATCH 123/269] Faster parser --- core/parser/parser-docs.factor | 6 ---- core/parser/parser.factor | 47 +++++++++++++++++--------------- extra/multiline/multiline.factor | 4 +-- 3 files changed, 27 insertions(+), 30 deletions(-) mode change 100644 => 100755 extra/multiline/multiline.factor diff --git a/core/parser/parser-docs.factor b/core/parser/parser-docs.factor index d8d6c9b7bc..ae38925c68 100755 --- a/core/parser/parser-docs.factor +++ b/core/parser/parser-docs.factor @@ -136,8 +136,6 @@ ARTICLE: "parser-lexer" "The lexer" { $subsection } "A word to test of the end of input has been reached:" { $subsection still-parsing? } -"A word to get the text of the current line:" -{ $subsection line-text } "A word to advance the lexer to the next line:" { $subsection next-line } "Two generic words to override the lexer's token boundary detection:" @@ -222,10 +220,6 @@ HELP: { $values { "msg" "an error" } { "error" parse-error } } { $description "Creates a new " { $link parse-error } ", filling in the location information from the current " { $link lexer } "." } ; -HELP: line-text -{ $values { "lexer" lexer } { "str" string } } -{ $description "Outputs the text of the line being parsed." } ; - HELP: skip { $values { "i" "a starting index" } { "seq" "a sequence" } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "n" integer } } { $description "Variant of " { $link find* } " that outputs the length of the sequence instead of " { $link f } " if no elements satisfy the predicate." } ; diff --git a/core/parser/parser.factor b/core/parser/parser.factor index 6d7ad47843..59d18dc734 100755 --- a/core/parser/parser.factor +++ b/core/parser/parser.factor @@ -8,12 +8,17 @@ io.files io.streams.string io.streams.lines vocabs source-files classes hashtables compiler.errors compiler.units ; IN: parser -TUPLE: lexer text line column ; +TUPLE: lexer text line line-text line-length column ; -: ( text -- lexer ) 1 0 lexer construct-boa ; +: next-line ( lexer -- ) + 0 over set-lexer-column + dup lexer-line over lexer-text ?nth over set-lexer-line-text + dup lexer-line-text length over set-lexer-line-length + dup lexer-line 1+ swap set-lexer-line ; -: line-text ( lexer -- str ) - dup lexer-line 1- swap lexer-text ?nth ; +: ( text -- lexer ) + 0 { set-lexer-text set-lexer-line } lexer construct + dup lexer-text empty? [ dup next-line ] unless ; : location ( -- loc ) file get lexer get lexer-line 2dup and @@ -50,18 +55,14 @@ t parser-notes set-global "Note: " write dup print ] when drop ; -: next-line ( lexer -- ) - 0 over set-lexer-column - dup lexer-line 1+ swap set-lexer-line ; - : skip ( i seq ? -- n ) over >r [ swap CHAR: \s eq? xor ] curry find* drop - [ r> drop ] [ r> length ] if* ; inline + [ r> drop ] [ r> length ] if* ; : change-column ( lexer quot -- ) swap - [ dup lexer-column swap line-text rot call ] keep + [ dup lexer-column swap lexer-line-text rot call ] keep set-lexer-column ; inline GENERIC: skip-blank ( lexer -- ) @@ -73,20 +74,20 @@ GENERIC: skip-word ( lexer -- ) M: lexer skip-word ( lexer -- ) [ - 2dup nth CHAR: " = [ drop 1+ ] [ f skip ] if + 2dup nth CHAR: " eq? [ drop 1+ ] [ f skip ] if ] change-column ; : still-parsing? ( lexer -- ? ) dup lexer-line swap lexer-text length <= ; : still-parsing-line? ( lexer -- ? ) - dup lexer-column swap line-text length < ; + dup lexer-column swap lexer-line-length < ; : (parse-token) ( lexer -- str ) [ lexer-column ] keep [ skip-word ] keep [ lexer-column ] keep - line-text subseq ; + lexer-line-text subseq ; : parse-token ( lexer -- str/f ) dup still-parsing? [ @@ -139,9 +140,8 @@ TUPLE: parse-error file line col text ; : ( msg -- error ) file get - lexer get lexer-line - lexer get lexer-column - lexer get line-text + lexer get + { lexer-line lexer-column lexer-line-text } get-slots parse-error construct-boa [ set-delegate ] keep ; @@ -239,22 +239,25 @@ M: no-word summary word-restarts throw-restarts dup word-vocabulary (use+) ; -: check-forward ( str word -- word ) +: check-forward ( str word -- word/f ) dup forward-reference? [ drop - dup use get + use get [ at ] with map [ ] subset [ forward-reference? not ] find nip - [ ] [ no-word ] ?if ] [ nip ] if ; -: search ( str -- word ) - dup use get assoc-stack [ check-forward ] [ no-word ] if* ; +: search ( str -- word/f ) + dup use get assoc-stack check-forward ; : scan-word ( -- word/number/f ) - scan dup [ dup string>number [ ] [ search ] ?if ] when ; + scan dup [ + dup search [ ] [ + dup string>number [ ] [ no-word ] ?if + ] ?if + ] when ; TUPLE: staging-violation word ; diff --git a/extra/multiline/multiline.factor b/extra/multiline/multiline.factor old mode 100644 new mode 100755 index 7f831e5351..9a6d052b60 --- a/extra/multiline/multiline.factor +++ b/extra/multiline/multiline.factor @@ -4,7 +4,7 @@ USING: namespaces parser kernel sequences words quotations math ; IN: multiline : next-line-text ( -- str ) - lexer get dup next-line line-text ; + lexer get dup next-line lexer-line-text ; : (parse-here) ( -- ) next-line-text dup ";" = @@ -19,7 +19,7 @@ IN: multiline parse-here 1quotation define ; parsing : (parse-multiline-string) ( start-index end-text -- end-index ) - lexer get line-text 2dup start + lexer get lexer-line-text 2dup start [ rot dupd >r >r swap subseq % r> r> length + ] [ rot tail % "\n" % 0 lexer get next-line swap (parse-multiline-string) From ac0aa6b3b20354042f1b7dd74e596768391d2a5d Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Tue, 5 Feb 2008 22:49:36 -0600 Subject: [PATCH 124/269] do a better merge --- Makefile | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 06d0b28ccf..05a185f643 100755 --- a/Makefile +++ b/Makefile @@ -126,14 +126,10 @@ solaris-x86-64: winnt-x86-32: $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.32 -<<<<<<< HEAD:Makefile -windows-nt-x86-64: +winnt-x86-64: $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.64 -windows-ce-arm: -======= wince-arm: ->>>>>>> 1eda70f1ad1f0d744ed846ce8c975a1cd4b28fb6:Makefile $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.ce.arm macosx.app: factor From 3bbf622ff4795148fc10e5f9611029550a1c37db Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Tue, 5 Feb 2008 22:51:09 -0600 Subject: [PATCH 125/269] update factor.sh for new Makefile renaming --- misc/factor.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/misc/factor.sh b/misc/factor.sh index d1ef738cd9..903038a964 100755 --- a/misc/factor.sh +++ b/misc/factor.sh @@ -99,9 +99,9 @@ find_os() { uname_s=`uname -s` check_ret uname case $uname_s in - CYGWIN_NT-5.2-WOW64) OS=windows-nt;; - *CYGWIN_NT*) OS=windows-nt;; - *CYGWIN*) OS=windows-nt;; + CYGWIN_NT-5.2-WOW64) OS=winnt;; + *CYGWIN_NT*) OS=winnt;; + *CYGWIN*) OS=winnt;; *darwin*) OS=macosx;; *Darwin*) OS=macosx;; *linux*) OS=linux;; @@ -139,7 +139,7 @@ find_word_size() { set_factor_binary() { case $OS in - windows-nt) FACTOR_BINARY=factor-nt;; + winnt) FACTOR_BINARY=factor-nt;; macosx) FACTOR_BINARY=./Factor.app/Contents/MacOS/factor;; *) FACTOR_BINARY=factor;; esac @@ -227,7 +227,7 @@ get_boot_image() { } maybe_download_dlls() { - if [[ $OS == windows-nt ]] ; then + if [[ $OS == winnt ]] ; then wget http://factorcode.org/dlls/freetype6.dll check_ret wget wget http://factorcode.org/dlls/zlib1.dll From 4439e394cca6be96a06d85a9532795bd052f8f1a Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Tue, 5 Feb 2008 23:04:31 -0600 Subject: [PATCH 126/269] fix getcwd --- extra/io/unix/files/files.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/io/unix/files/files.factor b/extra/io/unix/files/files.factor index a70f7339d2..101114ffb2 100755 --- a/extra/io/unix/files/files.factor +++ b/extra/io/unix/files/files.factor @@ -6,7 +6,7 @@ alien ; IN: io.unix.files M: unix-io cwd - MAXPATHLEN dup getcwd + MAXPATHLEN dup swap getcwd [ alien>char-string ] [ (io-error) ] if* ; M: unix-io cd From e3e2cc7e0d647b628b245372a7c178ed492f42c4 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Tue, 5 Feb 2008 23:09:33 -0600 Subject: [PATCH 127/269] Add builder.load-everything --- extra/builder/builder.factor | 57 ++++++++++++------- .../load-everything/load-everything.factor | 23 ++++++++ 2 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 extra/builder/load-everything/load-everything.factor diff --git a/extra/builder/builder.factor b/extra/builder/builder.factor index 832b89a7dc..375023cb5e 100755 --- a/extra/builder/builder.factor +++ b/extra/builder/builder.factor @@ -33,19 +33,19 @@ SYMBOL: builder-recipients ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -! : target ( -- target ) `{ ,[ os ] %[ cpu "." split ] } "-" join ; +: target ( -- target ) `{ ,[ os ] %[ cpu "." split ] } "-" join ; -: target ( -- target ) - { { [ os "windows" = ] [ "windows-nt-x86-32" ] } - { [ t ] [ `{ ,[ os ] %[ cpu "." split ] } "-" join ] } } - cond ; +! : target ( -- target ) +! { { [ os "windows" = ] [ "windows-nt-x86-32" ] } +! { [ t ] [ `{ ,[ os ] %[ cpu "." split ] } "-" join ] } } +! cond ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! : factor-binary ( -- name ) os { { "macosx" [ "./Factor.app/Contents/MacOS/factor" ] } - { "windows" [ "./factor-nt.exe" ] } + { "winnt" [ "./factor-nt.exe" ] } [ drop "./factor" ] } case ; @@ -61,7 +61,13 @@ VAR: stamp "/builds/factor" cd - { "git" "pull" "--no-summary" "git://factorcode.org/git/factor.git" } + { + "git" + "pull" + "--no-summary" + "git://factorcode.org/git/factor.git" + "master" + } run-process process-status 0 = [ ] @@ -74,7 +80,7 @@ VAR: stamp "/builds/" stamp> append make-directory "/builds/" stamp> append cd - { "git" "clone" "/builds/factor" } run-process drop + { "git" "clone" "../factor" } run-process drop "factor" cd @@ -121,20 +127,27 @@ VAR: stamp "builder: bootstrap" throw ] if - `{ - { +arguments+ - { ,[ factor-binary ] "-e=USE: tools.browser load-everything" } } - { +stdout+ "../load-everything-log" } - { +stderr+ +stdout+ } - } - >hashtable [ run-process process-status ] benchmark nip - "../load-everything-time" [ . ] with-stream - 0 = - [ ] - [ - "builder: load-everything" "../load-everything-log" email-file - "builder: load-everything" throw - ] if ; +! `{ +! { +arguments+ +! { ,[ factor-binary ] "-e=USE: tools.browser load-everything" } } +! { +stdout+ "../load-everything-log" } +! { +stderr+ +stdout+ } +! } +! >hashtable [ run-process process-status ] benchmark nip +! "../load-everything-time" [ . ] with-stream +! 0 = +! [ ] +! [ +! "builder: load-everything" "../load-everything-log" email-file +! "builder: load-everything" throw +! ] if ; + + `{ ,[ factor-binary ] "-run=builder.load-everything" } run-process drop + "../load-everything-log" exists? + [ "builder: load-everything" "../load-everything-log" email-file ] + when + + ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! diff --git a/extra/builder/load-everything/load-everything.factor b/extra/builder/load-everything/load-everything.factor new file mode 100644 index 0000000000..12007f214b --- /dev/null +++ b/extra/builder/load-everything/load-everything.factor @@ -0,0 +1,23 @@ + +USING: kernel continuations io io.files prettyprint vocabs.loader + tools.time tools.browser ; + +IN: builder.load-everything + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: runtime ( quot -- time ) benchmark nip ; + +: log-runtime ( quot file -- ) + >r runtime r> [ . ] with-stream ; + +: log-object ( object file -- ) [ . ] with-stream ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: do-load-everything ( -- ) + [ [ load-everything ] catch ] "../load-everything-time" log-runtime + [ require-all-error-vocabs "../load-everything-log" log-object ] + when ; + +MAIN: do-load-everything \ No newline at end of file From 537d94566005c51b29fe358f79d2709b33c4b392 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Tue, 5 Feb 2008 23:14:10 -0600 Subject: [PATCH 128/269] fix getcwd --- extra/io/unix/files/files.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/io/unix/files/files.factor b/extra/io/unix/files/files.factor index 101114ffb2..3bf0e3f897 100755 --- a/extra/io/unix/files/files.factor +++ b/extra/io/unix/files/files.factor @@ -6,8 +6,8 @@ alien ; IN: io.unix.files M: unix-io cwd - MAXPATHLEN dup swap getcwd - [ alien>char-string ] [ (io-error) ] if* ; + MAXPATHLEN dup swap + getcwd [ (io-error) ] unless* ; M: unix-io cd chdir io-error ; From d27ae067089d7d196cc9634fd87940e0717ca236 Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Wed, 6 Feb 2008 00:53:18 -0500 Subject: [PATCH 129/269] Solution to Project Euler problem 44 --- extra/project-euler/044/044.factor | 50 ++++++++++++++++++++++++ extra/project-euler/project-euler.factor | 7 ++-- 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 extra/project-euler/044/044.factor diff --git a/extra/project-euler/044/044.factor b/extra/project-euler/044/044.factor new file mode 100644 index 0000000000..6369cb5372 --- /dev/null +++ b/extra/project-euler/044/044.factor @@ -0,0 +1,50 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel math math.functions math.ranges project-euler.common sequences ; +IN: project-euler.044 + +! http://projecteuler.net/index.php?section=problems&id=44 + +! DESCRIPTION +! ----------- + +! Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten +! pentagonal numbers are: + +! 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... + +! It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, +! 70 − 22 = 48, is not pentagonal. + +! Find the pair of pentagonal numbers, Pj and Pk, for which their sum and +! difference is pentagonal and D = |Pk − Pj| is minimised; what is the value of D? + + +! SOLUTION +! -------- + +! Brute force using a cartesian product and an arbitrarily chosen limit. + + [ 24 * 1+ sqrt 1+ 6 / 1 mod zero? ] [ drop f ] if ; + +: sum-and-diff? ( m n -- ? ) + 2dup + -rot - [ pentagonal? ] 2apply and ; + +PRIVATE> + +: euler044 ( -- answer ) + 2500 [1,b] [ nth-pentagonal ] map dup cartesian-product + [ first2 sum-and-diff? ] subset [ first2 - abs ] map infimum ; + +! [ euler044 ] 10 ave-time +! 8924 ms run / 2872 ms GC ave time - 10 trials + +! TODO: this solution is ugly and not very efficient...find a better algorithm + +MAIN: euler044 diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index ef28cf8778..36a9069d77 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -12,9 +12,10 @@ USING: definitions io io.files kernel math math.parser project-euler.ave-time project-euler.029 project-euler.030 project-euler.031 project-euler.032 project-euler.033 project-euler.034 project-euler.035 project-euler.036 project-euler.037 project-euler.038 project-euler.039 project-euler.040 - project-euler.041 project-euler.042 project-euler.043 project-euler.048 - project-euler.052 project-euler.067 project-euler.075 project-euler.097 - project-euler.134 project-euler.169 project-euler.173 project-euler.175 ; + project-euler.041 project-euler.042 project-euler.043 project-euler.044 + project-euler.048 project-euler.052 project-euler.067 project-euler.075 + project-euler.097 project-euler.134 project-euler.169 project-euler.173 + project-euler.175 ; IN: project-euler Date: Wed, 6 Feb 2008 04:26:13 -0600 Subject: [PATCH 130/269] Add builder.test --- extra/builder/builder.factor | 46 +++++++++++++--------------------- extra/builder/test/test.factor | 33 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 28 deletions(-) create mode 100644 extra/builder/test/test.factor diff --git a/extra/builder/builder.factor b/extra/builder/builder.factor index 375023cb5e..2acdbc3294 100755 --- a/extra/builder/builder.factor +++ b/extra/builder/builder.factor @@ -8,6 +8,15 @@ IN: builder ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +: runtime ( quot -- time ) benchmark nip ; + +: log-runtime ( quot file -- ) + >r runtime r> [ . ] with-stream ; + +: log-object ( object file -- ) [ . ] with-stream ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + : datestamp ( -- string ) now `{ ,[ dup timestamp-year ] ,[ dup timestamp-month ] @@ -35,11 +44,6 @@ SYMBOL: builder-recipients : target ( -- target ) `{ ,[ os ] %[ cpu "." split ] } "-" join ; -! : target ( -- target ) -! { { [ os "windows" = ] [ "windows-nt-x86-32" ] } -! { [ t ] [ `{ ,[ os ] %[ cpu "." split ] } "-" join ] } } -! cond ; - ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! : factor-binary ( -- name ) @@ -84,10 +88,8 @@ VAR: stamp "factor" cd - { "git" "show" } - [ readln ] with-stream - " " split second - "../git-id" [ print ] with-stream + { "git" "show" } [ readln ] with-stream " " split second + "../git-id" log-object { "make" "clean" } run-process drop @@ -117,9 +119,7 @@ VAR: stamp { +stdout+ "../boot-log" } { +stderr+ +stdout+ } } - >hashtable - [ run-process process-status ] - benchmark nip "../boot-time" [ . ] with-stream + >hashtable [ run-process ] "../boot-time" log-runtime process-status 0 = [ ] [ @@ -127,26 +127,16 @@ VAR: stamp "builder: bootstrap" throw ] if -! `{ -! { +arguments+ -! { ,[ factor-binary ] "-e=USE: tools.browser load-everything" } } -! { +stdout+ "../load-everything-log" } -! { +stderr+ +stdout+ } -! } -! >hashtable [ run-process process-status ] benchmark nip -! "../load-everything-time" [ . ] with-stream -! 0 = -! [ ] -! [ -! "builder: load-everything" "../load-everything-log" email-file -! "builder: load-everything" throw -! ] if ; - - `{ ,[ factor-binary ] "-run=builder.load-everything" } run-process drop + `{ ,[ factor-binary ] "-run=builder.test" } run-process drop + "../load-everything-log" exists? [ "builder: load-everything" "../load-everything-log" email-file ] when + "../failing-tests" exists? + [ "builder: failing tests" "../failing-tests" email-file ] + when + ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! diff --git a/extra/builder/test/test.factor b/extra/builder/test/test.factor new file mode 100644 index 0000000000..ed75e99527 --- /dev/null +++ b/extra/builder/test/test.factor @@ -0,0 +1,33 @@ + +USING: kernel sequences assocs builder continuations vocabs vocabs.loader + io + io.files + tools.browser + tools.test ; + +IN: builder.test + +: do-load ( -- ) + [ [ load-everything ] catch ] "../load-everything-time" log-runtime + [ require-all-error-vocabs "../load-everything-log" log-object ] + when* ; + +: do-tests ( -- ) + "" child-vocabs + [ vocab-source-loaded? ] subset + [ vocab-tests-path ] map + [ dup [ ?resource-path exists? ] when ] subset + [ dup run-test ] { } map>assoc + [ second empty? not ] subset + dup empty? + [ drop ] + [ + "../failing-tests" + [ [ nl failures. ] assoc-each ] + with-stream + ] + if ; + +: do-all ( -- ) do-load do-tests ; + +MAIN: do-all \ No newline at end of file From a5c69dae631af981fdc598828c44ecdc12423bbe Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Wed, 6 Feb 2008 06:10:55 -0600 Subject: [PATCH 131/269] update builder.test --- .../load-everything/load-everything.factor | 23 ------------------- extra/builder/test/test.factor | 9 +++++--- 2 files changed, 6 insertions(+), 26 deletions(-) delete mode 100644 extra/builder/load-everything/load-everything.factor diff --git a/extra/builder/load-everything/load-everything.factor b/extra/builder/load-everything/load-everything.factor deleted file mode 100644 index 12007f214b..0000000000 --- a/extra/builder/load-everything/load-everything.factor +++ /dev/null @@ -1,23 +0,0 @@ - -USING: kernel continuations io io.files prettyprint vocabs.loader - tools.time tools.browser ; - -IN: builder.load-everything - -! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -: runtime ( quot -- time ) benchmark nip ; - -: log-runtime ( quot file -- ) - >r runtime r> [ . ] with-stream ; - -: log-object ( object file -- ) [ . ] with-stream ; - -! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -: do-load-everything ( -- ) - [ [ load-everything ] catch ] "../load-everything-time" log-runtime - [ require-all-error-vocabs "../load-everything-log" log-object ] - when ; - -MAIN: do-load-everything \ No newline at end of file diff --git a/extra/builder/test/test.factor b/extra/builder/test/test.factor index ed75e99527..fb9c62e2aa 100644 --- a/extra/builder/test/test.factor +++ b/extra/builder/test/test.factor @@ -8,9 +8,12 @@ USING: kernel sequences assocs builder continuations vocabs vocabs.loader IN: builder.test : do-load ( -- ) - [ [ load-everything ] catch ] "../load-everything-time" log-runtime - [ require-all-error-vocabs "../load-everything-log" log-object ] - when* ; + [ + [ load-everything ] + [ require-all-error-vocabs "../load-everything-log" log-object ] + recover + ] + "../load-everything-time" log-runtime ; : do-tests ( -- ) "" child-vocabs From 548e6dce4774507eb289968268438c255028c054 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 12:09:42 -0600 Subject: [PATCH 132/269] Fixing crossreferencing --- core/compiler/test/redefine.factor | 37 +++++++++++++++++++++++++++++ core/generic/generic-tests.factor | 37 +++++++++++++++++++++++++++++ core/generic/generic.factor | 7 +++++- core/words/words.factor | 29 ++++++++++------------ extra/help/handbook/handbook.factor | 2 ++ 5 files changed, 94 insertions(+), 18 deletions(-) diff --git a/core/compiler/test/redefine.factor b/core/compiler/test/redefine.factor index 01dd27f8be..9bcdcdfcde 100755 --- a/core/compiler/test/redefine.factor +++ b/core/compiler/test/redefine.factor @@ -250,3 +250,40 @@ DEFER: defer-redefine-test-2 [ ] [ "IN: temporary : defer-redefine-test-1 2 ;" eval ] unit-test [ 2 1 ] [ defer-redefine-test-2 ] unit-test + +! Cross-referencing issue +: compiled-xref-a ; + +: compiled-xref-c ; inline + +GENERIC: compiled-xref-b ( a -- b ) + +TUPLE: c-1 ; + +M: c-1 compiled-xref-b compiled-xref-a compiled-xref-c ; + +TUPLE: c-2 ; + +M: c-2 compiled-xref-b drop 3 ; + +[ t ] [ + \ compiled-xref-a compiled-crossref get key? +] unit-test + +[ ] [ + [ + \ compiled-xref-a forget + ] with-compilation-unit +] unit-test + +[ f ] [ + \ compiled-xref-a compiled-crossref get key? +] unit-test + +[ ] [ + "IN: temporary : compiled-xref-c ; FORGET: { c-2 compiled-xref-b }" eval +] unit-test + +[ f ] [ + \ compiled-xref-a compiled-crossref get key? +] unit-test diff --git a/core/generic/generic-tests.factor b/core/generic/generic-tests.factor index f1e1ebd6d2..4de05aafd0 100755 --- a/core/generic/generic-tests.factor +++ b/core/generic/generic-tests.factor @@ -203,3 +203,40 @@ TUPLE: redefinition-test-tuple ; redefinition-test-generic , ] { } make all-equal? ] unit-test + +! Issues with forget +GENERIC: generic-forget-test-1 + +M: integer generic-forget-test-1 / ; + +[ t ] [ + \ / usage [ word? ] subset + [ word-name "generic-forget-test-1/integer" = ] contains? +] unit-test + +[ ] [ + [ \ generic-forget-test-1 forget ] with-compilation-unit +] unit-test + +[ f ] [ + \ / usage [ word? ] subset + [ word-name "generic-forget-test-1/integer" = ] contains? +] unit-test + +GENERIC: generic-forget-test-2 + +M: sequence generic-forget-test-2 = ; + +[ t ] [ + \ = usage [ word? ] subset + [ word-name "generic-forget-test-2/sequence" = ] contains? +] unit-test + +[ ] [ + [ { sequence generic-forget-test-2 } forget ] with-compilation-unit +] unit-test + +[ f ] [ + \ = usage [ word? ] subset + [ word-name "generic-forget-test-2/sequence" = ] contains? +] unit-test diff --git a/core/generic/generic.factor b/core/generic/generic.factor index 453d72effb..53f47c09d5 100755 --- a/core/generic/generic.factor +++ b/core/generic/generic.factor @@ -102,7 +102,9 @@ M: method-spec definition first2 method dup [ method-def ] when ; : forget-method ( class generic -- ) - check-method [ delete-at ] with-methods ; + check-method + [ delete-at* ] with-methods + [ method-word forget ] [ drop ] if ; M: method-spec forget* first2 forget-method ; @@ -145,5 +147,8 @@ M: generic subwords swap "default-method" word-prop add [ method-word ] map ; +M: generic forget-word + dup subwords [ forget-word ] each (forget-word) ; + : xref-generics ( -- ) all-words [ subwords [ xref ] each ] each ; diff --git a/core/words/words.factor b/core/words/words.factor index 93b1185335..c2118598af 100755 --- a/core/words/words.factor +++ b/core/words/words.factor @@ -71,7 +71,9 @@ GENERIC# (quot-uses) 1 ( obj assoc -- ) M: object (quot-uses) 2drop ; -M: interned (quot-uses) dupd set-at ; +M: word (quot-uses) + >r dup "forgotten" word-prop + [ r> 2drop ] [ dup r> set-at ] if ; : seq-uses ( seq assoc -- ) [ (quot-uses) ] curry each ; @@ -194,24 +196,17 @@ M: word where "loc" word-prop ; M: word set-where swap "loc" set-word-prop ; -GENERIC: (forget-word) ( word -- ) +GENERIC: forget-word ( word -- ) -M: interned (forget-word) - dup word-name swap word-vocabulary vocab-words delete-at ; +: (forget-word) ( word -- ) + dup "forgotten" word-prop [ + dup delete-xref + dup delete-compiled-xref + dup word-name over word-vocabulary vocab-words delete-at + dup t "forgotten" set-word-prop + ] unless drop ; -M: word (forget-word) - drop ; - -: rename-word ( word newname newvocab -- ) - pick (forget-word) - pick set-word-vocabulary - over set-word-name - reveal ; - -: forget-word ( word -- ) - dup delete-xref - dup delete-compiled-xref - (forget-word) ; +M: word forget-word (forget-word) ; M: word forget* forget-word ; diff --git a/extra/help/handbook/handbook.factor b/extra/help/handbook/handbook.factor index 81e4bea7b3..d6b4ec7ffe 100755 --- a/extra/help/handbook/handbook.factor +++ b/extra/help/handbook/handbook.factor @@ -32,6 +32,8 @@ $nl { { $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 "$" { $emphasis "foo" } } { "help markup" } { $links $heading $emphasis } } } +{ $heading "Stack effect conventions" } +"Stack effect conventions are documented in " { $link "effect-declaration" } "." { $heading "Glossary of terms" } "Common terminology and abbreviations used throughout Factor and its documentation:" { $table From 38b4f67b70d7cbe007fdb525dc8931edae8bd6b7 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 12:44:13 -0600 Subject: [PATCH 133/269] Save bootstrap time in a global variable --- core/bootstrap/stage2.factor | 79 +++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/core/bootstrap/stage2.factor b/core/bootstrap/stage2.factor index 1a9bdd599a..9dd56c6524 100755 --- a/core/bootstrap/stage2.factor +++ b/core/bootstrap/stage2.factor @@ -8,25 +8,63 @@ definitions assocs compiler.errors compiler.units math.parser generic ; IN: bootstrap.stage2 +SYMBOL: bootstrap-time + +: default-image-name ( -- string ) + vm file-name windows? [ "." split1 drop ] when + ".image" append ; + +: do-crossref ( -- ) + "Cross-referencing..." print flush + H{ } clone crossref set-global + xref-words + xref-generics + xref-sources ; + +: load-components ( -- ) + "exclude" "include" + [ get-global " " split [ empty? not ] subset ] 2apply + seq-diff + [ "bootstrap." swap append require ] each ; + +: compile-remaining ( -- ) + "Compiling remaining words..." print flush + vocabs [ + words "compile" "compiler" lookup execute + ] each ; + +: count-words ( pred -- ) + all-words swap subset length number>string write ; + +: print-report ( time -- ) + 1000 /i + 60 /mod swap + "Bootstrap completed in " write number>string write + " minutes and " write number>string write " seconds." print + + [ compiled? ] count-words " compiled words" print + [ symbol? ] count-words " symbol words" print + [ ] count-words " words total" print + + "Bootstrapping is complete." print + "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 [ - vm file-name windows? [ "." split1 drop ] when - ".image" append "output-image" set-global + ! We time bootstrap + millis >r + + default-image-name "output-image" set-global "math help compiler tools ui ui.tools io" "include" set-global "" "exclude" set-global parse-command-line - "-no-crossref" cli-args member? [ - "Cross-referencing..." print flush - H{ } clone crossref set-global - xref-words - xref-generics - xref-sources - ] unless + "-no-crossref" cli-args member? [ do-crossref ] unless ! Set dll paths wince? [ "windows.ce" require ] when @@ -40,19 +78,12 @@ IN: bootstrap.stage2 ] if [ - "exclude" "include" - [ get-global " " split [ empty? not ] subset ] 2apply - seq-diff - [ "bootstrap." swap append require ] each + load-components run-bootstrap-init - "Compiling remaining words..." print flush - "bootstrap.compiler" vocab [ - vocabs [ - words "compile" "compiler" lookup execute - ] each + compile-remaining ] when ] with-compiler-errors :errors @@ -74,16 +105,8 @@ IN: bootstrap.stage2 ] [ print-error 1 exit ] recover ] set-boot-quot - : count-words ( pred -- ) - all-words swap subset length number>string write ; - - [ compiled? ] count-words " compiled words" print - [ symbol? ] count-words " symbol words" print - [ ] count-words " words total" print - - "Bootstrapping is complete." print - "Now, you can run Factor:" print - vm write " -i=" write "output-image" get print flush + millis r> - dup bootstrap-time set-global + print-report "output-image" get resource-path save-image-and-exit ] if From d9338b1cd26a519d00fee2bbab7cebdcf888ecb0 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 12:47:15 -0600 Subject: [PATCH 134/269] Remove interned predicate class --- core/classes/classes-tests.factor | 4 +++- core/compiler/test/redefine.factor | 4 +--- core/source-files/source-files.factor | 2 +- core/tuples/tuples-tests.factor | 2 +- core/vocabs/vocabs-docs.factor | 2 +- core/words/words-docs.factor | 16 +--------------- core/words/words-tests.factor | 11 +---------- core/words/words.factor | 21 ++++++++++++--------- 8 files changed, 21 insertions(+), 41 deletions(-) mode change 100644 => 100755 core/vocabs/vocabs-docs.factor diff --git a/core/classes/classes-tests.factor b/core/classes/classes-tests.factor index 854e6add5a..efff0db5d1 100755 --- a/core/classes/classes-tests.factor +++ b/core/classes/classes-tests.factor @@ -172,7 +172,9 @@ UNION: forget-class-bug-2 forget-class-bug-1 dll ; FORGET: forget-class-bug-1 FORGET: forget-class-bug-2 -[ t ] [ integer dll class-or interned? ] unit-test +[ f ] [ forget-class-bug-1 typemap get values [ memq? ] with contains? ] unit-test + +[ f ] [ forget-class-bug-2 typemap get values [ memq? ] with contains? ] unit-test DEFER: mixin-forget-test-g diff --git a/core/compiler/test/redefine.factor b/core/compiler/test/redefine.factor index 9bcdcdfcde..5d07e764d6 100755 --- a/core/compiler/test/redefine.factor +++ b/core/compiler/test/redefine.factor @@ -92,8 +92,6 @@ DEFER: x-4 [ t ] [ \ x-3 "compiled-uses" word-prop >boolean ] unit-test -[ t ] [ \ x-3 "compiled-uses" word-prop [ drop interned? ] assoc-all? ] unit-test - DEFER: g-test-1 DEFER: g-test-3 @@ -237,7 +235,7 @@ DEFER: flushable-test-2 : bx ax ; [ \ bx forget ] with-compilation-unit -[ t ] [ \ ax compiled-usage [ drop interned? ] assoc-all? ] unit-test +[ f ] [ \ bx \ ax compiled-usage contains? ] unit-test DEFER: defer-redefine-test-2 diff --git a/core/source-files/source-files.factor b/core/source-files/source-files.factor index 64ae2e376e..7ddf6f02c0 100755 --- a/core/source-files/source-files.factor +++ b/core/source-files/source-files.factor @@ -38,7 +38,7 @@ uses definitions ; : (xref-source) ( source-file -- pathname uses ) dup source-file-path swap source-file-uses - [ interned? ] subset ; + [ crossref? ] subset ; : xref-source ( source-file -- ) (xref-source) crossref get add-vertex ; diff --git a/core/tuples/tuples-tests.factor b/core/tuples/tuples-tests.factor index edd2387645..627ee5562f 100755 --- a/core/tuples/tuples-tests.factor +++ b/core/tuples/tuples-tests.factor @@ -123,7 +123,7 @@ TUPLE: yo-momma ; [ ] [ \ yo-momma forget ] unit-test [ f ] [ \ yo-momma typemap get values memq? ] unit-test - [ f ] [ \ yo-momma interned? ] unit-test + [ f ] [ \ yo-momma crossref ] unit-test ] with-compilation-unit TUPLE: loc-recording ; diff --git a/core/vocabs/vocabs-docs.factor b/core/vocabs/vocabs-docs.factor old mode 100644 new mode 100755 index cb2cabb369..f16a33f0d5 --- a/core/vocabs/vocabs-docs.factor +++ b/core/vocabs/vocabs-docs.factor @@ -76,7 +76,7 @@ HELP: all-words HELP: forget-vocab { $values { "vocab" string } } -{ $description "Removes a vocabulary. All words in the vocabulary become uninterned." } +{ $description "Removes a vocabulary. All words in the vocabulary are forgotten." } { $notes "This word must be called from inside " { $link with-compilation-unit } "." } ; HELP: load-vocab-hook diff --git a/core/words/words-docs.factor b/core/words/words-docs.factor index 24e81c70a6..62848e46b2 100755 --- a/core/words/words-docs.factor +++ b/core/words/words-docs.factor @@ -14,9 +14,7 @@ $nl { $subsection lookup } "Words can output their name and vocabulary:" { $subsection word-name } -{ $subsection word-vocabulary } -"Testing if a word object is part of a vocabulary:" -{ $subsection interned? } ; +{ $subsection word-vocabulary } ; ARTICLE: "uninterned-words" "Uninterned words" "A word that is not a member of any vocabulary is said to be " { $emphasis "uninterned" } "." @@ -369,18 +367,6 @@ HELP: delimiter? { $description "Tests if an object is a delimiter word declared by " { $link POSTPONE: delimiter } "." } { $notes "Outputs " { $link f } " if the object is not a word." } ; -HELP: interned -{ $class-description "The class of words defined in the " { $link dictionary } "." } -{ $examples - { $example "\\ + interned? ." "t" } - { $example "gensym interned? ." "f" } -} ; - -HELP: rename-word -{ $values { "word" word } { "newname" string } { "newvocab" string } } -{ $description "Changes the name and vocabulary of a word, and adds it to its new vocabulary." } -{ $side-effects "word" } ; - HELP: make-flushable { $values { "word" word } } { $description "Declares a word as " { $link POSTPONE: flushable } "." } diff --git a/core/words/words-tests.factor b/core/words/words-tests.factor index 35a2421e71..92f5284c49 100755 --- a/core/words/words-tests.factor +++ b/core/words/words-tests.factor @@ -54,22 +54,14 @@ GENERIC: testing [ f ] [ \ testing generic? ] unit-test -[ f ] [ gensym interned? ] unit-test - : forgotten ; : another-forgotten ; -[ f ] [ \ forgotten interned? ] unit-test - FORGET: forgotten -[ f ] [ \ another-forgotten interned? ] unit-test - FORGET: another-forgotten : another-forgotten ; -[ t ] [ \ + interned? ] unit-test - ! I forgot remove-crossref calls! : fee ; : foe fee ; @@ -87,8 +79,7 @@ FORGET: foe ] unit-test [ t ] [ - \ * usage [ word? ] subset - [ dup interned? swap method-body? or ] all? + \ * usage [ word? ] subset [ crossref? ] all? ] unit-test DEFER: calls-a-gensym diff --git a/core/words/words.factor b/core/words/words.factor index c2118598af..f628d68bee 100755 --- a/core/words/words.factor +++ b/core/words/words.factor @@ -1,10 +1,10 @@ -! Copyright (C) 2004, 2007 Slava Pestov. +! Copyright (C) 2004, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -IN: words USING: arrays definitions graphs assocs kernel kernel.private slots.private math namespaces sequences strings vectors sbufs quotations assocs hashtables sorting math.parser words.private -vocabs ; +vocabs combinators ; +IN: words : word ( -- word ) \ word get-global ; @@ -65,15 +65,20 @@ SYMBOL: bootstrapping? : bootstrap-word ( word -- target ) [ target-word ] [ ] if-bootstrapping ; -PREDICATE: word interned dup target-word eq? ; +: crossref? ( word -- ? ) + { + { [ dup "forgotten" word-prop ] [ f ] } + { [ dup "method" word-prop ] [ t ] } + { [ dup word-vocabulary ] [ t ] } + { [ t ] [ f ] } + } cond nip ; GENERIC# (quot-uses) 1 ( obj assoc -- ) M: object (quot-uses) 2drop ; M: word (quot-uses) - >r dup "forgotten" word-prop - [ r> 2drop ] [ dup r> set-at ] if ; + >r dup crossref? [ dup r> set-at ] [ r> 2drop ] if ; : seq-uses ( seq assoc -- ) [ (quot-uses) ] curry each ; @@ -94,6 +99,7 @@ SYMBOL: compiled-crossref compiled-crossref global [ H{ } assoc-like ] change-at : compiled-xref ( word dependencies -- ) + [ crossref? ] subset 2dup "compiled-uses" set-word-prop compiled-crossref get add-vertex* ; @@ -118,9 +124,6 @@ SYMBOL: changed-words [ no-compilation-unit ] unless* set-at ; -: crossref? ( word -- ? ) - dup word-vocabulary swap "method" word-prop or ; - : define ( word def -- ) [ ] like over unxref From 8a4db990297699eb69f1e5c105230fa75314dc54 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 13:15:15 -0600 Subject: [PATCH 135/269] Improved tools.test --- extra/tools/test/test.factor | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/extra/tools/test/test.factor b/extra/tools/test/test.factor index 2a26c8639e..aa994e91d2 100755 --- a/extra/tools/test/test.factor +++ b/extra/tools/test/test.factor @@ -3,7 +3,7 @@ USING: namespaces arrays prettyprint sequences kernel vectors quotations words parser assocs combinators continuations debugger io io.files vocabs tools.time -vocabs.loader source-files compiler.units ; +vocabs.loader source-files compiler.units inspector ; IN: tools.test SYMBOL: failures @@ -30,9 +30,17 @@ SYMBOL: this-test TUPLE: expected-error ; -: unit-test-fails ( quot -- ) - [ f ] append [ [ drop t ] recover ] curry - [ t ] swap unit-test ; +M: expected-error summary + drop + "The unit test expected the quotation to throw an error" ; + +: must-fail-with ( quot test -- ) + >r [ expected-error construct-empty throw ] compose r> + [ recover ] 2curry + [ ] swap unit-test ; + +: must-fail ( quot -- ) + [ drop t ] must-fail-with ; : run-test ( path -- failures ) [ "temporary" forget-vocab ] with-compilation-unit From be2c8b13d742c843ed5cc1d1fe7019808d87d933 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 13:47:19 -0600 Subject: [PATCH 136/269] Rename unit-test-fails to must-fail and add must-fail-with to replace [ t ] [ [ ... ] catch ... ] unit-test idiom --- core/alien/alien-tests.factor | 6 +- core/alien/c-types/c-types-tests.factor | 2 +- core/arrays/arrays-tests.factor | 12 +-- core/bit-arrays/bit-arrays-tests.factor | 2 +- core/byte-arrays/byte-arrays-tests.factor | 2 +- core/classes/classes-tests.factor | 6 +- core/combinators/combinators-tests.factor | 2 +- core/compiler/test/alien.factor | 20 ++--- core/compiler/test/intrinsics.factor | 4 +- core/compiler/test/optimizer.factor | 4 +- core/compiler/test/redefine.factor | 2 +- core/compiler/test/simple.factor | 4 +- core/compiler/test/stack-trace.factor | 8 +- core/continuations/continuations-docs.factor | 12 +-- core/continuations/continuations-tests.factor | 42 +++++----- core/continuations/continuations.factor | 10 +-- core/float-arrays/float-arrays-tests.factor | 2 +- core/generic/generic-tests.factor | 8 +- core/growable/growable-tests.factor | 8 +- core/hashtables/hashtables-tests.factor | 6 +- core/heaps/heaps-tests.factor | 4 +- core/inference/inference-tests.factor | 72 +++++++---------- .../transforms/transforms-tests.factor | 2 +- core/io/streams/duplex/duplex-tests.factor | 4 +- core/kernel/kernel-tests.factor | 46 +++++------ core/listener/listener-tests.factor | 4 +- core/math/integers/integers-tests.factor | 4 +- core/math/parser/parser-tests.factor | 6 +- core/memory/memory-tests.factor | 2 +- core/parser/parser-tests.factor | 78 ++++++++----------- core/quotations/quotations-tests.factor | 2 +- core/sequences/sequences-tests.factor | 20 ++--- core/splitting/splitting-tests.factor | 2 +- core/strings/strings-tests.factor | 11 ++- core/threads/threads-tests.factor | 2 +- core/tuples/tuples-tests.factor | 18 ++--- core/vectors/vectors-tests.factor | 20 ++--- core/vocabs/loader/loader-tests.factor | 14 +--- core/words/words-tests.factor | 8 +- extra/bitfields/bitfields-tests.factor | 12 +-- extra/bootstrap/io/io.factor | 2 - extra/calendar/calendar-tests.factor | 16 ++-- extra/circular/circular-tests.factor | 4 +- extra/combinators/lib/lib-tests.factor | 15 ++-- extra/concurrency/concurrency-docs.factor | 2 +- extra/concurrency/concurrency-tests.factor | 15 ++-- extra/concurrency/concurrency.factor | 2 +- extra/coroutines/coroutines-tests.factor | 2 +- extra/crypto/xor/xor-tests.factor | 8 +- extra/db/postgresql/postgresql-tests.factor | 4 +- extra/db/sqlite/sqlite-tests.factor | 4 +- extra/destructors/destructors-tests.factor | 4 +- extra/help/crossref/crossref-tests.factor | 2 +- extra/inverse/inverse-tests.factor | 8 +- extra/io/buffers/buffers-tests.factor | 2 +- extra/io/mmap/mmap-tests.factor | 4 +- extra/io/unix/launcher/launcher-tests.factor | 8 +- extra/io/unix/linux/linux.factor | 6 +- extra/io/unix/unix-tests.factor | 20 ++--- extra/io/windows/nt/nt.factor | 3 + extra/irc/irc.factor | 2 +- extra/math/complex/complex-tests.factor | 4 +- extra/math/functions/functions-tests.factor | 2 +- extra/memoize/memoize-tests.factor | 2 +- .../multi-methods/multi-methods-tests.factor | 2 +- .../parser-combinators-tests.factor | 2 +- extra/regexp/regexp-tests.factor | 2 +- extra/roman/roman-tests.factor | 6 +- extra/sequences/lib/lib-tests.factor | 2 +- extra/tetris/board/board-tests.factor | 2 +- .../interpreter/interpreter-tests.factor | 2 +- extra/tools/test/inference/inference.factor | 7 +- extra/tools/test/test.factor | 3 + extra/ui/tools/listener/listener-tests.factor | 2 +- extra/xml/test/errors.factor | 2 +- extra/xml/test/test.factor | 2 +- 76 files changed, 299 insertions(+), 369 deletions(-) diff --git a/core/alien/alien-tests.factor b/core/alien/alien-tests.factor index d5133753c1..74c94c8edf 100755 --- a/core/alien/alien-tests.factor +++ b/core/alien/alien-tests.factor @@ -14,7 +14,7 @@ prettyprint ; ! Testing the various bignum accessor 10 "dump" set -[ "dump" get alien-address ] unit-test-fails +[ "dump" get alien-address ] must-fail [ 123 ] [ 123 "dump" get 0 set-alien-signed-1 @@ -61,9 +61,9 @@ cell 8 = [ [ ] [ 0 F{ 1 2 3 } drop ] unit-test [ ] [ 0 ?{ t f t } drop ] unit-test -[ 0 B{ 1 2 3 } alien-address ] unit-test-fails +[ 0 B{ 1 2 3 } alien-address ] must-fail -[ 1 1 ] unit-test-fails +[ 1 1 ] must-fail [ f ] [ 0 B{ 1 2 3 } pinned-c-ptr? ] unit-test diff --git a/core/alien/c-types/c-types-tests.factor b/core/alien/c-types/c-types-tests.factor index 3148b85782..719068e031 100755 --- a/core/alien/c-types/c-types-tests.factor +++ b/core/alien/c-types/c-types-tests.factor @@ -71,4 +71,4 @@ TYPEDEF: uchar* MyLPBYTE [ 0 B{ 1 2 3 4 } -] unit-test-fails +] must-fail diff --git a/core/arrays/arrays-tests.factor b/core/arrays/arrays-tests.factor index 3ff81fda72..e07f192197 100755 --- a/core/arrays/arrays-tests.factor +++ b/core/arrays/arrays-tests.factor @@ -2,10 +2,10 @@ USING: arrays kernel sequences sequences.private growable tools.test vectors layouts system math vectors.private ; IN: temporary -[ -2 { "a" "b" "c" } nth ] unit-test-fails -[ 10 { "a" "b" "c" } nth ] unit-test-fails -[ "hi" -2 { "a" "b" "c" } set-nth ] unit-test-fails -[ "hi" 10 { "a" "b" "c" } set-nth ] unit-test-fails +[ -2 { "a" "b" "c" } nth ] must-fail +[ 10 { "a" "b" "c" } nth ] must-fail +[ "hi" -2 { "a" "b" "c" } set-nth ] must-fail +[ "hi" 10 { "a" "b" "c" } set-nth ] must-fail [ f ] [ { "a" "b" "c" } dup clone eq? ] unit-test [ "hi" ] [ "hi" 1 { "a" "b" "c" } clone [ set-nth ] keep second ] unit-test [ V{ "a" "b" "c" } ] [ { "a" "b" "c" } >vector ] unit-test @@ -17,5 +17,5 @@ IN: temporary [ { "a" "b" "c" "d" "e" } ] [ { "a" } { "b" "c" } { "d" "e" } 3append ] unit-test -[ -1 f ] unit-test-fails -[ cell-bits cell log2 - 2^ f ] unit-test-fails +[ -1 f ] must-fail +[ cell-bits cell log2 - 2^ f ] must-fail diff --git a/core/bit-arrays/bit-arrays-tests.factor b/core/bit-arrays/bit-arrays-tests.factor index f605eba24c..5f89b90608 100755 --- a/core/bit-arrays/bit-arrays-tests.factor +++ b/core/bit-arrays/bit-arrays-tests.factor @@ -51,4 +51,4 @@ IN: temporary [ ?{ t t } ] [ 2 ?{ t t f t f t f t t t f t } resize-bit-array ] unit-test -[ -10 ?{ } resize-bit-array ] unit-test-fails +[ -10 ?{ } resize-bit-array ] must-fail diff --git a/core/byte-arrays/byte-arrays-tests.factor b/core/byte-arrays/byte-arrays-tests.factor index b39551eb86..b5b01c201b 100755 --- a/core/byte-arrays/byte-arrays-tests.factor +++ b/core/byte-arrays/byte-arrays-tests.factor @@ -5,4 +5,4 @@ USING: tools.test byte-arrays ; [ B{ 1 2 } ] [ 2 B{ 1 2 3 4 5 6 7 8 9 } resize-byte-array ] unit-test -[ -10 B{ } resize-byte-array ] unit-test-fails +[ -10 B{ } resize-byte-array ] must-fail diff --git a/core/classes/classes-tests.factor b/core/classes/classes-tests.factor index efff0db5d1..d78436bd5f 100755 --- a/core/classes/classes-tests.factor +++ b/core/classes/classes-tests.factor @@ -91,7 +91,7 @@ M: union-1 generic-update-test drop "union-1" ; [ f ] [ union-1 union-class? ] unit-test [ t ] [ union-1 predicate-class? ] unit-test [ "union-1" ] [ 8 generic-update-test ] unit-test -[ -7 generic-update-test ] unit-test-fails +[ -7 generic-update-test ] must-fail ! Test mixins MIXIN: sequence-mixin @@ -193,7 +193,7 @@ DEFER: mixin-forget-test-g ] unit-test [ { } ] [ { } mixin-forget-test-g ] unit-test -[ H{ } mixin-forget-test-g ] unit-test-fails +[ H{ } mixin-forget-test-g ] must-fail [ ] [ { @@ -207,7 +207,7 @@ DEFER: mixin-forget-test-g parse-stream drop ] unit-test -[ { } mixin-forget-test-g ] unit-test-fails +[ { } mixin-forget-test-g ] must-fail [ H{ } ] [ H{ } mixin-forget-test-g ] unit-test ! Method flattening interfered with mixin update diff --git a/core/combinators/combinators-tests.factor b/core/combinators/combinators-tests.factor index 208f8c0c84..3cefda7f71 100644 --- a/core/combinators/combinators-tests.factor +++ b/core/combinators/combinators-tests.factor @@ -38,7 +38,7 @@ namespaces combinators words ; ! Interpreted [ "two" ] [ 2 \ case-test-1 word-def call ] unit-test -[ "x" case-test-1 ] unit-test-fails +[ "x" case-test-1 ] must-fail : case-test-2 { diff --git a/core/compiler/test/alien.factor b/core/compiler/test/alien.factor index 9416fd1415..dbdbbfc9fa 100755 --- a/core/compiler/test/alien.factor +++ b/core/compiler/test/alien.factor @@ -13,7 +13,7 @@ FUNCTION: int ffi_test_1 ; FUNCTION: int ffi_test_2 int x int y ; [ 5 ] [ 2 3 ffi_test_2 ] unit-test -[ "hi" 3 ffi_test_2 ] unit-test-fails +[ "hi" 3 ffi_test_2 ] must-fail FUNCTION: int ffi_test_3 int x int y int z int t ; [ 25 ] [ 2 3 4 5 ffi_test_3 ] unit-test @@ -26,8 +26,8 @@ FUNCTION: double ffi_test_5 ; FUNCTION: int ffi_test_9 int a int b int c int d int e int f int g ; [ 28 ] [ 1 2 3 4 5 6 7 ffi_test_9 ] unit-test -[ "a" 2 3 4 5 6 7 ffi_test_9 ] unit-test-fails -[ 1 2 3 4 5 6 "a" ffi_test_9 ] unit-test-fails +[ "a" 2 3 4 5 6 7 ffi_test_9 ] must-fail +[ 1 2 3 4 5 6 "a" ffi_test_9 ] must-fail C-STRUCT: foo { "int" "x" } @@ -53,7 +53,7 @@ FUNCTION: char* ffi_test_15 char* x char* y ; [ "foo" ] [ "xy" "zt" ffi_test_15 ] unit-test [ "bar" ] [ "xy" "xy" ffi_test_15 ] unit-test -[ 1 2 ffi_test_15 ] unit-test-fails +[ 1 2 ffi_test_15 ] must-fail C-STRUCT: bar { "long" "x" } @@ -75,7 +75,7 @@ FUNCTION: tiny ffi_test_17 int x ; [ 11 ] [ 11 ffi_test_17 tiny-x ] unit-test -[ t ] [ [ [ alien-indirect ] infer ] catch inference-error? ] unit-test +[ [ alien-indirect ] infer ] [ inference-error? ] must-fail-with : indirect-test-1 "int" { } "cdecl" alien-indirect ; @@ -84,7 +84,7 @@ FUNCTION: tiny ffi_test_17 int x ; [ 3 ] [ "ffi_test_1" f dlsym indirect-test-1 ] unit-test -[ -1 indirect-test-1 ] unit-test-fails +[ -1 indirect-test-1 ] must-fail : indirect-test-2 "int" { "int" "int" } "cdecl" alien-indirect data-gc ; @@ -120,7 +120,7 @@ unit-test FUNCTION: double ffi_test_6 float x float y ; [ 6.0 ] [ 3.0 2.0 ffi_test_6 ] unit-test -[ "a" "b" ffi_test_6 ] unit-test-fails +[ "a" "b" ffi_test_6 ] must-fail FUNCTION: double ffi_test_7 double x double y ; [ 6.0 ] [ 3.0 2.0 ffi_test_7 ] unit-test @@ -157,7 +157,7 @@ FUNCTION: long ffi_test_22 long x longlong y longlong z ; [ 987655432 ] [ 1111 121932631112635269 123456789 ffi_test_22 ] unit-test -[ 1111 f 123456789 ffi_test_22 ] unit-test-fails +[ 1111 f 123456789 ffi_test_22 ] must-fail C-STRUCT: rect { "float" "x" } @@ -177,7 +177,7 @@ FUNCTION: int ffi_test_12 int a int b rect c int d int e int f ; [ 45 ] [ 1 2 3.0 4.0 5.0 6.0 7 8 9 ffi_test_12 ] unit-test -[ 1 2 { 1 2 3 } 7 8 9 ffi_test_12 ] unit-test-fails +[ 1 2 { 1 2 3 } 7 8 9 ffi_test_12 ] must-fail FUNCTION: float ffi_test_23 ( float[3] x, float[3] y ) ; @@ -292,7 +292,7 @@ FUNCTION: double ffi_test_36 ( test-struct-12 x ) ; [ ] [ callback-1 callback_test_1 ] unit-test -: callback-2 "void" { } "cdecl" [ [ 5 throw ] catch drop ] alien-callback ; +: callback-2 "void" { } "cdecl" [ [ 5 throw ] ignore-errors ] alien-callback ; [ ] [ callback-2 callback_test_1 ] unit-test diff --git a/core/compiler/test/intrinsics.factor b/core/compiler/test/intrinsics.factor index 1d0ad141c2..679938b7f3 100755 --- a/core/compiler/test/intrinsics.factor +++ b/core/compiler/test/intrinsics.factor @@ -422,11 +422,11 @@ cell 8 = [ [ B{ 0 0 0 0 } [ { byte-array } declare ] compile-call -] unit-test-fails +] must-fail [ B{ 0 0 0 0 } [ { c-ptr } declare ] compile-call -] unit-test-fails +] must-fail [ 4 5 diff --git a/core/compiler/test/optimizer.factor b/core/compiler/test/optimizer.factor index b59c0d5f33..091648cbbc 100755 --- a/core/compiler/test/optimizer.factor +++ b/core/compiler/test/optimizer.factor @@ -136,7 +136,7 @@ TUPLE: pred-test ; GENERIC: void-generic ( obj -- * ) : breakage "hi" void-generic ; [ t ] [ \ breakage compiled? ] unit-test -[ breakage ] unit-test-fails +[ breakage ] must-fail ! regression : test-0 ( n -- ) dup 0 = [ drop ] [ 1- test-0 ] if ; inline @@ -247,7 +247,7 @@ M: slice foozul ; GENERIC: detect-number ( obj -- obj ) M: number detect-number ; -[ 10 f [ 0 + detect-number ] compile-call ] unit-test-fails +[ 10 f [ 0 + detect-number ] compile-call ] must-fail ! Regression [ 4 [ + ] ] [ 2 2 [ [ + ] [ call ] keep ] compile-call ] unit-test diff --git a/core/compiler/test/redefine.factor b/core/compiler/test/redefine.factor index 5d07e764d6..e9927f4964 100755 --- a/core/compiler/test/redefine.factor +++ b/core/compiler/test/redefine.factor @@ -243,7 +243,7 @@ DEFER: defer-redefine-test-2 [ ] [ "IN: temporary : defer-redefine-test-2 defer-redefine-test-1 1 ;" eval ] unit-test -[ defer-redefine-test-2 ] unit-test-fails +[ defer-redefine-test-2 ] must-fail [ ] [ "IN: temporary : defer-redefine-test-1 2 ;" eval ] unit-test diff --git a/core/compiler/test/simple.factor b/core/compiler/test/simple.factor index 9f831bb1f8..6f5cb33c1a 100755 --- a/core/compiler/test/simple.factor +++ b/core/compiler/test/simple.factor @@ -57,8 +57,8 @@ IN: temporary ! Make sure error reporting works -[ [ dup ] compile-call ] unit-test-fails -[ [ drop ] compile-call ] unit-test-fails +[ [ dup ] compile-call ] must-fail +[ [ drop ] compile-call ] must-fail ! Regression diff --git a/core/compiler/test/stack-trace.factor b/core/compiler/test/stack-trace.factor index 59ee3c3d88..71c95b1b61 100755 --- a/core/compiler/test/stack-trace.factor +++ b/core/compiler/test/stack-trace.factor @@ -10,7 +10,7 @@ words splitting ; : foo 3 throw 7 ; : bar foo 4 ; : baz bar 5 ; -[ 3 ] [ [ baz ] catch ] unit-test +[ baz ] [ 3 = ] must-fail-with [ t ] [ symbolic-stack-trace [ word? ] subset @@ -22,11 +22,11 @@ words splitting ; : stack-trace-contains? symbolic-stack-trace memq? ; [ t ] [ - [ { 1 "hi" } bleh ] catch drop \ + stack-trace-contains? + [ { 1 "hi" } bleh ] ignore-errors \ + stack-trace-contains? ] unit-test [ t f ] [ - [ { "hi" } bleh ] catch drop + [ { "hi" } bleh ] ignore-errors \ + stack-trace-contains? \ > stack-trace-contains? ] unit-test @@ -34,6 +34,6 @@ words splitting ; : quux [ t [ "hi" throw ] when ] times ; [ t ] [ - [ 10 quux ] catch drop + [ 10 quux ] ignore-errors \ (each-integer) stack-trace-contains? ] unit-test diff --git a/core/continuations/continuations-docs.factor b/core/continuations/continuations-docs.factor index 51e461c715..2977d02c6f 100755 --- a/core/continuations/continuations-docs.factor +++ b/core/continuations/continuations-docs.factor @@ -23,10 +23,9 @@ $nl "Two words raise an error in the innermost error handler for the current dynamic extent:" { $subsection throw } { $subsection rethrow } -"A set of words establish an error handler:" +"Two words for establishing an error handler:" { $subsection cleanup } { $subsection recover } -{ $subsection catch } "Unhandled errors are reported in the listener and can be debugged using various tools. See " { $link "debugger" } "." { $subsection "errors-restartable" } { $subsection "errors-post-mortem" } ; @@ -147,12 +146,7 @@ HELP: throw { $values { "error" object } } { $description "Saves the current continuation in the " { $link error-continuation } " global variable and throws an error. Execution does not continue at the point after the " { $link throw } " call. Rather, the innermost catch block is invoked, and execution continues at that point." } ; -HELP: catch -{ $values { "try" quotation } { "error/f" object } } -{ $description "Calls the " { $snippet "try" } " quotation. If an error is thrown in the dynamic extent of the quotation, restores the data stack and pushes the error. If the quotation returns successfully, outputs " { $link f } " without restoring the data stack." } -{ $notes "This word cannot differentiate between the case of " { $link f } " being thrown, and no error being thrown. You should never throw " { $link f } ", and you should also use other error handling combinators where possible." } ; - -{ catch cleanup recover } related-words +{ cleanup recover } related-words HELP: cleanup { $values { "try" quotation } { "cleanup-always" quotation } { "cleanup-error" quotation } } @@ -166,7 +160,7 @@ HELP: rethrow { $values { "error" object } } { $description "Throws an error without saving the current continuation in the " { $link error-continuation } " global variable. This is done so that inspecting the error stacks sheds light on the original cause of the exception, rather than the point where it was rethrown." } { $notes - "This word is intended to be used in conjunction with " { $link recover } " or " { $link catch } " to implement error handlers which perform an action and pass the error to the next outermost error handler." + "This word is intended to be used in conjunction with " { $link recover } " to implement error handlers which perform an action and pass the error to the next outermost error handler." } { $examples "The " { $link with-parser } " catches errors, annotates them with file name and line number information, and rethrows them:" diff --git a/core/continuations/continuations-tests.factor b/core/continuations/continuations-tests.factor index 360f4750c9..b7d580afe5 100755 --- a/core/continuations/continuations-tests.factor +++ b/core/continuations/continuations-tests.factor @@ -25,13 +25,11 @@ IN: temporary [ t ] [ 10 callcc1-test 10 reverse >vector = ] unit-test [ t ] [ callcc-namespace-test ] unit-test -[ f ] [ [ ] catch ] unit-test - -[ 5 ] [ [ 5 throw ] catch ] unit-test +[ 5 throw ] [ 5 = ] must-fail-with [ t ] [ - [ "Hello" throw ] catch drop - global [ error get ] bind + [ "Hello" throw ] ignore-errors + error get-global "Hello" = ] unit-test @@ -41,13 +39,13 @@ IN: temporary "!!! The following error is part of the test" print -[ [ "2 car" ] eval ] catch print-error +[ ] [ [ [ "2 car" ] eval ] [ print-error ] recover ] unit-test -[ f throw ] unit-test-fails +[ f throw ] must-fail ! Weird PowerPC bug. [ ] [ - [ "4" throw ] catch drop + [ "4" throw ] ignore-errors data-gc data-gc ] unit-test @@ -56,10 +54,10 @@ IN: temporary [ f ] [ { "A" "B" } kernel-error? ] unit-test ! ! See how well callstack overflow is handled -! [ clear drop ] unit-test-fails +! [ clear drop ] must-fail ! ! : callstack-overflow callstack-overflow f ; -! [ callstack-overflow ] unit-test-fails +! [ callstack-overflow ] must-fail : don't-compile-me { } [ ] each ; @@ -84,24 +82,20 @@ SYMBOL: error-counter [ 1 ] [ always-counter get ] unit-test [ 0 ] [ error-counter get ] unit-test - [ "a" ] [ - [ - [ "a" throw ] - [ always-counter inc ] - [ error-counter inc ] cleanup - ] catch - ] unit-test + [ + [ "a" throw ] + [ always-counter inc ] + [ error-counter inc ] cleanup + ] [ "a" = ] must-fail-with [ 2 ] [ always-counter get ] unit-test [ 1 ] [ error-counter get ] unit-test - [ "a" ] [ - [ - [ ] - [ always-counter inc "a" throw ] - [ error-counter inc ] cleanup - ] catch - ] unit-test + [ + [ ] + [ always-counter inc "a" throw ] + [ error-counter inc ] cleanup + ] [ "a" = ] must-fail-with [ 3 ] [ always-counter get ] unit-test [ 1 ] [ error-counter get ] unit-test diff --git a/core/continuations/continuations.factor b/core/continuations/continuations.factor index 6e4ce16bea..b6ca056691 100755 --- a/core/continuations/continuations.factor +++ b/core/continuations/continuations.factor @@ -1,4 +1,4 @@ -! Copyright (C) 2003, 2007 Slava Pestov. +! Copyright (C) 2003, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: arrays vectors kernel kernel.private sequences namespaces math splitting sorting quotations assocs ; @@ -17,9 +17,6 @@ SYMBOL: restarts : c> ( -- continuation ) catchstack* pop ; -: (catch) ( quot -- newquot ) - [ swap >c call c> drop ] curry ; inline - : dummy ( -- obj ) #! Optimizing compiler assumes stack won't be messed with #! in-transit. To ensure that a value is actually reified @@ -120,11 +117,8 @@ PRIVATE> catchstack* empty? [ die ] when dup save-error c> continue-with ; -: catch ( try -- error/f ) - (catch) [ f ] compose callcc1 ; inline - : recover ( try recovery -- ) - >r (catch) r> ifcc ; inline + >r [ swap >c call c> drop ] curry r> ifcc ; inline : cleanup ( try cleanup-always cleanup-error -- ) over >r compose [ dip rethrow ] curry diff --git a/core/float-arrays/float-arrays-tests.factor b/core/float-arrays/float-arrays-tests.factor index afadaac0db..0e0ab3feb6 100755 --- a/core/float-arrays/float-arrays-tests.factor +++ b/core/float-arrays/float-arrays-tests.factor @@ -7,4 +7,4 @@ USING: float-arrays tools.test ; [ F{ 1 2 } ] [ 2 F{ 1 2 3 4 5 6 7 8 9 } resize-float-array ] unit-test -[ -10 F{ } resize-float-array ] unit-test-fails +[ -10 F{ } resize-float-array ] must-fail diff --git a/core/generic/generic-tests.factor b/core/generic/generic-tests.factor index 4de05aafd0..e4d4160605 100755 --- a/core/generic/generic-tests.factor +++ b/core/generic/generic-tests.factor @@ -16,7 +16,7 @@ M: word class-of drop "word" ; [ "fixnum" ] [ 5 class-of ] unit-test [ "word" ] [ \ class-of class-of ] unit-test -[ 3.4 class-of ] unit-test-fails +[ 3.4 class-of ] must-fail [ "Hello world" ] [ 4 foobar foobar ] unit-test [ "Goodbye cruel world" ] [ 4 foobar ] unit-test @@ -90,7 +90,7 @@ M: number union-containment drop 2 ; "IN: temporary GENERIC: unhappy ( x -- x )" eval [ "IN: temporary M: dictionary unhappy ;" eval -] unit-test-fails +] must-fail [ ] [ "IN: temporary GENERIC: unhappy ( x -- x )" eval ] unit-test GENERIC# complex-combination 1 ( a b -- c ) @@ -155,9 +155,7 @@ M: string my-hook "a string" ; [ "an integer" ] [ 3 my-var set my-hook ] unit-test [ "a string" ] [ my-hook my-var set my-hook ] unit-test -[ T{ no-method f 1.0 my-hook } ] [ - 1.0 my-var set [ my-hook ] catch -] unit-test +[ 1.0 my-var set my-hook ] [ [ T{ no-method f 1.0 my-hook } = ] must-fail-with GENERIC: tag-and-f ( x -- x x ) diff --git a/core/growable/growable-tests.factor b/core/growable/growable-tests.factor index 39d8721726..a220ccc45e 100755 --- a/core/growable/growable-tests.factor +++ b/core/growable/growable-tests.factor @@ -9,16 +9,16 @@ IN: temporary ! overflow bugs [ "hi" most-positive-fixnum 2 * 2 + V{ } clone set-nth ] -unit-test-fails +must-fail [ most-positive-fixnum 2 * 2 + { 1 } clone nth ] -unit-test-fails +must-fail [ most-positive-fixnum 2 * 2 + V{ } clone lengthen ] -unit-test-fails +must-fail [ most-positive-fixnum 2 * 2 + V{ } clone set-length ] -unit-test-fails +must-fail [ ] [ 10 V{ } [ set-length ] keep diff --git a/core/hashtables/hashtables-tests.factor b/core/hashtables/hashtables-tests.factor index 40d079402c..acb05be720 100755 --- a/core/hashtables/hashtables-tests.factor +++ b/core/hashtables/hashtables-tests.factor @@ -127,9 +127,9 @@ H{ } "x" set ! Another crash discovered by erg [ ] [ H{ } clone - [ 1 swap set-at ] catch drop - [ 2 swap set-at ] catch drop - [ 3 swap set-at ] catch drop + [ 1 swap set-at ] ignore-errors + [ 2 swap set-at ] ignore-errors + [ 3 swap set-at ] ignore-errors drop ] unit-test diff --git a/core/heaps/heaps-tests.factor b/core/heaps/heaps-tests.factor index de661fad92..92b06b866c 100644 --- a/core/heaps/heaps-tests.factor +++ b/core/heaps/heaps-tests.factor @@ -5,8 +5,8 @@ USING: arrays kernel math namespaces tools.test heaps heaps.private ; IN: temporary -[ heap-pop ] unit-test-fails -[ heap-pop ] unit-test-fails +[ heap-pop ] must-fail +[ heap-pop ] must-fail [ t ] [ heap-empty? ] unit-test [ f ] [ 1 t pick heap-push heap-empty? ] unit-test diff --git a/core/inference/inference-tests.factor b/core/inference/inference-tests.factor index 3e3858d45d..1738a71b7e 100755 --- a/core/inference/inference-tests.factor +++ b/core/inference/inference-tests.factor @@ -12,14 +12,14 @@ IN: temporary { 1 2 } [ dup ] unit-test-effect { 1 2 } [ [ dup ] call ] unit-test-effect -[ [ call ] infer ] unit-test-fails +[ [ call ] infer ] must-fail { 2 4 } [ 2dup ] unit-test-effect { 1 0 } [ [ ] [ ] if ] unit-test-effect -[ [ if ] infer ] unit-test-fails -[ [ [ ] if ] infer ] unit-test-fails -[ [ [ 2 ] [ ] if ] infer ] unit-test-fails +[ [ if ] infer ] must-fail +[ [ [ ] if ] infer ] must-fail +[ [ [ 2 ] [ ] if ] infer ] must-fail { 4 3 } [ [ rot ] [ -rot ] if ] unit-test-effect { 4 3 } [ @@ -42,7 +42,7 @@ IN: temporary [ [ [ [ 2 2 fixnum+ ] ] [ [ 2 2 fixnum* ] ] if call ] infer -] unit-test-fails +] must-fail ! Test inference of termination of control flow : termination-test-1 @@ -54,10 +54,10 @@ IN: temporary : infinite-loop infinite-loop ; -[ [ infinite-loop ] infer ] unit-test-fails +[ [ infinite-loop ] infer ] must-fail : no-base-case-1 dup [ no-base-case-1 ] [ no-base-case-1 ] if ; -[ [ no-base-case-1 ] infer ] unit-test-fails +[ [ no-base-case-1 ] infer ] must-fail : simple-recursion-1 ( obj -- obj ) dup [ simple-recursion-1 ] [ ] if ; @@ -72,7 +72,7 @@ IN: temporary : bad-recursion-2 ( obj -- obj ) dup [ dup first swap second bad-recursion-2 ] [ ] if ; -[ [ bad-recursion-2 ] infer ] unit-test-fails +[ [ bad-recursion-2 ] infer ] must-fail : funny-recursion ( obj -- obj ) dup [ funny-recursion 1 ] [ 2 ] if drop ; @@ -192,7 +192,7 @@ DEFER: blah4 [ swap slip ] keep swap bad-combinator ] if ; inline -[ [ [ 1 ] [ ] bad-combinator ] infer ] unit-test-fails +[ [ [ 1 ] [ ] bad-combinator ] infer ] must-fail ! Regression : bad-input# @@ -207,13 +207,13 @@ DEFER: blah4 DEFER: do-crap : more-crap ( obj -- ) dup [ drop ] [ dup do-crap call ] if ; : do-crap ( obj -- ) dup [ more-crap ] [ do-crap ] if ; -[ [ do-crap ] infer ] unit-test-fails +[ [ do-crap ] infer ] must-fail ! This one does not DEFER: do-crap* : more-crap* ( obj -- ) dup [ drop ] [ dup do-crap* call ] if ; : do-crap* ( obj -- ) dup [ do-crap* ] [ more-crap* ] if ; -[ [ do-crap* ] infer ] unit-test-fails +[ [ do-crap* ] infer ] must-fail ! Regression : too-deep ( a b -- c ) @@ -226,7 +226,7 @@ M: fixnum xyz 2array ; M: float xyz [ 3 ] 2apply swapd >r 2array swap r> 2array swap ; -[ t ] [ [ [ xyz ] infer ] catch inference-error? ] unit-test +[ [ xyz ] infer ] [ inference-error? ] must-fail-with ! Doug Coleman discovered this one while working on the ! calendar library @@ -277,78 +277,66 @@ DEFER: #1 : #4 ( a -- ) dup [ drop ] [ dup #4 dup #3 call ] if ; : #1 ( a -- ) dup [ dup #4 dup #3 ] [ ] if drop ; -[ \ #4 word-def infer ] unit-test-fails -[ [ #1 ] infer ] unit-test-fails +[ \ #4 word-def infer ] must-fail +[ [ #1 ] infer ] must-fail ! Similar DEFER: bar : foo ( a b -- c d ) dup [ 2drop f f bar ] [ ] if ; : bar ( a b -- ) [ 2 2 + ] t foo drop call drop ; -[ [ foo ] infer ] unit-test-fails +[ [ foo ] infer ] must-fail -[ 1234 infer ] unit-test-fails +[ 1234 infer ] must-fail ! This used to hang -[ t ] [ - [ [ [ dup call ] dup call ] infer ] catch - inference-error? -] unit-test +[ [ [ dup call ] dup call ] infer ] +[ inference-error? ] must-fail-with : m dup call ; inline -[ t ] [ - [ [ [ m ] m ] infer ] catch inference-error? -] unit-test +[ [ [ m ] m ] infer ] [ inference-error? ] must-fail-with : m' dup curry call ; inline -[ t ] [ - [ [ [ m' ] m' ] infer ] catch inference-error? -] unit-test +[ [ [ m' ] m' ] infer ] [ inference-error? ] must-fail-with : m'' [ dup curry ] ; inline : m''' m'' call call ; inline -[ t ] [ - [ [ [ m''' ] m''' ] infer ] catch inference-error? -] unit-test +[ [ [ m''' ] m''' ] infer ] [ inference-error? ] must-fail-with : m-if t over if ; inline -[ t ] [ - [ [ [ m-if ] m-if ] infer ] catch inference-error? -] unit-test +[ [ [ m-if ] m-if ] infer ] [ inference-error? ] must-fail-with ! This doesn't hang but it's also an example of the ! undedicable case -[ t ] [ - [ [ [ [ drop 3 ] swap call ] dup call ] infer ] catch - inference-error? -] unit-test +[ [ [ [ drop 3 ] swap call ] dup call ] infer ] +[ inference-error? ] must-fail-with ! This form should not have a stack effect : bad-recursion-1 ( a -- b ) dup [ drop bad-recursion-1 5 ] [ ] if ; -[ [ bad-recursion-1 ] infer ] unit-test-fails +[ [ bad-recursion-1 ] infer ] must-fail : bad-bin ( a b -- ) 5 [ 5 bad-bin bad-bin 5 ] [ 2drop ] if ; -[ [ bad-bin ] infer ] unit-test-fails +[ [ bad-bin ] infer ] must-fail -[ t ] [ [ [ r> ] infer ] catch inference-error? ] unit-test +[ [ [ r> ] infer ] [ inference-error? ] must-fail-with ! Regression -[ t ] [ [ [ get-slots ] infer ] catch inference-error? ] unit-test +[ [ [ get-slots ] infer ] [ inference-error? ] must-fail-with ! Test some curry stuff { 1 1 } [ 3 [ ] curry 4 [ ] curry if ] unit-test-effect { 2 1 } [ [ ] curry 4 [ ] curry if ] unit-test-effect -[ [ 3 [ ] curry 1 2 [ ] 2curry if ] infer ] unit-test-fails +[ [ 3 [ ] curry 1 2 [ ] 2curry if ] infer ] must-fail ! Test number protocol \ bitor must-infer @@ -459,7 +447,7 @@ DEFER: bar : fooxxx ( a b -- c ) over [ foo ] when ; inline : barxxx fooxxx ; -[ [ barxxx ] infer ] unit-test-fails +[ [ barxxx ] infer ] must-fail ! A typo { 1 0 } [ { [ ] } dispatch ] unit-test-effect diff --git a/core/inference/transforms/transforms-tests.factor b/core/inference/transforms/transforms-tests.factor index 152da8c757..f58e557b10 100755 --- a/core/inference/transforms/transforms-tests.factor +++ b/core/inference/transforms/transforms-tests.factor @@ -31,4 +31,4 @@ TUPLE: a-tuple x y z ; : set-slots-test-2 { set-a-tuple-x set-a-tuple-x } set-slots ; -[ [ set-slots-test-2 ] infer ] unit-test-fails +[ [ set-slots-test-2 ] infer ] must-fail diff --git a/core/io/streams/duplex/duplex-tests.factor b/core/io/streams/duplex/duplex-tests.factor index 962a46413f..44542e05ce 100755 --- a/core/io/streams/duplex/duplex-tests.factor +++ b/core/io/streams/duplex/duplex-tests.factor @@ -28,13 +28,13 @@ M: unclosable-stream dispose [ t ] [ [ - [ dup dispose ] catch 2drop + [ dup dispose ] [ 2drop ] recover ] keep closing-stream-closed? ] unit-test [ t ] [ [ - [ dup dispose ] catch 2drop + [ dup dispose ] [ 2drop ] recover ] keep closing-stream-closed? ] unit-test diff --git a/core/kernel/kernel-tests.factor b/core/kernel/kernel-tests.factor index c294c23738..e37b208ef0 100755 --- a/core/kernel/kernel-tests.factor +++ b/core/kernel/kernel-tests.factor @@ -7,25 +7,22 @@ IN: temporary [ t ] [ [ \ = \ = ] all-equal? ] unit-test ! Don't leak extra roots if error is thrown -[ ] [ 10000 [ [ 3 throw ] catch drop ] times ] unit-test +[ ] [ 10000 [ [ 3 throw ] ignore-errors ] times ] unit-test -[ ] [ 10000 [ [ -1 f ] catch drop ] times ] unit-test +[ ] [ 10000 [ [ -1 f ] ignore-errors ] times ] unit-test ! Make sure we report the correct error on stack underflow -[ { "kernel-error" 11 f f } ] -[ [ clear drop ] catch ] unit-test +[ clear drop ] [ { "kernel-error" 11 f f } = ] must-fail-with [ ] [ :c ] unit-test -[ { "kernel-error" 13 f f } ] -[ [ { } set-retainstack r> ] catch ] unit-test +[ { } set-retainstack r> ] [ { "kernel-error" 13 f f } = ] must-fail-with [ ] [ :c ] unit-test : overflow-d 3 overflow-d ; -[ { "kernel-error" 12 f f } ] -[ [ overflow-d ] catch ] unit-test +[ overflow-d ] [ { "kernel-error" 12 f f } = ] must-fail-with [ ] [ :c ] unit-test @@ -33,24 +30,17 @@ IN: temporary : overflow-d-alt (overflow-d-alt) overflow-d-alt ; -[ { "kernel-error" 12 f f } ] -[ [ overflow-d-alt ] catch ] unit-test +[ overflow-d-alt ] [ { "kernel-error" 12 f f } = ] must-fail-with [ ] [ [ :c ] string-out drop ] unit-test : overflow-r 3 >r overflow-r ; -[ { "kernel-error" 14 f f } ] -[ [ overflow-r ] catch ] unit-test +[ overflow-r ] [ { "kernel-error" 14 f f } = ] must-fail-with [ ] [ :c ] unit-test -! : overflow-c overflow-c 3 ; -! -! [ { "kernel-error" 16 f f } ] -! [ [ overflow-c ] catch ] unit-test - -[ -7 ] unit-test-fails +[ -7 ] must-fail [ 2 3 4 1 ] [ 1 2 3 4 roll ] unit-test [ 1 2 3 4 ] [ 2 3 4 1 -roll ] unit-test @@ -61,27 +51,27 @@ IN: temporary [ 4 ] [ 4 6 or ] unit-test [ 6 ] [ f 6 or ] unit-test -[ slip ] unit-test-fails +[ slip ] must-fail [ ] [ :c ] unit-test -[ 1 slip ] unit-test-fails +[ 1 slip ] must-fail [ ] [ :c ] unit-test -[ 1 2 slip ] unit-test-fails +[ 1 2 slip ] must-fail [ ] [ :c ] unit-test -[ 1 2 3 slip ] unit-test-fails +[ 1 2 3 slip ] must-fail [ ] [ :c ] unit-test [ 5 ] [ [ 2 2 + ] 1 slip + ] unit-test -[ [ ] keep ] unit-test-fails +[ [ ] keep ] must-fail [ 6 ] [ 2 [ sq ] keep + ] unit-test -[ [ ] 2keep ] unit-test-fails -[ 1 [ ] 2keep ] unit-test-fails +[ [ ] 2keep ] must-fail +[ 1 [ ] 2keep ] must-fail [ 3 1 2 ] [ 1 2 [ 2drop 3 ] 2keep ] unit-test [ 0 ] [ f [ sq ] [ 0 ] if* ] unit-test @@ -100,13 +90,13 @@ IN: temporary [ ] [ callstack set-callstack ] unit-test -[ 3drop datastack ] unit-test-fails +[ 3drop datastack ] must-fail [ ] [ :c ] unit-test ! Doesn't compile; important : foo 5 + 0 [ ] each ; -[ drop foo ] unit-test-fails +[ drop foo ] must-fail [ ] [ :c ] unit-test ! Regression @@ -117,4 +107,4 @@ IN: temporary : loop ( obj obj -- ) H{ } values swap >r dup length swap r> 0 -roll (loop) ; -[ loop ] unit-test-fails +[ loop ] must-fail diff --git a/core/listener/listener-tests.factor b/core/listener/listener-tests.factor index 626c2b3e06..4570b1162a 100755 --- a/core/listener/listener-tests.factor +++ b/core/listener/listener-tests.factor @@ -22,7 +22,7 @@ IN: temporary [ "\\ + 1 2 3 4" parse-interactive "cont" get continue-with - ] catch + ] ignore-errors "USE: debugger :1" eval ] callcc1 ] unit-test @@ -36,7 +36,7 @@ IN: temporary [ "USE: vocabs.loader.test.c" parse-interactive -] unit-test-fails +] must-fail [ ] [ [ diff --git a/core/math/integers/integers-tests.factor b/core/math/integers/integers-tests.factor index 680119a56e..194edb8f7e 100755 --- a/core/math/integers/integers-tests.factor +++ b/core/math/integers/integers-tests.factor @@ -121,8 +121,8 @@ unit-test ! We don't care if this fails or returns 0 (its CPU-specific) ! as long as it doesn't crash -[ ] [ [ 0 0 /i ] catch clear ] unit-test -[ ] [ [ 100000000000000000 0 /i ] catch clear ] unit-test +[ ] [ [ 0 0 /i drop ] ignore-errors ] unit-test +[ ] [ [ 100000000000000000 0 /i drop ] ignore-errors ] unit-test [ -2 ] [ 1 bitnot ] unit-test [ -2 ] [ 1 >bignum bitnot ] unit-test diff --git a/core/math/parser/parser-tests.factor b/core/math/parser/parser-tests.factor index 62893e2618..7c30012a19 100755 --- a/core/math/parser/parser-tests.factor +++ b/core/math/parser/parser-tests.factor @@ -105,6 +105,6 @@ unit-test ! [ dup number>string string>number = ] all? ! ] unit-test -[ 1 1 >base ] unit-test-fails -[ 1 0 >base ] unit-test-fails -[ 1 -1 >base ] unit-test-fails +[ 1 1 >base ] must-fail +[ 1 0 >base ] must-fail +[ 1 -1 >base ] must-fail diff --git a/core/memory/memory-tests.factor b/core/memory/memory-tests.factor index f543c08744..d0dfd2c0be 100755 --- a/core/memory/memory-tests.factor +++ b/core/memory/memory-tests.factor @@ -4,7 +4,7 @@ IN: temporary TUPLE: testing x y z ; -[ save-image-and-exit ] unit-test-fails +[ save-image-and-exit ] must-fail [ ] [ num-types get [ diff --git a/core/parser/parser-tests.factor b/core/parser/parser-tests.factor index f503528a24..eb04e329d9 100755 --- a/core/parser/parser-tests.factor +++ b/core/parser/parser-tests.factor @@ -93,12 +93,12 @@ IN: temporary ! Funny bug [ 2 ] [ "IN: temporary : \0. 2 ; \0." eval ] unit-test - [ "IN: temporary : missing-- ( a b ) ;" eval ] unit-test-fails + [ "IN: temporary : missing-- ( a b ) ;" eval ] must-fail ! These should throw errors - [ "HEX: zzz" eval ] unit-test-fails - [ "OCT: 999" eval ] unit-test-fails - [ "BIN: --0" eval ] unit-test-fails + [ "HEX: zzz" eval ] must-fail + [ "OCT: 999" eval ] must-fail + [ "BIN: --0" eval ] must-fail ! Another funny bug [ t ] [ @@ -205,12 +205,10 @@ IN: temporary "a" source-files get delete-at - [ t ] [ - [ - "IN: temporary : x ; : y 3 throw ; this is an error" - "a" parse-stream - ] catch parse-error? - ] unit-test + [ + "IN: temporary : x ; : y 3 throw ; this is an error" + "a" parse-stream + ] [ parse-error? ] must-fail-with [ t ] [ "y" "temporary" lookup >boolean @@ -307,62 +305,50 @@ IN: temporary "killer?" "temporary" lookup >boolean ] unit-test - [ t ] [ - [ - "IN: temporary TUPLE: another-pred-test ; GENERIC: another-pred-test?" - "removing-the-predicate" parse-stream - ] catch [ redefine-error? ] is? - ] unit-test + [ + "IN: temporary TUPLE: another-pred-test ; GENERIC: another-pred-test?" + "removing-the-predicate" parse-stream + ] [ [ redefine-error? ] is? ] must-fail-with - [ t ] [ - [ - "IN: temporary TUPLE: class-redef-test ; TUPLE: class-redef-test ;" - "redefining-a-class-1" parse-stream - ] catch [ redefine-error? ] is? - ] unit-test + [ + "IN: temporary TUPLE: class-redef-test ; TUPLE: class-redef-test ;" + "redefining-a-class-1" parse-stream + ] [ [ redefine-error? ] is? ] must-fail-with [ ] [ "IN: temporary TUPLE: class-redef-test ; SYMBOL: class-redef-test" "redefining-a-class-2" parse-stream drop ] unit-test - [ t ] [ - [ - "IN: temporary TUPLE: class-redef-test ; SYMBOL: class-redef-test : class-redef-test ;" - "redefining-a-class-3" parse-stream drop - ] catch [ redefine-error? ] is? - ] unit-test + [ + "IN: temporary TUPLE: class-redef-test ; SYMBOL: class-redef-test : class-redef-test ;" + "redefining-a-class-3" parse-stream drop + ] [ [ redefine-error? ] is? ] must-fail-with [ ] [ "IN: temporary TUPLE: class-fwd-test ;" "redefining-a-class-3" parse-stream drop ] unit-test - [ t ] [ - [ - "IN: temporary \\ class-fwd-test" - "redefining-a-class-3" parse-stream drop - ] catch [ no-word? ] is? - ] unit-test + [ + "IN: temporary \\ class-fwd-test" + "redefining-a-class-3" parse-stream drop + ] [ [ no-word? ] is? ] must-fail-with [ ] [ "IN: temporary TUPLE: class-fwd-test ; SYMBOL: class-fwd-test" "redefining-a-class-3" parse-stream drop ] unit-test - [ t ] [ - [ - "IN: temporary \\ class-fwd-test" - "redefining-a-class-3" parse-stream drop - ] catch [ no-word? ] is? - ] unit-test + [ + "IN: temporary \\ class-fwd-test" + "redefining-a-class-3" parse-stream drop + ] [ [ no-word? ] is? ] must-fail-with - [ t ] [ - [ - "IN: temporary : foo ; TUPLE: foo ;" - "redefining-a-class-4" parse-stream drop - ] catch [ redefine-error? ] is? - ] unit-test + [ + "IN: temporary : foo ; TUPLE: foo ;" + "redefining-a-class-4" parse-stream drop + ] [ [ redefine-error? ] is? ] must-fail-with ] with-file-vocabs [ diff --git a/core/quotations/quotations-tests.factor b/core/quotations/quotations-tests.factor index f1cc6cd828..d357fb70ff 100644 --- a/core/quotations/quotations-tests.factor +++ b/core/quotations/quotations-tests.factor @@ -15,4 +15,4 @@ IN: temporary [ [ "hi" ] ] [ "hi" 1quotation ] unit-test -[ 1 \ + curry ] unit-test-fails +[ 1 \ + curry ] must-fail diff --git a/core/sequences/sequences-tests.factor b/core/sequences/sequences-tests.factor index 73ae4737ba..40b2fef85e 100755 --- a/core/sequences/sequences-tests.factor +++ b/core/sequences/sequences-tests.factor @@ -83,8 +83,8 @@ unit-test [ [ 1 2 3 4 ] ] [ [ 1 2 3 ] [ 4 ] append ] unit-test [ [ 1 2 3 4 ] ] [ [ 1 2 3 ] { 4 } append ] unit-test -[ "a" -1 append ] unit-test-fails -[ -1 "a" append ] unit-test-fails +[ "a" -1 append ] must-fail +[ -1 "a" append ] must-fail [ [ ] ] [ 1 [ ] remove ] unit-test [ [ ] ] [ 1 [ 1 ] remove ] unit-test @@ -119,7 +119,7 @@ unit-test [ V{ 0 1 4 5 } ] [ 6 >vector 2 4 pick delete-slice ] unit-test -[ 6 >vector 2 8 pick delete-slice ] unit-test-fails +[ 6 >vector 2 8 pick delete-slice ] must-fail [ V{ } ] [ 6 >vector 0 6 pick delete-slice ] unit-test @@ -173,7 +173,7 @@ unit-test [ V{ "C" } V{ "c" } ] [ { "a" "b" "C" } { "a" "b" "c" } drop-prefix [ >vector ] 2apply ] unit-test -[ -1 1 "abc" ] unit-test-fails +[ -1 1 "abc" ] must-fail [ V{ "a" "b" } V{ } ] [ { "X" "a" "b" } { "X" } drop-prefix [ >vector ] 2apply ] unit-test @@ -195,8 +195,8 @@ unit-test ! Pathological case [ "ihbye" ] [ "hi" "bye" append ] unit-test -[ -10 "hi" "bye" copy ] unit-test-fails -[ 10 "hi" "bye" copy ] unit-test-fails +[ -10 "hi" "bye" copy ] must-fail +[ 10 "hi" "bye" copy ] must-fail [ V{ 1 2 3 5 6 } ] [ 3 V{ 1 2 3 4 5 6 } clone [ delete-nth ] keep @@ -228,13 +228,13 @@ unit-test [ SBUF" \0\0\0" ] [ 3 SBUF" " new ] unit-test [ 0 ] [ f length ] unit-test -[ f first ] unit-test-fails +[ f first ] must-fail [ 3 ] [ 3 10 nth ] unit-test [ 3 ] [ 3 10 nth-unsafe ] unit-test -[ -3 10 nth ] unit-test-fails -[ 11 10 nth ] unit-test-fails +[ -3 10 nth ] must-fail +[ 11 10 nth ] must-fail -[ -1./0. 0 delete-nth ] unit-test-fails +[ -1./0. 0 delete-nth ] must-fail [ "" ] [ "" [ CHAR: \s = ] trim ] unit-test [ "" ] [ "" [ CHAR: \s = ] left-trim ] unit-test [ "" ] [ "" [ CHAR: \s = ] right-trim ] unit-test diff --git a/core/splitting/splitting-tests.factor b/core/splitting/splitting-tests.factor index 3ca78248ab..2b6107e08b 100644 --- a/core/splitting/splitting-tests.factor +++ b/core/splitting/splitting-tests.factor @@ -1,7 +1,7 @@ USING: splitting tools.test ; IN: temporary -[ { 1 2 3 } 0 group ] unit-test-fails +[ { 1 2 3 } 0 group ] must-fail [ { "hell" "o wo" "rld" } ] [ "hello world" 4 group ] unit-test diff --git a/core/strings/strings-tests.factor b/core/strings/strings-tests.factor index 985c025827..90e74275ff 100755 --- a/core/strings/strings-tests.factor +++ b/core/strings/strings-tests.factor @@ -4,7 +4,7 @@ IN: temporary [ CHAR: b ] [ 1 >bignum "abc" nth ] unit-test -[ ] [ 10 [ [ -1000000 ] catch drop ] times ] unit-test +[ ] [ 10 [ [ -1000000 ] ignore-errors ] times ] unit-test [ "abc" ] [ [ "a" "b" "c" ] [ [ % ] each ] "" make ] unit-test @@ -31,7 +31,7 @@ IN: temporary [ t ] [ "abc" "abd" <=> 0 < ] unit-test [ t ] [ "z" "abd" <=> 0 > ] unit-test -[ f ] [ [ 0 10 "hello" subseq ] catch not ] unit-test +[ 0 10 "hello" subseq ] must-fail [ "Replacing+spaces+with+plus" ] [ @@ -43,8 +43,8 @@ unit-test [ "05" ] [ "5" 2 CHAR: 0 pad-left ] unit-test [ "666" ] [ "666" 2 CHAR: 0 pad-left ] unit-test -[ 1 "" nth ] unit-test-fails -[ -6 "hello" nth ] unit-test-fails +[ 1 "" nth ] must-fail +[ -6 "hello" nth ] must-fail [ t ] [ "hello world" dup >vector >string = ] unit-test @@ -55,8 +55,7 @@ unit-test [ "\u001234bc\0\0\0" ] [ 6 "\u001234bc" resize-string ] unit-test ! Random tester found this -[ { "kernel-error" 3 12 -7 } ] -[ [ 2 -7 resize-string ] catch ] unit-test +[ 2 -7 resize-string ] [ { "kernel-error" 3 12 -7 } = ] must-fail-with ! Make sure 24-bit strings work "hello world" "s" set diff --git a/core/threads/threads-tests.factor b/core/threads/threads-tests.factor index b1b2f86a47..379b10ce88 100755 --- a/core/threads/threads-tests.factor +++ b/core/threads/threads-tests.factor @@ -9,4 +9,4 @@ IN: temporary yield [ ] [ 0.3 sleep ] unit-test -[ "hey" sleep ] unit-test-fails +[ "hey" sleep ] must-fail diff --git a/core/tuples/tuples-tests.factor b/core/tuples/tuples-tests.factor index 627ee5562f..dede1a2136 100755 --- a/core/tuples/tuples-tests.factor +++ b/core/tuples/tuples-tests.factor @@ -55,7 +55,7 @@ C: point "IN: temporary TUPLE: point z y ;" eval -[ "p" get point-x ] unit-test-fails +[ "p" get point-x ] must-fail [ 200 ] [ "p" get point-y ] unit-test [ 300 ] [ "p" get "point-z" "temporary" lookup execute ] unit-test @@ -97,7 +97,7 @@ TUPLE: delegate-clone ; [ f ] [ \ tuple \ delegate-clone class< ] unit-test ! Compiler regression -[ t ] [ [ t length ] catch no-method-object ] unit-test +[ t length ] [ no-method-object t eq? ] must-fail-with [ "" ] [ "TUPLE: constructor-test ; C: constructor-test" eval word word-name ] unit-test @@ -204,15 +204,15 @@ SYMBOL: not-a-tuple-class [ "IN: temporary C: not-a-tuple-class" eval -] unit-test-fails +] must-fail [ t ] [ "not-a-tuple-class" "temporary" lookup symbol? ] unit-test ! Missing check -[ not-a-tuple-class construct-boa ] unit-test-fails -[ not-a-tuple-class construct-empty ] unit-test-fails +[ not-a-tuple-class construct-boa ] must-fail +[ not-a-tuple-class construct-empty ] must-fail TUPLE: erg's-reshape-problem a b c d ; @@ -234,8 +234,6 @@ C: erg's-reshape-problem [ t ] [ 1 cons-test-3 array-capacity "a" get array-capacity = ] unit-test -[ t ] [ - [ - "IN: temporary SYMBOL: not-a-class C: not-a-class" eval - ] catch [ check-tuple? ] is? -] unit-test +[ + "IN: temporary SYMBOL: not-a-class C: not-a-class" eval +] [ [ check-tuple? ] is? ] must-fail-with diff --git a/core/vectors/vectors-tests.factor b/core/vectors/vectors-tests.factor index 4c57c238b4..b56cee1b34 100755 --- a/core/vectors/vectors-tests.factor +++ b/core/vectors/vectors-tests.factor @@ -3,25 +3,25 @@ sequences sequences.private strings tools.test vectors continuations random growable classes ; IN: temporary -[ ] [ 10 [ [ -1000000 ] catch drop ] times ] unit-test +[ ] [ 10 [ [ -1000000 ] ignore-errors ] times ] unit-test [ 3 ] [ [ t f t ] length ] unit-test [ 3 ] [ V{ t f t } length ] unit-test -[ -3 V{ } nth ] unit-test-fails -[ 3 V{ } nth ] unit-test-fails -[ 3 54.3 nth ] unit-test-fails +[ -3 V{ } nth ] must-fail +[ 3 V{ } nth ] must-fail +[ 3 54.3 nth ] must-fail -[ "hey" [ 1 2 ] set-length ] unit-test-fails -[ "hey" V{ 1 2 } set-length ] unit-test-fails +[ "hey" [ 1 2 ] set-length ] must-fail +[ "hey" V{ 1 2 } set-length ] must-fail [ 3 ] [ 3 0 [ set-length ] keep length ] unit-test [ "yo" ] [ "yo" 4 1 [ set-nth ] keep 4 swap nth ] unit-test -[ 1 V{ } nth ] unit-test-fails -[ -1 V{ } set-length ] unit-test-fails +[ 1 V{ } nth ] must-fail +[ -1 V{ } set-length ] must-fail [ V{ } ] [ [ ] >vector ] unit-test [ V{ 1 2 } ] [ [ 1 2 ] >vector ] unit-test @@ -64,8 +64,8 @@ IN: temporary [ V{ 2 3 } ] [ "funny-stack" get pop ] unit-test [ V{ 1 5 } ] [ "funny-stack" get peek ] unit-test [ V{ 1 5 } ] [ "funny-stack" get pop ] unit-test -[ "funny-stack" get pop ] unit-test-fails -[ "funny-stack" get pop ] unit-test-fails +[ "funny-stack" get pop ] must-fail +[ "funny-stack" get pop ] must-fail [ ] [ "funky" "funny-stack" get push ] unit-test [ "funky" ] [ "funny-stack" get pop ] unit-test diff --git a/core/vocabs/loader/loader-tests.factor b/core/vocabs/loader/loader-tests.factor index 560affa566..764f14e45f 100755 --- a/core/vocabs/loader/loader-tests.factor +++ b/core/vocabs/loader/loader-tests.factor @@ -18,16 +18,6 @@ debugger compiler.units ; [ t ] [ "kernel" f >vocab-link "kernel" vocab = ] unit-test -! This vocab should not exist, but just in case... -[ ] [ [ "core" forget-vocab ] with-compilation-unit ] unit-test - -2 [ - [ T{ no-vocab f "core" } ] - [ [ "core" require ] catch ] unit-test -] times - -[ f ] [ "core" vocab ] unit-test - [ t ] [ "kernel" vocab-files "kernel" vocab vocab-files @@ -59,7 +49,7 @@ IN: temporary 0 "count-me" set-global 2 [ - [ "vocabs.loader.test.a" require ] unit-test-fails + [ "vocabs.loader.test.a" require ] must-fail [ f ] [ "vocabs.loader.test.a" vocab-source-loaded? ] unit-test @@ -97,7 +87,7 @@ IN: temporary ] with-compilation-unit ] unit-test -[ "vocabs.loader.test.b" require ] unit-test-fails +[ "vocabs.loader.test.b" require ] must-fail [ 1 ] [ "count-me" get-global ] unit-test diff --git a/core/words/words-tests.factor b/core/words/words-tests.factor index 92f5284c49..f29d21cd9f 100755 --- a/core/words/words-tests.factor +++ b/core/words/words-tests.factor @@ -110,7 +110,7 @@ M: array freakish ; [ t ] [ \ bar \ freakish usage member? ] unit-test DEFER: x -[ t ] [ [ x ] catch undefined? ] unit-test +[ x ] [ undefined? ] must-fail-with [ ] [ "no-loc" "temporary" create drop ] unit-test [ f ] [ "no-loc" "temporary" lookup where ] unit-test @@ -141,10 +141,8 @@ SYMBOL: quot-uses-b [ { + } ] [ \ quot-uses-b uses ] unit-test -[ t ] [ - [ "IN: temporary : undef-test ; << undef-test >>" eval ] catch - [ undefined? ] is? -] unit-test +[ "IN: temporary : undef-test ; << undef-test >>" eval ] +[ [ undefined? ] is? ] must-fail-with [ ] [ "IN: temporary GENERIC: symbol-generic" eval diff --git a/extra/bitfields/bitfields-tests.factor b/extra/bitfields/bitfields-tests.factor index 6c82ec0323..8a3bb1f043 100644 --- a/extra/bitfields/bitfields-tests.factor +++ b/extra/bitfields/bitfields-tests.factor @@ -10,12 +10,12 @@ SAFE-BITFIELD: foo bar:5 baz:10 111 bing:2 ; [ 855 ] [ 21 852 3 855 swap with-foo-baz foo-baz ] unit-test [ 1 ] [ 21 852 3 1 swap with-foo-bing foo-bing ] unit-test -[ 100 0 0 ] unit-test-fails -[ 0 5000 0 ] unit-test-fails -[ 0 0 10 ] unit-test-fails +[ 100 0 0 ] must-fail +[ 0 5000 0 ] must-fail +[ 0 0 10 ] must-fail -[ 100 0 with-foo-bar ] unit-test-fails -[ 5000 0 with-foo-baz ] unit-test-fails -[ 10 0 with-foo-bing ] unit-test-fails +[ 100 0 with-foo-bar ] must-fail +[ 5000 0 with-foo-baz ] must-fail +[ 10 0 with-foo-bing ] must-fail [ BIN: 00101100000000111111 ] [ BIN: 101 BIN: 1000000001 BIN: 11 ] unit-test diff --git a/extra/bootstrap/io/io.factor b/extra/bootstrap/io/io.factor index 4d5440e546..065f7dd5c4 100755 --- a/extra/bootstrap/io/io.factor +++ b/extra/bootstrap/io/io.factor @@ -10,5 +10,3 @@ IN: bootstrap.io { [ wince? ] [ "windows.ce" ] } } cond append require ] when - -"vocabs.monitor" require diff --git a/extra/calendar/calendar-tests.factor b/extra/calendar/calendar-tests.factor index fbb60b2d49..3b0cfc8455 100644 --- a/extra/calendar/calendar-tests.factor +++ b/extra/calendar/calendar-tests.factor @@ -1,14 +1,14 @@ USING: arrays calendar kernel math sequences tools.test continuations system ; -[ "invalid timestamp" ] [ [ 2004 12 32 0 0 0 0 make-timestamp ] catch ] unit-test -[ "invalid timestamp" ] [ [ 2004 2 30 0 0 0 0 make-timestamp ] catch ] unit-test -[ "invalid timestamp" ] [ [ 2003 2 29 0 0 0 0 make-timestamp ] catch ] unit-test -[ "invalid timestamp" ] [ [ 2004 -2 9 0 0 0 0 make-timestamp ] catch ] unit-test -[ "invalid timestamp" ] [ [ 2004 12 0 0 0 0 0 make-timestamp ] catch ] unit-test -[ "invalid timestamp" ] [ [ 2004 12 1 24 0 0 0 make-timestamp ] catch ] unit-test -[ "invalid timestamp" ] [ [ 2004 12 1 23 60 0 0 make-timestamp ] catch ] unit-test -[ "invalid timestamp" ] [ [ 2004 12 1 23 59 60 0 0 make-timestamp ] catch ] unit-test +[ 2004 12 32 0 0 0 0 make-timestamp ] [ "invalid timestamp" = ] must-fail-with +[ 2004 2 30 0 0 0 0 make-timestamp ] [ "invalid timestamp" = ] must-fail-with +[ 2003 2 29 0 0 0 0 make-timestamp ] [ "invalid timestamp" = ] must-fail-with +[ 2004 -2 9 0 0 0 0 make-timestamp ] [ "invalid timestamp" = ] must-fail-with +[ 2004 12 0 0 0 0 0 make-timestamp ] [ "invalid timestamp" = ] must-fail-with +[ 2004 12 1 24 0 0 0 make-timestamp ] [ "invalid timestamp" = ] must-fail-with +[ 2004 12 1 23 60 0 0 make-timestamp ] [ "invalid timestamp" = ] must-fail-with +[ 2004 12 1 23 59 60 0 make-timestamp ] [ "invalid timestamp" = ] must-fail-with [ f ] [ 1900 leap-year? ] unit-test [ t ] [ 1904 leap-year? ] unit-test diff --git a/extra/circular/circular-tests.factor b/extra/circular/circular-tests.factor index 01504a0e8a..8ca4574885 100644 --- a/extra/circular/circular-tests.factor +++ b/extra/circular/circular-tests.factor @@ -9,7 +9,7 @@ circular strings ; [ CHAR: t ] [ "test" 0 swap nth ] unit-test [ "test" ] [ "test" >string ] unit-test -[ "test" 5 swap nth ] unit-test-fails +[ "test" 5 swap nth ] must-fail [ CHAR: e ] [ "test" 5 swap nth-unsafe ] unit-test [ [ 1 2 3 ] ] [ { 1 2 3 } [ ] like ] unit-test @@ -18,7 +18,7 @@ circular strings ; [ [ 3 1 2 ] ] [ { 1 2 3 } -100 over change-circular-start [ ] like ] unit-test [ "fob" ] [ "foo" CHAR: b 2 pick set-nth >string ] unit-test -[ "foo" CHAR: b 3 rot set-nth ] unit-test-fails +[ "foo" CHAR: b 3 rot set-nth ] must-fail [ "boo" ] [ "foo" CHAR: b 3 pick set-nth-unsafe >string ] unit-test [ "ornact" ] [ "factor" 4 over change-circular-start CHAR: n 2 pick set-nth >string ] unit-test diff --git a/extra/combinators/lib/lib-tests.factor b/extra/combinators/lib/lib-tests.factor index deeb105758..235f441b8b 100755 --- a/extra/combinators/lib/lib-tests.factor +++ b/extra/combinators/lib/lib-tests.factor @@ -8,26 +8,25 @@ IN: temporary [ 50 ] [ 100 [1,b] [ odd? ] count ] unit-test [ 328350 ] [ 100 [ sq ] sigma ] unit-test -: infers? [ infer drop ] curry catch not ; - [ { 910 911 912 } ] [ 10 900 3 [ + + ] map-with2 ] unit-test { 6 2 } [ 1 2 [ 5 + ] dip ] unit-test { 6 2 1 } [ 1 2 1 [ 5 + ] dipd ] unit-test -{ t } [ [ [ 99 ] 1 2 3 4 5 5 nslip ] infers? ] unit-test + +[ [ 99 ] 1 2 3 4 5 5 nslip ] must-infer { 99 1 2 3 4 5 } [ [ 99 ] 1 2 3 4 5 5 nslip ] unit-test -{ t } [ [ 1 2 3 4 5 [ drop drop drop drop drop 2 ] 5 nkeep ] infers? ] unit-test +[ 1 2 3 4 5 [ drop drop drop drop drop 2 ] 5 nkeep ] must-infer { 2 1 2 3 4 5 } [ 1 2 3 4 5 [ drop drop drop drop drop 2 ] 5 nkeep ] unit-test [ [ 1 2 3 + ] ] [ 1 2 3 [ + ] 3 ncurry ] unit-test -{ t } [ [ 1 2 { 3 4 } [ + + ] 2 map-withn ] infers? ] unit-test +[ 1 2 { 3 4 } [ + + ] 2 map-withn ] must-infer { { 6 7 } } [ 1 2 { 3 4 } [ + + ] 2 map-withn ] unit-test { { 16 17 18 19 20 } } [ 1 2 3 4 { 6 7 8 9 10 } [ + + + + ] 4 map-withn ] unit-test -{ t } [ [ 1 2 { 3 4 } [ + + drop ] 2 each-withn ] infers? ] unit-test +[ 1 2 { 3 4 } [ + + drop ] 2 each-withn ] must-infer { 13 } [ 1 2 { 3 4 } [ + + ] 2 each-withn + ] unit-test [ 1 1 2 2 3 3 ] [ 1 2 3 [ dup ] 3apply ] unit-test [ 1 4 9 ] [ 1 2 3 [ sq ] 3apply ] unit-test -[ t ] [ [ [ sq ] 3apply ] infers? ] unit-test +[ [ sq ] 3apply ] must-infer [ { 1 2 } { 2 4 } { 3 8 } { 4 16 } { 5 32 } ] [ 1 2 3 4 5 [ dup 2^ 2array ] 5 napply ] unit-test -[ t ] [ [ [ dup 2^ 2array ] 5 napply ] infers? ] unit-test +[ [ dup 2^ 2array ] 5 napply ] must-infer ! && diff --git a/extra/concurrency/concurrency-docs.factor b/extra/concurrency/concurrency-docs.factor index dafbafbc5b..f04811b72a 100644 --- a/extra/concurrency/concurrency-docs.factor +++ b/extra/concurrency/concurrency-docs.factor @@ -146,7 +146,7 @@ ARTICLE: { "concurrency" "exceptions" } "Exceptions" "A process can handle exceptions using the standard Factor exception handling mechanism. If an exception is uncaught the process will terminate. For example:" { $code "[ 1 0 / \"This will not print\" print ] spawn" } "Processes can be linked so that a parent process can receive the exception that caused the child process to terminate. In this way 'supervisor' processes can be created that are notified when child processes terminate and possibly restart them.\n\nThe easiest way to form this link is using " { $link spawn-link } ". This will create a unidirectional link, such that if an uncaught exception causes the child to terminate, the parent process can catch it:" -{ $code "[\n [ 1 0 / \"This will not print\" print ] spawn-link drop\n receive\n] catch [ \"Exception caught.\" print ] when" } +{ $code "[\n [ 1 0 / \"This will not print\" print ] spawn-link drop\n receive\n] [ \"Exception caught.\" print ] recover" } "Exceptions are only raised in the parent when the parent does a " { $link receive } " or " { $link receive-if } ". This is because the exception is sent from the child to the parent as a message." ; ARTICLE: { "concurrency" "futures" } "Futures" diff --git a/extra/concurrency/concurrency-tests.factor b/extra/concurrency/concurrency-tests.factor index a9d4b39854..2f9b6605d7 100644 --- a/extra/concurrency/concurrency-tests.factor +++ b/extra/concurrency/concurrency-tests.factor @@ -67,15 +67,12 @@ IN: temporary ] unit-test -[ "crash" ] [ +[ [ - [ - "crash" throw - ] spawn-link drop - receive - ] - catch -] unit-test + "crash" throw + ] spawn-link drop + receive +] [ "crash" = ] must-fail-with [ 50 ] [ [ 50 ] future ?future @@ -115,7 +112,7 @@ SYMBOL: value ! this is fixed (via a timeout). ! [ ! [ "this should propogate" throw ] future ?future -! ] unit-test-fails +! ] must-fail [ ] [ [ "this should not propogate" throw ] future drop diff --git a/extra/concurrency/concurrency.factor b/extra/concurrency/concurrency.factor index bc0d01956f..8d842f15d0 100644 --- a/extra/concurrency/concurrency.factor +++ b/extra/concurrency/concurrency.factor @@ -166,7 +166,7 @@ M: process send ( message process -- ) PRIVATE> : spawn-link ( quot -- process ) - [ catch [ rethrow-linked ] when* ] curry + [ [ rethrow-linked ] recover ] curry [ ((spawn)) ] curry (spawn-link) ; inline "parent-test" parse-stream drop - ] catch [ :1 ] when + ] [ :1 ] recover ] unit-test [ "xxx" ] [ "yyy" article-parent ] unit-test diff --git a/extra/inverse/inverse-tests.factor b/extra/inverse/inverse-tests.factor index a61be734fc..31e7c5f78a 100644 --- a/extra/inverse/inverse-tests.factor +++ b/extra/inverse/inverse-tests.factor @@ -3,7 +3,7 @@ math.functions math.constants ; IN: inverse-tests [ 2 ] [ { 3 2 } [ 3 swap 2array ] undo ] unit-test -[ { 3 4 } [ dup 2array ] undo ] unit-test-fails +[ { 3 4 } [ dup 2array ] undo ] must-fail TUPLE: foo bar baz ; @@ -15,7 +15,7 @@ C: foo [ t ] [ { 3 3 } [ 2same ] matches? ] unit-test [ f ] [ { 3 4 } [ 2same ] matches? ] unit-test -[ [ 2same ] matches? ] unit-test-fails +[ [ 2same ] matches? ] must-fail : something ( array -- num ) { @@ -25,9 +25,9 @@ C: foo [ 5 ] [ { 1 2 2 } something ] unit-test [ 6 ] [ { 2 3 } something ] unit-test -[ { 1 } something ] unit-test-fails +[ { 1 } something ] must-fail -[ 1 2 [ eq? ] undo ] unit-test-fails +[ 1 2 [ eq? ] undo ] must-fail : f>c ( *fahrenheit -- *celsius ) 32 - 1.8 / ; diff --git a/extra/io/buffers/buffers-tests.factor b/extra/io/buffers/buffers-tests.factor index 6fcdc86423..c9203d9ef8 100755 --- a/extra/io/buffers/buffers-tests.factor +++ b/extra/io/buffers/buffers-tests.factor @@ -75,5 +75,5 @@ sequences tools.test namespaces ; "b" get buffer-free 100 "b" set -[ 1000 "b" get n>buffer ] unit-test-fails +[ 1000 "b" get n>buffer ] must-fail "b" get buffer-free diff --git a/extra/io/mmap/mmap-tests.factor b/extra/io/mmap/mmap-tests.factor index a01481ecdc..f0547961bc 100644 --- a/extra/io/mmap/mmap-tests.factor +++ b/extra/io/mmap/mmap-tests.factor @@ -1,9 +1,9 @@ USING: io io.mmap io.files kernel tools.test continuations sequences ; IN: temporary -[ "mmap-test-file.txt" resource-path delete-file ] catch drop +[ "mmap-test-file.txt" resource-path delete-file ] ignore-errors [ ] [ "mmap-test-file.txt" resource-path [ "12345" write ] with-stream ] unit-test [ ] [ "mmap-test-file.txt" resource-path dup file-length [ CHAR: 2 0 pick set-nth drop ] with-mapped-file ] unit-test [ 5 ] [ "mmap-test-file.txt" resource-path dup file-length [ length ] with-mapped-file ] unit-test [ "22345" ] [ "mmap-test-file.txt" resource-path file-contents ] unit-test -[ "mmap-test-file.txt" resource-path delete-file ] catch drop +[ "mmap-test-file.txt" resource-path delete-file ] ignore-errors diff --git a/extra/io/unix/launcher/launcher-tests.factor b/extra/io/unix/launcher/launcher-tests.factor index fec97baa5a..eb3038e1b5 100755 --- a/extra/io/unix/launcher/launcher-tests.factor +++ b/extra/io/unix/launcher/launcher-tests.factor @@ -1,8 +1,8 @@ IN: temporary USING: io.unix.launcher tools.test ; -[ "" tokenize-command ] unit-test-fails -[ " " tokenize-command ] unit-test-fails +[ "" tokenize-command ] must-fail +[ " " tokenize-command ] must-fail [ { "a" } ] [ "a" tokenize-command ] unit-test [ { "abc" } ] [ "abc" tokenize-command ] unit-test [ { "abc" } ] [ "abc " tokenize-command ] unit-test @@ -14,8 +14,8 @@ USING: io.unix.launcher tools.test ; [ { "abc\\ def" } ] [ " 'abc\\\\ def'" tokenize-command ] unit-test [ { "abc\\ def" "hey" } ] [ "'abc\\\\ def' hey" tokenize-command ] unit-test [ { "abc def" "hey" } ] [ "'abc def' \"hey\"" tokenize-command ] unit-test -[ "'abc def' \"hey" tokenize-command ] unit-test-fails -[ "'abc def" tokenize-command ] unit-test-fails +[ "'abc def' \"hey" tokenize-command ] must-fail +[ "'abc def" tokenize-command ] must-fail [ { "abc def" "h\"ey" } ] [ "'abc def' \"h\\\"ey\" " tokenize-command ] unit-test [ diff --git a/extra/io/unix/linux/linux.factor b/extra/io/unix/linux/linux.factor index 9c4aced03f..55f5f01abc 100755 --- a/extra/io/unix/linux/linux.factor +++ b/extra/io/unix/linux/linux.factor @@ -3,7 +3,7 @@ USING: kernel io.backend io.monitors io.monitors.private io.files io.buffers io.nonblocking io.unix.backend io.unix.select io.unix.launcher unix.linux.inotify assocs namespaces threads -continuations init math alien.c-types alien ; +continuations init math alien.c-types alien vocabs.loader ; IN: io.unix.linux TUPLE: linux-io ; @@ -134,4 +134,6 @@ M: linux-io init-io ( -- ) T{ linux-io } set-io-backend -[ start-wait-thread ] "io.unix.linux" add-init-hook \ No newline at end of file +[ start-wait-thread ] "io.unix.linux" add-init-hook + +"vocabs.monitor" require \ No newline at end of file diff --git a/extra/io/unix/unix-tests.factor b/extra/io/unix/unix-tests.factor index 8a621f8f48..5a93257949 100755 --- a/extra/io/unix/unix-tests.factor +++ b/extra/io/unix/unix-tests.factor @@ -7,7 +7,7 @@ IN: temporary [ [ "unix-domain-socket-test" resource-path delete-file - ] catch drop + ] ignore-errors "unix-domain-socket-test" resource-path [ @@ -36,7 +36,7 @@ yield ! Unix domain datagram sockets [ "unix-domain-datagram-test" resource-path delete-file -] catch drop +] ignore-errors : server-addr "unix-domain-datagram-test" resource-path ; : client-addr "unix-domain-datagram-test-2" resource-path ; @@ -75,7 +75,7 @@ yield [ "unix-domain-datagram-test-2" resource-path delete-file -] catch drop +] ignore-errors client-addr "d" set @@ -110,7 +110,7 @@ client-addr [ "unix-domain-datagram-test-3" resource-path delete-file -] catch drop +] ignore-errors "unix-domain-datagram-test-2" resource-path delete-file @@ -118,29 +118,29 @@ client-addr [ B{ 1 2 3 } "unix-domain-datagram-test-3" "d" get send -] unit-test-fails +] must-fail [ ] [ "d" get dispose ] unit-test ! See what happens on send/receive after close -[ "d" get receive ] unit-test-fails +[ "d" get receive ] must-fail -[ B{ 1 2 } server-addr "d" get send ] unit-test-fails +[ B{ 1 2 } server-addr "d" get send ] must-fail ! Invalid parameter tests [ image [ stdio get accept ] with-stream -] unit-test-fails +] must-fail [ image [ stdio get receive ] with-stream -] unit-test-fails +] must-fail [ image [ B{ 1 2 } server-addr stdio get send ] with-stream -] unit-test-fails +] must-fail diff --git a/extra/io/windows/nt/nt.factor b/extra/io/windows/nt/nt.factor index b957aa2fca..be57a398a2 100755 --- a/extra/io/windows/nt/nt.factor +++ b/extra/io/windows/nt/nt.factor @@ -1,6 +1,7 @@ ! Copyright (C) 2004, 2008 Mackenzie Straight, Doug Coleman, ! Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. +USE: vocabs.loader USE: io.windows USE: io.windows.nt.backend USE: io.windows.nt.files @@ -11,3 +12,5 @@ USE: io.windows.mmap USE: io.backend T{ windows-nt-io } set-io-backend + +"vocabs.monitor" require diff --git a/extra/irc/irc.factor b/extra/irc/irc.factor index 5b4355986f..44c682e671 100755 --- a/extra/irc/irc.factor +++ b/extra/irc/irc.factor @@ -189,7 +189,7 @@ SYMBOL: line : with-infinite-loop ( quot timeout -- quot timeout ) "looping" print flush - over catch drop dup sleep with-infinite-loop ; + over [ drop ] recover dup sleep with-infinite-loop ; : start-irc ( irc-client -- ) ! [ [ do-irc ] curry 3000 with-infinite-loop ] with-scope ; diff --git a/extra/math/complex/complex-tests.factor b/extra/math/complex/complex-tests.factor index be512e5052..e8535d0637 100755 --- a/extra/math/complex/complex-tests.factor +++ b/extra/math/complex/complex-tests.factor @@ -2,8 +2,8 @@ USING: kernel math math.constants math.functions tools.test prettyprint ; IN: temporary -[ 1 C{ 0 1 } rect> ] unit-test-fails -[ C{ 0 1 } 1 rect> ] unit-test-fails +[ 1 C{ 0 1 } rect> ] must-fail +[ C{ 0 1 } 1 rect> ] must-fail [ f ] [ C{ 5 12.5 } 5 = ] unit-test [ t ] [ C{ 1.0 2.0 } C{ 1 2 } = ] unit-test diff --git a/extra/math/functions/functions-tests.factor b/extra/math/functions/functions-tests.factor index 439eaace6f..6f4dc42593 100755 --- a/extra/math/functions/functions-tests.factor +++ b/extra/math/functions/functions-tests.factor @@ -73,7 +73,7 @@ IN: temporary [ 3 ] [ 5 7 mod-inv ] unit-test [ 78572682077 ] [ 234829342 342389423843 mod-inv ] unit-test -[ 2 10 mod-inv ] unit-test-fails +[ 2 10 mod-inv ] must-fail [ t ] [ 0 0 ^ fp-nan? ] unit-test [ 1 ] [ 10 0 ^ ] unit-test diff --git a/extra/memoize/memoize-tests.factor b/extra/memoize/memoize-tests.factor index f5a7f85edb..dbd2d3a16a 100644 --- a/extra/memoize/memoize-tests.factor +++ b/extra/memoize/memoize-tests.factor @@ -7,4 +7,4 @@ MEMO: fib ( m -- n ) [ 89 ] [ 10 fib ] unit-test -[ "USING: kernel math memoize ; MEMO: x ( a b c d e -- f g h i j ) >r >r >r >r 1+ r> r> r> r> ;" eval ] unit-test-fails +[ "USING: kernel math memoize ; MEMO: x ( a b c d e -- f g h i j ) >r >r >r >r 1+ r> r> r> r> ;" eval ] must-fail diff --git a/extra/multi-methods/multi-methods-tests.factor b/extra/multi-methods/multi-methods-tests.factor index d2af88d02a..a0769dffda 100755 --- a/extra/multi-methods/multi-methods-tests.factor +++ b/extra/multi-methods/multi-methods-tests.factor @@ -52,7 +52,7 @@ METHOD: beats? { thing thing } f ; : play ( obj1 obj2 -- ? ) beats? 2nip ; -[ { } 3 play ] unit-test-fails +[ { } 3 play ] must-fail [ t ] [ error get no-method? ] unit-test [ ] [ error get error. ] unit-test [ t ] [ T{ paper } T{ scissors } play ] unit-test diff --git a/extra/parser-combinators/parser-combinators-tests.factor b/extra/parser-combinators/parser-combinators-tests.factor index fc8cec770b..a1f82391a0 100644 --- a/extra/parser-combinators/parser-combinators-tests.factor +++ b/extra/parser-combinators/parser-combinators-tests.factor @@ -76,7 +76,7 @@ IN: scratchpad [ "begin1" "begin" token some parse -] unit-test-fails +] must-fail { "begin" } [ "begin" "begin" token some parse diff --git a/extra/regexp/regexp-tests.factor b/extra/regexp/regexp-tests.factor index 9c0ed5bd81..f6e7c05910 100755 --- a/extra/regexp/regexp-tests.factor +++ b/extra/regexp/regexp-tests.factor @@ -95,7 +95,7 @@ IN: regexp-tests [ t ] [ "]" "[]]" f matches? ] unit-test [ f ] [ "]" "[^]]" f matches? ] unit-test -! [ "^" "[^]" f matches? ] unit-test-fails +! [ "^" "[^]" f matches? ] must-fail [ t ] [ "^" "[]^]" f matches? ] unit-test [ t ] [ "]" "[]^]" f matches? ] unit-test diff --git a/extra/roman/roman-tests.factor b/extra/roman/roman-tests.factor index e850411726..a15dcef354 100644 --- a/extra/roman/roman-tests.factor +++ b/extra/roman/roman-tests.factor @@ -28,11 +28,11 @@ USING: arrays kernel math roman roman.private sequences tools.test ; [ 1666 ] [ 1666 >roman roman> ] unit-test [ 3444 ] [ 3444 >roman roman> ] unit-test [ 3999 ] [ 3999 >roman roman> ] unit-test -[ 0 >roman ] unit-test-fails -[ 4000 >roman ] unit-test-fails +[ 0 >roman ] must-fail +[ 4000 >roman ] must-fail [ "vi" ] [ "iii" "iii" roman+ ] unit-test [ "viii" ] [ "x" "ii" roman- ] unit-test [ "ix" ] [ "iii" "iii" roman* ] unit-test [ "i" ] [ "iii" "ii" roman/i ] unit-test [ "i" "ii" ] [ "v" "iii" roman/mod ] unit-test -[ "iii" "iii" roman- ] unit-test-fails +[ "iii" "iii" roman- ] must-fail diff --git a/extra/sequences/lib/lib-tests.factor b/extra/sequences/lib/lib-tests.factor index 717f463c45..d0bc0a9e52 100644 --- a/extra/sequences/lib/lib-tests.factor +++ b/extra/sequences/lib/lib-tests.factor @@ -38,7 +38,7 @@ math.functions tools.test strings ; [ f ] [ { "asdf" "bsdf" } singleton? ] unit-test [ 2 ] [ V{ 10 20 30 } [ delete-random drop ] keep length ] unit-test -[ V{ } [ delete-random drop ] keep length ] unit-test-fails +[ V{ } [ delete-random drop ] keep length ] must-fail [ { 1 9 25 } ] [ { 1 3 5 6 } [ sq ] [ even? ] map-until ] unit-test [ { 2 4 } ] [ { 2 4 1 3 } [ even? ] take-while ] unit-test diff --git a/extra/tetris/board/board-tests.factor b/extra/tetris/board/board-tests.factor index 3a870e621e..bd8789c4d6 100644 --- a/extra/tetris/board/board-tests.factor +++ b/extra/tetris/board/board-tests.factor @@ -5,7 +5,7 @@ colors ; [ { { f f } { f f } { f f } } ] [ 2 3 board-rows ] unit-test [ 1 { f f } ] [ 2 3 { 1 1 } board@block ] unit-test [ f ] [ 2 3 { 1 1 } board-block ] unit-test -[ 2 3 { 2 3 } board-block ] unit-test-fails +[ 2 3 { 2 3 } board-block ] must-fail red 1array [ 2 3 dup { 1 1 } red board-set-block { 1 1 } board-block ] unit-test [ t ] [ 2 3 { 1 1 } block-free? ] unit-test [ f ] [ 2 3 dup { 1 1 } red board-set-block { 1 1 } block-free? ] unit-test diff --git a/extra/tools/interpreter/interpreter-tests.factor b/extra/tools/interpreter/interpreter-tests.factor index 3976ada845..e7fe7854fa 100644 --- a/extra/tools/interpreter/interpreter-tests.factor +++ b/extra/tools/interpreter/interpreter-tests.factor @@ -99,7 +99,7 @@ IN: temporary [ [ [ 3 swap continue-with ] callcc1 2 * ] test-interpreter ] unit-test [ { 6 } ] -[ [ [ 3 throw ] catch 2 * ] test-interpreter ] unit-test +[ [ [ 3 throw ] [ 2 * ] recover ] test-interpreter ] unit-test [ { "{ 1 2 3 }\n" } ] [ [ [ { 1 2 3 } . ] string-out ] test-interpreter diff --git a/extra/tools/test/inference/inference.factor b/extra/tools/test/inference/inference.factor index 17ff7e1acd..cc77f4910d 100755 --- a/extra/tools/test/inference/inference.factor +++ b/extra/tools/test/inference/inference.factor @@ -10,7 +10,6 @@ IN: tools.test.inference : unit-test-effect ( effect quot -- ) >r 1quotation r> [ infer short-effect ] curry unit-test ; -: must-infer ( word -- ) - dup "declared-effect" word-prop - dup effect-in length swap effect-out length 2array - swap 1quotation unit-test-effect ; +: must-infer ( word/quot -- ) + dup word? [ 1quotation ] when + [ infer drop ] curry [ ] swap unit-test ; diff --git a/extra/tools/test/test.factor b/extra/tools/test/test.factor index aa994e91d2..1037323ddb 100755 --- a/extra/tools/test/test.factor +++ b/extra/tools/test/test.factor @@ -42,6 +42,9 @@ M: expected-error summary : must-fail ( quot -- ) [ drop t ] must-fail-with ; +: ignore-errors ( quot -- ) + [ drop ] recover ; inline + : run-test ( path -- failures ) [ "temporary" forget-vocab ] with-compilation-unit [ diff --git a/extra/ui/tools/listener/listener-tests.factor b/extra/ui/tools/listener/listener-tests.factor index eab85209cc..56c90f760f 100755 --- a/extra/ui/tools/listener/listener-tests.factor +++ b/extra/ui/tools/listener/listener-tests.factor @@ -25,7 +25,7 @@ timers [ init-timers ] unless [ ] [ "SYMBOL:" "i" get set-editor-string ] unit-test [ ] [ - "i" get [ { "SYMBOL:" } parse-lines ] catch go-to-error + "i" get [ { "SYMBOL:" } parse-lines ] [ go-to-error ] recover ] unit-test [ t ] [ diff --git a/extra/xml/test/errors.factor b/extra/xml/test/errors.factor index 596f1e6c43..c0a60d8a3f 100644 --- a/extra/xml/test/errors.factor +++ b/extra/xml/test/errors.factor @@ -1,7 +1,7 @@ USING: continuations xml xml.errors tools.test kernel arrays xml.data state-parser quotations ; : xml-error-test ( expected-error xml-string -- ) - swap 1array >quotation swap [ [ string>xml ] catch nip ] curry unit-test ; + [ string>xml ] curry swap [ = ] curry must-fail-with ; T{ no-entity T{ parsing-error f 1 10 } "nbsp" } " " xml-error-test T{ mismatched T{ parsing-error f 1 8 } T{ name f "" "x" "" } T{ name f "" "y" "" } diff --git a/extra/xml/test/test.factor b/extra/xml/test/test.factor index ec59d3564e..0198ebacb7 100644 --- a/extra/xml/test/test.factor +++ b/extra/xml/test/test.factor @@ -17,7 +17,7 @@ SYMBOL: xml-file xml-file get T{ name f "" "this" "http://d.de" } swap at ] unit-test [ t ] [ xml-file get tag-children second contained-tag? ] unit-test -[ t ] [ [ "" string>xml ] catch xml-parse-error? ] unit-test +[ "" string>xml ] [ xml-parse-error? ] must-fail-with [ T{ comment f "This is where the fun begins!" } ] [ xml-file get xml-before [ comment? ] find nip ] unit-test From f7ca140c230af21ad26a00e0320f056783d56a6c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 13:51:16 -0600 Subject: [PATCH 137/269] Fix compiled-xref --- core/words/words.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/words/words.factor b/core/words/words.factor index f628d68bee..bd49a3d855 100755 --- a/core/words/words.factor +++ b/core/words/words.factor @@ -99,7 +99,7 @@ SYMBOL: compiled-crossref compiled-crossref global [ H{ } assoc-like ] change-at : compiled-xref ( word dependencies -- ) - [ crossref? ] subset + [ drop crossref? ] assoc-subset 2dup "compiled-uses" set-word-prop compiled-crossref get add-vertex* ; From 31b863f8b20da0a8850b2eabcafa0625ff13d035 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 13:51:23 -0600 Subject: [PATCH 138/269] Fix docs load error --- extra/tools/test/test-docs.factor | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) mode change 100644 => 100755 extra/tools/test/test-docs.factor diff --git a/extra/tools/test/test-docs.factor b/extra/tools/test/test-docs.factor old mode 100644 new mode 100755 index 32825c965d..147e795861 --- a/extra/tools/test/test-docs.factor +++ b/extra/tools/test/test-docs.factor @@ -10,7 +10,8 @@ $nl $nl "If the test harness needs to define words, they should be placed in the " { $snippet "temporary" } " vocabulary so that they can be forgotten after the tests have been run. Test harness files consist mostly of calls to the following two words:" { $subsection unit-test } -{ $subsection unit-test-fails } +{ $subsection must-fail } +{ $subsection must-fail-with } "The following words run test harness files; any test failures are collected and printed at the end:" { $subsection test } { $subsection test-all } ; @@ -21,7 +22,7 @@ HELP: unit-test { $values { "output" "a sequence of expected stack elements" } { "input" "a quotation run with an empty stack" } } { $description "Runs a quotation with an empty stack, comparing the resulting stack with " { $snippet "output" } ". Elements are compared using " { $link = } ". Throws an error if the expected stack does not match the resulting stack." } ; -HELP: unit-test-fails +HELP: must-fail { $values { "quot" "a quotation run with an empty stack" } } { $description "Runs a quotation with an empty stack, expecting it to throw an error. If the quotation throws an error, this word returns normally. If the quotation does not throw an error, this word " { $emphasis "does" } " raise an error." } { $notes "This word is used to test boundary conditions and fail-fast behavior." } ; From b18a4632852bee2b421c2e35df254e84e738d1f1 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 14:59:53 -0600 Subject: [PATCH 139/269] Better inlining heuristic --- core/compiler/test/optimizer.factor | 11 ++++++++++- core/compiler/test/redefine.factor | 2 +- core/optimizer/backend/backend.factor | 22 ++++++++++++++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/core/compiler/test/optimizer.factor b/core/compiler/test/optimizer.factor index 091648cbbc..7ee4ebfd1c 100755 --- a/core/compiler/test/optimizer.factor +++ b/core/compiler/test/optimizer.factor @@ -2,7 +2,7 @@ USING: arrays compiler generic hashtables inference kernel kernel.private math optimizer prettyprint sequences sbufs strings tools.test vectors words sequences.private quotations optimizer.backend classes inference.dataflow tuples.private -continuations ; +continuations growable ; IN: temporary [ H{ { 1 5 } { 3 4 } { 2 5 } } ] [ @@ -291,3 +291,12 @@ TUPLE: silly-tuple a b ; : construct-empty-bug construct-empty ; [ ] [ [ construct-empty ] dataflow optimize drop ] unit-test + +! Make sure we have sane heuristics +: should-inline? method method-word flat-length 10 <= ; + +[ t ] [ \ fixnum \ shift should-inline? ] unit-test +[ f ] [ \ array \ equal? should-inline? ] unit-test +[ f ] [ \ sequence \ hashcode* should-inline? ] unit-test +[ t ] [ \ array \ nth-unsafe should-inline? ] unit-test +[ t ] [ \ growable \ nth-unsafe should-inline? ] unit-test diff --git a/core/compiler/test/redefine.factor b/core/compiler/test/redefine.factor index e9927f4964..ab472668c3 100755 --- a/core/compiler/test/redefine.factor +++ b/core/compiler/test/redefine.factor @@ -235,7 +235,7 @@ DEFER: flushable-test-2 : bx ax ; [ \ bx forget ] with-compilation-unit -[ f ] [ \ bx \ ax compiled-usage contains? ] unit-test +[ f ] [ \ bx \ ax compiled-usage key? ] unit-test DEFER: defer-redefine-test-2 diff --git a/core/optimizer/backend/backend.factor b/core/optimizer/backend/backend.factor index e73200b861..788f862849 100755 --- a/core/optimizer/backend/backend.factor +++ b/core/optimizer/backend/backend.factor @@ -245,18 +245,32 @@ M: #dispatch optimize-node* : dispatching-class ( node word -- class ) [ dispatch# node-class# ] keep specific-method ; -: flat-length ( seq -- n ) +! A heuristic to avoid excessive inlining +DEFER: (flat-length) + +: word-flat-length ( word -- n ) + dup get over inline? not or + [ drop 1 ] [ dup dup set word-def (flat-length) ] if ; + +: (flat-length) ( seq -- n ) [ - dup quotation? over array? or - [ flat-length ] [ drop 1 ] if + { + { [ dup quotation? ] [ (flat-length) 1+ ] } + { [ dup array? ] [ (flat-length) ] } + { [ dup word? ] [ word-flat-length ] } + { [ t ] [ drop 1 ] } + } cond ] map sum ; +: flat-length ( seq -- n ) + [ word-def (flat-length) ] with-scope ; + : will-inline-method ( node word -- method-spec/t quot/t ) #! t indicates failure tuck dispatching-class dup [ swap [ 2array ] 2keep method method-word - dup word-def flat-length 5 >= + dup flat-length 10 >= [ 1quotation ] [ word-def ] if ] [ 2drop t t From 8428f66933f1cfb9c20e818667b8ef36eb93b614 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 15:00:10 -0600 Subject: [PATCH 140/269] Fixing unit tests --- core/classes/classes-tests.factor | 6 ++++-- core/generic/generic-tests.factor | 2 +- core/inference/inference-tests.factor | 4 ++-- core/tuples/tuples-tests.factor | 2 +- core/vocabs/loader/loader-tests.factor | 17 +++++++---------- extra/combinators/lib/lib-tests.factor | 2 +- extra/tools/test/test.factor | 2 +- 7 files changed, 17 insertions(+), 18 deletions(-) diff --git a/core/classes/classes-tests.factor b/core/classes/classes-tests.factor index d78436bd5f..c7024a7490 100755 --- a/core/classes/classes-tests.factor +++ b/core/classes/classes-tests.factor @@ -169,8 +169,10 @@ UNION: redefine-bug-2 redefine-bug-1 quotation ; UNION: forget-class-bug-1 integer ; UNION: forget-class-bug-2 forget-class-bug-1 dll ; -FORGET: forget-class-bug-1 -FORGET: forget-class-bug-2 +[ + \ forget-class-bug-1 forget + \ forget-class-bug-2 forget +] with-compilation-unit [ f ] [ forget-class-bug-1 typemap get values [ memq? ] with contains? ] unit-test diff --git a/core/generic/generic-tests.factor b/core/generic/generic-tests.factor index e4d4160605..e3fdbc7b46 100755 --- a/core/generic/generic-tests.factor +++ b/core/generic/generic-tests.factor @@ -155,7 +155,7 @@ M: string my-hook "a string" ; [ "an integer" ] [ 3 my-var set my-hook ] unit-test [ "a string" ] [ my-hook my-var set my-hook ] unit-test -[ 1.0 my-var set my-hook ] [ [ T{ no-method f 1.0 my-hook } = ] must-fail-with +[ 1.0 my-var set my-hook ] [ T{ no-method f 1.0 my-hook } = ] must-fail-with GENERIC: tag-and-f ( x -- x x ) diff --git a/core/inference/inference-tests.factor b/core/inference/inference-tests.factor index 1738a71b7e..b43226166a 100755 --- a/core/inference/inference-tests.factor +++ b/core/inference/inference-tests.factor @@ -326,10 +326,10 @@ DEFER: bar : bad-bin ( a b -- ) 5 [ 5 bad-bin bad-bin 5 ] [ 2drop ] if ; [ [ bad-bin ] infer ] must-fail -[ [ [ r> ] infer ] [ inference-error? ] must-fail-with +[ [ r> ] infer ] [ inference-error? ] must-fail-with ! Regression -[ [ [ get-slots ] infer ] [ inference-error? ] must-fail-with +[ [ get-slots ] infer ] [ inference-error? ] must-fail-with ! Test some curry stuff { 1 1 } [ 3 [ ] curry 4 [ ] curry if ] unit-test-effect diff --git a/core/tuples/tuples-tests.factor b/core/tuples/tuples-tests.factor index dede1a2136..c9656a3b9e 100755 --- a/core/tuples/tuples-tests.factor +++ b/core/tuples/tuples-tests.factor @@ -123,7 +123,7 @@ TUPLE: yo-momma ; [ ] [ \ yo-momma forget ] unit-test [ f ] [ \ yo-momma typemap get values memq? ] unit-test - [ f ] [ \ yo-momma crossref ] unit-test + [ f ] [ \ yo-momma crossref get at ] unit-test ] with-compilation-unit TUPLE: loc-recording ; diff --git a/core/vocabs/loader/loader-tests.factor b/core/vocabs/loader/loader-tests.factor index 764f14e45f..3a8fc37583 100755 --- a/core/vocabs/loader/loader-tests.factor +++ b/core/vocabs/loader/loader-tests.factor @@ -63,14 +63,12 @@ IN: temporary [ 2 ] [ "count-me" get-global ] unit-test -[ t ] [ - [ - "IN: vocabs.loader.test.a v-l-t-a-hello" - - "resource:core/vocabs/loader/test/a/a.factor" - parse-stream - ] catch [ no-word? ] is? -] unit-test +[ + "IN: vocabs.loader.test.a v-l-t-a-hello" + + "resource:core/vocabs/loader/test/a/a.factor" + parse-stream +] [ [ no-word? ] is? ] must-fail-with 0 "count-me" set-global @@ -121,8 +119,7 @@ IN: temporary [ "kernel" vocab where ] unit-test [ t ] [ - [ "vocabs.loader.test.d" require ] catch - [ :1 ] when + [ "vocabs.loader.test.d" require ] [ :1 ] recover "vocabs.loader.test.d" vocab-source-loaded? ] unit-test diff --git a/extra/combinators/lib/lib-tests.factor b/extra/combinators/lib/lib-tests.factor index 235f441b8b..20f52b2ea3 100755 --- a/extra/combinators/lib/lib-tests.factor +++ b/extra/combinators/lib/lib-tests.factor @@ -1,5 +1,5 @@ USING: combinators.lib kernel math math.ranges random sequences -tools.test inference continuations arrays vectors ; +tools.test tools.test.inference continuations arrays vectors ; IN: temporary [ 5 ] [ [ 10 random ] [ 5 = ] generate ] unit-test diff --git a/extra/tools/test/test.factor b/extra/tools/test/test.factor index 1037323ddb..9590f32539 100755 --- a/extra/tools/test/test.factor +++ b/extra/tools/test/test.factor @@ -37,7 +37,7 @@ M: expected-error summary : must-fail-with ( quot test -- ) >r [ expected-error construct-empty throw ] compose r> [ recover ] 2curry - [ ] swap unit-test ; + [ t ] swap unit-test ; : must-fail ( quot -- ) [ drop t ] must-fail-with ; From 90ed177a9c410eacdf6ddad3a09cf025bcae13fd Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 19:23:39 -0600 Subject: [PATCH 141/269] Fixing load-everything and unit tests --- core/dlists/dlists.factor | 5 +++++ core/io/files/files-tests.factor | 3 ++- core/io/files/files.factor | 8 ++++++-- core/parser/parser-tests.factor | 8 ++++++++ core/parser/parser.factor | 10 +++++++--- core/vocabs/loader/loader.factor | 7 ++++--- extra/asn1/asn1-tests.factor | 4 ++-- extra/concurrency/concurrency-tests.factor | 2 ++ extra/concurrency/concurrency.factor | 2 +- extra/hardware-info/windows/ce/ce.factor | 16 ++++++++-------- extra/http/server/templating/templating.factor | 7 ++++--- extra/ldap/libldap/libldap.factor | 8 ++++---- extra/math/constants/constants-docs.factor | 4 ++-- extra/math/constants/constants.factor | 2 +- .../math/matrices/elimination/elimination.factor | 7 +++++-- extra/nehe/5/5.factor | 4 +++- extra/openssl/libcrypto/libcrypto.factor | 2 +- extra/openssl/openssl-tests.factor | 2 +- extra/openssl/openssl.factor | 2 +- .../partial-continuations.factor | 4 ++-- extra/random-tester/random-tester.factor | 6 +++--- extra/regexp/regexp.factor | 2 +- extra/serialize/serialize-tests.factor | 4 +--- extra/state-parser/state-parser-tests.factor | 2 +- extra/tuple-syntax/tuple-syntax-tests.factor | 1 + extra/tuple-syntax/tuple-syntax.factor | 9 +++++---- extra/ui/gadgets/editors/editors.factor | 6 +++--- extra/xmode/utilities/utilities-tests.factor | 4 ++-- 28 files changed, 86 insertions(+), 55 deletions(-) mode change 100644 => 100755 extra/concurrency/concurrency-tests.factor mode change 100644 => 100755 extra/concurrency/concurrency.factor mode change 100644 => 100755 extra/ldap/libldap/libldap.factor mode change 100644 => 100755 extra/nehe/5/5.factor mode change 100644 => 100755 extra/openssl/libcrypto/libcrypto.factor mode change 100644 => 100755 extra/openssl/openssl-tests.factor mode change 100644 => 100755 extra/openssl/openssl.factor mode change 100644 => 100755 extra/partial-continuations/partial-continuations.factor mode change 100644 => 100755 extra/random-tester/random-tester.factor mode change 100644 => 100755 extra/serialize/serialize-tests.factor mode change 100644 => 100755 extra/state-parser/state-parser-tests.factor mode change 100644 => 100755 extra/tuple-syntax/tuple-syntax-tests.factor mode change 100644 => 100755 extra/tuple-syntax/tuple-syntax.factor mode change 100644 => 100755 extra/xmode/utilities/utilities-tests.factor diff --git a/core/dlists/dlists.factor b/core/dlists/dlists.factor index ddec312182..12b1cd51ad 100755 --- a/core/dlists/dlists.factor +++ b/core/dlists/dlists.factor @@ -144,6 +144,11 @@ PRIVATE> : dlist-delete ( obj dlist -- obj/f ) >r [ eq? ] curry r> delete-node-if ; +: dlist-delete-all ( dlist -- ) + f over set-dlist-front + f over set-dlist-back + 0 swap set-dlist-length ; + : dlist-each ( dlist quot -- ) [ dlist-node-obj ] swap compose dlist-each-node ; inline diff --git a/core/io/files/files-tests.factor b/core/io/files/files-tests.factor index 5d4bb70912..bac9a2e65e 100755 --- a/core/io/files/files-tests.factor +++ b/core/io/files/files-tests.factor @@ -2,7 +2,8 @@ IN: temporary USING: tools.test io.files io threads kernel continuations ; [ "passwd" ] [ "/etc/passwd" file-name ] unit-test -[ "awk/" ] [ "/usr/libexec/awk/" file-name ] unit-test +[ "awk" ] [ "/usr/libexec/awk/" file-name ] unit-test +[ "awk" ] [ "/usr/libexec/awk///" file-name ] unit-test [ ] [ "test-foo.txt" resource-path [ diff --git a/core/io/files/files.factor b/core/io/files/files.factor index 9a99090699..5d0cf6bf11 100755 --- a/core/io/files/files.factor +++ b/core/io/files/files.factor @@ -64,7 +64,7 @@ M: object root-directory? ( path -- ? ) path-separator? ; normalize-directory dup (directory) fixup-directory ; : last-path-separator ( path -- n ? ) - [ length 2 [-] ] keep [ path-separator? ] find-last* ; + [ length 1- ] keep [ path-separator? ] find-last* ; TUPLE: no-parent-directory path ; @@ -83,7 +83,11 @@ TUPLE: no-parent-directory path ; } cond ; : file-name ( path -- string ) - dup last-path-separator [ 1+ tail ] [ drop ] if ; + right-trim-separators { + { [ dup empty? ] [ drop "/" ] } + { [ dup last-path-separator ] [ 1+ tail ] } + { [ t ] [ drop ] } + } cond ; : resource-path ( path -- newpath ) \ resource-path get [ image parent-directory ] unless* diff --git a/core/parser/parser-tests.factor b/core/parser/parser-tests.factor index eb04e329d9..c40bc54335 100755 --- a/core/parser/parser-tests.factor +++ b/core/parser/parser-tests.factor @@ -349,6 +349,14 @@ IN: temporary "IN: temporary : foo ; TUPLE: foo ;" "redefining-a-class-4" parse-stream drop ] [ [ redefine-error? ] is? ] must-fail-with + + [ ] [ + "IN: temporary : foo ( x y -- z ) 1 2 ; : bar ( a -- b ) ;" eval + ] unit-test + + [ + "IN: temporary : foo ( x y -- z) 1 2 ; : bar ( a -- b ) ;" eval + ] must-fail ] with-file-vocabs [ diff --git a/core/parser/parser.factor b/core/parser/parser.factor index 59d18dc734..d54bf1c1f4 100755 --- a/core/parser/parser.factor +++ b/core/parser/parser.factor @@ -307,10 +307,14 @@ SYMBOL: lexer-factory ! Parsing word utilities : parse-effect ( -- effect ) - ")" parse-tokens { "--" } split1 dup [ - + ")" parse-tokens "(" over member? [ + "Stack effect declaration must not contain (" throw ] [ - "Stack effect declaration must contain --" throw + { "--" } split1 dup [ + + ] [ + "Stack effect declaration must contain --" throw + ] if ] if ; TUPLE: bad-number ; diff --git a/core/vocabs/loader/loader.factor b/core/vocabs/loader/loader.factor index 64372fe4b7..e42dace945 100755 --- a/core/vocabs/loader/loader.factor +++ b/core/vocabs/loader/loader.factor @@ -149,12 +149,14 @@ SYMBOL: load-help? dup modified-sources swap modified-docs ; : load-error. ( vocab error -- ) - "While loading " rot dup >vocab-link write-object ":" print - print-error ; + "==== " write >r + dup vocab-name swap f >vocab-link write-object ":" print nl + r> print-error ; TUPLE: require-all-error vocabs ; : require-all-error ( vocabs -- ) + [ vocab-name ] map \ require-all-error construct-boa throw ; M: require-all-error summary @@ -167,7 +169,6 @@ M: require-all-error summary [ [ require ] [ 2array , ] recover ] each ] { } make dup empty? [ drop ] [ - "==== LOAD ERRORS:" print dup [ nl load-error. ] assoc-each keys require-all-error ] if diff --git a/extra/asn1/asn1-tests.factor b/extra/asn1/asn1-tests.factor index 1c9bc79d76..329ba8256d 100755 --- a/extra/asn1/asn1-tests.factor +++ b/extra/asn1/asn1-tests.factor @@ -5,11 +5,11 @@ USING: asn1 asn1.ldap io io.streams.string tools.test ; ] unit-test [ "testing" ] [ - "\u0004\u0007testing" [ asn-syntax read-ber ] with-stream + "\u000004\u000007testing" [ asn-syntax read-ber ] with-stream ] unit-test [ { 1 { 3 "Administrator" "ad_is_bogus" } } ] [ - "0$\u0002\u0001\u0001`\u001f\u0002\u0001\u0003\u0004\rAdministrator\u0080\u000bad_is_bogus" + "0$\u000002\u000001\u000001`\u00001f\u000002\u000001\u000003\u000004\rAdministrator\u000080\u00000bad_is_bogus" [ asn-syntax read-ber ] with-stream ] unit-test diff --git a/extra/concurrency/concurrency-tests.factor b/extra/concurrency/concurrency-tests.factor old mode 100644 new mode 100755 index 2f9b6605d7..b6f62d1779 --- a/extra/concurrency/concurrency-tests.factor +++ b/extra/concurrency/concurrency-tests.factor @@ -6,6 +6,8 @@ namespaces tools.test continuations dlists strings math words match quotations concurrency.private ; IN: temporary +[ ] [ self process-mailbox mailbox-data dlist-delete-all ] unit-test + [ V{ 1 2 3 } ] [ 0 make-mailbox diff --git a/extra/concurrency/concurrency.factor b/extra/concurrency/concurrency.factor old mode 100644 new mode 100755 index 8d842f15d0..cf44ab125c --- a/extra/concurrency/concurrency.factor +++ b/extra/concurrency/concurrency.factor @@ -73,7 +73,7 @@ PRIVATE> : mailbox-get?* ( pred mailbox timeout -- obj ) 2over >r >r (mailbox-block-unless-pred) r> r> - mailbox-data delete-node ; inline + mailbox-data delete-node-if ; inline : mailbox-get? ( pred mailbox -- obj ) f mailbox-get?* ; diff --git a/extra/hardware-info/windows/ce/ce.factor b/extra/hardware-info/windows/ce/ce.factor index 8923d86b03..f671ea9426 100755 --- a/extra/hardware-info/windows/ce/ce.factor +++ b/extra/hardware-info/windows/ce/ce.factor @@ -10,25 +10,25 @@ T{ wince-os } os set-global "MEMORYSTATUS" heap-size over set-MEMORYSTATUS-dwLength [ GlobalMemoryStatus ] keep ; -M: wince cpus ( -- n ) 1 ; +M: wince-os cpus ( -- n ) 1 ; -M: wince memory-load ( -- n ) +M: wince-os memory-load ( -- n ) memory-status MEMORYSTATUS-dwMemoryLoad ; -M: wince physical-mem ( -- n ) +M: wince-os physical-mem ( -- n ) memory-status MEMORYSTATUS-dwTotalPhys ; -M: wince available-mem ( -- n ) +M: wince-os available-mem ( -- n ) memory-status MEMORYSTATUS-dwAvailPhys ; -M: wince total-page-file ( -- n ) +M: wince-os total-page-file ( -- n ) memory-status MEMORYSTATUS-dwTotalPageFile ; -M: wince available-page-file ( -- n ) +M: wince-os available-page-file ( -- n ) memory-status MEMORYSTATUS-dwAvailPageFile ; -M: wince total-virtual-mem ( -- n ) +M: wince-os total-virtual-mem ( -- n ) memory-status MEMORYSTATUS-dwTotalVirtual ; -M: wince available-virtual-mem ( -- n ) +M: wince-os available-virtual-mem ( -- n ) memory-status MEMORYSTATUS-dwAvailVirtual ; diff --git a/extra/http/server/templating/templating.factor b/extra/http/server/templating/templating.factor index f5de4664a1..dc83562600 100755 --- a/extra/http/server/templating/templating.factor +++ b/extra/http/server/templating/templating.factor @@ -32,17 +32,18 @@ M: template-lexer skip-word DEFER: <% delimiter : check-<% ( lexer -- col ) - "<%" over line-text rot lexer-column start* ; + "<%" over lexer-line-text rot lexer-column start* ; : found-<% ( accum lexer col -- accum ) [ - over line-text >r >r lexer-column r> r> subseq parsed + over lexer-line-text + >r >r lexer-column r> r> subseq parsed \ write-html parsed ] 2keep 2 + swap set-lexer-column ; : still-looking ( accum lexer -- accum ) [ - dup line-text swap lexer-column tail + dup lexer-line-text swap lexer-column tail parsed \ print-html parsed ] keep next-line ; diff --git a/extra/ldap/libldap/libldap.factor b/extra/ldap/libldap/libldap.factor old mode 100644 new mode 100755 index 6113fe5b7e..492aed1a54 --- a/extra/ldap/libldap/libldap.factor +++ b/extra/ldap/libldap/libldap.factor @@ -40,9 +40,9 @@ IN: ldap.libldap : LDAP_RES_UNSOLICITED 0 ; inline ! how many messages to retrieve results for -: LDAP_MSG_ONE HEX: 00 ; inline -: LDAP_MSG_ALL HEX: 01 ; inline -: LDAP_MSG_RECEIVED HEX: 02 ; inline +: LDAP_MSG_ONE HEX: 00 ; inline +: LDAP_MSG_ALL HEX: 01 ; inline +: LDAP_MSG_RECEIVED HEX: 02 ; inline ! the possible result types returned : LDAP_RES_BIND HEX: 61 ; inline @@ -71,7 +71,7 @@ IN: ldap.libldap { HEX: 79 "LDAP_RES_EXTENDED_PARTIAL" } } ; -: LDAP_OPT_PROTOCOL_VERSION HEX: 0011 ; inline +: LDAP_OPT_PROTOCOL_VERSION HEX: 0011 ; inline C-STRUCT: ldap { "char" "ld_lberoptions" } diff --git a/extra/math/constants/constants-docs.factor b/extra/math/constants/constants-docs.factor index 653444376a..42cdf0e8f1 100755 --- a/extra/math/constants/constants-docs.factor +++ b/extra/math/constants/constants-docs.factor @@ -4,7 +4,7 @@ IN: math.constants ARTICLE: "math-constants" "Constants" "Standard mathematical constants:" { $subsection e } -{ $subsection gamma } +{ $subsection euler } { $subsection phi } { $subsection pi } "Various limits:" @@ -17,7 +17,7 @@ ABOUT: "math-constants" HELP: e { $values { "e" "base of natural logarithm" } } ; -HELP: gamma +HELP: euler { $values { "gamma" "Euler-Mascheroni constant" } } { $description "The Euler-Mascheroni constant, also called \"Euler's constant\" or \"the Euler constant\"." } ; diff --git a/extra/math/constants/constants.factor b/extra/math/constants/constants.factor index c4abeca0eb..c207eaa63c 100755 --- a/extra/math/constants/constants.factor +++ b/extra/math/constants/constants.factor @@ -3,7 +3,7 @@ IN: math.constants : e ( -- e ) 2.7182818284590452354 ; inline -: gamma ( -- gamma ) 0.57721566490153286060 ; inline +: euler ( -- gamma ) 0.57721566490153286060 ; inline : phi ( -- phi ) 1.61803398874989484820 ; inline : pi ( -- pi ) 3.14159265358979323846 ; inline : epsilon ( -- epsilon ) 2.2204460492503131e-16 ; inline diff --git a/extra/math/matrices/elimination/elimination.factor b/extra/math/matrices/elimination/elimination.factor index 73f6dd7e96..8ac9771767 100755 --- a/extra/math/matrices/elimination/elimination.factor +++ b/extra/math/matrices/elimination/elimination.factor @@ -1,7 +1,7 @@ -! Copyright (C) 2006, 2007 Slava Pestov. +! Copyright (C) 2006, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: kernel math math.vectors math.matrices namespaces -sequences parser ; +sequences ; IN: math.matrices.elimination SYMBOL: matrix @@ -20,6 +20,9 @@ SYMBOL: matrix : cols ( -- n ) 0 nth-row length ; +: skip ( i seq quot -- n ) + over >r find* drop r> length or ; inline + : first-col ( row# -- n ) #! First non-zero column 0 swap nth-row [ zero? not ] skip ; diff --git a/extra/nehe/5/5.factor b/extra/nehe/5/5.factor old mode 100644 new mode 100755 index a792f04479..31a7d059ae --- a/extra/nehe/5/5.factor +++ b/extra/nehe/5/5.factor @@ -108,10 +108,12 @@ M: nehe5-gadget draw-gadget* ( gadget -- ) : nehe5-update-thread ( gadget -- ) dup nehe5-gadget-quit? [ + drop + ] [ redraw-interval sleep dup relayout-1 nehe5-update-thread - ] unless ; + ] if ; M: nehe5-gadget graft* ( gadget -- ) [ f swap set-nehe5-gadget-quit? ] keep diff --git a/extra/openssl/libcrypto/libcrypto.factor b/extra/openssl/libcrypto/libcrypto.factor old mode 100644 new mode 100755 index 52cb06f62e..8378a11956 --- a/extra/openssl/libcrypto/libcrypto.factor +++ b/extra/openssl/libcrypto/libcrypto.factor @@ -49,7 +49,7 @@ C-STRUCT: bio : BIO_CLOSE HEX: 01 ; inline : RSA_3 HEX: 3 ; inline -: RSA_F4 HEX: 10001 ; inline +: RSA_F4 HEX: 10001 ; inline : BIO_C_SET_SSL 109 ; inline : BIO_C_GET_SSL 110 ; inline diff --git a/extra/openssl/openssl-tests.factor b/extra/openssl/openssl-tests.factor old mode 100644 new mode 100755 index f4576dca19..c40bc5628b --- a/extra/openssl/openssl-tests.factor +++ b/extra/openssl/openssl-tests.factor @@ -1,6 +1,6 @@ USING: alien alien.c-types assocs bit-arrays hashtables io io.files io.sockets kernel mirrors openssl.libcrypto openssl.libssl -namespaces math math.parser openssl prettyprint sequences tools.test unix ; +namespaces math math.parser openssl prettyprint sequences tools.test ; ! ========================================================= ! Some crypto functions (still to be turned into words) diff --git a/extra/openssl/openssl.factor b/extra/openssl/openssl.factor old mode 100644 new mode 100755 index 3b5474ea9f..bfa7f32594 --- a/extra/openssl/openssl.factor +++ b/extra/openssl/openssl.factor @@ -4,7 +4,7 @@ ! Tested with OpenSSL 0.9.8a_0 on Mac OS X 10.4.9 PowerPC USING: alien alien.c-types assocs kernel libc namespaces -openssl.libcrypto openssl.libssl sequences unix ; +openssl.libcrypto openssl.libssl sequences ; IN: openssl diff --git a/extra/partial-continuations/partial-continuations.factor b/extra/partial-continuations/partial-continuations.factor old mode 100644 new mode 100755 index 0dce7c2390..b80e3a9ddb --- a/extra/partial-continuations/partial-continuations.factor +++ b/extra/partial-continuations/partial-continuations.factor @@ -6,7 +6,7 @@ USING: kernel continuations arrays sequences quotations ; : breset ( quot -- ) [ 1array swap keep first continue-with ] callcc1 nip ; -: (bshift) ( v r k -- ) +: (bshift) ( v r k -- obj ) >r dup first -rot r> [ rot set-first @@ -19,4 +19,4 @@ USING: kernel continuations arrays sequences quotations ; over >r [ (bshift) ] 2curry swap call r> first continue-with - ] callcc1 2nip ; + ] callcc1 2nip ; inline diff --git a/extra/random-tester/random-tester.factor b/extra/random-tester/random-tester.factor old mode 100644 new mode 100755 index c3a1ecbec4..8704687e34 --- a/extra/random-tester/random-tester.factor +++ b/extra/random-tester/random-tester.factor @@ -17,9 +17,9 @@ TUPLE: random-tester-error ; : test-compiler ! ( data... quot -- ... ) errored off dup quot set - datastack clone >vector dup pop* before set - [ call ] catch drop - datastack clone after set + datastack 1 head* before set + [ call ] [ drop ] recover + datastack after set clear before get [ ] each quot get [ compile-call ] [ errored on ] recover ; diff --git a/extra/regexp/regexp.factor b/extra/regexp/regexp.factor index ef88e84f05..fe1d87d9e9 100755 --- a/extra/regexp/regexp.factor +++ b/extra/regexp/regexp.factor @@ -77,7 +77,7 @@ PRIVATE> : 'hex' ( -- parser ) "x" token 'hex-digit' 2 exactly-n &> - "u" token 'hex-digit' 4 exactly-n &> <|> + "u" token 'hex-digit' 6 exactly-n &> <|> [ hex> ] <@ ; : satisfy-tokens ( assoc -- parser ) diff --git a/extra/serialize/serialize-tests.factor b/extra/serialize/serialize-tests.factor old mode 100644 new mode 100755 index a713840a20..e0ecb5393a --- a/extra/serialize/serialize-tests.factor +++ b/extra/serialize/serialize-tests.factor @@ -10,8 +10,6 @@ TUPLE: serialize-test a b ; C: serialize-test -: CURRY< \ > parse-until first2 curry parsed ; parsing - : objects { f @@ -33,7 +31,7 @@ C: serialize-test B{ 50 13 55 64 1 } ?{ t f t f f t f } F{ 1.0 3.0 4.0 1.0 2.35 0.33 } - CURRY< 1 [ 2 ] > + << 1 [ 2 ] curry parsed >> { { "a" "bc" } { "de" "fg" } } H{ { "a" "bc" } { "de" "fg" } } } ; diff --git a/extra/state-parser/state-parser-tests.factor b/extra/state-parser/state-parser-tests.factor old mode 100644 new mode 100755 index ff8ac91513..4e1ecaddfc --- a/extra/state-parser/state-parser-tests.factor +++ b/extra/state-parser/state-parser-tests.factor @@ -1,4 +1,4 @@ -USING: tools.test state-parser kernel io strings ; +USING: tools.test state-parser kernel io strings ascii ; [ "hello" ] [ "hello" [ rest ] string-parse ] unit-test [ 2 4 ] [ "12\n123" [ rest drop get-line get-column ] string-parse ] unit-test diff --git a/extra/tuple-syntax/tuple-syntax-tests.factor b/extra/tuple-syntax/tuple-syntax-tests.factor old mode 100644 new mode 100755 index b16c5b337d..0a9711c446 --- a/extra/tuple-syntax/tuple-syntax-tests.factor +++ b/extra/tuple-syntax/tuple-syntax-tests.factor @@ -1,4 +1,5 @@ USING: tools.test tuple-syntax ; +IN: temporary TUPLE: foo bar baz ; diff --git a/extra/tuple-syntax/tuple-syntax.factor b/extra/tuple-syntax/tuple-syntax.factor old mode 100644 new mode 100755 index 6082f529ac..2f0ba6bde5 --- a/extra/tuple-syntax/tuple-syntax.factor +++ b/extra/tuple-syntax/tuple-syntax.factor @@ -1,4 +1,5 @@ -USING: kernel sequences slots parser words classes ; +USING: kernel sequences slots parser words classes +slots.private ; IN: tuple-syntax ! TUPLE: foo bar baz ; @@ -7,15 +8,15 @@ IN: tuple-syntax : parse-object ( -- object ) scan-word dup parsing? [ V{ } clone swap execute first ] when ; -: parse-slot-writer ( tuple -- slot-setter ) +: parse-slot-writer ( tuple -- slot# ) scan dup "}" = [ 2drop f ] [ 1 head* swap class "slots" word-prop - [ slot-spec-name = ] with find nip slot-spec-writer + [ slot-spec-name = ] with find nip slot-spec-offset ] if ; : parse-slots ( accum tuple -- accum tuple ) dup parse-slot-writer - [ parse-object pick rot execute parse-slots ] when* ; + [ parse-object pick rot set-slot parse-slots ] when* ; : TUPLE{ scan-word construct-empty parse-slots parsed ; parsing diff --git a/extra/ui/gadgets/editors/editors.factor b/extra/ui/gadgets/editors/editors.factor index 00b574f853..e2df6a343b 100755 --- a/extra/ui/gadgets/editors/editors.factor +++ b/extra/ui/gadgets/editors/editors.factor @@ -249,11 +249,11 @@ M: editor gadget-text* editor-string % ; : extend-selection ( editor -- ) dup request-focus dup editor-caret click-loc ; -: mouse-elt ( -- elelement ) +: mouse-elt ( -- element ) hand-click# get { + { 1 T{ one-char-elt } } { 2 T{ one-word-elt } } - { 3 T{ one-line-elt } } - } at T{ one-char-elt } or ; + } at T{ one-line-elt } or ; : drag-direction? ( loc editor -- ? ) editor-mark* <=> 0 < ; diff --git a/extra/xmode/utilities/utilities-tests.factor b/extra/xmode/utilities/utilities-tests.factor old mode 100644 new mode 100755 index 89cb588336..713700bf7a --- a/extra/xmode/utilities/utilities-tests.factor +++ b/extra/xmode/utilities/utilities-tests.factor @@ -1,6 +1,6 @@ IN: temporary -USING: xmode.utilities tools.test xml xml.data -kernel strings vectors sequences io.files prettyprint assocs ; +USING: xmode.utilities tools.test xml xml.data kernel strings +vectors sequences io.files prettyprint assocs unicode.case ; [ "hi" 3 ] [ { 1 2 3 4 5 6 7 8 } [ H{ { 3 "hi" } } at ] map-find From 2a417f4a9c79f02fa1af909337c2e669910cf42b Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 6 Feb 2008 19:36:53 -0600 Subject: [PATCH 142/269] add with-file-in with-file-out with-file-appender --- core/io/files/files.factor | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/io/files/files.factor b/core/io/files/files.factor index 9a99090699..8c9bd8f2e9 100755 --- a/core/io/files/files.factor +++ b/core/io/files/files.factor @@ -169,3 +169,12 @@ PRIVATE> : file-contents ( path -- str ) dup swap file-length [ stream-copy ] keep >string ; + +: with-file-in ( path quot -- ) + >r r> with-stream ; inline + +: with-file-out ( path quot -- ) + >r r> with-stream ; inline + +: with-file-appender ( path quot -- ) + >r r> with-stream ; inline From 99411495c2eaa5e4a6a7501cf8c3dbed2bed80b7 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 6 Feb 2008 19:49:07 -0600 Subject: [PATCH 143/269] add http-update to work around firewalls --- misc/factor.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/misc/factor.sh b/misc/factor.sh index 26ebd04531..f0eb232821 100755 --- a/misc/factor.sh +++ b/misc/factor.sh @@ -200,6 +200,12 @@ git_pull_factorcode() { check_ret git } +http_git_pull_factorcode() { + echo "Updating the git repository from factorcode.org..." + git pull http://factorcode.org/git/factor.git master + check_ret git +} + cd_factor() { cd factor check_ret cd @@ -271,6 +277,7 @@ install() { bootstrap } + update() { get_config_info git_pull_factorcode @@ -278,6 +285,13 @@ update() { make_factor } +http_update() { + get_config_info + http_git_pull_factorcode + make_clean + make_factor +} + update_bootstrap() { delete_boot_images get_boot_image @@ -310,6 +324,7 @@ case "$1" in self-update) update; make_boot_image; bootstrap;; quick-update) update; refresh_image ;; update) update; update_bootstrap ;; + http-update) http_update; update_bootstrap ;; bootstrap) get_config_info; bootstrap ;; wget-bootstrap) get_config_info; delete_boot_images; get_boot_image; bootstrap ;; *) usage ;; From 5f997fe2c7c5c071a0f7975170daa6d9f4256aef Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 20:04:09 -0600 Subject: [PATCH 144/269] Make extra/unix load on Windows --- extra/unix/unix.factor | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/extra/unix/unix.factor b/extra/unix/unix.factor index d32fc25eab..59141c1940 100755 --- a/extra/unix/unix.factor +++ b/extra/unix/unix.factor @@ -220,7 +220,8 @@ FUNCTION: pid_t waitpid ( pid_t wpid, int* status, int options ) ; FUNCTION: ssize_t write ( int fd, void* buf, size_t nbytes ) ; { - { [ linux? ] [ "unix.linux" ] } - { [ bsd? ] [ "unix.bsd" ] } - { [ solaris? ] [ "unix.solaris" ] } -} cond require + { [ linux? ] [ "unix.linux" require ] } + { [ bsd? ] [ "unix.bsd" require ] } + { [ solaris? ] [ "unix.solaris" require ] } + { [ t ] [ ] } +} cond From 93eb74476e776f044283ce61354852037a5c0cb1 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 6 Feb 2008 20:04:46 -0600 Subject: [PATCH 145/269] add with-file-in docs, update a couple of usages --- core/io/files/files-docs.factor | 15 +++++++++++++++ extra/tar/tar.factor | 5 ++--- extra/tools/browser/browser.factor | 4 ++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/core/io/files/files-docs.factor b/core/io/files/files-docs.factor index 0b9a748eb8..99f2d42542 100755 --- a/core/io/files/files-docs.factor +++ b/core/io/files/files-docs.factor @@ -52,6 +52,21 @@ HELP: { $description "Outputs an output stream for writing to the specified pathname. The stream begins writing at the end of the file." } { $errors "Throws an error if the file cannot be opened for writing." } ; +HELP: with-file-in +{ $values { "path" "a pathname string" } { "quot" "a quotation" } } +{ $description "Opens a file for reading and calls the quotation using " { $link with-stream } "." } +{ $errors "Throws an error if the file is unreadable." } ; + +HELP: with-file-out +{ $values { "path" "a pathname string" } { "quot" "a quotation" } } +{ $description "Opens a file for writing and calls the quotation using " { $link with-stream } "." } +{ $errors "Throws an error if the file cannot be opened for writing." } ; + +HELP: with-file-appender +{ $values { "path" "a pathname string" } { "quot" "a quotation" } } +{ $description "Opens a file for appending and calls the quotation using " { $link with-stream } "." } +{ $errors "Throws an error if the file cannot be opened for writing." } ; + HELP: cwd { $values { "path" "a pathname string" } } { $description "Outputs the current working directory of the Factor process." } diff --git a/extra/tar/tar.factor b/extra/tar/tar.factor index 20e997185d..e15d9511a3 100755 --- a/extra/tar/tar.factor +++ b/extra/tar/tar.factor @@ -236,10 +236,9 @@ TUPLE: unimplemented-typeflag header ; ] when* ; : parse-tar ( path -- obj ) - [ + [ "tar-test" resource-path base-dir set global [ nl nl nl "Starting to parse .tar..." print flush ] bind global [ "Expanding to: " write base-dir get . flush ] bind (parse-tar) - ] with-stream ; - + ] with-file-out ; diff --git a/extra/tools/browser/browser.factor b/extra/tools/browser/browser.factor index 7aefbc8aaa..167c238069 100755 --- a/extra/tools/browser/browser.factor +++ b/extra/tools/browser/browser.factor @@ -10,7 +10,7 @@ IN: tools.browser MEMO: (vocab-file-contents) ( path -- lines ) ?resource-path dup exists? - [ lines ] [ drop f ] if ; + [ file-lines ] [ drop f ] if ; : vocab-file-contents ( vocab name -- seq ) vocab-path+ dup [ (vocab-file-contents) ] when ; @@ -18,7 +18,7 @@ MEMO: (vocab-file-contents) ( path -- lines ) : set-vocab-file-contents ( seq vocab name -- ) dupd vocab-path+ [ ?resource-path - [ [ print ] each ] with-stream + [ [ print ] each ] with-file-out ] [ "The " swap vocab-name " vocabulary was not loaded from the file system" From f3c8bd266b0300a920fd8896372177504aa6984c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 20:05:03 -0600 Subject: [PATCH 146/269] Improved syntax for ratios --- core/math/parser/parser-tests.factor | 10 --- core/math/parser/parser.factor | 89 ++++++++++++++++++--------- extra/math/ratios/ratios-tests.factor | 5 ++ extra/math/ratios/ratios.factor | 1 + 4 files changed, 66 insertions(+), 39 deletions(-) diff --git a/core/math/parser/parser-tests.factor b/core/math/parser/parser-tests.factor index 7c30012a19..226e47090a 100755 --- a/core/math/parser/parser-tests.factor +++ b/core/math/parser/parser-tests.factor @@ -95,16 +95,6 @@ unit-test [ f ] [ "\0." string>number ] unit-test -! [ t ] [ -! { "1.0/0.0" "-1.0/0.0" "0.0/0.0" } -! [ dup string>number number>string = ] all? -! ] unit-test -! -! [ t ] [ -! { 1.0/0.0 -1.0/0.0 0.0/0.0 } -! [ dup number>string string>number = ] all? -! ] unit-test - [ 1 1 >base ] must-fail [ 1 0 >base ] must-fail [ 1 -1 >base ] must-fail diff --git a/core/math/parser/parser.factor b/core/math/parser/parser.factor index 7f0404812d..73b4a725d2 100755 --- a/core/math/parser/parser.factor +++ b/core/math/parser/parser.factor @@ -4,12 +4,6 @@ USING: kernel math.private namespaces sequences strings arrays combinators splitting math assocs ; IN: math.parser -DEFER: base> - -: string>ratio ( str radix -- a/b ) - >r "/" split1 r> tuck base> >r base> r> - 2dup and [ / ] [ 2drop f ] if ; - : digit> ( ch -- n ) H{ { CHAR: 0 0 } @@ -36,30 +30,54 @@ DEFER: base> { CHAR: f 15 } } at ; -: digits>integer ( radix seq -- n ) - 0 rot [ swapd * + ] curry reduce ; - -: valid-digits? ( radix seq -- ? ) - { - { [ dup empty? ] [ 2drop f ] } - { [ f over memq? ] [ 2drop f ] } - { [ t ] [ swap [ < ] curry all? ] } - } cond ; - : string>digits ( str -- digits ) [ digit> ] { } map-as ; -: string>integer ( str radix -- n/f ) - swap "-" ?head >r - string>digits 2dup valid-digits? - [ digits>integer r> [ neg ] when ] [ r> 3drop f ] if ; +DEFER: base> + +) ( str -- n ) radix get base> ; + +: whole-part ( str -- m n ) + "+" split1 >r (base>) r> + dup [ (base>) ] [ drop 0 swap ] if ; + +: string>ratio ( str -- a/b ) + "/" split1 (base>) >r whole-part r> + 3dup and and [ / + ] [ 3drop f ] if ; + +: digits>integer ( seq -- n ) + 0 radix get [ swapd * + ] curry reduce ; + +: valid-digits? ( seq -- ? ) + { + { [ dup empty? ] [ drop f ] } + { [ f over memq? ] [ drop f ] } + { [ t ] [ radix get [ < ] curry all? ] } + } cond ; + +: string>integer ( str -- n/f ) + string>digits dup valid-digits? + [ digits>integer ] [ drop f ] if ; + +PRIVATE> : base> ( str radix -- n/f ) - { - { [ CHAR: / pick member? ] [ string>ratio ] } - { [ CHAR: . pick member? ] [ drop string>float ] } - { [ t ] [ string>integer ] } - } cond ; + [ + "-" ?head >r + { + { [ CHAR: / over member? ] [ string>ratio ] } + { [ CHAR: . over member? ] [ string>float ] } + { [ t ] [ string>integer ] } + } cond + r> [ dup [ neg ] when ] when + ] with-radix ; : string>number ( str -- n/f ) 10 base> ; : bin> ( str -- n/f ) 2 base> ; @@ -74,8 +92,16 @@ DEFER: base> dup >r /mod >digit , dup 0 > [ r> integer, ] [ r> 2drop ] if ; +PRIVATE> + GENERIC# >base 1 ( n radix -- str ) +base) ( n -- str ) radix get >base ; + +PRIVATE> + M: integer >base [ over 0 < [ @@ -87,10 +113,15 @@ M: integer >base M: ratio >base [ - over numerator over >base % - CHAR: / , - swap denominator swap >base % - ] "" make ; + [ + dup 0 < [ "-" % neg ] when + 1 /mod + >r dup zero? [ drop ] [ (>base) % "+" % ] if r> + dup numerator (>base) % + "/" % + denominator (>base) % + ] "" make + ] with-radix ; : fix-float ( str -- newstr ) { diff --git a/extra/math/ratios/ratios-tests.factor b/extra/math/ratios/ratios-tests.factor index 79b0b21d28..858a7b0544 100755 --- a/extra/math/ratios/ratios-tests.factor +++ b/extra/math/ratios/ratios-tests.factor @@ -105,3 +105,8 @@ unit-test [ "33/100" ] [ "66/200" string>number number>string ] unit-test + +[ 3 ] [ "1+1/2" string>number 2 * ] unit-test +[ -3 ] [ "-1+1/2" string>number 2 * ] unit-test +[ "2+1/7" ] [ 1 7 / 2 + number>string ] unit-test +[ "1/8" ] [ 1 8 / number>string ] unit-test diff --git a/extra/math/ratios/ratios.factor b/extra/math/ratios/ratios.factor index 954fd8dd20..5d07bd046f 100755 --- a/extra/math/ratios/ratios.factor +++ b/extra/math/ratios/ratios.factor @@ -48,3 +48,4 @@ M: ratio * 2>fraction * >r * r> / ; M: ratio / scale / ; M: ratio /i scale /i ; M: ratio mod 2dup >r >r /i r> r> rot * - ; +M: ratio /mod [ /i ] 2keep mod ; From 01b1ba0f88836a6543b8b17a8cb83ae0f0cc4c23 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Wed, 6 Feb 2008 20:05:52 -0600 Subject: [PATCH 147/269] Temporarily use onigirihouse as the primary --- extra/builder/builder.factor | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extra/builder/builder.factor b/extra/builder/builder.factor index 2acdbc3294..5bfd5e01cf 100755 --- a/extra/builder/builder.factor +++ b/extra/builder/builder.factor @@ -69,7 +69,8 @@ VAR: stamp "git" "pull" "--no-summary" - "git://factorcode.org/git/factor.git" + ! "git://factorcode.org/git/factor.git" + "http://dharmatech.onigirihouse.com/factor.git" "master" } run-process process-status From 7534d84d2769fa7d83a78dfa9ec00bca79db38b5 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 21:15:33 -0600 Subject: [PATCH 148/269] Refactor tools.test --- extra/tools/test/test.factor | 41 +++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/extra/tools/test/test.factor b/extra/tools/test/test.factor index 9590f32539..d761df35d2 100755 --- a/extra/tools/test/test.factor +++ b/extra/tools/test/test.factor @@ -61,35 +61,42 @@ M: expected-error summary dup first print-error "Traceback" swap third write-object ; -: failures. ( path failures -- ) - "Failing tests in " write swap . - [ nl failure. nl ] each ; - -: run-tests ( seq -- ) - dup empty? [ drop "==== NOTHING TO TEST" print ] [ - [ dup run-test ] { } map>assoc - [ second empty? not ] subset +: failures. ( assoc -- ) + dup [ nl dup empty? [ drop "==== ALL TESTS PASSED" print ] [ "==== FAILING TESTS:" print - [ nl failures. ] assoc-each + [ + nl + "Failing tests in " write swap . + [ nl failure. nl ] each + ] assoc-each ] if + ] [ + drop "==== NOTHING TO TEST" print ] if ; -: run-vocab-tests ( vocabs -- ) - [ vocab-tests-path ] map - [ dup [ ?resource-path exists? ] when ] subset - run-tests ; +: run-vocab-tests ( vocabs -- failures ) + dup empty? [ f ] [ + [ dup run-test ] { } map>assoc + [ second empty? not ] subset + ] if ; -: test ( prefix -- ) +: run-tests ( prefix -- failures ) child-vocabs [ vocab-source-loaded? ] subset + [ vocab-tests-path ] map + [ dup [ ?resource-path exists? ] when ] subset run-vocab-tests ; -: test-all ( -- ) "" test ; +: test ( prefix -- ) + run-tests failures. ; -: test-changes ( -- ) - "" to-refresh dupd do-refresh run-vocab-tests ; +: run-all-tests ( prefix -- failures ) + "" run-tests ; + +: test-all ( -- ) + run-all-tests failures. ; From 2541c62e291ad04de93fadbac7514820bcae657c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 21:15:47 -0600 Subject: [PATCH 149/269] Fix code for math.parser changes --- core/math/parser/parser.factor | 8 ++++---- core/syntax/syntax-docs.factor | 4 +++- extra/json/reader/reader.factor | 2 +- extra/math/ratios/ratios-docs.factor | 1 + extra/math/text/english/english.factor | 2 +- extra/parser-combinators/simple/simple.factor | 2 +- extra/peg/peg.factor | 2 +- extra/project-euler/024/024.factor | 2 +- extra/project-euler/032/032.factor | 10 +++++----- extra/project-euler/035/035.factor | 2 +- extra/project-euler/037/037.factor | 2 +- extra/project-euler/038/038.factor | 2 +- extra/project-euler/040/040.factor | 2 +- extra/random-tester/safe-words/safe-words.factor | 2 +- 14 files changed, 23 insertions(+), 20 deletions(-) mode change 100644 => 100755 extra/json/reader/reader.factor mode change 100644 => 100755 extra/math/text/english/english.factor mode change 100644 => 100755 extra/peg/peg.factor mode change 100644 => 100755 extra/project-euler/024/024.factor mode change 100644 => 100755 extra/project-euler/032/032.factor mode change 100644 => 100755 extra/project-euler/035/035.factor mode change 100644 => 100755 extra/project-euler/037/037.factor mode change 100644 => 100755 extra/project-euler/038/038.factor mode change 100644 => 100755 extra/project-euler/040/040.factor mode change 100644 => 100755 extra/random-tester/safe-words/safe-words.factor diff --git a/core/math/parser/parser.factor b/core/math/parser/parser.factor index 73b4a725d2..64ce296a0b 100755 --- a/core/math/parser/parser.factor +++ b/core/math/parser/parser.factor @@ -33,6 +33,9 @@ IN: math.parser : string>digits ( str -- digits ) [ digit> ] { } map-as ; +: digits>integer ( seq radix -- n ) + 0 swap [ swapd * + ] curry reduce ; + DEFER: base> ) >r whole-part r> 3dup and and [ / + ] [ 3drop f ] if ; -: digits>integer ( seq -- n ) - 0 radix get [ swapd * + ] curry reduce ; - : valid-digits? ( seq -- ? ) { { [ dup empty? ] [ drop f ] } @@ -64,7 +64,7 @@ SYMBOL: radix : string>integer ( str -- n/f ) string>digits dup valid-digits? - [ digits>integer ] [ drop f ] if ; + [ radix get digits>integer ] [ drop f ] if ; PRIVATE> diff --git a/core/syntax/syntax-docs.factor b/core/syntax/syntax-docs.factor index 2e5b41cd8d..9ccfd2efcd 100755 --- a/core/syntax/syntax-docs.factor +++ b/core/syntax/syntax-docs.factor @@ -47,11 +47,13 @@ ARTICLE: "syntax-integers" "Integer syntax" "More information on integers can be found in " { $link "integers" } "." ; ARTICLE: "syntax-ratios" "Ratio syntax" -"The printed representation of a ratio is a pair of integers separated by a slash (/). No intermediate whitespace is permitted. Either integer may be signed, however the ratio will be normalized into a form where the denominator is positive and the greatest common divisor of the two terms is 1." +"The printed representation of a ratio is a pair of integers separated by a slash (/), prefixed by an optional whole number part followed by a plus (+). No intermediate whitespace is permitted. Here are some examples:" { $code "75/33" "1/10" "-5/-6" + "1+1/3" + "-10+1/7" } "More information on ratios can be found in " { $link "rationals" } ; diff --git a/extra/json/reader/reader.factor b/extra/json/reader/reader.factor old mode 100644 new mode 100755 index 105989ab93..b136012433 --- a/extra/json/reader/reader.factor +++ b/extra/json/reader/reader.factor @@ -104,7 +104,7 @@ LAZY: 'digit1-9' ( -- parser ) LAZY: 'digit0-9' ( -- parser ) [ digit? ] satisfy [ digit> ] <@ ; -: decimal>integer ( seq -- num ) 10 swap digits>integer ; +: decimal>integer ( seq -- num ) 10 digits>integer ; LAZY: 'int' ( -- parser ) 'zero' diff --git a/extra/math/ratios/ratios-docs.factor b/extra/math/ratios/ratios-docs.factor index d996acaf1f..b780a7c322 100755 --- a/extra/math/ratios/ratios-docs.factor +++ b/extra/math/ratios/ratios-docs.factor @@ -7,6 +7,7 @@ ARTICLE: "rationals" "Rational numbers" "When we add, subtract or multiply any two integers, the result is always an integer. However, dividing a numerator by a denominator that is not an integral divisor of the denominator yields a ratio:" { $example "1210 11 / ." "110" } { $example "100 330 / ." "10/33" } +{ $example "14 10 / ." "1+2/5" } "Ratios are printed and can be input literally in the form above. Ratios are always reduced to lowest terms by factoring out the greatest common divisor of the numerator and denominator. A ratio with a denominator of 1 becomes an integer. Division with a denominator of 0 throws an error." $nl "Ratios behave just like any other number -- all numerical operations work as you would expect." diff --git a/extra/math/text/english/english.factor b/extra/math/text/english/english.factor old mode 100644 new mode 100755 index 645d7e2054..b77ac725ab --- a/extra/math/text/english/english.factor +++ b/extra/math/text/english/english.factor @@ -33,7 +33,7 @@ SYMBOL: and-needed? : 3digit-groups ( n -- seq ) number>string 3 - [ reverse 10 string>integer ] map ; + [ reverse string>number ] map ; : hundreds-place ( n -- str ) 100 /mod swap dup zero? [ diff --git a/extra/parser-combinators/simple/simple.factor b/extra/parser-combinators/simple/simple.factor index 763f823348..745442610c 100755 --- a/extra/parser-combinators/simple/simple.factor +++ b/extra/parser-combinators/simple/simple.factor @@ -8,7 +8,7 @@ IN: parser-combinators.simple [ digit? ] satisfy [ digit> ] <@ ; : 'integer' ( -- parser ) - 'digit' [ 10 swap digits>integer ] <@ ; + 'digit' [ 10 digits>integer ] <@ ; : 'string' ( -- parser ) [ CHAR: " = ] satisfy diff --git a/extra/peg/peg.factor b/extra/peg/peg.factor old mode 100644 new mode 100755 index 41df8735e5..59a8b63c14 --- a/extra/peg/peg.factor +++ b/extra/peg/peg.factor @@ -343,7 +343,7 @@ MEMO: 'digit' ( -- parser ) [ digit? ] satisfy [ digit> ] action ; MEMO: 'integer' ( -- parser ) - 'digit' repeat1 [ 10 swap digits>integer ] action ; + 'digit' repeat1 [ 10 digits>integer ] action ; MEMO: 'string' ( -- parser ) [ diff --git a/extra/project-euler/024/024.factor b/extra/project-euler/024/024.factor old mode 100644 new mode 100755 index c795fc0169..0cc0c39e07 --- a/extra/project-euler/024/024.factor +++ b/extra/project-euler/024/024.factor @@ -23,7 +23,7 @@ IN: project-euler.024 ! -------- : euler024 ( -- answer ) - 999999 10 permutation 10 swap digits>integer ; + 999999 10 permutation 10 digits>integer ; ! [ euler024 ] 100 ave-time ! 0 ms run / 0 ms GC ave time - 100 trials diff --git a/extra/project-euler/032/032.factor b/extra/project-euler/032/032.factor old mode 100644 new mode 100755 index 2baa6f8714..b8b0758974 --- a/extra/project-euler/032/032.factor +++ b/extra/project-euler/032/032.factor @@ -27,21 +27,21 @@ IN: project-euler.032 integer ] map ; + 9 factorial [ 9 permutation [ 1+ ] map 10 digits>integer ] map ; : 1and4 ( n -- ? ) number>string 1 cut-slice 4 cut-slice - [ 10 string>integer ] 3apply [ * ] dip = ; + [ string>number ] 3apply [ * ] dip = ; : 2and3 ( n -- ? ) number>string 2 cut-slice 3 cut-slice - [ 10 string>integer ] 3apply [ * ] dip = ; + [ string>number ] 3apply [ * ] dip = ; : valid? ( n -- ? ) dup 1and4 swap 2and3 or ; : products ( seq -- m ) - [ number>string 4 tail* 10 string>integer ] map ; + [ number>string 4 tail* string>number ] map ; PRIVATE> @@ -65,7 +65,7 @@ PRIVATE> ! multiplicand/multiplier/product : mmp ( pair -- n ) - first2 2dup * [ number>string ] 3apply 3append 10 string>integer ; + first2 2dup * [ number>string ] 3apply 3append string>number ; PRIVATE> diff --git a/extra/project-euler/035/035.factor b/extra/project-euler/035/035.factor old mode 100644 new mode 100755 index 867bbc44ac..29172111c1 --- a/extra/project-euler/035/035.factor +++ b/extra/project-euler/035/035.factor @@ -38,7 +38,7 @@ IN: project-euler.035 : (circular?) ( seq n -- ? ) dup 0 > [ - 2dup rotate 10 swap digits>integer + 2dup rotate 10 digits>integer prime? [ 1- (circular?) ] [ 2drop f ] if ] [ 2drop t diff --git a/extra/project-euler/037/037.factor b/extra/project-euler/037/037.factor old mode 100644 new mode 100755 index f2d5d17c4d..66b1665037 --- a/extra/project-euler/037/037.factor +++ b/extra/project-euler/037/037.factor @@ -32,7 +32,7 @@ IN: project-euler.037 ] if ; : reverse-digits ( n -- m ) - number>string reverse 10 string>integer ; + number>string reverse string>number ; : l-trunc? ( n -- ? ) reverse-digits 10 /i reverse-digits dup 0 > [ diff --git a/extra/project-euler/038/038.factor b/extra/project-euler/038/038.factor old mode 100644 new mode 100755 index cbe6f2363c..2369db25fb --- a/extra/project-euler/038/038.factor +++ b/extra/project-euler/038/038.factor @@ -36,7 +36,7 @@ IN: project-euler.038 : (concat-product) ( accum n multiplier -- m ) pick length 8 > [ - 2drop 10 swap digits>integer + 2drop 10 digits>integer ] [ [ * number>digits over push-all ] 2keep 1+ (concat-product) ] if ; diff --git a/extra/project-euler/040/040.factor b/extra/project-euler/040/040.factor old mode 100644 new mode 100755 index 8984559265..e2df1df2c9 --- a/extra/project-euler/040/040.factor +++ b/extra/project-euler/040/040.factor @@ -37,7 +37,7 @@ IN: project-euler.040 SBUF" " clone 1 -rot (concat-upto) ; : nth-integer ( n str -- m ) - [ 1- ] dip nth 1string 10 string>integer ; + [ 1- ] dip nth 1string string>number ; PRIVATE> diff --git a/extra/random-tester/safe-words/safe-words.factor b/extra/random-tester/safe-words/safe-words.factor old mode 100644 new mode 100755 index 9bc87a9c5a..ab528786bb --- a/extra/random-tester/safe-words/safe-words.factor +++ b/extra/random-tester/safe-words/safe-words.factor @@ -16,7 +16,7 @@ IN: random-tester.safe-words array? integer? complex? value-ref? ref? key-ref? interval? number? wrapper? tuple? - [-1,1]? between? bignum? both? either? eq? equal? even? fixnum? float? fp-nan? hashtable? interval-contains? interval-subset? interval? key-ref? key? number? odd? pair? power-of-2? ratio? rational? real? subassoc? valid-digits? zero? assoc? curry? vector? callstack? ! clear 3.14 [ assoc? ] compile-1 + [-1,1]? between? bignum? both? either? eq? equal? even? fixnum? float? fp-nan? hashtable? interval-contains? interval-subset? interval? key-ref? key? number? odd? pair? power-of-2? ratio? rational? real? subassoc? zero? assoc? curry? vector? callstack? ! clear 3.14 [ assoc? ] compile-1 2^ not ! arrays resize-array From c1dd7cf855c2f863c44f4d8cb0877e3f854f525c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 21:16:52 -0600 Subject: [PATCH 150/269] Fix Doug's bug --- extra/ui/tools/operations/operations.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/ui/tools/operations/operations.factor b/extra/ui/tools/operations/operations.factor index 2375730a81..fbb4338b17 100755 --- a/extra/ui/tools/operations/operations.factor +++ b/extra/ui/tools/operations/operations.factor @@ -188,7 +188,7 @@ source-editor "These commands operate on the Factor word named by the token at the caret position." \ selected-word [ selected-word ] -[ search ] +[ dup search [ ] [ no-word ] ?if ] define-operation-map interactor From 9271da5070370a0ea4e2c2bada37ddf0bc53c408 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 22:12:44 -0600 Subject: [PATCH 151/269] More cleanups to require-all and unit tests --- core/vocabs/loader/loader.factor | 23 ++++++++++++----- extra/tools/test/test.factor | 44 ++++++++++++++++---------------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/core/vocabs/loader/loader.factor b/core/vocabs/loader/loader.factor index e42dace945..352ef9fe02 100755 --- a/core/vocabs/loader/loader.factor +++ b/core/vocabs/loader/loader.factor @@ -148,10 +148,17 @@ SYMBOL: load-help? dup update-roots dup modified-sources swap modified-docs ; -: load-error. ( vocab error -- ) - "==== " write >r - dup vocab-name swap f >vocab-link write-object ":" print nl - r> print-error ; +: vocab-heading. ( vocab -- ) + nl + "==== " write + dup vocab-name swap f >vocab-link write-object ":" print + nl ; + +: load-error. ( triple -- ) + dup first vocab-heading. + dup second print-error + drop ; + ! third "Traceback" swap write-object ; TUPLE: require-all-error vocabs ; @@ -166,10 +173,14 @@ M: require-all-error summary dup length 1 = [ first require ] [ [ [ - [ [ require ] [ 2array , ] recover ] each + [ + [ require ] + [ error-continuation get 3array , ] + recover + ] each ] { } make dup empty? [ drop ] [ - dup [ nl load-error. ] assoc-each + dup [ load-error. nl ] each keys require-all-error ] if ] with-compiler-errors diff --git a/extra/tools/test/test.factor b/extra/tools/test/test.factor index d761df35d2..09d497aac7 100755 --- a/extra/tools/test/test.factor +++ b/extra/tools/test/test.factor @@ -11,7 +11,8 @@ SYMBOL: failures : ( error what -- triple ) error-continuation get 3array ; -: failure ( error what -- ) failures get push ; +: failure ( error what -- ) + failures get push ; SYMBOL: this-test @@ -45,16 +46,23 @@ M: expected-error summary : ignore-errors ( quot -- ) [ drop ] recover ; inline -: run-test ( path -- failures ) - [ "temporary" forget-vocab ] with-compilation-unit - [ - V{ } clone [ - failures [ - [ run-file ] [ swap failure ] recover - ] with-variable - ] keep - ] keep - [ forget-source ] with-compilation-unit ; +: (run-test) ( vocab -- ) + dup vocab-source-loaded? [ + vocab-tests-path dup [ + dup ?resource-path exists? [ + [ "temporary" forget-vocab ] with-compilation-unit + dup run-file + [ dup forget-source ] with-compilation-unit + ] when + ] when + ] when drop ; + +: run-test ( vocab -- failures ) + V{ } clone [ + failures [ + (run-test) + ] with-variable + ] keep ; : failure. ( triple -- ) dup second . @@ -70,8 +78,7 @@ M: expected-error summary ] [ "==== FAILING TESTS:" print [ - nl - "Failing tests in " write swap . + swap vocab-heading. [ nl failure. nl ] each ] assoc-each ] if @@ -79,19 +86,12 @@ M: expected-error summary drop "==== NOTHING TO TEST" print ] if ; -: run-vocab-tests ( vocabs -- failures ) - dup empty? [ f ] [ +: run-tests ( prefix -- failures ) + child-vocabs dup empty? [ f ] [ [ dup run-test ] { } map>assoc [ second empty? not ] subset ] if ; -: run-tests ( prefix -- failures ) - child-vocabs - [ vocab-source-loaded? ] subset - [ vocab-tests-path ] map - [ dup [ ?resource-path exists? ] when ] subset - run-vocab-tests ; - : test ( prefix -- ) run-tests failures. ; From 5ecf3f722587f95232485c21949b9983bdf549fa Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 6 Feb 2008 22:58:41 -0600 Subject: [PATCH 152/269] Improve unit test documentation and update some tests --- core/bootstrap/image/image-tests.factor | 3 +- core/compiler/test/alien.factor | 6 +- core/compiler/test/redefine.factor | 8 +- core/inference/inference-docs.factor | 11 +- core/inference/inference-tests.factor | 115 +++++++++--------- .../transforms/transforms-tests.factor | 2 +- extra/combinators/lib/lib-tests.factor | 2 +- extra/io/launcher/launcher-tests.factor | 2 +- extra/io/server/server-tests.factor | 4 +- extra/tools/test/test-docs.factor | 77 ++++++++++-- extra/tools/test/test.factor | 17 ++- extra/ui/gadgets/books/books-tests.factor | 2 +- extra/ui/gadgets/buttons/buttons-tests.factor | 3 +- extra/ui/gadgets/editors/editors-tests.factor | 5 +- extra/ui/gadgets/gadgets-tests.factor | 2 +- .../gadgets/scrollers/scrollers-tests.factor | 2 +- extra/ui/tools/browser/browser-tests.factor | 3 +- .../tools/interactor/interactor-tests.factor | 2 +- extra/ui/tools/walker/walker-tests.factor | 2 +- .../ui/tools/workspace/workspace-tests.factor | 2 +- 20 files changed, 172 insertions(+), 98 deletions(-) mode change 100644 => 100755 extra/io/server/server-tests.factor diff --git a/core/bootstrap/image/image-tests.factor b/core/bootstrap/image/image-tests.factor index ea533f0d6f..8c618a8f30 100755 --- a/core/bootstrap/image/image-tests.factor +++ b/core/bootstrap/image/image-tests.factor @@ -1,6 +1,5 @@ IN: temporary -USING: bootstrap.image bootstrap.image.private -tools.test.inference ; +USING: bootstrap.image bootstrap.image.private tools.test ; \ ' must-infer \ write-image must-infer diff --git a/core/compiler/test/alien.factor b/core/compiler/test/alien.factor index dbdbbfc9fa..4adb1c234b 100755 --- a/core/compiler/test/alien.factor +++ b/core/compiler/test/alien.factor @@ -3,7 +3,7 @@ USING: alien alien.c-types alien.syntax compiler kernel namespaces namespaces tools.test sequences inference words arrays parser quotations continuations inference.backend effects namespaces.private io io.streams.string memory system threads -tools.test.inference ; +tools.test ; FUNCTION: void ffi_test_0 ; [ ] [ ffi_test_0 ] unit-test @@ -80,7 +80,7 @@ FUNCTION: tiny ffi_test_17 int x ; : indirect-test-1 "int" { } "cdecl" alien-indirect ; -{ 1 1 } [ indirect-test-1 ] unit-test-effect +{ 1 1 } [ indirect-test-1 ] must-infer-as [ 3 ] [ "ffi_test_1" f dlsym indirect-test-1 ] unit-test @@ -89,7 +89,7 @@ FUNCTION: tiny ffi_test_17 int x ; : indirect-test-2 "int" { "int" "int" } "cdecl" alien-indirect data-gc ; -{ 3 1 } [ indirect-test-2 ] unit-test-effect +{ 3 1 } [ indirect-test-2 ] must-infer-as [ 5 ] [ 2 3 "ffi_test_2" f dlsym indirect-test-2 ] diff --git a/core/compiler/test/redefine.factor b/core/compiler/test/redefine.factor index ab472668c3..9eaf2d1263 100755 --- a/core/compiler/test/redefine.factor +++ b/core/compiler/test/redefine.factor @@ -1,6 +1,6 @@ USING: compiler definitions generic assocs inference math namespaces parser tools.test words kernel sequences arrays io -effects tools.test.inference compiler.units inference.state ; +effects tools.test compiler.units inference.state ; IN: temporary DEFER: x-1 @@ -28,13 +28,13 @@ DEFER: c [ 1 2 1 2 ] [ "USE: temporary b" eval ] unit-test -{ 0 4 } [ b ] unit-test-effect +{ 0 4 } [ b ] must-infer-as [ ] [ "IN: temporary : a 1 2 3 ;" eval ] unit-test [ 1 2 3 1 2 3 ] [ "USE: temporary b" eval ] unit-test -{ 0 6 } [ b ] unit-test-effect +{ 0 6 } [ b ] must-infer-as \ b word-xt "b-xt" set @@ -52,7 +52,7 @@ DEFER: c [ ] [ "IN: temporary : a 1 2 ;" eval ] unit-test -{ 0 4 } [ c ] unit-test-effect +{ 0 4 } [ c ] must-infer-as [ f ] [ "c-xt" get \ c word-xt = ] unit-test diff --git a/core/inference/inference-docs.factor b/core/inference/inference-docs.factor index 5f7e926b6a..68e5920a3d 100755 --- a/core/inference/inference-docs.factor +++ b/core/inference/inference-docs.factor @@ -73,6 +73,12 @@ $nl { $subsection infer-quot-value } "The " { $vocab-link "macros" } " vocabulary defines some nice syntax sugar which makes compiler transforms easier to work with." ; +ARTICLE: "dataflow-graphs" "Inspecting the dataflow graph" +"The dataflow graph used by " { $link "compiler" } " can be obtained:" +{ $subsection dataflow } +"The " { $vocab-link "optimizer.debugger" } " tool prints the dataflow graph in human readable form." +$nl ; + ARTICLE: "inference" "Stack effect inference" "The stack effect inference tool is used to check correctness of code before it is run. It is also used by the compiler to build a dataflow graph on which optimizations can be performed. Only words for which a stack effect can be inferred will compile." $nl @@ -80,14 +86,15 @@ $nl { $subsection infer. } "Instead of printing the inferred information, it can be returned as objects on the stack:" { $subsection infer } -"The dataflow graph used by " { $link "compiler" } " can be obtained:" -{ $subsection dataflow } +"Static stack effect inference can be combined with unit tests; see " { $link "tools.test.write" } "." +$nl "The following articles describe the implementation of the stack effect inference algorithm:" { $subsection "inference-simple" } { $subsection "inference-combinators" } { $subsection "inference-branches" } { $subsection "inference-recursive" } { $subsection "inference-limitations" } +{ $subsection "dataflow-graphs" } { $subsection "compiler-transforms" } ; ABOUT: "inference" diff --git a/core/inference/inference-tests.factor b/core/inference/inference-tests.factor index b43226166a..c5bc3b5fda 100755 --- a/core/inference/inference-tests.factor +++ b/core/inference/inference-tests.factor @@ -4,23 +4,22 @@ math.parser math.private namespaces namespaces.private parser sequences strings vectors words quotations effects tools.test continuations generic.standard sorting assocs definitions prettyprint io inspector tuples classes.union classes.predicate -debugger threads.private io.streams.string combinators.private -tools.test.inference ; +debugger threads.private io.streams.string combinators.private ; IN: temporary -{ 0 2 } [ 2 "Hello" ] unit-test-effect -{ 1 2 } [ dup ] unit-test-effect +{ 0 2 } [ 2 "Hello" ] must-infer-as +{ 1 2 } [ dup ] must-infer-as -{ 1 2 } [ [ dup ] call ] unit-test-effect +{ 1 2 } [ [ dup ] call ] must-infer-as [ [ call ] infer ] must-fail -{ 2 4 } [ 2dup ] unit-test-effect +{ 2 4 } [ 2dup ] must-infer-as -{ 1 0 } [ [ ] [ ] if ] unit-test-effect +{ 1 0 } [ [ ] [ ] if ] must-infer-as [ [ if ] infer ] must-fail [ [ [ ] if ] infer ] must-fail [ [ [ 2 ] [ ] if ] infer ] must-fail -{ 4 3 } [ [ rot ] [ -rot ] if ] unit-test-effect +{ 4 3 } [ [ rot ] [ -rot ] if ] must-infer-as { 4 3 } [ [ @@ -28,17 +27,17 @@ IN: temporary ] [ -rot ] if -] unit-test-effect +] must-infer-as -{ 1 1 } [ dup [ ] when ] unit-test-effect -{ 1 1 } [ dup [ dup fixnum* ] when ] unit-test-effect -{ 2 1 } [ [ dup fixnum* ] when ] unit-test-effect +{ 1 1 } [ dup [ ] when ] must-infer-as +{ 1 1 } [ dup [ dup fixnum* ] when ] must-infer-as +{ 2 1 } [ [ dup fixnum* ] when ] must-infer-as -{ 1 0 } [ [ drop ] when* ] unit-test-effect -{ 1 1 } [ [ { { [ ] } } ] unless* ] unit-test-effect +{ 1 0 } [ [ drop ] when* ] must-infer-as +{ 1 1 } [ [ { { [ ] } } ] unless* ] must-infer-as { 0 1 } -[ [ 2 2 fixnum+ ] dup [ ] when call ] unit-test-effect +[ [ 2 2 fixnum+ ] dup [ ] when call ] must-infer-as [ [ [ [ 2 2 fixnum+ ] ] [ [ 2 2 fixnum* ] ] if call ] infer @@ -50,7 +49,7 @@ IN: temporary : termination-test-2 [ termination-test-1 ] [ 3 ] if ; -{ 1 1 } [ termination-test-2 ] unit-test-effect +{ 1 1 } [ termination-test-2 ] must-infer-as : infinite-loop infinite-loop ; @@ -62,12 +61,12 @@ IN: temporary : simple-recursion-1 ( obj -- obj ) dup [ simple-recursion-1 ] [ ] if ; -{ 1 1 } [ simple-recursion-1 ] unit-test-effect +{ 1 1 } [ simple-recursion-1 ] must-infer-as : simple-recursion-2 ( obj -- obj ) dup [ ] [ simple-recursion-2 ] if ; -{ 1 1 } [ simple-recursion-2 ] unit-test-effect +{ 1 1 } [ simple-recursion-2 ] must-infer-as : bad-recursion-2 ( obj -- obj ) dup [ dup first swap second bad-recursion-2 ] [ ] if ; @@ -77,10 +76,10 @@ IN: temporary : funny-recursion ( obj -- obj ) dup [ funny-recursion 1 ] [ 2 ] if drop ; -{ 1 1 } [ funny-recursion ] unit-test-effect +{ 1 1 } [ funny-recursion ] must-infer-as ! Simple combinators -{ 1 2 } [ [ first ] keep second ] unit-test-effect +{ 1 2 } [ [ first ] keep second ] must-infer-as ! Mutual recursion DEFER: foe @@ -103,8 +102,8 @@ DEFER: foe 2drop f ] if ; -{ 2 1 } [ fie ] unit-test-effect -{ 2 1 } [ foe ] unit-test-effect +{ 2 1 } [ fie ] must-infer-as +{ 2 1 } [ foe ] must-infer-as : nested-when ( -- ) t [ @@ -113,7 +112,7 @@ DEFER: foe ] when ] when ; -{ 0 0 } [ nested-when ] unit-test-effect +{ 0 0 } [ nested-when ] must-infer-as : nested-when* ( obj -- ) [ @@ -122,11 +121,11 @@ DEFER: foe ] when* ] when* ; -{ 1 0 } [ nested-when* ] unit-test-effect +{ 1 0 } [ nested-when* ] must-infer-as SYMBOL: sym-test -{ 0 1 } [ sym-test ] unit-test-effect +{ 0 1 } [ sym-test ] must-infer-as : terminator-branch dup [ @@ -135,7 +134,7 @@ SYMBOL: sym-test "foo" throw ] if ; -{ 1 1 } [ terminator-branch ] unit-test-effect +{ 1 1 } [ terminator-branch ] must-infer-as : recursive-terminator ( obj -- ) dup [ @@ -144,7 +143,7 @@ SYMBOL: sym-test "Hi" throw ] if ; -{ 1 0 } [ recursive-terminator ] unit-test-effect +{ 1 0 } [ recursive-terminator ] must-infer-as GENERIC: potential-hang ( obj -- obj ) M: fixnum potential-hang dup [ potential-hang ] when ; @@ -157,24 +156,24 @@ M: funny-cons iterate funny-cons-cdr iterate ; M: f iterate drop ; M: real iterate drop ; -{ 1 0 } [ iterate ] unit-test-effect +{ 1 0 } [ iterate ] must-infer-as ! Regression : cat ( obj -- * ) dup [ throw ] [ throw ] if ; : dog ( a b c -- ) dup [ cat ] [ 3drop ] if ; -{ 3 0 } [ dog ] unit-test-effect +{ 3 0 } [ dog ] must-infer-as ! Regression DEFER: monkey : friend ( a b c -- ) dup [ friend ] [ monkey ] if ; : monkey ( a b c -- ) dup [ 3drop ] [ friend ] if ; -{ 3 0 } [ friend ] unit-test-effect +{ 3 0 } [ friend ] must-infer-as ! Regression -- same as above but we infer the second word first DEFER: blah2 : blah ( a b c -- ) dup [ blah ] [ blah2 ] if ; : blah2 ( a b c -- ) dup [ blah ] [ 3drop ] if ; -{ 3 0 } [ blah2 ] unit-test-effect +{ 3 0 } [ blah2 ] must-infer-as ! Regression DEFER: blah4 @@ -182,7 +181,7 @@ DEFER: blah4 dup [ blah3 ] [ dup [ blah4 ] [ blah3 ] if ] if ; : blah4 ( a b c -- ) dup [ blah4 ] [ dup [ 3drop ] [ blah3 ] if ] if ; -{ 3 0 } [ blah4 ] unit-test-effect +{ 3 0 } [ blah4 ] must-infer-as ! Regression : bad-combinator ( obj quot -- ) @@ -199,7 +198,7 @@ DEFER: blah4 dup string? [ 2array throw ] unless over string? [ 2array throw ] unless ; -{ 2 2 } [ bad-input# ] unit-test-effect +{ 2 2 } [ bad-input# ] must-infer-as ! Regression @@ -218,7 +217,7 @@ DEFER: do-crap* ! Regression : too-deep ( a b -- c ) dup [ drop ] [ 2dup too-deep too-deep * ] if ; inline -{ 2 1 } [ too-deep ] unit-test-effect +{ 2 1 } [ too-deep ] must-infer-as ! Error reporting is wrong MATH: xyz @@ -258,17 +257,17 @@ DEFER: C [ dup B C ] } dispatch ; -{ 1 0 } [ A ] unit-test-effect -{ 1 0 } [ B ] unit-test-effect -{ 1 0 } [ C ] unit-test-effect +{ 1 0 } [ A ] must-infer-as +{ 1 0 } [ B ] must-infer-as +{ 1 0 } [ C ] must-infer-as ! I found this bug by thinking hard about the previous one DEFER: Y : X ( a b -- c d ) dup [ swap Y ] [ ] if ; : Y ( a b -- c d ) X ; -{ 2 2 } [ X ] unit-test-effect -{ 2 2 } [ Y ] unit-test-effect +{ 2 2 } [ X ] must-infer-as +{ 2 2 } [ Y ] must-infer-as ! This one comes from UI code DEFER: #1 @@ -332,9 +331,9 @@ DEFER: bar [ [ get-slots ] infer ] [ inference-error? ] must-fail-with ! Test some curry stuff -{ 1 1 } [ 3 [ ] curry 4 [ ] curry if ] unit-test-effect +{ 1 1 } [ 3 [ ] curry 4 [ ] curry if ] must-infer-as -{ 2 1 } [ [ ] curry 4 [ ] curry if ] unit-test-effect +{ 2 1 } [ [ ] curry 4 [ ] curry if ] must-infer-as [ [ 3 [ ] curry 1 2 [ ] 2curry if ] infer ] must-fail @@ -381,7 +380,7 @@ DEFER: bar \ assoc-like must-infer \ assoc-clone-like must-infer \ >alist must-infer -{ 1 3 } [ [ 2drop f ] assoc-find ] unit-test-effect +{ 1 3 } [ [ 2drop f ] assoc-find ] must-infer-as ! Test some random library words \ 1quotation must-infer @@ -404,10 +403,10 @@ DEFER: bar \ define-predicate-class must-infer ! Test words with continuations -{ 0 0 } [ [ drop ] callcc0 ] unit-test-effect -{ 0 1 } [ [ 4 swap continue-with ] callcc1 ] unit-test-effect -{ 2 1 } [ [ + ] [ ] [ ] cleanup ] unit-test-effect -{ 2 1 } [ [ + ] [ 3drop 0 ] recover ] unit-test-effect +{ 0 0 } [ [ drop ] callcc0 ] must-infer-as +{ 0 1 } [ [ 4 swap continue-with ] callcc1 ] must-infer-as +{ 2 1 } [ [ + ] [ ] [ ] cleanup ] must-infer-as +{ 2 1 } [ [ + ] [ 3drop 0 ] recover ] must-infer-as \ dispose must-infer @@ -450,13 +449,13 @@ DEFER: bar [ [ barxxx ] infer ] must-fail ! A typo -{ 1 0 } [ { [ ] } dispatch ] unit-test-effect +{ 1 0 } [ { [ ] } dispatch ] must-infer-as DEFER: inline-recursive-2 : inline-recursive-1 ( -- ) inline-recursive-2 ; : inline-recursive-2 ( -- ) inline-recursive-1 ; -{ 0 0 } [ inline-recursive-1 ] unit-test-effect +{ 0 0 } [ inline-recursive-1 ] must-infer-as ! Hooks SYMBOL: my-var @@ -465,22 +464,22 @@ HOOK: my-hook my-var ( -- x ) M: integer my-hook "an integer" ; M: string my-hook "a string" ; -{ 0 1 } [ my-hook ] unit-test-effect +{ 0 1 } [ my-hook ] must-infer-as DEFER: deferred-word : calls-deferred-word [ deferred-word ] [ 3 ] if ; -{ 1 1 } [ calls-deferred-word ] unit-test-effect +{ 1 1 } [ calls-deferred-word ] must-infer-as USE: inference.dataflow -{ 1 0 } [ [ iterate-next ] iterate-nodes ] unit-test-effect +{ 1 0 } [ [ iterate-next ] iterate-nodes ] must-infer-as { 1 0 } [ [ [ iterate-next ] iterate-nodes ] with-node-iterator -] unit-test-effect +] must-infer-as : nilpotent ( quot -- ) t [ [ call ] keep nilpotent ] [ drop ] if ; inline @@ -490,11 +489,11 @@ USE: inference.dataflow { 0 1 } [ [ ] [ call ] keep [ [ call ] keep ] nilpotent ] -unit-test-effect +must-infer-as -{ 0 0 } [ [ ] semisimple ] unit-test-effect +{ 0 0 } [ [ ] semisimple ] must-infer-as -{ 1 0 } [ [ drop ] each-node ] unit-test-effect +{ 1 0 } [ [ drop ] each-node ] must-infer-as DEFER: an-inline-word @@ -510,9 +509,9 @@ DEFER: an-inline-word : an-inline-word ( obj quot -- ) >r normal-word r> call ; inline -{ 1 1 } [ [ 3 * ] an-inline-word ] unit-test-effect +{ 1 1 } [ [ 3 * ] an-inline-word ] must-infer-as -{ 0 1 } [ [ 2 ] [ 2 ] [ + ] compose compose call ] unit-test-effect +{ 0 1 } [ [ 2 ] [ 2 ] [ + ] compose compose call ] must-infer-as TUPLE: custom-error ; @@ -536,4 +535,4 @@ TUPLE: custom-error ; ! This was a false trigger of the undecidable quotation ! recursion bug -{ 2 1 } [ find-last-sep ] unit-test-effect +{ 2 1 } [ find-last-sep ] must-infer-as diff --git a/core/inference/transforms/transforms-tests.factor b/core/inference/transforms/transforms-tests.factor index f58e557b10..0e5c3e231e 100755 --- a/core/inference/transforms/transforms-tests.factor +++ b/core/inference/transforms/transforms-tests.factor @@ -1,6 +1,6 @@ IN: temporary USING: sequences inference.transforms tools.test math kernel -quotations tools.test.inference inference ; +quotations inference ; : compose-n-quot >quotation ; : compose-n compose-n-quot call ; diff --git a/extra/combinators/lib/lib-tests.factor b/extra/combinators/lib/lib-tests.factor index 20f52b2ea3..24d70a86c6 100755 --- a/extra/combinators/lib/lib-tests.factor +++ b/extra/combinators/lib/lib-tests.factor @@ -1,5 +1,5 @@ USING: combinators.lib kernel math math.ranges random sequences -tools.test tools.test.inference continuations arrays vectors ; +tools.test continuations arrays vectors ; IN: temporary [ 5 ] [ [ 10 random ] [ 5 = ] generate ] unit-test diff --git a/extra/io/launcher/launcher-tests.factor b/extra/io/launcher/launcher-tests.factor index b9f8f3e061..6705caa33c 100755 --- a/extra/io/launcher/launcher-tests.factor +++ b/extra/io/launcher/launcher-tests.factor @@ -1,4 +1,4 @@ IN: temporary -USING: tools.test tools.test.inference io.launcher ; +USING: tools.test io.launcher ; \ must-infer diff --git a/extra/io/server/server-tests.factor b/extra/io/server/server-tests.factor old mode 100644 new mode 100755 index 5c37a37380..776bc4b429 --- a/extra/io/server/server-tests.factor +++ b/extra/io/server/server-tests.factor @@ -1,4 +1,4 @@ IN: temporary -USING: tools.test.inference io.server ; +USING: tools.test io.server ; -{ 1 0 } [ [ ] spawn-server ] unit-test-effect +{ 1 0 } [ [ ] spawn-server ] must-infer-as diff --git a/extra/tools/test/test-docs.factor b/extra/tools/test/test-docs.factor index 147e795861..c027073398 100755 --- a/extra/tools/test/test-docs.factor +++ b/extra/tools/test/test-docs.factor @@ -1,6 +1,36 @@ -USING: help.markup help.syntax kernel ; +USING: help.markup help.syntax kernel quotations io ; IN: tools.test +ARTICLE: "tools.test.write" "Writing unit tests" +"Assert that a quotation outputs a specific set of values:" +{ $subsection unit-test } +"Assert that a quotation throws an error:" +{ $subsection must-fail } +{ $subsection must-fail-with } +"Assert that a quotation or word has a specific static stack effect (see " { $link "inference" } "):" +{ $subsection must-infer } +{ $subsection must-infer-as } ; + +ARTICLE: "tools.test.run" "Running unit tests" +"The following words run test harness files; any test failures are collected and printed at the end:" +{ $subsection test } +{ $subsection test-all } ; + +ARTICLE: "tools.test.failure" "Handling test failures" +"Most of the time the words documented in " { $link "tools.test.run" } " are used because they print all test failures in human-readable form. Sometimes, you want to develop a tool which inspects the test failures and takes some kind of action instead; one example is " { $vocab-link "builder" } "." +$nl +"The following words output an association list mapping vocabulary names to sequences of failures; a failure is an array having the shape " { $snippet "{ error test continuation }" } ", and the elements are as follows:" +{ $list + { { $snippet "error" } " - the error thrown by the unit test" } + { { $snippet "test" } " - a pair " { $snippet "{ output input }" } " containing expected output and a unit test quotation which didn't produce this output" } + { { $snippet "continuation" } " - the traceback at the point of the error" } +} +"The following words run test harness files and output failures:" +{ $subsection run-tests } +{ $subsection run-all-tests } +"The following word prints failures:" +{ $subsection failures. } ; + ARTICLE: "tools.test" "Unit testing" "A unit test is a piece of code which starts with known input values, then compares the output of a word with an expected output, where the expected output is defined by the word's contract." $nl @@ -8,13 +38,10 @@ $nl $nl "Unit tests for a vocabulary are placed in test files, named " { $snippet { $emphasis "vocab" } "-tests.factor" } " alongside " { $snippet { $emphasis "vocab" } ".factor" } "; see " { $link "vocabs.loader" } " for details." $nl -"If the test harness needs to define words, they should be placed in the " { $snippet "temporary" } " vocabulary so that they can be forgotten after the tests have been run. Test harness files consist mostly of calls to the following two words:" -{ $subsection unit-test } -{ $subsection must-fail } -{ $subsection must-fail-with } -"The following words run test harness files; any test failures are collected and printed at the end:" -{ $subsection test } -{ $subsection test-all } ; +"If the test harness needs to define words, they should be placed in the " { $snippet "temporary" } " vocabulary so that they can be forgotten after the tests have been run." +{ $subsection "tools.test.write" } +{ $subsection "tools.test.run" } +{ $subsection "tools.test.failure" } ; ABOUT: "tools.test" @@ -26,3 +53,37 @@ HELP: must-fail { $values { "quot" "a quotation run with an empty stack" } } { $description "Runs a quotation with an empty stack, expecting it to throw an error. If the quotation throws an error, this word returns normally. If the quotation does not throw an error, this word " { $emphasis "does" } " raise an error." } { $notes "This word is used to test boundary conditions and fail-fast behavior." } ; + +HELP: must-fail-with +{ $values { "quot" "a quotation run with an empty stack" } { "pred" "a quotation with stack effect " { $snippet "( error -- ? )" } } } +{ $description "Runs a quotation with an empty stack, expecting it to throw an error which must satisfy " { $snippet "pred" } ". If the quotation does not throw an error, or if the error does not match the predicate, the unit test fails." } +{ $notes "This word is used to test error handling code, ensuring that errors thrown by code contain the relevant debugging information." } ; + +HELP: must-infer +{ $values { "word/quot" "a quotation or a word" } } +{ $description "Ensures that the quotation or word has a static stack effect without running it." } +{ $notes "This word is used to test that code will compile with the optimizing compiler for optimum performance. See " { $link "compiler" } "." } ; + +HELP: must-infer-as +{ $values { "effect" "a pair with shape " { $snippet "{ inputs outputs }" } } { "quot" quotation } } +{ $description "Ensures that the quotation has the indicated stack effect without running it." } +{ $notes "This word is used to test that code will compile with the optimizing compiler for optimum performance. See " { $link "compiler" } "." } ; + +HELP: test +{ $values { "prefix" "a vocabulary name" } } +{ $description "Runs unit tests for the vocabulary named " { $snippet "prefix" } " and all of its child vocabularies." } ; + +HELP: run-tests +{ $values { "prefix" "a vocabulary name" } { "failures" "an association list of unit test failures" } } +{ $description "Runs unit tests for the vocabulary named " { $snippet "prefix" } " and all of its child vocabularies. Outputs unit test failures as documented in " { $link "tools.test.failure" } "." } ; + +HELP: test-all +{ $description "Runs unit tests for all loaded vocabularies." } ; + +HELP: run-all-tests +{ $values { "prefix" "a vocabulary name" } { "failures" "an association list of unit test failures" } } +{ $description "Runs unit tests for all loaded vocabularies and outputs unit test failures as documented in " { $link "tools.test.failure" } "." } ; + +HELP: failure. +{ $values { "failures" "an association list of unit test failures" } } +{ $description "Prints unit test failures output by " { $link run-tests } " or " { $link run-all-tests } " to the " { $link stdio } " stream." } ; diff --git a/extra/tools/test/test.factor b/extra/tools/test/test.factor index 09d497aac7..0b1a495e90 100755 --- a/extra/tools/test/test.factor +++ b/extra/tools/test/test.factor @@ -3,7 +3,8 @@ USING: namespaces arrays prettyprint sequences kernel vectors quotations words parser assocs combinators continuations debugger io io.files vocabs tools.time -vocabs.loader source-files compiler.units inspector ; +vocabs.loader source-files compiler.units inspector +inference effects ; IN: tools.test SYMBOL: failures @@ -29,13 +30,23 @@ SYMBOL: this-test { } swap with-datastack swap >array assert= ] 2curry (unit-test) ; +: short-effect ( effect -- pair ) + dup effect-in length swap effect-out length 2array ; + +: must-infer-as ( effect quot -- ) + >r 1quotation r> [ infer short-effect ] curry unit-test ; + +: must-infer ( word/quot -- ) + dup word? [ 1quotation ] when + [ infer drop ] curry [ ] swap unit-test ; + TUPLE: expected-error ; M: expected-error summary drop "The unit test expected the quotation to throw an error" ; -: must-fail-with ( quot test -- ) +: must-fail-with ( quot pred -- ) >r [ expected-error construct-empty throw ] compose r> [ recover ] 2curry [ t ] swap unit-test ; @@ -60,7 +71,7 @@ M: expected-error summary : run-test ( vocab -- failures ) V{ } clone [ failures [ - (run-test) + [ (run-test) ] [ swap failure ] recover ] with-variable ] keep ; diff --git a/extra/ui/gadgets/books/books-tests.factor b/extra/ui/gadgets/books/books-tests.factor index 35016e1669..9e1b0aa985 100755 --- a/extra/ui/gadgets/books/books-tests.factor +++ b/extra/ui/gadgets/books/books-tests.factor @@ -1,4 +1,4 @@ IN: temporary -USING: tools.test.inference ui.gadgets.books ; +USING: tools.test ui.gadgets.books ; \ must-infer diff --git a/extra/ui/gadgets/buttons/buttons-tests.factor b/extra/ui/gadgets/buttons/buttons-tests.factor index 77dfd30d96..224ef9e1ce 100755 --- a/extra/ui/gadgets/buttons/buttons-tests.factor +++ b/extra/ui/gadgets/buttons/buttons-tests.factor @@ -1,7 +1,6 @@ IN: temporary USING: ui.commands ui.gadgets.buttons ui.gadgets.labels -ui.gadgets tools.test namespaces sequences kernel models -tools.test.inference ; +ui.gadgets tools.test namespaces sequences kernel models ; TUPLE: foo-gadget ; diff --git a/extra/ui/gadgets/editors/editors-tests.factor b/extra/ui/gadgets/editors/editors-tests.factor index bc302c1a09..f3a6b9fd5d 100755 --- a/extra/ui/gadgets/editors/editors-tests.factor +++ b/extra/ui/gadgets/editors/editors-tests.factor @@ -1,7 +1,6 @@ USING: ui.gadgets.editors tools.test kernel io io.streams.plain -definitions namespaces ui.gadgets -ui.gadgets.grids prettyprint documents ui.gestures -tools.test.inference tools.test.ui models ; +definitions namespaces ui.gadgets ui.gadgets.grids prettyprint +documents ui.gestures tools.test.ui models ; [ "foo bar" ] [ "editor" set diff --git a/extra/ui/gadgets/gadgets-tests.factor b/extra/ui/gadgets/gadgets-tests.factor index 81b30559df..1e27744f33 100755 --- a/extra/ui/gadgets/gadgets-tests.factor +++ b/extra/ui/gadgets/gadgets-tests.factor @@ -1,6 +1,6 @@ IN: temporary USING: ui.gadgets ui.gadgets.packs ui.gadgets.worlds tools.test -namespaces models kernel tools.test.inference dlists math +namespaces models kernel dlists math math.parser ui sequences hashtables assocs io arrays prettyprint io.streams.string ; diff --git a/extra/ui/gadgets/scrollers/scrollers-tests.factor b/extra/ui/gadgets/scrollers/scrollers-tests.factor index 30ba4a18f3..dd667fdfec 100755 --- a/extra/ui/gadgets/scrollers/scrollers-tests.factor +++ b/extra/ui/gadgets/scrollers/scrollers-tests.factor @@ -3,7 +3,7 @@ USING: ui.gadgets ui.gadgets.scrollers namespaces tools.test kernel models ui.gadgets.viewports ui.gadgets.labels ui.gadgets.grids ui.gadgets.frames ui.gadgets.sliders math math.vectors arrays sequences -tools.test.inference tools.test.ui ; +tools.test.ui ; [ ] [ "g" set diff --git a/extra/ui/tools/browser/browser-tests.factor b/extra/ui/tools/browser/browser-tests.factor index 3102ad1bd9..7262c72756 100755 --- a/extra/ui/tools/browser/browser-tests.factor +++ b/extra/ui/tools/browser/browser-tests.factor @@ -1,6 +1,5 @@ IN: temporary -USING: tools.test tools.test.ui ui.tools.browser -tools.test.inference ; +USING: tools.test tools.test.ui ui.tools.browser ; \ must-infer [ ] [ [ ] with-grafted-gadget ] unit-test diff --git a/extra/ui/tools/interactor/interactor-tests.factor b/extra/ui/tools/interactor/interactor-tests.factor index bf9de10a1e..0422c4170a 100755 --- a/extra/ui/tools/interactor/interactor-tests.factor +++ b/extra/ui/tools/interactor/interactor-tests.factor @@ -1,4 +1,4 @@ IN: temporary -USING: ui.tools.interactor tools.test.inference ; +USING: ui.tools.interactor tools.test ; \ must-infer diff --git a/extra/ui/tools/walker/walker-tests.factor b/extra/ui/tools/walker/walker-tests.factor index a23b629d1e..acf0a39bfb 100755 --- a/extra/ui/tools/walker/walker-tests.factor +++ b/extra/ui/tools/walker/walker-tests.factor @@ -2,7 +2,7 @@ USING: arrays continuations ui.tools.listener ui.tools.walker ui.tools.workspace inspector kernel namespaces sequences threads listener tools.test ui ui.gadgets ui.gadgets.worlds ui.gadgets.packs vectors ui.tools tools.interpreter -tools.interpreter.debug tools.test.inference tools.test.ui ; +tools.interpreter.debug tools.test.ui ; IN: temporary \ must-infer diff --git a/extra/ui/tools/workspace/workspace-tests.factor b/extra/ui/tools/workspace/workspace-tests.factor index 41f0151746..5e3695fed3 100755 --- a/extra/ui/tools/workspace/workspace-tests.factor +++ b/extra/ui/tools/workspace/workspace-tests.factor @@ -1,4 +1,4 @@ IN: temporary -USING: tools.test tools.test.inference ui.tools ; +USING: tools.test ui.tools ; \ must-infer From 78abc143d626838d551592dd61bf1de31e4fe458 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 01:01:14 -0600 Subject: [PATCH 153/269] Load fix --- core/math/parser/parser-docs.factor | 11 +- .../furnace-pastebin/annotate-paste.furnace | 28 ----- .../furnace-pastebin/annotation.furnace | 11 -- unmaintained/furnace-pastebin/load.factor | 4 - .../furnace-pastebin/new-paste.furnace | 27 ----- .../furnace-pastebin/paste-list.furnace | 7 -- .../furnace-pastebin/paste-summary.furnace | 9 -- unmaintained/furnace-pastebin/pastebin.factor | 110 ------------------ .../furnace-pastebin/show-paste.furnace | 15 --- 9 files changed, 1 insertion(+), 221 deletions(-) delete mode 100644 unmaintained/furnace-pastebin/annotate-paste.furnace delete mode 100644 unmaintained/furnace-pastebin/annotation.furnace delete mode 100644 unmaintained/furnace-pastebin/load.factor delete mode 100644 unmaintained/furnace-pastebin/new-paste.furnace delete mode 100644 unmaintained/furnace-pastebin/paste-list.furnace delete mode 100644 unmaintained/furnace-pastebin/paste-summary.furnace delete mode 100644 unmaintained/furnace-pastebin/pastebin.factor delete mode 100644 unmaintained/furnace-pastebin/show-paste.furnace diff --git a/core/math/parser/parser-docs.factor b/core/math/parser/parser-docs.factor index b0d52ef2ef..1d2a24057c 100644 --- a/core/math/parser/parser-docs.factor +++ b/core/math/parser/parser-docs.factor @@ -25,14 +25,10 @@ $nl ABOUT: "number-strings" HELP: digits>integer -{ $values { "radix" "an integer between 2 and 36" } { "seq" "a sequence of integers" } { "n" integer } } +{ $values { "seq" "a sequence of integers" } { "radix" "an integer between 2 and 36" } { "n" integer } } { $description "Converts a sequence of digits (with most significant digit first) into an integer." } { $notes "This is one of the factors of " { $link string>number } "." } ; -HELP: valid-digits? -{ $values { "radix" "an integer between 2 and 36" } { "seq" "a sequence of integers" } { "?" "a boolean" } } -{ $description "Tests if this sequence of integers represents a valid integer in the given radix." } ; - HELP: >digit { $values { "n" "an integer between 0 and 35" } { "ch" "a character" } } { $description "Outputs a character representation of a digit." } @@ -43,11 +39,6 @@ HELP: digit> { $description "Converts a character representation of a digit to an integer." } { $notes "This is one of the factors of " { $link string>number } "." } ; -HELP: string>integer -{ $values { "str" string } { "radix" "an integer between 2 and 36" } { "n/f" "an integer or " { $link f } } } -{ $description "Creates an integer from a string representation." } -{ $notes "The " { $link base> } " word is more general." } ; - HELP: base> { $values { "str" string } { "radix" "an integer between 2 and 36" } { "n/f" "a real number or " { $link f } } } { $description "Creates a real number from a string representation with the given radix. The radix is ignored for floating point literals; they are always taken to be in base 10." diff --git a/unmaintained/furnace-pastebin/annotate-paste.furnace b/unmaintained/furnace-pastebin/annotate-paste.furnace deleted file mode 100644 index 24f0d4ea94..0000000000 --- a/unmaintained/furnace-pastebin/annotate-paste.furnace +++ /dev/null @@ -1,28 +0,0 @@ -<% USING: namespaces math io ; %> - -

Annotate

- -
- - - -string write %>" /> - - - - - - - - - - - - - - - -
Summary:
Your name:
Contents:
- - -
diff --git a/unmaintained/furnace-pastebin/annotation.furnace b/unmaintained/furnace-pastebin/annotation.furnace deleted file mode 100644 index ed1bdac845..0000000000 --- a/unmaintained/furnace-pastebin/annotation.furnace +++ /dev/null @@ -1,11 +0,0 @@ -<% USING: namespaces io ; %> - -

Annotation: <% "summary" get write %>

- - - - - -
Annotation by:<% "author" get write %>
Channel:<% "channel" get write %>
Created:<% "date" get write %>
- -
<% "contents" get write %>
diff --git a/unmaintained/furnace-pastebin/load.factor b/unmaintained/furnace-pastebin/load.factor deleted file mode 100644 index 4f3bdc8db9..0000000000 --- a/unmaintained/furnace-pastebin/load.factor +++ /dev/null @@ -1,4 +0,0 @@ -REQUIRES: libs/concurrency libs/furnace libs/irc libs/store ; - -PROVIDE: apps/furnace-pastebin -{ +files+ { "pastebin.factor" } } ; diff --git a/unmaintained/furnace-pastebin/new-paste.furnace b/unmaintained/furnace-pastebin/new-paste.furnace deleted file mode 100644 index 36f0397b67..0000000000 --- a/unmaintained/furnace-pastebin/new-paste.furnace +++ /dev/null @@ -1,27 +0,0 @@ -
- - - - - - - - - - - - - - - - - - - - - - -
Summary:
Your name:
Channel:
Contents:
- - -
diff --git a/unmaintained/furnace-pastebin/paste-list.furnace b/unmaintained/furnace-pastebin/paste-list.furnace deleted file mode 100644 index 7a25ae2f50..0000000000 --- a/unmaintained/furnace-pastebin/paste-list.furnace +++ /dev/null @@ -1,7 +0,0 @@ -<% USING: namespaces furnace sequences ; %> - - -<% "new-paste-quot" get "New paste" render-link %> - -<% "pastes" get [ "paste-summary" render-template ] each %>
 Summary:Paste by:LinkDate
- diff --git a/unmaintained/furnace-pastebin/paste-summary.furnace b/unmaintained/furnace-pastebin/paste-summary.furnace deleted file mode 100644 index ad54c8d397..0000000000 --- a/unmaintained/furnace-pastebin/paste-summary.furnace +++ /dev/null @@ -1,9 +0,0 @@ -<% USING: namespaces io kernel math furnace ; %> - - -<% "n" get number>string write %> -<% "summary" get write %> -<% "author" get write %> -<% "n" get number>string "show-paste-quot" get curry "Show" render-link %> -<% "date" get print %> - diff --git a/unmaintained/furnace-pastebin/pastebin.factor b/unmaintained/furnace-pastebin/pastebin.factor deleted file mode 100644 index b11129312f..0000000000 --- a/unmaintained/furnace-pastebin/pastebin.factor +++ /dev/null @@ -1,110 +0,0 @@ -IN: furnace:pastebin -USING: calendar concurrency irc kernel namespaces sequences -furnace hashtables math store ; - -TUPLE: paste n summary author channel contents date annotations ; - -TUPLE: annotation summary author contents ; - -C: paste ( summary author channel contents -- paste ) - V{ } clone over set-paste-annotations - [ set-paste-contents ] keep - [ set-paste-channel ] keep - [ set-paste-author ] keep - [ set-paste-summary ] keep ; - -TUPLE: pastebin pastes ; - -C: pastebin ( -- pastebin ) - V{ } clone over set-pastebin-pastes ; - -SYMBOL: store -"pastebin.store" load-store store set-global - pastebin store get store-variable - -: add-paste ( paste pastebin -- ) - now timestamp>http-string pick set-paste-date - dup pastebin-pastes length pick set-paste-n - pastebin-pastes push ; - -: get-paste ( n -- paste ) - pastebin get pastebin-pastes nth ; - -: show-paste ( n -- ) - get-paste "show-paste" "Paste" render-page ; - -\ show-paste { { "n" v-number } } define-action - -: new-paste ( -- ) - f "new-paste" "New paste" render-page ; - -\ new-paste { } define-action - -: make-remote-process - "trifocus.net" 4030 "public-irc" ; - -: alert-new-paste ( paste -- ) - >r make-remote-process r> - f over paste-channel rot [ - dup paste-author % - " pasted " % - CHAR: " , - dup paste-summary % - CHAR: " , - " at " % - "http://wee-url.com/responder/pastebin/show-paste?n=" % - paste-n # - ] "" make swap send ; - -: alert-annotation ( annotation paste -- ) - make-remote-process -rot - f over paste-channel 2swap [ - over annotation-author % - " annotated paste " % - " with \"" % - over annotation-summary % - "\" at " % - "http://wee-url.com/responder/pastebin/show-paste?n=" % - dup paste-n # - 2drop - ] "" make swap send ; - - -: submit-paste ( summary author channel contents -- ) - dup pastebin get-global add-paste - alert-new-paste store get save-store ; - -\ submit-paste { - { "summary" v-required } - { "author" v-required } - { "channel" "#concatenative" v-default } - { "contents" v-required } -} define-action - -: paste-list ( -- ) - [ - [ show-paste ] "show-paste-quot" set - [ new-paste ] "new-paste-quot" set - - pastebin get "paste-list" "Pastebin" render-page - ] with-scope ; - -\ paste-list { } define-action - -\ submit-paste [ paste-list ] define-redirect - -: annotate-paste ( paste# summary author contents -- ) - swap get-paste - [ paste-annotations push ] 2keep - alert-annotation store get save-store ; - -\ annotate-paste { - { "n" v-required v-number } - { "summary" v-required } - { "author" v-required } - { "contents" v-required } -} define-action - -\ annotate-paste [ "n" show-paste ] define-redirect - -"pastebin" "paste-list" "apps/furnace-pastebin" web-app diff --git a/unmaintained/furnace-pastebin/show-paste.furnace b/unmaintained/furnace-pastebin/show-paste.furnace deleted file mode 100644 index b3b4e99b6e..0000000000 --- a/unmaintained/furnace-pastebin/show-paste.furnace +++ /dev/null @@ -1,15 +0,0 @@ -<% USING: namespaces io furnace sequences ; %> - -

Paste: <% "summary" get write %>

- - - - - -
Paste by:<% "author" get write %>
Channel:<% "channel" get write %>
Created:<% "date" get write %>
- -
<% "contents" get write %>
- -<% "annotations" get [ "annotation" render-template ] each %> - -<% model get "annotate-paste" render-template %> From 831b712f848c90d97e5431e25ff47f122c27843e Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 01:02:26 -0600 Subject: [PATCH 154/269] Move logging code to io.logging --- extra/io/logging/authors.txt | 1 + extra/io/logging/logging-docs.factor | 26 +++++++++++++++ extra/io/logging/logging.factor | 47 ++++++++++++++++++++++++++++ extra/io/logging/summary.txt | 1 + extra/io/server/server-docs.factor | 23 -------------- extra/io/server/server.factor | 41 ++---------------------- 6 files changed, 77 insertions(+), 62 deletions(-) create mode 100644 extra/io/logging/authors.txt create mode 100644 extra/io/logging/logging-docs.factor create mode 100644 extra/io/logging/logging.factor create mode 100644 extra/io/logging/summary.txt diff --git a/extra/io/logging/authors.txt b/extra/io/logging/authors.txt new file mode 100644 index 0000000000..1901f27a24 --- /dev/null +++ b/extra/io/logging/authors.txt @@ -0,0 +1 @@ +Slava Pestov diff --git a/extra/io/logging/logging-docs.factor b/extra/io/logging/logging-docs.factor new file mode 100644 index 0000000000..6cd03ce212 --- /dev/null +++ b/extra/io/logging/logging-docs.factor @@ -0,0 +1,26 @@ +IN: io.logging +USING: help.markup help.syntax io ; + +HELP: log-stream +{ $var-description "Holds an output stream for logging messages." } +{ $see-also log-error log-client with-logging } ; + +HELP: log-message +{ $values { "str" "a string" } } +{ $description "Logs a message to the log stream. If " { $link log-stream } " is not set, logs to the " { $link stdio } " stream." } +{ $see-also log-error log-client } ; + +HELP: log-error +{ $values { "str" "a string" } } +{ $description "Logs an error message." } +{ $see-also log-message log-client } ; + +HELP: log-client +{ $values { "client" "a client socket stream" } } +{ $description "Logs an incoming client connection." } +{ $see-also log-message log-error } ; + +HELP: with-logging +{ $values { "service" "a string or " { $link f } } { "quot" "a quotation" } } +{ $description "Calls the quotation in a new dynamic scope where the " { $link log-stream } " is set to a file stream appending to a log file (if " { $snippet "service" } " is not " { $link f } ") or the " { $link stdio } " stream at the time " { $link with-logging } " is called (if " { $snippet "service" } " is " { $link f } ")." } ; + diff --git a/extra/io/logging/logging.factor b/extra/io/logging/logging.factor new file mode 100644 index 0000000000..bd9dc0862e --- /dev/null +++ b/extra/io/logging/logging.factor @@ -0,0 +1,47 @@ +! Copyright (C) 2003, 2008 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: namespaces kernel io calendar sequences io.files +io.sockets continuations prettyprint ; +IN: io.logging + +SYMBOL: log-stream + +: to-log-stream ( quot -- ) + log-stream get swap with-stream* ; inline + +: log-message ( str -- ) + [ + "[" write now timestamp>string write "] " write + print flush + ] to-log-stream ; + +: log-error ( str -- ) "Error: " swap append log-message ; + +: log-client ( client -- ) + "Accepted connection from " + swap client-stream-addr unparse append log-message ; + +: log-file ( service -- path ) + ".log" append resource-path ; + +: with-log-stream ( stream quot -- ) + log-stream get [ nip call ] [ + log-stream swap with-variable + ] if ; inline + +: with-log-file ( file quot -- ) + >r r> + [ with-log-stream ] curry + with-disposal ; inline + +: with-log-stdio ( quot -- ) + stdio get swap with-log-stream ; inline + +: with-logging ( service quot -- ) + over [ + >r log-file + "Writing log messages to " write dup print flush r> + with-log-file + ] [ + nip with-log-stdio + ] if ; inline diff --git a/extra/io/logging/summary.txt b/extra/io/logging/summary.txt new file mode 100644 index 0000000000..0edce8f0cf --- /dev/null +++ b/extra/io/logging/summary.txt @@ -0,0 +1 @@ +Basic logging framework for server applications diff --git a/extra/io/server/server-docs.factor b/extra/io/server/server-docs.factor index ea8320f18d..4e4342266a 100644 --- a/extra/io/server/server-docs.factor +++ b/extra/io/server/server-docs.factor @@ -1,29 +1,6 @@ USING: help help.syntax help.markup io ; IN: io.server -HELP: log-stream -{ $var-description "Holds an output stream for logging messages." } -{ $see-also log-error log-client with-logging } ; - -HELP: log-message -{ $values { "str" "a string" } } -{ $description "Logs a message to the log stream. If " { $link log-stream } " is not set, logs to the " { $link stdio } " stream." } -{ $see-also log-error log-client } ; - -HELP: log-error -{ $values { "str" "a string" } } -{ $description "Logs an error message." } -{ $see-also log-message log-client } ; - -HELP: log-client -{ $values { "client" "a client socket stream" } } -{ $description "Logs an incoming client connection." } -{ $see-also log-message log-error } ; - -HELP: with-logging -{ $values { "service" "a string or " { $link f } } { "quot" "a quotation" } } -{ $description "Calls the quotation in a new dynamic scope where the " { $link log-stream } " is set to a file stream appending to a log file (if " { $snippet "service" } " is not " { $link f } ") or the " { $link stdio } " stream at the time " { $link with-logging } " is called (if " { $snippet "service" } " is " { $link f } ")." } ; - HELP: with-client { $values { "quot" "a quotation" } { "client" "a client socket stream" } } { $description "Logs a client connection and spawns a new thread that calls the quotation, with the " { $link stdio } " stream set to the client stream. If the quotation throws an error, the client connection is closed, and the error is printed to the " { $link stdio } " stream at the time the thread was spawned." } ; diff --git a/extra/io/server/server.factor b/extra/io/server/server.factor index 3c3d2c20f5..182712c984 100755 --- a/extra/io/server/server.factor +++ b/extra/io/server/server.factor @@ -1,49 +1,12 @@ ! Copyright (C) 2003, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: io io.sockets io.files continuations kernel math -math.parser namespaces parser sequences strings +USING: io io.sockets io.files io.logging continuations kernel +math math.parser namespaces parser sequences strings prettyprint debugger quotations calendar qualified ; QUALIFIED: concurrency IN: io.server -SYMBOL: log-stream - -: with-log-stream ( quot -- ) - log-stream get swap with-stream* ; inline - -: log-message ( str -- ) - [ - "[" write now timestamp>string write "] " write - print flush - ] with-log-stream ; - -: log-error ( str -- ) "Error: " swap append log-message ; - -: log-client ( client -- ) - "Accepted connection from " - swap client-stream-addr unparse append log-message ; - -: log-file ( service -- path ) - ".log" append resource-path ; - -: with-log-file ( file quot -- ) - >r r> - [ log-stream swap with-variable ] curry - with-disposal ; inline - -: with-log-stdio ( quot -- ) - stdio get log-stream rot with-variable ; inline - -: with-logging ( service quot -- ) - over [ - >r log-file - "Writing log messages to " write dup print flush r> - with-log-file - ] [ - nip with-log-stdio - ] if ; inline - : with-client ( quot client -- ) dup log-client [ swap with-stream ] 2curry concurrency:spawn drop ; inline From 6373e350baf86d3814fdd05c3614522db84cd4b4 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 01:02:38 -0600 Subject: [PATCH 155/269] Removed test-changes word --- extra/ui/tools/tools.factor | 4 ---- 1 file changed, 4 deletions(-) diff --git a/extra/ui/tools/tools.factor b/extra/ui/tools/tools.factor index 8e2eeaa0ba..71a7080c86 100755 --- a/extra/ui/tools/tools.factor +++ b/extra/ui/tools/tools.factor @@ -80,14 +80,10 @@ H{ { +nullary+ t } } define-command \ refresh-all H{ { +nullary+ t } { +listener+ t } } define-command -\ test-changes -H{ { +nullary+ t } { +listener+ t } } define-command - workspace "workflow" f { { T{ key-down f { C+ } "n" } workspace-window } { T{ key-down f f "ESC" } hide-popup } { T{ key-down f f "F2" } refresh-all } - { T{ key-down f { A+ } "F2" } test-changes } } define-command-map [ From a06c536123a2b8a1c7785caa591a9bdf1e742cbc Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 01:03:27 -0600 Subject: [PATCH 156/269] Cleaned up SMTP implementation and added some features --- extra/smtp/authors.txt | 2 + extra/smtp/server/server.factor | 72 ++++++++++ extra/smtp/smtp-tests.factor | 130 ++++++++++++++---- extra/smtp/smtp.factor | 237 ++++++++++++++++++-------------- 4 files changed, 310 insertions(+), 131 deletions(-) create mode 100644 extra/smtp/server/server.factor diff --git a/extra/smtp/authors.txt b/extra/smtp/authors.txt index 7c29e7c401..159b1e91e9 100644 --- a/extra/smtp/authors.txt +++ b/extra/smtp/authors.txt @@ -1 +1,3 @@ Elie Chaftari +Dirk Vleugels +Slava Pestov diff --git a/extra/smtp/server/server.factor b/extra/smtp/server/server.factor new file mode 100644 index 0000000000..2cfc1e65e4 --- /dev/null +++ b/extra/smtp/server/server.factor @@ -0,0 +1,72 @@ +! Copyright (C) 2007 Elie CHAFTARI +! See http://factorcode.org/license.txt for BSD license. + +! Mock SMTP server for testing purposes. + +! Usage: 4321 smtp-server +! $ telnet 127.0.0.1 4321 +! Trying 127.0.0.1... +! Connected to localhost. +! Escape character is '^]'. +! 220 hello +! EHLO +! 220 and..? +! MAIL FROM: +! 220 OK +! RCPT TO: +! 220 OK +! Hi +! 500 ERROR +! DATA +! 354 Enter message, ending with "." on a line by itself +! Hello I am still waiting for your call +! Thanks +! . +! 220 OK +! QUIT +! bye +! Connection closed by foreign host. + +USING: combinators kernel prettyprint io io.server sequences +namespaces io.sockets continuations ; + +SYMBOL: data-mode + +: process ( -- ) + readln { + { [ [ dup "HELO" head? ] keep "EHLO" head? or ] [ + "220 and..?\r\n" write flush t + ] } + { [ dup "QUIT" = ] [ + "bye\r\n" write flush f + ] } + { [ dup "MAIL FROM:" head? ] [ + "220 OK\r\n" write flush t + ] } + { [ dup "RCPT TO:" head? ] [ + "220 OK\r\n" write flush t + ] } + { [ dup "DATA" = ] [ + data-mode on + "354 Enter message, ending with \".\" on a line by itself\r\n" + write flush t + ] } + { [ dup "." = data-mode get and ] [ + data-mode off + "220 OK\r\n" write flush t + ] } + { [ data-mode get ] [ t ] } + { [ t ] [ + "500 ERROR\r\n" write flush t + ] } + } cond nip [ process ] when ; + +: smtp-server ( port -- ) + "Starting SMTP server on port " write dup . flush + "127.0.0.1" swap [ + accept [ + 60000 stdio get set-timeout + "220 hello\r\n" write flush + process + ] with-stream + ] with-disposal ; diff --git a/extra/smtp/smtp-tests.factor b/extra/smtp/smtp-tests.factor index 8ab1fd0899..9a357fdc7d 100644 --- a/extra/smtp/smtp-tests.factor +++ b/extra/smtp/smtp-tests.factor @@ -1,41 +1,111 @@ -! Tested with Apache JAMES version 2.3.1 on localhost -! cram-md5 authentication tested against Exim 4 -! Replace "localhost" with your smtp server -! e.g. "your.smtp.server" initialize +USING: smtp tools.test io.streams.string io.logging threads +smtp.server kernel sequences namespaces ; +IN: temporary -USING: smtp tools.test ; +{ 0 0 } [ [ ] with-smtp-connection ] must-infer-as -"localhost" initialize ! replace localhost with your smtp server +[ "hello\nworld" validate-address ] must-fail -! 8889 set-port ! default port = 25, change for testing purposes +[ "slava@factorcode.org" ] +[ "slava@factorcode.org" validate-address ] unit-test -! 30000 set-read-timeout ! default = 60000 -! f set-esmtp ! when esmtp (extended smtp) is not supported +[ { "hello" "." "world" } validate-message ] must-fail -start +[ "hello\r\nworld\r\n.\r\n" ] [ + { "hello" "world" } [ send-body ] string-out +] unit-test -! "md5 password here" "login" cram-md5-auth +[ + [ + "500 syntax error" check-response + ] with-log-stdio +] must-fail -"root@localhost" mailfrom ! your@mail.address +[ ] [ + [ + "220 success" check-response + ] with-log-stdio +] unit-test -"root@localhost" rcptto ! someone@example.com +[ "220 success" ] [ + "220 success" [ receive-response ] string-in +] unit-test -! { "From: Your Name " -! "To: Destination Address " -! "Subject: test message" -! "Date: Thu, 17 May 2007 18:46:45 +0200" -! "Message-Id: " -! " " -! "This is a test message." -! } send-message +[ "220 the end" ] [ + [ + "220-a multiline response\r\n250-another line\r\n220 the end" + [ receive-response ] string-in + ] with-log-stdio +] unit-test -{ "From: Your Name " - "To: Destination Address " - "Subject: test message" - "Date: Thu, 17 May 2007 18:46:45 +0200" - "Message-Id: " - " " - "This is a test message." -} send-message +[ ] [ + [ + "220-a multiline response\r\n250-another line\r\n220 the end" + [ get-ok ] string-in + ] with-log-stdio +] unit-test -quit \ No newline at end of file +[ + "Subject:\r\nsecurity hole" validate-header +] must-fail + +[ + V{ + { "To" "Slava , Ed " } + { "From" "Doug " } + { "Subject" "Factor rules" } + } + { "slava@factorcode.org" "dharmatech@factorcode.org" } + "erg@factorcode.org" +] [ + "Factor rules" + { + "Slava " + "Ed " + } + "Doug " + simple-headers >r >r 2 head* r> r> +] unit-test + +[ + { + "To: Slava , Ed " + "From: Doug " + "Subject: Factor rules" + f + f + " " + "Hi guys" + "Bye guys" + } + { "slava@factorcode.org" "dharmatech@factorcode.org" } + "erg@factorcode.org" +] [ + "Hi guys\nBye guys" + "Factor rules" + { + "Slava " + "Ed " + } + "Doug " + prepare-simple-message + >r >r f 3 pick set-nth f 4 pick set-nth r> r> +] unit-test + +[ ] [ [ 4321 smtp-server ] in-thread ] unit-test + +[ ] [ + [ + 4321 smtp-port set + + "Hi guys\nBye guys" + "Factor rules" + { + "Slava " + "Ed " + } + "Doug " + + send-simple-message + ] with-scope +] unit-test \ No newline at end of file diff --git a/extra/smtp/smtp.factor b/extra/smtp/smtp.factor index 9116d094de..77bfb6cd82 100644 --- a/extra/smtp/smtp.factor +++ b/extra/smtp/smtp.factor @@ -1,138 +1,173 @@ -! Copyright (C) 2007 Elie CHAFTARI +! Copyright (C) 2007, 2008 Elie CHAFTARI, Dirk Vleugels, Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -! -! cram-md5 auth code contributed by Dirk Vleugels - -USING: alien alien.c-types combinators crypto.common crypto.hmac base64 -kernel io io.sockets namespaces sequences splitting ; +USING: namespaces io kernel io.logging io.sockets sequences +combinators sequences.lib splitting assocs strings math.parser +random system calendar ; IN: smtp -! ========================================================= -! smtp.factor implementation -! ========================================================= +SYMBOL: smtp-domain +SYMBOL: smtp-host "localhost" smtp-host set-global +SYMBOL: smtp-port 25 smtp-port set-global +SYMBOL: read-timeout 60000 read-timeout set-global +SYMBOL: esmtp t esmtp set-global -! Connection default values -: default-port 25 ; inline -: read-timeout 60000 ; inline -: esmtp t ; inline ! t = ehlo -: domain "localhost.localdomain" ; inline +: log-smtp-connection ( host port -- ) + [ + "Establishing SMTP connection to " % swap % ":" % # + ] "" make log-message ; -SYMBOL: sess -SYMBOL: conn -SYMBOL: challenge +: with-smtp-connection ( quot -- ) + [ + smtp-host get smtp-port get + 2dup log-smtp-connection + [ + smtp-domain [ host-name or ] change + read-timeout get stdio get set-timeout + call + ] with-stream + ] with-log-stdio ; inline -TUPLE: session address port timeout domain esmtp ; +: crlf "\r\n" write ; -: ( address -- session ) - default-port read-timeout domain esmtp - session construct-boa ; +: helo ( -- ) + esmtp get "EHLO " "HELO " ? write host-name write crlf ; -! ========================================================= -! Initialization routines -! ========================================================= +: validate-address ( string -- string' ) + #! Make sure we send funky stuff to the server by accident. + dup [ "\r\n>" member? ] contains? + [ "Bad e-mail address: " swap append throw ] when ; -: initialize ( address -- ) - sess set ; +: mail-from ( fromaddr -- ) + "MAIL FROM:<" write validate-address write ">" write crlf ; -: set-port ( port -- ) - sess get set-session-port ; +: rcpt-to ( to -- ) + "RCPT TO:<" write validate-address write ">" write crlf ; -: set-read-timeout ( timeout -- ) - sess get set-session-timeout ; +: data ( -- ) + "DATA" write crlf ; -: set-esmtp ( esmtp -- ) - sess get set-session-esmtp ; +: validate-message ( msg -- msg' ) + "." over member? [ "Message cannot contain . on a line by itself" throw ] when ; -: set-domain ( -- ) - host-name sess get set-session-domain ; +: send-body ( body -- ) + validate-message + [ write crlf ] each + "." write crlf ; -: do-start ( -- ) - sess get [ session-address ] keep session-port - dup conn set [ sess get session-timeout swap set-timeout ] - keep stream-readln print ; +: quit ( -- ) + "QUIT" write crlf ; -! ========================================================= -! Command routines -! ========================================================= +: log-response ( string -- ) "SMTP: " swap append log-message ; : check-response ( response -- ) { - { [ dup "220" head? ] [ print ] } - { [ dup "235" swap subseq? ] [ print ] } - { [ dup "250" head? ] [ print ] } - { [ dup "221" head? ] [ print ] } - { [ dup "bye" head? ] [ print ] } + { [ dup "220" head? ] [ log-response ] } + { [ dup "235" swap subseq? ] [ log-response ] } + { [ dup "250" head? ] [ log-response ] } + { [ dup "221" head? ] [ log-response ] } + { [ dup "bye" head? ] [ log-response ] } { [ dup "4" head? ] [ "server busy" throw ] } - { [ dup "334" head? ] [ " " split 1 swap nth base64> challenge set ] } - { [ dup "354" head? ] [ print ] } - { [ dup "50" head? ] [ print "syntax error" throw ] } - { [ dup "53" head? ] [ print "invalid authentication data" throw ] } - { [ dup "55" head? ] [ print "fatal error" throw ] } - { [ t ] [ "unknow error" throw ] } + { [ dup "354" head? ] [ log-response ] } + { [ dup "50" head? ] [ log-response "syntax error" throw ] } + { [ dup "53" head? ] [ log-response "invalid authentication data" throw ] } + { [ dup "55" head? ] [ log-response "fatal error" throw ] } + { [ t ] [ "unknown error" throw ] } } cond ; -SYMBOL: multiline - : multiline? ( response -- boolean ) - CHAR: - swap index 3 = ; + ?fourth CHAR: - = ; -: process-multiline ( -- response ) - conn get stream-readln dup - multiline get " " append head? [ - print +: process-multiline ( multiline -- response ) + >r readln r> 2dup " " append head? [ + drop dup log-response ] [ - check-response process-multiline + swap check-response process-multiline ] if ; -: recv-response ( -- response ) - conn get stream-readln - dup multiline? [ - dup 3 head multiline set process-multiline - ] [ ] if ; +: receive-response ( -- response ) + readln + dup multiline? [ 3 head process-multiline ] when ; -: get-ok ( command -- ) - >r conn get r> over stream-write stream-flush - recv-response check-response ; +: get-ok ( -- ) flush receive-response check-response ; -: helo ( -- ) - "HELO " sess get session-domain append "\r\n" append get-ok ; +: send-raw-message ( body to from -- ) + [ + helo get-ok + mail-from get-ok + [ rcpt-to get-ok ] each + data get-ok + send-body get-ok + quit get-ok + ] with-smtp-connection ; -: ehlo ( -- ) - "EHLO " sess get session-domain append "\r\n" append get-ok ; +: validate-header ( string -- string' ) + dup [ "\r\n" member? ] contains? + [ "Invalid header string: " swap append throw ] when ; -: mailfrom ( fromaddr -- ) - "MAIL FROM:<" swap append ">\r\n" append get-ok ; +: prepare-header ( key value -- ) + swap + validate-header % + ": " % + validate-header % ; -: rcptto ( to -- ) - "RCPT TO:<" swap append ">\r\n" append get-ok ; +: prepare-headers ( assoc -- ) + [ [ prepare-header ] "" make , ] assoc-each ; -: (cram-md5-auth) ( -- response ) - swap challenge get - string>md5-hmac hex-string - " " swap append append - >base64 ; +: extract-email ( recepient -- email ) + #! This could be much smarter. + " " last-split1 [ ] [ ] ?if "<" ?head drop ">" ?tail drop ; -: cram-md5-auth ( key login -- ) - "AUTH CRAM-MD5\r\n" get-ok - (cram-md5-auth) "\r\n" append get-ok ; - -: data ( -- ) - "DATA\r\n" get-ok ; +: message-id ( -- string ) + [ + "<" % + 2 big-random # + "-" % + millis # + "@" % + smtp-domain get % + ">" % + ] "" make ; -: start ( -- ) - set-domain ! replaces localhost.localdomain with hostname - do-start - sess get session-esmtp [ - ehlo - ] [ - helo - ] if ; +: simple-headers ( subject to from -- headers to from ) + [ + >r dup ", " join "To" set [ extract-email ] map r> + dup "From" set extract-email + rot "Subject" set + now timestamp>rfc822-string "Date" set + message-id "Message-Id" set + ] { } make-assoc -rot ; -: send-message ( msg -- ) - data - "\r\n" join conn get swap "\r\n" append over stream-write - stream-flush ".\r\n" get-ok ; +: prepare-message ( body headers -- body' ) + [ + prepare-headers + " " , + dup string? [ string-lines ] when % + ] { } make ; -: quit ( -- ) - "QUIT\r\n" get-ok ; +: prepare-simple-message ( body subject to from -- body' to from ) + simple-headers >r >r prepare-message r> r> ; + +: send-message ( body headers to from -- ) + >r >r prepare-message r> r> send-raw-message ; + +: send-simple-message ( body subject to from -- ) + prepare-simple-message send-raw-message ; + +! Dirk's old AUTH CRAM-MD5 code. I don't know anything about +! CRAM MD5, and the old code didn't work properly either, so here +! it is in case anyone wants to fix it later. +! +! check-response used to have this clause: +! { [ dup "334" head? ] [ " " split 1 swap nth base64> challenge set ] } +! +! and the rest of the code was as follows: +! : (cram-md5-auth) ( -- response ) +! swap challenge get +! string>md5-hmac hex-string +! " " swap append append +! >base64 ; +! +! : cram-md5-auth ( key login -- ) +! "AUTH CRAM-MD5\r\n" get-ok +! (cram-md5-auth) "\r\n" append get-ok ; From 2f46a618a694e4eb8cc2830684b21ba64dba0a84 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 01:05:00 -0600 Subject: [PATCH 157/269] Add new word to calendar --- extra/calendar/calendar.factor | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/extra/calendar/calendar.factor b/extra/calendar/calendar.factor index a1fe0a55ea..32c5c0233c 100755 --- a/extra/calendar/calendar.factor +++ b/extra/calendar/calendar.factor @@ -349,13 +349,23 @@ M: timestamp year. ( timestamp -- ) : timestamp>string ( timestamp -- str ) [ (timestamp>string) ] string-out ; +: timestamp>rfc822-string ( timestamp -- str ) + #! RFC822 timestamp format + #! Example: Tue, 15 Nov 1994 08:12:31 +0200 + [ + dup (timestamp>string) + " " write + timestamp-gmt-offset { + { [ dup zero? ] [ drop "GMT" write ] } + { [ dup 0 < ] [ "-" write neg write-00 "00" write ] } + { [ dup 0 > ] [ "+" write write-00 "00" write ] } + } cond + ] string-out ; + : timestamp>http-string ( timestamp -- str ) #! http timestamp format #! Example: Tue, 15 Nov 1994 08:12:31 GMT - >gmt [ - (timestamp>string) - " GMT" write - ] string-out ; + >gmt timestamp>rfc822-string ; : (timestamp>rfc3339) ( timestamp -- ) dup timestamp-year number>string write CHAR: - write1 From dad715e7b0e2fe985eb5b5027632c2ee0d4acaaf Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 01:05:10 -0600 Subject: [PATCH 158/269] Update for io.logging change --- extra/http/server/responders/responders.factor | 4 ++-- extra/http/server/server.factor | 4 ++-- extra/webapps/planet/planet.factor | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/extra/http/server/responders/responders.factor b/extra/http/server/responders/responders.factor index 70503236f6..8f4f146508 100644 --- a/extra/http/server/responders/responders.factor +++ b/extra/http/server/responders/responders.factor @@ -1,8 +1,8 @@ -! Copyright (C) 2004, 2007 Slava Pestov. +! Copyright (C) 2004, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: arrays assocs hashtables html html.elements splitting http io kernel math math.parser namespaces parser sequences -strings io.server vectors assocs.lib ; +strings io.server vectors assocs.lib io.logging ; IN: http.server.responders diff --git a/extra/http/server/server.factor b/extra/http/server/server.factor index 99ed41afa3..f8ac503819 100644 --- a/extra/http/server/server.factor +++ b/extra/http/server/server.factor @@ -1,8 +1,8 @@ -! Copyright (C) 2003, 2007 Slava Pestov. +! Copyright (C) 2003, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: assocs kernel namespaces io strings splitting threads http http.server.responders sequences prettyprint -io.server ; +io.server io.logging ; IN: http.server diff --git a/extra/webapps/planet/planet.factor b/extra/webapps/planet/planet.factor index ede0c579de..b777780e11 100755 --- a/extra/webapps/planet/planet.factor +++ b/extra/webapps/planet/planet.factor @@ -2,7 +2,7 @@ USING: sequences rss arrays concurrency kernel sorting html.elements io assocs namespaces math threads vocabs html furnace http.server.templating calendar math.parser splitting continuations debugger system http.server.responders -xml.writer prettyprint io.server ; +xml.writer prettyprint io.logging ; IN: webapps.planet : print-posting-summary ( posting -- ) @@ -90,7 +90,7 @@ SYMBOL: last-update [ fetch-feed ] [ - swap [ . error. ] with-log-stream f + swap [ . error. ] to-log-stream f ] recover ; : fetch-blogroll ( blogroll -- entries ) From 386d93b6e5a68b3d65a1b342a17cced48c554bdc Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 01:05:28 -0600 Subject: [PATCH 159/269] Moved smtp-server.factor to smtp/server/server.factor --- extra/smtp/smtp-server.factor | 68 ----------------------------------- 1 file changed, 68 deletions(-) delete mode 100644 extra/smtp/smtp-server.factor diff --git a/extra/smtp/smtp-server.factor b/extra/smtp/smtp-server.factor deleted file mode 100644 index e980ee36e6..0000000000 --- a/extra/smtp/smtp-server.factor +++ /dev/null @@ -1,68 +0,0 @@ -! Copyright (C) 2007 Elie CHAFTARI -! See http://factorcode.org/license.txt for BSD license. - -! Usage: 8889 start-server -! $ telnet 127.0.0.1 8889 -! Trying 127.0.0.1... -! Connected to localhost. -! Escape character is '^]'. -! 220 hello -! EHLO -! 220 and..? -! MAIL FROM: -! 220 OK -! RCPT TO: -! 220 OK -! Hi -! 500 ERROR -! DATA -! 354 Enter message, ending with "." on a line by itself -! Hello I am still waiting for your call -! Thanks -! . -! 220 OK -! QUIT -! bye -! Connection closed by foreign host. - -USING: combinators kernel prettyprint io io.server sequences -namespaces ; - -SYMBOL: data-mode - -: process ( -- ) - readln { - { [ [ dup "HELO" head? ] keep "EHLO" head? or ] [ - "220 and..?\r\n" write flush t - ] } - { [ dup "QUIT" = ] [ - "bye\r\n" write flush f - ] } - { [ dup "MAIL FROM:" head? ] [ - "220 OK\r\n" write flush t - ] } - { [ dup "RCPT TO:" head? ] [ - "220 OK\r\n" write flush t - ] } - { [ dup "DATA" = ] [ - data-mode on - "354 Enter message, ending with \".\" on a line by itself\r\n" - write flush t - ] } - { [ dup "." = data-mode get and ] [ - data-mode off - "220 OK\r\n" write flush t - ] } - { [ data-mode get ] [ t ] } - { [ t ] [ - "500 ERROR\r\n" write flush t - ] } - } cond nip [ process ] when ; - -: start-server ( port -- ) - "Starting SMTP server on port " write dup . flush - internet-server "smtp-server" [ - 60000 stdio get set-timeout - "220 hello\r\n" write flush - process - ] with-server ; From b5e1edfeed462036ce9c211006cfca29273bf333 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 01:36:11 -0600 Subject: [PATCH 160/269] Removed obsolete vocab --- extra/tools/test/inference/authors.txt | 1 - extra/tools/test/inference/inference.factor | 15 --------------- 2 files changed, 16 deletions(-) delete mode 100755 extra/tools/test/inference/authors.txt delete mode 100755 extra/tools/test/inference/inference.factor diff --git a/extra/tools/test/inference/authors.txt b/extra/tools/test/inference/authors.txt deleted file mode 100755 index 1901f27a24..0000000000 --- a/extra/tools/test/inference/authors.txt +++ /dev/null @@ -1 +0,0 @@ -Slava Pestov diff --git a/extra/tools/test/inference/inference.factor b/extra/tools/test/inference/inference.factor deleted file mode 100755 index cc77f4910d..0000000000 --- a/extra/tools/test/inference/inference.factor +++ /dev/null @@ -1,15 +0,0 @@ -! Copyright (C) 2007 Slava Pestov. -! See http://factorcode.org/license.txt for BSD license. -USING: effects sequences kernel arrays quotations inference -tools.test words ; -IN: tools.test.inference - -: short-effect - dup effect-in length swap effect-out length 2array ; - -: unit-test-effect ( effect quot -- ) - >r 1quotation r> [ infer short-effect ] curry unit-test ; - -: must-infer ( word/quot -- ) - dup word? [ 1quotation ] when - [ infer drop ] curry [ ] swap unit-test ; From 6204f5698130c8d35056593e60b7c8ac79dab282 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 7 Feb 2008 13:48:49 -0600 Subject: [PATCH 161/269] fix gmt-offset on windows --- extra/calendar/windows/windows.factor | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/extra/calendar/windows/windows.factor b/extra/calendar/windows/windows.factor index 320400822c..afc040ef75 100755 --- a/extra/calendar/windows/windows.factor +++ b/extra/calendar/windows/windows.factor @@ -9,5 +9,4 @@ T{ windows-calendar } calendar-backend set-global M: windows-calendar gmt-offset ( -- float ) "TIME_ZONE_INFORMATION" [ GetTimeZoneInformation win32-error=0/f ] keep - [ TIME_ZONE_INFORMATION-Bias ] keep - TIME_ZONE_INFORMATION-DaylightBias + 60 /f neg ; + TIME_ZONE_INFORMATION-Bias 60 / neg ; From e05bb24a697b5721be7ed6d5caa9e1136b05f1ee Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 7 Feb 2008 14:17:07 -0600 Subject: [PATCH 162/269] make rfc822-string print fractional times fix windows gmt-offset yet again -- bad return value --- extra/calendar/calendar.factor | 16 +++++++++++----- extra/calendar/windows/windows.factor | 5 ++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/extra/calendar/calendar.factor b/extra/calendar/calendar.factor index 32c5c0233c..012080d3b7 100755 --- a/extra/calendar/calendar.factor +++ b/extra/calendar/calendar.factor @@ -349,17 +349,23 @@ M: timestamp year. ( timestamp -- ) : timestamp>string ( timestamp -- str ) [ (timestamp>string) ] string-out ; +: (write-gmt-offset) ( ratio -- ) + 1 /mod swap write-00 60 * write-00 ; + +: write-gmt-offset ( gmt-offset -- ) + { + { [ dup zero? ] [ drop "GMT" write ] } + { [ dup 0 < ] [ "-" write neg (write-gmt-offset) ] } + { [ dup 0 > ] [ "+" write (write-gmt-offset) ] } + } cond ; + : timestamp>rfc822-string ( timestamp -- str ) #! RFC822 timestamp format #! Example: Tue, 15 Nov 1994 08:12:31 +0200 [ dup (timestamp>string) " " write - timestamp-gmt-offset { - { [ dup zero? ] [ drop "GMT" write ] } - { [ dup 0 < ] [ "-" write neg write-00 "00" write ] } - { [ dup 0 > ] [ "+" write write-00 "00" write ] } - } cond + timestamp-gmt-offset write-gmt-offset ] string-out ; : timestamp>http-string ( timestamp -- str ) diff --git a/extra/calendar/windows/windows.factor b/extra/calendar/windows/windows.factor index afc040ef75..9e34fdac00 100755 --- a/extra/calendar/windows/windows.factor +++ b/extra/calendar/windows/windows.factor @@ -6,7 +6,10 @@ TUPLE: windows-calendar ; T{ windows-calendar } calendar-backend set-global +: TIME_ZONE_ID_INVALID HEX: ffffffff ; inline + M: windows-calendar gmt-offset ( -- float ) "TIME_ZONE_INFORMATION" - [ GetTimeZoneInformation win32-error=0/f ] keep + dup GetTimeZoneInformation + TIME_ZONE_ID_INVALID = [ win32-error ] when TIME_ZONE_INFORMATION-Bias 60 / neg ; From 0570449ffdabeaad6dc49e4489e178200568d1cc Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Thu, 7 Feb 2008 15:14:40 -0600 Subject: [PATCH 163/269] Tweak builder --- extra/builder/builder.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/builder/builder.factor b/extra/builder/builder.factor index 5bfd5e01cf..5e992ccc81 100755 --- a/extra/builder/builder.factor +++ b/extra/builder/builder.factor @@ -69,8 +69,8 @@ VAR: stamp "git" "pull" "--no-summary" - ! "git://factorcode.org/git/factor.git" - "http://dharmatech.onigirihouse.com/factor.git" + "git://factorcode.org/git/factor.git" + ! "http://dharmatech.onigirihouse.com/factor.git" "master" } run-process process-status From 5310a2cabea0341d33f7096e4e7ff9f043717bc0 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 17:07:43 -0600 Subject: [PATCH 164/269] New logging framework --- .../distributed/distributed.factor | 2 +- .../http/server/responders/responders.factor | 39 +++--- extra/http/server/server.factor | 6 +- extra/io/logging/logging-docs.factor | 26 ---- extra/io/logging/logging.factor | 47 ------- extra/io/logging/summary.txt | 1 - extra/io/server/server.factor | 40 +++--- extra/logging/analysis/analysis.factor | 69 ++++++++++ .../logging => logging/analysis}/authors.txt | 2 +- extra/logging/analysis/summary.txt | 1 + extra/logging/authors.txt | 1 + extra/logging/insomniac/authors.txt | 1 + extra/logging/insomniac/insomniac.factor | 49 +++++++ extra/logging/insomniac/summary.txt | 1 + extra/logging/logging.factor | 122 ++++++++++++++++++ extra/logging/parser/authors.txt | 1 + extra/logging/parser/parser.factor | 66 ++++++++++ extra/logging/parser/summary.txt | 1 + extra/logging/server/authors.txt | 1 + extra/logging/server/server.factor | 101 +++++++++++++++ extra/logging/server/summary.txt | 1 + extra/logging/summary.txt | 1 + extra/raptor/cron/cron.factor | 6 +- extra/smtp/smtp-tests.factor | 2 +- extra/smtp/smtp.factor | 47 ++++--- extra/tools/annotations/annotations.factor | 20 ++- extra/tools/browser/browser.factor | 1 + extra/webapps/file/file.factor | 18 ++- extra/webapps/planet/planet.factor | 20 +-- 29 files changed, 523 insertions(+), 170 deletions(-) mode change 100644 => 100755 extra/concurrency/distributed/distributed.factor mode change 100644 => 100755 extra/http/server/responders/responders.factor mode change 100644 => 100755 extra/http/server/server.factor delete mode 100644 extra/io/logging/logging-docs.factor delete mode 100644 extra/io/logging/logging.factor delete mode 100644 extra/io/logging/summary.txt create mode 100755 extra/logging/analysis/analysis.factor rename extra/{io/logging => logging/analysis}/authors.txt (92%) mode change 100644 => 100755 create mode 100755 extra/logging/analysis/summary.txt create mode 100755 extra/logging/authors.txt create mode 100755 extra/logging/insomniac/authors.txt create mode 100755 extra/logging/insomniac/insomniac.factor create mode 100755 extra/logging/insomniac/summary.txt create mode 100755 extra/logging/logging.factor create mode 100755 extra/logging/parser/authors.txt create mode 100755 extra/logging/parser/parser.factor create mode 100755 extra/logging/parser/summary.txt create mode 100755 extra/logging/server/authors.txt create mode 100755 extra/logging/server/server.factor create mode 100755 extra/logging/server/summary.txt create mode 100755 extra/logging/summary.txt mode change 100644 => 100755 extra/raptor/cron/cron.factor mode change 100644 => 100755 extra/smtp/smtp-tests.factor mode change 100644 => 100755 extra/smtp/smtp.factor diff --git a/extra/concurrency/distributed/distributed.factor b/extra/concurrency/distributed/distributed.factor old mode 100644 new mode 100755 index 9024c0630f..83052b803a --- a/extra/concurrency/distributed/distributed.factor +++ b/extra/concurrency/distributed/distributed.factor @@ -14,7 +14,7 @@ C: node : node-server ( port -- ) internet-server - "concurrency" + "concurrency.distributed" [ handle-node-client ] with-server ; : send-to-node ( msg pid host port -- ) diff --git a/extra/http/server/responders/responders.factor b/extra/http/server/responders/responders.factor old mode 100644 new mode 100755 index 8f4f146508..e4e0e257c4 --- a/extra/http/server/responders/responders.factor +++ b/extra/http/server/responders/responders.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: arrays assocs hashtables html html.elements splitting http io kernel math math.parser namespaces parser sequences -strings io.server vectors assocs.lib io.logging ; +strings io.server vectors assocs.lib logging ; IN: http.server.responders @@ -22,7 +22,7 @@ SYMBOL: responders

write

; : error-head ( error -- ) - dup log-error response + response H{ { "Content-Type" V{ "text/html" } } } print-header nl ; : httpd-error ( error -- ) @@ -30,6 +30,8 @@ SYMBOL: responders dup error-head "head" "method" get = [ drop ] [ error-body ] if ; +\ httpd-error ERROR add-error-logging + : bad-request ( -- ) [ ! Make httpd-error print a body @@ -84,17 +86,21 @@ SYMBOL: max-post-request : read-post-request ( header -- str hash ) content-length [ read dup query>hash ] [ f f ] if* ; -: log-headers ( hash -- ) +LOG: log-headers DEBUG + +: interesting-headers ( assoc -- string ) [ - drop { - "user-agent" - "referer" - "x-forwarded-for" - "host" - } member? - ] assoc-subset [ - ": " swap 3append log-message - ] multi-assoc-each ; + [ + drop { + "user-agent" + "referer" + "x-forwarded-for" + "host" + } member? + ] assoc-subset [ + ": " swap 3append % "\n" % + ] multi-assoc-each + ] "" make ; : prepare-url ( url -- url ) #! This is executed in the with-request namespace. @@ -105,7 +111,7 @@ SYMBOL: max-post-request : prepare-header ( -- ) read-header dup "header" set - dup log-headers + dup interesting-headers log-headers read-post-request "response" set "raw-response" set ; ! Responders are called in a new namespace with these @@ -177,9 +183,6 @@ SYMBOL: max-post-request "/" "responder-url" set "default" responder call-responder ; -: log-responder ( path -- ) - "Calling responder " swap append log-message ; - : trim-/ ( url -- url ) #! Trim a leading /, if there is one. "/" ?head drop ; @@ -199,13 +202,15 @@ SYMBOL: max-post-request #! /foo/bar... - default responder used #! /responder/foo/bar - responder foo, argument bar vhost [ - dup log-responder trim-/ "responder/" ?head [ + trim-/ "responder/" ?head [ serve-explicit-responder ] [ serve-default-responder ] if ] bind ; +\ serve-responder DEBUG add-input-logging + : no-such-responder ( -- ) "404 No such responder" httpd-error ; diff --git a/extra/http/server/server.factor b/extra/http/server/server.factor old mode 100644 new mode 100755 index f8ac503819..eca2253e2a --- a/extra/http/server/server.factor +++ b/extra/http/server/server.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: assocs kernel namespaces io strings splitting threads http http.server.responders sequences prettyprint -io.server io.logging ; +io.server logging ; IN: http.server @@ -36,7 +36,6 @@ IN: http.server [ (handle-request) serve-responder ] with-scope ; : parse-request ( request -- ) - dup log-message " " split1 dup [ " HTTP" split1 drop url>path secure-path dup [ swap handle-request @@ -47,8 +46,9 @@ IN: http.server 2drop bad-request ] if ; +\ parse-request NOTICE add-input-logging + : httpd ( port -- ) - "Starting HTTP server on port " write dup . flush internet-server "http.server" [ 60000 stdio get set-timeout readln [ parse-request ] when* diff --git a/extra/io/logging/logging-docs.factor b/extra/io/logging/logging-docs.factor deleted file mode 100644 index 6cd03ce212..0000000000 --- a/extra/io/logging/logging-docs.factor +++ /dev/null @@ -1,26 +0,0 @@ -IN: io.logging -USING: help.markup help.syntax io ; - -HELP: log-stream -{ $var-description "Holds an output stream for logging messages." } -{ $see-also log-error log-client with-logging } ; - -HELP: log-message -{ $values { "str" "a string" } } -{ $description "Logs a message to the log stream. If " { $link log-stream } " is not set, logs to the " { $link stdio } " stream." } -{ $see-also log-error log-client } ; - -HELP: log-error -{ $values { "str" "a string" } } -{ $description "Logs an error message." } -{ $see-also log-message log-client } ; - -HELP: log-client -{ $values { "client" "a client socket stream" } } -{ $description "Logs an incoming client connection." } -{ $see-also log-message log-error } ; - -HELP: with-logging -{ $values { "service" "a string or " { $link f } } { "quot" "a quotation" } } -{ $description "Calls the quotation in a new dynamic scope where the " { $link log-stream } " is set to a file stream appending to a log file (if " { $snippet "service" } " is not " { $link f } ") or the " { $link stdio } " stream at the time " { $link with-logging } " is called (if " { $snippet "service" } " is " { $link f } ")." } ; - diff --git a/extra/io/logging/logging.factor b/extra/io/logging/logging.factor deleted file mode 100644 index bd9dc0862e..0000000000 --- a/extra/io/logging/logging.factor +++ /dev/null @@ -1,47 +0,0 @@ -! Copyright (C) 2003, 2008 Slava Pestov. -! See http://factorcode.org/license.txt for BSD license. -USING: namespaces kernel io calendar sequences io.files -io.sockets continuations prettyprint ; -IN: io.logging - -SYMBOL: log-stream - -: to-log-stream ( quot -- ) - log-stream get swap with-stream* ; inline - -: log-message ( str -- ) - [ - "[" write now timestamp>string write "] " write - print flush - ] to-log-stream ; - -: log-error ( str -- ) "Error: " swap append log-message ; - -: log-client ( client -- ) - "Accepted connection from " - swap client-stream-addr unparse append log-message ; - -: log-file ( service -- path ) - ".log" append resource-path ; - -: with-log-stream ( stream quot -- ) - log-stream get [ nip call ] [ - log-stream swap with-variable - ] if ; inline - -: with-log-file ( file quot -- ) - >r r> - [ with-log-stream ] curry - with-disposal ; inline - -: with-log-stdio ( quot -- ) - stdio get swap with-log-stream ; inline - -: with-logging ( service quot -- ) - over [ - >r log-file - "Writing log messages to " write dup print flush r> - with-log-file - ] [ - nip with-log-stdio - ] if ; inline diff --git a/extra/io/logging/summary.txt b/extra/io/logging/summary.txt deleted file mode 100644 index 0edce8f0cf..0000000000 --- a/extra/io/logging/summary.txt +++ /dev/null @@ -1 +0,0 @@ -Basic logging framework for server applications diff --git a/extra/io/server/server.factor b/extra/io/server/server.factor index 182712c984..829da27f6e 100755 --- a/extra/io/server/server.factor +++ b/extra/io/server/server.factor @@ -1,32 +1,34 @@ ! Copyright (C) 2003, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: io io.sockets io.files io.logging continuations kernel +USING: io io.sockets io.files logging continuations kernel math math.parser namespaces parser sequences strings prettyprint debugger quotations calendar qualified ; QUALIFIED: concurrency IN: io.server -: with-client ( quot client -- ) - dup log-client - [ swap with-stream ] 2curry concurrency:spawn drop ; inline +LOG: accepted-connection NOTICE + +: with-client ( client quot -- ) + [ + over client-stream-addr accepted-connection + with-stream* + ] curry with-disposal ; inline + +\ with-client NOTICE add-error-logging : accept-loop ( server quot -- ) - [ swap accept with-client ] 2keep accept-loop ; inline + [ + >r accept r> [ with-client ] 2curry concurrency:spawn + ] 2keep accept-loop ; inline : server-loop ( server quot -- ) [ accept-loop ] curry with-disposal ; inline : spawn-server ( addrspec quot -- ) - "Waiting for connections on " pick unparse append - log-message - [ - >r r> server-loop - ] [ - "Cannot spawn server: " print - print-error - 2drop - ] recover ; inline + >r r> server-loop ; inline + +\ spawn-server NOTICE add-error-logging : local-server ( port -- seq ) "localhost" swap t resolve-host ; @@ -39,19 +41,21 @@ IN: io.server [ spawn-server ] curry concurrency:parallel-each ] curry with-logging ; inline -: log-datagram ( addrspec -- ) - "Received datagram from " swap unparse append log-message ; +: received-datagram ( addrspec -- ) drop ; + +\ received-datagram NOTICE add-input-logging : datagram-loop ( quot datagram -- ) [ - [ receive dup log-datagram >r swap call r> ] keep + [ receive dup received-datagram >r swap call r> ] keep pick [ send ] [ 3drop ] keep ] 2keep datagram-loop ; inline : spawn-datagrams ( quot addrspec -- ) - "Waiting for datagrams on " over unparse append log-message [ datagram-loop ] with-disposal ; inline +\ spawn-datagrams NOTICE add-input-logging + : with-datagrams ( seq service quot -- ) [ [ swap spawn-datagrams ] curry concurrency:parallel-each diff --git a/extra/logging/analysis/analysis.factor b/extra/logging/analysis/analysis.factor new file mode 100755 index 0000000000..df53a8e70b --- /dev/null +++ b/extra/logging/analysis/analysis.factor @@ -0,0 +1,69 @@ +! Copyright (C) 2008 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel sequences namespaces words assocs logging sorting +prettyprint io io.styles strings logging.parser ; +IN: logging.analysis + +SYMBOL: word-names +SYMBOL: errors +SYMBOL: word-histogram +SYMBOL: message-histogram + +: analyze-entry ( entry -- ) + dup second ERROR eq? [ dup errors get push ] when + 1 over third word-histogram get at+ + dup third word-names get member? [ + 1 over 1 tail message-histogram get at+ + ] when + drop ; + +: analyze-entries ( entries word-names -- errors word-histogram message-histogram ) + [ + word-names set + V{ } clone errors set + H{ } clone word-histogram set + H{ } clone message-histogram set + + [ + analyze-entry + ] each + + errors get + word-histogram get + message-histogram get + ] with-scope ; + +: histogram. ( assoc quot -- ) + standard-table-style [ + >r >alist sort-values r> [ + [ >r swap r> with-cell pprint-cell ] with-row + ] curry assoc-each + ] tabular-output ; + +: log-entry. + [ + dup first [ write ] with-cell + dup second [ pprint ] with-cell + dup third [ write ] with-cell + fourth "\n" join [ write ] with-cell + ] with-row ; + +: errors. ( errors -- ) + standard-table-style + [ [ log-entry. ] each ] tabular-output ; + +: analysis. ( errors word-histogram message-histogram -- ) + "==== INTERESTING MESSAGES:" print nl + "Total: " write dup values sum . nl + [ + dup second write ": " write third "\n" join write + ] histogram. + nl + "==== WORDS:" print nl + [ write ] histogram. + nl + "==== ERRORS:" print nl + errors. ; + +: log-analysis ( lines word-names -- ) + >r parse-log r> analyze-entries analysis. ; diff --git a/extra/io/logging/authors.txt b/extra/logging/analysis/authors.txt old mode 100644 new mode 100755 similarity index 92% rename from extra/io/logging/authors.txt rename to extra/logging/analysis/authors.txt index 1901f27a24..56f4654064 --- a/extra/io/logging/authors.txt +++ b/extra/logging/analysis/authors.txt @@ -1 +1 @@ -Slava Pestov +Slava Pestov diff --git a/extra/logging/analysis/summary.txt b/extra/logging/analysis/summary.txt new file mode 100755 index 0000000000..e614abca96 --- /dev/null +++ b/extra/logging/analysis/summary.txt @@ -0,0 +1 @@ +Analyze logs and produce summaries diff --git a/extra/logging/authors.txt b/extra/logging/authors.txt new file mode 100755 index 0000000000..56f4654064 --- /dev/null +++ b/extra/logging/authors.txt @@ -0,0 +1 @@ +Slava Pestov diff --git a/extra/logging/insomniac/authors.txt b/extra/logging/insomniac/authors.txt new file mode 100755 index 0000000000..56f4654064 --- /dev/null +++ b/extra/logging/insomniac/authors.txt @@ -0,0 +1 @@ +Slava Pestov diff --git a/extra/logging/insomniac/insomniac.factor b/extra/logging/insomniac/insomniac.factor new file mode 100755 index 0000000000..b065dec9d3 --- /dev/null +++ b/extra/logging/insomniac/insomniac.factor @@ -0,0 +1,49 @@ +! Copyright (C) 2008 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: logging.analysis logging.server logging smtp io.sockets +kernel io.files io.streams.string namespaces raptor.cron ; +IN: logging.insomniac + +SYMBOL: insomniac-config + +SYMBOL: insomniac-smtp-host +SYMBOL: insomniac-smtp-port +SYMBOL: insomniac-sender +SYMBOL: insomniac-recipients + +: ?log-analysis ( service word-names -- string/f ) + >r log-path 1 log# dup exists? [ + file-lines r> [ log-analysis ] string-out + ] [ + r> 2drop f + ] if ; + +: with-insomniac-smtp ( quot -- ) + [ + insomniac-smtp-host get [ smtp-host set ] when* + insomniac-smtp-port get [ smtp-port set ] when* + call + ] with-scope ; inline + +: email-subject ( service -- string ) + [ "[INSOMNIAC] " % % " on " % host-name % ] "" make ; + +: (email-log-report) ( service word-names -- ) + [ + over >r + ?log-analysis dup [ + r> email-subject + insomniac-recipients get + insomniac-sender get + send-simple-message + ] [ r> 2drop ] if + ] with-insomniac-smtp ; + +: email-log-report ( service word-names -- ) + (email-log-report) ; + +\ email-log-report NOTICE add-error-logging + +: schedule-insomniac ( service word-names -- ) + { 25 } { 6 } f f f -rot + [ email-log-report ] 2curry schedule ; diff --git a/extra/logging/insomniac/summary.txt b/extra/logging/insomniac/summary.txt new file mode 100755 index 0000000000..ddd21fb5b9 --- /dev/null +++ b/extra/logging/insomniac/summary.txt @@ -0,0 +1 @@ +Task which rotates logs and e-mails summaries diff --git a/extra/logging/logging.factor b/extra/logging/logging.factor new file mode 100755 index 0000000000..71ea247567 --- /dev/null +++ b/extra/logging/logging.factor @@ -0,0 +1,122 @@ +! Copyright (C) 2003, 2008 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: logging.server sequences namespaces concurrency +words kernel arrays shuffle tools.annotations +prettyprint.config prettyprint debugger io.streams.string +splitting continuations effects arrays.lib parser strings +combinators.lib ; +IN: logging + +SYMBOL: DEBUG +SYMBOL: NOTICE +SYMBOL: WARNING +SYMBOL: ERROR +SYMBOL: CRITICAL + +: log-levels + { DEBUG NOTICE NOTICE WARNING ERROR CRITICAL } ; + +: send-to-log-server ( array string -- ) + add* "log-server" get send ; + +SYMBOL: log-service + +: check-log-message + pick string? + pick word? + pick word? and and + [ "Bad parameters to log-message" throw ] unless ; + +: log-message ( msg word level -- ) + check-log-message + log-service get dup [ + >r >r >r string-lines r> word-name r> word-name r> + 4array "log-message" send-to-log-server + ] [ + 4drop + ] if ; + +: rotate-logs ( -- ) + { } "rotate-logs" send-to-log-server ; + +: close-log-files ( -- ) + { } "close-log-files" send-to-log-server ; + +: with-logging ( service quot -- ) + log-service swap with-variable ; inline + +! Aspect-oriented programming idioms + +message ( obj -- inputs>message ) + dup one-string? [ first ] [ + H{ + { string-limit f } + { line-limit 1 } + { nesting-limit 3 } + { margin 0 } + } clone [ unparse ] bind + ] if ; + +PRIVATE> + +: (define-logging) ( word level quot -- ) + >r >r dup r> r> 2curry annotate ; + +: call-logging-quot ( quot word level -- quot' ) + "called" -rot [ log-message ] 3curry swap compose ; + +: add-logging ( word level -- ) + [ call-logging-quot ] (define-logging) ; + +: log-inputs ( n word level -- ) + log-service get [ + >r >r [ ndup ] keep narray inputs>message + r> r> log-message + ] [ + 3drop + ] if ; inline + +: input# stack-effect effect-in length ; + +: input-logging-quot ( quot word level -- quot' ) + over input# -rot [ log-inputs ] 3curry swap compose ; + +: add-input-logging ( word level -- ) + [ input-logging-quot ] (define-logging) ; + +: (log-error) ( object word level -- ) + log-service get [ + >r >r [ print-error ] string-out r> r> log-message + ] [ + 2drop rethrow + ] if ; + +: log-error ( object word -- ) ERROR (log-error) ; + +: log-critical ( object word -- ) CRITICAL (log-error) ; + +: error-logging-quot ( quot word -- quot' ) + dup stack-effect effect-in length + [ >r log-error r> ndrop ] 2curry + [ recover ] 2curry ; + +: add-error-logging ( word level -- ) + [ over >r input-logging-quot r> error-logging-quot ] + (define-logging) ; + +: LOG: + #! Syntax: name level + CREATE + dup reset-generic + dup scan-word + [ >r >r 1array inputs>message r> r> log-message ] 2curry + define ; parsing diff --git a/extra/logging/parser/authors.txt b/extra/logging/parser/authors.txt new file mode 100755 index 0000000000..56f4654064 --- /dev/null +++ b/extra/logging/parser/authors.txt @@ -0,0 +1 @@ +Slava Pestov diff --git a/extra/logging/parser/parser.factor b/extra/logging/parser/parser.factor new file mode 100755 index 0000000000..f1cb7aa17e --- /dev/null +++ b/extra/logging/parser/parser.factor @@ -0,0 +1,66 @@ +! Copyright (C) 2008 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: parser-combinators memoize kernel sequences +logging arrays words strings vectors io io.files +namespaces combinators combinators.lib logging.server ; +IN: logging.parser + +: string-of satisfy [ >string ] <@ ; + +: 'date' + [ CHAR: ] eq? not ] string-of + "[" "]" surrounded-by ; + +: 'log-level' + log-levels [ + [ word-name token ] keep [ nip ] curry <@ + ] map ; + +: 'word-name' + [ " :" member? not ] string-of ; + +SYMBOL: malformed + +: 'malformed-line' + [ drop t ] string-of [ malformed swap 2array ] <@ ; + +: 'log-message' + [ drop t ] string-of [ 1vector ] <@ ; + +MEMO: 'log-line' ( -- parser ) + 'date' " " token <& + 'log-level' " " token <& <&> + 'word-name' ": " token <& <:&> + 'log-message' <:&> + 'malformed-line' <|> ; + +: parse-log-line ( string -- entry ) + 'log-line' parse-1 ; + +: malformed? ( line -- ? ) + first malformed eq? ; + +: multiline? ( line -- ? ) + first first CHAR: - = ; + +: malformed-line + "Warning: malformed log line:" print + second print ; + +: add-multiline ( line -- ) + building get empty? [ + "Warning: log begins with multiline entry" print drop + ] [ + fourth first building get peek fourth push + ] if ; + +: parse-log ( lines -- entries ) + [ + [ + parse-log-line { + { [ dup malformed? ] [ malformed-line ] } + { [ dup multiline? ] [ add-multiline ] } + { [ t ] [ , ] } + } cond + ] each + ] { } make ; diff --git a/extra/logging/parser/summary.txt b/extra/logging/parser/summary.txt new file mode 100755 index 0000000000..cd5c68b156 --- /dev/null +++ b/extra/logging/parser/summary.txt @@ -0,0 +1 @@ +Log parser diff --git a/extra/logging/server/authors.txt b/extra/logging/server/authors.txt new file mode 100755 index 0000000000..56f4654064 --- /dev/null +++ b/extra/logging/server/authors.txt @@ -0,0 +1 @@ +Slava Pestov diff --git a/extra/logging/server/server.factor b/extra/logging/server/server.factor new file mode 100755 index 0000000000..cddcea8d70 --- /dev/null +++ b/extra/logging/server/server.factor @@ -0,0 +1,101 @@ +! Copyright (C) 2008 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: namespaces kernel io calendar sequences io.files +io.sockets continuations prettyprint assocs math.parser +words debugger math combinators concurrency arrays init +math.ranges strings ; +IN: logging.server + +: log-root ( -- string ) + \ log-root get "logs" resource-path or ; + +: log-path ( service -- path ) + log-root swap path+ ; + +: log# ( path n -- path' ) + number>string ".log" append path+ ; + +SYMBOL: log-files + +: open-log-stream ( service -- stream ) + log-path + dup make-directories + 1 log# ; + +: log-stream ( service -- stream ) + log-files get [ open-log-stream ] cache ; + +: (write-message) ( msg word-name level multi? -- ) + [ + "[" write 20 CHAR: - write "] " write + ] [ + "[" write now (timestamp>rfc3339) "] " write + ] if + write bl write ": " write print ; + +: write-message ( msg word-name level -- ) + rot [ empty? not ] subset { + { [ dup empty? ] [ 3drop ] } + { [ dup length 1 = ] [ first -rot f (write-message) ] } + { [ t ] [ + [ first -rot f (write-message) ] 3keep + 1 tail -rot [ t (write-message) ] 2curry each + ] } + } cond ; + +: (log-message) ( msg -- ) + #! msg: { msg word-name level service } + first4 log-stream [ write-message flush ] with-stream* ; + +: try-dispose ( stream -- ) + [ dispose ] curry [ error. ] recover ; + +: close-log-file ( service -- ) + log-files get delete-at* + [ try-dispose ] [ drop ] if ; + +: (close-log-files) ( -- ) + log-files get + dup values [ try-dispose ] each + clear-assoc ; + +: keep-logs 10 ; + +: ?delete-file ( path -- ) + dup exists? [ delete-file ] [ drop ] if ; + +: delete-oldest keep-logs log# ?delete-file ; + +: ?rename-file ( old new -- ) + over exists? [ rename-file ] [ 2drop ] if ; + +: advance-log ( path n -- ) + [ 1- log# ] 2keep log# ?rename-file ; + +: rotate-log ( service -- ) + dup close-log-file + log-path + dup delete-oldest + keep-logs 1 [a,b] [ advance-log ] with each ; + +: (rotate-logs) ( -- ) + (close-log-files) + log-root directory [ drop rotate-log ] assoc-each ; + +: log-server-loop + [ + receive unclip { + { "log-message" [ (log-message) ] } + { "rotate-logs" [ drop (rotate-logs) ] } + { "close-log-files" [ drop (close-log-files) ] } + } case + ] [ error. (close-log-files) ] recover + log-server-loop ; + +: log-server ( -- ) + [ log-server-loop ] spawn "log-server" set-global ; + +[ + H{ } clone log-files set-global + log-server +] "logging" add-init-hook diff --git a/extra/logging/server/summary.txt b/extra/logging/server/summary.txt new file mode 100755 index 0000000000..bebf3465f1 --- /dev/null +++ b/extra/logging/server/summary.txt @@ -0,0 +1 @@ +Distributed concurrency log server diff --git a/extra/logging/summary.txt b/extra/logging/summary.txt new file mode 100755 index 0000000000..dbf29c2112 --- /dev/null +++ b/extra/logging/summary.txt @@ -0,0 +1 @@ +AOP Logging framework with support for log rotation and machine-readable logs diff --git a/extra/raptor/cron/cron.factor b/extra/raptor/cron/cron.factor old mode 100644 new mode 100755 index 8158a03286..e20598d2eb --- a/extra/raptor/cron/cron.factor +++ b/extra/raptor/cron/cron.factor @@ -1,6 +1,6 @@ USING: kernel namespaces threads sequences calendar - combinators.cleave combinators.lib ; + combinators.cleave combinators.lib debugger ; IN: raptor.cron @@ -43,9 +43,9 @@ C: when ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! : recurring-job ( when quot -- ) - [ swap when=now? [ call ] [ drop ] if 60000 sleep ] [ recurring-job ] 2bi ; + [ swap when=now? [ try ] [ drop ] if 60000 sleep ] [ recurring-job ] 2bi ; -: schedule ( when quot -- ) [ recurring-job ] curry curry in-thread ; +: schedule ( when quot -- ) [ recurring-job ] 2curry in-thread ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! diff --git a/extra/smtp/smtp-tests.factor b/extra/smtp/smtp-tests.factor old mode 100644 new mode 100755 index 9a357fdc7d..eda8d7cc1f --- a/extra/smtp/smtp-tests.factor +++ b/extra/smtp/smtp-tests.factor @@ -1,4 +1,4 @@ -USING: smtp tools.test io.streams.string io.logging threads +USING: smtp tools.test io.streams.string threads smtp.server kernel sequences namespaces ; IN: temporary diff --git a/extra/smtp/smtp.factor b/extra/smtp/smtp.factor old mode 100644 new mode 100755 index 77bfb6cd82..211fbbcabd --- a/extra/smtp/smtp.factor +++ b/extra/smtp/smtp.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2007, 2008 Elie CHAFTARI, Dirk Vleugels, Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: namespaces io kernel io.logging io.sockets sequences +USING: namespaces io kernel logging io.sockets sequences combinators sequences.lib splitting assocs strings math.parser random system calendar ; @@ -12,21 +12,18 @@ SYMBOL: smtp-port 25 smtp-port set-global SYMBOL: read-timeout 60000 read-timeout set-global SYMBOL: esmtp t esmtp set-global -: log-smtp-connection ( host port -- ) - [ - "Establishing SMTP connection to " % swap % ":" % # - ] "" make log-message ; +: log-smtp-connection ( host port -- ) 2drop ; + +\ log-smtp-connection NOTICE add-input-logging : with-smtp-connection ( quot -- ) - [ - smtp-host get smtp-port get - 2dup log-smtp-connection - [ - smtp-domain [ host-name or ] change - read-timeout get stdio get set-timeout - call - ] with-stream - ] with-log-stdio ; inline + smtp-host get smtp-port get + 2dup log-smtp-connection + [ + smtp-domain [ host-name or ] change + read-timeout get stdio get set-timeout + call + ] with-stream ; inline : crlf "\r\n" write ; @@ -58,20 +55,20 @@ SYMBOL: esmtp t esmtp set-global : quit ( -- ) "QUIT" write crlf ; -: log-response ( string -- ) "SMTP: " swap append log-message ; +LOG: smtp-response DEBUG : check-response ( response -- ) { - { [ dup "220" head? ] [ log-response ] } - { [ dup "235" swap subseq? ] [ log-response ] } - { [ dup "250" head? ] [ log-response ] } - { [ dup "221" head? ] [ log-response ] } - { [ dup "bye" head? ] [ log-response ] } + { [ dup "220" head? ] [ smtp-response ] } + { [ dup "235" swap subseq? ] [ smtp-response ] } + { [ dup "250" head? ] [ smtp-response ] } + { [ dup "221" head? ] [ smtp-response ] } + { [ dup "bye" head? ] [ smtp-response ] } { [ dup "4" head? ] [ "server busy" throw ] } - { [ dup "354" head? ] [ log-response ] } - { [ dup "50" head? ] [ log-response "syntax error" throw ] } - { [ dup "53" head? ] [ log-response "invalid authentication data" throw ] } - { [ dup "55" head? ] [ log-response "fatal error" throw ] } + { [ dup "354" head? ] [ smtp-response ] } + { [ dup "50" head? ] [ smtp-response "syntax error" throw ] } + { [ dup "53" head? ] [ smtp-response "invalid authentication data" throw ] } + { [ dup "55" head? ] [ smtp-response "fatal error" throw ] } { [ t ] [ "unknown error" throw ] } } cond ; @@ -80,7 +77,7 @@ SYMBOL: esmtp t esmtp set-global : process-multiline ( multiline -- response ) >r readln r> 2dup " " append head? [ - drop dup log-response + drop dup smtp-response ] [ swap check-response process-multiline ] if ; diff --git a/extra/tools/annotations/annotations.factor b/extra/tools/annotations/annotations.factor index cd0d574083..6dee51cbc0 100755 --- a/extra/tools/annotations/annotations.factor +++ b/extra/tools/annotations/annotations.factor @@ -7,23 +7,31 @@ IN: tools.annotations : reset ( word -- ) dup "unannotated-def" word-prop [ [ - dup "unannotated-def" word-prop define + dup dup "unannotated-def" word-prop define ] with-compilation-unit + f "unannotated-def" set-word-prop ] [ drop ] if ; : annotate ( word quot -- ) + over "unannotated-def" word-prop [ + "Cannot annotate a word twice" throw + ] when [ over dup word-def "unannotated-def" set-word-prop >r dup word-def r> call define ] with-compilation-unit ; inline +: word-inputs ( word -- seq ) + stack-effect [ + >r datastack r> effect-in length tail* + ] [ + datastack + ] if* ; + : entering ( str -- ) "/-- Entering: " write dup . - stack-effect [ - >r datastack r> effect-in length tail* stack. - ] [ - .s - ] if* "\\--" print flush ; + word-inputs stack. + "\\--" print flush ; : leaving ( str -- ) "/-- Leaving: " write dup . diff --git a/extra/tools/browser/browser.factor b/extra/tools/browser/browser.factor index 7aefbc8aaa..48de69b025 100755 --- a/extra/tools/browser/browser.factor +++ b/extra/tools/browser/browser.factor @@ -127,6 +127,7 @@ MEMO: all-vocabs-seq ( -- seq ) { [ "windows." ?head ] [ t ] } { [ "cocoa" ?head ] [ t ] } { [ ".test" ?tail ] [ t ] } + { [ "raptor" ?head ] [ t ] } { [ dup "tools.deploy.app" = ] [ t ] } { [ t ] [ f ] } } cond nip ; diff --git a/extra/webapps/file/file.factor b/extra/webapps/file/file.factor index 110b90f84a..552f5e0977 100755 --- a/extra/webapps/file/file.factor +++ b/extra/webapps/file/file.factor @@ -3,7 +3,7 @@ USING: calendar html io io.files kernel math math.parser http.server.responders http.server.templating namespaces parser sequences strings assocs hashtables debugger http.mime sorting -html.elements ; +html.elements logging ; IN: webapps.file @@ -58,6 +58,8 @@ SYMBOL: page [ [ dup page set run-template-file ] with-scope ] try drop ; +\ run-page DEBUG add-input-logging + : include-page ( filename -- ) "doc-root" get swap path+ run-page ; @@ -69,6 +71,8 @@ SYMBOL: page dup mime-type dup "application/x-factor-server-page" = [ drop serve-fhtml ] [ serve-static ] if ; +\ serve-file NOTICE add-input-logging + : file. ( name dirp -- ) [ "/" append ] when dup
write ; @@ -104,15 +108,15 @@ SYMBOL: page ] if ; : serve-object ( filename -- ) - dup directory? [ serve-directory ] [ serve-file ] if ; + serving-path dup exists? [ + dup directory? [ serve-directory ] [ serve-file ] if + ] [ + drop "404 not found" httpd-error + ] if ; : file-responder ( -- ) "doc-root" get [ - "argument" get serving-path dup exists? [ - serve-object - ] [ - drop "404 not found" httpd-error - ] if + "argument" get serve-object ] [ "404 doc-root not set" httpd-error ] if ; diff --git a/extra/webapps/planet/planet.factor b/extra/webapps/planet/planet.factor index b777780e11..a9fd443fe6 100755 --- a/extra/webapps/planet/planet.factor +++ b/extra/webapps/planet/planet.factor @@ -2,7 +2,7 @@ USING: sequences rss arrays concurrency kernel sorting html.elements io assocs namespaces math threads vocabs html furnace http.server.templating calendar math.parser splitting continuations debugger system http.server.responders -xml.writer prettyprint io.logging ; +xml.writer prettyprint logging ; IN: webapps.planet : print-posting-summary ( posting -- ) @@ -75,27 +75,19 @@ SYMBOL: cached-postings SYMBOL: last-update -: fetch-feed ( triple -- feed ) - second - "Fetching " over append log-message - dup download-feed feed-entries - "Done fetching " swap append log-message ; - : ( author entry -- entry' ) clone [ ": " swap entry-title 3append ] keep [ set-entry-title ] keep ; -: ?fetch-feed ( triple -- feed/f ) - [ - fetch-feed - ] [ - swap [ . error. ] to-log-stream f - ] recover ; +: fetch-feed ( url -- feed ) + download-feed feed-entries ; + +\ fetch-feed DEBUG add-error-logging : fetch-blogroll ( blogroll -- entries ) dup 0 - swap [ ?fetch-feed ] parallel-map + swap [ fetch-feed ] parallel-map [ [ ] with map ] 2map concat ; : sort-entries ( entries -- entries' ) From b08907ef2737c942cf3953ba83ac4ccf5eb47621 Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Thu, 7 Feb 2008 17:12:50 -0600 Subject: [PATCH 165/269] extra/multiline checks for EOF now --- extra/multiline/multiline.factor | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/extra/multiline/multiline.factor b/extra/multiline/multiline.factor index 7f831e5351..0ed72f6936 100644 --- a/extra/multiline/multiline.factor +++ b/extra/multiline/multiline.factor @@ -7,8 +7,11 @@ IN: multiline lexer get dup next-line line-text ; : (parse-here) ( -- ) - next-line-text dup ";" = - [ drop lexer get next-line ] [ % "\n" % (parse-here) ] if ; + next-line-text [ + dup ";" = + [ drop lexer get next-line ] + [ % "\n" % (parse-here) ] if + ] [ ";" unexpected-eof ] if* ; : parse-here ( -- str ) [ (parse-here) ] "" make 1 head* @@ -19,11 +22,13 @@ IN: multiline parse-here 1quotation define ; parsing : (parse-multiline-string) ( start-index end-text -- end-index ) - lexer get line-text 2dup start - [ rot dupd >r >r swap subseq % r> r> length + ] [ - rot tail % "\n" % 0 - lexer get next-line swap (parse-multiline-string) - ] if* ; + lexer get line-text [ + 2dup start + [ rot dupd >r >r swap subseq % r> r> length + ] [ + rot tail % "\n" % 0 + lexer get next-line swap (parse-multiline-string) + ] if* + ] [ nip unexpected-eof ] if* ; : parse-multiline-string ( end-text -- str ) [ From 6187a1e5e14978eec4a87e8f2ab094b20a9a8e0b Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 17:55:31 -0600 Subject: [PATCH 166/269] Improved http.client, bootstrap.image.{download,upload} --- core/bootstrap/image/image.factor | 34 ++++++++++++------- extra/benchmark/bootstrap2/bootstrap2.factor | 4 +-- extra/bootstrap/image/download/authors.txt | 1 + .../bootstrap/image/download/download.factor | 25 ++++++++++++++ extra/bootstrap/image/download/summary.txt | 1 + extra/bootstrap/image/upload/authors.txt | 1 + extra/bootstrap/image/upload/summary.txt | 1 + extra/bootstrap/image/upload/upload.factor | 25 ++++++++++++++ extra/crypto/sha1/sha1.factor | 11 +++--- extra/http/client/client.factor | 27 +++++++-------- extra/io/server/server.factor | 3 +- extra/rss/rss.factor | 2 +- extra/tools/deploy/backend/backend.factor | 7 ++-- extra/webapps/fjsc/fjsc.factor | 2 +- extra/yahoo/yahoo.factor | 4 +-- 15 files changed, 102 insertions(+), 46 deletions(-) create mode 100644 extra/bootstrap/image/download/authors.txt create mode 100644 extra/bootstrap/image/download/download.factor create mode 100644 extra/bootstrap/image/download/summary.txt create mode 100644 extra/bootstrap/image/upload/authors.txt create mode 100644 extra/bootstrap/image/upload/summary.txt create mode 100644 extra/bootstrap/image/upload/upload.factor diff --git a/core/bootstrap/image/image.factor b/core/bootstrap/image/image.factor index 3dadee5193..7452e31cf8 100755 --- a/core/bootstrap/image/image.factor +++ b/core/bootstrap/image/image.factor @@ -10,6 +10,23 @@ definitions debugger float-arrays quotations.private combinators.private combinators ; IN: bootstrap.image +: my-arch ( -- arch ) + cpu dup "ppc" = [ os "-" rot 3append ] when ; + +: boot-image-name ( arch -- string ) + "boot." swap ".image" 3append ; + +: my-boot-image-name ( -- string ) + my-arch boot-image-name ; + +: images ( -- seq ) + { + "x86.32" + "x86.64" + "linux-ppc" "macosx-ppc" + ! "arm" + } ; + le write ] curry each ] if ; -: image-name - "boot." architecture get ".image" 3append resource-path ; - : write-image ( image filename -- ) "Writing image to " write dup write "..." print flush [ (write-image) ] with-stream ; @@ -415,16 +429,10 @@ PRIVATE> begin-image "resource:/core/bootstrap/stage1.factor" run-file end-image - image get image-name write-image + image get + architecture get boot-image-name resource-path + write-image ] with-variable ; -: my-arch ( -- arch ) - cpu dup "ppc" = [ os "-" rot 3append ] when ; - : make-images ( -- ) - { - "x86.32" - "x86.64" - "linux-ppc" "macosx-ppc" - ! "arm" - } [ make-image ] each ; + images [ make-image ] each ; diff --git a/extra/benchmark/bootstrap2/bootstrap2.factor b/extra/benchmark/bootstrap2/bootstrap2.factor index bde92a2260..54bc73f4a1 100755 --- a/extra/benchmark/bootstrap2/bootstrap2.factor +++ b/extra/benchmark/bootstrap2/bootstrap2.factor @@ -1,4 +1,4 @@ -USING: io.files io.launcher system tools.deploy.backend +USING: io.files io.launcher system bootstrap.image namespaces sequences kernel ; IN: benchmark.bootstrap2 @@ -6,7 +6,7 @@ IN: benchmark.bootstrap2 "." resource-path cd [ vm , - "-i=" boot-image-name append , + "-i=" my-boot-image-name append , "-output-image=foo.image" , "-no-user-init" , ] { } make run-process drop ; diff --git a/extra/bootstrap/image/download/authors.txt b/extra/bootstrap/image/download/authors.txt new file mode 100644 index 0000000000..1901f27a24 --- /dev/null +++ b/extra/bootstrap/image/download/authors.txt @@ -0,0 +1 @@ +Slava Pestov diff --git a/extra/bootstrap/image/download/download.factor b/extra/bootstrap/image/download/download.factor new file mode 100644 index 0000000000..deed045221 --- /dev/null +++ b/extra/bootstrap/image/download/download.factor @@ -0,0 +1,25 @@ +! 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 ; + +: url "http://factorcode.org/images/latest/" ; + +: download-checksums ( -- alist ) + url "checksums.txt" append http-get + string-lines [ " " split1 ] { } map>assoc ; + +: need-new-image? ( image -- ? ) + dup exists? + [ dup file>md5str swap download-checksums at = not ] + [ drop t ] if ; + +: download-image ( arch -- ) + boot-image-name dup need-new-image? [ + "Downloading " write dup write "..." print + url swap append download + ] [ + "Boot image up to date" print + drop + ] if ; diff --git a/extra/bootstrap/image/download/summary.txt b/extra/bootstrap/image/download/summary.txt new file mode 100644 index 0000000000..fc0ed97ff1 --- /dev/null +++ b/extra/bootstrap/image/download/summary.txt @@ -0,0 +1 @@ +Smart image downloader utility which first checks MD5 checksum diff --git a/extra/bootstrap/image/upload/authors.txt b/extra/bootstrap/image/upload/authors.txt new file mode 100644 index 0000000000..1901f27a24 --- /dev/null +++ b/extra/bootstrap/image/upload/authors.txt @@ -0,0 +1 @@ +Slava Pestov diff --git a/extra/bootstrap/image/upload/summary.txt b/extra/bootstrap/image/upload/summary.txt new file mode 100644 index 0000000000..85497270a2 --- /dev/null +++ b/extra/bootstrap/image/upload/summary.txt @@ -0,0 +1 @@ +Image upload utility diff --git a/extra/bootstrap/image/upload/upload.factor b/extra/bootstrap/image/upload/upload.factor new file mode 100644 index 0000000000..a9f5d1dcd4 --- /dev/null +++ b/extra/bootstrap/image/upload/upload.factor @@ -0,0 +1,25 @@ +! Copyright (C) 2008 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +IN: bootstrap.image.upload +USING: http.client crypto.md5 splitting assocs kernel io.files +bootstrap.image sequences io namespaces io.launcher math ; + +: destination "slava@factorcode.org:www/images/latest/" ; + +: boot-image-names images [ boot-image-name ] map ; + +: compute-checksums ( -- ) + "checksums.txt" [ + boot-image-names [ dup write bl file>md5str print ] each + ] with-file-out ; + +: upload-images ( -- ) + [ + "scp" , boot-image-names % "checksums.txt" , destination , + ] { } make run-process + wait-for-process zero? [ "Upload failed" throw ] unless ; + +: new-images ( -- ) + make-images compute-checksums upload-images ; + +MAIN: new-images diff --git a/extra/crypto/sha1/sha1.factor b/extra/crypto/sha1/sha1.factor index 94a51288bb..f6dfbcd031 100644 --- a/extra/crypto/sha1/sha1.factor +++ b/extra/crypto/sha1/sha1.factor @@ -48,14 +48,13 @@ SYMBOL: K ! f(t;B,C,D) = (B AND C) OR (B AND D) OR (C AND D) (40 <= t <= 59) ! f(t;B,C,D) = B XOR C XOR D (60 <= t <= 79) : sha1-f ( B C D t -- f_tbcd ) - #! Maybe use dispatch 20 /i { - { [ dup 0 = ] [ drop >r over bitnot r> bitand >r bitand r> bitor ] } - { [ dup 1 = ] [ drop bitxor bitxor ] } - { [ dup 2 = ] [ drop 2dup bitand >r pick bitand >r bitand r> r> bitor bitor ] } - { [ dup 3 = ] [ drop bitxor bitxor ] } - } cond ; + { 0 [ >r over bitnot r> bitand >r bitand r> bitor ] } + { 1 [ bitxor bitxor ] } + { 2 [ 2dup bitand >r pick bitand >r bitand r> r> bitor bitor ] } + { 3 [ bitxor bitxor ] } + } case ; : make-w ( str -- ) #! compute w, steps a-b of RFC 3174, section 6.1 diff --git a/extra/http/client/client.factor b/extra/http/client/client.factor index 8e6d8257a4..109bf17c40 100755 --- a/extra/http/client/client.factor +++ b/extra/http/client/client.factor @@ -47,32 +47,31 @@ DEFER: http-get-stream dispose "location" swap peek-at nip http-get-stream ] when ; +: default-timeout 60 1000 * over set-timeout ; + : http-get-stream ( url -- code headers stream ) #! Opens a stream for reading from an HTTP URL. parse-url over parse-host [ [ [ get-request read-response ] with-stream* ] keep + default-timeout ] [ ] [ dispose ] cleanup do-redirect ; -: http-get ( url -- code headers string ) - #! Opens a stream for reading from an HTTP URL. - [ - http-get-stream [ stdio get contents ] with-stream - ] with-scope ; +: success? ( code -- ? ) 200 = ; + +: check-response ( code headers stream -- stream ) + nip swap success? + [ dispose "HTTP download failed" throw ] unless ; + +: http-get ( url -- string ) + http-get-stream check-response contents ; : download-name ( url -- name ) file-name "?" split1 drop "/" ?tail drop ; -: default-timeout 60 1000 * over set-timeout ; - -: success? ( code -- ? ) 200 = ; - : download-to ( url file -- ) #! Downloads the contents of a URL to a file. - >r http-get-stream nip default-timeout swap success? [ - r> stream-copy - ] [ - r> drop dispose "HTTP download failed" throw - ] if ; + >r http-get-stream check-response + r> stream-copy ; : download ( url -- ) dup download-name download-to ; diff --git a/extra/io/server/server.factor b/extra/io/server/server.factor index 829da27f6e..a23984c207 100755 --- a/extra/io/server/server.factor +++ b/extra/io/server/server.factor @@ -19,7 +19,8 @@ LOG: accepted-connection NOTICE : accept-loop ( server quot -- ) [ - >r accept r> [ with-client ] 2curry concurrency:spawn + >r accept r> [ with-client ] 2curry + concurrency:spawn drop ] 2keep accept-loop ; inline : server-loop ( server quot -- ) diff --git a/extra/rss/rss.factor b/extra/rss/rss.factor index be2f648189..0591c60014 100644 --- a/extra/rss/rss.factor +++ b/extra/rss/rss.factor @@ -78,7 +78,7 @@ C: entry : download-feed ( url -- feed ) #! Retrieve an news syndication file, return as a feed tuple. - http-get-stream rot 200 = [ + http-get-stream rot success? [ nip read-feed ] [ 2drop "Error retrieving newsfeed file" throw diff --git a/extra/tools/deploy/backend/backend.factor b/extra/tools/deploy/backend/backend.factor index 95d19712c0..c295f6369d 100755 --- a/extra/tools/deploy/backend/backend.factor +++ b/extra/tools/deploy/backend/backend.factor @@ -24,12 +24,9 @@ IN: tools.deploy.backend dup duplex-stream-out dispose copy-lines ; -: boot-image-name ( -- string ) - "boot." my-arch ".image" 3append ; - : make-boot-image ( -- ) #! If stage1 image doesn't exist, create one. - boot-image-name resource-path exists? + my-boot-image-name resource-path exists? [ my-arch make-image ] unless ; : ?, [ , ] [ drop ] if ; @@ -49,7 +46,7 @@ IN: tools.deploy.backend : staging-command-line ( config -- flags ) [ - "-i=" boot-image-name append , + "-i=" my-boot-image-name append , "-output-image=" over staging-image-name append , diff --git a/extra/webapps/fjsc/fjsc.factor b/extra/webapps/fjsc/fjsc.factor index 19dab4ed1b..55609c72f9 100755 --- a/extra/webapps/fjsc/fjsc.factor +++ b/extra/webapps/fjsc/fjsc.factor @@ -25,7 +25,7 @@ IN: webapps.fjsc : compile-url ( url -- ) #! Compile the factor code at the given url, return the javascript. dup "http:" head? [ "Unable to access remote sites." throw ] when - "http://" "Host" header-param rot 3append http-get 2nip compile "();" write flush ; + "http://" "Host" header-param rot 3append http-get compile "();" write flush ; \ compile-url { { "url" v-required } diff --git a/extra/yahoo/yahoo.factor b/extra/yahoo/yahoo.factor index 2c982306cd..1725c10a44 100644 --- a/extra/yahoo/yahoo.factor +++ b/extra/yahoo/yahoo.factor @@ -26,6 +26,4 @@ C: result ] "" make ; : search-yahoo ( search num -- seq ) - query http-get 2nip - [ "Search failed" throw ] unless* - string>xml parse-yahoo ; + query http-get string>xml parse-yahoo ; From b08409884e72dc4879942a23c56c10167cf5695f Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 18:03:01 -0600 Subject: [PATCH 167/269] Add try-everything for Ed --- core/vocabs/loader/loader-docs.factor | 5 ---- core/vocabs/loader/loader.factor | 34 +++++++++------------------ extra/tools/browser/browser.factor | 14 ++++++++--- extra/tools/test/test-docs.factor | 2 +- extra/tools/test/test.factor | 6 ++--- 5 files changed, 26 insertions(+), 35 deletions(-) diff --git a/core/vocabs/loader/loader-docs.factor b/core/vocabs/loader/loader-docs.factor index f8626f3370..379b300eaa 100755 --- a/core/vocabs/loader/loader-docs.factor +++ b/core/vocabs/loader/loader-docs.factor @@ -124,11 +124,6 @@ HELP: refresh { $values { "prefix" string } } { $description "Reloads source files and documentation belonging to loaded vocabularies whose names are prefixed by " { $snippet "prefix" } " which have been modified on disk." } ; -HELP: require-all-error -{ $values { "vocabs" "a sequence of vocabularies" } } -{ $description "Throws a " { $link require-all-error } "." } -{ $error-description "Thrown by " { $link require-all } " if one or more vocabulary failed to load." } ; - HELP: refresh-all { $description "Reloads source files and documentation for all loaded vocabularies which have been modified on disk." } ; diff --git a/core/vocabs/loader/loader.factor b/core/vocabs/loader/loader.factor index 352ef9fe02..4fcb74df66 100755 --- a/core/vocabs/loader/loader.factor +++ b/core/vocabs/loader/loader.factor @@ -160,37 +160,25 @@ SYMBOL: load-help? drop ; ! third "Traceback" swap write-object ; -TUPLE: require-all-error vocabs ; +: load-failures. ( failures -- ) + [ load-error. nl ] each ; -: require-all-error ( vocabs -- ) - [ vocab-name ] map - \ require-all-error construct-boa throw ; - -M: require-all-error summary - drop "The require-all operation failed" ; - -: require-all ( vocabs -- ) - dup length 1 = [ first require ] [ +: require-all ( vocabs -- failures ) + [ [ [ - [ - [ require ] - [ error-continuation get 3array , ] - recover - ] each - ] { } make - dup empty? [ drop ] [ - dup [ load-error. nl ] each - keys require-all-error - ] if - ] with-compiler-errors - ] if ; + [ require ] + [ error-continuation get 3array , ] + recover + ] each + ] { } make + ] with-compiler-errors ; : do-refresh ( modified-sources modified-docs -- ) 2dup [ f swap set-vocab-docs-loaded? ] each [ f swap set-vocab-source-loaded? ] each - append prune require-all ; + append prune require-all drop ; : refresh ( prefix -- ) to-refresh do-refresh ; diff --git a/extra/tools/browser/browser.factor b/extra/tools/browser/browser.factor index 48de69b025..87b4ba9939 100755 --- a/extra/tools/browser/browser.factor +++ b/extra/tools/browser/browser.factor @@ -132,11 +132,17 @@ MEMO: all-vocabs-seq ( -- seq ) { [ t ] [ f ] } } cond nip ; -: load-everything ( -- ) +: filter-dangerous ( seq -- seq' ) + [ vocab-name dangerous? not ] subset ; + +: try-everything ( -- failures ) all-vocabs-seq - [ vocab-name dangerous? not ] subset + filter-dangerous require-all ; +: load-everything ( -- ) + try-everything drop ; + : unrooted-child-vocabs ( prefix -- seq ) dup empty? [ CHAR: . add ] unless vocabs @@ -155,7 +161,9 @@ MEMO: all-vocabs-seq ( -- seq ) : load-children ( prefix -- ) all-child-vocabs values concat - require-all ; + filter-dangerous + require-all + drop ; : vocab-status-string ( vocab -- string ) { diff --git a/extra/tools/test/test-docs.factor b/extra/tools/test/test-docs.factor index c027073398..b756f9279e 100755 --- a/extra/tools/test/test-docs.factor +++ b/extra/tools/test/test-docs.factor @@ -29,7 +29,7 @@ $nl { $subsection run-tests } { $subsection run-all-tests } "The following word prints failures:" -{ $subsection failures. } ; +{ $subsection test-failures. } ; ARTICLE: "tools.test" "Unit testing" "A unit test is a piece of code which starts with known input values, then compares the output of a word with an expected output, where the expected output is defined by the word's contract." diff --git a/extra/tools/test/test.factor b/extra/tools/test/test.factor index 0b1a495e90..192a248161 100755 --- a/extra/tools/test/test.factor +++ b/extra/tools/test/test.factor @@ -80,7 +80,7 @@ M: expected-error summary dup first print-error "Traceback" swap third write-object ; -: failures. ( assoc -- ) +: test-failures. ( assoc -- ) dup [ nl dup empty? [ @@ -104,10 +104,10 @@ M: expected-error summary ] if ; : test ( prefix -- ) - run-tests failures. ; + run-tests test-failures. ; : run-all-tests ( prefix -- failures ) "" run-tests ; : test-all ( -- ) - run-all-tests failures. ; + run-all-tests test-failures. ; From 6bbbd3f9043a4162ce70e11acf3a3afc88bda7c9 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 18:06:53 -0600 Subject: [PATCH 168/269] Forgot to call load-failures. --- core/vocabs/loader/loader.factor | 2 +- extra/tools/browser/browser.factor | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/vocabs/loader/loader.factor b/core/vocabs/loader/loader.factor index 4fcb74df66..a1276341b3 100755 --- a/core/vocabs/loader/loader.factor +++ b/core/vocabs/loader/loader.factor @@ -178,7 +178,7 @@ SYMBOL: load-help? 2dup [ f swap set-vocab-docs-loaded? ] each [ f swap set-vocab-source-loaded? ] each - append prune require-all drop ; + append prune require-all load-failures. ; : refresh ( prefix -- ) to-refresh do-refresh ; diff --git a/extra/tools/browser/browser.factor b/extra/tools/browser/browser.factor index 87b4ba9939..ae1901ff66 100755 --- a/extra/tools/browser/browser.factor +++ b/extra/tools/browser/browser.factor @@ -163,7 +163,7 @@ MEMO: all-vocabs-seq ( -- seq ) all-child-vocabs values concat filter-dangerous require-all - drop ; + load-failures. ; : vocab-status-string ( vocab -- string ) { From a2e6c372136f35a1d62a8add94293efbd8b52649 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Thu, 7 Feb 2008 18:30:20 -0600 Subject: [PATCH 169/269] simplify builder.test --- extra/builder/builder.factor | 9 +++++-- extra/builder/test/test.factor | 48 ++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/extra/builder/builder.factor b/extra/builder/builder.factor index 5e992ccc81..caa381ba5d 100755 --- a/extra/builder/builder.factor +++ b/extra/builder/builder.factor @@ -1,8 +1,8 @@ -USING: kernel io io.files io.launcher hashtables tools.deploy.backend +USING: kernel io io.files io.launcher hashtables system continuations namespaces sequences splitting math.parser prettyprint tools.time calendar bake vars http.client - combinators ; + combinators bootstrap.image ; IN: builder @@ -82,6 +82,11 @@ VAR: stamp ] if + { + "git" "pull" "--no-summary" + "http://dharmatech.onigirihouse.com/factor.git" "master" + } run-process process-status + "/builds/" stamp> append make-directory "/builds/" stamp> append cd diff --git a/extra/builder/test/test.factor b/extra/builder/test/test.factor index fb9c62e2aa..2a867b1fbc 100644 --- a/extra/builder/test/test.factor +++ b/extra/builder/test/test.factor @@ -7,28 +7,42 @@ USING: kernel sequences assocs builder continuations vocabs vocabs.loader IN: builder.test +! : do-load ( -- ) +! [ +! [ load-everything ] +! [ require-all-error-vocabs "../load-everything-log" log-object ] +! recover +! ] +! "../load-everything-time" log-runtime ; + : do-load ( -- ) - [ - [ load-everything ] - [ require-all-error-vocabs "../load-everything-log" log-object ] - recover - ] - "../load-everything-time" log-runtime ; + [ try-everything ] "../load-everything-time" log-runtime + dup empty? + [ drop ] + [ "../load-everything-log" log-object ] + if ; + +! : do-tests ( -- ) +! "" child-vocabs +! [ vocab-source-loaded? ] subset +! [ vocab-tests-path ] map +! [ dup [ ?resource-path exists? ] when ] subset +! [ dup run-test ] { } map>assoc +! [ second empty? not ] subset +! dup empty? +! [ drop ] +! [ +! "../failing-tests" +! [ [ nl failures. ] assoc-each ] +! with-stream +! ] +! if ; : do-tests ( -- ) - "" child-vocabs - [ vocab-source-loaded? ] subset - [ vocab-tests-path ] map - [ dup [ ?resource-path exists? ] when ] subset - [ dup run-test ] { } map>assoc - [ second empty? not ] subset + run-all-tests keys dup empty? [ drop ] - [ - "../failing-tests" - [ [ nl failures. ] assoc-each ] - with-stream - ] + [ "../failing-tests" log-object ] if ; : do-all ( -- ) do-load do-tests ; From 4dfc151c89c04828d0beabf3a701deeaad48146d Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Thu, 7 Feb 2008 19:48:00 -0500 Subject: [PATCH 170/269] Solution to Project Euler problem 79 --- extra/project-euler/079/079.factor | 65 ++++++++++++++++++++++++ extra/project-euler/079/keylog.txt | 50 ++++++++++++++++++ extra/project-euler/project-euler.factor | 4 +- 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 extra/project-euler/079/079.factor create mode 100644 extra/project-euler/079/keylog.txt diff --git a/extra/project-euler/079/079.factor b/extra/project-euler/079/079.factor new file mode 100644 index 0000000000..d28484c881 --- /dev/null +++ b/extra/project-euler/079/079.factor @@ -0,0 +1,65 @@ +! Copyright (c) 2008 Aaron Schaefer. +! See http://factorcode.org/license.txt for BSD license. +USING: assocs hashtables io.files kernel math math.parser namespaces sequences ; +IN: project-euler.079 + +! http://projecteuler.net/index.php?section=problems&id=79 + +! DESCRIPTION +! ----------- + +! A common security method used for online banking is to ask the user for three +! random characters from a passcode. For example, if the passcode was 531278, +! they may asked for the 2nd, 3rd, and 5th characters; the expected reply would +! be: 317. + +! The text file, keylog.txt, contains fifty successful login attempts. + +! Given that the three characters are always asked for in order, analyse the +! file so as to determine the shortest possible secret passcode of unknown +! length. + + +! SOLUTION +! -------- + +edges ( seq -- seq ) + [ + [ string>digits [ 2 head , ] keep 2 tail* , ] each + ] { } make ; + +: find-source ( seq -- elt ) + dup values swap keys [ prune ] 2apply seq-diff + dup empty? [ "Topological sort failed" throw ] [ first ] if ; + +: remove-source ( seq elt -- seq ) + [ swap member? not ] curry subset ; + +: (topological-sort) ( seq -- ) + dup length 1 > [ + dup find-source dup , remove-source (topological-sort) + ] [ + dup empty? [ drop ] [ first [ , ] each ] if + ] if ; + +PRIVATE> + +: topological-sort ( seq -- seq ) + [ [ (topological-sort) ] { } make ] keep + concat prune dupd seq-diff append ; + +: euler079 ( -- answer ) + source-079 >edges topological-sort 10 swap digits>integer ; + +! [ euler079 ] 100 ave-time +! 2 ms run / 0 ms GC ave time - 100 trials + +! TODO: prune and seq-diff are relatively slow; topological sort could be +! cleaned up and generalized much better, but it works for this problem + +MAIN: euler079 diff --git a/extra/project-euler/079/keylog.txt b/extra/project-euler/079/keylog.txt new file mode 100644 index 0000000000..b6f9903128 --- /dev/null +++ b/extra/project-euler/079/keylog.txt @@ -0,0 +1,50 @@ +319 +680 +180 +690 +129 +620 +762 +689 +762 +318 +368 +710 +720 +710 +629 +168 +160 +689 +716 +731 +736 +729 +316 +729 +729 +710 +769 +290 +719 +680 +318 +389 +162 +289 +162 +718 +729 +319 +790 +680 +890 +362 +319 +760 +316 +729 +380 +319 +728 +716 diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index 36a9069d77..c3db60c481 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -14,8 +14,8 @@ USING: definitions io io.files kernel math math.parser project-euler.ave-time project-euler.037 project-euler.038 project-euler.039 project-euler.040 project-euler.041 project-euler.042 project-euler.043 project-euler.044 project-euler.048 project-euler.052 project-euler.067 project-euler.075 - project-euler.097 project-euler.134 project-euler.169 project-euler.173 - project-euler.175 ; + project-euler.079 project-euler.097 project-euler.134 project-euler.169 + project-euler.173 project-euler.175 ; IN: project-euler Date: Thu, 7 Feb 2008 20:25:03 -0500 Subject: [PATCH 171/269] Fix PE solutions using old math.parser --- extra/project-euler/041/041.factor | 2 +- extra/project-euler/043/043.factor | 6 +++--- extra/project-euler/079/079.factor | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/extra/project-euler/041/041.factor b/extra/project-euler/041/041.factor index 60017f39a1..14084cc01d 100644 --- a/extra/project-euler/041/041.factor +++ b/extra/project-euler/041/041.factor @@ -32,7 +32,7 @@ IN: project-euler.041 : euler041 ( -- answer ) { 7 6 5 4 3 2 1 } all-permutations - [ 10 swap digits>integer ] map [ prime? ] find nip ; + [ 10 digits>integer ] map [ prime? ] find nip ; ! [ euler041 ] 100 ave-time ! 107 ms run / 7 ms GC ave time - 100 trials diff --git a/extra/project-euler/043/043.factor b/extra/project-euler/043/043.factor index abe455e273..54d75c6980 100644 --- a/extra/project-euler/043/043.factor +++ b/extra/project-euler/043/043.factor @@ -36,7 +36,7 @@ IN: project-euler.043 integer swap mod zero? ; + [ 1- dup 3 + ] dip subseq 10 digits>integer swap mod zero? ; : interesting? ( seq -- ? ) { @@ -53,7 +53,7 @@ PRIVATE> : euler043 ( -- answer ) 1234567890 number>digits all-permutations - [ interesting? ] subset [ 10 swap digits>integer ] map sum ; + [ interesting? ] subset [ 10 digits>integer ] map sum ; ! [ euler043 ] time ! 125196 ms run / 19548 ms GC time @@ -89,7 +89,7 @@ PRIVATE> PRIVATE> : euler043a ( -- answer ) - interesting-pandigitals [ 10 swap digits>integer ] sigma ; + interesting-pandigitals [ 10 digits>integer ] sigma ; ! [ euler043a ] 100 ave-time ! 19 ms run / 1 ms GC ave time - 100 trials diff --git a/extra/project-euler/079/079.factor b/extra/project-euler/079/079.factor index d28484c881..f068db77ec 100644 --- a/extra/project-euler/079/079.factor +++ b/extra/project-euler/079/079.factor @@ -54,7 +54,7 @@ PRIVATE> concat prune dupd seq-diff append ; : euler079 ( -- answer ) - source-079 >edges topological-sort 10 swap digits>integer ; + source-079 >edges topological-sort 10 digits>integer ; ! [ euler079 ] 100 ave-time ! 2 ms run / 0 ms GC ave time - 100 trials From 1c3efa89d214ad2b4f9f6b468de2519c6bdbae2c Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Thu, 7 Feb 2008 19:50:26 -0600 Subject: [PATCH 172/269] builder improvements (download-image, simpler do-all) --- extra/builder/builder.factor | 12 ++++++------ extra/builder/test/test.factor | 24 ------------------------ 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/extra/builder/builder.factor b/extra/builder/builder.factor index caa381ba5d..9af79efb29 100755 --- a/extra/builder/builder.factor +++ b/extra/builder/builder.factor @@ -2,7 +2,7 @@ USING: kernel io io.files io.launcher hashtables system continuations namespaces sequences splitting math.parser prettyprint tools.time calendar bake vars http.client - combinators bootstrap.image ; + combinators bootstrap.image bootstrap.image.download ; IN: builder @@ -70,7 +70,6 @@ VAR: stamp "pull" "--no-summary" "git://factorcode.org/git/factor.git" - ! "http://dharmatech.onigirihouse.com/factor.git" "master" } run-process process-status @@ -85,7 +84,7 @@ VAR: stamp { "git" "pull" "--no-summary" "http://dharmatech.onigirihouse.com/factor.git" "master" - } run-process process-status + } run-process drop "/builds/" stamp> append make-directory "/builds/" stamp> append cd @@ -112,14 +111,15 @@ VAR: stamp "builder: vm compile" throw ] if - [ "http://factorcode.org/images/latest/" boot-image-name append download ] + [ my-arch download-image ] + [ ] [ "builder: image download" email-string ] - recover + cleanup `{ { +arguments+ { ,[ factor-binary ] - ,[ "-i=" boot-image-name append ] + ,[ "-i=" my-boot-image-name append ] "-no-user-init" } } { +stdout+ "../boot-log" } diff --git a/extra/builder/test/test.factor b/extra/builder/test/test.factor index 2a867b1fbc..c887c668e6 100644 --- a/extra/builder/test/test.factor +++ b/extra/builder/test/test.factor @@ -7,14 +7,6 @@ USING: kernel sequences assocs builder continuations vocabs vocabs.loader IN: builder.test -! : do-load ( -- ) -! [ -! [ load-everything ] -! [ require-all-error-vocabs "../load-everything-log" log-object ] -! recover -! ] -! "../load-everything-time" log-runtime ; - : do-load ( -- ) [ try-everything ] "../load-everything-time" log-runtime dup empty? @@ -22,22 +14,6 @@ IN: builder.test [ "../load-everything-log" log-object ] if ; -! : do-tests ( -- ) -! "" child-vocabs -! [ vocab-source-loaded? ] subset -! [ vocab-tests-path ] map -! [ dup [ ?resource-path exists? ] when ] subset -! [ dup run-test ] { } map>assoc -! [ second empty? not ] subset -! dup empty? -! [ drop ] -! [ -! "../failing-tests" -! [ [ nl failures. ] assoc-each ] -! with-stream -! ] -! if ; - : do-tests ( -- ) run-all-tests keys dup empty? From 48b96a9e5bf8734e7b2fb484f533e668fc6ae6ba Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 19:51:37 -0600 Subject: [PATCH 173/269] Documentation updates, tags updates --- extra/asn1/tags.txt | 1 + extra/db/authors.txt | 1 + extra/db/summary.txt | 1 + extra/db/tags.txt | 1 + extra/furnace/tags.txt | 1 + extra/logging/analysis/analysis-docs.factor | 31 +++++ extra/logging/analysis/analysis.factor | 3 +- extra/logging/analysis/tags.txt | 1 + extra/logging/insomniac/insomniac-docs.factor | 44 ++++++ extra/logging/insomniac/insomniac.factor | 23 ++-- extra/logging/insomniac/tags.txt | 1 + extra/logging/logging-docs.factor | 130 ++++++++++++++++++ extra/logging/logging.factor | 26 ++-- extra/logging/parser/parser-docs.factor | 21 +++ extra/logging/parser/tags.txt | 1 + extra/logging/server/server-docs.factor | 4 + extra/logging/server/server.factor | 12 +- extra/logging/server/tags.txt | 1 + extra/logging/summary.txt | 2 +- extra/logging/tags.txt | 1 + 20 files changed, 277 insertions(+), 29 deletions(-) create mode 100644 extra/asn1/tags.txt create mode 100644 extra/db/authors.txt create mode 100644 extra/db/summary.txt create mode 100644 extra/db/tags.txt create mode 100644 extra/furnace/tags.txt create mode 100644 extra/logging/analysis/analysis-docs.factor create mode 100644 extra/logging/analysis/tags.txt create mode 100644 extra/logging/insomniac/insomniac-docs.factor create mode 100644 extra/logging/insomniac/tags.txt create mode 100644 extra/logging/logging-docs.factor create mode 100644 extra/logging/parser/parser-docs.factor create mode 100644 extra/logging/parser/tags.txt create mode 100644 extra/logging/server/server-docs.factor create mode 100644 extra/logging/server/tags.txt create mode 100644 extra/logging/tags.txt diff --git a/extra/asn1/tags.txt b/extra/asn1/tags.txt new file mode 100644 index 0000000000..0aef4feca8 --- /dev/null +++ b/extra/asn1/tags.txt @@ -0,0 +1 @@ +enterprise diff --git a/extra/db/authors.txt b/extra/db/authors.txt new file mode 100644 index 0000000000..7c1b2f2279 --- /dev/null +++ b/extra/db/authors.txt @@ -0,0 +1 @@ +Doug Coleman diff --git a/extra/db/summary.txt b/extra/db/summary.txt new file mode 100644 index 0000000000..daebf38da6 --- /dev/null +++ b/extra/db/summary.txt @@ -0,0 +1 @@ +Relational database abstraction layer diff --git a/extra/db/tags.txt b/extra/db/tags.txt new file mode 100644 index 0000000000..0aef4feca8 --- /dev/null +++ b/extra/db/tags.txt @@ -0,0 +1 @@ +enterprise diff --git a/extra/furnace/tags.txt b/extra/furnace/tags.txt new file mode 100644 index 0000000000..0aef4feca8 --- /dev/null +++ b/extra/furnace/tags.txt @@ -0,0 +1 @@ +enterprise diff --git a/extra/logging/analysis/analysis-docs.factor b/extra/logging/analysis/analysis-docs.factor new file mode 100644 index 0000000000..2919f2bcd4 --- /dev/null +++ b/extra/logging/analysis/analysis-docs.factor @@ -0,0 +1,31 @@ +USING: help.markup help.syntax assocs logging math ; +IN: logging.analysis + +HELP: analyze-entries +{ $values { "entries" "a sequence of log entries" } { "word-names" "a sequence of strings" } { "errors" "a sequence of log entries" } { "word-histogram" assoc } { "message-histogram" assoc } } +{ $description "Analyzes log entries:" + { $list + { "Errors (entries with level " { $link ERROR } " or " { $link CRITICAL } ") are collected into the " { $snippet "errors" } " sequence." } + { "All logging words are tallied into " { $snippet "word-histogram" } " - for example, this can tell you about HTTP server hit counts." } + { "All words listed in " { $snippet "word-names" } " have their messages tallied into " { $snippet "message-histogram" } " - for example, this can tell you about popular URLs on an HTTP server." } + } +} ; + +HELP: analysis. +{ $values { "errors" "a sequence of log entries" } { "word-histogram" assoc } { "message-histogram" assoc } } +{ $description "Prints a logging report output by " { $link analyze-entries } ". Formatted output words are used, so the report looks nice in the UI or if sent to an HTML stream." } ; + +HELP: analyze-log +{ $values { "service" "a log service name" } { "n" integer } { "word-names" "a sequence of strings" } } +{ $description "Analyzes a log file and prints a formatted report. The " { $snippet "word-names" } " parameter is documented in " { $link analyze-entries } "." } ; + +ARTICLE: "logging.analysis" "Log analysis" +"The " { $vocab-link "logging.analysis" } " vocabulary builds on the " { $vocab-link "logging.parser" } " vocabulary. It parses log files and produces formatted summary reports. It is used by the " { $vocab-link "logger.insomniac" } " vocabulary to e-mail daily reports." +$nl +"Print log file summary:" +{ $subsection analyze-log } +"Factors:" +{ $subsection analyze-entries } +{ $subsection analysis. } ; + +ABOUT: "logging.analysis" diff --git a/extra/logging/analysis/analysis.factor b/extra/logging/analysis/analysis.factor index df53a8e70b..b530c09b22 100755 --- a/extra/logging/analysis/analysis.factor +++ b/extra/logging/analysis/analysis.factor @@ -11,6 +11,7 @@ SYMBOL: message-histogram : analyze-entry ( entry -- ) dup second ERROR eq? [ dup errors get push ] when + dup second CRITICAL eq? [ dup errors get push ] when 1 over third word-histogram get at+ dup third word-names get member? [ 1 over 1 tail message-histogram get at+ @@ -65,5 +66,5 @@ SYMBOL: message-histogram "==== ERRORS:" print nl errors. ; -: log-analysis ( lines word-names -- ) +: analyze-log ( lines word-names -- ) >r parse-log r> analyze-entries analysis. ; diff --git a/extra/logging/analysis/tags.txt b/extra/logging/analysis/tags.txt new file mode 100644 index 0000000000..0aef4feca8 --- /dev/null +++ b/extra/logging/analysis/tags.txt @@ -0,0 +1 @@ +enterprise diff --git a/extra/logging/insomniac/insomniac-docs.factor b/extra/logging/insomniac/insomniac-docs.factor new file mode 100644 index 0000000000..64ac3b4ff6 --- /dev/null +++ b/extra/logging/insomniac/insomniac-docs.factor @@ -0,0 +1,44 @@ +USING: help.markup help.syntax assocs strings logging +logging.analysis smtp ; +IN: logging.insomniac + +HELP: insomniac-smtp-host +{ $var-description "An SMTP server to use for e-mailing log reports. If not set, the value of " { $link smtp-host } " is used." } ; + +HELP: insomniac-smtp-port +{ $var-description "An SMTP server port to use for e-mailing log reports. If not set, the value of " { $link smtp-port } " is used." } ; + +HELP: insomniac-sender +{ $var-description "The originating e-mail address for mailing log reports. Must be set before " { $vocab-link "logging.insomniac" } " is used." } ; + +HELP: insomniac-recipients +{ $var-description "A sequence of e-mail addresses to mail log reports to. Must be set before " { $vocab-link "logging.insomniac" } " is used." } ; + +HELP: ?analyze-log +{ $values { "service" "a log service name" } { "word-names" "a sequence of strings" } { "string" string } } +{ $description "Analyzes the most recent log and outputs the string analysis, or outputs " { $link f } " if it doesn't exist." } +{ $see-also analyze-log } ; + +HELP: email-log-report +{ $values { "service" "a log service name" } { "word-names" "a sequence of strings" } } +{ $description "E-mails a log report for the given log service. The " { $link insomniac-smtp-host } ", " { $link insomniac-sender } " and " { $link insomniac-recipients } " parameters must be set up first. The " { $snippet "word-names" } " parameter is documented in " { $link analyze-entries } "." } ; + +HELP: schedule-insomniac +{ $values { "alist" "a sequence of pairs of shape " { $snippet "{ service word-names }" } } } +{ $description "Starts a thread which e-mails log reports and rotates logs daily." } ; + +ARTICLE: "logging.insomniac" "Automating log analysis and rotation" +"The " { $vocab-link "logging.insomniac" } " vocabulary builds on the " { $vocab-link "logging.analysis" } " vocabulary. It provides support for e-mailing log reports and rotating logs on a daily basis. E-mails are sent using the " { $vocab-link "smtp" } " vocabulary." +$nl +"Required configuration parameters:" +{ $subsection insomniac-sender } +{ $subsection insomniac-recipients } +"Optional configuration parameters:" +{ $subsection insomniac-smtp-host } +{ $subsection insomniac-smtp-port } +"E-mailing a one-off report:" +{ $subsection email-log-report } +"E-mailing reports and rotating logs on a daily basis:" +{ $subsection schedule-insomniac } ; + +ABOUT: "logging.insomniac" diff --git a/extra/logging/insomniac/insomniac.factor b/extra/logging/insomniac/insomniac.factor index b065dec9d3..d79eca3495 100755 --- a/extra/logging/insomniac/insomniac.factor +++ b/extra/logging/insomniac/insomniac.factor @@ -1,19 +1,17 @@ ! Copyright (C) 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: logging.analysis logging.server logging smtp io.sockets -kernel io.files io.streams.string namespaces raptor.cron ; +kernel io.files io.streams.string namespaces raptor.cron assocs ; IN: logging.insomniac -SYMBOL: insomniac-config - SYMBOL: insomniac-smtp-host SYMBOL: insomniac-smtp-port SYMBOL: insomniac-sender SYMBOL: insomniac-recipients -: ?log-analysis ( service word-names -- string/f ) +: ?analyze-log ( service word-names -- string/f ) >r log-path 1 log# dup exists? [ - file-lines r> [ log-analysis ] string-out + file-lines r> [ analyze-log ] string-out ] [ r> 2drop f ] if ; @@ -31,7 +29,7 @@ SYMBOL: insomniac-recipients : (email-log-report) ( service word-names -- ) [ over >r - ?log-analysis dup [ + ?analyze-log dup [ r> email-subject insomniac-recipients get insomniac-sender get @@ -39,11 +37,12 @@ SYMBOL: insomniac-recipients ] [ r> 2drop ] if ] with-insomniac-smtp ; +\ (email-log-report) NOTICE add-error-logging + : email-log-report ( service word-names -- ) - (email-log-report) ; + "logging.insomniac" [ (email-log-report) ] with-logging ; -\ email-log-report NOTICE add-error-logging - -: schedule-insomniac ( service word-names -- ) - { 25 } { 6 } f f f -rot - [ email-log-report ] 2curry schedule ; +: schedule-insomniac ( alist -- ) + { 25 } { 6 } f f f -rot [ + [ email-log-report ] assoc-each rotate-logs + ] 2curry schedule ; diff --git a/extra/logging/insomniac/tags.txt b/extra/logging/insomniac/tags.txt new file mode 100644 index 0000000000..0aef4feca8 --- /dev/null +++ b/extra/logging/insomniac/tags.txt @@ -0,0 +1 @@ +enterprise diff --git a/extra/logging/logging-docs.factor b/extra/logging/logging-docs.factor new file mode 100644 index 0000000000..3b112e0166 --- /dev/null +++ b/extra/logging/logging-docs.factor @@ -0,0 +1,130 @@ +IN: logging +USING: help.markup help.syntax assocs math calendar +logging.server strings words quotations ; + +HELP: DEBUG +{ $description "Log level for debug messages." } ; + +HELP: NOTICE +{ $description "Log level for ordinary messages." } ; + +HELP: ERROR +{ $description "Log level for error messages." } ; + +HELP: CRITICAL +{ $description "Log level for critical errors which require immediate attention." } ; + +ARTICLE: "logging.levels" "Log levels" +"Several log levels are supported, from lowest to highest:" +{ $subsection DEBUG } +{ $subsection NOTICE } +{ $subsection ERROR } +{ $subsection CRITICAL } ; + +ARTICLE: "logging.files" "Log files" +"Each application that wishes to use logging must choose a log service name; the following combinator should wrap the top level of the application:" +{ $subsection with-logging } +"Log messages are written to " { $snippet "log-root/service/1.log" } ", where" +{ $list + { { $snippet "log-root" } " is the Factor source directory by default, but can be overriden with the " { $link log-root } " variable" } + { { $snippet "service" } " is the service name" } +} +"You can get the log path for a service:" +{ $subsection log-path } +{ $subsection log# } +"New log entries are always sent to " { $snippet "1.log" } " but " { $link "logging.rotation" } " moves " { $snippet "1.log" } " to " { $snippet "2.log" } ", " { $snippet "2.log" } " to " { $snippet "3.log" } ", and so on." ; + +HELP: log-message +{ $values { "msg" string } { "word" word } { "level" "a log level" } } +{ $description "Sends a message to the current log. Does nothing if not executing in a dynamic scope established by " { $link with-logging } "." } ; + +HELP: add-logging +{ $values { "word" word } } +{ $description "Causes the word to log a message every time it is called." } ; + +HELP: add-input-logging +{ $values { "word" word } } +{ $description "Causes the word to log its input values every time it is called. The word must have a stack effect declaration." } ; + +HELP: add-output-logging +{ $values { "word" word } } +{ $description "Causes the word to log its output values every time it is called. The word must have a stack effect declaration." } ; + +HELP: add-error-logging +{ $values { "word" word } } +{ $description "Causes the word to log its input values and any errors it throws." +$nl +"If the word is not executed in a dynamic scope established by " { $link with-logging } ", its behavior is unchanged, and any errors it throws are passed to the caller." +$nl +"If called from a logging context, its input values are logged, and if it throws an error, the error is logged and the word returns normally. Any inputs are popped from the stack and " { $link f } " is pushed in place of each output." } ; + +HELP: log-error +{ $values { "error" "an error" } { "word" word } } +{ $description "Logs an error." } ; + +HELP: log-critical +{ $values { "critical" "an critical" } { "word" word } } +{ $description "Logs a critical error." } ; + +HELP: LOG: +{ $syntax "LOG: name level" } +{ $values { "name" "a new word name" } { "level" "a log level" } } +{ $description "Creates a word with stack effect " { $snippet "( object -- )" } " which logs its input and does nothing else." } ; + +ARTICLE: "logging.messages" "Logging messages" +"Logging messages explicitly:" +{ $subsection log-message } +{ $subsection log-error } +{ $subsection log-critical } +"A utility for defining words which just log and do nothing else:" +{ $subsection POSTPONE: LOG: } +"Annotating words to log; this uses the " { $link "tools.annotations" } " feature:" +{ $subsection add-input-logging } +{ $subsection add-output-logging } +{ $subsection add-error-logging } ; + +HELP: rotate-logs +{ $description "Rotates all logs. The highest numbered log file in each log directory is deleted, and each file is renamed so that its number increments by one. Subsequent logging calls will create a new #1 log file. This keeps log files from getting too large and makes them easier to search." } ; + +HELP: close-logs +{ $description "Closes all open log streams. Subsequent logging will re-open the streams. This should be used before moving or deleting log files." } ; + +HELP: with-logging +{ $values { "service" "a log service name" } { "quot" quotation } } +{ $description "Calls the quotation a new dynamic scope where all logging calls are sent to the log file for " { $snippet "service" } "." } ; + +ARTICLE: "logging.rotation" "Log rotation" +"Log files should be rotated periodically to prevent unbounded growth." +{ $subsection rotate-logs } +{ $subsection close-logs } +"The " { $vocab-link "logging.insomniac" } " vocabulary automates log rotation." ; + +ARTICLE: "logging.server" "Log implementation" +"The " { $vocab-link "logging.server" } " vocabulary implements a concurrent log server using " { $vocab-link "concurrency" } ". User code never interacts with the server directly, instead ot uses the words in the " { $link "logging" } " vocabulary. The server is used to synchronize access to log files and ensure that log rotation can proceed in an orderly fashion." +$nl +"The " { $link log-message } " word sends a message to the server which results in the server executing an internal word:" +{ $subsection (log-message) } +"The " { $link rotate-logs } " word sends a message to the server which results in the server executing an internal word:" +{ $subsection (rotate-logs) } +"The " { $link close-logs } " word sends a message to the server which results in the server executing an internal word:" +{ $subsection (close-logs) } ; + +ARTICLE: "logging" "Logging framework" +"The " { $vocab-link "logging" } " vocabulary implements a comprehensive logging framework suitable for server-side production applications." +{ $subsection "logging.files" } +{ $subsection "logging.levels" } +{ $subsection "logging.messages" } +{ $subsection "logging.rotation" } +{ $subsection "logging.parser" } +{ $subsection "logging.analysis" } +{ $subsection "logging.insomniac" } +{ $subsection "logging.server" } ; + +ABOUT: "logging" + +! A workaround for circular dependency prohibition +USING: threads vocabs.loader ; +[ + yield + "logging.insomniac" require +] in-thread diff --git a/extra/logging/logging.factor b/extra/logging/logging.factor index 71ea247567..d4f0bd1fbf 100755 --- a/extra/logging/logging.factor +++ b/extra/logging/logging.factor @@ -39,8 +39,8 @@ SYMBOL: log-service : rotate-logs ( -- ) { } "rotate-logs" send-to-log-server ; -: close-log-files ( -- ) - { } "close-log-files" send-to-log-server ; +: close-logs ( -- ) + { } "close-logs" send-to-log-server ; : with-logging ( service quot -- ) log-service swap with-variable ; inline @@ -56,7 +56,7 @@ SYMBOL: log-service [ dup first string? ] } && nip ; -: inputs>message ( obj -- inputs>message ) +: stack>message ( obj -- inputs>message ) dup one-string? [ first ] [ H{ { string-limit f } @@ -77,9 +77,9 @@ PRIVATE> : add-logging ( word level -- ) [ call-logging-quot ] (define-logging) ; -: log-inputs ( n word level -- ) +: log-stack ( n word level -- ) log-service get [ - >r >r [ ndup ] keep narray inputs>message + >r >r [ ndup ] keep narray stack>message r> r> log-message ] [ 3drop @@ -88,11 +88,19 @@ PRIVATE> : input# stack-effect effect-in length ; : input-logging-quot ( quot word level -- quot' ) - over input# -rot [ log-inputs ] 3curry swap compose ; + over input# -rot [ log-stack ] 3curry swap compose ; : add-input-logging ( word level -- ) [ input-logging-quot ] (define-logging) ; +: output# stack-effect effect-out length ; + +: output-logging-quot ( quot word level -- quot' ) + over output# -rot [ log-stack ] 3curry compose ; + +: add-output-logging ( word level -- ) + [ output-logging-quot ] (define-logging) ; + : (log-error) ( object word level -- ) log-service get [ >r >r [ print-error ] string-out r> r> log-message @@ -100,9 +108,9 @@ PRIVATE> 2drop rethrow ] if ; -: log-error ( object word -- ) ERROR (log-error) ; +: log-error ( error word -- ) ERROR (log-error) ; -: log-critical ( object word -- ) CRITICAL (log-error) ; +: log-critical ( error word -- ) CRITICAL (log-error) ; : error-logging-quot ( quot word -- quot' ) dup stack-effect effect-in length @@ -118,5 +126,5 @@ PRIVATE> CREATE dup reset-generic dup scan-word - [ >r >r 1array inputs>message r> r> log-message ] 2curry + [ >r >r 1array stack>message r> r> log-message ] 2curry define ; parsing diff --git a/extra/logging/parser/parser-docs.factor b/extra/logging/parser/parser-docs.factor new file mode 100644 index 0000000000..ee995749be --- /dev/null +++ b/extra/logging/parser/parser-docs.factor @@ -0,0 +1,21 @@ +IN: logging.parser +USING: help.markup help.syntax assocs logging math calendar ; + +HELP: parse-log +{ $values { "lines" "a sequence of strings" } { "entries" "a sequence of log entries" } } +{ $description "Parses a sequence of log entries. Malformed entries are printed out and ignore. The result is a sequence of arrays of the shape " { $snippet "{ timestamp level word-name message }" } ", where" + { $list + { { $snippet "timestamp" } " is a " { $link timestamp } } + { { $snippet "level" } " is a log level; see " { $link "logger.levels" } } + { { $snippet "word-name" } " is a string" } + { { $snippet "message" } " is a string" } + } +} ; + +ARTICLE: "logging.parser" "Log file parser" +"The " { $vocab-link "logging.parser" } " vocabulary parses log files output by the " { $vocab-link "logging" } " vocabulary. It is used by " { $link "logging.analysis" } " and " { $link "logging.insomniac" } " to analyze logs." +$nl +"There is only one primary entry point:" +{ $subsection parse-log } ; + +ABOUT: "logging.parser" diff --git a/extra/logging/parser/tags.txt b/extra/logging/parser/tags.txt new file mode 100644 index 0000000000..0aef4feca8 --- /dev/null +++ b/extra/logging/parser/tags.txt @@ -0,0 +1 @@ +enterprise diff --git a/extra/logging/server/server-docs.factor b/extra/logging/server/server-docs.factor new file mode 100644 index 0000000000..08b99dd1cc --- /dev/null +++ b/extra/logging/server/server-docs.factor @@ -0,0 +1,4 @@ +IN: logging.server +USING: help.syntax ; + +ABOUT: "logging.server" diff --git a/extra/logging/server/server.factor b/extra/logging/server/server.factor index cddcea8d70..0300208e7e 100755 --- a/extra/logging/server/server.factor +++ b/extra/logging/server/server.factor @@ -50,11 +50,11 @@ SYMBOL: log-files : try-dispose ( stream -- ) [ dispose ] curry [ error. ] recover ; -: close-log-file ( service -- ) +: close-log ( service -- ) log-files get delete-at* [ try-dispose ] [ drop ] if ; -: (close-log-files) ( -- ) +: (close-logs) ( -- ) log-files get dup values [ try-dispose ] each clear-assoc ; @@ -73,13 +73,13 @@ SYMBOL: log-files [ 1- log# ] 2keep log# ?rename-file ; : rotate-log ( service -- ) - dup close-log-file + dup close-log log-path dup delete-oldest keep-logs 1 [a,b] [ advance-log ] with each ; : (rotate-logs) ( -- ) - (close-log-files) + (close-logs) log-root directory [ drop rotate-log ] assoc-each ; : log-server-loop @@ -87,9 +87,9 @@ SYMBOL: log-files receive unclip { { "log-message" [ (log-message) ] } { "rotate-logs" [ drop (rotate-logs) ] } - { "close-log-files" [ drop (close-log-files) ] } + { "close-logs" [ drop (close-logs) ] } } case - ] [ error. (close-log-files) ] recover + ] [ error. (close-logs) ] recover log-server-loop ; : log-server ( -- ) diff --git a/extra/logging/server/tags.txt b/extra/logging/server/tags.txt new file mode 100644 index 0000000000..0aef4feca8 --- /dev/null +++ b/extra/logging/server/tags.txt @@ -0,0 +1 @@ +enterprise diff --git a/extra/logging/summary.txt b/extra/logging/summary.txt index dbf29c2112..42246bbd3e 100755 --- a/extra/logging/summary.txt +++ b/extra/logging/summary.txt @@ -1 +1 @@ -AOP Logging framework with support for log rotation and machine-readable logs +Logging framework with support for log rotation and machine-readable logs diff --git a/extra/logging/tags.txt b/extra/logging/tags.txt new file mode 100644 index 0000000000..0aef4feca8 --- /dev/null +++ b/extra/logging/tags.txt @@ -0,0 +1 @@ +enterprise From fb67a7621be9e22a85f76a79d8c0ef10d206b06b Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Feb 2008 19:52:40 -0600 Subject: [PATCH 174/269] Cleanup --- extra/logging/server/server.factor | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/extra/logging/server/server.factor b/extra/logging/server/server.factor index cddcea8d70..601237ba81 100755 --- a/extra/logging/server/server.factor +++ b/extra/logging/server/server.factor @@ -25,9 +25,11 @@ SYMBOL: log-files : log-stream ( service -- stream ) log-files get [ open-log-stream ] cache ; +: multiline-header 20 CHAR: - ; foldable + : (write-message) ( msg word-name level multi? -- ) [ - "[" write 20 CHAR: - write "] " write + "[" write multiline-header write "] " write ] [ "[" write now (timestamp>rfc3339) "] " write ] if From 7cdcac3fc97f33d23344985c376bc043ad3b22e2 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 01:08:09 -0600 Subject: [PATCH 175/269] Add another unit test --- core/compiler/test/optimizer.factor | 1 + 1 file changed, 1 insertion(+) diff --git a/core/compiler/test/optimizer.factor b/core/compiler/test/optimizer.factor index 7ee4ebfd1c..987aace00a 100755 --- a/core/compiler/test/optimizer.factor +++ b/core/compiler/test/optimizer.factor @@ -300,3 +300,4 @@ TUPLE: silly-tuple a b ; [ f ] [ \ sequence \ hashcode* should-inline? ] unit-test [ t ] [ \ array \ nth-unsafe should-inline? ] unit-test [ t ] [ \ growable \ nth-unsafe should-inline? ] unit-test +[ t ] [ \ sbuf \ set-nth-unsafe should-inline? ] unit-test From f67ab9a6897ea24982c8049e821740864b6e1f77 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 01:08:23 -0600 Subject: [PATCH 176/269] Multi-methods work in progress --- .../multi-methods/multi-methods-tests.factor | 12 ++ extra/multi-methods/multi-methods.factor | 117 ++++++++++++------ 2 files changed, 88 insertions(+), 41 deletions(-) diff --git a/extra/multi-methods/multi-methods-tests.factor b/extra/multi-methods/multi-methods-tests.factor index a0769dffda..1c68cbe540 100755 --- a/extra/multi-methods/multi-methods-tests.factor +++ b/extra/multi-methods/multi-methods-tests.factor @@ -84,3 +84,15 @@ METHOD: hook-test { hashtable number } assoc-size ; [ fixnum ] [ 3 hook-test ] unit-test 5.0 some-var set [ 0 ] [ H{ } hook-test ] unit-test + +MIXIN: busted + +TUPLE: busted-1 ; +TUPLE: busted-2 ; INSTANCE: busted-2 busted +TUPLE: busted-3 ; + +GENERIC: busted-sort + +METHOD: busted-sort { busted-1 busted-2 } ; +METHOD: busted-sort { busted-2 busted-3 } ; +METHOD: busted-sort { busted busted } ; diff --git a/extra/multi-methods/multi-methods.factor b/extra/multi-methods/multi-methods.factor index 827d64b95f..9a74cc65e8 100755 --- a/extra/multi-methods/multi-methods.factor +++ b/extra/multi-methods/multi-methods.factor @@ -3,12 +3,12 @@ USING: kernel math sequences vectors classes combinators arrays words assocs parser namespaces definitions prettyprint prettyprint.backend quotations arrays.lib -debugger io compiler.units ; +debugger io compiler.units kernel.private effects ; IN: multi-methods -TUPLE: method loc def ; +GENERIC: generic-prologue ( combination -- quot ) -: { set-method-def } \ method construct ; +GENERIC: method-prologue ( combination -- quot ) : maximal-element ( seq quot -- n elt ) dupd [ @@ -25,6 +25,7 @@ TUPLE: method loc def ; [ { { [ 2dup eq? ] [ 0 ] } + { [ 2dup [ class< ] 2keep swap class< and ] [ 0 ] } { [ 2dup class< ] [ -1 ] } { [ 2dup swap class< ] [ 1 ] } { [ t ] [ 0 ] } @@ -54,8 +55,37 @@ TUPLE: method loc def ; : methods ( word -- alist ) "multi-methods" word-prop >alist ; -: method-defs ( methods -- methods' ) - [ method-def ] assoc-map ; +: make-method-def ( quot classes generic -- quot ) + [ + swap [ declare ] curry % + "multi-combination" word-prop method-prologue % + % + ] [ ] make ; + +TUPLE: method word def classes generic loc ; + +PREDICATE: word method-body "multi-method" word-prop >boolean ; + +M: method-body stack-effect + "multi-method" word-prop method-generic stack-effect ; + +: method-word-name ( classes generic -- string ) + [ + word-name % + "-(" % [ "," % ] [ word-name % ] interleave ")" % + ] "" make ; + +: ( quot classes generic -- word ) + #! We xref here because the "multi-method" word-prop isn't + #! set yet so crossref? yields f. + [ make-method-def ] 2keep + method-word-name f + dup rot define + dup xref ; + +: ( quot classes generic -- method ) + [ ] 3keep f \ method construct-boa + dup method-word over "multi-method" set-word-prop ; TUPLE: no-method arguments generic ; @@ -68,8 +98,11 @@ TUPLE: no-method arguments generic ; ] if ; : multi-dispatch-quot ( methods generic -- quot ) - >r - [ [ >r multi-predicate r> ] assoc-map ] keep argument-count + >r [ + [ + >r multi-predicate r> method-word 1quotation + ] assoc-map + ] keep argument-count r> [ no-method ] 2curry swap reverse alist>quot ; @@ -98,36 +131,36 @@ M: no-method error. methods congruify-methods sorted-methods keys [ niceify-method ] map stack. ; -GENERIC: perform-combination ( word combination -- quot ) - TUPLE: standard-combination ; -: standard-combination ( methods generic -- quot ) - >r congruify-methods sorted-methods r> multi-dispatch-quot ; +M: standard-combination method-prologue drop [ ] ; -M: standard-combination perform-combination - drop [ methods method-defs ] keep standard-combination ; +M: standard-combination generic-prologue drop [ ] ; + +: make-generic ( generic -- quot ) + dup "multi-combination" word-prop generic-prologue swap + [ methods congruify-methods sorted-methods ] keep + multi-dispatch-quot append ; TUPLE: hook-combination var ; -M: hook-combination perform-combination - hook-combination-var [ get ] curry swap methods - [ method-defs [ [ drop ] swap append ] assoc-map ] keep - standard-combination append ; +M: hook-combination method-prologue + drop [ drop ] ; -: make-generic ( word -- ) - dup dup "multi-combination" word-prop perform-combination - define ; +M: hook-combination generic-prologue + hook-combination-var [ get ] curry ; -: init-methods ( word -- ) - dup "multi-methods" word-prop - H{ } assoc-like - "multi-methods" set-word-prop ; +: update-generic ( word -- ) + dup make-generic define ; : define-generic ( word combination -- ) - dupd "multi-combination" set-word-prop - dup init-methods - make-generic ; + over "multi-combination" word-prop over = [ + 2drop + ] [ + dupd "multi-combination" set-word-prop + dup H{ } clone "multi-methods" set-word-prop + update-generic + ] if ; : define-standard-generic ( word -- ) T{ standard-combination } define-generic ; @@ -146,29 +179,31 @@ M: hook-combination perform-combination : with-methods ( word quot -- ) over >r >r "multi-methods" word-prop - r> call r> make-generic ; inline + r> call r> update-generic ; inline -: add-method ( method classes word -- ) +: define-method ( quot classes generic -- ) + >r [ bootstrap-word ] map r> + [ ] 2keep [ set-at ] with-methods ; -: forget-method ( classes word -- ) +: forget-method ( classes generic -- ) [ delete-at ] with-methods ; -: parse-method ( -- method classes word method-spec ) - parse-definition 2 cut - over >r - >r first2 swap r> -rot - r> first2 swap add* >array ; +: method>spec ( method -- spec ) + dup method-classes swap method-generic add* ; + +: parse-method ( -- quot classes generic ) + parse-definition dup 2 tail over second rot first ; : METHOD: location - >r parse-method >r add-method r> r> + >r parse-method [ define-method ] 2keep add* r> remember-definition ; parsing ! For compatibility : M: - scan-word 1array scan-word parse-definition - -rot add-method ; parsing + scan-word 1array scan-word parse-definition + -rot define-method ; parsing ! Definition protocol. We qualify core generics here USE: qualified @@ -202,7 +237,7 @@ PREDICATE: array method-spec unclip generic? >r [ class? ] all? r> and ; syntax:M: method-spec where - dup unclip method method-loc [ ] [ second where ] ?if ; + dup unclip method [ method-loc ] [ second where ] ?if ; syntax:M: method-spec set-where unclip method set-method-loc ; @@ -211,11 +246,11 @@ syntax:M: method-spec definer drop \ METHOD: \ ; ; syntax:M: method-spec definition - unclip method method-def ; + unclip method dup [ method-def ] when ; syntax:M: method-spec synopsis* dup definer. unclip pprint* pprint* ; syntax:M: method-spec forget* - unclip [ delete-at ] with-methods ; + unclip forget-method ; From 492e569b627ed7826b6fd9b4a946fa7c15e379d2 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 01:08:52 -0600 Subject: [PATCH 177/269] 'about' now requires first --- extra/help/help.factor | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/extra/help/help.factor b/extra/help/help.factor index aefbf2aba2..77b9f699aa 100755 --- a/extra/help/help.factor +++ b/extra/help/help.factor @@ -1,10 +1,10 @@ -! Copyright (C) 2005, 2007 Slava Pestov. +! Copyright (C) 2005, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: arrays io kernel namespaces parser prettyprint sequences -words assocs definitions generic quotations effects -slots continuations tuples debugger combinators -vocabs help.stylesheet help.topics help.crossref help.markup -sorting classes ; +words assocs definitions generic quotations effects slots +continuations tuples debugger combinators vocabs help.stylesheet +help.topics help.crossref help.markup sorting classes +vocabs.loader ; IN: help GENERIC: word-help* ( word -- content ) @@ -96,6 +96,7 @@ M: word set-article-parent swap "help-parent" set-word-prop ; article-content print-content nl ; : about ( vocab -- ) + dup require dup vocab [ ] [ "No such vocabulary: " swap append throw ] ?if From 52b5c5a0682644327c22d7e10f8fe16d006e67a6 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 01:48:51 -0600 Subject: [PATCH 178/269] Reorganize compiler tests --- core/alien/c-types/c-types.factor | 2 + core/alien/compiler/compiler-tests.factor | 356 ++++++++++++++++++ core/compiler/compiler-tests.factor | 28 +- .../curry-tests.factor} | 0 core/compiler/test/curry/curry.factor | 0 .../float-tests.factor} | 0 core/compiler/test/float/float.factor | 0 core/compiler/test/generic.factor | 30 -- core/compiler/test/ifte.factor | 131 ------- .../intrinsics-tests.factor} | 0 .../test/intrinsics/intrinsics.factor | 0 .../redefine-tests.factor} | 0 core/compiler/test/redefine/redefine.factor | 0 core/compiler/test/simple.factor | 71 ---- core/compiler/test/simple/simple-tests.factor | 227 +++++++++++ core/compiler/test/simple/simple.factor | 0 .../stack-trace-tests.factor} | 0 .../test/stack-trace/stack-trace.factor | 0 .../templates-early-tests.factor} | 0 .../templates-early/templates-early.factor | 0 .../templates-tests.factor} | 0 core/compiler/test/templates/templates.factor | 0 .../tuples-tests.factor} | 0 core/compiler/test/tuples/tuples.factor | 0 core/inference/class/class-tests.factor | 10 + core/inference/known-words/known-words.factor | 17 + core/inference/transforms/transforms.factor | 4 +- core/math/bitfields/bitfields-tests.factor | 11 +- core/optimizer/optimizer-tests.factor | 303 +++++++++++++++ core/parser/parser.factor | 2 +- 30 files changed, 936 insertions(+), 256 deletions(-) create mode 100755 core/alien/compiler/compiler-tests.factor mode change 100644 => 100755 core/compiler/compiler-tests.factor rename core/compiler/test/{curry.factor => curry/curry-tests.factor} (100%) create mode 100644 core/compiler/test/curry/curry.factor rename core/compiler/test/{float.factor => float/float-tests.factor} (100%) create mode 100644 core/compiler/test/float/float.factor delete mode 100644 core/compiler/test/generic.factor delete mode 100755 core/compiler/test/ifte.factor rename core/compiler/test/{intrinsics.factor => intrinsics/intrinsics-tests.factor} (100%) create mode 100644 core/compiler/test/intrinsics/intrinsics.factor rename core/compiler/test/{redefine.factor => redefine/redefine-tests.factor} (100%) create mode 100644 core/compiler/test/redefine/redefine.factor delete mode 100755 core/compiler/test/simple.factor create mode 100755 core/compiler/test/simple/simple-tests.factor create mode 100644 core/compiler/test/simple/simple.factor rename core/compiler/test/{stack-trace.factor => stack-trace/stack-trace-tests.factor} (100%) create mode 100644 core/compiler/test/stack-trace/stack-trace.factor rename core/compiler/test/{templates-early.factor => templates-early/templates-early-tests.factor} (100%) create mode 100644 core/compiler/test/templates-early/templates-early.factor rename core/compiler/test/{templates.factor => templates/templates-tests.factor} (100%) create mode 100644 core/compiler/test/templates/templates.factor rename core/compiler/test/{tuples.factor => tuples/tuples-tests.factor} (100%) create mode 100644 core/compiler/test/tuples/tuples.factor mode change 100644 => 100755 core/math/bitfields/bitfields-tests.factor create mode 100755 core/optimizer/optimizer-tests.factor diff --git a/core/alien/c-types/c-types.factor b/core/alien/c-types/c-types.factor index 6c46cb946a..ed0721a7ff 100755 --- a/core/alien/c-types/c-types.factor +++ b/core/alien/c-types/c-types.factor @@ -7,6 +7,8 @@ math.parser cpu.architecture alien alien.accessors quotations system compiler.units ; IN: alien.c-types +: little-endian? ( -- ? ) 1 *char 1 = ; foldable + TUPLE: c-type boxer prep unboxer getter setter diff --git a/core/alien/compiler/compiler-tests.factor b/core/alien/compiler/compiler-tests.factor new file mode 100755 index 0000000000..c0c3733afa --- /dev/null +++ b/core/alien/compiler/compiler-tests.factor @@ -0,0 +1,356 @@ +IN: temporary +USING: alien alien.c-types alien.syntax compiler kernel +namespaces namespaces tools.test sequences inference words +arrays parser quotations continuations inference.backend effects +namespaces.private io io.streams.string memory system threads +tools.test ; + +FUNCTION: void ffi_test_0 ; +[ ] [ ffi_test_0 ] unit-test + +FUNCTION: int ffi_test_1 ; +[ 3 ] [ ffi_test_1 ] unit-test + +FUNCTION: int ffi_test_2 int x int y ; +[ 5 ] [ 2 3 ffi_test_2 ] unit-test +[ "hi" 3 ffi_test_2 ] must-fail + +FUNCTION: int ffi_test_3 int x int y int z int t ; +[ 25 ] [ 2 3 4 5 ffi_test_3 ] unit-test + +FUNCTION: float ffi_test_4 ; +[ 1.5 ] [ ffi_test_4 ] unit-test + +FUNCTION: double ffi_test_5 ; +[ 1.5 ] [ ffi_test_5 ] unit-test + +FUNCTION: int ffi_test_9 int a int b int c int d int e int f int g ; +[ 28 ] [ 1 2 3 4 5 6 7 ffi_test_9 ] unit-test +[ "a" 2 3 4 5 6 7 ffi_test_9 ] must-fail +[ 1 2 3 4 5 6 "a" ffi_test_9 ] must-fail + +C-STRUCT: foo + { "int" "x" } + { "int" "y" } +; + +: make-foo ( x y -- foo ) + "foo" [ set-foo-y ] keep [ set-foo-x ] keep ; + +FUNCTION: int ffi_test_11 int a foo b int c ; + +[ 14 ] [ 1 2 3 make-foo 4 ffi_test_11 ] unit-test + +FUNCTION: int ffi_test_13 int a int b int c int d int e int f int g int h int i int j int k ; + +[ 66 ] [ 1 2 3 4 5 6 7 8 9 10 11 ffi_test_13 ] unit-test + +FUNCTION: foo ffi_test_14 int x int y ; + +[ 11 6 ] [ 11 6 ffi_test_14 dup foo-x swap foo-y ] unit-test + +FUNCTION: char* ffi_test_15 char* x char* y ; + +[ "foo" ] [ "xy" "zt" ffi_test_15 ] unit-test +[ "bar" ] [ "xy" "xy" ffi_test_15 ] unit-test +[ 1 2 ffi_test_15 ] must-fail + +C-STRUCT: bar + { "long" "x" } + { "long" "y" } + { "long" "z" } +; + +FUNCTION: bar ffi_test_16 long x long y long z ; + +[ 11 6 -7 ] [ + 11 6 -7 ffi_test_16 dup bar-x over bar-y rot bar-z +] unit-test + +C-STRUCT: tiny + { "int" "x" } +; + +FUNCTION: tiny ffi_test_17 int x ; + +[ 11 ] [ 11 ffi_test_17 tiny-x ] unit-test + +[ [ alien-indirect ] infer ] [ inference-error? ] must-fail-with + +: indirect-test-1 + "int" { } "cdecl" alien-indirect ; + +{ 1 1 } [ indirect-test-1 ] must-infer-as + +[ 3 ] [ "ffi_test_1" f dlsym indirect-test-1 ] unit-test + +[ -1 indirect-test-1 ] must-fail + +: indirect-test-2 + "int" { "int" "int" } "cdecl" alien-indirect data-gc ; + +{ 3 1 } [ indirect-test-2 ] must-infer-as + +[ 5 ] +[ 2 3 "ffi_test_2" f dlsym indirect-test-2 ] +unit-test + +: indirect-test-3 + "int" { "int" "int" "int" "int" } "stdcall" alien-indirect + data-gc ; + +<< "f-stdcall" f "stdcall" add-library >> + +[ f ] [ "f-stdcall" load-library ] unit-test +[ "stdcall" ] [ "f-stdcall" library library-abi ] unit-test + +: ffi_test_18 ( w x y z -- int ) + "int" "f-stdcall" "ffi_test_18" { "int" "int" "int" "int" } + alien-invoke data-gc ; + +[ 25 ] [ 2 3 4 5 ffi_test_18 ] unit-test + +: ffi_test_19 ( x y z -- bar ) + "bar" "f-stdcall" "ffi_test_19" { "long" "long" "long" } + alien-invoke data-gc ; + +[ 11 6 -7 ] [ + 11 6 -7 ffi_test_19 dup bar-x over bar-y rot bar-z +] unit-test + +FUNCTION: double ffi_test_6 float x float y ; +[ 6.0 ] [ 3.0 2.0 ffi_test_6 ] unit-test +[ "a" "b" ffi_test_6 ] must-fail + +FUNCTION: double ffi_test_7 double x double y ; +[ 6.0 ] [ 3.0 2.0 ffi_test_7 ] unit-test + +FUNCTION: double ffi_test_8 double x float y double z float t int w ; +[ 19.0 ] [ 3.0 2.0 1.0 6.0 7 ffi_test_8 ] unit-test + +FUNCTION: int ffi_test_10 int a int b double c int d float e int f int g int h ; +[ -34 ] [ 1 2 3.0 4 5.0 6 7 8 ffi_test_10 ] unit-test + +FUNCTION: void ffi_test_20 double x1, double x2, double x3, + double y1, double y2, double y3, + double z1, double z2, double z3 ; + +[ ] [ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ffi_test_20 ] unit-test + +! Make sure XT doesn't get clobbered in stack frame + +: ffi_test_31 + "void" + f "ffi_test_31" + { "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" } + alien-invoke code-gc 3 ; + +[ 3 ] [ 42 [ ] each ffi_test_31 ] unit-test + +FUNCTION: longlong ffi_test_21 long x long y ; + +[ 121932631112635269 ] +[ 123456789 987654321 ffi_test_21 ] unit-test + +FUNCTION: long ffi_test_22 long x longlong y longlong z ; + +[ 987655432 ] +[ 1111 121932631112635269 123456789 ffi_test_22 ] unit-test + +[ 1111 f 123456789 ffi_test_22 ] must-fail + +C-STRUCT: rect + { "float" "x" } + { "float" "y" } + { "float" "w" } + { "float" "h" } +; + +: + "rect" + [ set-rect-h ] keep + [ set-rect-w ] keep + [ set-rect-y ] keep + [ set-rect-x ] keep ; + +FUNCTION: int ffi_test_12 int a int b rect c int d int e int f ; + +[ 45 ] [ 1 2 3.0 4.0 5.0 6.0 7 8 9 ffi_test_12 ] unit-test + +[ 1 2 { 1 2 3 } 7 8 9 ffi_test_12 ] must-fail + +FUNCTION: float ffi_test_23 ( float[3] x, float[3] y ) ; + +[ 32.0 ] [ { 1.0 2.0 3.0 } >c-float-array { 4.0 5.0 6.0 } >c-float-array ffi_test_23 ] unit-test + +! Test odd-size structs +C-STRUCT: test-struct-1 { { "char" 1 } "x" } ; + +FUNCTION: test-struct-1 ffi_test_24 ; + +[ B{ 1 } ] [ ffi_test_24 ] unit-test + +C-STRUCT: test-struct-2 { { "char" 2 } "x" } ; + +FUNCTION: test-struct-2 ffi_test_25 ; + +[ B{ 1 2 } ] [ ffi_test_25 ] unit-test + +C-STRUCT: test-struct-3 { { "char" 3 } "x" } ; + +FUNCTION: test-struct-3 ffi_test_26 ; + +[ B{ 1 2 3 } ] [ ffi_test_26 ] unit-test + +C-STRUCT: test-struct-4 { { "char" 4 } "x" } ; + +FUNCTION: test-struct-4 ffi_test_27 ; + +[ B{ 1 2 3 4 } ] [ ffi_test_27 ] unit-test + +C-STRUCT: test-struct-5 { { "char" 5 } "x" } ; + +FUNCTION: test-struct-5 ffi_test_28 ; + +[ B{ 1 2 3 4 5 } ] [ ffi_test_28 ] unit-test + +C-STRUCT: test-struct-6 { { "char" 6 } "x" } ; + +FUNCTION: test-struct-6 ffi_test_29 ; + +[ B{ 1 2 3 4 5 6 } ] [ ffi_test_29 ] unit-test + +C-STRUCT: test-struct-7 { { "char" 7 } "x" } ; + +FUNCTION: test-struct-7 ffi_test_30 ; + +[ B{ 1 2 3 4 5 6 7 } ] [ ffi_test_30 ] unit-test + +C-STRUCT: test-struct-8 { "double" "x" } { "double" "y" } ; + +FUNCTION: double ffi_test_32 test-struct-8 x int y ; + +[ 9.0 ] [ + "test-struct-8" + 1.0 over set-test-struct-8-x + 2.0 over set-test-struct-8-y + 3 ffi_test_32 +] unit-test + +C-STRUCT: test-struct-9 { "float" "x" } { "float" "y" } ; + +FUNCTION: double ffi_test_33 test-struct-9 x int y ; + +[ 9.0 ] [ + "test-struct-9" + 1.0 over set-test-struct-9-x + 2.0 over set-test-struct-9-y + 3 ffi_test_33 +] unit-test + +C-STRUCT: test-struct-10 { "float" "x" } { "int" "y" } ; + +FUNCTION: double ffi_test_34 test-struct-10 x int y ; + +[ 9.0 ] [ + "test-struct-10" + 1.0 over set-test-struct-10-x + 2 over set-test-struct-10-y + 3 ffi_test_34 +] unit-test + +C-STRUCT: test-struct-11 { "int" "x" } { "int" "y" } ; + +FUNCTION: double ffi_test_35 test-struct-11 x int y ; + +[ 9.0 ] [ + "test-struct-11" + 1 over set-test-struct-11-x + 2 over set-test-struct-11-y + 3 ffi_test_35 +] unit-test + +C-STRUCT: test-struct-12 { "int" "a" } { "double" "x" } ; + +: make-struct-12 + "test-struct-12" + [ set-test-struct-12-x ] keep ; + +FUNCTION: double ffi_test_36 ( test-struct-12 x ) ; + +[ 1.23456 ] [ 1.23456 make-struct-12 ffi_test_36 ] unit-test + +! Test callbacks + +: callback-1 "void" { } "cdecl" [ ] alien-callback ; + +[ 0 1 ] [ [ callback-1 ] infer dup effect-in swap effect-out ] unit-test + +[ t ] [ callback-1 alien? ] unit-test + +: callback_test_1 "void" { } "cdecl" alien-indirect ; + +[ ] [ callback-1 callback_test_1 ] unit-test + +: callback-2 "void" { } "cdecl" [ [ 5 throw ] ignore-errors ] alien-callback ; + +[ ] [ callback-2 callback_test_1 ] unit-test + +: callback-3 "void" { } "cdecl" [ 5 "x" set ] alien-callback ; + +[ t ] [ + namestack* + 3 "x" set callback-3 callback_test_1 + namestack* eq? +] unit-test + +[ 5 ] [ + [ + 3 "x" set callback-3 callback_test_1 "x" get + ] with-scope +] unit-test + +: callback-4 + "void" { } "cdecl" [ "Hello world" write ] alien-callback + data-gc ; + +[ "Hello world" ] [ + [ callback-4 callback_test_1 ] string-out +] unit-test + +: callback-5 + "void" { } "cdecl" [ data-gc ] alien-callback ; + +[ "testing" ] [ + "testing" callback-5 callback_test_1 +] unit-test + +: callback-5a + "void" { } "cdecl" [ 8000000 f drop ] alien-callback ; + +! Hack; if we're on ARM, we probably don't have much RAM, so +! skip this test. +cpu "arm" = [ + [ "testing" ] [ + "testing" callback-5a callback_test_1 + ] unit-test +] unless + +: callback-6 + "void" { } "cdecl" [ [ continue ] callcc0 ] alien-callback ; + +[ 1 2 3 ] [ callback-6 callback_test_1 1 2 3 ] unit-test + +: callback-7 + "void" { } "cdecl" [ 1000 sleep ] alien-callback ; + +[ 1 2 3 ] [ callback-7 callback_test_1 1 2 3 ] unit-test + +[ f ] [ namespace global eq? ] unit-test + +: callback-8 + "void" { } "cdecl" [ + [ continue ] callcc0 + ] alien-callback ; + +[ ] [ callback-8 callback_test_1 ] unit-test diff --git a/core/compiler/compiler-tests.factor b/core/compiler/compiler-tests.factor old mode 100644 new mode 100755 index bd9b26ce6d..7e4e79437d --- a/core/compiler/compiler-tests.factor +++ b/core/compiler/compiler-tests.factor @@ -1,21 +1,7 @@ -USING: io.files tools.test sequences namespaces kernel -compiler.units ; - -{ - "templates-early" - "simple" - "intrinsics" - "float" - "generic" - "ifte" - "templates" - "optimizer" - "redefine" - "stack-trace" - "alien" - "curry" - "tuples" -} -[ "resource:core/compiler/test/" swap ".factor" 3append ] map -[ run-test ] map -[ failures get push-all ] each +IN: temporary +USING: tools.browser tools.test kernel sequences vocabs ; + +"compiler.test" child-vocabs empty? [ + "compiler.test" load-children + "compiler.test" test +] when diff --git a/core/compiler/test/curry.factor b/core/compiler/test/curry/curry-tests.factor similarity index 100% rename from core/compiler/test/curry.factor rename to core/compiler/test/curry/curry-tests.factor diff --git a/core/compiler/test/curry/curry.factor b/core/compiler/test/curry/curry.factor new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core/compiler/test/float.factor b/core/compiler/test/float/float-tests.factor similarity index 100% rename from core/compiler/test/float.factor rename to core/compiler/test/float/float-tests.factor diff --git a/core/compiler/test/float/float.factor b/core/compiler/test/float/float.factor new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core/compiler/test/generic.factor b/core/compiler/test/generic.factor deleted file mode 100644 index c54dbd753d..0000000000 --- a/core/compiler/test/generic.factor +++ /dev/null @@ -1,30 +0,0 @@ -IN: temporary -USING: compiler generic tools.test math kernel words arrays -sequences quotations ; - -GENERIC: single-combination-test - -M: object single-combination-test drop ; -M: f single-combination-test nip ; -M: array single-combination-test drop ; -M: integer single-combination-test drop ; - -[ 2 3 ] [ 2 3 t single-combination-test ] unit-test -[ 2 3 ] [ 2 3 4 single-combination-test ] unit-test -[ 2 f ] [ 2 3 f single-combination-test ] unit-test - -DEFER: single-combination-test-2 - -: single-combination-test-4 - dup [ single-combination-test-2 ] when ; - -: single-combination-test-3 - drop 3 ; - -GENERIC: single-combination-test-2 -M: object single-combination-test-2 single-combination-test-3 ; -M: f single-combination-test-2 single-combination-test-4 ; - -[ 3 ] [ t single-combination-test-2 ] unit-test -[ 3 ] [ 3 single-combination-test-2 ] unit-test -[ f ] [ f single-combination-test-2 ] unit-test diff --git a/core/compiler/test/ifte.factor b/core/compiler/test/ifte.factor deleted file mode 100755 index 802cad5032..0000000000 --- a/core/compiler/test/ifte.factor +++ /dev/null @@ -1,131 +0,0 @@ -IN: temporary -USING: alien strings compiler tools.test math kernel words -math.private combinators ; - -: dummy-if-1 t [ ] [ ] if ; - -[ ] [ dummy-if-1 ] unit-test - -: dummy-if-2 f [ ] [ ] if ; - -[ ] [ dummy-if-2 ] unit-test - -: dummy-if-3 t [ 1 ] [ 2 ] if ; - -[ 1 ] [ dummy-if-3 ] unit-test - -: dummy-if-4 f [ 1 ] [ 2 ] if ; - -[ 2 ] [ dummy-if-4 ] unit-test - -: dummy-if-5 0 dup 1 fixnum<= [ drop 1 ] [ ] if ; - -[ 1 ] [ dummy-if-5 ] unit-test - -: dummy-if-6 - dup 1 fixnum<= [ - drop 1 - ] [ - 1 fixnum- dup 1 fixnum- fixnum+ - ] if ; - -[ 17 ] [ 10 dummy-if-6 ] unit-test - -: dead-code-rec - t [ - 3.2 - ] [ - dead-code-rec - ] if ; - -[ 3.2 ] [ dead-code-rec ] unit-test - -: one-rec [ f one-rec ] [ "hi" ] if ; - -[ "hi" ] [ t one-rec ] unit-test - -: after-if-test - t [ ] [ ] if 5 ; - -[ 5 ] [ after-if-test ] unit-test - -DEFER: countdown-b - -: countdown-a ( n -- ) dup 0 eq? [ drop ] [ 1 fixnum- countdown-b ] if ; -: countdown-b ( n -- ) dup 0 eq? [ drop ] [ 1 fixnum- countdown-a ] if ; - -[ ] [ 10 countdown-b ] unit-test - -: dummy-when-1 t [ ] when ; - -[ ] [ dummy-when-1 ] unit-test - -: dummy-when-2 f [ ] when ; - -[ ] [ dummy-when-2 ] unit-test - -: dummy-when-3 dup [ dup fixnum* ] when ; - -[ 16 ] [ 4 dummy-when-3 ] unit-test -[ f ] [ f dummy-when-3 ] unit-test - -: dummy-when-4 dup [ dup dup fixnum* fixnum* ] when swap ; - -[ 64 f ] [ f 4 dummy-when-4 ] unit-test -[ f t ] [ t f dummy-when-4 ] unit-test - -: dummy-when-5 f [ dup fixnum* ] when ; - -[ f ] [ f dummy-when-5 ] unit-test - -: dummy-unless-1 t [ ] unless ; - -[ ] [ dummy-unless-1 ] unit-test - -: dummy-unless-2 f [ ] unless ; - -[ ] [ dummy-unless-2 ] unit-test - -: dummy-unless-3 dup [ drop 3 ] unless ; - -[ 3 ] [ f dummy-unless-3 ] unit-test -[ 4 ] [ 4 dummy-unless-3 ] unit-test - -! Test cond expansion -[ "even" ] [ - [ - 2 { - { [ dup 2 mod 0 = ] [ drop "even" ] } - { [ dup 2 mod 1 = ] [ drop "odd" ] } - } cond - ] compile-call -] unit-test - -[ "odd" ] [ - [ - 3 { - { [ dup 2 mod 0 = ] [ drop "even" ] } - { [ dup 2 mod 1 = ] [ drop "odd" ] } - } cond - ] compile-call -] unit-test - -[ "neither" ] [ - [ - 3 { - { [ dup string? ] [ drop "string" ] } - { [ dup float? ] [ drop "float" ] } - { [ dup alien? ] [ drop "alien" ] } - { [ t ] [ drop "neither" ] } - } cond - ] compile-call -] unit-test - -[ 3 ] [ - [ - 3 { - { [ dup fixnum? ] [ ] } - { [ t ] [ drop t ] } - } cond - ] compile-call -] unit-test diff --git a/core/compiler/test/intrinsics.factor b/core/compiler/test/intrinsics/intrinsics-tests.factor similarity index 100% rename from core/compiler/test/intrinsics.factor rename to core/compiler/test/intrinsics/intrinsics-tests.factor diff --git a/core/compiler/test/intrinsics/intrinsics.factor b/core/compiler/test/intrinsics/intrinsics.factor new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core/compiler/test/redefine.factor b/core/compiler/test/redefine/redefine-tests.factor similarity index 100% rename from core/compiler/test/redefine.factor rename to core/compiler/test/redefine/redefine-tests.factor diff --git a/core/compiler/test/redefine/redefine.factor b/core/compiler/test/redefine/redefine.factor new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core/compiler/test/simple.factor b/core/compiler/test/simple.factor deleted file mode 100755 index 6f5cb33c1a..0000000000 --- a/core/compiler/test/simple.factor +++ /dev/null @@ -1,71 +0,0 @@ -USING: compiler tools.test kernel kernel.private -combinators.private ; -IN: temporary - -! Test empty word -[ ] [ [ ] compile-call ] unit-test - -! Test literals -[ 1 ] [ [ 1 ] compile-call ] unit-test -[ 31 ] [ [ 31 ] compile-call ] unit-test -[ 255 ] [ [ 255 ] compile-call ] unit-test -[ -1 ] [ [ -1 ] compile-call ] unit-test -[ 65536 ] [ [ 65536 ] compile-call ] unit-test -[ -65536 ] [ [ -65536 ] compile-call ] unit-test -[ "hey" ] [ [ "hey" ] compile-call ] unit-test - -! Calls -: no-op ; - -[ ] [ [ no-op ] compile-call ] unit-test -[ 3 ] [ [ no-op 3 ] compile-call ] unit-test -[ 3 ] [ [ 3 no-op ] compile-call ] unit-test - -: bar 4 ; - -[ 4 ] [ [ bar no-op ] compile-call ] unit-test -[ 4 3 ] [ [ no-op bar 3 ] compile-call ] unit-test -[ 3 4 ] [ [ 3 no-op bar ] compile-call ] unit-test - -[ ] [ no-op ] unit-test - -! Conditionals - -[ 1 ] [ t [ [ 1 ] [ 2 ] if ] compile-call ] unit-test -[ 2 ] [ f [ [ 1 ] [ 2 ] if ] compile-call ] unit-test -[ 1 3 ] [ t [ [ 1 ] [ 2 ] if 3 ] compile-call ] unit-test -[ 2 3 ] [ f [ [ 1 ] [ 2 ] if 3 ] compile-call ] unit-test - -[ "hi" ] [ 0 [ { [ "hi" ] [ "bye" ] } dispatch ] compile-call ] unit-test -[ "bye" ] [ 1 [ { [ "hi" ] [ "bye" ] } dispatch ] compile-call ] unit-test - -[ "hi" 3 ] [ 0 [ { [ "hi" ] [ "bye" ] } dispatch 3 ] compile-call ] unit-test -[ "bye" 3 ] [ 1 [ { [ "hi" ] [ "bye" ] } dispatch 3 ] compile-call ] unit-test - -[ 4 1 ] [ 0 [ { [ bar 1 ] [ 3 1 ] } dispatch ] compile-call ] unit-test -[ 3 1 ] [ 1 [ { [ bar 1 ] [ 3 1 ] } dispatch ] compile-call ] unit-test -[ 4 1 3 ] [ 0 [ { [ bar 1 ] [ 3 1 ] } dispatch 3 ] compile-call ] unit-test -[ 3 1 3 ] [ 1 [ { [ bar 1 ] [ 3 1 ] } dispatch 3 ] compile-call ] unit-test - -! Labels - -: recursive ( ? -- ) [ f recursive ] when ; inline - -[ ] [ t [ recursive ] compile-call ] unit-test - -[ ] [ t recursive ] unit-test - -! Make sure error reporting works - -[ [ dup ] compile-call ] must-fail -[ [ drop ] compile-call ] must-fail - -! Regression - -[ ] [ [ callstack ] compile-call drop ] unit-test - -! Regression - -: empty ; - -[ "b" ] [ 1 [ empty { [ "a" ] [ "b" ] } dispatch ] compile-call ] unit-test diff --git a/core/compiler/test/simple/simple-tests.factor b/core/compiler/test/simple/simple-tests.factor new file mode 100755 index 0000000000..3f4f6451a3 --- /dev/null +++ b/core/compiler/test/simple/simple-tests.factor @@ -0,0 +1,227 @@ +USING: compiler tools.test kernel kernel.private +combinators.private math.private math combinators strings +alien arrays ; +IN: temporary + +! Test empty word +[ ] [ [ ] compile-call ] unit-test + +! Test literals +[ 1 ] [ [ 1 ] compile-call ] unit-test +[ 31 ] [ [ 31 ] compile-call ] unit-test +[ 255 ] [ [ 255 ] compile-call ] unit-test +[ -1 ] [ [ -1 ] compile-call ] unit-test +[ 65536 ] [ [ 65536 ] compile-call ] unit-test +[ -65536 ] [ [ -65536 ] compile-call ] unit-test +[ "hey" ] [ [ "hey" ] compile-call ] unit-test + +! Calls +: no-op ; + +[ ] [ [ no-op ] compile-call ] unit-test +[ 3 ] [ [ no-op 3 ] compile-call ] unit-test +[ 3 ] [ [ 3 no-op ] compile-call ] unit-test + +: bar 4 ; + +[ 4 ] [ [ bar no-op ] compile-call ] unit-test +[ 4 3 ] [ [ no-op bar 3 ] compile-call ] unit-test +[ 3 4 ] [ [ 3 no-op bar ] compile-call ] unit-test + +[ ] [ no-op ] unit-test + +! Conditionals + +[ 1 ] [ t [ [ 1 ] [ 2 ] if ] compile-call ] unit-test +[ 2 ] [ f [ [ 1 ] [ 2 ] if ] compile-call ] unit-test +[ 1 3 ] [ t [ [ 1 ] [ 2 ] if 3 ] compile-call ] unit-test +[ 2 3 ] [ f [ [ 1 ] [ 2 ] if 3 ] compile-call ] unit-test + +[ "hi" ] [ 0 [ { [ "hi" ] [ "bye" ] } dispatch ] compile-call ] unit-test +[ "bye" ] [ 1 [ { [ "hi" ] [ "bye" ] } dispatch ] compile-call ] unit-test + +[ "hi" 3 ] [ 0 [ { [ "hi" ] [ "bye" ] } dispatch 3 ] compile-call ] unit-test +[ "bye" 3 ] [ 1 [ { [ "hi" ] [ "bye" ] } dispatch 3 ] compile-call ] unit-test + +[ 4 1 ] [ 0 [ { [ bar 1 ] [ 3 1 ] } dispatch ] compile-call ] unit-test +[ 3 1 ] [ 1 [ { [ bar 1 ] [ 3 1 ] } dispatch ] compile-call ] unit-test +[ 4 1 3 ] [ 0 [ { [ bar 1 ] [ 3 1 ] } dispatch 3 ] compile-call ] unit-test +[ 3 1 3 ] [ 1 [ { [ bar 1 ] [ 3 1 ] } dispatch 3 ] compile-call ] unit-test + +! Labels + +: recursive ( ? -- ) [ f recursive ] when ; inline + +[ ] [ t [ recursive ] compile-call ] unit-test + +[ ] [ t recursive ] unit-test + +! Make sure error reporting works + +[ [ dup ] compile-call ] must-fail +[ [ drop ] compile-call ] must-fail + +! Regression + +[ ] [ [ callstack ] compile-call drop ] unit-test + +! Regression + +: empty ; + +[ "b" ] [ 1 [ empty { [ "a" ] [ "b" ] } dispatch ] compile-call ] unit-test + +: dummy-if-1 t [ ] [ ] if ; + +[ ] [ dummy-if-1 ] unit-test + +: dummy-if-2 f [ ] [ ] if ; + +[ ] [ dummy-if-2 ] unit-test + +: dummy-if-3 t [ 1 ] [ 2 ] if ; + +[ 1 ] [ dummy-if-3 ] unit-test + +: dummy-if-4 f [ 1 ] [ 2 ] if ; + +[ 2 ] [ dummy-if-4 ] unit-test + +: dummy-if-5 0 dup 1 fixnum<= [ drop 1 ] [ ] if ; + +[ 1 ] [ dummy-if-5 ] unit-test + +: dummy-if-6 + dup 1 fixnum<= [ + drop 1 + ] [ + 1 fixnum- dup 1 fixnum- fixnum+ + ] if ; + +[ 17 ] [ 10 dummy-if-6 ] unit-test + +: dead-code-rec + t [ + 3.2 + ] [ + dead-code-rec + ] if ; + +[ 3.2 ] [ dead-code-rec ] unit-test + +: one-rec [ f one-rec ] [ "hi" ] if ; + +[ "hi" ] [ t one-rec ] unit-test + +: after-if-test + t [ ] [ ] if 5 ; + +[ 5 ] [ after-if-test ] unit-test + +DEFER: countdown-b + +: countdown-a ( n -- ) dup 0 eq? [ drop ] [ 1 fixnum- countdown-b ] if ; +: countdown-b ( n -- ) dup 0 eq? [ drop ] [ 1 fixnum- countdown-a ] if ; + +[ ] [ 10 countdown-b ] unit-test + +: dummy-when-1 t [ ] when ; + +[ ] [ dummy-when-1 ] unit-test + +: dummy-when-2 f [ ] when ; + +[ ] [ dummy-when-2 ] unit-test + +: dummy-when-3 dup [ dup fixnum* ] when ; + +[ 16 ] [ 4 dummy-when-3 ] unit-test +[ f ] [ f dummy-when-3 ] unit-test + +: dummy-when-4 dup [ dup dup fixnum* fixnum* ] when swap ; + +[ 64 f ] [ f 4 dummy-when-4 ] unit-test +[ f t ] [ t f dummy-when-4 ] unit-test + +: dummy-when-5 f [ dup fixnum* ] when ; + +[ f ] [ f dummy-when-5 ] unit-test + +: dummy-unless-1 t [ ] unless ; + +[ ] [ dummy-unless-1 ] unit-test + +: dummy-unless-2 f [ ] unless ; + +[ ] [ dummy-unless-2 ] unit-test + +: dummy-unless-3 dup [ drop 3 ] unless ; + +[ 3 ] [ f dummy-unless-3 ] unit-test +[ 4 ] [ 4 dummy-unless-3 ] unit-test + +! Test cond expansion +[ "even" ] [ + [ + 2 { + { [ dup 2 mod 0 = ] [ drop "even" ] } + { [ dup 2 mod 1 = ] [ drop "odd" ] } + } cond + ] compile-call +] unit-test + +[ "odd" ] [ + [ + 3 { + { [ dup 2 mod 0 = ] [ drop "even" ] } + { [ dup 2 mod 1 = ] [ drop "odd" ] } + } cond + ] compile-call +] unit-test + +[ "neither" ] [ + [ + 3 { + { [ dup string? ] [ drop "string" ] } + { [ dup float? ] [ drop "float" ] } + { [ dup alien? ] [ drop "alien" ] } + { [ t ] [ drop "neither" ] } + } cond + ] compile-call +] unit-test + +[ 3 ] [ + [ + 3 { + { [ dup fixnum? ] [ ] } + { [ t ] [ drop t ] } + } cond + ] compile-call +] unit-test + +GENERIC: single-combination-test + +M: object single-combination-test drop ; +M: f single-combination-test nip ; +M: array single-combination-test drop ; +M: integer single-combination-test drop ; + +[ 2 3 ] [ 2 3 t single-combination-test ] unit-test +[ 2 3 ] [ 2 3 4 single-combination-test ] unit-test +[ 2 f ] [ 2 3 f single-combination-test ] unit-test + +DEFER: single-combination-test-2 + +: single-combination-test-4 + dup [ single-combination-test-2 ] when ; + +: single-combination-test-3 + drop 3 ; + +GENERIC: single-combination-test-2 +M: object single-combination-test-2 single-combination-test-3 ; +M: f single-combination-test-2 single-combination-test-4 ; + +[ 3 ] [ t single-combination-test-2 ] unit-test +[ 3 ] [ 3 single-combination-test-2 ] unit-test +[ f ] [ f single-combination-test-2 ] unit-test diff --git a/core/compiler/test/simple/simple.factor b/core/compiler/test/simple/simple.factor new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core/compiler/test/stack-trace.factor b/core/compiler/test/stack-trace/stack-trace-tests.factor similarity index 100% rename from core/compiler/test/stack-trace.factor rename to core/compiler/test/stack-trace/stack-trace-tests.factor diff --git a/core/compiler/test/stack-trace/stack-trace.factor b/core/compiler/test/stack-trace/stack-trace.factor new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core/compiler/test/templates-early.factor b/core/compiler/test/templates-early/templates-early-tests.factor similarity index 100% rename from core/compiler/test/templates-early.factor rename to core/compiler/test/templates-early/templates-early-tests.factor diff --git a/core/compiler/test/templates-early/templates-early.factor b/core/compiler/test/templates-early/templates-early.factor new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core/compiler/test/templates.factor b/core/compiler/test/templates/templates-tests.factor similarity index 100% rename from core/compiler/test/templates.factor rename to core/compiler/test/templates/templates-tests.factor diff --git a/core/compiler/test/templates/templates.factor b/core/compiler/test/templates/templates.factor new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core/compiler/test/tuples.factor b/core/compiler/test/tuples/tuples-tests.factor similarity index 100% rename from core/compiler/test/tuples.factor rename to core/compiler/test/tuples/tuples-tests.factor diff --git a/core/compiler/test/tuples/tuples.factor b/core/compiler/test/tuples/tuples.factor new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core/inference/class/class-tests.factor b/core/inference/class/class-tests.factor index 3bd90a3aca..17cc3d3cf8 100755 --- a/core/inference/class/class-tests.factor +++ b/core/inference/class/class-tests.factor @@ -263,3 +263,13 @@ cell-bits 32 = [ \ fixnum-shift inlined? ] unit-test ] when + +[ t ] [ + [ B{ 1 0 } *short 0 number= ] + \ number= inlined? +] unit-test + +[ t ] [ + [ B{ 1 0 } *short 0 = ] + \ number= inlined? +] unit-test diff --git a/core/inference/known-words/known-words.factor b/core/inference/known-words/known-words.factor index 6be3899acd..69e331a9bf 100755 --- a/core/inference/known-words/known-words.factor +++ b/core/inference/known-words/known-words.factor @@ -414,64 +414,81 @@ t over set-effect-terminated? \ make-flushable \ alien-signed-cell { c-ptr integer } { integer } "inferred-effect" set-word-prop +\ alien-signed-cell make-flushable \ set-alien-signed-cell { integer c-ptr integer } { } "inferred-effect" set-word-prop \ alien-unsigned-cell { c-ptr integer } { integer } "inferred-effect" set-word-prop +\ alien-unsigned-cell make-flushable \ set-alien-unsigned-cell { integer c-ptr integer } { } "inferred-effect" set-word-prop \ alien-signed-8 { c-ptr integer } { integer } "inferred-effect" set-word-prop +\ alien-signed-8 make-flushable \ set-alien-signed-8 { integer c-ptr integer } { } "inferred-effect" set-word-prop \ alien-unsigned-8 { c-ptr integer } { integer } "inferred-effect" set-word-prop +\ alien-unsigned-8 make-flushable \ set-alien-unsigned-8 { integer c-ptr integer } { } "inferred-effect" set-word-prop \ alien-signed-4 { c-ptr integer } { integer } "inferred-effect" set-word-prop +\ alien-signed-4 make-flushable \ set-alien-signed-4 { integer c-ptr integer } { } "inferred-effect" set-word-prop \ alien-unsigned-4 { c-ptr integer } { integer } "inferred-effect" set-word-prop +\ alien-unsigned-4 make-flushable \ set-alien-unsigned-4 { integer c-ptr integer } { } "inferred-effect" set-word-prop \ alien-signed-2 { c-ptr integer } { fixnum } "inferred-effect" set-word-prop +\ alien-signed-2 make-flushable \ set-alien-signed-2 { integer c-ptr integer } { } "inferred-effect" set-word-prop \ alien-unsigned-2 { c-ptr integer } { fixnum } "inferred-effect" set-word-prop +\ alien-unsigned-2 make-flushable \ set-alien-unsigned-2 { integer c-ptr integer } { } "inferred-effect" set-word-prop \ alien-signed-1 { c-ptr integer } { fixnum } "inferred-effect" set-word-prop +\ alien-signed-1 make-flushable \ set-alien-signed-1 { integer c-ptr integer } { } "inferred-effect" set-word-prop \ alien-unsigned-1 { c-ptr integer } { fixnum } "inferred-effect" set-word-prop +\ alien-unsigned-1 make-flushable \ set-alien-unsigned-1 { integer c-ptr integer } { } "inferred-effect" set-word-prop \ alien-float { c-ptr integer } { float } "inferred-effect" set-word-prop +\ alien-float make-flushable \ set-alien-float { float c-ptr integer } { } "inferred-effect" set-word-prop \ alien-double { c-ptr integer } { float } "inferred-effect" set-word-prop +\ alien-double make-flushable \ set-alien-double { float c-ptr integer } { } "inferred-effect" set-word-prop \ alien-cell { c-ptr integer } { simple-c-ptr } "inferred-effect" set-word-prop +\ alien-cell make-flushable \ set-alien-cell { c-ptr c-ptr integer } { } "inferred-effect" set-word-prop \ alien>char-string { c-ptr } { string } "inferred-effect" set-word-prop +\ alien>char-string make-flushable \ string>char-alien { string } { byte-array } "inferred-effect" set-word-prop +\ string>char-alien make-flushable \ alien>u16-string { c-ptr } { string } "inferred-effect" set-word-prop +\ alien>u16-string make-flushable \ string>u16-alien { string } { byte-array } "inferred-effect" set-word-prop +\ string>u16-alien make-flushable \ alien-address { alien } { integer } "inferred-effect" set-word-prop \ alien-address make-flushable diff --git a/core/inference/transforms/transforms.factor b/core/inference/transforms/transforms.factor index ad2bacc789..b1b56ca1a1 100755 --- a/core/inference/transforms/transforms.factor +++ b/core/inference/transforms/transforms.factor @@ -54,7 +54,9 @@ M: pair (bitfield-quot) ( spec -- quot ) \ bitfield [ bitfield-quot ] 1 define-transform -\ flags [ flags [ ] curry ] 1 define-transform +\ flags [ + [ 0 , [ , \ bitor , ] each ] [ ] make +] 1 define-transform ! Tuple operations : [get-slots] ( slots -- quot ) diff --git a/core/math/bitfields/bitfields-tests.factor b/core/math/bitfields/bitfields-tests.factor old mode 100644 new mode 100755 index c382d3352d..a10c0566f8 --- a/core/math/bitfields/bitfields-tests.factor +++ b/core/math/bitfields/bitfields-tests.factor @@ -1,4 +1,4 @@ -USING: math math.bitfields tools.test kernel ; +USING: math math.bitfields tools.test kernel words ; IN: temporary [ 0 ] [ { } bitfield ] unit-test @@ -6,3 +6,12 @@ IN: temporary [ 268 ] [ 3 1 { 8 2 } bitfield ] unit-test [ 268 ] [ 1 { 8 { 3 2 } } bitfield ] unit-test [ 512 ] [ 1 { { 1+ 8 } } bitfield ] unit-test + +: a 1 ; inline +: b 2 ; inline + +: foo { a b } flags ; + +[ 3 ] [ foo ] unit-test +[ 3 ] [ { a b } flags ] unit-test +[ t ] [ \ foo compiled? ] unit-test diff --git a/core/optimizer/optimizer-tests.factor b/core/optimizer/optimizer-tests.factor new file mode 100755 index 0000000000..232eb5a83a --- /dev/null +++ b/core/optimizer/optimizer-tests.factor @@ -0,0 +1,303 @@ +USING: arrays compiler generic hashtables inference kernel +kernel.private math optimizer prettyprint sequences sbufs +strings tools.test vectors words sequences.private quotations +optimizer.backend classes inference.dataflow tuples.private +continuations growable ; +IN: temporary + +[ H{ { 1 5 } { 3 4 } { 2 5 } } ] [ + H{ { 1 2 } { 3 4 } } H{ { 2 5 } } union* +] unit-test + +[ H{ { 1 4 } { 2 4 } { 3 4 } } ] [ + H{ { 1 2 } { 3 4 } } H{ { 2 3 } } union* +] unit-test + +! Test method inlining +[ f ] [ fixnum { } min-class ] unit-test + +[ string ] [ + \ string + [ integer string array reversed sbuf + slice vector quotation ] + sort-classes min-class +] unit-test + +[ fixnum ] [ + \ fixnum + [ fixnum integer object ] + sort-classes min-class +] unit-test + +[ integer ] [ + \ fixnum + [ integer float object ] + sort-classes min-class +] unit-test + +[ object ] [ + \ word + [ integer float object ] + sort-classes min-class +] unit-test + +[ reversed ] [ + \ reversed + [ integer reversed slice ] + sort-classes min-class +] unit-test + +GENERIC: xyz ( obj -- obj ) +M: array xyz xyz ; + +[ t ] [ \ xyz compiled? ] unit-test + +! Test predicate inlining +: pred-test-1 + dup fixnum? [ + dup integer? [ "integer" ] [ "nope" ] if + ] [ + "not a fixnum" + ] if ; + +[ 1 "integer" ] [ 1 pred-test-1 ] unit-test + +TUPLE: pred-test ; + +: pred-test-2 + dup tuple? [ + dup pred-test? [ "pred-test" ] [ "nope" ] if + ] [ + "not a tuple" + ] if ; + +[ T{ pred-test } "pred-test" ] [ T{ pred-test } pred-test-2 ] unit-test + +: pred-test-3 + dup pred-test? [ + dup tuple? [ "pred-test" ] [ "nope" ] if + ] [ + "not a tuple" + ] if ; + +[ T{ pred-test } "pred-test" ] [ T{ pred-test } pred-test-3 ] unit-test + +: inline-test + "nom" = ; + +[ t ] [ "nom" inline-test ] unit-test +[ f ] [ "shayin" inline-test ] unit-test +[ f ] [ 3 inline-test ] unit-test + +: fixnum-declarations >fixnum 24 shift 1234 bitxor ; + +[ ] [ 1000000 fixnum-declarations . ] unit-test + +! regression + +: literal-not-branch 0 not [ ] [ ] if ; + +[ ] [ literal-not-branch ] unit-test + +! regression + +: bad-kill-1 ( a b -- c d e ) [ 3 f ] [ dup bad-kill-1 ] if ; inline +: bad-kill-2 bad-kill-1 drop ; + +[ 3 ] [ t bad-kill-2 ] unit-test + +! regression +: (the-test) ( x -- y ) dup 0 > [ 1- (the-test) ] when ; inline +: the-test ( -- x y ) 2 dup (the-test) ; + +[ 2 0 ] [ the-test ] unit-test + +! regression +: (double-recursion) ( start end -- ) + < [ + 6 1 (double-recursion) + 3 2 (double-recursion) + ] when ; inline + +: double-recursion 0 2 (double-recursion) ; + +[ ] [ double-recursion ] unit-test + +! regression +: double-label-1 ( a b c -- d ) + [ f double-label-1 ] [ swap nth-unsafe ] if ; inline + +: double-label-2 ( a -- b ) + dup array? [ ] [ ] if 0 t double-label-1 ; + +[ 0 ] [ 10 double-label-2 ] unit-test + +! regression +GENERIC: void-generic ( obj -- * ) +: breakage "hi" void-generic ; +[ t ] [ \ breakage compiled? ] unit-test +[ breakage ] must-fail + +! regression +: test-0 ( n -- ) dup 0 = [ drop ] [ 1- test-0 ] if ; inline +: test-1 ( n -- ) t [ test-0 ] [ delegate dup [ test-1 ] [ drop ] if ] if ; inline +: test-2 ( -- ) 5 test-1 ; + +[ f ] [ f test-2 ] unit-test + +: branch-fold-regression-0 ( m -- n ) + t [ ] [ 1+ branch-fold-regression-0 ] if ; inline + +: branch-fold-regression-1 ( -- m ) + 10 branch-fold-regression-0 ; + +[ 10 ] [ branch-fold-regression-1 ] unit-test + +! another regression +: constant-branch-fold-0 "hey" ; foldable +: constant-branch-fold-1 constant-branch-fold-0 "hey" = ; inline +[ 1 ] [ [ constant-branch-fold-1 [ 1 ] [ 2 ] if ] compile-call ] unit-test + +! another regression +: foo f ; +: bar foo 4 4 = and ; +[ f ] [ bar ] unit-test + +! ensure identities are working in some form +[ t ] [ + [ { number } declare 0 + ] dataflow optimize + [ #push? ] node-exists? not +] unit-test + +! compiling with a non-literal class failed +: -regression ; + +[ t ] [ \ -regression compiled? ] unit-test + +GENERIC: foozul ( a -- b ) +M: reversed foozul ; +M: integer foozul ; +M: slice foozul ; + +[ reversed ] [ reversed \ foozul specific-method ] unit-test + +! regression +: constant-fold-2 f ; foldable +: constant-fold-3 4 ; foldable + +[ f t ] [ + [ constant-fold-2 constant-fold-3 4 = ] compile-call +] unit-test + +: constant-fold-4 f ; foldable +: constant-fold-5 f ; foldable + +[ f ] [ + [ constant-fold-4 constant-fold-5 or ] compile-call +] unit-test + +[ 5 ] [ 5 [ 0 + ] compile-call ] unit-test +[ 5 ] [ 5 [ 0 swap + ] compile-call ] unit-test + +[ 5 ] [ 5 [ 0 - ] compile-call ] unit-test +[ -5 ] [ 5 [ 0 swap - ] compile-call ] unit-test +[ 0 ] [ 5 [ dup - ] compile-call ] unit-test + +[ 5 ] [ 5 [ 1 * ] compile-call ] unit-test +[ 5 ] [ 5 [ 1 swap * ] compile-call ] unit-test +[ 0 ] [ 5 [ 0 * ] compile-call ] unit-test +[ 0 ] [ 5 [ 0 swap * ] compile-call ] unit-test +[ -5 ] [ 5 [ -1 * ] compile-call ] unit-test +[ -5 ] [ 5 [ -1 swap * ] compile-call ] unit-test + +[ 0 ] [ 5 [ 1 mod ] compile-call ] unit-test +[ 0 ] [ 5 [ 1 rem ] compile-call ] unit-test + +[ 5 ] [ 5 [ -1 bitand ] compile-call ] unit-test +[ 0 ] [ 5 [ 0 bitand ] compile-call ] unit-test +[ 5 ] [ 5 [ -1 swap bitand ] compile-call ] unit-test +[ 0 ] [ 5 [ 0 swap bitand ] compile-call ] unit-test +[ 5 ] [ 5 [ dup bitand ] compile-call ] unit-test + +[ 5 ] [ 5 [ 0 bitor ] compile-call ] unit-test +[ -1 ] [ 5 [ -1 bitor ] compile-call ] unit-test +[ 5 ] [ 5 [ 0 swap bitor ] compile-call ] unit-test +[ -1 ] [ 5 [ -1 swap bitor ] compile-call ] unit-test +[ 5 ] [ 5 [ dup bitor ] compile-call ] unit-test + +[ 5 ] [ 5 [ 0 bitxor ] compile-call ] unit-test +[ 5 ] [ 5 [ 0 swap bitxor ] compile-call ] unit-test +[ -6 ] [ 5 [ -1 bitxor ] compile-call ] unit-test +[ -6 ] [ 5 [ -1 swap bitxor ] compile-call ] unit-test +[ 0 ] [ 5 [ dup bitxor ] compile-call ] unit-test + +[ 0 ] [ 5 [ 0 swap shift ] compile-call ] unit-test +[ 5 ] [ 5 [ 0 shift ] compile-call ] unit-test + +[ f ] [ 5 [ dup < ] compile-call ] unit-test +[ t ] [ 5 [ dup <= ] compile-call ] unit-test +[ f ] [ 5 [ dup > ] compile-call ] unit-test +[ t ] [ 5 [ dup >= ] compile-call ] unit-test + +[ t ] [ 5 [ dup eq? ] compile-call ] unit-test +[ t ] [ 5 [ dup = ] compile-call ] unit-test +[ t ] [ 5 [ dup number= ] compile-call ] unit-test +[ t ] [ \ vector [ \ vector = ] compile-call ] unit-test + +GENERIC: detect-number ( obj -- obj ) +M: number detect-number ; + +[ 10 f [ 0 + detect-number ] compile-call ] must-fail + +! Regression +[ 4 [ + ] ] [ 2 2 [ [ + ] [ call ] keep ] compile-call ] unit-test + +! Regression +USE: sorting +USE: sorting.private + +: old-binsearch ( elt quot seq -- elt quot i ) + dup length 1 <= [ + slice-from + ] [ + [ midpoint swap call ] 3keep roll dup zero? + [ drop dup slice-from swap midpoint@ + ] + [ partition old-binsearch ] if + ] if ; inline + +[ 10 ] [ + 10 20 >vector + [ [ - ] swap old-binsearch ] compile-call 2nip +] unit-test + +! Regression +TUPLE: silly-tuple a b ; + +[ 1 2 { silly-tuple-a silly-tuple-b } ] [ + T{ silly-tuple f 1 2 } + [ + { silly-tuple-a silly-tuple-b } [ get-slots ] keep + ] compile-call +] unit-test + +! Regression +: empty-compound ; + +: node-successor-f-bug ( x -- * ) + [ 3 throw ] [ empty-compound ] compose [ 3 throw ] if ; + +[ t ] [ \ node-successor-f-bug compiled? ] unit-test + +: construct-empty-bug construct-empty ; + +[ ] [ [ construct-empty ] dataflow optimize drop ] unit-test + +! Make sure we have sane heuristics +: should-inline? method method-word flat-length 10 <= ; + +[ t ] [ \ fixnum \ shift should-inline? ] unit-test +[ f ] [ \ array \ equal? should-inline? ] unit-test +[ f ] [ \ sequence \ hashcode* should-inline? ] unit-test +[ t ] [ \ array \ nth-unsafe should-inline? ] unit-test +[ t ] [ \ growable \ nth-unsafe should-inline? ] unit-test +[ t ] [ \ sbuf \ set-nth-unsafe should-inline? ] unit-test diff --git a/core/parser/parser.factor b/core/parser/parser.factor index d54bf1c1f4..486c589134 100755 --- a/core/parser/parser.factor +++ b/core/parser/parser.factor @@ -18,7 +18,7 @@ TUPLE: lexer text line line-text line-length column ; : ( text -- lexer ) 0 { set-lexer-text set-lexer-line } lexer construct - dup lexer-text empty? [ dup next-line ] unless ; + dup next-line ; : location ( -- loc ) file get lexer get lexer-line 2dup and From 59cc83c29614f33bd177ebfb2d8f40fd12fbffb4 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 01:49:05 -0600 Subject: [PATCH 179/269] Fix bugs in tools.test --- extra/tools/test/test.factor | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/extra/tools/test/test.factor b/extra/tools/test/test.factor index 192a248161..2cbdc3d7c7 100755 --- a/extra/tools/test/test.factor +++ b/extra/tools/test/test.factor @@ -81,7 +81,7 @@ M: expected-error summary "Traceback" swap third write-object ; : test-failures. ( assoc -- ) - dup [ + [ nl dup empty? [ drop @@ -90,15 +90,15 @@ M: expected-error summary "==== FAILING TESTS:" print [ swap vocab-heading. - [ nl failure. nl ] each + [ failure. nl ] each ] assoc-each ] if ] [ - drop "==== NOTHING TO TEST" print - ] if ; + "==== NOTHING TO TEST" print + ] if* ; : run-tests ( prefix -- failures ) - child-vocabs dup empty? [ f ] [ + child-vocabs dup empty? [ drop f ] [ [ dup run-test ] { } map>assoc [ second empty? not ] subset ] if ; From 6df325c16830f55c925fede788e997d8e4288099 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 01:49:30 -0600 Subject: [PATCH 180/269] Moved little-endian? to alien.c-types --- extra/io/unix/select/select.factor | 2 -- 1 file changed, 2 deletions(-) mode change 100644 => 100755 extra/io/unix/select/select.factor diff --git a/extra/io/unix/select/select.factor b/extra/io/unix/select/select.factor old mode 100644 new mode 100755 index c28686d2f2..06e257a610 --- a/extra/io/unix/select/select.factor +++ b/extra/io/unix/select/select.factor @@ -9,8 +9,6 @@ TUPLE: select-mx read-fdset write-fdset ; ! Factor's bit-arrays are an array of bytes, OS X expects ! FD_SET to be an array of cells, so we have to account for ! byte order differences on big endian platforms -: little-endian? 1 *char 1 = ; foldable - : munge ( i -- i' ) little-endian? [ BIN: 11000 bitxor ] unless ; inline From b14197fadcb607ffc84f9f05531c11e567cd0561 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 01:49:49 -0600 Subject: [PATCH 181/269] Remove obsolete files --- core/compiler/test/alien.factor | 356 ---------------------------- core/compiler/test/optimizer.factor | 303 ----------------------- 2 files changed, 659 deletions(-) delete mode 100755 core/compiler/test/alien.factor delete mode 100755 core/compiler/test/optimizer.factor diff --git a/core/compiler/test/alien.factor b/core/compiler/test/alien.factor deleted file mode 100755 index 4adb1c234b..0000000000 --- a/core/compiler/test/alien.factor +++ /dev/null @@ -1,356 +0,0 @@ -IN: temporary -USING: alien alien.c-types alien.syntax compiler kernel -namespaces namespaces tools.test sequences inference words -arrays parser quotations continuations inference.backend effects -namespaces.private io io.streams.string memory system threads -tools.test ; - -FUNCTION: void ffi_test_0 ; -[ ] [ ffi_test_0 ] unit-test - -FUNCTION: int ffi_test_1 ; -[ 3 ] [ ffi_test_1 ] unit-test - -FUNCTION: int ffi_test_2 int x int y ; -[ 5 ] [ 2 3 ffi_test_2 ] unit-test -[ "hi" 3 ffi_test_2 ] must-fail - -FUNCTION: int ffi_test_3 int x int y int z int t ; -[ 25 ] [ 2 3 4 5 ffi_test_3 ] unit-test - -FUNCTION: float ffi_test_4 ; -[ 1.5 ] [ ffi_test_4 ] unit-test - -FUNCTION: double ffi_test_5 ; -[ 1.5 ] [ ffi_test_5 ] unit-test - -FUNCTION: int ffi_test_9 int a int b int c int d int e int f int g ; -[ 28 ] [ 1 2 3 4 5 6 7 ffi_test_9 ] unit-test -[ "a" 2 3 4 5 6 7 ffi_test_9 ] must-fail -[ 1 2 3 4 5 6 "a" ffi_test_9 ] must-fail - -C-STRUCT: foo - { "int" "x" } - { "int" "y" } -; - -: make-foo ( x y -- foo ) - "foo" [ set-foo-y ] keep [ set-foo-x ] keep ; - -FUNCTION: int ffi_test_11 int a foo b int c ; - -[ 14 ] [ 1 2 3 make-foo 4 ffi_test_11 ] unit-test - -FUNCTION: int ffi_test_13 int a int b int c int d int e int f int g int h int i int j int k ; - -[ 66 ] [ 1 2 3 4 5 6 7 8 9 10 11 ffi_test_13 ] unit-test - -FUNCTION: foo ffi_test_14 int x int y ; - -[ 11 6 ] [ 11 6 ffi_test_14 dup foo-x swap foo-y ] unit-test - -FUNCTION: char* ffi_test_15 char* x char* y ; - -[ "foo" ] [ "xy" "zt" ffi_test_15 ] unit-test -[ "bar" ] [ "xy" "xy" ffi_test_15 ] unit-test -[ 1 2 ffi_test_15 ] must-fail - -C-STRUCT: bar - { "long" "x" } - { "long" "y" } - { "long" "z" } -; - -FUNCTION: bar ffi_test_16 long x long y long z ; - -[ 11 6 -7 ] [ - 11 6 -7 ffi_test_16 dup bar-x over bar-y rot bar-z -] unit-test - -C-STRUCT: tiny - { "int" "x" } -; - -FUNCTION: tiny ffi_test_17 int x ; - -[ 11 ] [ 11 ffi_test_17 tiny-x ] unit-test - -[ [ alien-indirect ] infer ] [ inference-error? ] must-fail-with - -: indirect-test-1 - "int" { } "cdecl" alien-indirect ; - -{ 1 1 } [ indirect-test-1 ] must-infer-as - -[ 3 ] [ "ffi_test_1" f dlsym indirect-test-1 ] unit-test - -[ -1 indirect-test-1 ] must-fail - -: indirect-test-2 - "int" { "int" "int" } "cdecl" alien-indirect data-gc ; - -{ 3 1 } [ indirect-test-2 ] must-infer-as - -[ 5 ] -[ 2 3 "ffi_test_2" f dlsym indirect-test-2 ] -unit-test - -: indirect-test-3 - "int" { "int" "int" "int" "int" } "stdcall" alien-indirect - data-gc ; - -<< "f-stdcall" f "stdcall" add-library >> - -[ f ] [ "f-stdcall" load-library ] unit-test -[ "stdcall" ] [ "f-stdcall" library library-abi ] unit-test - -: ffi_test_18 ( w x y z -- int ) - "int" "f-stdcall" "ffi_test_18" { "int" "int" "int" "int" } - alien-invoke data-gc ; - -[ 25 ] [ 2 3 4 5 ffi_test_18 ] unit-test - -: ffi_test_19 ( x y z -- bar ) - "bar" "f-stdcall" "ffi_test_19" { "long" "long" "long" } - alien-invoke data-gc ; - -[ 11 6 -7 ] [ - 11 6 -7 ffi_test_19 dup bar-x over bar-y rot bar-z -] unit-test - -FUNCTION: double ffi_test_6 float x float y ; -[ 6.0 ] [ 3.0 2.0 ffi_test_6 ] unit-test -[ "a" "b" ffi_test_6 ] must-fail - -FUNCTION: double ffi_test_7 double x double y ; -[ 6.0 ] [ 3.0 2.0 ffi_test_7 ] unit-test - -FUNCTION: double ffi_test_8 double x float y double z float t int w ; -[ 19.0 ] [ 3.0 2.0 1.0 6.0 7 ffi_test_8 ] unit-test - -FUNCTION: int ffi_test_10 int a int b double c int d float e int f int g int h ; -[ -34 ] [ 1 2 3.0 4 5.0 6 7 8 ffi_test_10 ] unit-test - -FUNCTION: void ffi_test_20 double x1, double x2, double x3, - double y1, double y2, double y3, - double z1, double z2, double z3 ; - -[ ] [ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ffi_test_20 ] unit-test - -! Make sure XT doesn't get clobbered in stack frame - -: ffi_test_31 - "void" - f "ffi_test_31" - { "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" } - alien-invoke code-gc 3 ; - -[ 3 ] [ 42 [ ] each ffi_test_31 ] unit-test - -FUNCTION: longlong ffi_test_21 long x long y ; - -[ 121932631112635269 ] -[ 123456789 987654321 ffi_test_21 ] unit-test - -FUNCTION: long ffi_test_22 long x longlong y longlong z ; - -[ 987655432 ] -[ 1111 121932631112635269 123456789 ffi_test_22 ] unit-test - -[ 1111 f 123456789 ffi_test_22 ] must-fail - -C-STRUCT: rect - { "float" "x" } - { "float" "y" } - { "float" "w" } - { "float" "h" } -; - -: - "rect" - [ set-rect-h ] keep - [ set-rect-w ] keep - [ set-rect-y ] keep - [ set-rect-x ] keep ; - -FUNCTION: int ffi_test_12 int a int b rect c int d int e int f ; - -[ 45 ] [ 1 2 3.0 4.0 5.0 6.0 7 8 9 ffi_test_12 ] unit-test - -[ 1 2 { 1 2 3 } 7 8 9 ffi_test_12 ] must-fail - -FUNCTION: float ffi_test_23 ( float[3] x, float[3] y ) ; - -[ 32.0 ] [ { 1.0 2.0 3.0 } >c-float-array { 4.0 5.0 6.0 } >c-float-array ffi_test_23 ] unit-test - -! Test odd-size structs -C-STRUCT: test-struct-1 { { "char" 1 } "x" } ; - -FUNCTION: test-struct-1 ffi_test_24 ; - -[ B{ 1 } ] [ ffi_test_24 ] unit-test - -C-STRUCT: test-struct-2 { { "char" 2 } "x" } ; - -FUNCTION: test-struct-2 ffi_test_25 ; - -[ B{ 1 2 } ] [ ffi_test_25 ] unit-test - -C-STRUCT: test-struct-3 { { "char" 3 } "x" } ; - -FUNCTION: test-struct-3 ffi_test_26 ; - -[ B{ 1 2 3 } ] [ ffi_test_26 ] unit-test - -C-STRUCT: test-struct-4 { { "char" 4 } "x" } ; - -FUNCTION: test-struct-4 ffi_test_27 ; - -[ B{ 1 2 3 4 } ] [ ffi_test_27 ] unit-test - -C-STRUCT: test-struct-5 { { "char" 5 } "x" } ; - -FUNCTION: test-struct-5 ffi_test_28 ; - -[ B{ 1 2 3 4 5 } ] [ ffi_test_28 ] unit-test - -C-STRUCT: test-struct-6 { { "char" 6 } "x" } ; - -FUNCTION: test-struct-6 ffi_test_29 ; - -[ B{ 1 2 3 4 5 6 } ] [ ffi_test_29 ] unit-test - -C-STRUCT: test-struct-7 { { "char" 7 } "x" } ; - -FUNCTION: test-struct-7 ffi_test_30 ; - -[ B{ 1 2 3 4 5 6 7 } ] [ ffi_test_30 ] unit-test - -C-STRUCT: test-struct-8 { "double" "x" } { "double" "y" } ; - -FUNCTION: double ffi_test_32 test-struct-8 x int y ; - -[ 9.0 ] [ - "test-struct-8" - 1.0 over set-test-struct-8-x - 2.0 over set-test-struct-8-y - 3 ffi_test_32 -] unit-test - -C-STRUCT: test-struct-9 { "float" "x" } { "float" "y" } ; - -FUNCTION: double ffi_test_33 test-struct-9 x int y ; - -[ 9.0 ] [ - "test-struct-9" - 1.0 over set-test-struct-9-x - 2.0 over set-test-struct-9-y - 3 ffi_test_33 -] unit-test - -C-STRUCT: test-struct-10 { "float" "x" } { "int" "y" } ; - -FUNCTION: double ffi_test_34 test-struct-10 x int y ; - -[ 9.0 ] [ - "test-struct-10" - 1.0 over set-test-struct-10-x - 2 over set-test-struct-10-y - 3 ffi_test_34 -] unit-test - -C-STRUCT: test-struct-11 { "int" "x" } { "int" "y" } ; - -FUNCTION: double ffi_test_35 test-struct-11 x int y ; - -[ 9.0 ] [ - "test-struct-11" - 1 over set-test-struct-11-x - 2 over set-test-struct-11-y - 3 ffi_test_35 -] unit-test - -C-STRUCT: test-struct-12 { "int" "a" } { "double" "x" } ; - -: make-struct-12 - "test-struct-12" - [ set-test-struct-12-x ] keep ; - -FUNCTION: double ffi_test_36 ( test-struct-12 x ) ; - -[ 1.23456 ] [ 1.23456 make-struct-12 ffi_test_36 ] unit-test - -! Test callbacks - -: callback-1 "void" { } "cdecl" [ ] alien-callback ; - -[ 0 1 ] [ [ callback-1 ] infer dup effect-in swap effect-out ] unit-test - -[ t ] [ callback-1 alien? ] unit-test - -: callback_test_1 "void" { } "cdecl" alien-indirect ; - -[ ] [ callback-1 callback_test_1 ] unit-test - -: callback-2 "void" { } "cdecl" [ [ 5 throw ] ignore-errors ] alien-callback ; - -[ ] [ callback-2 callback_test_1 ] unit-test - -: callback-3 "void" { } "cdecl" [ 5 "x" set ] alien-callback ; - -[ t ] [ - namestack* - 3 "x" set callback-3 callback_test_1 - namestack* eq? -] unit-test - -[ 5 ] [ - [ - 3 "x" set callback-3 callback_test_1 "x" get - ] with-scope -] unit-test - -: callback-4 - "void" { } "cdecl" [ "Hello world" write ] alien-callback - data-gc ; - -[ "Hello world" ] [ - [ callback-4 callback_test_1 ] string-out -] unit-test - -: callback-5 - "void" { } "cdecl" [ data-gc ] alien-callback ; - -[ "testing" ] [ - "testing" callback-5 callback_test_1 -] unit-test - -: callback-5a - "void" { } "cdecl" [ 8000000 f drop ] alien-callback ; - -! Hack; if we're on ARM, we probably don't have much RAM, so -! skip this test. -cpu "arm" = [ - [ "testing" ] [ - "testing" callback-5a callback_test_1 - ] unit-test -] unless - -: callback-6 - "void" { } "cdecl" [ [ continue ] callcc0 ] alien-callback ; - -[ 1 2 3 ] [ callback-6 callback_test_1 1 2 3 ] unit-test - -: callback-7 - "void" { } "cdecl" [ 1000 sleep ] alien-callback ; - -[ 1 2 3 ] [ callback-7 callback_test_1 1 2 3 ] unit-test - -[ f ] [ namespace global eq? ] unit-test - -: callback-8 - "void" { } "cdecl" [ - [ continue ] callcc0 - ] alien-callback ; - -[ ] [ callback-8 callback_test_1 ] unit-test diff --git a/core/compiler/test/optimizer.factor b/core/compiler/test/optimizer.factor deleted file mode 100755 index 987aace00a..0000000000 --- a/core/compiler/test/optimizer.factor +++ /dev/null @@ -1,303 +0,0 @@ -USING: arrays compiler generic hashtables inference kernel -kernel.private math optimizer prettyprint sequences sbufs -strings tools.test vectors words sequences.private quotations -optimizer.backend classes inference.dataflow tuples.private -continuations growable ; -IN: temporary - -[ H{ { 1 5 } { 3 4 } { 2 5 } } ] [ - H{ { 1 2 } { 3 4 } } H{ { 2 5 } } union* -] unit-test - -[ H{ { 1 4 } { 2 4 } { 3 4 } } ] [ - H{ { 1 2 } { 3 4 } } H{ { 2 3 } } union* -] unit-test - -! Test method inlining -[ f ] [ fixnum { } min-class ] unit-test - -[ string ] [ - \ string - [ integer string array reversed sbuf - slice vector quotation ] - sort-classes min-class -] unit-test - -[ fixnum ] [ - \ fixnum - [ fixnum integer object ] - sort-classes min-class -] unit-test - -[ integer ] [ - \ fixnum - [ integer float object ] - sort-classes min-class -] unit-test - -[ object ] [ - \ word - [ integer float object ] - sort-classes min-class -] unit-test - -[ reversed ] [ - \ reversed - [ integer reversed slice ] - sort-classes min-class -] unit-test - -GENERIC: xyz ( obj -- obj ) -M: array xyz xyz ; - -[ t ] [ \ xyz compiled? ] unit-test - -! Test predicate inlining -: pred-test-1 - dup fixnum? [ - dup integer? [ "integer" ] [ "nope" ] if - ] [ - "not a fixnum" - ] if ; - -[ 1 "integer" ] [ 1 pred-test-1 ] unit-test - -TUPLE: pred-test ; - -: pred-test-2 - dup tuple? [ - dup pred-test? [ "pred-test" ] [ "nope" ] if - ] [ - "not a tuple" - ] if ; - -[ T{ pred-test } "pred-test" ] [ T{ pred-test } pred-test-2 ] unit-test - -: pred-test-3 - dup pred-test? [ - dup tuple? [ "pred-test" ] [ "nope" ] if - ] [ - "not a tuple" - ] if ; - -[ T{ pred-test } "pred-test" ] [ T{ pred-test } pred-test-3 ] unit-test - -: inline-test - "nom" = ; - -[ t ] [ "nom" inline-test ] unit-test -[ f ] [ "shayin" inline-test ] unit-test -[ f ] [ 3 inline-test ] unit-test - -: fixnum-declarations >fixnum 24 shift 1234 bitxor ; - -[ ] [ 1000000 fixnum-declarations . ] unit-test - -! regression - -: literal-not-branch 0 not [ ] [ ] if ; - -[ ] [ literal-not-branch ] unit-test - -! regression - -: bad-kill-1 ( a b -- c d e ) [ 3 f ] [ dup bad-kill-1 ] if ; inline -: bad-kill-2 bad-kill-1 drop ; - -[ 3 ] [ t bad-kill-2 ] unit-test - -! regression -: (the-test) ( x -- y ) dup 0 > [ 1- (the-test) ] when ; inline -: the-test ( -- x y ) 2 dup (the-test) ; - -[ 2 0 ] [ the-test ] unit-test - -! regression -: (double-recursion) ( start end -- ) - < [ - 6 1 (double-recursion) - 3 2 (double-recursion) - ] when ; inline - -: double-recursion 0 2 (double-recursion) ; - -[ ] [ double-recursion ] unit-test - -! regression -: double-label-1 ( a b c -- d ) - [ f double-label-1 ] [ swap nth-unsafe ] if ; inline - -: double-label-2 ( a -- b ) - dup array? [ ] [ ] if 0 t double-label-1 ; - -[ 0 ] [ 10 double-label-2 ] unit-test - -! regression -GENERIC: void-generic ( obj -- * ) -: breakage "hi" void-generic ; -[ t ] [ \ breakage compiled? ] unit-test -[ breakage ] must-fail - -! regression -: test-0 ( n -- ) dup 0 = [ drop ] [ 1- test-0 ] if ; inline -: test-1 ( n -- ) t [ test-0 ] [ delegate dup [ test-1 ] [ drop ] if ] if ; inline -: test-2 ( -- ) 5 test-1 ; - -[ f ] [ f test-2 ] unit-test - -: branch-fold-regression-0 ( m -- n ) - t [ ] [ 1+ branch-fold-regression-0 ] if ; inline - -: branch-fold-regression-1 ( -- m ) - 10 branch-fold-regression-0 ; - -[ 10 ] [ branch-fold-regression-1 ] unit-test - -! another regression -: constant-branch-fold-0 "hey" ; foldable -: constant-branch-fold-1 constant-branch-fold-0 "hey" = ; inline -[ 1 ] [ [ constant-branch-fold-1 [ 1 ] [ 2 ] if ] compile-call ] unit-test - -! another regression -: foo f ; -: bar foo 4 4 = and ; -[ f ] [ bar ] unit-test - -! ensure identities are working in some form -[ t ] [ - [ { number } declare 0 + ] dataflow optimize - [ #push? ] node-exists? not -] unit-test - -! compiling with a non-literal class failed -: -regression ; - -[ t ] [ \ -regression compiled? ] unit-test - -GENERIC: foozul ( a -- b ) -M: reversed foozul ; -M: integer foozul ; -M: slice foozul ; - -[ reversed ] [ reversed \ foozul specific-method ] unit-test - -! regression -: constant-fold-2 f ; foldable -: constant-fold-3 4 ; foldable - -[ f t ] [ - [ constant-fold-2 constant-fold-3 4 = ] compile-call -] unit-test - -: constant-fold-4 f ; foldable -: constant-fold-5 f ; foldable - -[ f ] [ - [ constant-fold-4 constant-fold-5 or ] compile-call -] unit-test - -[ 5 ] [ 5 [ 0 + ] compile-call ] unit-test -[ 5 ] [ 5 [ 0 swap + ] compile-call ] unit-test - -[ 5 ] [ 5 [ 0 - ] compile-call ] unit-test -[ -5 ] [ 5 [ 0 swap - ] compile-call ] unit-test -[ 0 ] [ 5 [ dup - ] compile-call ] unit-test - -[ 5 ] [ 5 [ 1 * ] compile-call ] unit-test -[ 5 ] [ 5 [ 1 swap * ] compile-call ] unit-test -[ 0 ] [ 5 [ 0 * ] compile-call ] unit-test -[ 0 ] [ 5 [ 0 swap * ] compile-call ] unit-test -[ -5 ] [ 5 [ -1 * ] compile-call ] unit-test -[ -5 ] [ 5 [ -1 swap * ] compile-call ] unit-test - -[ 0 ] [ 5 [ 1 mod ] compile-call ] unit-test -[ 0 ] [ 5 [ 1 rem ] compile-call ] unit-test - -[ 5 ] [ 5 [ -1 bitand ] compile-call ] unit-test -[ 0 ] [ 5 [ 0 bitand ] compile-call ] unit-test -[ 5 ] [ 5 [ -1 swap bitand ] compile-call ] unit-test -[ 0 ] [ 5 [ 0 swap bitand ] compile-call ] unit-test -[ 5 ] [ 5 [ dup bitand ] compile-call ] unit-test - -[ 5 ] [ 5 [ 0 bitor ] compile-call ] unit-test -[ -1 ] [ 5 [ -1 bitor ] compile-call ] unit-test -[ 5 ] [ 5 [ 0 swap bitor ] compile-call ] unit-test -[ -1 ] [ 5 [ -1 swap bitor ] compile-call ] unit-test -[ 5 ] [ 5 [ dup bitor ] compile-call ] unit-test - -[ 5 ] [ 5 [ 0 bitxor ] compile-call ] unit-test -[ 5 ] [ 5 [ 0 swap bitxor ] compile-call ] unit-test -[ -6 ] [ 5 [ -1 bitxor ] compile-call ] unit-test -[ -6 ] [ 5 [ -1 swap bitxor ] compile-call ] unit-test -[ 0 ] [ 5 [ dup bitxor ] compile-call ] unit-test - -[ 0 ] [ 5 [ 0 swap shift ] compile-call ] unit-test -[ 5 ] [ 5 [ 0 shift ] compile-call ] unit-test - -[ f ] [ 5 [ dup < ] compile-call ] unit-test -[ t ] [ 5 [ dup <= ] compile-call ] unit-test -[ f ] [ 5 [ dup > ] compile-call ] unit-test -[ t ] [ 5 [ dup >= ] compile-call ] unit-test - -[ t ] [ 5 [ dup eq? ] compile-call ] unit-test -[ t ] [ 5 [ dup = ] compile-call ] unit-test -[ t ] [ 5 [ dup number= ] compile-call ] unit-test -[ t ] [ \ vector [ \ vector = ] compile-call ] unit-test - -GENERIC: detect-number ( obj -- obj ) -M: number detect-number ; - -[ 10 f [ 0 + detect-number ] compile-call ] must-fail - -! Regression -[ 4 [ + ] ] [ 2 2 [ [ + ] [ call ] keep ] compile-call ] unit-test - -! Regression -USE: sorting -USE: sorting.private - -: old-binsearch ( elt quot seq -- elt quot i ) - dup length 1 <= [ - slice-from - ] [ - [ midpoint swap call ] 3keep roll dup zero? - [ drop dup slice-from swap midpoint@ + ] - [ partition old-binsearch ] if - ] if ; inline - -[ 10 ] [ - 10 20 >vector - [ [ - ] swap old-binsearch ] compile-call 2nip -] unit-test - -! Regression -TUPLE: silly-tuple a b ; - -[ 1 2 { silly-tuple-a silly-tuple-b } ] [ - T{ silly-tuple f 1 2 } - [ - { silly-tuple-a silly-tuple-b } [ get-slots ] keep - ] compile-call -] unit-test - -! Regression -: empty-compound ; - -: node-successor-f-bug ( x -- * ) - [ 3 throw ] [ empty-compound ] compose [ 3 throw ] if ; - -[ t ] [ \ node-successor-f-bug compiled? ] unit-test - -: construct-empty-bug construct-empty ; - -[ ] [ [ construct-empty ] dataflow optimize drop ] unit-test - -! Make sure we have sane heuristics -: should-inline? method method-word flat-length 10 <= ; - -[ t ] [ \ fixnum \ shift should-inline? ] unit-test -[ f ] [ \ array \ equal? should-inline? ] unit-test -[ f ] [ \ sequence \ hashcode* should-inline? ] unit-test -[ t ] [ \ array \ nth-unsafe should-inline? ] unit-test -[ t ] [ \ growable \ nth-unsafe should-inline? ] unit-test -[ t ] [ \ sbuf \ set-nth-unsafe should-inline? ] unit-test From 7adb07bcc4354c8f32befc3cfce5242c6b11687e Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Fri, 8 Feb 2008 03:11:47 -0600 Subject: [PATCH 182/269] concurrency docs fix --- extra/concurrency/concurrency-docs.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/concurrency/concurrency-docs.factor b/extra/concurrency/concurrency-docs.factor index f04811b72a..538ed847f0 100644 --- a/extra/concurrency/concurrency-docs.factor +++ b/extra/concurrency/concurrency-docs.factor @@ -138,7 +138,7 @@ ARTICLE: { "concurrency" "servers" } "Servers" ARTICLE: { "concurrency" "synchronous-sends" } "Synchronous Sends" { $link send } " sends a message asynchronously, and the sending process continues immediately. The 'pong server' example shown previously all sent messages to the server and waited for a reply back from the server. This pattern of synchronous sending is made easier with " { $link send-synchronous } ".\n\nThis word will send a message to the given process and immediately block until a reply is received for this particular message send. It leaves the reply on the stack. Note that it doesn't wait for just any reply, it waits for a reply specifically to this send.\n\nTo do this it wraps the requested message inside a tagged message format using " { $link tag-message } ":" { $code "\"My Message\" tag-message .\n => { ...from... ...tag... \"My Message\" }" } -"The message is wrapped in array where the first item is the sending process object, the second is a unique tag, and the third is the original message. Server processes can use the 'from' to reply to the process that originally sent the message. The tag can is used in the receiving server to include the value in the reply. After the send-synchronous call the current process will block waiting for a reply that has the exact same tag. In this way you can be sure that the reply you got was for the specific message sent. Here is the pong-server recoded to use 'send-synchronous':" +"The message is wrapped in array where the first item is the sending process object, the second is a unique tag, and the third is the original message. Server processes can use the 'from' to reply to the process that originally sent the message. The tag can used in the receiving server to include the value in the reply. After the send-synchronous call the current process will block waiting for a reply that has the exact same tag. In this way you can be sure that the reply you got was for the specific message sent. Here is the pong-server recoded to use 'send-synchronous':" { $code ": pong-server ( -- )\n receive {\n { { ?from ?tag \"ping\" } [ ?tag \"pong\" 2array ?from send pong-server ] }\n { { ?from _ } [ ?tag \"server shutdown\" 2array ?from send ] }\n } match-cond ;\n\n[ pong-server ] spawn \"ping\" swap send-synchronous .\n => \"pong\"" } "Notice that the code to send the reply back to the original caller wraps the reply in an array where the first item is the tag originally sent. 'send-synchronous' only returns if it receives a reply containing that specific tag." ; From 3f38bf18ec98e02af5a42422d167bc8122053b89 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Fri, 8 Feb 2008 03:14:08 -0600 Subject: [PATCH 183/269] concurrency docs fix --- extra/concurrency/concurrency-docs.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/concurrency/concurrency-docs.factor b/extra/concurrency/concurrency-docs.factor index 538ed847f0..16a2e65a90 100644 --- a/extra/concurrency/concurrency-docs.factor +++ b/extra/concurrency/concurrency-docs.factor @@ -138,7 +138,7 @@ ARTICLE: { "concurrency" "servers" } "Servers" ARTICLE: { "concurrency" "synchronous-sends" } "Synchronous Sends" { $link send } " sends a message asynchronously, and the sending process continues immediately. The 'pong server' example shown previously all sent messages to the server and waited for a reply back from the server. This pattern of synchronous sending is made easier with " { $link send-synchronous } ".\n\nThis word will send a message to the given process and immediately block until a reply is received for this particular message send. It leaves the reply on the stack. Note that it doesn't wait for just any reply, it waits for a reply specifically to this send.\n\nTo do this it wraps the requested message inside a tagged message format using " { $link tag-message } ":" { $code "\"My Message\" tag-message .\n => { ...from... ...tag... \"My Message\" }" } -"The message is wrapped in array where the first item is the sending process object, the second is a unique tag, and the third is the original message. Server processes can use the 'from' to reply to the process that originally sent the message. The tag can used in the receiving server to include the value in the reply. After the send-synchronous call the current process will block waiting for a reply that has the exact same tag. In this way you can be sure that the reply you got was for the specific message sent. Here is the pong-server recoded to use 'send-synchronous':" +"The message is wrapped in array where the first item is the sending process object, the second is a unique tag, and the third is the original message. Server processes can use the 'from' to reply to the process that originally sent the message. The tag is used in the receiving server to include the value in the reply. After the send-synchronous call the current process will block waiting for a reply that has the exact same tag. In this way you can be sure that the reply you got was for the specific message sent. Here is the pong-server recoded to use 'send-synchronous':" { $code ": pong-server ( -- )\n receive {\n { { ?from ?tag \"ping\" } [ ?tag \"pong\" 2array ?from send pong-server ] }\n { { ?from _ } [ ?tag \"server shutdown\" 2array ?from send ] }\n } match-cond ;\n\n[ pong-server ] spawn \"ping\" swap send-synchronous .\n => \"pong\"" } "Notice that the code to send the reply back to the original caller wraps the reply in an array where the first item is the tag originally sent. 'send-synchronous' only returns if it receives a reply containing that specific tag." ; From d41bfc64f1686af2a53fb9be984b8324763aee28 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 17:00:42 -0600 Subject: [PATCH 184/269] Minor tests fix --- extra/tools/test/test.factor | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/extra/tools/test/test.factor b/extra/tools/test/test.factor index 2cbdc3d7c7..0b5e436e44 100755 --- a/extra/tools/test/test.factor +++ b/extra/tools/test/test.factor @@ -61,9 +61,14 @@ M: expected-error summary dup vocab-source-loaded? [ vocab-tests-path dup [ dup ?resource-path exists? [ - [ "temporary" forget-vocab ] with-compilation-unit + [ + "temporary" forget-vocab + ] with-compilation-unit dup run-file - [ dup forget-source ] with-compilation-unit + [ + dup forget-source + "temporary" forget-vocab + ] with-compilation-unit ] when ] when ] when drop ; From 5570f367a631dddd2e0f42078baa15641ed12567 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Fri, 8 Feb 2008 20:09:59 -0600 Subject: [PATCH 185/269] builder: build-status variable --- extra/builder/builder.factor | 12 ++++++++++++ 1 file changed, 12 insertions(+) mode change 100755 => 100644 extra/builder/builder.factor diff --git a/extra/builder/builder.factor b/extra/builder/builder.factor old mode 100755 new mode 100644 index 9af79efb29..1c5f5ff3fd --- a/extra/builder/builder.factor +++ b/extra/builder/builder.factor @@ -59,8 +59,12 @@ VAR: stamp ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +SYMBOL: build-status + : build ( -- ) + "running" build-status set-global + datestamp >stamp "/builds/factor" cd @@ -98,6 +102,8 @@ VAR: stamp { "make" "clean" } run-process drop + ! "vm" build-status set-global + `{ { +arguments+ { "make" ,[ target ] } } { +stdout+ "../compile-log" } @@ -116,6 +122,8 @@ VAR: stamp [ "builder: image download" email-string ] cleanup + ! "bootstrap" build-status set-global + `{ { +arguments+ { ,[ factor-binary ] @@ -133,6 +141,8 @@ VAR: stamp "builder: bootstrap" throw ] if + ! "test" build-status set-global + `{ ,[ factor-binary ] "-run=builder.test" } run-process drop "../load-everything-log" exists? @@ -143,6 +153,8 @@ VAR: stamp [ "builder: failing tests" "../failing-tests" email-file ] when + ! "ready" build-status set-global + ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! From 7b07ababba5a9f95d17fa9c67fbfe006d97916cd Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Fri, 8 Feb 2008 20:16:12 -0600 Subject: [PATCH 186/269] add builder.server --- extra/builder/server/server.factor | 68 ++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 extra/builder/server/server.factor diff --git a/extra/builder/server/server.factor b/extra/builder/server/server.factor new file mode 100644 index 0000000000..672de1e47d --- /dev/null +++ b/extra/builder/server/server.factor @@ -0,0 +1,68 @@ + +USING: kernel continuations namespaces threads match bake concurrency builder ; + +IN: builder.server + +! : build-server ( -- ) +! receive +! { +! { +! "start" +! [ [ build ] in-thread ] +! } + +! { +! { ?from ?tag "status" } +! [ `{ ?tag ,[ build-status get ] } ?from send ] +! } +! } +! match-cond +! build-server ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +! : build-server ( -- ) +! receive +! { +! { +! "start" +! [ +! [ [ build ] [ drop ] recover "idle" build-status set-global ] in-thread +! ] +! } + +! { +! { ?from ?tag "status" } +! [ `{ ?tag ,[ build-status get ] } ?from send ] +! } +! } +! match-cond +! build-server ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: build-server ( -- ) + receive + { + { + "start" + [ + build-status get "idle" = + build-status get f = + or + [ + [ [ build ] [ drop ] recover "idle" build-status set-global ] + in-thread + ] + when + ] + } + + { + { ?from ?tag "status" } + [ `{ ?tag ,[ build-status get ] } ?from send ] + } + } + match-cond + build-server ; + From d7af06c75ae454e15097108af22f9544a7e6a7ee Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 21:13:05 -0600 Subject: [PATCH 187/269] Remove obsolete scripts --- misc/integration/deploy-size-test.factor | 28 ------- misc/integration/macosx-deploy.factor | 24 ------ misc/integration/macosx.sh | 43 ----------- misc/integration/test.sh | 93 ------------------------ misc/integration/unix-arm.sh | 10 --- misc/integration/unix-ppc.sh | 10 --- misc/integration/unix-x86.32.sh | 21 ------ misc/integration/unix-x86.64.sh | 10 --- misc/integration/x11-deploy.factor | 8 -- 9 files changed, 247 deletions(-) delete mode 100644 misc/integration/deploy-size-test.factor delete mode 100644 misc/integration/macosx-deploy.factor delete mode 100644 misc/integration/macosx.sh delete mode 100644 misc/integration/test.sh delete mode 100644 misc/integration/unix-arm.sh delete mode 100644 misc/integration/unix-ppc.sh delete mode 100644 misc/integration/unix-x86.32.sh delete mode 100644 misc/integration/unix-x86.64.sh delete mode 100644 misc/integration/x11-deploy.factor diff --git a/misc/integration/deploy-size-test.factor b/misc/integration/deploy-size-test.factor deleted file mode 100644 index 91cdaba293..0000000000 --- a/misc/integration/deploy-size-test.factor +++ /dev/null @@ -1,28 +0,0 @@ -USING: tools.deploy sequences io.files io.launcher io -kernel concurrency prettyprint ; - -"." resource-path cd - -"deploy-log" make-directory - -{ - "automata.ui" - "boids.ui" - "bunny" - "color-picker" - "gesture-logger" - "golden-section" - "hello-world" - "hello-ui" - "lsys.ui" - "maze" - "nehe" - "tetris" - "catalyst-talk" -} [ - dup - "deploy-log/" over append - [ deploy ] with-stream - dup file-length 1024 /f - 2array -] parallel-map . diff --git a/misc/integration/macosx-deploy.factor b/misc/integration/macosx-deploy.factor deleted file mode 100644 index f1e6e7fe06..0000000000 --- a/misc/integration/macosx-deploy.factor +++ /dev/null @@ -1,24 +0,0 @@ -USING: tools.deploy.app sequences io.files io.launcher io -kernel concurrency ; - -"." resource-path cd - -"deploy-log" make-directory - -{ - "automata.ui" - "boids.ui" - "bunny" - "color-picker" - "gesture-logger" - "golden-section" - "hello-ui" - "lsys.ui" - "maze" - "nehe" - "tetris" - "catalyst-talk" -} [ - "deploy-log/" over append - [ deploy.app ] with-stream -] parallel-each diff --git a/misc/integration/macosx.sh b/misc/integration/macosx.sh deleted file mode 100644 index dafe9524c6..0000000000 --- a/misc/integration/macosx.sh +++ /dev/null @@ -1,43 +0,0 @@ -CPU=$1 - -if [ "$CPU" = "x86.32" ]; then - TARGET="macosx-x86" -elif [ "$CPU" = "ppc" ]; then - TARGET="macosx-ppc" - CPU = "macosx-ppc" -else - echo "Specify a CPU" - exit 1 -fi - -EXE=factor - -bash misc/integration/test.sh \ - $EXE \ - $CPU \ - $TARGET \ - no \ - no \ - no \ - "X11=1" \ - "-ui-backend=x11" \ - "-x11" || exit 1 - -echo "Testing deployment" -$EXE "misc/integration/x11-deploy.factor" -run=none $VM_LOG $BOOT_LOG /tmp/factor-$$ - - $EXE -i=$IMAGE \ - /tmp/factor-$$ \ - -run=none \ - >$LOAD_LOG $TEST_LOG $BENCHMARK_LOG [ deploy ] with-stream From 52d91bf0bc0a568ae4d561890cd0082b3410b387 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 21:15:29 -0600 Subject: [PATCH 188/269] Add try-process word --- extra/benchmark/bootstrap2/bootstrap2.factor | 2 +- extra/bootstrap/image/upload/upload.factor | 3 +-- extra/editors/emacs/emacs.factor | 2 +- extra/editors/textmate/textmate.factor | 2 +- extra/io/launcher/launcher-docs.factor | 10 ++++++++++ extra/io/launcher/launcher.factor | 9 +++++++++ extra/logging/parser/parser.factor | 10 +++++++--- extra/tools/deploy/backend/backend.factor | 5 ++++- extra/tools/deploy/macosx/macosx.factor | 4 ++-- 9 files changed, 36 insertions(+), 11 deletions(-) mode change 100644 => 100755 extra/bootstrap/image/upload/upload.factor diff --git a/extra/benchmark/bootstrap2/bootstrap2.factor b/extra/benchmark/bootstrap2/bootstrap2.factor index 54bc73f4a1..f57e92e5e0 100755 --- a/extra/benchmark/bootstrap2/bootstrap2.factor +++ b/extra/benchmark/bootstrap2/bootstrap2.factor @@ -9,6 +9,6 @@ IN: benchmark.bootstrap2 "-i=" my-boot-image-name append , "-output-image=foo.image" , "-no-user-init" , - ] { } make run-process drop ; + ] { } make try-process ; MAIN: bootstrap-benchmark diff --git a/extra/bootstrap/image/upload/upload.factor b/extra/bootstrap/image/upload/upload.factor old mode 100644 new mode 100755 index a9f5d1dcd4..3b5ab4cb77 --- a/extra/bootstrap/image/upload/upload.factor +++ b/extra/bootstrap/image/upload/upload.factor @@ -16,8 +16,7 @@ bootstrap.image sequences io namespaces io.launcher math ; : upload-images ( -- ) [ "scp" , boot-image-names % "checksums.txt" , destination , - ] { } make run-process - wait-for-process zero? [ "Upload failed" throw ] unless ; + ] { } make try-process ; : new-images ( -- ) make-images compute-checksums upload-images ; diff --git a/extra/editors/emacs/emacs.factor b/extra/editors/emacs/emacs.factor index 31e0761043..966c4f368e 100755 --- a/extra/editors/emacs/emacs.factor +++ b/extra/editors/emacs/emacs.factor @@ -8,7 +8,7 @@ IN: editors.emacs "--no-wait" , "+" swap number>string append , , - ] { } make run-process drop ; + ] { } make try-process ; : emacs ( word -- ) where first2 emacsclient ; diff --git a/extra/editors/textmate/textmate.factor b/extra/editors/textmate/textmate.factor index 0145ccae81..12d45aa192 100755 --- a/extra/editors/textmate/textmate.factor +++ b/extra/editors/textmate/textmate.factor @@ -5,6 +5,6 @@ IN: editors.textmate : textmate-location ( file line -- ) [ "mate" , "-a" , "-l" , number>string , , ] { } make - run-process drop ; + try-process ; [ textmate-location ] edit-hook set-global diff --git a/extra/io/launcher/launcher-docs.factor b/extra/io/launcher/launcher-docs.factor index 4979f135ac..e414d98d65 100755 --- a/extra/io/launcher/launcher-docs.factor +++ b/extra/io/launcher/launcher-docs.factor @@ -116,6 +116,15 @@ HELP: run-detached "The output value can be passed to " { $link wait-for-process } " to get an exit code." } ; +HELP: process-failed +{ $values { "code" "an exit status" } } +{ $description "Throws a " { $link process-failed } " error." } +{ $error-description "Thrown by " { $link try-process } " if the process exited with a non-zero status code." } ; + +HELP: try-process +{ $values { "desc" "a launch descriptor" } } +{ $description "Launches a process and waits for it to complete. If it exits with a non-zero status code, throws a " { $link process-failed } " error." } ; + HELP: kill-process { $values { "process" process } } { $description "Kills a running process. Does nothing if the process has already exited." } ; @@ -175,6 +184,7 @@ $nl "The following words are used to launch processes:" { $subsection run-process } { $subsection run-detached } +{ $subsection try-process } "Stopping processes:" { $subsection kill-process } "Redirecting standard input and output to a pipe:" diff --git a/extra/io/launcher/launcher.factor b/extra/io/launcher/launcher.factor index f2ed59a591..7044004218 100755 --- a/extra/io/launcher/launcher.factor +++ b/extra/io/launcher/launcher.factor @@ -84,6 +84,15 @@ HOOK: run-process* io-backend ( desc -- handle ) : run-detached ( desc -- process ) >descriptor H{ { +detached+ t } } union run-process ; +TUPLE: process-failed code ; + +: process-failed ( code -- * ) + process-failed construct-boa throw ; + +: try-process ( desc -- ) + run-process wait-for-process dup zero? + [ drop ] [ process-failed ] if ; + HOOK: kill-process* io-backend ( handle -- ) : kill-process ( process -- ) diff --git a/extra/logging/parser/parser.factor b/extra/logging/parser/parser.factor index f1cb7aa17e..f9bf97a442 100755 --- a/extra/logging/parser/parser.factor +++ b/extra/logging/parser/parser.factor @@ -2,13 +2,17 @@ ! See http://factorcode.org/license.txt for BSD license. USING: parser-combinators memoize kernel sequences logging arrays words strings vectors io io.files -namespaces combinators combinators.lib logging.server ; +namespaces combinators combinators.lib logging.server +calendar ; IN: logging.parser : string-of satisfy [ >string ] <@ ; +SYMBOL: multiline + : 'date' - [ CHAR: ] eq? not ] string-of + multiline-header token [ drop multiline ] <@ + [ CHAR: ] eq? not ] string-of [ rfc3339>timestamp ] <@ <|> "[" "]" surrounded-by ; : 'log-level' @@ -41,7 +45,7 @@ MEMO: 'log-line' ( -- parser ) first malformed eq? ; : multiline? ( line -- ? ) - first first CHAR: - = ; + first multiline eq? ; : malformed-line "Warning: malformed log line:" print diff --git a/extra/tools/deploy/backend/backend.factor b/extra/tools/deploy/backend/backend.factor index c295f6369d..2439ef8636 100755 --- a/extra/tools/deploy/backend/backend.factor +++ b/extra/tools/deploy/backend/backend.factor @@ -22,7 +22,10 @@ IN: tools.deploy.backend +stdout+ +stderr+ set ] H{ } make-assoc dup duplex-stream-out dispose - copy-lines ; + dup copy-lines + process-stream-process wait-for-process zero? [ + "Deployment failed" throw + ] unless ; : make-boot-image ( -- ) #! If stage1 image doesn't exist, create one. diff --git a/extra/tools/deploy/macosx/macosx.factor b/extra/tools/deploy/macosx/macosx.factor index 1bbf198ea0..eb1a4af4a7 100755 --- a/extra/tools/deploy/macosx/macosx.factor +++ b/extra/tools/deploy/macosx/macosx.factor @@ -8,10 +8,10 @@ QUALIFIED: unix IN: tools.deploy.macosx : touch ( path -- ) - { "touch" } swap add run-process drop ; + { "touch" } swap add try-process ; : rm ( path -- ) - { "rm" "-rf" } swap add run-process drop ; + { "rm" "-rf" } swap add try-process ; : bundle-dir ( -- dir ) vm parent-directory parent-directory ; From 20649302fa59634b8bf3fc5aa99f72b94f2d2c10 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 21:47:35 -0600 Subject: [PATCH 189/269] Fix a couple of issues with futures --- extra/concurrency/concurrency-tests.factor | 14 +++++++--- extra/concurrency/concurrency.factor | 30 +++++++++++++--------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/extra/concurrency/concurrency-tests.factor b/extra/concurrency/concurrency-tests.factor index b6f62d1779..1a19ce7096 100755 --- a/extra/concurrency/concurrency-tests.factor +++ b/extra/concurrency/concurrency-tests.factor @@ -112,9 +112,9 @@ SYMBOL: value ! The following unit test blocks forever if the ! exception does not propogate. Uncomment when ! this is fixed (via a timeout). -! [ -! [ "this should propogate" throw ] future ?future -! ] must-fail +[ + [ "this should propogate" throw ] future ?future +] must-fail [ ] [ [ "this should not propogate" throw ] future drop @@ -127,4 +127,10 @@ SYMBOL: value [ f ] [ [ "testing unregistering on error" throw ] spawn 100 sleep process-pid get-process -] unit-test \ No newline at end of file +] unit-test + +! Race condition with futures +[ 3 3 ] [ + [ 3 ] future + dup ?future swap ?future +] unit-test \ No newline at end of file diff --git a/extra/concurrency/concurrency.factor b/extra/concurrency/concurrency.factor index cf44ab125c..e4972c9030 100755 --- a/extra/concurrency/concurrency.factor +++ b/extra/concurrency/concurrency.factor @@ -264,29 +264,35 @@ PRIVATE> #! so the server continuation gets its new self updated. self swap call ; +TUPLE: future value processes ; + +: notify-future ( value future -- ) + tuck set-future-value + dup future-processes [ schedule-thread ] each + f swap set-future-processes ; + : future ( quot -- future ) #! Spawn a process to call the quotation and immediately return #! a 'future' on the stack. The future can later be queried with #! ?future. If the quotation has completed the result will be returned. #! If not, the process will block until the quotation completes. #! 'quot' must have stack effect ( -- X ). - [ self send ] compose spawn ; + \ future construct-empty [ + [ + >r [ t 2array ] compose [ f 2array ] recover r> + notify-future + ] 2curry spawn drop + ] keep ; : ?future ( future -- result ) #! Block the process until the future has completed and then #! place the result on the stack. Return the result #! immediately if the future has completed. - process-mailbox mailbox-get ; - -: parallel-map ( seq quot -- newseq ) - #! Spawn a process to apply quot to each element of seq, - #! joining the results into a sequence at the end. - [ curry future ] curry map [ ?future ] map ; - -: parallel-each ( seq quot -- ) - #! Spawn a process to apply quot to each element of seq, - #! and waits for all processes to complete. - [ f ] compose parallel-map drop ; + dup future-value [ + first2 [ throw ] unless + ] [ + dup [ future-processes push stop ] curry callcc0 ?future + ] ?if ; TUPLE: promise fulfilled? value processes ; From f05cf861eb032f3215690557f16cda2bf4f57394 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 8 Feb 2008 21:47:47 -0600 Subject: [PATCH 190/269] Fix USING: in io.launcher --- extra/io/launcher/launcher.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/io/launcher/launcher.factor b/extra/io/launcher/launcher.factor index 7044004218..4a6bbf46fb 100755 --- a/extra/io/launcher/launcher.factor +++ b/extra/io/launcher/launcher.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: io io.backend system kernel namespaces strings hashtables sequences assocs combinators vocabs.loader init threads -continuations ; +continuations math ; IN: io.launcher ! Non-blocking process exit notification facility From f45f6879ab04d4d115ee91b21493471592971fb9 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Fri, 8 Feb 2008 23:28:06 -0600 Subject: [PATCH 191/269] Makefile: winnt target downloads dlls --- Makefile | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 05a185f643..9776027a59 100755 --- a/Makefile +++ b/Makefile @@ -123,7 +123,15 @@ solaris-x86-32: solaris-x86-64: $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.solaris.x86.64 -winnt-x86-32: +freetype6.dll: + wget http://factorcode.org/dlls/freetype6.dll + chmod 755 freetype6.dll + +zlib1.dll: + wget http://factorcode.org/dlls/zlib1.dll + chmod 755 zlib1.dll + +winnt-x86-32: freetype6.dll zlib1.dll $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.32 winnt-x86-64: From d65bde09d1a0f6eca0511826eb60d7b493232e25 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 00:16:14 -0600 Subject: [PATCH 192/269] Fix bootstrap --- core/alien/c-types/c-types.factor | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/alien/c-types/c-types.factor b/core/alien/c-types/c-types.factor index ed0721a7ff..fbd49cedbb 100755 --- a/core/alien/c-types/c-types.factor +++ b/core/alien/c-types/c-types.factor @@ -7,6 +7,9 @@ math.parser cpu.architecture alien alien.accessors quotations system compiler.units ; IN: alien.c-types +DEFER: +DEFER: *char + : little-endian? ( -- ? ) 1 *char 1 = ; foldable TUPLE: c-type From cb2dc00762edf5101c3a5689f541cfec39a72252 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 00:16:24 -0600 Subject: [PATCH 193/269] Add MAIN: to bootstrap.image.download --- extra/bootstrap/image/download/download.factor | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/extra/bootstrap/image/download/download.factor b/extra/bootstrap/image/download/download.factor index deed045221..df559f49da 100644 --- a/extra/bootstrap/image/download/download.factor +++ b/extra/bootstrap/image/download/download.factor @@ -23,3 +23,7 @@ bootstrap.image sequences io ; "Boot image up to date" print drop ] if ; + +: download-my-image ( -- ) my-arch download-image ; + +MAIN: download-my-image From 6f0e64bb4cb5843174c67df58bdd6c5bb5639a76 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 00:16:30 -0600 Subject: [PATCH 194/269] Add some tags --- extra/concurrency/distributed/tags.txt | 1 + extra/cpu/8080/emulator/tags.txt | 2 +- extra/cpu/8080/tags.txt | 2 +- extra/cryptlib/tags.txt | 1 + extra/http/server/tags.txt | 1 + extra/ldap/tags.txt | 1 + extra/openssl/tags.txt | 1 + extra/smtp/tags.txt | 1 + extra/xml-rpc/tags.txt | 1 + extra/xml/tags.txt | 1 + 10 files changed, 10 insertions(+), 2 deletions(-) diff --git a/extra/concurrency/distributed/tags.txt b/extra/concurrency/distributed/tags.txt index f4274299b1..50cfa263f6 100644 --- a/extra/concurrency/distributed/tags.txt +++ b/extra/concurrency/distributed/tags.txt @@ -1 +1,2 @@ +enterprise extensions diff --git a/extra/cpu/8080/emulator/tags.txt b/extra/cpu/8080/emulator/tags.txt index 86069f7680..ff94650b8e 100644 --- a/extra/cpu/8080/emulator/tags.txt +++ b/extra/cpu/8080/emulator/tags.txt @@ -1 +1 @@ -emulator +emulators diff --git a/extra/cpu/8080/tags.txt b/extra/cpu/8080/tags.txt index 86069f7680..ff94650b8e 100644 --- a/extra/cpu/8080/tags.txt +++ b/extra/cpu/8080/tags.txt @@ -1 +1 @@ -emulator +emulators diff --git a/extra/cryptlib/tags.txt b/extra/cryptlib/tags.txt index bb863cf9a0..b88f9848cd 100644 --- a/extra/cryptlib/tags.txt +++ b/extra/cryptlib/tags.txt @@ -1 +1,2 @@ +enterprise bindings diff --git a/extra/http/server/tags.txt b/extra/http/server/tags.txt index ebb39bcce3..b0881a9ec0 100644 --- a/extra/http/server/tags.txt +++ b/extra/http/server/tags.txt @@ -1,2 +1,3 @@ +enterprise network web diff --git a/extra/ldap/tags.txt b/extra/ldap/tags.txt index 992ae12982..80d57bb287 100644 --- a/extra/ldap/tags.txt +++ b/extra/ldap/tags.txt @@ -1 +1,2 @@ +enterprise network diff --git a/extra/openssl/tags.txt b/extra/openssl/tags.txt index 59ccdd65e6..93e252c19e 100644 --- a/extra/openssl/tags.txt +++ b/extra/openssl/tags.txt @@ -1,2 +1,3 @@ +enterprise network bindings diff --git a/extra/smtp/tags.txt b/extra/smtp/tags.txt index 992ae12982..80d57bb287 100644 --- a/extra/smtp/tags.txt +++ b/extra/smtp/tags.txt @@ -1 +1,2 @@ +enterprise network diff --git a/extra/xml-rpc/tags.txt b/extra/xml-rpc/tags.txt index c0772185a0..7698983a7f 100644 --- a/extra/xml-rpc/tags.txt +++ b/extra/xml-rpc/tags.txt @@ -1 +1,2 @@ +enterprise web diff --git a/extra/xml/tags.txt b/extra/xml/tags.txt index c0772185a0..7698983a7f 100644 --- a/extra/xml/tags.txt +++ b/extra/xml/tags.txt @@ -1 +1,2 @@ +enterprise web From fdac73a4d74a05306293fddebcd39142313b3887 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 02:15:29 -0600 Subject: [PATCH 195/269] Oops --- extra/concurrency/concurrency.factor | 33 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/extra/concurrency/concurrency.factor b/extra/concurrency/concurrency.factor index e4972c9030..b46439b583 100755 --- a/extra/concurrency/concurrency.factor +++ b/extra/concurrency/concurrency.factor @@ -264,12 +264,7 @@ PRIVATE> #! so the server continuation gets its new self updated. self swap call ; -TUPLE: future value processes ; - -: notify-future ( value future -- ) - tuck set-future-value - dup future-processes [ schedule-thread ] each - f swap set-future-processes ; +TUPLE: future status value processes ; : future ( quot -- future ) #! Spawn a process to call the quotation and immediately return @@ -277,22 +272,28 @@ TUPLE: future value processes ; #! ?future. If the quotation has completed the result will be returned. #! If not, the process will block until the quotation completes. #! 'quot' must have stack effect ( -- X ). - \ future construct-empty [ + [ [ - >r [ t 2array ] compose [ f 2array ] recover r> - notify-future - ] 2curry spawn drop - ] keep ; + t + ] compose + ] spawn drop + [ self send ] compose spawn ; : ?future ( future -- result ) #! Block the process until the future has completed and then #! place the result on the stack. Return the result #! immediately if the future has completed. - dup future-value [ - first2 [ throw ] unless - ] [ - dup [ future-processes push stop ] curry callcc0 ?future - ] ?if ; + process-mailbox mailbox-get ; + +: parallel-map ( seq quot -- newseq ) + #! Spawn a process to apply quot to each element of seq, + #! joining the results into a sequence at the end. + [ curry future ] curry map [ ?future ] map ; + +: parallel-each ( seq quot -- ) + #! Spawn a process to apply quot to each element of seq, + #! and waits for all processes to complete. + [ f ] compose parallel-map drop ; TUPLE: promise fulfilled? value processes ; From 122be5b48ec22a69dd1afd0d2f441aacb9e4ed97 Mon Sep 17 00:00:00 2001 From: Matthew Willis Date: Sat, 9 Feb 2008 00:17:24 -0800 Subject: [PATCH 196/269] Added set-fullscreen? and fullscreen? hooks along with their cocoa implementations. --- extra/cocoa/cocoa.factor | 1 + extra/ui/backend/backend.factor | 4 ++++ extra/ui/cocoa/cocoa.factor | 14 +++++++++++++- extra/ui/gadgets/worlds/worlds-docs.factor | 9 +++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/extra/cocoa/cocoa.factor b/extra/cocoa/cocoa.factor index cbc6c9d762..c94984f00b 100755 --- a/extra/cocoa/cocoa.factor +++ b/extra/cocoa/cocoa.factor @@ -58,6 +58,7 @@ SYMBOL: super-sent-messages "NSPasteboard" "NSResponder" "NSSavePanel" + "NSScreen" "NSView" "NSWindow" "NSWorkspace" diff --git a/extra/ui/backend/backend.factor b/extra/ui/backend/backend.factor index a0646f35b0..cc1f5f7d05 100755 --- a/extra/ui/backend/backend.factor +++ b/extra/ui/backend/backend.factor @@ -7,6 +7,10 @@ SYMBOL: ui-backend HOOK: set-title ui-backend ( string world -- ) +HOOK: set-fullscreen? ui-backend ( ? world -- ) + +HOOK: fullscreen? ui-backend ( world -- ? ) + HOOK: (open-window) ui-backend ( world -- ) HOOK: (close-window) ui-backend ( handle -- ) diff --git a/extra/ui/cocoa/cocoa.factor b/extra/ui/cocoa/cocoa.factor index 1e46544180..184e6fd856 100755 --- a/extra/ui/cocoa/cocoa.factor +++ b/extra/ui/cocoa/cocoa.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2006, 2007 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: arrays cocoa cocoa.application command-line +USING: math arrays cocoa cocoa.application command-line kernel memory namespaces cocoa.messages cocoa.runtime cocoa.subclassing cocoa.pasteboard cocoa.types cocoa.windows cocoa.classes cocoa.application sequences system ui ui.backend @@ -53,6 +53,18 @@ M: pasteboard set-clipboard-contents M: cocoa-ui-backend set-title ( string world -- ) world-handle second swap -> setTitle: ; +: enter-fullscreen ( world -- ) + world-handle first NSScreen -> mainScreen f -> enterFullScreenMode:withOptions: drop ; + +: exit-fullscreen ( world -- ) + world-handle first f -> exitFullScreenModeWithOptions: ; + +M: cocoa-ui-backend set-fullscreen? ( ? world -- ) + swap [ enter-fullscreen ] [ exit-fullscreen ] if ; + +M: cocoa-ui-backend fullscreen? ( world -- ? ) + world-handle first -> isInFullScreenMode zero? not ; + : auto-position ( world -- ) dup world-loc { 0 0 } = [ world-handle second -> center diff --git a/extra/ui/gadgets/worlds/worlds-docs.factor b/extra/ui/gadgets/worlds/worlds-docs.factor index a47717329d..8a64750751 100755 --- a/extra/ui/gadgets/worlds/worlds-docs.factor +++ b/extra/ui/gadgets/worlds/worlds-docs.factor @@ -13,6 +13,15 @@ HELP: set-title { $description "Sets the title bar of the native window containing the world." } { $notes "This word should not be called directly by user code. Instead, change the " { $link world-title } " model; see " { $link "models" } "." } ; +HELP: set-fullscreen? +{ $values { "?" "a boolean" } { "world" world } } +{ $description "Sets and unsets fullscreen mode for the world." } +{ $notes "Find a world using " { $link find-world } "." } ; + +HELP: fullscreen? +{ $values { "world" world } { "?" "a boolean" } } +{ $description "Queries the world to see if it is running in fullscreen mode." } ; + HELP: raise-window { $values { "world" world } } { $description "Makes the native window containing the given world the front-most window." } From 7fbbe94d80c473c94f5b11f558cda2f5977d78d2 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 02:19:26 -0600 Subject: [PATCH 197/269] FEP work in progress --- vm/debug.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/vm/debug.c b/vm/debug.c index 5b4320b5e9..01e1ab0f43 100755 --- a/vm/debug.c +++ b/vm/debug.c @@ -38,6 +38,9 @@ void print_array(F_ARRAY* array, CELL nesting) CELL length = array_capacity(array); CELL i; + if(length > 10) + length = 10; + for(i = 0; i < length; i++) { printf(" "); @@ -201,7 +204,7 @@ void dump_objects(F_FIXNUM type) if(type == -1 || type_of(obj) == type) { printf("%lx ",obj); - print_nested_obj(obj,3); + print_nested_obj(obj,1); printf("\n"); } } @@ -210,6 +213,36 @@ void dump_objects(F_FIXNUM type) gc_off = false; } +CELL obj; +CELL look_for; + +void find_references_step(CELL *scan) +{ + if(look_for == *scan) + { + printf("%lx ",obj); + print_nested_obj(obj,1); + printf("\n"); + } +} + +void find_references(CELL look_for_) +{ + look_for = look_for_; + + begin_scan(); + + CELL obj_; + while((obj_ = next_object()) != F) + { + obj = obj_; + do_slots(obj_,find_references_step); + } + + /* end scan */ + gc_off = false; +} + void factorbug(void) { reset_stdio(); From e9a63d7a2c2d080e778a3f3e8bd4b99d2867588f Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 14:10:52 -0600 Subject: [PATCH 198/269] Arrggh --- extra/concurrency/concurrency.factor | 34 ++++++++++++++++++---------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/extra/concurrency/concurrency.factor b/extra/concurrency/concurrency.factor index b46439b583..3c8011cc6b 100755 --- a/extra/concurrency/concurrency.factor +++ b/extra/concurrency/concurrency.factor @@ -264,26 +264,36 @@ PRIVATE> #! so the server continuation gets its new self updated. self swap call ; -TUPLE: future status value processes ; +TUPLE: future value processes ; +: notify-future ( value future -- ) + tuck set-future-value + dup future-processes [ schedule-thread ] each + f swap set-future-processes ; + : future ( quot -- future ) - #! Spawn a process to call the quotation and immediately return - #! a 'future' on the stack. The future can later be queried with - #! ?future. If the quotation has completed the result will be returned. - #! If not, the process will block until the quotation completes. - #! 'quot' must have stack effect ( -- X ). + #! Spawn a process to call the quotation and immediately return. + \ future construct-empty [ [ [ + >r [ t 2array ] compose [ f 2array ] recover r> + notify-future + ] 2curry spawn drop + ] keep ; t ] compose ] spawn drop [ self send ] compose spawn ; - -: ?future ( future -- result ) - #! Block the process until the future has completed and then - #! place the result on the stack. Return the result - #! immediately if the future has completed. - process-mailbox mailbox-get ; + + : ?future ( future -- result ) + #! Block the process until the future has completed and then + #! place the result on the stack. Return the result + #! immediately if the future has completed. + dup future-value [ + first2 [ throw ] unless + ] [ + dup [ future-processes push stop ] curry callcc0 ?future + ] ?if ; : parallel-map ( seq quot -- newseq ) #! Spawn a process to apply quot to each element of seq, From 3121e740f2838d6d29ef0e1291fd8da670bb2416 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 14:12:14 -0600 Subject: [PATCH 199/269] Fix typo --- core/continuations/continuations-docs.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/continuations/continuations-docs.factor b/core/continuations/continuations-docs.factor index 2977d02c6f..7cf15394ef 100755 --- a/core/continuations/continuations-docs.factor +++ b/core/continuations/continuations-docs.factor @@ -169,7 +169,7 @@ HELP: rethrow HELP: throw-restarts { $values { "error" object } { "restarts" "a sequence of " { $snippet "{ string object }" } " pairs" } { "restart" object } } -{ $description "Throws a restartable error using " { $link throw } ". The " { $snippet "restarts" } " parameter is a sequence of pairs where the first element in each pair is a human-readable description and the second is an arbitrary object. If the error reaches the top-level error handler, the user will be presented with the list of possible restarts, and upon invoking one, execution will continue after the call to " { $link condition } " with the object associated to the chosen restart on the stack." } +{ $description "Throws a restartable error using " { $link throw } ". The " { $snippet "restarts" } " parameter is a sequence of pairs where the first element in each pair is a human-readable description and the second is an arbitrary object. If the error reaches the top-level error handler, the user will be presented with the list of possible restarts, and upon invoking one, execution will continue after the call to " { $link throw-restarts } " with the object associated to the chosen restart on the stack." } { $examples "Try invoking one of the two restarts which are offered after the below code throws an error:" { $code From 25c64c8ac713cc94bf706124900f3658e3e34167 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 14:13:06 -0600 Subject: [PATCH 200/269] Arrghh!!! --- extra/concurrency/concurrency.factor | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/extra/concurrency/concurrency.factor b/extra/concurrency/concurrency.factor index 3c8011cc6b..50abee8418 100755 --- a/extra/concurrency/concurrency.factor +++ b/extra/concurrency/concurrency.factor @@ -280,15 +280,11 @@ TUPLE: future value processes ; notify-future ] 2curry spawn drop ] keep ; - t - ] compose - ] spawn drop - [ self send ] compose spawn ; : ?future ( future -- result ) - #! Block the process until the future has completed and then - #! place the result on the stack. Return the result - #! immediately if the future has completed. + #! Block the process until the future has completed and then + #! place the result on the stack. Return the result + #! immediately if the future has completed. dup future-value [ first2 [ throw ] unless ] [ From a21781e3807d1c89cba88989cb694e65d81d0ee3 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 14:14:37 -0600 Subject: [PATCH 201/269] Concurrency fix --- extra/concurrency/concurrency.factor | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/extra/concurrency/concurrency.factor b/extra/concurrency/concurrency.factor index 50abee8418..a8e0bc6eeb 100755 --- a/extra/concurrency/concurrency.factor +++ b/extra/concurrency/concurrency.factor @@ -270,11 +270,10 @@ TUPLE: future value processes ; tuck set-future-value dup future-processes [ schedule-thread ] each f swap set-future-processes ; - + : future ( quot -- future ) #! Spawn a process to call the quotation and immediately return. \ future construct-empty [ - [ [ >r [ t 2array ] compose [ f 2array ] recover r> notify-future From 5ca99b0105c82b881ccb023fee8b502e5a2651ba Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 14:17:15 -0600 Subject: [PATCH 202/269] Fix 'class' in early bootstrap --- core/classes/classes.factor | 4 +++- core/generic/math/math.factor | 2 +- core/generic/standard/standard.factor | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/core/classes/classes.factor b/core/classes/classes.factor index 151429bf69..345676e106 100755 --- a/core/classes/classes.factor +++ b/core/classes/classes.factor @@ -20,7 +20,9 @@ PREDICATE: class tuple-class : classes ( -- seq ) classclass ( n -- class ) builtins get nth ; +: type>class ( n -- class ) builtins get-global nth ; + +: bootstrap-type>class ( n -- class ) builtins get nth ; : predicate-word ( word -- predicate ) [ word-name "?" append ] keep word-vocabulary create ; diff --git a/core/generic/math/math.factor b/core/generic/math/math.factor index 8cf83b0ba7..21a7857646 100755 --- a/core/generic/math/math.factor +++ b/core/generic/math/math.factor @@ -61,7 +61,7 @@ TUPLE: no-math-method left right generic ; : math-vtable* ( picker max quot -- quot ) [ rot , \ tag , - [ >r [ type>class ] map r> map % ] { } make , + [ >r [ bootstrap-type>class ] map r> map % ] { } make , \ dispatch , ] [ ] make ; inline diff --git a/core/generic/standard/standard.factor b/core/generic/standard/standard.factor index 88f6a05bc2..7f4f423d8b 100755 --- a/core/generic/standard/standard.factor +++ b/core/generic/standard/standard.factor @@ -97,7 +97,7 @@ TUPLE: no-method object generic ; [ small-generic ] picker class-hash-dispatch-quot ; : vtable-class ( n -- class ) - type>class [ hi-tag bootstrap-word ] unless* ; + bootstrap-type>class [ hi-tag bootstrap-word ] unless* ; : group-methods ( assoc -- vtable ) #! Input is a predicate -> method association. From ee912c5996e9342d921c51051cd71001d94b2048 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 14:17:40 -0600 Subject: [PATCH 203/269] Walker cleanup --- extra/ui/tools/walker/walker.factor | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/extra/ui/tools/walker/walker.factor b/extra/ui/tools/walker/walker.factor index 4740ff86d4..a23345d214 100755 --- a/extra/ui/tools/walker/walker.factor +++ b/extra/ui/tools/walker/walker.factor @@ -21,21 +21,21 @@ TUPLE: walker model interpreter history ; : walker-active? ( walker -- ? ) walker-interpreter interpreter-continuation >boolean ; -: walker-command ( gadget quot -- ) - over walker-active? [ with-walker ] [ 2drop ] if ; inline - : save-interpreter ( walker -- ) dup walker-interpreter interpreter-continuation clone swap walker-history push ; -: com-step ( walker -- ) - dup save-interpreter [ step ] walker-command ; +: walker-command ( gadget quot -- ) + over walker-active? [ + over save-interpreter + with-walker + ] [ 2drop ] if ; inline -: com-into ( walker -- ) - dup save-interpreter [ step-into ] walker-command ; +: com-step ( walker -- ) [ step ] walker-command ; -: com-out ( walker -- ) - dup save-interpreter [ step-out ] walker-command ; +: com-into ( walker -- ) [ step-into ] walker-command ; + +: com-out ( walker -- ) [ step-out ] walker-command ; : com-back ( walker -- ) dup walker-history From ef63333980d03f963bb50b076ec52c10923cbcff Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 18:12:07 -0600 Subject: [PATCH 204/269] Fix another bug with futures --- extra/concurrency/concurrency-tests.factor | 5 +++++ extra/concurrency/concurrency.factor | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/extra/concurrency/concurrency-tests.factor b/extra/concurrency/concurrency-tests.factor index 1a19ce7096..8908506d51 100755 --- a/extra/concurrency/concurrency-tests.factor +++ b/extra/concurrency/concurrency-tests.factor @@ -133,4 +133,9 @@ SYMBOL: value [ 3 3 ] [ [ 3 ] future dup ?future swap ?future +] unit-test + +! Another race +[ 3 ] [ + [ 3 yield ] future ?future ] unit-test \ No newline at end of file diff --git a/extra/concurrency/concurrency.factor b/extra/concurrency/concurrency.factor index a8e0bc6eeb..1c5f6322a8 100755 --- a/extra/concurrency/concurrency.factor +++ b/extra/concurrency/concurrency.factor @@ -273,14 +273,14 @@ TUPLE: future value processes ; : future ( quot -- future ) #! Spawn a process to call the quotation and immediately return. - \ future construct-empty [ + f V{ } clone \ future construct-boa [ [ >r [ t 2array ] compose [ f 2array ] recover r> notify-future ] 2curry spawn drop ] keep ; - - : ?future ( future -- result ) + +: ?future ( future -- result ) #! Block the process until the future has completed and then #! place the result on the stack. Return the result #! immediately if the future has completed. From f655a25762173982ee894d61f7ca755524127aa1 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 21:08:47 -0600 Subject: [PATCH 205/269] Fixing compiler test --- core/bootstrap/compiler/compiler.factor | 11 +++++++++++ core/compiler/test/simple/simple-tests.factor | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/core/bootstrap/compiler/compiler.factor b/core/bootstrap/compiler/compiler.factor index ff9d5c5e1e..2b278ac458 100755 --- a/core/bootstrap/compiler/compiler.factor +++ b/core/bootstrap/compiler/compiler.factor @@ -77,3 +77,14 @@ nl [ compiled-usages recompile ] recompile-hook set-global " done" print flush + +! Load empty test vocabs +USE: compiler.test.curry +USE: compiler.test.float +USE: compiler.test.intrinsics +USE: compiler.test.redefine +USE: compiler.test.simple +USE: compiler.test.stack-trace +USE: compiler.test.templates +USE: compiler.test.templates-early +USE: compiler.test.tuples diff --git a/core/compiler/test/simple/simple-tests.factor b/core/compiler/test/simple/simple-tests.factor index 3f4f6451a3..743fb713d9 100755 --- a/core/compiler/test/simple/simple-tests.factor +++ b/core/compiler/test/simple/simple-tests.factor @@ -1,6 +1,6 @@ USING: compiler tools.test kernel kernel.private combinators.private math.private math combinators strings -alien arrays ; +alien arrays memory ; IN: temporary ! Test empty word @@ -48,6 +48,8 @@ IN: temporary [ 4 1 3 ] [ 0 [ { [ bar 1 ] [ 3 1 ] } dispatch 3 ] compile-call ] unit-test [ 3 1 3 ] [ 1 [ { [ bar 1 ] [ 3 1 ] } dispatch 3 ] compile-call ] unit-test +[ 2 3 ] [ 1 [ { [ code-gc 1 ] [ code-gc 2 ] } dispatch 3 ] compile-call ] unit-test + ! Labels : recursive ( ? -- ) [ f recursive ] when ; inline From 93e10566bef56950add23087e64af1e3da3f2575 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 9 Feb 2008 21:12:00 -0600 Subject: [PATCH 206/269] Simpler compilation of dispatch --- core/cpu/architecture/architecture.factor | 4 +- core/cpu/ppc/architecture/architecture.factor | 23 +++++------ core/cpu/x86/architecture/architecture.factor | 39 ++++++++++--------- core/generator/generator.factor | 29 +++++++++----- 4 files changed, 50 insertions(+), 45 deletions(-) diff --git a/core/cpu/architecture/architecture.factor b/core/cpu/architecture/architecture.factor index 4da22ff38a..4bb10b23a2 100755 --- a/core/cpu/architecture/architecture.factor +++ b/core/cpu/architecture/architecture.factor @@ -60,9 +60,7 @@ HOOK: %jump-label compiler-backend ( label -- ) ! Test if vreg is 'f' or not HOOK: %jump-t compiler-backend ( label -- ) -HOOK: %call-dispatch compiler-backend ( -- label ) - -HOOK: %jump-dispatch compiler-backend ( -- ) +HOOK: %dispatch compiler-backend ( -- ) HOOK: %dispatch-label compiler-backend ( word -- ) diff --git a/core/cpu/ppc/architecture/architecture.factor b/core/cpu/ppc/architecture/architecture.factor index 7444c21a8c..1daf3ac622 100755 --- a/core/cpu/ppc/architecture/architecture.factor +++ b/core/cpu/ppc/architecture/architecture.factor @@ -111,20 +111,15 @@ M: ppc-backend %jump-label ( label -- ) B ; M: ppc-backend %jump-t ( label -- ) 0 "flag" operand f v>operand CMPI BNE ; -: (%dispatch) ( len -- ) - 0 11 LOAD32 rc-absolute-ppc-2/2 rel-here - "offset" operand "n" operand 1 SRAWI - 11 11 "offset" operand ADD - 11 dup rot cells LWZ ; - -M: ppc-backend %call-dispatch ( word-table# -- ) - [ 7 (%dispatch) (%call)