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


Groups > comp.lang.forth > #135471

Re: dynamic scoping in Forth in one line of code

Newsgroups comp.lang.forth
References <87bjamc5xh.fsf@debian>
From albert@spenarnc.xs4all.nl
Subject Re: dynamic scoping in Forth in one line of code
Message-ID <nnd$4aef30ef$417a695d@6608074c8df9df23> (permalink)
Organization www.abavia.com
Date 2026-08-29 12:10 +0200

Show all headers | View raw


In article <87bjamc5xh.fsf@debian>,
Kragen Javier Sitaker  <kragen@canonical.org> wrote:
<SNIP>
>Van der Horst’s `co` operator
>-----------------------------
>
>In <https://home.hccnet.nl/a.w.m.van.der.horst/forthlecture6.html> Van
>der Horst implements stackless coroutines in Forth by swapping the top
>two items on the return stack, so that its caller’s caller will return
>to the point in its caller that called `co`, assuming they aren’t using
>the return stack for anything else:
>
>    : co  2r> >r >r ;
>
>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 ! ;
 4 ( Add a , after 4 digits )
 5  : 4?  1+ 4 MOD 0= IF &, HOLD THEN ;
 6  : 3?  1+ 3 MOD 0= IF &, HOLD THEN ;
 7 ( Generate string with hex format of DOUBLE of LEN digits)
 8  : (DH.) HEX: <# 1- 0 ?DO # I 4? LOOP # #> ;
 9  : B.  S>D 2 (DH.) TYPE ; ( print BYTE in hex )
 10  : H.  S>D 2 CELLS (DH.) TYPE ; ( print SINGLE in hex )
 11  : DH. 4 CELLS (DH.) TYPE ; (  print DOUBLE in hex )
 12 (  print DOUBLE in decimal )
 13  : DEC. 5 CELLS DEC: <# 1- 0 ?DO # I 3? LOOP # #> TYPE ;
 14  : BASE?  BASE @ B. ;   ( print true value of base)

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.

[ Decorator's 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. ]

<SNIP>
>
>Kragen
-- 
The Chinese government is satisfied with its military superiority over USA.
The next 5 year plan has as primary goal to advance life expectancy
over 80 years, like Western Europe.

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