Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #56442 > unrolled thread

Define a variable only not defined.

Started byFaisal <faisalm83@gmail.com>
First post2017-03-29 02:39 -0700
Last post2017-03-29 16:35 -1000
Articles 12 — 9 participants

Back to article view | Back to comp.lang.forth


Contents

  Define a variable only not defined. Faisal <faisalm83@gmail.com> - 2017-03-29 02:39 -0700
    Re: Define a variable only not defined. minforth@arcor.de - 2017-03-29 04:20 -0700
      Re: Define a variable only not defined. Faisal <faisalm83@gmail.com> - 2017-03-29 04:26 -0700
        Re: Define a variable only not defined. Coos Haak <htrofhc@gmail.com> - 2017-03-29 14:58 +0200
          Re: Define a variable only not defined. Alex <alex@rivadpm.com> - 2017-03-29 14:41 +0100
          Re: Define a variable only not defined. anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-29 13:54 +0000
            Re: Define a variable only not defined. Mikael Nordman <oh2aun@gmail.com> - 2017-03-29 12:06 -0700
              Re: Define a variable only not defined. Coos Haak <htrofhc@gmail.com> - 2017-03-29 21:30 +0200
                Re: Define a variable only not defined. Mikael Nordman <oh2aun@gmail.com> - 2017-03-29 12:53 -0700
    Re: Define a variable only not defined. "HAA" <someone@microsoft.com> - 2017-03-30 10:03 +1000
      Re: Define a variable only not defined. Julian Fondren <julian.fondren@gmail.com> - 2017-03-29 19:00 -0700
        Re: Define a variable only not defined. "Elizabeth D. Rather" <erather@forth.com> - 2017-03-29 16:35 -1000

#56442 — Define a variable only not defined.

FromFaisal <faisalm83@gmail.com>
Date2017-03-29 02:39 -0700
SubjectDefine a variable only not defined.
Message-ID<252d2e3a-a277-4447-9bb0-7de4955d03d6@googlegroups.com>
I want to define a forth variable only if  not defined. Basically I want to avoid the redefinition of variables. Is there any word in forth to support this? I'm thinking about something like this(hypothetical code).

: DefineOnly (varname)
   (Ifndef) varname
      variable varname
   (then)
;

[toc] | [next] | [standalone]


#56443

Fromminforth@arcor.de
Date2017-03-29 04:20 -0700
Message-ID<60ffebaf-082e-440e-a294-d9ebb64a38cc@googlegroups.com>
In reply to#56442
Am Mittwoch, 29. März 2017 11:39:23 UTC+2 schrieb Faisal:
> I want to define a forth variable only if  not defined. Basically I want to avoid the redefinition of variables. Is there any word in forth to support this? I'm thinking about something like this(hypothetical code).
> 
> : DefineOnly (varname)
>    (Ifndef) varname
>       variable varname
>    (then)
> ;

[UNDEFINED] VV [IF]
VARIABLE VV
[THEN]

[toc] | [prev] | [next] | [standalone]


#56444

FromFaisal <faisalm83@gmail.com>
Date2017-03-29 04:26 -0700
Message-ID<0d6a701e-6d94-4880-8242-8b09ebd793ec@googlegroups.com>
In reply to#56443
Is this can be written as forth word?

[toc] | [prev] | [next] | [standalone]


#56451

FromCoos Haak <htrofhc@gmail.com>
Date2017-03-29 14:58 +0200
Message-ID<ir9ahuw05px3.qz5gr8urz6n3.dlg@40tude.net>
In reply to#56444
Op Wed, 29 Mar 2017 04:26:54 -0700 (PDT) schreef Faisal:

> Is this can be written as forth word?

: newvar	( "ccc" -- )
        >in @ bl word find nip 0=
        if      >in ! variable
        else    drop
        then
    ;

groet Coos

[toc] | [prev] | [next] | [standalone]


#56452

FromAlex <alex@rivadpm.com>
Date2017-03-29 14:41 +0100
Message-ID<obgdcf$cjq$1@dont-email.me>
In reply to#56451
On 3/29/2017 13:58, Coos Haak wrote:
> Op Wed, 29 Mar 2017 04:26:54 -0700 (PDT) schreef Faisal:
>
>> Is this can be written as forth word?
>
> : newvar	( "ccc" -- )
>         >in @ bl word find nip 0=
>         if      >in ! variable
>         else    drop
>         then
>     ;
>
> groet Coos
>

With the caveat that if it doesn't declare a variable if there is a 
pre-existing word of the same name in the search order,

: foo 2 3 ;
newvar foo
10 foo !

is likely to cause the system to fail as it tries to store 2 into 3.

-- 
Alex

[toc] | [prev] | [next] | [standalone]


#56453

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2017-03-29 13:54 +0000
Message-ID<2017Mar29.155436@mips.complang.tuwien.ac.at>
In reply to#56451
Coos Haak <htrofhc@gmail.com> writes:
>: newvar	( "ccc" -- )
>        >in @ bl word find nip 0=
>        if      >in ! variable
>        else    drop
>        then
>    ;

Or, with the Forth-2012 [DEFINED]:

