greid@adobe.com (Glenn Reid) (03/24/89)
Several people have asked me, over the years, how to do something like
a "case" or "switch") statement in PostScript. Here is a procedure
that lets you get most of what you need out of the "switch" statement,
leaving complications like fall-through up to you. I hope somebody
finds it useful.
Glenn Reid
Adobe Systems
Developer Tools & Strategies
%!PS-Adobe-2.1
%%Title: switch.ps
%%CreationDate: Thu Mar 23 13:18:36 1989
%%Creator: Glenn Reid, Adobe Systems
%%EndComments
% "switch" behaves sort of like the C "switch" language construct.
% It is up to the programmer to build "procs" to contain the
% various procedures that should be executed and the values to trigger
% them. "fall-through" is similarly up to the programmer, as in the
% examples.
% The implementation is done with a dictionary of procedures, rather
% than attempting to use "ifelse". It can be adapted not to pass the
% dictionary if only one is needed (but loses generality and nestability).
% dictionary any "switch" -
/switch { %def
2 copy known { %ifelse
get exec
}{ %else
exch /default get exec % leave unknown val on stack for "default"
} ifelse
} bind def
/procs 12 dict begin
0 { (zero found) == } def
1 { (one found) == } def
2 { %def
(two found, and ) print
procs 3 get exec % falls through to 3
} def
3 { (three found) == } def
4 3 load def % 4 also falls through to 3
/default { %def
save exch 128 string cvs print restore
(: not found in switch statement; default used.) =
} bind def
currentdict end def
%%EndProlog
procs 0 switch
procs 1 switch
procs 2 switch
procs 3 switch
procs 4 switch
procs 5 switch
%%Trailer