=?windows-1255?Q?Re=3A_=5Bcolorforth=5D_if_then_else_-?= =?windows-1255?Q?_but_why_is_there_no_=91else=92_in_colo?= =?windows-1255?Q?rForth=3F?=

Jason Kemp <[email protected]> Tue, 21 Oct 2008 22:36:04 +0100
Newsgroups gmane.comp.lang.forth.colorforth
Message-ID <[email protected]>
Hi Nick,

( n)  : word1 dup ( nn) -1 + drop ( n) 0if ( n,f) drop 1 ( no ";" ) then ( n/1) dup ( nn/11) -2 + drop 2 then (still no ";") 3 + ; 


But each ג€˜thenג€™ has to match an ג€˜ifג€™ so this canג€™t work can it?

ג€˜ifג€™ and ג€˜thenג€™ are macros.  ג€˜ifג€™ compiles a JZ into the dictionary, 
leaving space for the offset byte (you knowג€”the byte telling it how far 
to jump) and using ג€˜hereג€™ to put the next free dictionary address on the 
stack (ie into eax).  ג€˜thenג€™ first copies the top of return stack to 
list (donג€™t know whyג€”for later optimisation?) Next it calculates the 
offset to this location (H) (ie where the ג€˜thenג€™ is) from the ג€˜ifג€™ (the 
next location after which is held in eax) and places this offset in the 
empty byte following the ג€˜JZג€™ of the ג€˜ifג€™.

It's just as hard to explain it as Iג€™m sure it is to read!

 From the kernel:-
then:
        mov [list], esp ;esp is byte pointer to TOR
        mov edx, [H]    ;H is the pointer to the end of the code space
        sub edx, eax     ; edx := edx-eax offset for if to jump
        mov [eax-1], dl  ; location to store offset (directly after jz)
        DROP             ; location after if
        ret


 From Block 24
 macro 
 if 74h 2, here ; 
 comment: jz, flags set, max 127 bytes, leave address 

So I can create an ג€˜elseג€™ macro as:
: else eb 2, here swap then ;

eb is dark green, then is cyan.  eb is the opcode for JMP rel8. This can 
then be used:

: test w1 if w2 else w3 then w4 ;

w1 goes first, then either w2 is executed and then w4 or w3 and then 
w4.  This seems to work, but there must be a reason why Mr Moore didn't 
put an ג€˜elseג€™ in.  Any idea why?  Is it optimisation?  Does having to 
use two lines make it more readable or better factored?

Jason