Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #41929 > unrolled thread
| Started by | Mark Wills <markwills1970@gmail.com> |
|---|---|
| First post | 2015-09-10 13:55 -0700 |
| Last post | 2015-09-14 14:40 -0700 |
| Articles | 20 — 9 participants |
Back to article view | Back to comp.lang.forth
? 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
| From | Mark Wills <markwills1970@gmail.com> |
|---|---|
| Date | 2015-09-10 13:55 -0700 |
| Subject | ? operator in Forth |
| Message-ID | <1f86d832-a0bd-4d35-9587-0b81afe11035@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Mark Wills <markwills1970@gmail.com> |
|---|---|
| Date | 2015-09-10 14:16 -0700 |
| Message-ID | <4121ada4-218e-4d14-9527-3a3d9bee5185@googlegroups.com> |
| In reply to | #41929 |
On Thursday, 10 September 2015 21:55:22 UTC+1, 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? To answer my own question: Yes. This works on my system and is muuuuch neater: : ?? postpone if ' , ; immediate : :: postpone else ' , postpone then ; immediate How cool is Forth? :-)
[toc] | [prev] | [next] | [standalone]
| From | "Elizabeth D. Rather" <erather@forth.com> |
|---|---|
| Date | 2015-09-10 11:58 -1000 |
| Message-ID | <wIidnTibfrCKYGzInZ2dnUU7-VmdnZ2d@supernews.com> |
| In reply to | #41930 |
On 9/10/15 11:16 AM, Mark Wills wrote: > On Thursday, 10 September 2015 21:55:22 UTC+1, 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? > > To answer my own question: Yes. > > This works on my system and is muuuuch neater: > > : ?? postpone if ' , ; immediate > : :: postpone else ' , postpone then ; immediate > > How cool is Forth? :-) Your first version uses literals 8 and 4 which implies implementation knowledge not only of cell size but also the structure of conditionals. The second is better, but still depends on the assumption that you have an ITC system and can comma down an xt and it will be executable. In some systems, the compiler would generate a jsr at that point, for example, even if you turn off the optimizer. Cheers, Elizabeth -- ================================================== Elizabeth D. Rather (US & Canada) 800-55-FORTH FORTH Inc. +1 310.999.6784 5959 West Century Blvd. Suite 700 Los Angeles, CA 90045 http://www.forth.com "Forth-based products and Services for real-time applications since 1973." ==================================================
[toc] | [prev] | [next] | [standalone]
| From | "Rod Pemberton" <boo@fasdfrewar.cdm> |
|---|---|
| Date | 2015-09-11 01:30 -0400 |
| Message-ID | <op.x4r9kjczyfako5@localhost> |
| In reply to | #41931 |
On Thu, 10 Sep 2015 17:58:44 -0400, Elizabeth D. Rather <erather@forth.com> wrote: > The second is better, but still depends on the assumption that you have > an ITC system and can comma down an xt and it will be executable. In > some systems, the compiler would generate a jsr at that point, for > example, even if you turn off the optimizer. For an ITC system, is there a way to simulate the *inability* to "comma down an xt" of non-ITC systems? What about other historical ITC behaviors people commonly expect? I.e., if I need my ITC system to be free from common assumptions about abilities of Forth words which are *not* standardized for ANS Forth, what words do I redefine and how? Rod Pemberton -- Just how many texting and calendar apps does humanity need?
[toc] | [prev] | [next] | [standalone]
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Date | 2015-09-11 06:17 +0000 |
| Message-ID | <2015Sep11.081749@mips.complang.tuwien.ac.at> |
| In reply to | #41945 |
"Rod Pemberton" <boo@fasdfrewar.cdm> writes:
>On Thu, 10 Sep 2015 17:58:44 -0400, Elizabeth D. Rather <erather@forth.com> wrote:
>
>> The second is better, but still depends on the assumption that you have
>> an ITC system and can comma down an xt and it will be executable. In
>> some systems, the compiler would generate a jsr at that point, for
>> example, even if you turn off the optimizer.
>
>For an ITC system, is there a way to simulate the *inability* to
>"comma down an xt" of non-ITC systems?
You could have a variable COMP-DP that remembers the position of HERE
after the last compiling word put something in the dictionary, and
each compiling word checks whether HERE is still at that position. If
not, there was a "," or ALLOT in between.
>What about other historical ITC behaviors people commonly expect?
create x ] a b c [
instead of
create x ' a , ' b , ' c '
Also, being able to read from the code and interpret it as xts.
Especially in combination with return-address manipulation.
>I.e., if I need my ITC system to be free from common assumptions
>about abilities of Forth words which are *not* standardized for
>ANS Forth, what words do I redefine and how?
I assume you mean that you want to make sure that *programs* don't
make such assumptions; otherwise I don't understand what you are
looking for.
One way to do it is to remember for each cell (or maybe byte) whether
it is code or data, have all storing words set that, and have all
loading words check that they load only data, and all execution check
that it executes only code. Lots of work, and it would consume
considerable memory and run-time.
An easier way is to run the program on a system that does not satisfy
the assumptions. After all, the point of such checks is to make the
program portable, so why not try out directly if it is portable?
- 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 2015: http://www.rigwit.co.uk/EuroForth2015/
[toc] | [prev] | [next] | [standalone]
| From | julian.fondren@endurance.com |
|---|---|
| Date | 2015-09-10 21:00 -0700 |
| Message-ID | <5fa7456a-312b-4921-9945-df9d1758fda8@googlegroups.com> |
| In reply to | #41930 |
On Thursday, September 10, 2015 at 4:16:38 PM UTC-5, Mark Wills wrote:
> This works on my system and is muuuuch neater:
>
> : ?? postpone if ' , ; immediate
> : :: postpone else ' , postpone then ; immediate
: :: ;
: ?? postpone if ' compile, postpone else
' ['] :: <> abort" invalid ??"
' compile, postpone then ; immediate
This requires the ::-part to be on the same line, however.
-- Julian
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2015-09-10 21:20 -0700 |
| Message-ID | <ad7670e8-2df9-492b-b8a3-9ee979b2617e@googlegroups.com> |
| In reply to | #41940 |
On Thursday, September 10, 2015 at 9:00:19 PM UTC-7, julian....@endurance.com wrote: > On Thursday, September 10, 2015 at 4:16:38 PM UTC-5, Mark Wills wrote: > > This works on my system and is muuuuch neater: > > > > : ?? postpone if ' , ; immediate > > : :: postpone else ' , postpone then ; immediate > > : :: ; > : ?? postpone if ' compile, postpone else > ' ['] :: <> abort" invalid ??" > ' compile, postpone then ; immediate > > This requires the ::-part to be on the same line, however. > > -- Julian Julian's code fails if either the word after ?? or the word after :: is a macro (such as defined with MACRO: in the novice-package). Also, Julian's code fails if the :: is missing and there is a special-case word there instead (such as semicolon etc.) because gForth aborts when tick is used on a special-case word. I said to use EVALUATE for a reason (actually, two reasons, listed above).
[toc] | [prev] | [next] | [standalone]
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Date | 2015-09-11 05:16 +0000 |
| Message-ID | <2015Sep11.071650@mips.complang.tuwien.ac.at> |
| In reply to | #41930 |
Mark Wills <markwills1970@gmail.com> writes:
>On Thursday, 10 September 2015 21:55:22 UTC+1, Mark Wills wrote:
>> Gauntlet: Can this be done in ANS?
>
>To answer my own question: Yes.
>
>This works on my system and is muuuuch neater:
>
>: ?? postpone if ' , ; immediate
>: :: postpone else ' , postpone then ; immediate
>
>How cool is Forth? :-)
As Elizabeth Rather writes, you would need COMPILE, instead of "," to
make this standard. And then it does not work as intended for
immediate words (whether they abort on ticking or not), so, as Hugh
Aguilar writes, EVALUATE is more appropriate here; and in this case it
is not problematic (unlike many other cases), because the string is
EVALUATEd exactly in the context in which it was written. So this
gives us:
: ?? postpone if ; immediate
: :: postpone else parse-name evaluate postpone then ; immediate
Example usage:
: foo [ 1e 2e ] ?? fliteral :: fliteral ; ok
Generated code:
see foo
: foo
IF 2.000000E0
ELSE 1.000000E0
THEN ; ok
Unlike the original, this supports not just 1 or more words between ??
and ::, but 0 or more words, and is simpler.
Overall, the only thing this gives us is that you do not need to type
THEN, but restricts you to having just one word after :: (whereas ? :
in C allows arbitrarily complex expressions).
- 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 2015: http://www.rigwit.co.uk/EuroForth2015/
[toc] | [prev] | [next] | [standalone]
| From | humptydumpty <ouatubi@gmail.com> |
|---|---|
| Date | 2015-09-12 01:11 -0700 |
| Message-ID | <7345f567-6bdb-407f-8df4-6dcbaf005c00@googlegroups.com> |
| In reply to | #41947 |
On Friday, September 11, 2015 at 8:31:13 AM UTC+3, Anton Ertl wrote:
> Mark Wills writes:
> >On Thursday, 10 September 2015 21:55:22 UTC+1, Mark Wills wrote:
> >> Gauntlet: Can this be done in ANS?
> >
> >To answer my own question: Yes.
> >
> >This works on my system and is muuuuch neater:
> >
> >: ?? postpone if ' , ; immediate
> >: :: postpone else ' , postpone then ; immediate
> >
> >How cool is Forth? :-)
>
> As Elizabeth Rather writes, you would need COMPILE, instead of "," to
> make this standard. And then it does not work as intended for
> immediate words (whether they abort on ticking or not), so, as Hugh
> Aguilar writes, EVALUATE is more appropriate here; and in this case it
> is not problematic (unlike many other cases), because the string is
> EVALUATEd exactly in the context in which it was written. So this
> gives us:
>
> : ?? postpone if ; immediate
> : :: postpone else parse-name evaluate postpone then ; immediate
>
> Example usage:
>
> : foo [ 1e 2e ] ?? fliteral :: fliteral ; ok
>
> Generated code:
>
> see foo
> : foo
> IF 2.000000E0
> ELSE 1.000000E0
> THEN ; ok
>
> Unlike the original, this supports not just 1 or more words between ??
> and ::, but 0 or more words, and is simpler.
>
> Overall, the only thing this gives us is that you do not need to type
> THEN, but restricts you to having just one word after :: (whereas ? :
> in C allows arbitrarily complex expressions).
>
> - 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 2015: http://www.rigwit.co.uk/EuroForth2015/
Hi!
I would use something like (looks clunky, but has some power):
$ gforth 2quest.fs
: ]?? ] 2>R 2>R ]] IF [[ 2R> evaluate ]] ELSE [[ 2R> evaluate ]] THEN [[
; IMMEDIATE
\ -- TESTS --
: t0 [ s" -1 ." s" 0 ." ]??
;
: t1 [
s\" -1 . [ s\" -1 .\" s\" 0 .\" ]??"
s\" 0 . [ s\" -1 .\" s\" 0 .\" ]??"
]??
;
s" 2quest.fs" slurp-file type see t0 see t1 cr bye
: t0
IF -1 .
ELSE 0 .
THEN ;
: t1
IF -1 .
IF -1 .
ELSE 0 .
THEN
ELSE 0 .
IF -1 .
ELSE 0 .
THEN
THEN ;
Have a nice day,
humptydumpty
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2015-09-12 17:14 -0700 |
| Message-ID | <7a2f16d6-14e4-449f-a79e-7d4e3d456282@googlegroups.com> |
| In reply to | #41966 |
On Saturday, September 12, 2015 at 1:11:28 AM UTC-7, humptydumpty wrote: > Hi! > I would use something like (looks clunky, but has some power): > > $ gforth 2quest.fs > : ]?? ] 2>R 2>R ]] IF [[ 2R> evaluate ]] ELSE [[ 2R> evaluate ]] THEN [[ > ; IMMEDIATE Here is yet another example of somebody working around the bug in ANS-Forth in which the control-flow stack may be the data-stack --- it is meaningless that it *may* be separate --- in all of these "may or may not" ambiguous circumstances, the programmer has to assume the worst-case scenario (in this case that the control-flow stack is the data-stack), and so SwiftForth is effectively the standard (because it is always the worst-case scenario). > \ -- TESTS -- > : t0 [ s" -1 ." s" 0 ." ]?? > ; > : t1 [ > s\" -1 . [ s\" -1 .\" s\" 0 .\" ]??" > s\" 0 . [ s\" -1 .\" s\" 0 .\" ]??" > ]?? > ; > s" 2quest.fs" slurp-file type see t0 see t1 cr bye > > : t0 > IF -1 . > ELSE 0 . > THEN ; > : t1 > IF -1 . > IF -1 . > ELSE 0 . > THEN > ELSE 0 . > IF -1 . > ELSE 0 . > THEN > THEN ; I don't know what S\" does, so T1 lost me, although I understood T0. T0 was clunky, but T1 went beyond clunky --- man, that is some ugly code! Mark Wills' ?? and :: wasn't so bad, although somewhat superfluous --- but this ]?? is pure Forth-200x style programming --- realistically, I'm never going to become a Forth-200x programmer!
[toc] | [prev] | [next] | [standalone]
| From | humptydumpty <ouatubi@gmail.com> |
|---|---|
| Date | 2015-09-13 02:18 -0700 |
| Message-ID | <bcc1dceb-9f7e-42df-baf5-09de588bbc52@googlegroups.com> |
| In reply to | #41978 |
On Sunday, September 13, 2015 at 3:14:20 AM UTC+3, hughag...@gmail.com wrote: > On Saturday, September 12, 2015 at 1:11:28 AM UTC-7, humptydumpty wrote: > > Hi! > > I would use something like (looks clunky, but has some power): > > > > $ gforth 2quest.fs > > : ]?? ] 2>R 2>R ]] IF [[ 2R> evaluate ]] ELSE [[ 2R> evaluate ]] THEN [[ > > ; IMMEDIATE > > Here is yet another example of somebody working around the bug in ANS-Forth in which the control-flow stack may be the data-stack --- it is meaningless that it *may* be separate --- in all of these "may or may not" ambiguous circumstances, the programmer has to assume the worst-case scenario (in this case that the control-flow stack is the data-stack), and so SwiftForth is effectively the standard (because it is always the worst-case scenario). > > > \ -- TESTS -- > > : t0 [ s" -1 ." s" 0 ." ]?? > > ; > > : t1 [ > > s\" -1 . [ s\" -1 .\" s\" 0 .\" ]??" > > s\" 0 . [ s\" -1 .\" s\" 0 .\" ]??" > > ]?? > > ; > > s" 2quest.fs" slurp-file type see t0 see t1 cr bye > > > > : t0 > > IF -1 . > > ELSE 0 . > > THEN ; > > : t1 > > IF -1 . > > IF -1 . > > ELSE 0 . > > THEN > > ELSE 0 . > > IF -1 . > > ELSE 0 . > > THEN > > THEN ; > > I don't know what S\" does, so T1 lost me, although I understood T0. > > T0 was clunky, but T1 went beyond clunky --- man, that is some ugly code! > > Mark Wills' ?? and :: wasn't so bad, although somewhat superfluous --- but this ]?? is pure Forth-200x style programming --- realistically, I'm never going to become a Forth-200x programmer! Hi! Word S\" permits characters inside to be escaped. In T1 case ']??' macro is applied to himself. I know is ugly code, but can do things :) I'm not bothered so much about control-stack=data-stack, is relative easy to re-write control words to save and restore control-stack to/from another stack (done it on gforth, but not used yet). As for Forth-200x style, I don't know if is compliant with or not. Standards I'll read for corner case handling. Have a nice day, humptydumpty
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2015-09-10 15:21 -0700 |
| Message-ID | <7eb81011-ca6c-4e61-b28b-5f0e43a9afe7@googlegroups.com> |
| In reply to | #41929 |
On Thursday, September 10, 2015 at 1:55:22 PM UTC-7, 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
This is equivalent to:
: test ( flag -- ) if yes else no then ." done" ;
So, you could write your ?? extract the string after the ?? and extract the string after the :: and construct a string to be given to EVALUATE. In this example, the string given to EVALUATE would be: if yes else no then
Use CONCAT in the novice-package to concatenate your strings.
All that you are accomplishing here is to get rid of the THEN --- you also replace IF with ?? and ELSE with ::
The end result is only one less word in your source-code --- it doesn't seem worthwhile to me --- I never used the ? in C programming either.
There is a book out called: "JavaScript: the Good Parts." This book advocates using a reduced version of JavaScript. I did the same in C --- the ? operator that you think is neat was one of the many bad parts of C that I purposely ignored. Another "feature" of C that I ignored is that in certain circumstances the { } brackets can be discarded. Almost every C programmer uses a reduced version of C containing only the "good parts" --- but they don't all agree on what are good parts and what are bad parts.
I'm reminded of the book, "The Life of Pi." The boy says that most animals, even if they are ugly, have at least a few redeeming features. The hyena however, doesn't have any good parts...
[toc] | [prev] | [next] | [standalone]
| From | "Rod Pemberton" <boo@fasdfrewar.cdm> |
|---|---|
| Date | 2015-09-11 01:35 -0400 |
| Message-ID | <op.x4r9s30kyfako5@localhost> |
| In reply to | #41929 |
On Thu, 10 Sep 2015 16:55:19 -0400, Mark Wills <markwills1970@gmail.com> wrote: > I quite like C's ? operator, so I knocked up one in Forth. It's a useless C feature, BTW, IMO. It's not needed to code anything. It's convenient in printf() statements on rare occasions. Rod Pemberton -- Just how many texting and calendar apps does humanity need?
[toc] | [prev] | [next] | [standalone]
| From | humptydumpty <ouatubi@gmail.com> |
|---|---|
| Date | 2015-09-10 22:58 -0700 |
| Message-ID | <2aec57ea-e70c-4651-b43d-8318f9ce9f3d@googlegroups.com> |
| In reply to | #41929 |
On Thursday, September 10, 2015 at 11:55:22 PM UTC+3, 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? Hi! If is a need to move from compile-time to run-time, and 'tick' or 'bracket-tick' is OK to use: : ?? IF swap THEN drop execute ; \ --- : test ( f -- ) ['] no ['] yes rot ?? ; \ or at run-time ' no ' yes -1 ?? Have a nice day, humptydumpty
[toc] | [prev] | [next] | [standalone]
| From | Hans Bezemer <the.beez.speaks@gmail.com> |
|---|---|
| Date | 2015-09-11 18:48 +0200 |
| Message-ID | <55f305a4$0$23791$e4fe514c@news.xs4all.nl> |
| In reply to | #41929 |
Mark Wills wrote: The Factor way: : NO! ." no" ; ok : YES! ." yes" ; ok : ?? 0= if swap then drop execute ; ' YES! ' NO! true ?? yes ok ' YES! ' NO! false ?? no ok Add [: and ;] and you're C - in an RPN kind of syntax, of course. Hans Bezemer
[toc] | [prev] | [next] | [standalone]
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Date | 2015-09-11 16:54 +0000 |
| Message-ID | <2015Sep11.185427@mips.complang.tuwien.ac.at> |
| In reply to | #41959 |
Hans Bezemer <the.beez.speaks@gmail.com> writes:
>Mark Wills wrote:
>
>The Factor way:
>: NO! ." no" ; ok
>: YES! ." yes" ; ok
>: ?? 0= if swap then drop execute ;
>
>' YES! ' NO! true ?? yes ok
>' YES! ' NO! false ?? no ok
>
>Add [: and ;] and you're C - in an RPN kind of syntax, of course.
No, you are Postscript (or, as originally written, Factor):
GS>true {(yes) =} {(no) =} ifelse
yes
GS>false {(yes) =} {(no) =} ifelse
no
The Postscript stack effect is better, BTW.
- 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 2015: http://www.rigwit.co.uk/EuroForth2015/
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2015-09-11 21:32 -0700 |
| Message-ID | <43c5457f-1dd7-4b86-8b98-cf3fb780e973@googlegroups.com> |
| In reply to | #41960 |
On Friday, September 11, 2015 at 9:57:49 AM UTC-7, Anton Ertl wrote:
> Hans Bezemer <the.beez.speaks@gmail.com> writes:
> >Mark Wills wrote:
> >
> >The Factor way:
> >: NO! ." no" ; ok
> >: YES! ." yes" ; ok
> >: ?? 0= if swap then drop execute ;
> >
> >' YES! ' NO! true ?? yes ok
> >' YES! ' NO! false ?? no ok
> >
> >Add [: and ;] and you're C - in an RPN kind of syntax, of course.
>
> No, you are Postscript (or, as originally written, Factor):
>
> GS>true {(yes) =} {(no) =} ifelse
> yes
> GS>false {(yes) =} {(no) =} ifelse
> no
>
> The Postscript stack effect is better, BTW.
>
> - anton
On the FMITE I have this:
|IF could be used like this:
: test \ a b flag -- a+b|a-b \ FLAG is true for non-negative and false for negative
{ + } { - } |if ;
Using traditional Forth this would be:
: test \ a b flag -- a+b|a-b
if + else - then ;
I said in the document that this was possible, but that I didn't consider it to be a good idea:
1.) It is less readable than the Forth version, especially if the quotations are multi-line.
2.) It is not possible to EXIT out of a quotation and get out of the parent function.
3.) Meta-compiling is difficult or impossible (I haven't determined which yet).
[toc] | [prev] | [next] | [standalone]
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2015-09-14 18:13 +0000 |
| Message-ID | <mt72os$9ps$1@dont-email.me> |
| In reply to | #41929 |
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
--
The report card by the American Society of Civil Engineers showed the national
infrastructure a single grade above failure, a step from declining to the point
where everyday things simply stop working the way people expect them to. ---
washingtonpost.com/local/trafficandcommuting/us-infrastructure-gets-d-in-annual-report/2013/03/19/c48cb010-900b-11e2-9cfd-36d6c9b5d7ad_story.html
[toc] | [prev] | [next] | [standalone]
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2015-09-14 18:32 +0000 |
| Message-ID | <mt73r1$en4$1@dont-email.me> |
| In reply to | #42000 |
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 }
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2015-09-14 14:40 -0700 |
| Message-ID | <8ca2b1c5-d566-4b58-8b68-f49d9c12c32e@googlegroups.com> |
| In reply to | #42001 |
On Monday, September 14, 2015 at 11:33:59 AM UTC-7, WJ wrote:
> Shorter:
>
> : ?: { rot not #swap times drop perform }
On the FMITE, |IF can be defined like this:
s" |if" header
HUFF12
s" |if" header21
LINK
>GP21
TOR
TOR
ZERO? EXECUTE-GP UNLINK ??
DRFETCH>GP
EXECUTE-GP
UNLINK
Note that these are all one-byte primitives. Also note that there is no stack-juggling. The FMITE is designed for quotations and code working with quotations is efficient and short.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.forth
csiph-web