Google Skupine ne podpirajo več novih objav ali naročnin v storitvi Usenet. Zgodovinsko vsebino si je še vedno mogoče ogledati.
Dismiss

Simple Static Scoping for Structures

12 ogledov
Preskoči na prvo neprebrano sporočilo

m_l_g3

neprebran,
18. dec. 2008, 12:52:4118. 12. 08
do
I propose a word set that ... well... fixes a disadvantage of many
Forth implementations of structures: the global visibility of field
names. Currently this is overcome by including the class name
into field names, which forces the user to remember which class
the field was introduced in.

The code is available at:
http://www.forth.org.ru/~mlg/SrcLib/ssss.f

The user has the following options:
- make field names globally visible (as before)
- place names of fields declared in derived structures into the same
name space
- construct a tree of name spaces

\ ssss.f Simple Statically Scoped Structures
\ (p) Michael L Gassanenko -- 18 Dec 2008
\ this code and specification are Public Domain

\ SCOPE ( "scope-name" -- ) Create an empty scope that later may be
\ referenced as scope-name. scope-name is a definition that may be,
\ for example, ticked. A word list is associated with scope-name,
\ this word list may be added to the search order by executing
\ s{ scope-name or ' scope-name OPEN-SCOPE.
\ The behaviour of scope-name is implementation-defined.

\ EXTENSION-SCOPE ( scope-xt "scope-name" -- ) create a scope that
\ later may be referenced as scope-name. scope-name is associate with
\ all word lists from the scope identified by scope-xt (later
referenced
\ as the basic scope for scope-name) plus one more word list
\ (later referenced as own word list of scope-name).
\ The behaviour of scope-name is implementation-defined.
\ Ambiguous conditions:
\ - The word }s is not visible in the search order, or
\ some different functionality is visible under the name }s

\ SAME-SCOPE ( scope-xt "scope-name" -- ) create an alias scope
\ for scope-xt. The same set of word lists is associated with
\ scope-xt and scope-name.
\ The behaviour of scope-name is implementation-defined.

\ OPEN-SCOPE ( scope-xt -- ) Add all word lists associated with the
scope
\ to the search order. If scope-xt identifies an extension scope,
\ first (recursively) add word lists from the basic scope, then
\ add own word list (so that it is searhed first).
\ An ambiguous condition exists if scope-xt does not represent a
scope.

\ s{ ( "scope-name" -- ) "s-brace" Parse scope-name delimited by
spaces.
\ Find scope-name in the search order and perform OPEN-SCOPE.
\ This is an immediate word.

\ }s ( -- ) "closing-brace-s" Close the scope, that is, remove from
\ the search order all word lists associated with the last opened
scope.
\ This is an immediate word.
\ Ambiguous conditions:
\ - In the process of removing the word lists, the search order
\ reaches such a state that the name }s is not visible, or
\ some different functionality is visible under the name }s
\ - The execution token for }s is executed when the search order is
\ in a different state than at the moment of finding }s
\ - The state of the search order is different than what was
established
\ by OPEN-SCOPE or s{

1 [IF]
\ Reference Implementation 1
: order-depth ( -- n ) get-order dup >r 0 ?do drop loop r> ;
: >order ( wid -- ) >r get-order r> swap 1+ set-order ;

: scope ( "name" -- ) wordlist create , does> @ >order ;
: same-scope ( xt "name" -- ) create , does> @ execute ;

: open-scope ( xt -- ) order-depth >r execute
order-depth r> <= abort" expected a scope";
: s{ ( "name" -- ) ' open-scope ; immediate
: }s previous ; immediate

: extension-scope ( xt "name" -- )
wordlist get-current over set-current
[ char | parse : }s previous s" }s" evaluate ; immediate|] sliteral
evaluate
set-current ( xt wid )
create , , does> 2@ >r open-scope r> >order ;

[ELSE]
\ Reference Implementation 2
315 constant scope-tag
: ?scope scope-tag xor abort" expected a scope";
: scope wordlist scope-tag 2constant ;
: same-scope >r : r> compile, postpone ; ;
: open-scope ( xt -- )
>r get-order depth - r> swap >r execute ?scope r> depth + set-
order ;
: extension-scope ( xt "name" -- )
wordlist
get-current swap set-current
over open-scope
S" : }s previous postpone }s ; immediate }s" evaluate
get-current swap set-current
( xt wid ) create , , does> 2@ >r execute ?scope r> scope-tag ;
: s{ ( "name" -- ) ' open-scope ; immediate
: }s ( -- ) previous ; immediate

[THEN]

\ ========== example of usage ==================

1 [IF]

cr .( == example: lists ==) cr
.( starting: ) cr order cr .s

\ define a simple field
: SFIELD ( offset "name" -- ) ( name: addr -- addr+offset )
CREATE DUP , DOES> @ + ;

scope list
s{ list definitions
0
sfield next cell+
: .next next @ ;
: !next next ! ;
}s definitions
constant /listelem

cr .( /listelem = ) /listelem .

