Re: [Flashforth-devel] jumping without a return
Brought to you by:
oh2aun
From: Mikael N. <mik...@fl...> - 2021-04-22 16:20:35
|
You can use value or defer for mutual recursion. flash 0 value foo ok<$,flash> flash 0 value bar ok<$,flash> :noname ." foo " bar execute ; is foo ok<$,ram> :noname ." bar " foo execute ; is bar ok<$,ram> bar execute<enter> bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo Goes on forever. foo dis<enter> 3ab2 940e 035f call (s" 3ab6 6604 3ab8 6f6f 3aba 0320 3abc 940e 0351 call type 3ac0 dff4 rcall bar 3ac2 940c 01a0 jmp execute bar dis<enter> 3ac6 940e 035f call (s" 3aca 6204 3acc 7261 3ace 0320 3ad0 940e 0351 call type 3ad4 dfe3 rcall foo 3ad6 940c 01a0 jmp execute This also works with defer instead of value. flash defer foo ok<$,flash> flash defer bar ok<$,flash> :noname ." foo " bar ; is foo ok<$,ram> :noname ." bar " foo ; is bar ok<$,ram> bar<enter> bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo Goes on forever. Calling foo or bar in the middle of the word would overflow the return stack. Like in Scheme you can tail jump yourself using FlashForth. : foo ( n -- ) dup . 1+ dup 100 = if drop exit then foo ; It is equivalent to using begin and again. : foo ( n -- ) begin dup . 1+ dup 100 = if drop exit then again ; The stuff with evaluate will not work in FlashForth without blowing the return stack. Evaluate and interpret would make calls using the return stack. Does the mutual recursion example really work in Scheme ? BR Mikael On 2021-04-22 16:51, Christopher Howard wrote: > The goal is to make a call to the function /without/ growing the return > stack. An example would be tail call optimization in a language like > scheme. In that specific example, you would be calling the same > function recursively, but the optimization would prevent you from > growing the return stack. It would also be helpful for things like > mutual recursion. E.g., in gforth: > > : foo 124 emit s" bar" evaluate ; > : bar 43 emit s" foo" evaluate ; |