Tuesday 3 October 2017

V4.7 SWITCH CASE BREAK improvements

CASE structures are handy for when a list of values need to be compared against the supplied parameter and perform a specific action if there is a match. 

For instance if we were outputting a character stream to an LCD display we would want to be able to interpret special control characters.Take for instance this cut-down but typical word that detects special control characters otherwise it handles the rest as data. The SWITCH word saves the parameter which all the CASE statements then use to compare to the supplied value. If there is a match it will execute everything up to the BREAK after which it will exit early. Once all the CASE words are exhausted then the parameter is recalled onto the stack again with SWITCH@ and handled as data.

pub LCDEMIT ( ch -- )
    SWITCH
    $0D CASE LCDCR BREAK
    $0A CASE LCDLF BREAK
    $0C CASE LCDCLS BREAK
    SWITCH@ LCDCHAR
    ;


Previously the CASE word was compiling this sequence
SWITCH@ = IF
while BREAK would always compile an EXIT.
Then this was improved by having SWITCH compile
SWITCH= IF
while BREAK would test to see if it could the last word before BREAK into a jump rather than a call since Tachyon V4 wordcode allows for the lsb to be set to indicate a jump thus saving a EXIT in most cases (yeah, that was a pun).

How do we improve this again? This leads up to the next blog.

No comments:

Post a Comment