\ POCKET FORTH: CASE...ENDCASE \ \ Copyright (c) 1993 Randolph M. Peters \ \ This library is free software; you can redistribute it and/or modify it \ under the terms of the GNU Lesser General Public License as published by \ the Free Software Foundation; either version 2.1 of the License, or (at \ your option) any later version. \ \ This library is distributed in the hope that it will be useful, but \ WITHOUT ANY WARRANTY; without even the implied warranty of \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser \ General Public License for more details. \ \ You should have received a copy of the GNU Lesser General Public License \ along with this library; if not, write to the Free Software Foundation, \ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. \ \ \ 22 Oct 93 14:49:04 GMT \ University of Pennsylvania \ \ \ Newsgroups: comp.lang.forth[1] \ \ \ What follows is code I have cobbled together to create a \ case construct in pocket forth. I have found several instances \ where it would be useful to have a multi-branching if, and so \ I made this one up. Since I haven't seen the FAQ on case \ endcase recently, I am providing this for the benefit of the \ pocket forthers out there. \ : case 0 ; immediate : of [ ' over literal ] compile [ ' = literal ] compile [compile] if ; immediate : endof [compile] else ; immediate : otherwise ; immediate ( syntactic sugar) : endcase begin ?dup while [compile] then repeat [ ' drop literal ] compile ; immediate ( example of its use ) 123 constant SPADE 234 constant HEART 345 constant DIAMOND 456 constant CLUB : ?cardtype ( n -- ) case spade of ." It's a spade" cr endof heart of ." It's a heart" cr endof diamond of ." It's a diamond" cr endof club of ." It's a club" cr endof otherwise ." I don't recognize this suit." endcase ; 234 ?cardtype \ I found this useful in several cases, so please, no flames about \ the purity or appropriateness of case...endcase in forth. \ \ Randolph M. Peters, M.D. \ West Mifflin Primary Care Center \ 1907 Lebanon Church Road \ West Mifflin, PA 15122 \ (412) 650-2370 \ qwerty1965@aol.com \ June, 2000