( COMPATIBLE STRUCTURES--EXAMPLES Version: 1.0.1 File: examples.fs License: LGPL Starting date: July 4, 1994 Last revision: June 14, 2000 ) \ Original source: Copyright (C) 1993 Randolph M. Peters \ Modifications: Copyright (C) 1995 David N. Williams ( 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. Here we reproduce code examples supplied by Peters, adapted to the {}STRUCTOF/ and STRUCTOF{} styles. We have noted elsewhere the similarity of the {}STRUCTOF/ style to his NEW.STRUCT style. There are two demonstration words, DEMO1 and DEMO2, plus two early binding versions of the same words, DEMO1[] and DEMO2[]. Although two distinct structure instances are involved, each does exactly the same thing, namely, prompts for two inputs from the keyboard and then echos them to the screen. Documentation convention: upper case for Forth words in comments. ) cr .( Loading examples, ) unused struct{ 13 chars unstruct first 16 chars unstruct last }struct name.struct struct{ 2 unstruct month 2 unstruct day 2 unstruct year }struct date.struct struct{ name.struct field name date.struct field doa 12 unstruct mrn 64 unstruct precis }struct pt.struct : 0field ( addr size -- ) ( Factor to clear field. These characters get emitted by TYPE, so there is an implementation dependence in what TYPE does with nulls. ) 0 fill ; \ {}STRUCTOF/ style pt.struct {}structof/ }patient : demo1 ( -- ) { first name }patient 2dup 0field cr ." First name? " accept drop { last name }patient 2dup 0field cr ." Last name? " accept drop cr ." Patient Name: " { first name }patient type space { last name }patient type ; : demo1[] ( -- ) [ { first name }patient ]&/ 2dup 0field cr ." First name? " accept drop [ { last name }patient ]&/ 2dup 0field cr ." Last name? " accept drop cr ." Patient Name: " [ { first name }patient ]&/ type space [ { last name }patient ]&/ type ; \ STRUCTOF{} style pt.struct structof{} patient{ : demo2 ( -- ) patient{ name first }&/ 2dup 0field cr ." First name? " accept ( #received) drop patient{ name last }&/ 2dup 0field cr ." Last name? " accept drop cr ." Patient Name: " patient{ name first }&/ type space patient{ name last }&/ type ; : }]&/ ( id_1 ... id_m -- sfa sfsz ) \ interpret, uses stype, sda ( sfa sfsz -- ) \ compile ( -- sfa sfsz ) \ run }&/ ]&/ ; : demo2[] ( -- ) [ patient{ name first }]&/ 2dup 0field cr ." First name? " accept drop [ patient{ name last }]&/ 2dup 0field cr ." Last name? " accept drop cr ." Patient Name: " [ patient{ name first }]&/ type space [ patient{ name last }]&/ type ; unused - . .( bytes.) cr .( Done.) cr .( Execute the following words: demo1 demo2 demo1[] demo2[])