<<<<<<<<<<<<<<<<<

switch(prog_tools) Programming Tools switch(prog_tools)

NAME

switch - performs conditional actions based on value.

DESCRIPTION

The object switch performs conditional execution of LYMB actions based on its value. Switch is quite similar to the "switch/case" statements in the C programming language.

Since switch is a subclass of string, its value can be set any way that a string value can be set, i.e., its value can be set to a string, float, integer, or object pointer. Also, the various format fields and separators can be used as well.

Note: When creating instances of the switch class, particularly in scripts, it is safest to enclose the names in quotes. Switches return their value when an instance is referenced, not the name of the instance.

SUPERCLASS

string

INSTANCE VARIABLES

(none - all inherited)

MESSAGES

=, @, case: value set the current value of the switch. Note that value may be a float, int, string, or object pointer.

actions=, tick_actions= LYMB statements Specify LYMB statements for the current case.

actions+, tick_actions+ LYMB statements Add LYMB statements to the current statements for the current case.

actions?, tick_actions? Get the LYMB statements to execute for the current case.

do!, switch!, tick! Execute the appropriate LYMB statements associated with the current case.

compile_on! Turn on compile mode for actions associated with current case. When compile mode is on, the parser is used only to compile the tick actions and send "tokens" back to the internal actions object which maintains the compiled actions internally. Actions are compiled only when a "tick!", "switch", or

"do!" messsage is sent, an only if the tick_actions have been modified since the last time a compilation was done.

compile_off! Turn off compile mode for actions associated with the current case. At each "tick!", "switch!", or "do!" message, the parser is used to parse and execute the actions associated with this case. If a syntax error occurs during the compilation, this message is automatically issued to ensure that the internal actions object does not attempt to execute a partially generated token sequence.

assume_static_objects_on! Turn on the "assume_static_objects" mode for the actions associated with the current case. When this mode is on, and when compile mode is on, the internal actions object maintains object pointers for all objects referenced in the tick_actions, and method pointers for all messages. This eliminates the overhead associated with looking up identifiers in the object table, and also the overhead of doing msg_sends. (msg_connects are done instead.) If the msg_connect returns SUPERCLASS_RESPONSIBILITY, the actions object handles the propagation correctly itself. This mode will not work in situations where identifiers do not refer to the same underlying object from one invocation to the next, for example, when an object named in the actions is freed and then re-created (as an instance of either the same or a different class).

assume_static_objects_off! Turns off the "assume_static_objects" mode for the actions associated with the current case.

switch: value A convenience routine to set the current value and then execute the case actions.

EXAMPLE

/* * First example switches based on renderer type */ environment new: _env variable="LYMB_RENDERER" ;

switch new: hardcopy_actions @`PHIGS' actions=`#phigs hardcopy' actions+`#phigs bites' @`STARBASE' actions=`#starbase hardcopy' @`VOGLE_X11' actions=`#vogle hardcopy'

@`GL' actions=`#gl hardcopy' @`default' actions=`#no such renderer' ;

hardcopy_actions switch: [_env value?];

/* * Second example uses switch as computed goto */

scalar new: s upper= 1.0 upperon! lower= -1.0 loweron! ;

switch new: goto case: -1 actions=`#case negative' case: 0 actions=`#case zero' case: 1 actions=`#case positive' ;

goto case: [s = 101 integerize! value?] do!; goto case: [s = -9.3 integerize! value?] do!; goto case: [s = 0.8 integerize! value?] do!;

SEE ALSO

string


Please send comments and suggestions to
consult@rpi.edu