JimBoyd wrote:
Code:
: FORK ( -- FLAG )
TRUE R@ 1+ ENTER FALSE ;
Ooooooo... this one is fun. I wanted to print out some binary numbers so I tried:
Code:
: enter 1- >r ; ok
: fork true R@ 1+ enter false ; ok
fork . 0 ok
: test fork . ; ok
test -1 0 ok
: print01 if 1 else 0 then 1 u.r ; ok
5 print01 1 ok
0 print01 0 ok
: testmany fork print01 fork print01 fork print01 cr ; ok
testmany 111
0
01
0
011
0
01
0
ok
print01 prints a single 0 or 1 (using u.r so as to not print a space - I forget who showed me that, but it was someone here so thanks!).
testmany is supposed to print all combinations of 3-digit binary values. What I actually got was not what I was expecting, but after thinking about it I realized it was exactly what I asked for. The issue is that it has to do all of the right hand forks before it will re-evaluate the left hand fork. I have the correct number of CRs, but not all of the digits for each line. Here is my "fix":
Code:
variable dig1 ok
variable dig2 ok
variable dig3 ok
: printdigs dig1 @ print01 dig2 @ print01 dig3 @ print01 cr ; ok
printdigs 000
ok
: testmany fork dig1 ! fork dig2 ! fork dig3 ! printdigs ; redefined testmany ok
testmany 111
110
101
100
011
010
001
000
ok
I never would have thought to write a FORK word for a non-multitasking system, but I can see where it might be useful.