21st Century Forth Question 
Author Message
 21st Century Forth Question

Back in the 1970's I was introduced to Forth on my Apple II computer.  I
even wrote my own peculiar dialect of Forth as a direct threaded
compiler/interpretor for the 6502.  But I have been away from the language
since then and I'm trying to come up to speed on all the changes.

My current little project is writing a "Toy Forth" compiler.  I call it toy
Forth because I'm trying to keep it as direct and simple-minded as possible
and yet still compilant with ANS.  I've written it under Windows, but it
runs as if it were on an 8-bit system with 16-bit addresses.  I've set aside
a 64k buffer to simulate user ram, and "addresses" are really indexes into
that ram buffer.  So in a way it's also an 8-bit machine emulator.  Since it
emulates 8-bit operation and is simple and direct rather than optimized this
toy Forth is not at all suitable for any real-life applications.  But it's a
good excercise for me to re-learn Forth, and it makes a great beginner's
version of Forth since it avoids all the complications inherent in modern
addressing modes.

I started out just writing it from what I little I remembered of Forth on my
Apple.  The result works just fine, but uses a lot of the wrong names for
words, and is not yet ANS.

That brings me to my first question.  I'm looking through the ANS word list
and I don't find anything like a "return" word that would be compiled as the
last word in a definition to return to the caller.  Does such a word exist
under a different name or does the semi-colon just put some unnamed xt into
the compiled code?  How is this handled under ANS standards?  I seem to
recall that fig Forth had a word named "NEXT" for that, although I may be
misremembering.

--gary

--

Te audire no possum.
Musa sapientum fixa est in aure.



Wed, 05 May 2004 04:50:12 GMT  
 21st Century Forth Question
DOH!  Never mind.  I found it "EXIT"

Another dumb question, though.  What happened to the old Forth word "DTOP"?
does that no longer exist?  How does the comma word know where to stuff the
xt?  Is it OK to use it in a simple-minded 8-bit Forth compiler?

( sorry if these questions sound really dumb.  I've been away from Forth for
a loooong time. )

--gary


<snip>

Quote:
> That brings me to my first question.  I'm looking through the ANS word
list
> and I don't find anything like a "return" word that would be compiled as
the
> last word in a definition to return to the caller.  Does such a word exist
> under a different name or does the semi-colon just put some unnamed xt
into
> the compiled code?  How is this handled under ANS standards?  I seem to
> recall that fig Forth had a word named "NEXT" for that, although I may be
> misremembering.

> --gary



Wed, 05 May 2004 05:13:39 GMT  
 21st Century Forth Question

Quote:

> DOH!  Never mind.  I found it "EXIT"

> Another dumb question, though.  What happened to the old Forth word "DTOP"?

Dictionary top? See HERE .

Quote:
> does that no longer exist?  How does the comma word know where to stuff the
> xt?

: , HERE CELL ALLOT ! ; \ CELL == 1 CELLS == sizeof(int)

Quote:
>  Is it OK to use it in a simple-minded 8-bit Forth compiler?

IIRC the ANS Forth says that there are at least 16 bits in a cell.
AFAIK most Forths for 8-bit processors emulate a 16-bit machine.


Wed, 05 May 2004 04:38:22 GMT  
 21st Century Forth Question

Quote:

> DOH!  Never mind.  I found it "EXIT"

> Another dumb question, though.  What happened to the old Forth word "DTOP"?
> does that no longer exist?  How does the comma word know where to stuff the
> xt?  Is it OK to use it in a simple-minded 8-bit Forth compiler?

> ( sorry if these questions sound really dumb.  I've been away from Forth for
> a loooong time. )

> --gary



> <snip>

> > That brings me to my first question.  I'm looking through the ANS word
> list
> > and I don't find anything like a "return" word that would be compiled as
> the
> > last word in a definition to return to the caller.  Does such a word exist
> > under a different name or does the semi-colon just put some unnamed xt
> into
> > the compiled code?  How is this handled under ANS standards?  I seem to
> > recall that fig Forth had a word named "NEXT" for that, although I may be
> > misremembering.

> > --gary

EXIT is used primarily to break out of loops. It's not needed at the end
of a colon definition because, as you surmised, semicolon does that*. In
the assembler, NEXT is usually the address of the inner interpreter in a
threaded Forth as in an assembler instruction 'NEXT jump", but sometimes
it is a macro for the whole instruction. I need coffee. Did I come
across despite that?

Jerry
________________________________
* Semicolon might compile a call to NEXT and finish the dictionary
entry, unSMUDGE, and do whatever cleanup ls necessary.
--
Engineering is the art of making what you want from things you can get.
-----------------------------------------------------------------------



Wed, 05 May 2004 05:52:17 GMT  
 21st Century Forth Question

Quote:

> Back in the 1970's I was introduced to Forth on my Apple II computer.  I
> even wrote my own peculiar dialect of Forth as a direct threaded
> compiler/interpretor for the 6502.  But I have been away from the language
> since then and I'm trying to come up to speed on all the changes.

> My current little project is writing a "Toy Forth" compiler.  I call it toy
> Forth because I'm trying to keep it as direct and simple-minded as possible
> and yet still compilant with ANS.  I've written it under Windows, but it
> runs as if it were on an 8-bit system with 16-bit addresses.  I've set aside
> a 64k buffer to simulate user ram, and "addresses" are really indexes into
> that ram buffer.  So in a way it's also an 8-bit machine emulator.  Since it
> emulates 8-bit operation and is simple and direct rather than optimized this
> toy Forth is not at all suitable for any real-life applications.  But it's a
> good excercise for me to re-learn Forth, and it makes a great beginner's
> version of Forth since it avoids all the complications inherent in modern
> addressing modes.

IMO the best way to come up to speed on "modern Forth" is to get one,
such as the evaluation systems from FORTH, Inc. or MPE, and write
some applications.  I've never understood the notion that the way to
learn Forth is by implementing one; no other language works that way.
The result is often somebody who knows an implementation but couldn't
write a decent Forth program if his life depended on it.

Quote:
> I started out just writing it from what I little I remembered of Forth on my
> Apple.  The result works just fine, but uses a lot of the wrong names for
> words, and is not yet ANS.

If they are internal (not user) features, there are no "wrong" names.
The only names that are important (along with their behaviors) are the
words listed in ANS Forth.

Quote:
> That brings me to my first question.  I'm looking through the ANS word list
> and I don't find anything like a "return" word that would be compiled as the
> last word in a definition to return to the caller.  Does such a word exist
> under a different name or does the semi-colon just put some unnamed xt into
> the compiled code?  How is this handled under ANS standards?  I seem to
> recall that fig Forth had a word named "NEXT" for that, although I may be
> misremembering.

The important thing in modern Forth is that implementation details aren't
specified.  This has freed implementors to find strategies that yield much
better performance and programming flexibility.  Many modern Forths,
for example, don't compile xt's at all, but generate optimized machine
code.

Historically, NEXT ended code definitions by returning to the address
interpreter.  AFAIK it has always been the case that semi-colon compiled
_something_ (whose name varied, but was frequently EXIT) at that
point.  It isn't in the standard, because whatever it is is an implementation
detail.  For a Forth running machine code, it's usually a RET instruction.

Cheers,
Elizabeth

--
================================================
Elizabeth D. Rather   (US & Canada)       800-55-FORTH
FORTH Inc.                                      +1 310-491-3356
5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
Hawthorne, CA 90250
http://www.forth.com

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



Wed, 05 May 2004 07:34:35 GMT  
 21st Century Forth Question



<snip>

Quote:

> > My current little project is writing a "Toy Forth" compiler.  I call it
toy
> > Forth because I'm trying to keep it as direct and simple-minded as
possible
> > and yet still compilant with ANS.  I've written it under Windows, but it
> > runs as if it were on an 8-bit system with 16-bit addresses.  I've set
aside
> > a 64k buffer to simulate user ram, and "addresses" are really indexes
into
> > that ram buffer.  So in a way it's also an 8-bit machine emulator.
Since it
> > emulates 8-bit operation and is simple and direct rather than optimized
this
> > toy Forth is not at all suitable for any real-life applications.  But
it's a
> > good excercise for me to re-learn Forth, and it makes a great beginner's
> > version of Forth since it avoids all the complications inherent in
modern
> > addressing modes.

> IMO the best way to come up to speed on "modern Forth" is to get one,
> such as the evaluation systems from FORTH, Inc. or MPE, and write
> some applications.  I've never understood the notion that the way to
> learn Forth is by implementing one; no other language works that way.
> The result is often somebody who knows an implementation but couldn't
> write a decent Forth program if his life depended on it.

