home .. forth .. colorforth mail list archive ..

Re: [colorforth] if then else - but why is there no ‘else’ in colorForth?


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



---------------------------------------------------------------------
To unsubscribe, e-mail: colorforth-unsubscribe@xxxxxxxxxxxxxxxxxx
For additional commands, e-mail: colorforth-help@xxxxxxxxxxxxxxxxxx
Main web page - http://www.colorforth.com