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


Groups > comp.lang.forth > #135478

Re: dynamic scoping in Forth in one line of code

From Kragen Javier Sitaker <kragen@canonical.org>
Newsgroups comp.lang.forth
Subject Re: dynamic scoping in Forth in one line of code
Date 2026-08-29 11:17 -0300
Organization Primarily biological and memetic
Message-ID <874igd9fjt.fsf@debian> (permalink)
References <87bjamc5xh.fsf@debian> <nnd$4aef30ef$417a695d@6608074c8df9df23>

Show all headers | View raw


albert@spenarnc.xs4all.nl (Albert van der Horst) writes:
> In article <87bjamc5xh.fsf@debian>,
> Kragen Javier Sitaker  <kragen@canonical.org> wrote:
>>Van der Horst’s `co` operator
>>-----------------------------
>>(...)
>>This is very similar in its effect to Golang’s `defer`, so it occurred
>>to me that you could use it to restore saved variable values.
>
> Indeed it is (note the tricky use of the return stack.)
>  0 (  H. B. DH. DEC. BASE? HEX: DEC: ) \ AvdH B6dec18
>  1   \ Switch to hex for the duration of the definition.
>  2 : HEX:    R> BASE @ >R  >R HEX CO R> BASE ! ;
>  3 : DEC:    R> BASE @ >R  >R DECIMAL CO R> BASE ! ;

What a pleasant surprise to receive a response from the man himself!

Yes, you could see the `let!` word as a generalization of your `HEX:`
and `DEC:`.  Are there other standard Forth `variable`s that it’s useful
for?

>  5  : 4?  1+ 4 MOD 0= IF &, HOLD THEN ;
>  6  : 3?  1+ 3 MOD 0= IF &, HOLD THEN ;

I’m guessing `&,` is how Lina or ciforth spells `[char] ,`?

>  13  : DEC. 5 CELLS DEC: <# 1- 0 ?DO # I 3? LOOP # #> TYPE ;

This is very nice!  I hadn’t realized comma-separation was quite so
easy.  But I don’t fully understand how it works.

> It is also useful for decorators. A decorator is a debugging
> function executed before each call of a high level function.
> : dec ." BEFORE " .S ;
> ' dec ' myfunction decorated
>
> Useful as it, is CO enhances this further:
> : dec ." BEFORE " .S CO ." AFTER ".S ;
> Now the decorator prints the stack before and after myfunction
> is executed.
>
> [ Decorators are especially useful for Heisenbugs, where you can't
> add inline output to a program where the bug disappears the moment
> you change the addresses of functions. ]

In the Lisp world, this is known as “advice” or “method combination”; I
I’m guessing that `decorated` is specific to Lina or ciforth?  I can’t
see how to implement it portably.  I suppose the use of `co` here
depends on `decorated` having already placed the entry point to
`myfunction` on the return stack before `dec` is invoked?

I currently have some advice placed on the nntp-send-command function in
my Emacs because I was trying to figure out why the Gnus newsreader,
which runs under Emacs, was failing to show me the full list of
newsfroups.  This adds debugging output to the `*trace-output*` buffer
every time nntp-send-command is called or returns:

    (trace-function-background 'nntp-send-command)

Kragen

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


Thread

dynamic scoping in Forth in one line of code Kragen Javier Sitaker <kragen@canonical.org> - 2026-08-28 18:04 -0300
  Re: dynamic scoping in Forth in one line of code Paul Rubin <no.email@nospam.invalid> - 2026-08-28 18:12 -0700
    Re: dynamic scoping in Forth in one line of code Kragen Javier Sitaker <kragen@canonical.org> - 2026-08-29 10:50 -0300
  Re: dynamic scoping in Forth in one line of code albert@spenarnc.xs4all.nl - 2026-08-29 12:10 +0200
    Re: dynamic scoping in Forth in one line of code Kragen Javier Sitaker <kragen@canonical.org> - 2026-08-29 11:17 -0300
      Re: dynamic scoping in Forth in one line of code albert@spenarnc.xs4all.nl - 2026-08-30 12:49 +0200
        Re: dynamic scoping in Forth in one line of code Kragen Javier Sitaker <kragen@canonical.org> - 2026-09-01 20:38 -0300
          Re: dynamic scoping in Forth in one line of code Paul Rubin <no.email@nospam.invalid> - 2026-09-01 17:15 -0700
            coroutines (was Re: dynamic scoping in Forth in one line of code) Kragen Javier Sitaker <kragen@canonical.org> - 2026-09-03 20:38 -0300
              Re: coroutines (was Re: dynamic scoping in Forth in one line of code) dxf <dxforth@gmail.com> - 2026-09-04 15:24 +1000
                dynamic scoping in Forth in one line of code (was Re: coroutines) Kragen Javier Sitaker <kragen@canonical.org> - 2026-09-04 13:04 -0300
                Re: dynamic scoping in Forth in one line of code (was Re: coroutines) dxf <dxforth@gmail.com> - 2026-09-05 02:59 +1000

csiph-web