[comp.lang.forth] Questions about scoping

duggie@jessica.stanford.edu (Doug Felt) (09/03/88)

Some questions for those more up on these things than me:

Back in 1984 I used Forth to write a large graphics adventure-type
educational program for the Apple II.  I had special needs
(accomodating third-party graphics routines, and squeezing everything
into 48K) so wrote my own Forth and editor so I could dispose of the
headers, do I/O in the way I needed, generate subroutine-threaded (jsr
addr, jsr addr...)  code, and so on.  I say this so that people will
believe me :-) when I say that I like Forth and appreciate its many
strengths.

I have not used Forth much since then, although I have used Mach(I) on
the Mac to play around with.  Usually I use C or Object Pascal these
days.  I read stuff in MacTutor but don't know what current Forth
development systems provide, or what public-domain utilities there
might be.  I would like to know if there are ways around the following
problems I have had with Forth systems I have seen:

1) I would like to use something like header files in C, which I can
include so as to avoid typing in lots of constant definitions for
offsets into structures, parameters, and so on, all of which can be
very error-prone.  At the same time I would not like to compile
hundreds of constants into my code, 99% of which will never be used.
Stuff I see in MacTutor usually has pages of constants for just this
purpose.  Lots of effort compared to including a few header files.

2) Are there any good ways to scope names and other declarations?
Again I am thinking of static functions in C and and statics global to
all the routines in a file.  The Forth I wrote in '84 let me put names
in either of two places, one of which was thrown away after the
'module' was compiled and the private names were no longer needed.
This way I didn't need to worry about what these names were when I
used this dictionary later-- only the 'exported' names were there.  I
find name collisions a real headache.  Other types of scoping like
nested functions, constants local to a function, names of parts of
structures, etc also fall into this category.

3) Are there good mechanisms for dynamic memory management?  I'm
thinking of heaps, linked lists, trees, and so on.  I found it
difficult to use such structures in straight Forth because I couldn't
easily grow and shrink them-- since code and data went into the same
chunk of memory, and the only other dynamic data area was the stack.
I have to use the host machine's operating system to get the memory to
do anything like this, which tends to tie me pretty closely to the
machine (of course, on the Mac this is a given anyway).

Am I seriously off base here?  Have these issues been addressed and 
solved long ago?  Is there some secret cache of wonderful Forth tools
sitting out in netland waiting for me to stumble upon it, so that I may
rediscover the True Word (tick, perhaps, or comma?)  ;-)  Thanks for
everyone's patience!

Doug Felt
Courseware Authoring Tools Project
Sweet Hall 3rd Floor
Stanford University
duggie@jessica.stanford.edu

LANGOWSKI@FREMBL51.BITNET (Joerg Langowski) (09/06/88)

Doug Felt writes:
>1) I would like to use something like header files in C, which I can
>include so as to avoid typing in lots of constant definitions for
>offsets into structures, parameters, and so on, all of which can be
>very error-prone.  At the same time I would not like to compile
>hundreds of constants into my code, 99% of which will never be used.
>Stuff I see in MacTutor usually has pages of constants for just this
>purpose.  Lots of effort compared to including a few header files.

I'd like to have this, too. The reason why I'm including all those constant
definitions into the MacTutor examples is simply so that the example is self-
consistent; I've in fact been bashed a number of times on the nets by various
readers for forgetting one definition or the other. Unfortunately there exist
no 'standard' constant definition files in the Mach2 system, so you have to
write your own. With clever use of vocabularies you can introduce some kind
of scoping at that level. I agree, it is not quite satisfactory yet.
In the August MacTutor, you'll find a pretty interesting implementation of an
Object Forth under Mach2. Of course, with classes, scoping, type checking, etc.
becomes a piece of cake to implement (are you still there, Fraser?).
Constants in Mach2 are NOT compiled into the code, they only exist in the
vocabulary, which is thrown away in the actual application. So you don't lose
anything in terms of memory or efficiency.

>2) Are there any good ways to scope names and other declarations?

I think I answered some of that; the other point would be that with a good
local variable implementation you lose many of those problems. The new version
of Mach2 (2.14) has options to reserve local variable blocks and access the loca
l
dictionary (some hacking still required). Of course, a powerful local variable
mechanism has its advantages and drawbacks: the code becomes much more readable,
but one is also tempted to write longer blocks of code.
Side thought: maybe the admittedly bad readability of the classic 'drip drop
flip flop' stack manipulation - which affect code style only on a very low
level - has actually promoted factorization in Forth (???).

>3) Are there good mechanisms for dynamic memory management?  I'm
>thinking of heaps, linked lists, trees, and so on.  I found it
>difficult to use such structures in straight Forth because I couldn't
>easily grow and shrink them-- since code and data went into the same
>chunk of memory, and the only other dynamic data area was the stack.
>I have to use the host machine's operating system to get the memory to
>do anything like this, which tends to tie me pretty closely to the
>machine (of course, on the Mac this is a given anyway).

Yes, on the Mac all of this is provided, and I think in a pretty elegant way.
Don't know too much about other systems.

Joerg Langowski
<langowski@frembl51.bitnet>
EMBL, c/o ILL
156X
F-38042 Grenoble Cedex
France

brunjes@kadsma.kadsm (Roy Brunjes) (09/07/88)

In article <3547@Portia.Stanford.EDU> duggie@jessica.stanford.edu (Doug Felt) writes:
>Some questions for those more up on these things than me:
> [...] three main questions deleted.

Sorry about not replying via email.  I don't have access to netpath currently
so I don't know how to get to your system.

I am a JForth user (for the Amiga ... I don't know what hardware platform
you are currently running on) and JForth supports all of the things you
want (or similar things with slight twists) in addition to some things you
didn't mention.  I would recommend it to any Amiga programmer interested in
Forth.

P.S.  I don't sell their software, I just like it.

Roy Brunjes    ...rochester!kodak!kadsma!brunjes
Disclaimer:  My opinions only, blah blah.