john@aplcen.apl.jhu.edu (John Hayes) (09/06/90)
Recently John Wavrik asked if it will be possible to write significant programs in ANS Forth. My answer is "yes". As a committee member, this is certainly one of my goals. Towards that end, a new word was added (to the Core Extensions Word Set) at the Vancouver meeting. This new word, named START: (start- colon), creates an execution token. The glossary entry is: ------------------------------------------------------------------------ START: "start-colon" CORE EXT ( -- w) START: creates an execution token. Information is added to the end of the dictionary so that code compiled at the next dictionary location will be associated with w. This code can be executed later by using w EXECUTE. ------------------------------------------------------------------------ In other words, START: lets the programmer compile headerless code fragments, store them in data structures, and execute them later. Until now, the only way to compile Forth code was to use the syntax of : (colon): : <name> ... ; START: gives the programmer greater control of the compiler. The programmer now has more options for designing application specific programming languages. Let's look at an example suggested by Leonard Zettel. Leonard wanted to know how to portably implement a word called ME that returned the execution token of the currently executing word. The application of such a word is in event driven simulation. An executing word can add itself to an event queue to execute again at a certain time. ME cannot be implemented to work with : (colon). But with START: we have the tools to build a variant of : (colon): variable last-token : me last-token @ postpone literal ; immediate : sim: create here 1 cells allot ( allocate space for token ) start: dup last-token ! ( make token and record ) swap ! ] ( get ready to compile ) does> @ ; ( default: return token ) : ;sim postpone exit postpone [ ; immediate Typical usage of sim: might be: sim: checkthat ... if me 5 seconds queue-event then ... ;sim In terms of common practice, the functionality of START: has been in use for years. Advanced Forth programmers can implement START: on their systems by exploiting intimate knowledge of how the system works. However, on all Forth systems the implementation of START: will be different. For the first time, the function of START: has a standard name and can be used in portable programs. In my opinion, we have doubled ANS Forth's expressive power in one stroke. START: is an indispensable tool for writing application specific programming languages, ostensibly one of Forth's great strengths. In examining all the major programs I have written in Forth in the last eight years, I found that they all used the equivalent of START:, frequently as a cornerstone of the application. In a subsequent posting I will describe some of these examples. I want to encourage everyone to implement START: and work with it. I especially want to encourage vendors to provide this word. START: is in the Core Extensions, not Core (I proposed that it reside in Core). Any comments? John R. Hayes Applied Physics Laboratory Johns Hopkins University
andrew@idacom.uucp (Andrew Scott) (09/08/90)
John Hayes writes: > This new word, named START: (start-colon), creates an execution token. ... > I want to encourage everyone to implement START: and work with it. > Any comments? I like the word - I've used a similar construct many times before. I have one question, though. It's not stated in your description and example, but it is implied that ; cannot be used to end a code fragment started with START: . You created ;sim in your example: > : ;sim postpone exit postpone [ ; immediate Am I correct in drawing this conclusion? -- Andrew Scott | mail: andrew@idacom.uucp | - or - ..!uunet!ubc-cs!alberta!idacom!andrew
john@aplcen.apl.jhu.edu (John Hayes) (09/12/90)
Andrew Scott writes on START: > I like the word - I've used a similar construct many times before. I have > one question, though. It's not stated in your description and example, but > it is implied that ; cannot be used to end a code fragment started with > START: . You created ;sim in your example: > > > : ;sim postpone exit postpone [ ; immediate > > Am I correct in drawing this conclusion? There are two reasons why ; cannot be used with START:. First, ; expects a compile-time argument, a sys, to be present. This sys is provided by : (colon). People who do syntax checking in their compilers use this to pass a "magic cookie" from : to ; so that ; can check that it is paired with a :. START: could be specified to deliver this "magic cookie". Since the size of a sys is implementation defined, the sys would have to be beneath the execution token on the stack: START: ( --- sys w ) A second reason that ; cannot be used with : is that ; "makes the most recent dictionary entry findable". Since START: doen't make a header, ; unsmudges an earlier header. This is probably harmless, but it is kludgy. John R. Hayes john@aplcen.apl.jhu.edu Applied Physics Laboratory Johns Hopkins University