Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Kragen Javier Sitaker Newsgroups: comp.lang.forth Subject: Re: dynamic scoping in Forth in one line of code Date: Sat, 29 Aug 2026 11:17:42 -0300 Organization: Primarily biological and memetic Lines: 60 Message-ID: <874igd9fjt.fsf@debian> References: <87bjamc5xh.fsf@debian> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Sat, 29 Aug 2026 14:20:01 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3471279"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/BziyrYEzcfiwIZ1sYNZbX"; posting-host="19c753fba95970cf1a99e4804f17687c" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) Cancel-Lock: sha1:QP69UwMo1gI95X074QtrylK86WI= sha1:z10jFVgB5jkLxxrkTY6fIsfsLNA= sha256:x5Gc5znDRzgLvYzd0KL13KqmZ9hsURKhis+/WHomVlU= sha1:ySv9JwTpVaVsZ4+ZBMUNuTnm3HU= sha256:bbcdtnnQB7T71hU3lhgCNXLPqAfOD6hCkRXgEdrlRHs= Xref: csiph.com comp.lang.forth:135478 albert@spenarnc.xs4all.nl (Albert van der Horst) writes: > In article <87bjamc5xh.fsf@debian>, > Kragen Javier Sitaker 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