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


Groups > comp.lang.forth > #42001

Re: ? operator in Forth

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: ? operator in Forth
Date 2015-09-14 18:32 +0000
Organization A noiseless patient Spider
Message-ID <mt73r1$en4$1@dont-email.me> (permalink)
References <1f86d832-a0bd-4d35-9587-0b81afe11035@googlegroups.com> <mt72os$9ps$1@dont-email.me>

Show all headers | View raw


WJ wrote:

> Mark Wills wrote:
> 
> > I quite like C's ? operator, so I knocked up one in Forth.
> > 
> > Since there's no way to determine the end of the false choice
> > you can only put a single call in the true and false positions.
> > 
> > Like this:
> > flag ?? wordToCallIfTrue :: wordToCallIfFalse ... ... ...
> > 
> > I used ?? as ? is used in Forth to display variables' contents.
> > 
> > Test:
> > : yes ." Yes " ;
> > : no  ." No " ;
> > : test ( flag -- ) ?? yes :: no   ." done" ;
> > 0 test No done ok:0
> > 1 test Yes done ok:0 
> > 
> > Quite neat.
> > 
> > see test
> > AE26 | DOCOL ZBRANCH AE32 yes BRANCH AE34 no
> > AE34 | (S") done TYPE 
> > AE3E | EXIT
> > 
> > Implementation:
> > : ?? compile 0branch here 8 + , ' ,   compile branch here 4 + , ; immediate
> > : :: ; immediate \ syntactic sugar
> > 
> > Gauntlet: Can this be done in ANS?
> 
> Oforth:
> 
> Oforth has "quotations"; these make the solution trivial.
> 
> : ?: { rot sgn abs #swap times nip perform }
> 
> > .s
> Empty
> ok
> > -22 #[ "Success!" println] #["Failure." println] ?:
> Success!
> ok
> > 0 #[ "Success!" println] #["Failure." println] ?:
> Failure.
> ok
> > .s
> Empty
> ok

Shorter:

: ?: { rot not #swap times drop perform }

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

? operator in Forth Mark Wills <markwills1970@gmail.com> - 2015-09-10 13:55 -0700
  Re: ? operator in Forth Mark Wills <markwills1970@gmail.com> - 2015-09-10 14:16 -0700
    Re: ? operator in Forth "Elizabeth D. Rather" <erather@forth.com> - 2015-09-10 11:58 -1000
      Re: ? operator in Forth "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-11 01:30 -0400
        Re: ? operator in Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-09-11 06:17 +0000
    Re: ? operator in Forth julian.fondren@endurance.com - 2015-09-10 21:00 -0700
      Re: ? operator in Forth hughaguilar96@gmail.com - 2015-09-10 21:20 -0700
    Re: ? operator in Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-09-11 05:16 +0000
      Re: ? operator in Forth humptydumpty <ouatubi@gmail.com> - 2015-09-12 01:11 -0700
        Re: ? operator in Forth hughaguilar96@gmail.com - 2015-09-12 17:14 -0700
          Re: ? operator in Forth humptydumpty <ouatubi@gmail.com> - 2015-09-13 02:18 -0700
  Re: ? operator in Forth hughaguilar96@gmail.com - 2015-09-10 15:21 -0700
  Re: ? operator in Forth "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-11 01:35 -0400
  Re: ? operator in Forth humptydumpty <ouatubi@gmail.com> - 2015-09-10 22:58 -0700
  Re: ? operator in Forth Hans Bezemer <the.beez.speaks@gmail.com> - 2015-09-11 18:48 +0200
    Re: ? operator in Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-09-11 16:54 +0000
      Re: ? operator in Forth hughaguilar96@gmail.com - 2015-09-11 21:32 -0700
  Re: ? operator in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-09-14 18:13 +0000
    Re: ? operator in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-09-14 18:32 +0000
      Re: ? operator in Forth hughaguilar96@gmail.com - 2015-09-14 14:40 -0700

csiph-web