: reverse-list ( head -- head' )
0 swap
begin dup while ( prev this )
s{ list
dup dup .next ( prev this this next ) 2swap !next
}s
repeat
drop
;

' list extension-scope intlist
/listelem
s{ intlist definitions
sfield data cell+
: .data data @ ;
: !data data ! ;
}s definitions
constant /intlist-elem

cr .( /intlist-elem = ) /intlist-elem .

: int-elem ( tail x -- head )
align here /intlist-elem allot >r
s{ intlist r@ !data r@ !next }s r> ;

: print-intlist ( head -- )
begin dup while s{ intlist dup .data . .next }s repeat drop ;

variable my-intlist
0
10 int-elem 20 int-elem 30 int-elem 40 int-elem
my-intlist !

cr .( int list: )
my-intlist @ print-intlist
my-intlist @ reverse-list my-intlist !
cr .( reverse int list: )
my-intlist @ print-intlist

' list extension-scope stringlist

/listelem
s{ stringlist definitions
sfield length cell+
sfield data
: .data dup data swap length @ ;
: .length length @ ;
: !length length ! ;
}s definitions
constant /stringlist-base

cr .( /stringlist-base = ) /stringlist-base .

: string-elem ( tail addr len -- head )
align here over /stringlist-base + allot >r
s{ stringlist r@ !length r@ data r@ .length cmove r@ !next }s r>
;
: print-stringlist ( head -- )
begin dup while s{ stringlist dup .data type space .next }s repeat
drop ;

variable my-stringlist
0
S" first" string-elem S" second" string-elem
S" third" string-elem S" fourth" string-elem
my-stringlist !

cr .( string list: )
my-stringlist @ print-stringlist
my-stringlist @ reverse-list my-stringlist !
cr .( reverse string list: )
my-stringlist @ print-stringlist

cr .( finishing: ) cr order cr .s
[THEN]

Elizabeth D Rather

neprebran,
18. dec. 2008, 13:30:4718. 12. 08
do
m_l_g3 wrote:
> I propose a word set that ... well... fixes a disadvantage of many
> Forth implementations of structures: the global visibility of field
> names. Currently this is overcome by including the class name
> into field names, which forces the user to remember which class
> the field was introduced in.
>
> The code is available at:
> http://www.forth.org.ru/~mlg/SrcLib/ssss.f
>
> The user has the following options:
> - make field names globally visible (as before)
> - place names of fields declared in derived structures into the same
> name space
> - construct a tree of name spaces
>
[details snipped]

A couple of quick comments...

First, in most of the applications in which we have used structures it
has been important for them to be global.

That said, there are situations in which namespace scoping is important
(not just with structures), and many (most?) modern Forths provide
scoping tools. Have you looked at them? How does your proposal reflect
current practice in this regard?

Similarly, many modern Forths implement some sort of object or class
based system. How does your proposal resonate with these?

If you're proposing something that you hope to gain broad acceptance,
it's important to consider (and discuss) it in the context of current
practice (not just the current standard).

Cheers,
Elizabeth

--
==================================================
Elizabeth D. Rather (US & Canada) 800-55-FORTH
FORTH Inc. +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================

m_l_g3

neprebran,
22. dec. 2008, 17:49:3522. 12. 08
do
Elizabeth,

This is not a proposal for standardization.
It is a proposal for the community, "use this if you need something
like that".
In a sense, it complements the words that define fields.
(I understand that there will never be an agreement, some people
do not mind sizes going after field declarations, the others
think that field size MUST be specified before the field-declaring
word.)

The good thing about this word set is that it may be used with
any structure/object package that does not make any use of word lists.

I, of course, would like it to be a set of 6 one-liners.
I have not found a way to code this as 6 one-liners, but have found a
bug
(I will post an update a bit later).

By the way, I have a comment on the ANSI word set:

I have to implement >ORDER ORDER-TOP ORDER_DEPTH in terms of
GET-ORDER and SET-ORDER. This is terrible.
Why the standard word set is not >ORDER ORDER> ORDER-DEPTH ?

Ed

neprebran,
23. dec. 2008, 17:16:4823. 12. 08
do
m_l_g3 wrote:
> I propose a word set that ... well... fixes a disadvantage of many
> Forth implementations of structures: the global visibility of field
> names. Currently this is overcome by including the class name
> into field names, which forces the user to remember which class
> the field was introduced in.
>
> The code is available at:
> http://www.forth.org.ru/~mlg/SrcLib/ssss.f
> ...

Ref implementation 1 appears to be broken. Won't compile or
crashes on several systems tried.

jace...@gmail.com

neprebran,
24. dec. 2008, 03:47:2724. 12. 08
do
On 18 dic, 18:52, m_l_g3 <m_l...@yahoo.com> wrote:
> I propose a word set that ... well... fixes a disadvantage of many
> Forth implementations of structures: the global visibility of field
> names. Currently this is overcome by including the class name
> into field names, which forces the user to remember which class
> the field was introduced in.
>
> The code is available at:http://www.forth.org.ru/~mlg/SrcLib/ssss.f

Somewhat related to this, for namespaces I use:

\g Begin namespace definition
: ns ( "name" -- )
wordlist create immediate dup ,
+order definitions does>
get-order n>r
@ 1 set-order
'nfa ['] doword catch
nr> set-order throw ;

\g End namespace definition
: /ns ( -- )
previous definitions ;

And I combine that with the typical structs:

\g Start structure definition
: struct ( -- sizeof )
0 ;

\g Define structure field
: field ( prevsizeof size "name" -- sizeof )
create over , + does> @ + ;

\g Terminate structure definition
: /struct ( sizeof "name" -- )
create , does> @ * ;


When used in combination looks like this:

struct ns 2dpoint
1 cells field x
1 cells field y
: init ( x y p -- ) tuck y ! x ! ;
/ns /struct 2dpoints

create mypoint 1 2dpoints allot
1 2 mypoint init
mypoint 2dpoint x @ .
mypoint 2dpoint y @ .

The NS word can also be used to put libraries inside a namespace:

ns mylib
include mylib.fs
/ns

mylib init

m_l_g3

neprebran,
4. jan. 2009, 18:19:514. 1. 09
do

http://www.forth.org.ru/~mlg/SrcLib/ssss.f

The file has been updated.

A bug has been fixed, plus a tester has been added.
That is, if you happen to decide to use the proposed scopes,
you can first of all run the tester and make sure that it works.
(And if it does not, you may pick a different implementation,
because there are 4 different implementations in that file.)


0 novih sporočil