Merge branch 'master' of git://factorcode.org/git/factor
commit
c149c26a7e
|
@ -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
|
||||
|
|
|
@ -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 ;
|
||||
: digits>integer ( seq radix -- n )
|
||||
0 swap [ swapd * + ] curry reduce ;
|
||||
|
||||
DEFER: base>
|
||||
|
||||
<PRIVATE
|
||||
|
||||
SYMBOL: radix
|
||||
|
||||
: with-radix ( radix quot -- )
|
||||
radix swap with-variable ; inline
|
||||
|
||||
: (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 ;
|
||||
|
||||
: 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?
|
||||
[ radix get 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 )
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: (>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 )
|
||||
{
|
||||
|
|
|
@ -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" } ;
|
||||
|
||||
|
|
|
@ -8,6 +8,15 @@ IN: builder
|
|||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: runtime ( quot -- time ) benchmark nip ;
|
||||
|
||||
: log-runtime ( quot file -- )
|
||||
>r runtime r> <file-writer> [ . ] with-stream ;
|
||||
|
||||
: log-object ( object file -- ) <file-writer> [ . ] with-stream ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: datestamp ( -- string )
|
||||
now `{ ,[ dup timestamp-year ]
|
||||
,[ dup timestamp-month ]
|
||||
|
@ -40,7 +49,7 @@ SYMBOL: builder-recipients
|
|||
: factor-binary ( -- name )
|
||||
os
|
||||
{ { "macosx" [ "./Factor.app/Contents/MacOS/factor" ] }
|
||||
{ "windows" [ "./factor-nt.exe" ] }
|
||||
{ "winnt" [ "./factor-nt.exe" ] }
|
||||
[ drop "./factor" ] }
|
||||
case ;
|
||||
|
||||
|
@ -56,7 +65,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 =
|
||||
[ ]
|
||||
|
@ -69,14 +84,12 @@ 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
|
||||
|
||||
{ "git" "show" } <process-stream>
|
||||
[ readln ] with-stream
|
||||
" " split second
|
||||
"../git-id" <file-writer> [ print ] with-stream
|
||||
{ "git" "show" } <process-stream> [ readln ] with-stream " " split second
|
||||
"../git-id" log-object
|
||||
|
||||
{ "make" "clean" } run-process drop
|
||||
|
||||
|
@ -106,9 +119,7 @@ VAR: stamp
|
|||
{ +stdout+ "../boot-log" }
|
||||
{ +stderr+ +stdout+ }
|
||||
}
|
||||
>hashtable
|
||||
[ run-process process-status ]
|
||||
benchmark nip "../boot-time" <file-writer> [ . ] with-stream
|
||||
>hashtable [ run-process ] "../boot-time" log-runtime process-status
|
||||
0 =
|
||||
[ ]
|
||||
[
|
||||
|
@ -116,20 +127,17 @@ 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" <file-writer> [ . ] with-stream
|
||||
0 =
|
||||
[ ]
|
||||
[
|
||||
"builder: load-everything" "../load-everything-log" email-file
|
||||
"builder: load-everything" throw
|
||||
] if ;
|
||||
`{ ,[ 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
|
||||
|
||||
;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
|
||||
USING: kernel sequences assocs builder continuations vocabs vocabs.loader
|
||||
io
|
||||
io.files
|
||||
tools.browser
|
||||
tools.test ;
|
||||
|
||||
IN: builder.test
|
||||
|
||||
: do-load ( -- )
|
||||
[
|
||||
[ load-everything ]
|
||||
[ require-all-error-vocabs "../load-everything-log" log-object ]
|
||||
recover
|
||||
]
|
||||
"../load-everything-time" log-runtime ;
|
||||
|
||||
: 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" <file-writer>
|
||||
[ [ nl failures. ] assoc-each ]
|
||||
with-stream
|
||||
]
|
||||
if ;
|
||||
|
||||
: do-all ( -- ) do-load do-tests ;
|
||||
|
||||
MAIN: do-all
|
|
@ -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'
|
||||
|
|
|
@ -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."
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 ;
|
||||
|
|
|
@ -33,7 +33,7 @@ SYMBOL: and-needed?
|
|||
|
||||
: 3digit-groups ( n -- seq )
|
||||
number>string <reversed> 3 <groups>
|
||||
[ reverse 10 string>integer ] map ;
|
||||
[ reverse string>number ] map ;
|
||||
|
||||
: hundreds-place ( n -- str )
|
||||
100 /mod swap dup zero? [
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 )
|
||||
[
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -27,21 +27,21 @@ IN: project-euler.032
|
|||
<PRIVATE
|
||||
|
||||
: source-032 ( -- seq )
|
||||
9 factorial [ 9 permutation [ 1+ ] map 10 swap digits>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>
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 > [
|
||||
|
|
|
@ -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 ;
|
||||
|
|
|
@ -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>
|
||||
|
||||
|
|
|
@ -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 [ <vector> 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 [ <vector> assoc? ] compile-1
|
||||
2^ not
|
||||
! arrays
|
||||
resize-array <array>
|
||||
|
|
|
@ -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 <pathname> .
|
||||
[ 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 <pathname> .
|
||||
[ 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. ;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue