6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Nov 24, 2024 10:45 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Dec 31, 2014 4:01 pm 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
One problem I keep having while learning Forth is that the language has changed in the past 30 years but some of the most in-depth documentation is still stuck in the land of WORD (instead of PARSE-NAME). For instance, I am finally getting around to coding VALUE for Tali Forth, and I'm slightly at a loss why you would still want to use VARIABLE except for special cases: VALUE initializes and gives you the, ah, value you saved instead of the address where it lives.
Code:
13 VALUE morethantwelve  ok
morethantwelve .  13  ok
Compare this to:
Code:
VARIABLE morethantwelve  ok
13 morethantwelve !  ok
morethantwelve @  . 13  ok
(Typed from memory, can't test at the moment, sorry if typos) Am I missing something here, possibly in connection with TO, or is VARIABLE just outdated? Maybe we should start a series of tutorials with more modern Forth examples to complement the current literature.


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 31, 2014 4:09 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1952
Location: Sacramento, CA, USA
I am not as far along in learning Forth as you are, but it looks to me (from your example) that VALUE is akin to a constant declaration, whereas VARIABLE is more like a variable declaration. Of course, you didn't show how easy or difficult it is to change a VALUE on the fly, but it also reminds me of the old "pass by value" vs. "pass by reference" argument when discussing function parameters.

Mike


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 31, 2014 5:51 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
barrym95838 wrote:
VALUE is akin to a constant declaration
Based on the example provided, the behavior & usage of VALUE appear identical to that of Fig-Forth CONSTANT, so I'd say Mike's right. Which raises the question, why substitute a new name for an already-existing function?

FWIW, in my own code I sometimes (ab-)use CONSTANT to implement what is, in fact, a variable. This boosts performance in the very common case where a value is read frequently but written to infrequently. Writes are accomplished with a ! to the Parameter Field of the so-called constant, and of course that in itself yields no benefit. But reads are accelerated because it's unnecessary to explicitly use @. It takes one word, not two, to get that value on stack.

It's a handy trick, but the code's readability is compromised when a word defined as a CONSTANT returns a value which is not constant. Maybe that's why the less suggestive name VALUE was substituted. (Corrections and additional info welcome! I have not kept up to date on standards discussions.)

-- Jeff

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 31, 2014 6:53 pm 
Offline

Joined: Tue Jan 07, 2014 8:40 am
Posts: 91
A VALUE is not a CONSTANT. It's an alternate implementation of a variable. As follows:

VARIABLE FOO
FOO @ ( to get the contents of FOO )
n FOO ! ( to store n into FOO )

x VALUE BAR
BAR ( to get the contents of BAR, currently x )
n TO BAR ( to store n into BAR )

I've lost count of the number of different ways this has been implemented. People who come from other languages like the fact that referencing the name of a variable gives you its value (with no need for a @ operator).

There are two disadvantages to VALUEs: First, there's no agreed way to implement VALUEs for anything larger than a single cell. Second, there's no agreed way to do call by reference. (How do you pass the address of a VALUE to a function?)

_________________
Because there are never enough Forth implementations: http://www.camelforth.com


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 02, 2015 2:03 pm 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
Brad, thank you (as always) for the clarification. Hadn't though of the reference problem ...


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 26, 2015 12:07 pm 
Offline

Joined: Mon Jan 26, 2015 6:19 am
Posts: 85
When the value stored changes frequently, VARIABLE is the way to go.
Also, there is no easy way to implement something like +! with VALUE.


Top
 Profile  
Reply with quote  
PostPosted: Sun Aug 05, 2018 8:39 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
In his book Thinking Forth, Leo Brodie gives an example of hiding information to minimize the effects of design changes by localizing things that might change. He claims that direct memory access with @ and ! makes it possible. In his example, he uses a variable APPLES to keep a tally of apples. Then at the eleventh hour after a lot of code using APPLES has been written, a tally must be kept of two different kinds of apples, red and green. Leo Brodie proceeds to show how APPLES can be redefined as a colon definition which can be set to return the correct location for each tally.
Code:
VARIABLE COLOR  ( pointer to current tally)
VARIABLE REDS  ( tally of red apples)
VARIABLE GREENS  ( tally of green apples)
: RED  ( set apple-type to RED)  REDS COLOR ! ;
: GREEN  ( set apple-type to GREEN)  GREENS COLOR ! ;
: APPLES  ( -- adr of current apple tally)  COLOR @ ;

To quote Leo Brodie "If we first say RED, then we can use APPLES to refer to red apples. If we say GREEN, we can use it to refer to green apples. We didn't need to change the syntax of any existing code that uses APPLES. We can still say
20 APPLES !
and
1 APPLES +!"

Cheers,
Jim


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: