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


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

"Control structure mismatch" with simple if-else-then in SwiftForth

Started bychristopherdumas@gmail.com
First post2017-06-14 07:22 -0700
Last post2017-06-17 14:55 +1000
Articles 3 on this page of 23 — 13 participants

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


Contents

  "Control structure mismatch" with simple if-else-then in SwiftForth christopherdumas@gmail.com - 2017-06-14 07:22 -0700
    Re: "Control structure mismatch" with simple if-else-then in SwiftForth Cecil Bayona <cbayona@cbayona.com> - 2017-06-14 09:47 -0500
      Re: "Control structure mismatch" with simple if-else-then in SwiftForth "Elizabeth D. Rather" <erather@forth.com> - 2017-06-14 08:47 -1000
        Re: "Control structure mismatch" with simple if-else-then in SwiftForth hughaguilar96@gmail.com - 2017-06-16 11:22 -0700
          Re: "Control structure mismatch" with simple if-else-then in SwiftForth albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-06-16 20:31 +0200
            Re: "Control structure mismatch" with simple if-else-then in SwiftForth "Elizabeth D. Rather" <erather@forth.com> - 2017-06-16 16:29 -1000
          Re: "Control structure mismatch" with simple if-else-then in SwiftForth Cecil Bayona <cbayona@cbayona.com> - 2017-06-16 15:38 -0500
            Re: "Control structure mismatch" with simple if-else-then in SwiftForth Julian Fondren <julian.fondren@gmail.com> - 2017-06-16 18:12 -0700
            Re: "Control structure mismatch" with simple if-else-then in SwiftForth peter.m.falth@gmail.com - 2017-06-17 15:10 -0700
              Re: "Control structure mismatch" with simple if-else-then in SwiftForth "Robert L." <No_spamming@noWhere_7073.org> - 2017-06-18 00:50 +0000
    Re: "Control structure mismatch" with simple if-else-then in SwiftForth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-06-14 14:47 +0000
      Re: "Control structure mismatch" with simple if-else-then in SwiftForth "Robert L." <No_spamming@noWhere_7073.org> - 2017-06-14 16:16 +0000
        Re: "Control structure mismatch" with simple if-else-then in SwiftForth julian.fondren@endurance.com - 2017-06-14 09:34 -0700
        Re: "Control structure mismatch" with simple if-else-then in SwiftForth "HAA" <someone@microsoft.com> - 2017-06-16 10:20 +1000
          Re: "Control structure mismatch" with simple if-else-then in SwiftForth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-06-17 17:50 +0000
            Re: "Control structure mismatch" with simple if-else-then in SwiftForth Julian Fondren <julian.fondren@gmail.com> - 2017-06-17 12:11 -0700
    Re: "Control structure mismatch" with simple if-else-then in SwiftForth hum9 <obi65537@gmail.com> - 2017-06-14 18:12 +0300
      Re: "Control structure mismatch" with simple if-else-then inSwiftForth "Robert L." <No_spamming@noWhere_7073.org> - 2017-06-14 16:21 +0000
        Re: "Control structure mismatch" with simple if-else-then inSwiftForth Julian Fondren <julian.fondren@gmail.com> - 2017-06-14 09:37 -0700
          Re: "Control structure mismatch" with simple if-else-then inSwiftForth christopherdumas@gmail.com - 2017-06-14 10:48 -0700
    Re: "Control structure mismatch" with simple if-else-then in SwiftForth Julian Fondren <julian.fondren@gmail.com> - 2017-06-14 09:10 -0700
    Re: "Control structure mismatch" with simple if-else-then in SwiftForth The Beez <the.beez.speaks@gmail.com> - 2017-06-16 01:47 -0700
    Re: "Control structure mismatch" with simple if-else-then in SwiftForth "HAA" <someone@microsoft.com> - 2017-06-17 14:55 +1000

Page 2 of 2 — ← Prev page 1 [2]


#60739

FromJulian Fondren <julian.fondren@gmail.com>
Date2017-06-14 09:10 -0700
Message-ID<e43a2830-65a2-4770-8279-ea3dc67efef8@googlegroups.com>
In reply to#60722
On Wednesday, June 14, 2017 at 9:22:31 AM UTC-5, christop...@gmail.com wrote:
> So I'm thinking that control structures don't work unless they are in definitions? Is this correct at all? I seem to vaguely remember something about this from the GForth manual. Thanks!

That's correct. There are similar words like [IF] [ELSE] [THEN] , and
what they do is very different.

Very roughly:

  IF <- compile a conditional branch to <location undecided>
  ELSE <- compile an unconditional branch to <location undecided>
  THEN <- patch the previous locations so that those branches are now
          to the position after the THEN

  [IF] on false, start parsing words and throwing them away, REFILLing
       the input buffer, until [ELSE] or [THEN] are observed
       on true, do nothing; let Forth handle the succeeding code
       normally
  [ELSE] start parsing words and throwing them away, REFILLing the
         input buffer, until [THEN] is observed
  [THEN] do nothing

In a pinch you can use a temporary definition to do anything you want
interactively (with some caveats about dictionary use):

  :noname 0 2 rnd 1 = if 1+ else 2 * then ; execute

Or indeed you could just define a definition and use it by name.

Not everyone is happy with this limitation, and some Forths have
optional control flow words that do work as you'd expect
interactively, but it's not that big a deal. In the end Forth lets you
do more at compile time than other languages do, and that's the more
useful advantage.

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


#60809

FromThe Beez <the.beez.speaks@gmail.com>
Date2017-06-16 01:47 -0700
Message-ID<977d7b5d-2a97-4689-ba90-0a57aae67e14@googlegroups.com>
In reply to#60722
On Wednesday, June 14, 2017 at 4:22:31 PM UTC+2, christop...@gmail.com wrote:
> I'm new to Forth and I've recently downloaded the Free Trial of SwiftForth, which I am exceedingly happy with, because it integrates with Emacs and is one of the nicest, most dynamic and interactive languages I've used. However, there is just a tiny problem: I can't get a simple if-else-then to work in the REPL. Here is my code so far:
> 
> 0 2 rnd 1 = if 1+ else 2 * then       Control structure mismatch
> 
> It seems simple (and correct) to me. I'm sure its probably something simple that I'm missing, but there's no info on the internet on "Control structure mismatch"es. On a side note, this:
> 
> : TEST ( n -- ) dup 0= IF drop ELSE process THEN ;
> 
> seems to work, but this:
> 
> 7 dup 0= IF drop ELSE process THEN
> 
> Does not. So I'm thinking that control structures don't work unless they are in definitions? Is this correct at all? I seem to vaguely remember something about this from the GForth manual. Thanks!

In 4tH you can. It's just not interactive in the usual Forth way. However, you should be able to interface it with Emacs by setting the EDIT4TH variable. Then you start 4tH, enter Emacs by entering "e", save your source, quit Emacs as usual, run the source by entering "r". Much like early TurboPascal versions.

Hans Bezemer

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


#60884

From"HAA" <someone@microsoft.com>
Date2017-06-17 14:55 +1000
Message-ID<oi2cnu$14ja$1@gioia.aioe.org>
In reply to#60722
christopherdumas@gmail.com wrote:
> I'm new to Forth and I've recently downloaded the Free Trial of SwiftForth, which I am
> exceedingly happy with, because it integrates with Emacs and is one of the nicest, most
> dynamic and interactive languages I've used. However, there is just a tiny problem: I
> can't get a simple if-else-then to work in the REPL. Here is my code so far:
>
> 0 2 rnd 1 = if 1+ else 2 * then       Control structure mismatch
>
> It seems simple (and correct) to me. I'm sure its probably something simple that I'm
> missing, but there's no info on the internet on "Control structure mismatch"es. On a
> side note, this:
>
> : TEST ( n -- ) dup 0= IF drop ELSE process THEN ;
>
> seems to work, but this:
>
> 7 dup 0= IF drop ELSE process THEN
>
> Does not. So I'm thinking that control structures don't work unless they are in
> definitions? Is this correct at all? I seem to vaguely remember something about this
> from the GForth manual. Thanks!

FWIW the "Control structure mismatch" message derives from THEN wanting
to see an appropriate value on the data stack placed there by IF or ELSE.
The other operators you used (1+ 2 * etc) messes that up.  Had you just
written  IF ELSE THEN  there would be no message (but neither would it do
anything :)

Ideally SwiftForth would have 'compile-only' words such as IF check whether
the system is in compilation state and issue an error message otherwise.
In Fig-Forth that word was called ?COMP.


[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

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


csiph-web