: newvar ( "name" -- )
  >in @ postpone [defined] 0= if
    dup >in ! variable then
  drop ;

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2016: http://www.euroforth.org/ef16/

[toc] | [prev] | [next] | [standalone]


#56463

FromMikael Nordman <oh2aun@gmail.com>
Date2017-03-29 12:06 -0700
Message-ID<4434727f-ff07-46a2-8960-f7c91c47b38b@googlegroups.com>
In reply to#56453
Or, with FlashForth

variable buu ok
variable buu ALREADY_DEFINED!

It does not allow redefinition of words.

[toc] | [prev] | [next] | [standalone]


#56464

FromCoos Haak <htrofhc@gmail.com>
Date2017-03-29 21:30 +0200
Message-ID<6eawn7ebmoqk$.1bcerjm2ipgcq.dlg@40tude.net>
In reply to#56463
Op Wed, 29 Mar 2017 12:06:01 -0700 (PDT) schreef Mikael Nordman:

> Or, with FlashForth
> 
> variable buu ok
> variable buu ALREADY_DEFINED!
> 
> It does not allow redefinition of words.

All the Forths that I have seen allows reusing a name.
The old word still retains its behavior, so what's dangerous with that?

groet Coos

[toc] | [prev] | [next] | [standalone]


#56465

FromMikael Nordman <oh2aun@gmail.com>
Date2017-03-29 12:53 -0700
Message-ID<1ecac4e8-3d06-4fd7-a791-4a02b7dede21@googlegroups.com>
In reply to#56464
I guess it just appeals to my sense of order and simplicity.
And I could skip the smudge handling.

Calling a word beeing defined, gives recursion. Or a loop to the beginning of the word by tail call->jmp replacement.
Although I have never really needed either one.

It alse prevents redefining EMPTY and WARM. These are always available to reset the Forth to a working state.

[toc] | [prev] | [next] | [standalone]


#56468

From"HAA" <someone@microsoft.com>
Date2017-03-30 10:03 +1000
Message-ID<obhek7$1upr$1@gioia.aioe.org>
In reply to#56442
Faisal wrote:
> I want to define a forth variable only if  not defined. Basically I want to avoid the
> redefinition of variables. Is there any word in forth to support this? I'm thinking
> about something like this(hypothetical code).
>
> : DefineOnly (varname)
>    (Ifndef) varname
>       variable varname
>    (then)
> ;

What if pre-existing 'varname' happens not to be a variable?  Bad luck?


[toc] | [prev] | [next] | [standalone]


#56470

FromJulian Fondren <julian.fondren@gmail.com>
Date2017-03-29 19:00 -0700
Message-ID<fd717cf9-f621-47b3-a649-63b3f42a5860@googlegroups.com>
In reply to#56468
On Wednesday, March 29, 2017 at 6:05:49 PM UTC-5, HAA wrote:
> Faisal wrote:
> > I want to define a forth variable only if  not defined. Basically I want to avoid the
> > redefinition of variables. Is there any word in forth to support this? I'm thinking
> > about something like this(hypothetical code).
> >
> > : DefineOnly (varname)
> >    (Ifndef) varname
> >       variable varname
> >    (then)
> > ;
> 
> What if pre-existing 'varname' happens not to be a variable?  Bad luck?

For sure. But it's Forth, what are you going to do?

ISTR there's some way to know if two words share a DOES> though.

What I would do is: exert control over the build system and have
documentation instead of tricks like this.

[toc] | [prev] | [next] | [standalone]


#56472

From"Elizabeth D. Rather" <erather@forth.com>
Date2017-03-29 16:35 -1000
Message-ID<7YudnQ_7tPhi80HFnZ2dnUU7-U_NnZ2d@supernews.com>
In reply to#56470
On 3/29/17 4:00 PM, Julian Fondren wrote:
> On Wednesday, March 29, 2017 at 6:05:49 PM UTC-5, HAA wrote:
>> Faisal wrote:
>>> I want to define a forth variable only if  not defined. Basically I want to avoid the
>>> redefinition of variables. Is there any word in forth to support this? I'm thinking
>>> about something like this(hypothetical code).
>>>
>>> : DefineOnly (varname)
>>>    (Ifndef) varname
>>>       variable varname
>>>    (then)
>>> ;
>>
>> What if pre-existing 'varname' happens not to be a variable?  Bad luck?
>
> For sure. But it's Forth, what are you going to do?
>
> ISTR there's some way to know if two words share a DOES> though.
>
> What I would do is: exert control over the build system and have
> documentation instead of tricks like this.

Any half-way civilized Forth will let you know when you compile your 
program if you have a name conflict, and you can look at the code and 
take appropriate action. That's really easy and quick, and saves a lot 
of misunderstandings (such as the collision not being a VARIABLE). The 
problem with the code above is that it's silent, so you don't know 
whether you got a new variable or a collision that can be catastrophic 
if not fixed. Silent bugs are the most dangerous!

Cheers,
Elizabeth

-- 
Elizabeth R. Conklin
FORTH, Inc.
6080 Center Drive, Suite 600
Los Angeles, CA  90045
USA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web