From 9c2166b0be407e8ab494cf02e29faa46aed8a91d Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 12 Oct 2004 05:11:35 +0000 Subject: [PATCH] possibly controversial: removed destructive list manipulation; other cleanups --- library/compiler/compile-all.factor | 6 +- library/cross-compiler.factor | 2 - library/httpd/html-tags.factor | 2 +- library/interpreter.factor | 2 +- library/list-namespaces.factor | 17 +-- library/lists.factor | 129 ++---------------- library/namespaces.factor | 19 --- library/platform/jvm/cons.factor | 11 -- library/platform/jvm/namespaces.factor | 6 + library/platform/native/boot-stage2.factor | 9 +- library/platform/native/errors.factor | 4 +- library/platform/native/init-stage2.factor | 9 +- library/platform/native/kernel.factor | 9 -- library/platform/native/namespaces.factor | 3 + library/platform/native/parse-stream.factor | 2 +- library/platform/native/parse-syntax.factor | 6 +- library/platform/native/parser.factor | 6 +- library/platform/native/primitives.factor | 2 - library/platform/native/strings.factor | 6 + library/prettyprint.factor | 2 +- library/styles.factor | 31 +---- .../test/jvm-compiler/miscellaneous.factor | 4 - library/test/jvm-compiler/primitives.factor | 3 - library/test/lists/cons.factor | 1 - library/test/lists/destructive.factor | 34 ----- library/test/lists/java.factor | 7 - library/test/lists/lists.factor | 24 ---- library/test/lists/namespaces.factor | 6 - library/test/test.factor | 1 - library/vocabulary-style.factor | 15 +- native/cons.c | 14 -- native/cons.h | 2 - native/primitives.c | 2 - native/primitives.h | 2 +- 34 files changed, 64 insertions(+), 334 deletions(-) delete mode 100644 library/test/lists/destructive.factor diff --git a/library/compiler/compile-all.factor b/library/compiler/compile-all.factor index 68b83c50d0..5cd79b1ba3 100644 --- a/library/compiler/compile-all.factor +++ b/library/compiler/compile-all.factor @@ -110,7 +110,11 @@ SYMBOL: compilable-word-list [, [ dup can-compile? [ , ] [ drop ] ifte ] each-word ,] ; : cannot-compile ( word -- ) - "verbose-compile" get [ "Cannot compile " write . ] when ; + "verbose-compile" get [ + "Cannot compile " write . + ] [ + drop + ] ifte ; : init-compiler ( -- ) #! Compile all words. diff --git a/library/cross-compiler.factor b/library/cross-compiler.factor index 02985a4521..dcf2e14d2b 100644 --- a/library/cross-compiler.factor +++ b/library/cross-compiler.factor @@ -198,8 +198,6 @@ IN: image cons car cdr - set-car - set-cdr vector-length set-vector-length diff --git a/library/httpd/html-tags.factor b/library/httpd/html-tags.factor index 6d668d2f8e..f9f28e2ebd 100644 --- a/library/httpd/html-tags.factor +++ b/library/httpd/html-tags.factor @@ -78,7 +78,7 @@ USE: logic : attrs>string ( alist -- string ) #! Convert the attrs alist to a string #! suitable for embedding in an html tag. - nreverse <% [ dup car % "='" % cdr % "'" % ] each %> ; + reverse <% [ dup car % "='" % cdr % "'" % ] each %> ; : write-attributes ( n: namespace -- ) #! With the attribute namespace on the stack, get the attributes diff --git a/library/interpreter.factor b/library/interpreter.factor index 6980053c9a..567b36f707 100644 --- a/library/interpreter.factor +++ b/library/interpreter.factor @@ -81,7 +81,7 @@ USE: vectors : print-prompt ( -- ) <% " ( " % history# unparse % " )" % %> - [ "prompt" ] get-style write-attr + "prompt" get-style write-attr ! Print the space without a style, to workaround a bug in ! the GUI listener where the style from the prompt carries ! over to the input diff --git a/library/list-namespaces.factor b/library/list-namespaces.factor index 78bb33c2d4..c8b1bd371a 100644 --- a/library/list-namespaces.factor +++ b/library/list-namespaces.factor @@ -31,17 +31,6 @@ USE: kernel USE: namespaces USE: stack -: append@ ( [ list ] var -- ) - #! Append a proper list stored in a variable with another - #! list, storing the result back in the variable. - #! given variable using 'append'. - tuck get swap append put ; - -: add@ ( elem var -- ) - #! Add an element at the end of a proper list stored in a - #! variable, storing the result back in the variable. - tuck get swap add put ; - : cons@ ( x var -- ) #! Prepend x to the list stored in var. tuck get cons put ; @@ -78,10 +67,6 @@ USE: stack #! if the object does not already occur in the list. "list-buffer" unique@ ; -: list, ( list -- ) - #! Append each element to the currently constructing list. - [ , ] each ; - : ,] ( -- list ) #! Finish constructing a list and push it on the stack. - "list-buffer" get nreverse n> drop ; + "list-buffer" get reverse n> drop ; diff --git a/library/lists.factor b/library/lists.factor index 9f7dd26500..5db76e1e48 100644 --- a/library/lists.factor +++ b/library/lists.factor @@ -41,45 +41,9 @@ USE: vectors #! Construct a proper list of 3 elements. 2list cons ; -: 2rlist ( a b -- [ b a ] ) - #! Construct a proper list of 2 elements in reverse stack order. - swap unit cons ; - -: copy-cons ( accum cons -- accum cdr ) - uncons >r unit dup rot set-cdr r> ; - -: (clone-list) ( accum list -- last ) - dup cons? [ copy-cons (clone-list) ] [ over set-cdr ] ifte ; - -: clone-list* ( list -- list last ) - #! Push the cloned list, and the last cons cell of the - #! cloned list. - uncons >r unit dup r> (clone-list) ; - -: clone-list ( list -- list ) - #! Push a shallow copy of a list. - dup [ clone-list* drop ] when ; - : append ( [ list1 ] [ list2 ] -- [ list1 list2 ] ) - #! Append two lists. A new list is constructed by copying - #! the first list and setting its tail to the second. - over [ >r clone-list* r> swap set-cdr ] [ nip ] ifte ; - -: add ( [ list1 ] elem -- [ list1 elem ] ) - #! Push a new proper list with an element added to the end. - unit append ; - -: caar ( list -- caar ) - car car ; inline - -: cdar ( list -- cadr ) - cdr car ; inline - -: cadr ( list -- cdar ) - car cdr ; inline - -: cddr ( list -- cddr ) - cdr cdr ; inline + #! Append two lists. + over [ >r uncons r> append cons ] [ nip ] ifte ; : contains? ( element list -- remainder ) #! If the proper list contains the element, push the @@ -115,48 +79,7 @@ USE: vectors : list? ( list -- boolean ) #! Proper list test. A proper list is either f, or a cons #! cell whose cdr is a proper list. - dup [ - dup cons? [ - cdr list? - ] [ - drop f - ] ifte - ] [ - drop t - ] ifte ; - -: nappend ( [ list1 ] [ list2 ] -- [ list1 list2 ] ) - #! DESTRUCTIVE. Append two lists. The last node of the first - #! list is destructively modified to point to the second - #! list, unless the first list is f, in which case the - #! second list is returned. - over [ over last* set-cdr ] [ nip ] ifte ; - -: first ( list -- obj ) - #! Push the head of the list, or f if the list is empty. - dup [ car ] when ; - -: next ( obj list -- obj ) - #! Push the next object in the list after an object. Wraps - #! around to beginning of list if object is at the end. - tuck contains? dup [ - ! Is there another entry in the list? - cdr dup [ - nip car - ] [ - ! No. Pick first - drop first - ] ifte - ] [ - drop first - ] ifte ; - -: nreverse-iter ( list cons -- list cons ) - [ dup dup cdr 2swap set-cdr nreverse-iter ] when* ; - -: nreverse ( list -- list ) - #! DESTRUCTIVE. Reverse the given list, without consing. - f swap nreverse-iter ; + [ dup cons? [ cdr list? ] [ drop f ] ifte ] [ t ] ifte* ; : partition-add ( obj ? ret1 ret2 -- ret1 ret2 ) >r >r [ r> cons r> ] [ r> r> swapd cons ] ifte ; inline @@ -196,7 +119,7 @@ USE: vectors ! Recurse tuck sort >r sort r> ! Combine - swapd cons nappend + swapd cons append ] [ drop ] ifte ; inline interpret-only @@ -209,11 +132,7 @@ USE: vectors DEFER: tree-contains? : =-or-contains? ( element obj -- ? ) - dup cons? [ - tree-contains? - ] [ - = - ] ifte ; + dup cons? [ tree-contains? ] [ = ] ifte ; : tree-contains? ( element tree -- ? ) dup [ @@ -254,7 +173,7 @@ DEFER: tree-contains? f transp [ ! accum code elem -- accum code transp over >r >r call r> cons r> - ] each drop nreverse ; inline interpret-only + ] each drop reverse ; inline interpret-only : 2uncons ( list1 list2 -- car1 car2 cdr1 cdr2 ) uncons >r >r uncons r> swap r> ; @@ -283,31 +202,9 @@ DEFER: tree-contains? #! two lists in turn, collecting the return value into a #! new list. The quotation must have stack effect #! ( x y -- z ). - <2map [ pick >r 2map-step r> ] 2each drop nreverse ; + <2map [ pick >r 2map-step r> ] 2each drop reverse ; inline interpret-only -: substitute ( new old list -- list ) - [ 2dup = [ drop over ] when ] map nip nip ; - -: (head) ( accum list n -- last list ) - dup 1 = [ drop ] [ pred >r copy-cons r> (head) ] ifte ; - -: head* ( n list -- head last rest ) - #! Push the head of the list, the last cons cell of the - #! head, and the rest of the list. - uncons >r unit tuck r> rot (head) ; - -: head ( n list -- head ) - #! Push a new list containing the first n elements. - over 0 = [ 2drop f ] [ head* 2drop ] ifte ; - -: set-nth ( value index list -- list ) - over 0 = [ - nip cdr cons - ] [ - rot >r head* cdr r> swons swap set-cdr - ] ifte ; - : subset-add ( car pred accum -- accum ) >r over >r call r> r> rot [ cons ] [ nip ] ifte ; @@ -326,24 +223,16 @@ DEFER: tree-contains? #! #! In order to compile, the quotation must consume as many #! values as it produces. - f -rot subset-iter nreverse ; inline interpret-only + f -rot subset-iter reverse ; inline interpret-only : remove ( obj list -- list ) #! Remove all occurrences of the object from the list. [ dupd = not ] subset nip ; -: remove-nth ( n list -- list ) - #! Push a new list with the nth element removed. - over 0 = [ nip cdr ] [ head* cdr swap set-cdr ] ifte ; - : length ( list -- length ) #! Pushes the length of the given proper list. 0 swap [ drop succ ] each ; -: leaves ( list -- length ) - #! Like length, but counts each sub-list recursively. - 0 swap [ dup list? [ leaves + ] [ drop succ ] ifte ] each ; - : reverse ( list -- list ) #! Push a new list that is the reverse of a proper list. [ ] swap [ swons ] each ; @@ -401,4 +290,4 @@ DEFER: tree-contains? [ ] swap [ swons ] vector-each ; : vector>list ( vector -- list ) - stack>list nreverse ; + stack>list reverse ; diff --git a/library/namespaces.factor b/library/namespaces.factor index 6d5c996c4b..23b3df9b45 100644 --- a/library/namespaces.factor +++ b/library/namespaces.factor @@ -53,17 +53,6 @@ USE: vectors ! bind ( namespace quot -- ) executes a quotation with a ! namespace pushed on the namespace stack. -: namestack ( -- stack ) - #! Push a copy of the namespace stack; same naming - #! convention as the primitives datastack and callstack. - namestack* clone ; inline - -: set-namestack ( stack -- ) - #! Set the namespace stack to a copy of another stack; same - #! naming convention as the primitives datastack and - #! callstack. - clone set-namestack* ; inline - : >n ( namespace -- n:namespace ) #! Push a namespace on the namespace stack. namestack* vector-push ; inline @@ -98,14 +87,6 @@ USE: vectors #! result of evaluating [ a ]. over get [ drop get ] [ swap >r call dup r> set ] ifte ; -: alist> ( alist namespace -- ) - #! Set each key in the alist to its value in the - #! namespace. - [ [ unswons set ] each ] bind ; - -: alist>namespace ( alist -- namespace ) - tuck alist> ; - : traverse-path ( name object -- object ) dup has-namespace? [ get* ] [ 2drop f ] ifte ; diff --git a/library/platform/jvm/cons.factor b/library/platform/jvm/cons.factor index 1ecdd083f8..aae8c9c15b 100644 --- a/library/platform/jvm/cons.factor +++ b/library/platform/jvm/cons.factor @@ -51,14 +51,3 @@ IN: lists USE: kernel USE: stack : cons? ( list -- boolean ) #! Test for cons cell type. "factor.Cons" is ; inline - -: deep-clone ( cons -- cons ) - [ "factor.Cons" ] "factor.Cons" "deepClone" jinvoke-static ; - -: set-car ( A [ B | C ] -- ) - #! DESTRUCTIVE. Replace the head of a list. - "factor.Cons" "car" jvar-set ; inline - -: set-cdr ( A [ B | C ] -- ) - #! DESTRUCTIVE. Replace the tail of a list. - "factor.Cons" "cdr" jvar-set ; inline diff --git a/library/platform/jvm/namespaces.factor b/library/platform/jvm/namespaces.factor index f393931264..eaa0c1b3f6 100644 --- a/library/platform/jvm/namespaces.factor +++ b/library/platform/jvm/namespaces.factor @@ -45,6 +45,12 @@ DEFER: namespace interpreter "factor.FactorInterpreter" "namestack" jvar-set ; inline +: namestack ( -- stack ) + namestack* clone ; inline + +: set-namestack ( stack -- ) + clone set-namestack* ; inline + : global ( -- namespace ) interpreter "factor.FactorInterpreter" "global" jvar-get ; diff --git a/library/platform/native/boot-stage2.factor b/library/platform/native/boot-stage2.factor index c6dd71fbe3..758ead07f9 100644 --- a/library/platform/native/boot-stage2.factor +++ b/library/platform/native/boot-stage2.factor @@ -179,7 +179,14 @@ IN: compiler DEFER: compilable-words DEFER: compilable-word-list -[ warm-boot ] set-boot +IN: init +DEFER: init-interpreter + +[ + warm-boot + "interactive" get [ init-interpreter ] when + 0 exit* +] set-boot compilable-words compilable-word-list set diff --git a/library/platform/native/errors.factor b/library/platform/native/errors.factor index 1531c5e0c7..1f1811984b 100644 --- a/library/platform/native/errors.factor +++ b/library/platform/native/errors.factor @@ -32,6 +32,6 @@ USE: vectors ! This is a very lightweight exception handling system. : catchstack* ( -- cs ) 6 getenv ; -: catchstack ( -- cs ) catchstack* clone ; +: catchstack ( -- cs ) catchstack* vector-clone ; : set-catchstack* ( cs -- ) 6 setenv ; -: set-catchstack ( cs -- ) clone set-catchstack* ; +: set-catchstack ( cs -- ) vector-clone set-catchstack* ; diff --git a/library/platform/native/init-stage2.factor b/library/platform/native/init-stage2.factor index b66579516b..cb9b882e7b 100644 --- a/library/platform/native/init-stage2.factor +++ b/library/platform/native/init-stage2.factor @@ -64,14 +64,11 @@ USE: words t "ansi" set t "compile" set + "ansi" get [ "stdio" get "stdio" set ] when + ! The first CLI arg is the image name. cli-args uncons parse-command-line "image" set "compile" get [ init-compiler ] when - run-user-init - - "ansi" get [ "stdio" get "stdio" set ] when - "interactive" get [ init-interpreter ] when - - 0 exit* ; + run-user-init ; diff --git a/library/platform/native/kernel.factor b/library/platform/native/kernel.factor index 9ccdb9645c..1667726cee 100644 --- a/library/platform/native/kernel.factor +++ b/library/platform/native/kernel.factor @@ -114,7 +114,6 @@ IN: kernel : clone ( obj -- obj ) [ - [ cons? ] [ clone-list ] [ vector? ] [ vector-clone ] [ sbuf? ] [ sbuf-clone ] [ drop t ] [ ( return the object ) ] @@ -130,11 +129,3 @@ IN: kernel ! No compiler... : inline ; : interpret-only ; - -! HACKS - -IN: strings -: char? drop f ; -: >char ; -: >upper ; -: >lower ; diff --git a/library/platform/native/namespaces.factor b/library/platform/native/namespaces.factor index 4cf2d89025..09d33b1592 100644 --- a/library/platform/native/namespaces.factor +++ b/library/platform/native/namespaces.factor @@ -41,6 +41,9 @@ DEFER: >n : namestack* ( -- ns ) 3 getenv ; : set-namestack* ( ns -- ) 3 setenv ; +: namestack ( -- stack ) namestack* vector-clone ; +: set-namestack ( stack -- ) vector-clone set-namestack* ; + : global ( -- g ) 4 getenv ; : set-global ( g -- ) 4 setenv ; diff --git a/library/platform/native/parse-stream.factor b/library/platform/native/parse-stream.factor index 63acdef74e..dd751286e1 100644 --- a/library/platform/native/parse-stream.factor +++ b/library/platform/native/parse-stream.factor @@ -69,7 +69,7 @@ USE: strings : (parse-stream) ( name stream -- quot ) #! Uses the current namespace for temporary variables. >r "file" set f r> - [ (parse) ] read-lines nreverse + [ (parse) ] read-lines reverse "file" off "line-number" off ; diff --git a/library/platform/native/parse-syntax.factor b/library/platform/native/parse-syntax.factor index 16bb1ac94d..dae895b62a 100644 --- a/library/platform/native/parse-syntax.factor +++ b/library/platform/native/parse-syntax.factor @@ -124,7 +124,7 @@ IN: syntax ! Lists : [ [ ] ; parsing -: ] nreverse parsed ; parsing +: ] reverse parsed ; parsing : | ( syntax: | cdr ] ) #! See the word 'parsed'. We push a special sentinel, and @@ -133,7 +133,7 @@ IN: syntax ! Vectors : { f ; parsing -: } nreverse list>vector parsed ; parsing +: } reverse list>vector parsed ; parsing ! Do not execute parsing word : POSTPONE: ( -- ) scan-word parsed ; parsing @@ -149,7 +149,7 @@ IN: syntax : ; #! End a word definition. "in-definition" off - nreverse + reverse ;-hook ; parsing ! Symbols diff --git a/library/platform/native/parser.factor b/library/platform/native/parser.factor index 01f07436e1..383faa1b51 100644 --- a/library/platform/native/parser.factor +++ b/library/platform/native/parser.factor @@ -135,9 +135,9 @@ USE: unparser ] ifte ] when ; -: parsed| ( obj -- ) +: parsed| ( parsed parsed obj -- parsed ) #! Some ugly ugly code to handle [ a | b ] expressions. - >r nreverse dup last* r> swap set-cdr swons ; + >r unswons r> cons swap [ swons ] each swons ; : expect ( word -- ) dup scan = not [ @@ -158,7 +158,7 @@ USE: unparser : parse ( str -- code ) #! Parse the string into a parse tree that can be executed. - f swap (parse) nreverse ; + f swap (parse) reverse ; : eval ( "X" -- X ) parse call ; diff --git a/library/platform/native/primitives.factor b/library/platform/native/primitives.factor index 56864b9b93..a554da6255 100644 --- a/library/platform/native/primitives.factor +++ b/library/platform/native/primitives.factor @@ -51,8 +51,6 @@ USE: words [ cons | " car cdr -- [ car | cdr ] " ] [ car | " [ car | cdr ] -- car " ] [ cdr | " [ car | cdr ] -- cdr " ] - [ set-car | " car cons -- " ] - [ set-cdr | " cdr cons -- " ] [ | " capacity -- vector" ] [ vector-length | " vector -- n " ] [ set-vector-length | " n vector -- " ] diff --git a/library/platform/native/strings.factor b/library/platform/native/strings.factor index 5e512e2557..681c6a44fb 100644 --- a/library/platform/native/strings.factor +++ b/library/platform/native/strings.factor @@ -37,3 +37,9 @@ USE: stack dup >r sbuf-append r> dup >r sbuf-append r> sbuf>str ; + +! HACKS +: char? drop f ; +: >char ; +: >upper ; +: >lower ; diff --git a/library/prettyprint.factor b/library/prettyprint.factor index ef46dba232..b45256ced4 100644 --- a/library/prettyprint.factor +++ b/library/prettyprint.factor @@ -141,7 +141,7 @@ DEFER: prettyprint* dup ends-with-newline? dup [ nip ] [ drop ] ifte ; : prettyprint-comment ( comment -- ) - trim-newline [ "comments" ] get-style write-attr ; + trim-newline "comments" get-style write-attr ; : word-link ( word -- link ) <% diff --git a/library/styles.factor b/library/styles.factor index b32a3a5047..539c5c4650 100644 --- a/library/styles.factor +++ b/library/styles.factor @@ -36,32 +36,11 @@ USE: stack ! significance to the 'fwrite-attr' word when applied to a ! stream that supports attributed string output. -: default-style ( -- style ) - #! Push the default style object. - "styles" get [ "default" get ] bind ; - -: paragraph ( -- style ) - #! Push the paragraph break meta-style. - "styles" get [ "paragraph" get ] bind ; - -: