Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!willett!ForthNet From: ForthNet@willett.UUCP (ForthNet articles from GEnie) Newsgroups: comp.lang.forth Subject: Data Structures Message-ID: <572.UUL1.3#5129@willett.UUCP> Date: 27 Feb 90 23:55:15 GMT Organization: Latest link in the ForthNet chain. (Pgh, PA) Lines: 54 Date: 02-26-90 (08:06) Number: 2973 (Echo) To: IAN GREEN Refer#: NONE From: JACK WOEHR Read: NO Subj: FORTH DATA STRUCTURES Status: PUBLIC MESSAGE > > How about declaring data structures like an array, or a queue. A >stack is automatic but I am unsure how to implement these other >elementary data structures. > Ian, the CREATE ... DOES> syntax is Forth's way of declaring a custom object. Anything created thusly pushes its data address to the stack when executed, then performs the associated DOES> code. : ARRAY ( #entries ---) \ this "defining" word will create arrays CREATE ALLOT \ allot { #entries } bytes DOES> ( index --- addr) \ which at runtime self-indexes + ; 20 ARRAY FOO ok 0 FOO . 1234 ok 1 FOO . 1235 ok ... etc. Note there are no limit checks in this construct! However, limit checks can be implemented. : CHECKARRAY ( #entries ---) CREATE DUP , ALLOT DOES> ( index --- addr | abort) OVER 0< ABORT" Negative array index!" 2DUP @ >= ABORT" Index out of range!" CELL + + ; 4 CHECKARRAY FOO ok 0 FOO . 1234 ok 1 FOO . 1235 ok -1 FOO . Negative array index! 4 FOO . Index out of range! For a more elaborate example, a self-indexing n-dimensional array, see DIMARRAY.ARC on most of these fine ForthNet stations. =jax= NET/Mail : RCFB Golden, CO (303) 278-0364 VESTA & Denver FIG for Forth! ----- This message came from GEnie via willett through a semi-automated process. Report problems to: 'uunet!willett!dwp' or 'willett!dwp@gateway.sei.cmu.edu'