--- gforth/glocals.fs 1997/03/04 17:49:51 1.30 +++ gforth/glocals.fs 2012/02/07 16:48:55 1.65 @@ -1,12 +1,12 @@ \ A powerful locals implementation -\ Copyright (C) 1995 Free Software Foundation, Inc. +\ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2007,2011 Free Software Foundation, Inc. \ This file is part of Gforth. \ Gforth is free software; you can redistribute it and/or \ modify it under the terms of the GNU General Public License -\ as published by the Free Software Foundation; either version 2 +\ as published by the Free Software Foundation, either version 3 \ of the License, or (at your option) any later version. \ This program is distributed in the hope that it will be useful, @@ -15,8 +15,7 @@ \ GNU General Public License for more details. \ You should have received a copy of the GNU General Public License -\ along with this program; if not, write to the Free Software -\ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +\ along with this program. If not, see http://www.gnu.org/licenses/. \ More documentation can be found in the manual and in @@ -85,8 +84,9 @@ \ Currently locals may only be \ defined at the outer level and TO is not supported. -require search-order.fs +require search.fs require float.fs +require extend.fs \ for case : compile-@local ( n -- ) \ gforth compile-fetch-local case @@ -129,14 +129,25 @@ require float.fs slowvoc @ slowvoc on \ we want a linked list for the vocabulary locals vocabulary locals \ this contains the local variables -' locals >body ' locals-list >body ! +' locals >body wordlist-id ' locals-list >body ! slowvoc ! -create locals-buffer 1000 allot \ !! limited and unsafe - \ here the names of the local variables are stored - \ we would have problems storing them at the normal dp +variable locals-mem-list \ linked list of all locals name memory in +0 locals-mem-list ! \ the current (outer-level) definition -variable locals-dp \ so here's the special dp for locals. +: free-list ( addr -- ) + \ free all members of a linked list (link field is first) + begin + dup while + dup @ swap free throw + repeat + drop ; + +: prepend-list ( addr1 addr2 -- ) + \ addr1 is the address of a list element, addr2 is the address of + \ the cell containing the address of the first list element + 2dup @ swap ! \ store link to next element + ! ; \ store pointer to new first element : alignlp-w ( n1 -- n2 ) \ cell-align size and generate the corresponding code for aligning lp @@ -183,26 +194,26 @@ variable locals-dp \ so here's the speci = ; : list-size ( list -- u ) \ gforth-internal -\ size of the locals frame represented by list - 0 ( list n ) - begin - over 0<> - while - over - ((name>)) >body @ max - swap @ swap ( get next ) - repeat - faligned nip ; + \ size of the locals frame represented by list + 0 ( list n ) + begin + over 0<> + while + over + ((name>)) >body @ max + swap @ swap ( get next ) + repeat + faligned nip ; : set-locals-size-list ( list -- ) - dup locals-list ! - list-size locals-size ! ; + dup locals-list ! + list-size locals-size ! ; : check-begin ( list -- ) \ warn if list is not a sublist of locals-list locals-list @ sub-list? 0= if \ !! print current position - ." compiler was overly optimistic about locals at a BEGIN" cr + >stderr ." compiler was overly optimistic about locals at a BEGIN" cr \ !! print assumption and reality then ; @@ -221,12 +232,45 @@ variable locals-dp \ so here's the speci locals-size @ swap ! postpone lp@ postpone c! ; +7 cells 32 + constant locals-name-size \ 32-char name + fields + wiggle room + +: create-local1 ( "name" -- a-addr ) + create + immediate restrict + here 0 , ( place for the offset ) ; + +variable dict-execute-dp \ the special dp for DICT-EXECUTE + +0 value dict-execute-ude \ USABLE-DICTIONARY-END during DICT-EXECUTE + +: dict-execute1 ( ... addr1 addr2 xt -- ... ) + \ execute xt with HERE set to addr1 and USABLE-DICTIONARY-END set to addr2 + dict-execute-dp @ dp 2>r + dict-execute-ude ['] usable-dictionary-end defer@ 2>r + swap to dict-execute-ude + ['] dict-execute-ude is usable-dictionary-end + swap to dict-execute-dp + dict-execute-dp dpp ! + catch + 2r> is usable-dictionary-end to dict-execute-ude + 2r> dpp ! dict-execute-dp ! + throw ; + +defer dict-execute ( ... addr1 addr2 xt -- ... ) + +:noname ( ... addr1 addr2 xt -- ... ) + \ first have a dummy routine, for SOME-CLOCAL etc. below + nip nip execute ; +is dict-execute + : create-local ( " name" -- a-addr ) \ defines the local "name"; the offset of the local shall be \ stored in a-addr - create - immediate restrict - here 0 , ( place for the offset ) ; + locals-name-size allocate throw + dup locals-mem-list prepend-list + locals-name-size cell /string over + ['] create-local1 dict-execute ; + +variable locals-dp \ so here's the special dp for locals. : lp-offset ( n1 -- n2 ) \ converts the offset from the frame start to an offset from lp and @@ -292,26 +336,46 @@ locals-types definitions postpone laddr# @ lp-offset, ; \ you may want to make comments in a locals definitions group: -' \ alias \ immediate -' ( alias ( immediate +' \ alias \ ( compilation 'ccc' -- ; run-time -- ) \ core-ext,block-ext backslash +\G Comment till the end of the line if @code{BLK} contains 0 (i.e., +\G while not loading a block), parse and discard the remainder of the +\G parse area. Otherwise, parse and discard all subsequent characters +\G in the parse area corresponding to the current line. +immediate + +' ( alias ( ( compilation 'ccc' -- ; run-time -- ) \ core,file paren +\G Comment, usually till the next @code{)}: parse and discard all +\G subsequent characters in the parse area until ")" is +\G encountered. During interactive input, an end-of-line also acts as +\G a comment terminator. For file input, it does not; if the +\G end-of-file is encountered whilst parsing for the ")" delimiter, +\G Gforth will generate a warning. +immediate forth definitions +also locals-types + +\ these "locals" are used for comparison in TO +c: some-clocal 2drop +d: some-dlocal 2drop +f: some-flocal 2drop +w: some-wlocal 2drop +' dict-execute1 is dict-execute \ now the real thing + \ the following gymnastics are for declaring locals without type specifier. \ we exploit a feature of our dictionary: every wordlist \ has it's own methods for finding words etc. \ So we create a vocabulary new-locals, that creates a 'w:' local named x \ when it is asked if it contains x. -also locals-types - : new-locals-find ( caddr u w -- nfa ) \ this is the find method of the new-locals vocabulary \ make a new local with name caddr u; w is ignored \ the returned nfa denotes a word that produces what W: produces \ !! do the whole thing without nextname drop nextname - ['] W: >name ; + ['] W: >head-noprim ; previous @@ -322,29 +386,29 @@ create new-locals-map ( -- wordlist-map ' new-locals-find A, ' new-locals-reveal A, ' drop A, \ rehash method +' drop A, -slowvoc @ -slowvoc on -vocabulary new-locals -slowvoc ! -new-locals-map ' new-locals >body cell+ A! \ !! use special access words +new-locals-map mappedwordlist Constant new-locals-wl -variable old-dpp +\ slowvoc @ +\ slowvoc on +\ vocabulary new-locals +\ slowvoc ! +\ new-locals-map ' new-locals >body wordlist-map A! \ !! use special access words \ and now, finally, the user interface words -: { ( -- addr wid 0 ) \ gforth open-brace - dp old-dpp ! - locals-dp dpp ! - also new-locals - also get-current locals definitions locals-types +: { ( -- latestxt wid 0 ) \ gforth open-brace + latestxt get-current + get-order new-locals-wl swap 1+ set-order + also locals definitions locals-types 0 TO locals-wordlist 0 postpone [ ; immediate locals-types definitions -: } ( addr wid 0 a-addr1 xt1 ... -- ) \ gforth close-brace +: } ( latestxt wid 0 a-addr1 xt1 ... -- ) \ gforth close-brace \ ends locals definitions - ] old-dpp @ dpp ! + ] begin dup while @@ -352,9 +416,9 @@ locals-types definitions repeat drop locals-size @ alignlp-f locals-size ! \ the strictest alignment - set-current previous previous - locals-list TO locals-wordlist ; + set-current lastcfa ! + locals-list 0 wordlist-id - TO locals-wordlist ; : -- ( addr wid 0 ... -- ) \ gforth dash-dash } @@ -455,24 +519,27 @@ forth definitions \ explicit scoping : scope ( compilation -- scope ; run-time -- ) \ gforth - cs-push-part scopestart ; immediate + cs-push-part scopestart ; immediate + +: adjust-locals-list ( wid -- ) + locals-list @ common-list + dup list-size adjust-locals-size + locals-list ! ; : endscope ( compilation scope -- ; run-time -- ) \ gforth - scope? - drop - locals-list @ common-list - dup list-size adjust-locals-size - locals-list ! ; immediate + scope? + drop adjust-locals-list ; immediate \ adapt the hooks : locals-:-hook ( sys -- sys addr xt n ) \ addr is the nfa of the defined word, xt its xt DEFERS :-hook - last @ lastcfa @ + latest latestxt clear-leave-stack 0 locals-size ! - locals-buffer locals-dp ! + locals-mem-list @ free-list + 0 locals-mem-list ! 0 locals-list ! dead-code off defstart ; @@ -508,8 +575,7 @@ forth definitions else \ both live over list-size adjust-locals-size >resolve - locals-list @ common-list dup list-size adjust-locals-size - locals-list ! + adjust-locals-list then then ; @@ -613,10 +679,11 @@ forth definitions 2drop endif ; -: >definer ( xt -- definer ) - \ this gives a unique identifier for the way the xt was defined - \ words defined with different does>-codes have different definers - \ the definer can be used for comparison and in definer! +: >definer ( xt -- definer ) \ gforth + \G @var{Definer} is a unique identifier for the way the @var{xt} + \G was defined. Words defined with different @code{does>}-codes + \G have different definers. The definer can be used for + \G comparison and in @code{definer!}. dup >does-code ?dup-if nip 1 or @@ -624,8 +691,9 @@ forth definitions >code-address then ; -: definer! ( definer xt -- ) - \ gives the word represented by xt the behaviour associated with definer +: definer! ( definer xt -- ) \ gforth + \G The word represented by @var{xt} changes its behaviour to the + \G behaviour associated with @var{definer}. over 1 and if swap [ 1 invert ] literal and does-code! else @@ -633,35 +701,37 @@ forth definitions then ; :noname - ' dup >definer [ ' locals-wordlist >definer ] literal = + ' dup >definer [ ' locals-wordlist ] literal >definer = if >body ! else -&32 throw endif ; :noname - 0 0 0. 0.0e0 { c: clocal w: wlocal d: dlocal f: flocal } comp' drop dup >definer case [ ' locals-wordlist ] literal >definer \ value OF >body POSTPONE Aliteral POSTPONE ! ENDOF - [ comp' clocal drop ] literal >definer + \ !! dependent on c: etc. being does>-defining words + \ this works, because >definer uses >does-code in this case, + \ which produces a relocatable address + [ comp' some-clocal drop ] literal >definer OF POSTPONE laddr# >body @ lp-offset, POSTPONE c! ENDOF - [ comp' wlocal drop ] literal >definer + [ comp' some-wlocal drop ] literal >definer OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF - [ comp' dlocal drop ] literal >definer + [ comp' some-dlocal drop ] literal >definer OF POSTPONE laddr# >body @ lp-offset, POSTPONE 2! ENDOF - [ comp' flocal drop ] literal >definer + [ comp' some-flocal drop ] literal >definer OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF -&32 throw endcase ; interpret/compile: TO ( c|w|d|r "name" -- ) \ core-ext,local -: locals| +: locals| ( ... "name ..." -- ) \ local-ext locals-bar \ don't use 'locals|'! use '{'! A portable and free '{' \ implementation is compat/anslocals.fs BEGIN - name 2dup s" |" compare 0<> + name 2dup s" |" str= 0= WHILE (local) REPEAT