esulzner@cadev4.intel.com (Eric Sulzner ~) (06/23/89)
Can CREATE, VARIABLE and such (or equivalent functionality) be used within a colon definition without passing the names to the definitions? (Preferably, is there some place I can put the names and a pointer I can set to make Forth think it's reading the name from the input stream?) As in : EXAMP ( --) CREATE ENTRY 1024 ALLOT ; ( I know that doesn't work). I'd like my program to be able to allocate arrays and create variables based on runtime needs. I'm running FF-Forth on an AT clone. I could get around the problem by preallocating all possible arrays and variables and writing routines to save only the used portions, but it would be cleaner to do it as needed. Eric Sulzner esulzner@cadev4.UUCP esulzner@cadev4.intel.com 54177
jax@well.UUCP (Jack J. Woehr) (06/24/89)
In article <330@mipos3.intel.com> esulzner@cadev4.UUCP (Eric Sulzner ~) writes: >Can CREATE, VARIABLE and such (or equivalent functionality) be used >within a colon definition without passing the names to the >definitions? (Preferably, is there some place I can put the names >and a pointer I can set to make Forth think it's reading the name from >the input stream?) As in : EXAMP ( --) CREATE ENTRY 1024 ALLOT ; ( I >know that doesn't work). > Yes indeed, Eric. First of all, new defining words use CREATE and expect the input buffer to have the name string ready at the time these definers are executed, such as the simple example: : ARRAY ( #entries ---) CREATE ALLOT DOES> ( index --- adr) + ; ... which will, when executed, parse the input stream and create an array of n entries using whatever is the next lexical unit in the input stream for the name header. Now, suppose, as in your example, you wanted to use ARRAY in a colon definition, *and* you wanted at *compile* time ( when you compiled that same colon definition) to provide the name header. This is essentially the same thing as your word EXAMP. What you have to do is save the current input pointer and TIB and substitute new ones! Presuming that your Forth defines TIB as : TIB 'TIB @ ; and uses the variable >IN as the imput pointer within TIB, you could write: : ARRAY-EXAMP ( ---) >IN @ 'TIB @ ( save current input pointer and input buff address) " BLETCH " ( here's your name string ... returns --- addr ct) DROP ( we don't need the count, WORD will get that) 'TIB ! ( store string address as input buffer address!) >IN OFF ( input pointer set to 0, start of string) 4 ARRAY ( create an array of 4 elements called BLETCH) 'TIB ! >IN ! ( restore input buffer and input pointer) ; Note that the string " BLETCH " has a space at the end, to make sure that WORD ( called in CREATE called in ARRAY ) can successfully parse out a standard space-delimited Forth lexical unit. >I'd like my program to be able to allocate arrays and create variables >based on runtime needs. > As you progress in Forth, you will find more sophisticated techniques for heap management than trifling with the dictionary at runtime. Try allocating a large data space and writing some dynamic allocators to handle usage within that data space, it's less problematic than the scheme you envision. {}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{} {} {} {} jax@well ." Sysop, Realtime Control and Forth Board" FIG {} {} jax@chariot ." (303) 278-0364 3/12/2400 8-n-1 24 hrs." Chapter {} {} JAX on GEnie ." Tell them JAX sent you!" Coordinator {} {} {} {}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}