[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [gforth] words backwards
From: |
Anton Ertl |
Subject: |
Re: [gforth] words backwards |
Date: |
Sat, 20 May 2017 17:53:30 +0200 |
User-agent: |
Mutt/1.5.18 (2008-05-17) |
On Fri, May 19, 2017 at 06:24:13PM +0200, Bernd Paysan wrote:
> Am Freitag, 19. Mai 2017, 18:09:30 CEST schrieb Dennis Ruffer:
> > Since Forth dictionaries are typically, singly linked lists, it would take
> > memory and time to reverse the order.
>
> Yes, but on a desktop system, we actually have that memory. And time is even
> less important. This does it, by converting the list into an array and then
> stepping backwards through that array:
>
> Variable words[]
>
> : words ( -- )
> [: words[] dup $[]# swap $[] ! true ;] context @ traverse-wordlist
> source nip 1+ words[] $@ bounds cell- swap cell- U-DO
> I @ .word
> cell -LOOP drop
> words[] $free ;
And here's a version that uses the data stack as intermediate storage
(with the default data stack size, that is good for 4K words; if you
have more, use gforth -d 1M (or whatever you need)):
: words ( -- )
0 ['] true context @ traverse-wordlist 0 begin
over while
swap .word repeat
2drop ;