No doubt the best way to learn to write applications in Forth is to write
applications in Forth.  However, I write applications all day at work (in
C++) and my reason for playing with Forth (and I emphasize "playing") is to
learn more about the internals of how threaded languages work.

And for what's it's worth I learned a heck of a lot about C by writing a
mini C compiler some years back.  And my very first project on my very first
home computer was a BASIC interpretor.  I also wrote a fortran compiler for
the Z80 about 150 years ago.  So it's the internal aspects of the language
that hold the real fascination for me.

- Show quoted text -

Quote:
> Cheers,
> Elizabeth

> --
> ================================================
> Elizabeth D. Rather   (US & Canada)       800-55-FORTH
> FORTH Inc.                                      +1 310-491-3356
> 5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
> Hawthorne, CA 90250
> http://www.forth.com

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



Wed, 05 May 2004 08:06:10 GMT  
 21st Century Forth Question


Quote:

> > DOH!  Never mind.  I found it "EXIT"

> > Another dumb question, though.  What happened to the old Forth word
"DTOP"?

> Dictionary top? See HERE .

> > does that no longer exist?  How does the comma word know where to stuff
the
> > xt?

> : , HERE CELL ALLOT ! ; \ CELL == 1 CELLS == sizeof(int)

My mistake.  DTOP was the old way to know where to begine to search the
dictionary.  It pointed to the first entry.

Quote:
> >  Is it OK to use it in a simple-minded 8-bit Forth compiler?

> IIRC the ANS Forth says that there are at least 16 bits in a cell.
> AFAIK most Forths for 8-bit processors emulate a 16-bit machine.

Yes, 16 bits in a CELL, but 8 bits in a CHAR (see CHAR+)

--gary



Wed, 05 May 2004 08:08:26 GMT  
 21st Century Forth Question


<snip>

Quote:

> EXIT is used primarily to break out of loops. It's not needed at the end
> of a colon definition because, as you surmised, semicolon does that*. In
> the assembler, NEXT is usually the address of the inner interpreter in a
> threaded Forth as in an assembler instruction 'NEXT jump", but sometimes
> it is a macro for the whole instruction. I need coffee. Did I come
> across despite that?

Yes, I follow in spite of lack of coffee :-)

But wouldn't semicolon have to put SOMETHING as the last xt of the
definition?  And in and indirect threaded implementation wouldn't that be
the xt of EXIT?

Thanks so much for all your help.

--gary

Quote:
> Jerry
> ________________________________
> * Semicolon might compile a call to NEXT and finish the dictionary
> entry, unSMUDGE, and do whatever cleanup ls necessary.
> --
> Engineering is the art of making what you want from things you can get.
> -----------------------------------------------------------------------



Wed, 05 May 2004 08:28:08 GMT  
 21st Century Forth Question

Quote:

> > IMO the best way to come up to speed on "modern Forth" is to get one,
> > such as the evaluation systems from FORTH, Inc. or MPE, and write
> > some applications.  I've never understood the notion that the way to
> > learn Forth is by implementing one; no other language works that way.
> > The result is often somebody who knows an implementation but couldn't
> > write a decent Forth program if his life depended on it.

> No doubt the best way to learn to write applications in Forth is to write
> applications in Forth.  However, I write applications all day at work (in
> C++) and my reason for playing with Forth (and I emphasize "playing") is to
> learn more about the internals of how threaded languages work.

> And for what's it's worth I learned a heck of a lot about C by writing a
> mini C compiler some years back.  And my very first project on my very first
> home computer was a BASIC interpretor.  I also wrote a FORTRAN compiler for
> the Z80 about 150 years ago.  So it's the internal aspects of the language
> that hold the real fascination for me.

That being the case, you should approach Forth by first discarding the notion
that it's an inherently "threaded" language and free yourself to look at
alternative implementation strategies, or do several and compare.

Cheers,
Elizabeth

--
================================================
Elizabeth D. Rather   (US & Canada)       800-55-FORTH
FORTH Inc.                                      +1 310-491-3356
5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
Hawthorne, CA 90250
http://www.forth.com

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



Wed, 05 May 2004 08:52:15 GMT  
 21st Century Forth Question

Quote:




> <snip>

> > EXIT is used primarily to break out of loops. It's not needed at the end
> > of a colon definition because, as you surmised, semicolon does that*. In
> > the assembler, NEXT is usually the address of the inner interpreter in a
> > threaded Forth as in an assembler instruction 'NEXT jump", but sometimes
> > it is a macro for the whole instruction. I need coffee. Did I come
> > across despite that?

> Yes, I follow in spite of lack of coffee :-)

> But wouldn't semicolon have to put SOMETHING as the last xt of the
> definition?  And in and indirect threaded implementation wouldn't that be
> the xt of EXIT?

> Thanks so much for all your help.

: foo ." hi!" ;
 ok[Dec]

foo
hi! ok[Dec]

see foo
: foo     (.") [21696803"hi!"] EXIT  ok[Dec]

see ;
: ;     ?COMP FINISH-LOCALS ?CSP COMPILE EXIT [ REVEAL EXIT  ok[Dec]
\                                ^^^^^^^^^^^^   ^^^^^^
\                                |              |
\                                compiles EXIT  makes visible

see :
: :     donest CF, LASTCF ( IT ) ALIAS HIDE ] !CSP RESET-LOCALS EXIT  ok[Dec]



Wed, 05 May 2004 18:13:16 GMT  
 21st Century Forth Question

Quote:




> > > DOH!  Never mind.  I found it "EXIT"

> > > Another dumb question, though.  What happened to the old Forth word
> "DTOP"?

> > Dictionary top? See HERE .

> > > does that no longer exist?  How does the comma word know where to stuff
> the
> > > xt?

> > : , HERE CELL ALLOT ! ; \ CELL == 1 CELLS == sizeof(int)

> My mistake.  DTOP was the old way to know where to begine to search the
> dictionary.  It pointed to the first entry.


defined word. One of my pet words is IT ( -- xt ) that
returns xt of the last definition.

On some systems LATEST was defined as

while on other systems the address of the last name is kept in a variable,

Some system use word lists that include multiple linked lists
of names.
hash: name-string --> number
table-of-pointers x number --> the-right-list-for-name

Both words that define names and words that search for names
can calculate the-right-list-for-name.

Quote:
> It pointed to the first entry.

The first entry to search (last defined) or the first defined entry?


Wed, 05 May 2004 18:21:05 GMT  
 21st Century Forth Question

Quote:


>> But wouldn't semicolon have to put SOMETHING as the last xt of the
>> definition?  And in and indirect threaded implementation wouldn't that be
>> the xt of EXIT?

>Well, assuming that you're compiling to threaded version of code that
>actually contains XTs, you'd typically have to compile something.  But
>there's a large range of what might actually get done (especially if
>not using a threaded code model).

>EXIT is for the use of programmers when they want to leave a
>definition before its end.  EXIT thus has to do a lot of the same work
>that ";" does (except it doesn't actually switch to interpretation
>mode).

The xt compiled by  EXIT has to do *exactly* the same work that the xt
compiled by  ; does.

Quote:
>  Ie, a user-friendly system might put error checking into
>EXIT to warn the user if all the loops aren't cleaned up or if stack
>depth changed during compilation, etc.

These are differences in compile time action.  The run time action, which is
determined by the xt compiled, is the same for both.

Quote:

>Plus, ";" might compile in different words to exit for different colon
>definitions (ie, a "restore-locals-and-exit" word used only when the
>definition used local variables).

In such implementations  EXIT  would need to do the same.

But although the xt compiled by  EXIT  does the same as the xt compiled by
;  some implementations are designed compile a different xt for  EXIT  that
either invokes the same physical code as the xt compiled by  ;  or a
physically separate but identical piece of code.  The reason for this is
that using distinct xts makes it much simpler to write a decompiler that can
recognise the end of a definition.

Regards,
Philip.



Thu, 06 May 2004 02:56:18 GMT  
 21st Century Forth Question

Quote:





> > > > DOH!  Never mind.  I found it "EXIT"

> > > > Another dumb question, though.  What happened to the old Forth word
> > "DTOP"?

> > > Dictionary top? See HERE .

> > > > does that no longer exist?  How does the comma word know where to stuff
> > the
> > > > xt?

> > > : , HERE CELL ALLOT ! ; \ CELL == 1 CELLS == sizeof(int)

> > My mistake.  DTOP was the old way to know where to begine to search the
> > dictionary.  It pointed to the first entry.


> defined word. One of my pet words is IT ( -- xt ) that
> returns xt of the last definition.

> On some systems LATEST was defined as

> while on other systems the address of the last name is kept in a variable,

> Some system use word lists that include multiple linked lists
> of names.
> hash: name-string --> number
> table-of-pointers x number --> the-right-list-for-name

> Both words that define names and words that search for names
> can calculate the-right-list-for-name.

> > It pointed to the first entry.

> The first entry to search (last defined) or the first defined entry?

All of this is totally implementation-dependent, everyone does it
differently.  Consult your system documentation!  None of the
above works on any systems I've seen!

Cheers,
Elizabeth

--
================================================
Elizabeth D. Rather   (US & Canada)       800-55-FORTH
FORTH Inc.                                      +1 310-491-3356
5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
Hawthorne, CA 90250
http://www.forth.com

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



Sat, 08 May 2004 03:31:47 GMT  
 21st Century Forth Question

Quote:


> EXIT is used primarily to break out of loops. It's not needed at the end

 I thought LEAVE was done for that; EXIT exits from a colon definition.
 If you use EXIT from within a DO...LOOP, you'll have trouble, I think.
Quote:
> of a colon definition because, as you surmised, semicolon does that*. In
> the assembler, NEXT is usually the address of the inner interpreter in a
> threaded Forth as in an assembler instruction 'NEXT jump", but sometimes
> it is a macro for the whole instruction. I need coffee. Did I come
> across despite that?

> Jerry
> ________________________________
> * Semicolon might compile a call to NEXT and finish the dictionary
> entry, unSMUDGE, and do whatever cleanup ls necessary.



Sun, 09 May 2004 20:43:16 GMT  
 21st Century Forth Question
Loops can be implemented in different ways.  >LCD is not a loop, but
it's here to illustrate another use if EXIT . It's first because
LCD-ECHO uses it. In LCD-ECHO , there is a BEGIN ... AGAIN loop that
needs to be ended somehow. EXIT is perfect for that. (Constants in HEX.)

\ Smart LCD-EMIT that knows linefeed, carriage return, and backspace.
: >LCD ( terminal? c -- terminal? )
       DUP 8 = IF OVER IF BL EMIT EMIT ELSE DROP THEN LCD-BS EXIT THEN
       DUP A = IF DROP LCD-LF EXIT THEN
       DUP D = IF DROP LCD-CR EXIT THEN
       LCD-EMIT ;

: LCD-ECHO ( terminal? -- )
\ Writes typed characters to the LCD and optionally, the terminal.
\ CR, LF, and BS work.
        DUP IF CR THEN \  New line on the terminal?
        LCD-CUR \ Show cursor on the LCD
        BEGIN
            KEY DUP 1B = IF \ Exit on <esc>
                DROP LCD-ON \ Byebye cursor
                IF CR THEN   EXIT THEN
            OVER IF DUP EMIT THEN >LCD
        AGAIN ;

Jerry
--
Engineering is the art of making what you want from things you can get.
-----------------------------------------------------------------------

Quote:



> > EXIT is used primarily to break out of loops. It's not needed at the end

>  I thought LEAVE was done for that; EXIT exits from a colon definition.
>  If you use EXIT from within a DO...LOOP, you'll have trouble, I think.

  ...


Sun, 09 May 2004 21:53:36 GMT  
 
 [ 83 post ]  Go to page: [1] [2] [3] [4] [5] [6]

 Relevant Pages 

1. Career advice for the 21st century: Stay away from any job that can be done online

2. Career advice for the 21st century: Stay away from any job that can be done online

3. CFP reminder: Workshop on Exception Handling for a 21st Century Programming Language

4. CFP reminder: Workshop on Exception Handling for a 21st Century Programming Language

5. workshop on Exception Handling for a 21st Century Programming Language

6. Sick of the 21st Century ?

7. Scripting: Higher Level Programming for the 21st Century

8. A real XII Century church model and a question

9. (FWD) Request For Information on "21st century skills"

10. Review of "COBOL for the 21st Century" book?

11. Are Your Business Applications Ready for the 21st Century?

12. Privacy in the 21st century -- tclInt.h vs. tcl.h

 

 
Powered by phpBB® Forum Software