Remix.run Logo
kragen 3 days ago

It's funny that you say that, because from my point of view, control flow in Forth is one of its strong points over assembly language, because it has properly nesting control-flow structures. The syntax is a little strange; the literal translation of

    if (siz != 0) *d = '\0';
is (untested)

    siz @ 0<>  if 0 d @ c!  then
when what you'd normally expect is something like

    if  siz @ 0<>  then  0 d @ c!  fi
but that's sort of a question of RPN. At least else comes where you expect it, even though that makes then even stranger; we can translate

    if (key < tree->key) {
      tree++;
    } else {
      tree = tree->right;
    }
literally as (untested)

    key @ tree ->key @ <
      if   kvpairsize tree +!
      else tree ->right @ tree !
    then
Now, if you were comparing Forth to Lua or C here, I would agree: this is far less obvious, in the sense that it's completely unfamiliar and requires you to learn Forth's idiosyncratic way of writing things. But in assembly language? GCC generates, in context, the following assembly code for the C if-else statement above:

    addhi r0, r0, #8
    bhi .L5
    ldr r0, [r0, #4]
    b .L6
This is using the flags from a preceding equality comparison, and the branch target labels are earlier in the enclosing loop. You can not tell me that this is "obvious" if you don't know ARM assembly!

From a certain point of view, Forth is just what you get if you take assembly language and look for the simplest way to add properly nesting expressions, properly nesting control structures, subroutines with arguments and return values, arbitrary compile-time computation, an interactive debugging environment, a scripting language, disk storage, and multithreading.

bxparks 3 days ago | parent [-]

I kinda know how it works at the user-level, I meant to write that I don't understand how control-flow is implemented in Forth. I say more about that in my reply to a sibling comment: https://news.ycombinator.com/item?id=45334556

kragen 3 days ago | parent [-]

Oh! I see. I've commented there.